!636 !629 // standard functions !658 // image classes import java.awt.*; import java.awt.image.*; import java.util.List; import javax.imageio.*; public class main { public static void main(String[] args) { JFrame frame = new JFrame("A JavaX Frame"); final ImageSurface imageSurface = new ImageSurface(); Component panel = imageSurface; frame.add(panel); frame.setBounds(100, 100, 500, 400); frame.setVisible(true); exitOnFrameClose(frame); new Thread() { public void run() { RGBImage originalImage = loadImage("#1000326"); // Bryan Cranston! originalImage = resizeToWidth(originalImage, 100); reproduceOpenEnd(originalImage, imageSurface); } }.start(); } static void reproduceOpenEnd(RGBImage original, ImageSurface imageSurface) { double bestScore = 2.0; RGBImage bestImage = null; for (int ntry = 1; ; ntry++) { System.out.println("Try " + ntry + ", score: " + bestScore); RGBImage image = reproduce(original, ntry); double score = diff(original, image); if (score <= bestScore) { System.out.println("New best! " + score); bestScore = score; bestImage = image; imageSurface.setImage(image); } if (score == 0.0) break; } } // main reproduce function static RGBImage reproduce(RGBImage original, int ntry) { int w = original.getWidth(), h = original.getHeight(); int n = ntry % 2; RGBImage image = new RGBImage(w, h, Color.white); double splitPoint = random(); RGB col1 = probeRandomPixel(original); RGB col2 = probeRandomPixel(original); vsplit(image, splitPoint, col1, col2); return image; } static RGB probeRandomPixel(RGBImage image) { int x = (int) (random()*(image.getWidth()-1)); int y = (int) (random()*(image.getHeight()-1)); return image.getPixel(x, y); } static RGBImage resizeToWidth(RGBImage image, int w) { return resize(image, w, (int) ((image.getHeight()*(double) w)/image.getWidth())); } public static RGBImage resize(RGBImage image, int w, int h) { if (w == image.getWidth() && h == image.getHeight()) return image; int[] pixels = new int[w*h]; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) pixels[y*w+x] = image.getInt(x*image.getWidth()/w, y*image.getHeight()/h); return new RGBImage(w, h, pixels); } static boolean useImageCache = true; static RGBImage loadImage(String snippetID) { try { File dir = new File(System.getProperty("user.home"), ".tinybrain/image-cache"); if (useImageCache) { dir.mkdirs(); File file = new File(dir, snippetID + ".png"); if (file.exists() && file.length() != 0) try { return new RGBImage(ImageIO.read(file)); } catch (Throwable e) { e.printStackTrace(); // fall back to loading from sourceforge } } String imageURL = getImageURL(parseSnippetID(snippetID)); System.err.println("Loading image: " + imageURL); BufferedImage image = ImageIO.read(new URL(imageURL)); if (useImageCache) { File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis()); ImageIO.write(image, "png", tempFile); tempFile.renameTo(new File(dir, snippetID + ".png")); //Log.info("Cached image."); } //Log.info("Loaded image."); return new RGBImage(image); } catch (IOException e) { throw new RuntimeException(e); } } static String getImageURL(long snippetID) throws IOException { String url; if (snippetID == 1000010 || snippetID == 1000012) url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + snippetID; else url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + snippetID + "&contentType=image/png"; return url; } public static long parseSnippetID(String snippetID) { return Long.parseLong(shortenSnippetID(snippetID)); } private static String shortenSnippetID(String snippetID) { if (snippetID.startsWith("#")) snippetID = snippetID.substring(1); String httpBlaBla = "http://tinybrain.de/"; if (snippetID.startsWith(httpBlaBla)) snippetID = snippetID.substring(httpBlaBla.length()); return snippetID; } static Random _random = new Random(); static double random() { return _random.nextInt(100001)/100000.0; } static void vsplit(RGBImage img, double splitPoint, RGB col1, RGB col2) { int w = img.getWidth(), h = img.getHeight(); for (int yy = 0; yy < h; yy++) for (int xx = 0; xx < w; xx++) { double x = ((double) xx)/(w-1); double y = ((double) yy)/(h-1); RGB col = y <= splitPoint ? col1 : col2; img.setPixel(xx, yy, col); } } public static double pixelDiff(RGB a, RGB b) { return (Math.abs(a.r-b.r) + Math.abs(a.g-b.g) + Math.abs(a.b-b.b))/3; } public static double diff(RGBImage image, RGBImage image2) { int w = image.getWidth(), h = image.getHeight(); double sum = 0; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) sum += pixelDiff(image.getRGB(x, y), image2.getRGB(x, y)); return sum/(w*h); } }
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: | 775 / 683 |
Referenced in: | [show references] |