import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.Timer;
import javax.swing.Timer;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
public class main {
public static void main(final String[] args) throws Exception {
RGBImage rgb = loadImage("#1006358");
int w = rgb.w(), h = rgb.h();
float[] data = new float[h];
for (int y = 0; y < h; y++)
data[y] = rangeLength(printStruct(wideBlack(rgb, w/2, y)));
showImageWithVerticalGraph(rgb, data);
}
static Range wideBlack(RGBImage img, int x, int y) {
int w = img.w();
int x1 = x, x2 = x;
while (x1 > 0 && img.getPixel(x1-1, y).getBrightness() < 0.1) --x1;
while (x2 < w && img.getPixel(x2, y).getBrightness() < 0.1) ++x2;
return new Range(x1, x2);
}
static A printStruct(String prefix, A a) {
printStructure(prefix, a);
return a;
}
static A printStruct(A a) {
printStructure(a);
return a;
}
static ImageSurface showImageWithVerticalGraph(RGBImage img, float[] data) {
rgbMakeGraph_Info info = new rgbMakeGraph_Info();
ImageSurface is = showImage(mergeImagesHorizontally(img, rgbTurnLeft(rgbMakeGraph(reverseFloatArray(data), info))));
return setToolTipText(is, info);
}
static float rangeLength(Range r) {
return r == null ? 0 : r.length();
}
static RGBImage loadImage(String snippetIDOrURL) {
return new RGBImage(loadBufferedImage(snippetIDOrURL));
}
static A setToolTipText(A c, Object toolTip) {
if (c == null) return null;
c.setToolTipText(str(toolTip));
return c;
}
static class rgbMakeGraph_Info {
// input
float overdrive = 1f;
// output
float min, max;
public String toString() {
return "min: " + min + ", max: " + max;
}
}
static RGBImage rgbMakeGraph(float[] data) {
return rgbMakeGraph(data, null);
}
static RGBImage rgbMakeGraph(float[] data, rgbMakeGraph_Info info) {
if (empty(data)) return new RGBImage(1, 1);
float min = min(data), max = max(min+0.0001f, max(data));
if (info != null) {
info.min = min;
info.max = max;
}
int w = l(data), h = 60;
RGBImage img = new RGBImage(w, h, Color.white);
Color foreground = Color.red;
for (int x = 0; x < w; x++) {
float f = (data[x]-min)/(max-min);
if (info != null) f *= info.overdrive;
int y1 = max(0, iround(h*(1-f)));
for (int y = y1; y < h; y++)
img.setPixel(x, y, foreground);
}
return img;
}
static String showImage_defaultIcon = "#1004230"; // "#1004227";
static ImageSurface showImage(String snippetIDOrURL, String title) {
return showImage(loadImage(snippetIDOrURL), title);
}
static ImageSurface showImage(String title, BufferedImage img) {
return showImage(img, title);
}
static ImageSurface showImage(final BufferedImage img, final String title) {
return (ImageSurface) swing(new Object() { Object get() { try {
ImageSurface is = showImage(img);
getFrame(is).setTitle(title);
return is;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
public String toString() { return "ImageSurface is = showImage(img);\r\n getFrame(is).setTitle(title);\r\n return is;"; }});
}
static ImageSurface showImage(final BufferedImage img) {
return (ImageSurface) swing(new Object() { Object get() { try {
ImageSurface is = new ImageSurface(img);
JFrame frame = showPackedFrame(new JScrollPane(is));
moveToTopRightCorner(frame);
frameIcon(frame, showImage_defaultIcon);
return is;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
public String toString() { return "ImageSurface is = new ImageSurface(img);\r\n JFrame frame = showPackedFrame(new JScrollPane(is));\r\n moveToTopRightCorner(frame);\r\n frameIcon(frame, showImage_defaultIcon);\r\n return is;"; }});
}
static ImageSurface showImage(String imageID) {
return showImage(loadImage(imageID));
}
static ImageSurface showImage(ImageSurface surface, BufferedImage img) {
return showImage(img, surface);
}
static ImageSurface showImage(ImageSurface surface, BufferedImage img, String title) {
return setFrameTitle(showImage(img, surface), title);
}
static ImageSurface showImage(BufferedImage img, ImageSurface surface) {
if (surface == null)
return showImage(img);
else {
surface.setImage(img);
return surface;
}
}
static ImageSurface showImage(String title, RGBImage img) {
return showImage(title, img.getBufferedImage());
}
static ImageSurface showImage(RGBImage img) {
return showImage(img.getBufferedImage());
}
static ImageSurface showImage(RGBImage img, String title) {
ImageSurface is = showImage(img.getBufferedImage());
getFrame(is).setTitle(title);
return is;
}
static ImageSurface showImage(ImageSurface surface, RGBImage img) {
return showImage(img, surface);
}
static ImageSurface showImage(RGBImage img, ImageSurface surface) {
if (surface == null)
return showImage(img);
else {
surface.setImage(img);
return surface;
}
}
static ImageSurface showImage(ImageSurface surface, String title, RGBImage img) {
return showImage(surface, img, title);
}
static ImageSurface showImage(ImageSurface surface, RGBImage img, String title) {
return setFrameTitle(showImage(img, surface), title);
}
static ImageSurface showImage(BWImage img) {
return showImage(img.getBufferedImage());
}
static ImageSurface showImage(Image2B img) {
return showImage(img.getBufferedImage());
}
static boolean loadBufferedImage_useImageCache = true;
static BufferedImage loadBufferedImage(String snippetIDOrURL) { try {
if (snippetIDOrURL == null) return null;
if (isURL(snippetIDOrURL))
return ImageIO.read(new URL(snippetIDOrURL));
if (!isSnippetID(snippetIDOrURL)) throw fail("Not a URL or snippet ID: " + snippetIDOrURL);
String snippetID = "" + parseSnippetID(snippetIDOrURL);
try {
File dir = getCacheProgramDir("Image-Snippets");
if (loadBufferedImage_useImageCache) {
dir.mkdirs();
File file = new File(dir, snippetID + ".png");
if (file.exists() && file.length() != 0)
try {
return ImageIO.read(file);
} catch (Throwable e) {
e.printStackTrace();
// fall back to loading from sourceforge
}
}
String imageURL = snippetImageURL(snippetID);
System.err.println("Loading image: " + imageURL);
BufferedImage image = ImageIO.read(new URL(imageURL));
if (loadBufferedImage_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 image;
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static BufferedImage loadBufferedImage(File file) { try {
return file.isFile() ? ImageIO.read(file) : null;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static RGBImage mergeImagesHorizontally(RGBImage... images) {
return mergeImagesHorizontally(toList(images));
}
static RGBImage mergeImagesHorizontally(List images) {
int spacing = imageMergeSpacing();
RGB spaceColor = new RGB(0.9f);
int w = intSum(images, "width")+(l(images)-1)*spacing, h = intMax(images, "height");
RGBImage out = new RGBImage(w, h, spaceColor);
int x = 0;
for (int i = 0; i < l(images); i++) {
RGBImage img = images.get(i);
int y = (h-img.h())/2;
copyRGBImage(img, 0, 0, out, x, y);
x += img.w() + spacing;
}
return out;
}
static RGBImage rgbTurnLeft(RGBImage img) {
int w = img.w(), h = img.h();
RGBImage r = new RGBImage(h, w);
for (int y = 0; y < w; y++) for (int x = 0; x < h; x++)
r.setPixel(x, y, img.getPixel(w-1-y, x));
return r;
}
static float[] reverseFloatArray(float[] a) {
int n = l(a);
float[] b = new float[n];
for (int i = 0; i < n; i++) b[n-1-i] = a[i];
return b;
}
static void printStructure(String prefix, Object o) {
if (endsWithLetter(prefix)) prefix += ": ";
print(prefix + structure(o));
}
static void printStructure(Object o) {
print(structure(o));
}
static int intMax(Collection c, String field) {
int max = 0;
for (Object o : c)
max = Math.max(max, toInt(getOpt(o, field)));
return max;
}
static JFrame getFrame(Object o) {
if (!(o instanceof Component)) return null;
Component c = (Component) o;
while (c != null) {
if (c instanceof JFrame) return (JFrame) c;
c = c.getParent();
}
return null;
}
static A setFrameTitle(A c, String title) {
JFrame f = getFrame(c);
if (f == null)
showFrame(title, c);
else
f.setTitle(title);
return c;
}
static A setFrameTitle(String title, A c) {
return setFrameTitle(c, title);
}
// magically find a field called "frame" in main class :-)
static JFrame setFrameTitle(String title) {
Object f = getOpt(mc(), "frame");
if (f instanceof JFrame)
return setFrameTitle((JFrame) f, title);
return null;
}
static ArrayList toList(A[] a) {
return asList(a);
}
static ArrayList toList(int[] a) {
return asList(a);
}
static ArrayList toList(Set s) {
return asList(s);
}
static void copyRGBImage(RGBImage src, int srcX, int srcY, RGBImage dst, int dstX, int dstY) {
copyRGBImage(src, srcX, srcY, dst, dstX, dstY, src.getWidth()-srcX, src.getHeight()-srcY);
}
static void copyRGBImage(RGBImage src, int srcX, int srcY, RGBImage dst, int dstX, int dstY, int w, int h) {
w = min(w, dst.getWidth()-dstX);
h = min(h, dst.getHeight()-dstY);
w = min(w, src.getWidth()-srcX);
h = min(h, src.getHeight()-srcY);
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
dst.setPixel(dstX+x, dstY+y, src.getInt(srcX+x, srcY+y));
}
static File getCacheProgramDir() {
return getCacheProgramDir(getProgramID());
}
static File getCacheProgramDir(String snippetID) {
return new File(userHome(), "JavaX-Caches/" + formatSnippetIDOpt(snippetID));
}
public static boolean isSnippetID(String s) {
try {
parseSnippetID(s);
return true;
} catch (RuntimeException e) {
return false;
}
}
static boolean empty(Collection c) {
return isEmpty(c);
}
static boolean empty(String s) {
return isEmpty(s);
}
static boolean empty(Map map) {
return map == null || map.isEmpty();
}
static boolean empty(Object[] o) {
return o == null || o.length == 0;
}
static boolean empty(Object o) {
if (o instanceof Collection) return empty((Collection) o);
if (o instanceof String) return empty((String) o);
if (o instanceof Map) return empty((Map) o);
if (o instanceof Object[]) return empty((Object[]) o);
throw fail("unknown type for 'empty': " + getType(o));
}
static boolean empty(float[] a) { return a == null || a.length == 0; }
public static long parseSnippetID(String snippetID) {
long id = Long.parseLong(shortenSnippetID(snippetID));
if (id == 0) throw fail("0 is not a snippet ID");
return id;
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static float min(float a, float b) { return Math.min(a, b); }
static float min(float a, float b, float c) { return min(min(a, b), c); }
static double min(double a, double b) {
return Math.min(a, b);
}
static double min(double[] c) {
double x = Double.MAX_VALUE;
for (double d : c) x = Math.min(x, d);
return x;
}
static float min(float[] c) {
float x = Float.MAX_VALUE;
for (float d : c) x = Math.min(x, d);
return x;
}
static byte min(byte[] c) {
byte x = 127;
for (byte d : c) if (d < x) x = d;
return x;
}
static boolean isURL(String s) {
return s.startsWith("http://") || s.startsWith("https://");
}
static boolean endsWithLetter(String s) {
return nempty(s) && isLetter(last(s));
}
static JFrame showPackedFrame(String title, Component contents) {
return packFrame(showFrame(title, contents));
}
static JFrame showPackedFrame(Component contents) {
return packFrame(showFrame(contents));
}
static int iround(double d) {
return (int) Math.round(d);
}
static int max(int a, int b) {
return Math.max(a, b);
}
static int max(int a, int b, int c) {
return max(max(a, b), c);
}
static long max(int a, long b) {
return Math.max((long) a, b);
}
static long max(long a, long b) {
return Math.max(a, b);
}
static double max(int a, double b) {
return Math.max((double) a, b);
}
static float max(float a, float b) {
return Math.max(a, b);
}
static int max(Collection c) {
int x = Integer.MIN_VALUE;
for (int i : c) x = max(x, i);
return x;
}
static double max(double[] c) {
if (c.length == 0) return Double.MIN_VALUE;
double x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static float max(float[] c) {
if (c.length == 0) return Float.MAX_VALUE;
float x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static byte max(byte[] c) {
byte x = -128;
for (byte d : c) if (d > x) x = d;
return x;
}
static String snippetImageURL(String snippetID) {
long id = parseSnippetID(snippetID);
String url;
if (id == 1000010 || id == 1000012)
url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + id;
else
url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + id
+ "&contentType=image/png";
return url;
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); } static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o instanceof String ? l((String) o)
: l((Collection) o); // incomplete
}
static int l(MultiSet ms) { return ms == null ? 0 : ms.size(); }
static int intSum(Collection c, String field) {
int sum = 0;
for (Object o : c)
sum += toInt(getOpt(o, field));
return sum;
}
static boolean structure_showTiming, structure_checkTokenCount;
static String structure(Object o) {
structure_Data d = new structure_Data();
StringWriter sw = new StringWriter();
d.out = new PrintWriter(sw);
structure_go(o, d);
String s = str(sw);
if (structure_checkTokenCount) {
print("token count=" + d.n);
assertEquals("token count", l(javaTokC(s)), d.n);
}
return s;
}
static void structure_go(Object o, structure_Data d) {
structure_1(o, d);
while (nempty(d.stack))
popLast(d.stack).run();
}
static void structureToPrintWriter(Object o, PrintWriter out) {
structure_Data d = new structure_Data();
d.out = out;
structure_go(o, d);
}
// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
static int structure_shareStringsLongerThan = 20;
static class structure_Data {
PrintWriter out;
int stringSizeLimit;
IdentityHashMap