Libraryless. Click here for Pure Java version (4420L/28K/96K).
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 S 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 S reproduce(BWImage original) { |
43 | ++n; |
44 | DynamicObject p = new DynamicObject("Solid"); |
45 | if (n % 2 == 0) { |
46 | p.put("col", randomBrightness()); |
47 | } else { |
48 | p.put("col", probeRandomPixel(original)); |
49 | } |
50 | ret structure(p); |
51 | } |
52 | } |
53 | |
54 | static abstract class Params { |
55 | BWImage originalImage; |
56 | abstract BWImage render(); |
57 | abstract Params copy(); |
58 | |
59 | void baseClone(Params p) { |
60 | p.originalImage = originalImage; |
61 | } |
62 | |
63 | BWImage rendered; |
64 | BWImage getImage() { |
65 | if (rendered == null) |
66 | rendered = render(); |
67 | return rendered; |
68 | } |
69 | |
70 | Double score; |
71 | double getScore() { |
72 | if (score == null) |
73 | score = calcScore(); |
74 | return score; |
75 | } |
76 | |
77 | double calcScore() { |
78 | return 100*(1-diff(originalImage, getImage())); |
79 | } |
80 | |
81 | boolean isBetterThan(Params p) { |
82 | return getScore() > p.getScore(); |
83 | } |
84 | } |
85 | |
86 | static S reproduce(BWImage original) { |
87 | ret reproducer.reproduce(original); |
88 | } |
89 | |
90 | static void reproduceOpenEnd(BWImage original, ImageSurface imageSurface) { |
91 | S bestDesc = null; |
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: " + bestDesc); |
106 | System.out.println(s + ", score: " + formatDouble(best.getScore(), 2) + "%, structure size: " + structureSize(best, Params.class)); |
107 | } |
108 | } |
109 | S desc; |
110 | try { |
111 | desc = reproduce(original); |
112 | } catch e { |
113 | print("Production failed: " + exceptionToStringShort(e)); |
114 | continue; |
115 | } |
116 | Params p; |
117 | try { |
118 | p = makeParams(original, desc); |
119 | } catch { |
120 | print("Can't unstructure: " + desc); |
121 | continue; |
122 | } |
123 | if (bestDesc == null || p != null && p.getScore() > best.getScore()) { |
124 | //System.out.println("New best! " + p.getScore()); |
125 | bestDesc = desc; |
126 | best = p; |
127 | imageSurface.setImage(p.getImage().getBufferedImage()); |
128 | } |
129 | |
130 | if (p != null && p.getScore() >= 100.0) |
131 | break; |
132 | } |
133 | } |
134 | |
135 | static Params makeParams(BWImage original, S desc) { |
136 | Params p = cast unstructure(desc); |
137 | p.originalImage = original; |
138 | ret p; |
139 | } |
Began life as a copy of #1004542
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1004544 |
Snippet name: | Reproduce Image 2 (with strings) |
Eternal ID of this version: | #1004544/1 |
Text MD5: | fae0d389988d54faac1451f836efe324 |
Transpilation MD5: | d45ee536430024949ef4e2cbc4493877 |
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 16:10:21 |
Source code size: | 3249 bytes / 139 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 546 / 640 |
Referenced in: | [show references] |