import ij.*;
import ij.process.*;
import ij.plugin.PlugIn;

public class Image_To_Tool implements PlugIn {
	
	public void run(String arg) {
		ImagePlus imp = WindowManager.getCurrentImage();
		if (null == imp) return;
		int w = imp.getWidth();
		int h = imp.getHeight();
		if (w > 16) w = 16;
		if (h > 16) h = 16;
		ImageProcessor ip = imp.getProcessor();
		StringBuffer com = new StringBuffer("C000");
		boolean line = false;
		int x,y;
		for (y = 0; y < h; y++) {
			for (x = 0; x < w; x++) {
				//IJ.log(hex(x) + "__" + hex(y));
				int pix = ip.getPixel(x, y);
				if (line) {
					if (0 == pix) {
						continue; // continuing horizontal black line
					} else {
						//finish black line on the previous point
						com.append(hex(x-1)).append(hex(y));
						line = false;
					}
				} else if (0 == pix) {
					// start line
					com.append("L").append(hex(x)).append(hex(y));
					line = true;
				}
			}
			// finish line if any
			if (line) {
				com.append(hex(w-1)).append(hex(y));
				line = false;
			}
		}
		System.out.println(com.toString());
		IJ.log(com.toString());
	}

	private char hex(int p) {
		if (p < 10) return new StringBuffer().append(p).charAt(0); //what a waste, but quick and dirty
		else {
			switch (p) {
				case 10: return 'a';
				case 11: return 'b';
				case 12: return 'c';
				case 13: return 'd';
				case 14: return 'e';
				case 15: return 'f';
			}
		}
		return '0';
	}
}

