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).

1  
!752
2  
3  
static BWImage bw;
4  
static ImageSurface imageSurface;
5  
static Reproducer reproducer;
6  
7  
!include #1000522 // image helper functions
8  
9  
p {
10  
  bw = loadBWImage("#1004541");
11  
  imageSurface = showImage(bw.getBufferedImage());
12  
  reproducer = new RandomSolid;
13  
  reproduceOpenEnd(bw, imageSurface);
14  
}
15  
16  
interface Reproducer {
17  
  public Params reproduce(BWImage original);
18  
}
19  
20  
static class Solid extends Params {
21  
  float col;
22  
  
23  
  Solid copy() {
24  
    new Solid p;
25  
    baseClone(p);
26  
    p.col = col;
27  
    return p;
28  
  }
29  
  
30  
  public String toString() {
31  
    return "Solid " + formatDouble(col, 2);
32  
  }
33  
34  
  BWImage render() {
35  
    int w = originalImage.getWidth(), h = originalImage.getHeight();
36  
    return new BWImage(w, h, col);
37  
  }
38  
}
39  
40  
static class RandomSolid implements Reproducer {
41  
  int n = -1;
42  
  public Params reproduce(BWImage original) {
43  
    ++n;
44  
    Solid p = new Solid();
45  
    p.originalImage = original;
46  
    if (n % 2 == 0) {
47  
      p.col = randomBrightness();
48  
    } else {
49  
      p.col = probeRandomPixel(original);
50  
    }
51  
    return p;
52  
  }
53  
}
54  
55  
static abstract class Params {
56  
  BWImage originalImage;
57  
  abstract BWImage render();
58  
  abstract Params copy();
59  
  
60  
  void baseClone(Params p) {
61  
    p.originalImage = originalImage;
62  
  }
63  
  
64  
  BWImage rendered;
65  
  BWImage getImage() {
66  
    if (rendered == null)
67  
      rendered = render();
68  
    return rendered;
69  
  }
70  
  
71  
  Double score;
72  
  double getScore() {
73  
    if (score == null)
74  
      score = calcScore();
75  
    return score;
76  
  }
77  
  
78  
  double calcScore() {
79  
    return 100*(1-diff(originalImage, getImage()));
80  
  }
81  
  
82  
  boolean isBetterThan(Params p) {
83  
    return getScore() > p.getScore();
84  
  }
85  
}
86  
87  
static Params reproduce(BWImage original) {
88  
  ret reproducer.reproduce(original);
89  
}
90  
91  
static void reproduceOpenEnd(BWImage original, ImageSurface imageSurface) {
92  
  Params best = null;
93  
  long lastPrint = 0, lastN = 0;
94  
  for (long ntry = 1; ; ntry++) {
95  
    ping();
96  
    long now = now();
97  
    if (now >= lastPrint+1000) {
98  
      long tps = (ntry-lastN)*1000/(now-lastPrint);
99  
      lastPrint = now;
100  
      lastN = ntry;
101  
      String s = "Try " + ntry + " (" + tps + "/s)";
102  
      if (best == null)
103  
        System.out.println(s);
104  
      else {
105  
        System.out.println("Best: " + best);
106  
        System.out.println(s + ", score: " + formatDouble(best.getScore(), 2) + "%, structure size: " + structureSize(best, Params.class));
107  
      }
108  
    }
109  
    Params p = reproduce(original);
110  
    if (best == null || p != null && p.getScore() > best.getScore()) {
111  
      //System.out.println("New best! " + p.getScore());
112  
      best = p;
113  
      imageSurface.setImage(p.getImage().getBufferedImage());
114  
    }
115  
    
116  
    if (p != null && p.getScore() >= 100.0)
117  
      break;
118  
  }
119  
}

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: 531 / 610
Referenced in: [show references]