Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

119
LINES

< > BotCompany Repo | #1004542 // Reproduce Image (dev.)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (3910L/26K/85K).

!752

static BWImage bw;
static ImageSurface imageSurface;
static Reproducer reproducer;

!include #1000522 // image helper functions

p {
  bw = loadBWImage("#1004541");
  imageSurface = showImage(bw.getBufferedImage());
  reproducer = new RandomSolid;
  reproduceOpenEnd(bw, imageSurface);
}

interface Reproducer {
  public Params reproduce(BWImage original);
}

static class Solid extends Params {
  float col;
  
  Solid copy() {
    new Solid p;
    baseClone(p);
    p.col = col;
    return p;
  }
  
  public String toString() {
    return "Solid " + formatDouble(col, 2);
  }

  BWImage render() {
    int w = originalImage.getWidth(), h = originalImage.getHeight();
    return new BWImage(w, h, col);
  }
}

static class RandomSolid implements Reproducer {
  int n = -1;
  public Params reproduce(BWImage original) {
    ++n;
    Solid p = new Solid();
    p.originalImage = original;
    if (n % 2 == 0) {
      p.col = randomBrightness();
    } else {
      p.col = probeRandomPixel(original);
    }
    return p;
  }
}

static abstract class Params {
  BWImage originalImage;
  abstract BWImage render();
  abstract Params copy();
  
  void baseClone(Params p) {
    p.originalImage = originalImage;
  }
  
  BWImage rendered;
  BWImage getImage() {
    if (rendered == null)
      rendered = render();
    return rendered;
  }
  
  Double score;
  double getScore() {
    if (score == null)
      score = calcScore();
    return score;
  }
  
  double calcScore() {
    return 100*(1-diff(originalImage, getImage()));
  }
  
  boolean isBetterThan(Params p) {
    return getScore() > p.getScore();
  }
}

static Params reproduce(BWImage original) {
  ret reproducer.reproduce(original);
}

static void reproduceOpenEnd(BWImage original, ImageSurface imageSurface) {
  Params best = null;
  long lastPrint = 0, lastN = 0;
  for (long ntry = 1; ; ntry++) {
    ping();
    long now = now();
    if (now >= lastPrint+1000) {
      long tps = (ntry-lastN)*1000/(now-lastPrint);
      lastPrint = now;
      lastN = ntry;
      String s = "Try " + ntry + " (" + tps + "/s)";
      if (best == null)
        System.out.println(s);
      else {
        System.out.println("Best: " + best);
        System.out.println(s + ", score: " + formatDouble(best.getScore(), 2) + "%, structure size: " + structureSize(best, Params.class));
      }
    }
    Params p = reproduce(original);
    if (best == null || p != null && p.getScore() > best.getScore()) {
      //System.out.println("New best! " + p.getScore());
      best = p;
      imageSurface.setImage(p.getImage().getBufferedImage());
    }
    
    if (p != null && p.getScore() >= 100.0)
      break;
  }
}

Author comment

Began life as a copy of #1004535

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004542
Snippet name: Reproduce Image (dev.)
Eternal ID of this version: #1004542/1
Text MD5: bdc572ff14bee52f5117a4d84d583dfb
Transpilation MD5: 1cfb0b1f2e59aa2a41449bb456f9e0fe
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-20 15:52:42
Source code size: 2784 bytes / 119 lines
Pitched / IR pitched: No / No
Views / Downloads: 532 / 610
Referenced in: #1004544 - Reproduce Image 2 (with strings)