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 | public static void main(String[] args) { |
12 | JFrame frame = new JFrame("A JavaX Frame"); |
13 | |
14 | final ImageSurface imageSurface = new ImageSurface(); |
15 | Component panel = imageSurface; |
16 | |
17 | frame.add(panel); |
18 | frame.setBounds(100, 100, 500, 400); |
19 | frame.setVisible(true); |
20 | exitOnFrameClose(frame); |
21 | |
22 | new Thread() { |
23 | public void run() { |
24 | RGBImage originalImage = loadImage("#1000326"); // Bryan Cranston! |
25 | originalImage = resizeToWidth(originalImage, 100); |
26 | |
27 | reproduceOpenEnd(originalImage, imageSurface); |
28 | } |
29 | }.start(); |
30 | } |
31 | |
32 | static void reproduceOpenEnd(RGBImage original, ImageSurface imageSurface) { |
33 | double bestScore = 2.0; |
34 | RGBImage bestImage = null; |
35 | for (int ntry = 1; ; ntry++) { |
36 | System.out.println("Try " + ntry + ", score: " + bestScore); |
37 | RGBImage image = reproduce(original, ntry); |
38 | double score = diff(original, image); |
39 | if (score <= bestScore) { |
40 | System.out.println("New best! " + score); |
41 | bestScore = score; |
42 | bestImage = image; |
43 | imageSurface.setImage(image); |
44 | } |
45 | |
46 | if (score == 0.0) |
47 | break; |
48 | } |
49 | } |
50 | |
51 | // main reproduce function |
52 | static RGBImage reproduce(RGBImage original, int ntry) { |
53 | int w = original.getWidth(), h = original.getHeight(); |
54 | int n = ntry % 2; |
55 | RGBImage image = new RGBImage(w, h, Color.white); |
56 | double splitPoint = random(); |
57 | RGB col1 = probeRandomPixel(original); |
58 | RGB col2 = probeRandomPixel(original); |
59 | vsplit(image, splitPoint, col1, col2); |
60 | return image; |
61 | } |
62 | |
63 | static RGB probeRandomPixel(RGBImage image) { |
64 | int x = (int) (random()*(image.getWidth()-1)); |
65 | int y = (int) (random()*(image.getHeight()-1)); |
66 | return image.getPixel(x, y); |
67 | } |
68 | |
69 | static RGBImage resizeToWidth(RGBImage image, int w) { |
70 | return resize(image, w, (int) ((image.getHeight()*(double) w)/image.getWidth())); |
71 | } |
72 | |
73 | public static RGBImage resize(RGBImage image, int w, int h) { |
74 | if (w == image.getWidth() && h == image.getHeight()) return image; |
75 | |
76 | int[] pixels = new int[w*h]; |
77 | for (int y = 0; y < h; y++) |
78 | for (int x = 0; x < w; x++) |
79 | pixels[y*w+x] = image.getInt(x*image.getWidth()/w, y*image.getHeight()/h); |
80 | return new RGBImage(w, h, pixels); |
81 | } |
82 | |
83 | static boolean useImageCache = true; |
84 | static RGBImage loadImage(String snippetID) { |
85 | try { |
86 | File dir = new File(System.getProperty("user.home"), ".tinybrain/image-cache"); |
87 | if (useImageCache) { |
88 | dir.mkdirs(); |
89 | File file = new File(dir, snippetID + ".png"); |
90 | if (file.exists() && file.length() != 0) |
91 | try { |
92 | return new RGBImage(ImageIO.read(file)); |
93 | } catch (Throwable e) { |
94 | e.printStackTrace(); |
95 | // fall back to loading from sourceforge |
96 | } |
97 | } |
98 | |
99 | String imageURL = getImageURL(parseSnippetID(snippetID)); |
100 | System.err.println("Loading image: " + imageURL); |
101 | BufferedImage image = ImageIO.read(new URL(imageURL)); |
102 | |
103 | if (useImageCache) { |
104 | File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis()); |
105 | ImageIO.write(image, "png", tempFile); |
106 | tempFile.renameTo(new File(dir, snippetID + ".png")); |
107 | //Log.info("Cached image."); |
108 | } |
109 | |
110 | //Log.info("Loaded image."); |
111 | return new RGBImage(image); |
112 | } catch (IOException e) { |
113 | throw new RuntimeException(e); |
114 | } |
115 | } |
116 | |
117 | static String getImageURL(long snippetID) throws IOException { |
118 | String url; |
119 | if (snippetID == 1000010 || snippetID == 1000012) |
120 | url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + snippetID; |
121 | else |
122 | url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + snippetID |
123 | + "&contentType=image/png"; |
124 | return url; |
125 | } |
126 | |
127 | public static long parseSnippetID(String snippetID) { |
128 | return Long.parseLong(shortenSnippetID(snippetID)); |
129 | } |
130 | |
131 | private static String shortenSnippetID(String snippetID) { |
132 | if (snippetID.startsWith("#")) |
133 | snippetID = snippetID.substring(1); |
134 | String httpBlaBla = "http://tinybrain.de/"; |
135 | if (snippetID.startsWith(httpBlaBla)) |
136 | snippetID = snippetID.substring(httpBlaBla.length()); |
137 | return snippetID; |
138 | } |
139 | |
140 | static Random _random = new Random(); |
141 | static double random() { |
142 | return _random.nextInt(100001)/100000.0; |
143 | } |
144 | |
145 | static void vsplit(RGBImage img, double splitPoint, RGB col1, RGB col2) { |
146 | int w = img.getWidth(), h = img.getHeight(); |
147 | for (int yy = 0; yy < h; yy++) |
148 | for (int xx = 0; xx < w; xx++) { |
149 | double x = ((double) xx)/(w-1); |
150 | double y = ((double) yy)/(h-1); |
151 | RGB col = y <= splitPoint ? col1 : col2; |
152 | img.setPixel(xx, yy, col); |
153 | } |
154 | } |
155 | |
156 | public static double pixelDiff(RGB a, RGB b) { |
157 | return (Math.abs(a.r-b.r) + Math.abs(a.g-b.g) + Math.abs(a.b-b.b))/3; |
158 | } |
159 | |
160 | public static double diff(RGBImage image, RGBImage image2) { |
161 | int w = image.getWidth(), h = image.getHeight(); |
162 | double sum = 0; |
163 | for (int y = 0; y < h; y++) |
164 | for (int x = 0; x < w; x++) |
165 | sum += pixelDiff(image.getRGB(x, y), image2.getRGB(x, y)); |
166 | return sum/(w*h); |
167 | } |
168 | } |
Began life as a copy of #660
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, qbtsjoyahagl, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #661 |
Snippet name: | vsplit v2 (works on an image) |
Eternal ID of this version: | #661/1 |
Text MD5: | 234468405be464afca13dec0b7196fd3 |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-07-13 05:37:58 |
Source code size: | 5448 bytes / 168 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 777 / 686 |
Referenced in: | [show references] |