1 | !636 |
2 | !629 // standard functions |
3 | !658 // image classes |
4 | |
5 | import java.awt.*; |
6 | import java.awt.image.*; |
7 | import java.util.List; |
8 | import javax.imageio.*; |
9 | |
10 | public class main { |
11 | static RGBImage originalImage; |
12 | |
13 | static class Params { |
14 | double splitPoint; |
15 | RGB col1, col2; |
16 | |
17 | Params copy() { |
18 | Params p = new Params(); |
19 | p.splitPoint = splitPoint; |
20 | p.col1 = col1; |
21 | p.col2 = col2; |
22 | return p; |
23 | } |
24 | |
25 | RGBImage rendered; |
26 | RGBImage getImage() { |
27 | if (rendered == null) |
28 | rendered = render(); |
29 | return rendered; |
30 | } |
31 | |
32 | RGBImage render() { |
33 | int w = originalImage.getWidth(), h = originalImage.getHeight(); |
34 | RGBImage image = new RGBImage(w, h, Color.white); |
35 | vsplit(image, this); |
36 | return image; |
37 | } |
38 | |
39 | double score = 2.0; |
40 | double getScore() { |
41 | if (score == 2.0) |
42 | score = calcScore(); |
43 | return score; |
44 | } |
45 | |
46 | double calcScore() { |
47 | return diff(originalImage, getImage()); |
48 | } |
49 | |
50 | boolean isBetterThan(Params p) { |
51 | return getScore() < p.getScore(); |
52 | } |
53 | } |
54 | |
55 | interface Reproducer { |
56 | public Params reproduce(RGBImage original); |
57 | } |
58 | |
59 | static class TryRandom implements Reproducer { |
60 | int n = -1; |
61 | public Params reproduce(RGBImage original) { |
62 | ++n; |
63 | Params p = new Params(); |
64 | p.splitPoint = random(); |
65 | if (n % 2 == 0) { |
66 | p.col1 = randomColor(); |
67 | p.col2 = randomColor(); |
68 | } else { |
69 | p.col1 = probeRandomPixel(original); |
70 | p.col2 = probeRandomPixel(original); |
71 | } |
72 | return p; |
73 | } |
74 | } |
75 | |
76 | static class VaryBest implements Reproducer { |
77 | Reproducer base; |
78 | Params best; |
79 | |
80 | VaryBest(Reproducer base) { |
81 | this.base = base; |
82 | } |
83 | |
84 | public Params reproduce(RGBImage original) { |
85 | Params p = base.reproduce(original); |
86 | if (best == null || p.isBetterThan(best)) |
87 | best = p; |
88 | Params variation = vary(best); |
89 | System.out.println("Best: " + best.getScore() + ", variation: " + variation.getScore()); |
90 | if (variation.isBetterThan(best)) { |
91 | System.out.println("Using variation, diff=" + (best.getScore()-variation.getScore())); |
92 | best = variation; |
93 | } |
94 | return best; |
95 | } |
96 | |
97 | Params vary(Params p) { |
98 | Params n = p.copy(); |
99 | int s = random(3); |
100 | if (s == 0) |
101 | varySplitPoint(n); |
102 | else if (s == 1) |
103 | n.col1 = varyColor(n.col1); |
104 | else |
105 | n.col2 = varyColor(n.col2); |
106 | return n; |
107 | } |
108 | |
109 | void varySplitPoint(Params p) { |
110 | p.splitPoint = Math.max(0, Math.min(1, p.splitPoint+random(-0.1, 0.1))); |
111 | } |
112 | |
113 | RGB varyColor(RGB col) { |
114 | return col; // TODO |
115 | } |
116 | } |
117 | |
118 | // main reproduce function |
119 | static Reproducer reproducer = new VaryBest(new TryRandom()); |
120 | static Params reproduce(RGBImage original, int ntry) { |
121 | int w = original.getWidth(), h = original.getHeight(); |
122 | return reproducer.reproduce(original); |
123 | } |
124 | |
125 | public static void main(String[] args) { |
126 | JFrame frame = new JFrame("A JavaX Frame"); |
127 | |
128 | final ImageSurface imageSurface = new ImageSurface(); |
129 | Component panel = imageSurface; |
130 | |
131 | frame.add(panel); |
132 | frame.setBounds(100, 100, 500, 400); |
133 | frame.setVisible(true); |
134 | exitOnFrameClose(frame); |
135 | |
136 | new Thread() { |
137 | public void run() { |
138 | originalImage = loadImage("#1000326"); // Bryan Cranston! |
139 | originalImage = resizeToWidth(originalImage, 100); |
140 | |
141 | reproduceOpenEnd(originalImage, imageSurface); |
142 | } |
143 | }.start(); |
144 | } |
145 | |
146 | static void reproduceOpenEnd(RGBImage original, ImageSurface imageSurface) { |
147 | Params best = null; |
148 | for (int ntry = 1; ; ntry++) { |
149 | System.out.println("Try " + ntry + ", score: " + (best == null ? 2.0 : best.getScore())); |
150 | Params p = reproduce(original, ntry); |
151 | if (best == null || p != null && p.getScore() < best.getScore()) { |
152 | System.out.println("New best! " + p.getScore()); |
153 | best = p; |
154 | imageSurface.setImage(p.getImage()); |
155 | } |
156 | |
157 | if (p != null && p.getScore() == 0.0) |
158 | break; |
159 | } |
160 | } |
161 | |
162 | static RGB probeRandomPixel(RGBImage image) { |
163 | int x = (int) (random()*(image.getWidth()-1)); |
164 | int y = (int) (random()*(image.getHeight()-1)); |
165 | return image.getPixel(x, y); |
166 | } |
167 | |
168 | static RGB randomColor() { |
169 | return new RGB(random(), random(), random()); |
170 | } |
171 | |
172 | static RGBImage resizeToWidth(RGBImage image, int w) { |
173 | return resize(image, w, (int) ((image.getHeight()*(double) w)/image.getWidth())); |
174 | } |
175 | |
176 | public static RGBImage resize(RGBImage image, int w, int h) { |
177 | if (w == image.getWidth() && h == image.getHeight()) return image; |
178 | |
179 | int[] pixels = new int[w*h]; |
180 | for (int y = 0; y < h; y++) |
181 | for (int x = 0; x < w; x++) |
182 | pixels[y*w+x] = image.getInt(x*image.getWidth()/w, y*image.getHeight()/h); |
183 | return new RGBImage(w, h, pixels); |
184 | } |
185 | |
186 | static boolean useImageCache = true; |
187 | static RGBImage loadImage(String snippetID) { |
188 | try { |
189 | File dir = new File(System.getProperty("user.home"), ".tinybrain/image-cache"); |
190 | if (useImageCache) { |
191 | dir.mkdirs(); |
192 | File file = new File(dir, snippetID + ".png"); |
193 | if (file.exists() && file.length() != 0) |
194 | try { |
195 | return new RGBImage(ImageIO.read(file)); |
196 | } catch (Throwable e) { |
197 | e.printStackTrace(); |
198 | // fall back to loading from sourceforge |
199 | } |
200 | } |
201 | |
202 | String imageURL = getImageURL(parseSnippetID(snippetID)); |
203 | System.err.println("Loading image: " + imageURL); |
204 | BufferedImage image = ImageIO.read(new URL(imageURL)); |
205 | |
206 | if (useImageCache) { |
207 | File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis()); |
208 | ImageIO.write(image, "png", tempFile); |
209 | tempFile.renameTo(new File(dir, snippetID + ".png")); |
210 | //Log.info("Cached image."); |
211 | } |
212 | |
213 | //Log.info("Loaded image."); |
214 | return new RGBImage(image); |
215 | } catch (IOException e) { |
216 | throw new RuntimeException(e); |
217 | } |
218 | } |
219 | |
220 | static String getImageURL(long snippetID) throws IOException { |
221 | String url; |
222 | if (snippetID == 1000010 || snippetID == 1000012) |
223 | url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + snippetID; |
224 | else |
225 | url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + snippetID |
226 | + "&contentType=image/png"; |
227 | return url; |
228 | } |
229 | |
230 | public static long parseSnippetID(String snippetID) { |
231 | return Long.parseLong(shortenSnippetID(snippetID)); |
232 | } |
233 | |
234 | private static String shortenSnippetID(String snippetID) { |
235 | if (snippetID.startsWith("#")) |
236 | snippetID = snippetID.substring(1); |
237 | String httpBlaBla = "http://tinybrain.de/"; |
238 | if (snippetID.startsWith(httpBlaBla)) |
239 | snippetID = snippetID.substring(httpBlaBla.length()); |
240 | return snippetID; |
241 | } |
242 | |
243 | static Random _random = new Random(); |
244 | static double random() { |
245 | return _random.nextInt(100001)/100000.0; |
246 | } |
247 | |
248 | static int random(int max) { |
249 | return _random.nextInt(max); |
250 | } |
251 | |
252 | static double random(double min, double max) { |
253 | return min+random()*(max-min); |
254 | } |
255 | |
256 | static void vsplit(RGBImage img, Params p) { |
257 | int w = img.getWidth(), h = img.getHeight(); |
258 | for (int yy = 0; yy < h; yy++) |
259 | for (int xx = 0; xx < w; xx++) { |
260 | double x = ((double) xx)/(w-1); |
261 | double y = ((double) yy)/(h-1); |
262 | RGB col = y <= p.splitPoint ? p.col1 : p.col2; |
263 | img.setPixel(xx, yy, col); |
264 | } |
265 | } |
266 | |
267 | public static double pixelDiff(RGB a, RGB b) { |
268 | return (Math.abs(a.r-b.r) + Math.abs(a.g-b.g) + Math.abs(a.b-b.b))/3; |
269 | } |
270 | |
271 | public static double diff(RGBImage image, RGBImage image2) { |
272 | int w = image.getWidth(), h = image.getHeight(); |
273 | double sum = 0; |
274 | for (int y = 0; y < h; y++) |
275 | for (int x = 0; x < w; x++) |
276 | sum += pixelDiff(image.getRGB(x, y), image2.getRGB(x, y)); |
277 | return sum/(w*h); |
278 | } |
279 | } |
Began life as a copy of #661
download show line numbers debug dex old transpilations
Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, qbtsjoyahagl, tslmcundralx, tvejysmllsmz, vouqrxazstgt, zudvenktlakg
No comments. add comment
Snippet ID: | #663 |
Snippet name: | vsplit v3 (with parameters class) |
Eternal ID of this version: | #663/1 |
Text MD5: | c841a8204826d81523a4a00a831e53fb |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-07-13 06:08:56 |
Source code size: | 8116 bytes / 279 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 1086 / 900 |
Referenced in: | [show references] |