!636 !629 // standard functions !658 // image classes import java.awt.*; import java.awt.image.*; import java.util.List; import javax.imageio.*; public class main { static RGBImage originalImage; static class Params { double splitPoint; RGB col1, col2; Params copy() { Params p = new Params(); p.splitPoint = splitPoint; p.col1 = col1; p.col2 = col2; return p; } RGBImage rendered; RGBImage getImage() { if (rendered == null) rendered = render(); return rendered; } RGBImage render() { int w = originalImage.getWidth(), h = originalImage.getHeight(); RGBImage image = new RGBImage(w, h, Color.white); vsplit(image, this); return image; } double score = 2.0; double getScore() { if (score == 2.0) score = calcScore(); return score; } double calcScore() { return diff(originalImage, getImage()); } boolean isBetterThan(Params p) { return getScore() < p.getScore(); } } interface Reproducer { public Params reproduce(RGBImage original); } static class TryRandom implements Reproducer { int n = -1; public Params reproduce(RGBImage original) { ++n; Params p = new Params(); p.splitPoint = random(); if (n % 2 == 0) { p.col1 = randomColor(); p.col2 = randomColor(); } else { p.col1 = probeRandomPixel(original); p.col2 = probeRandomPixel(original); } return p; } } static class VaryBest implements Reproducer { Reproducer base; Params best; VaryBest(Reproducer base) { this.base = base; } public Params reproduce(RGBImage original) { Params p = base.reproduce(original); if (best == null || p.isBetterThan(best)) best = p; Params variation = vary(best); System.out.println("Best: " + best.getScore() + ", variation: " + variation.getScore()); if (variation.isBetterThan(best)) { System.out.println("Using variation, diff=" + (best.getScore()-variation.getScore())); best = variation; } return best; } Params vary(Params p) { Params n = p.copy(); int s = random(3); if (s == 0) varySplitPoint(n); else if (s == 1) n.col1 = varyColor(n.col1); else n.col2 = varyColor(n.col2); return n; } void varySplitPoint(Params p) { p.splitPoint = Math.max(0, Math.min(1, p.splitPoint+random(-0.1, 0.1))); } RGB varyColor(RGB col) { return col; // TODO } } // main reproduce function static Reproducer reproducer = new VaryBest(new TryRandom()); static Params reproduce(RGBImage original, int ntry) { int w = original.getWidth(), h = original.getHeight(); return reproducer.reproduce(original); } 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() { originalImage = loadImage("#1000326"); // Bryan Cranston! originalImage = resizeToWidth(originalImage, 100); reproduceOpenEnd(originalImage, imageSurface); } }.start(); } static void reproduceOpenEnd(RGBImage original, ImageSurface imageSurface) { Params best = null; for (int ntry = 1; ; ntry++) { System.out.println("Try " + ntry + ", score: " + (best == null ? 2.0 : best.getScore())); Params p = reproduce(original, ntry); if (best == null || p != null && p.getScore() < best.getScore()) { System.out.println("New best! " + p.getScore()); best = p; imageSurface.setImage(p.getImage()); } if (p != null && p.getScore() == 0.0) break; } } 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 RGB randomColor() { return new RGB(random(), random(), random()); } 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 int random(int max) { return _random.nextInt(max); } static double random(double min, double max) { return min+random()*(max-min); } static void vsplit(RGBImage img, Params p) { 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 <= p.splitPoint ? p.col1 : p.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 #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: | 1001 / 802 |
Referenced in: | [show references] |