/* * Copyright 2006 Jeffrey Palm */ import java.awt.*; import java.awt.image.*; import java.util.*; import java.util.List; import javax.swing.*; public final class ToHtml5 { public static void main(String[] args) { if (args == null || args.length == 0) { args = new String[]{"darth.jpg"}; } try { new ToHtml5().realMain(args); } catch (Exception e) {e.printStackTrace();} } void realMain(String[] args) throws Exception { for (String arg : args) convert(arg); } private void convert(String image) throws Exception { // Create an image and get the array of pixels Image img = Toolkit.getDefaultToolkit().createImage(image); JButton obs = new JButton(new ImageIcon(img)); final int w = img.getWidth(obs); final int h = img.getHeight(obs); // Get the pixels and analyze them int[] pixels = new int[w*h]; PixelGrabber pg = new PixelGrabber(img,0,0,w,h,pixels,0,w); try {pg.grabPixels();} catch (Exception e) {e.printStackTrace();} if ((pg.getStatus() & ImageObserver.ABORT) != 0) return; // Map the colors to list of coordinates class MyColor { final int a,r,g,b; MyColor(int a, int r, int g, int b) { this.a = a; final int A = 25; this.r = round(r,A); this.b = round(b,A); this.g = round(g,A); } public int hashCode() { return (r<<16) + (g<<8) + b; } public boolean equals(MyColor that) { return this.r == that.r && this.g == that.g && this.b == that.b; } public boolean equals(Object that) { return equals((MyColor)that); } private int round(int x, int amnt) { return x/amnt * amnt; } } final Map> color2points = new HashMap>(); // Analyze the pixels for (int i=0; i> 24) & 0xff; int r = (pixel >> 16) & 0xff; int g = (pixel >> 8) & 0xff; int b = (pixel ) & 0xff; MyColor c = new MyColor(a,r,g,b); Point p = new Point(j,i); List points = color2points.get(c); if (points == null) { color2points.put(c,points = new ArrayList()); } points.add(p); } System.err.println(String.format("%.2f",100*(float)i/(float)h) + "%"); } abstract class Drawer { abstract void draw(MyColor c, List points); void start() {} void end() {} } class Drawer1 extends Drawer { void draw(MyColor c, List points) { out("ctx.fillStyle = 'rgba(" + c.r + "," + c.g + "," + c.b + "," + c.a + ")';"); for (Point p : points) { out("ctx.fillRect(" + p.x + "," + p.y + ",1,1);"); } } } class BlurDrawer extends Drawer { private final int radius; BlurDrawer(int radius) {this.radius = radius;} final void draw(MyColor c, List points) { out("ctx.fillStyle = 'rgba(" + c.r + "," + c.g + "," + c.b + ",0.5)';"); for (Point p : points) { out("ctx.fillRect(" + p.x + "," + p.y + "," + radius + "," + radius + ");"); } } } class DrawerArcs extends Drawer { void draw(MyColor c, List points) { out("ctx.fillStyle = 'rgba(" + c.r + "," + c.g + "," + c.b + ",0.1)';"); Point pnt; pnt = points.get(0); out("ctx.beginPath();"); out("ctx.moveTo(" + pnt.x + "," + pnt.y + ");"); for(Point p : points) { out("ctx.lineTo(" + p.x + "," + p.y + ");"); } out("ctx.fill();"); } } final Drawer d = new DrawerArcs(); // Print out the html out(""); out(""); out(""); out(""); out(""); out(""); out(""); out(""); out(""); } private void out(String msg) { System.out.println(msg); } private void note(Object msg) { System.err.println(msg); } private String hex(int i) { return Integer.toHexString(i); } }