//FILENAME: iiPkg/ii.java
package iiPkg;
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.*;
public class ii {
public static class IntegralImage implements MakesBufferedImage {
public int w, h;
public int[] data;
public IntegralImage() {
}
public IntegralImage(RGBImage img) {
w = img.w();
h = img.h();
if (longMul(w, h) > 8000000)
throw fail("Image too big: " + w + "*" + h);
data = new int[w * h * 3];
int i = 0;
for (int y = 0; y < h; y++) {
int sumR = 0, sumG = 0, sumB = 0;
for (int x = 0; x < w; x++) {
int rgb = img.getInt(x, y);
sumR += (rgb >> 16) & 0xFF;
sumG += (rgb >> 8) & 0xFF;
sumB += rgb & 0xFF;
data[i] = y > 0 ? sumR + data[i - w * 3] : sumR;
data[i + 1] = y > 0 ? sumG + data[i - w * 3 + 1] : sumG;
data[i + 2] = y > 0 ? sumB + data[i - w * 3 + 2] : sumB;
i += 3;
}
}
}
public IntegralImage(BufferedImage img) {
w = img.getWidth();
h = img.getHeight();
if (longMul(w, h) > 8000000)
throw fail("Image too big: " + w + "*" + h);
int[] pixels = pixelsOfBufferedImage(img);
data = new int[w * h * 3];
int i = 0, j = 0;
for (int y = 0; y < h; y++) {
int sumR = 0, sumG = 0, sumB = 0;
for (int x = 0; x < w; x++) {
int rgb = pixels[j++];
sumR += (rgb >> 16) & 0xFF;
sumG += (rgb >> 8) & 0xFF;
sumB += rgb & 0xFF;
data[i] = y > 0 ? sumR + data[i - w * 3] : sumR;
data[i + 1] = y > 0 ? sumG + data[i - w * 3 + 1] : sumG;
data[i + 2] = y > 0 ? sumB + data[i - w * 3 + 2] : sumB;
i += 3;
}
}
}
public int get(int x, int y, int channel) {
return x < 0 || y < 0 || x >= w || y >= h ? 0 : data[(y * w + x) * 3 + channel];
}
public int get(int x, int y) {
if (x < 0 || y < 0 || x >= w || y >= h)
return 0;
int i = (y * w + x) * 3;
return data[i] + data[i + 1] + data[i + 2];
}
public double averageBrightness() {
return doubleRatio(get(w - 1, h - 1), w * h * 3 * 255.0);
}
public String toString() {
return "IntegralImage " + w + "*" + h + ", brightness: " + averageBrightness();
}
public BufferedImage getBufferedImage() {
return integralImageToBufferedImage(this);
}
public int getWidth() {
return w;
}
public int getHeight() {
return h;
}
}
public static class RGBImage implements MakesBufferedImage {
public transient BufferedImage bufferedImage;
public File file;
public int width, height;
public int[] pixels;
public RGBImage() {
}
public RGBImage(BufferedImage image) {
this(image, null);
}
public RGBImage(BufferedImage image, File file) {
this.file = file;
bufferedImage = image;
width = image.getWidth();
height = image.getHeight();
pixels = new int[width * height];
PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
try {
if (!pixelGrabber.grabPixels())
throw new RuntimeException("Could not grab pixels");
cleanPixels();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public RGBImage(String file) throws IOException {
this(new File(file));
}
public RGBImage(Dimension size, Color color) {
this(size.width, size.height, color);
}
public RGBImage(Dimension size, RGB color) {
this(size.width, size.height, color);
}
public final void cleanPixels() {
for (int i = 0; i < pixels.length; i++) pixels[i] &= 0xFFFFFF;
}
public RGBImage(int width, int height, int[] pixels) {
this.width = width;
this.height = height;
this.pixels = pixels;
}
public RGBImage(int w, int h, RGB[] pixels) {
this.width = w;
this.height = h;
this.pixels = asInts(pixels);
}
public static int[] asInts(RGB[] pixels) {
int[] ints = new int[pixels.length];
for (int i = 0; i < pixels.length; i++) ints[i] = pixels[i] == null ? 0 : pixels[i].getColor().getRGB();
return ints;
}
public RGBImage(int w, int h) {
this(w, h, Color.black);
}
public RGBImage(int w, int h, RGB rgb) {
this.width = w;
this.height = h;
this.pixels = new int[w * h];
int col = rgb.asInt();
if (col != 0)
for (int i = 0; i < pixels.length; i++) pixels[i] = col;
}
public RGBImage(RGBImage image) {
this(image.width, image.height, copyPixels(image.pixels));
}
public RGBImage(int width, int height, Color color) {
this(width, height, new RGB(color));
}
public RGBImage(File file) throws IOException {
this(javax.imageio.ImageIO.read(file));
}
public static int[] copyPixels(int[] pixels) {
int[] copy = new int[pixels.length];
System.arraycopy(pixels, 0, copy, 0, pixels.length);
return copy;
}
public int getIntPixel(int x, int y) {
if (inRange(x, y))
return pixels[y * width + x];
else
return 0xFFFFFF;
}
public static RGB asRGB(int packed) {
int r = (packed >> 16) & 0xFF;
int g = (packed >> 8) & 0xFF;
int b = packed & 0xFF;
return new RGB(r / 255f, g / 255f, b / 255f);
}
public RGB getRGB(int x, int y) {
if (inRange(x, y))
return asRGB(pixels[y * width + x]);
else
return new RGB(0xFFFFFF);
}
public RGB getPixel(int x, int y) {
return getRGB(x, y);
}
public RGB getPixel(Pt p) {
return getPixel(p.x, p.y);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int w() {
return width;
}
public int h() {
return height;
}
public BufferedImage getBufferedImage() {
if (bufferedImage == null) {
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) bufferedImage.setRGB(x, y, pixels[y * width + x]);
}
return bufferedImage;
}
public RGBImage clip(Rect r) {
return r == null ? null : clip(r.getRectangle());
}
public RGBImage clip(Rectangle r) {
r = fixClipRect(r);
if (r.x == 0 && r.y == 0 && r.width == width && r.height == height)
return this;
int[] newPixels;
try {
newPixels = new int[r.width * r.height];
} catch (RuntimeException e) {
System.out.println(r);
throw e;
}
for (int y = 0; y < r.height; y++) {
System.arraycopy(pixels, (y + r.y) * width + r.x, newPixels, y * r.width, r.width);
}
return new RGBImage(r.width, r.height, newPixels);
}
public final Rectangle fixClipRect(Rectangle r) {
r = r.intersection(new Rectangle(0, 0, width, height));
if (r.isEmpty())
r = new Rectangle(r.x, r.y, 0, 0);
return r;
}
public File getFile() {
return file;
}
public static RGBImage load(String fileName) {
return load(new File(fileName));
}
public static RGBImage load(File file) {
try {
BufferedImage bufferedImage = javax.imageio.ImageIO.read(file);
return new RGBImage(bufferedImage);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public int getInt(int x, int y) {
return pixels[y * width + x];
}
public void save(File file) throws IOException {
String name = file.getName().toLowerCase();
String type;
if (name.endsWith(".png"))
type = "png";
else if (name.endsWith(".jpg") || name.endsWith(".jpeg"))
type = "jpeg";
else
throw new IOException("Unknown image extension: " + name);
javax.imageio.ImageIO.write(getBufferedImage(), type, file);
}
public static RGBImage dummyImage() {
return new RGBImage(1, 1, new int[] { 0xFFFFFF });
}
public int[] getPixels() {
return pixels;
}
public void setPixel(int x, int y, int r, int g, int b) {
if (x >= 0 && y >= 0 && x < width && y < height)
pixels[y * width + x] = (limitToUByte(r) << 16) | (limitToUByte(g) << 8) | limitToUByte(b);
}
public void setPixel(int x, int y, RGB rgb) {
if (x >= 0 && y >= 0 && x < width && y < height)
pixels[y * width + x] = rgb.asInt();
}
public void setPixel(int x, int y, Color color) {
setPixel(x, y, new RGB(color));
}
public void setInt(int x, int y, int rgb) {
setPixel(x, y, rgb);
}
public void setPixel(int x, int y, int rgb) {
if (x >= 0 && y >= 0 && x < width && y < height)
pixels[y * width + x] = rgb;
}
public void setPixel(Pt p, RGB rgb) {
setPixel(p.x, p.y, rgb);
}
public void setPixel(Pt p, Color color) {
setPixel(p.x, p.y, color);
}
public RGBImage copy() {
return new RGBImage(this);
}
public boolean inRange(int x, int y) {
return x >= 0 && y >= 0 && x < width && y < height;
}
public Dimension getSize() {
return new Dimension(width, height);
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
RGBImage rgbImage = (RGBImage) o;
if (height != rgbImage.height)
return false;
if (width != rgbImage.width)
return false;
if (!Arrays.equals(pixels, rgbImage.pixels))
return false;
return true;
}
@Override
public int hashCode() {
int result = width;
result = 31 * result + height;
result = 31 * result + Arrays.hashCode(pixels);
return result;
}
public String getHex(int x, int y) {
return getPixel(x, y).getHexString();
}
public RGBImage clip(int x, int y, int width, int height) {
return clip(new Rectangle(x, y, width, height));
}
public RGBImage clipLine(int y) {
return clip(0, y, width, 1);
}
public int numPixels() {
return width * height;
}
}
public static interface MakesBufferedImage {
public BufferedImage getBufferedImage();
public int getWidth();
public int getHeight();
}
public static class Rect {
public int x, y, w, h;
public Rect() {
}
public Rect(Rectangle r) {
x = r.x;
y = r.y;
w = r.width;
h = r.height;
}
public Rect(int x, int y, int w, int h) {
this.h = h;
this.w = w;
this.y = y;
this.x = x;
}
public Rect(Pt p, int w, int h) {
this.h = h;
this.w = w;
x = p.x;
y = p.y;
}
public Rect(Rect r) {
x = r.x;
y = r.y;
w = r.w;
h = r.h;
}
public Rectangle getRectangle() {
return new Rectangle(x, y, w, h);
}
public boolean equals(Object o) {
return stdEq2(this, o);
}
public int hashCode() {
return stdHash2(this);
}
public String toString() {
return x + "," + y + " / " + w + "," + h;
}
public int x2() {
return x + w;
}
public int y2() {
return y + h;
}
public boolean contains(Pt p) {
return contains(p.x, p.y);
}
public boolean contains(int _x, int _y) {
return _x >= x && _y >= y && _x < x + w && _y < y + h;
}
public boolean empty() {
return w <= 0 || h <= 0;
}
}
public static class Pt {
public int x, y;
public Pt() {
}
public Pt(Point p) {
x = p.x;
y = p.y;
}
public Pt(int x, int y) {
this.y = y;
this.x = x;
}
public Point getPoint() {
return new Point(x, y);
}
public boolean equals(Object o) {
return stdEq2(this, o);
}
public int hashCode() {
return stdHash2(this);
}
public String toString() {
return x + ", " + y;
}
}
public static class RGB {
public float r, g, b;
public RGB() {
}
public RGB(float r, float g, float b) {
this.r = r;
this.g = g;
this.b = b;
}
public RGB(double r, double g, double b) {
this.r = (float) r;
this.g = (float) g;
this.b = (float) b;
}
public RGB(int rgb) {
this(new Color(rgb));
}
public RGB(double brightness) {
this.r = this.g = this.b = max(0f, min(1f, (float) brightness));
}
public RGB(Color color) {
this.r = color.getRed() / 255f;
this.g = color.getGreen() / 255f;
this.b = color.getBlue() / 255f;
}
public RGB(String hex) {
int i = l(hex) - 6;
r = Integer.parseInt(hex.substring(i, i + 2), 16) / 255f;
g = Integer.parseInt(hex.substring(i + 2, i + 4), 16) / 255f;
b = Integer.parseInt(hex.substring(i + 4, i + 6), 16) / 255f;
}
public float getComponent(int i) {
return i == 0 ? r : i == 1 ? g : b;
}
public Color getColor() {
return new Color(r, g, b);
}
public static RGB newSafe(float r, float g, float b) {
return new RGB(Math.max(0, Math.min(1, r)), Math.max(0, Math.min(1, g)), Math.max(0, Math.min(1, b)));
}
public int asInt() {
return getColor().getRGB() & 0xFFFFFF;
}
public int getInt() {
return getColor().getRGB() & 0xFFFFFF;
}
public float getBrightness() {
return (r + g + b) / 3.0f;
}
public String getHexString() {
return Integer.toHexString(asInt() | 0xFF000000).substring(2).toUpperCase();
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof RGB))
return false;
RGB rgb = (RGB) o;
if (Float.compare(rgb.b, b) != 0)
return false;
if (Float.compare(rgb.g, g) != 0)
return false;
if (Float.compare(rgb.r, r) != 0)
return false;
return true;
}
@Override
public int hashCode() {
int result = (r != +0.0f ? Float.floatToIntBits(r) : 0);
result = 31 * result + (g != +0.0f ? Float.floatToIntBits(g) : 0);
result = 31 * result + (b != +0.0f ? Float.floatToIntBits(b) : 0);
return result;
}
public boolean isBlack() {
return r == 0f && g == 0f && b == 0f;
}
public boolean isWhite() {
return r == 1f && g == 1f && b == 1f;
}
public String toString() {
return getHexString();
}
public int redInt() {
return iround(r * 255);
}
public int greenInt() {
return iround(g * 255);
}
public int blueInt() {
return iround(b * 255);
}
}
public static long longMul(long a, long b) {
return a * b;
}
public static RuntimeException fail() {
throw new RuntimeException("fail");
}
public static RuntimeException fail(Throwable e) {
throw asRuntimeException(e);
}
public static RuntimeException fail(Object msg) {
throw new RuntimeException(String.valueOf(msg));
}
public static RuntimeException fail(String msg) {
throw new RuntimeException(msg == null ? "" : msg);
}
public static RuntimeException fail(String msg, Throwable innerException) {
throw new RuntimeException(msg, innerException);
}
public static int[] pixelsOfBufferedImage(BufferedImage image) {
try {
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = new int[width * height];
PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
if (!pixelGrabber.grabPixels())
throw fail("Could not grab pixels");
return pixels;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static double averageBrightness(BufferedImage img) {
return new BWImage(img).averageBrightness();
}
public static double doubleRatio(double x, double y) {
return y == 0 ? 0 : x / y;
}
public static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
public static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
public static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
public static Object get(Object o, String field) {
try {
if (o == null)
return null;
if (o instanceof Class)
return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
makeAccessible(f);
return f.get(o);
}
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
public static Object get_raw(String field, Object o) {
return get_raw(o, field);
}
public static Object get_raw(Object o, String field) {
try {
if (o == null)
return null;
Field f = get_findField(o.getClass(), field);
makeAccessible(f);
return f.get(o);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
makeAccessible(f);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
public static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
public static Object get(String field, Object o) {
return get(o, field);
}
public static BufferedImage integralImageToBufferedImage(IntegralImage img) {
if (img == null)
return null;
int w = img.w, h = img.h;
BufferedImage out = newBufferedImage(w, h);
for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) {
int r = integralImage_sumRect(img, x, y, x + 1, y + 1, 0);
int g = integralImage_sumRect(img, x, y, x + 1, y + 1, 1);
int b = integralImage_sumRect(img, x, y, x + 1, y + 1, 2);
out.setRGB(x, y, (r << 16) | (g << 8) | b);
}
return out;
}
public static boolean inRange(int x, int n) {
return x >= 0 && x < n;
}
public static boolean inRange(int x, int a, int b) {
return x >= a && x < b;
}
public static int limitToUByte(int i) {
return max(0, min(255, i));
}
public static Class> getClass(String name) {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
return null;
}
}
public static Class getClass(Object o) {
return o instanceof Class ? (Class) o : o.getClass();
}
public static Class getClass(Object realm, String name) {
try {
try {
return getClass(realm).getClassLoader().loadClass(classNameToVM(name));
} catch (ClassNotFoundException e) {
return null;
}
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static boolean stdEq2(Object a, Object b) {
if (a == null)
return b == null;
if (b == null)
return false;
if (a.getClass() != b.getClass())
return false;
for (String field : allFields(a)) if (neq(getOpt(a, field), getOpt(b, field)))
return false;
return true;
}
public static int stdHash2(Object a) {
if (a == null)
return 0;
return stdHash(a, toStringArray(allFields(a)));
}
public static boolean contains(Collection c, Object o) {
return c != null && c.contains(o);
}
public static boolean contains(Object[] x, Object o) {
if (x != null)
for (Object a : x) if (eq(a, o))
return true;
return false;
}
public static boolean contains(String s, char c) {
return s != null && s.indexOf(c) >= 0;
}
public static boolean contains(String s, String b) {
return s != null && s.indexOf(b) >= 0;
}
public static boolean contains(BitSet bs, int i) {
return bs != null && bs.get(i);
}
public static int max(int a, int b) {
return Math.max(a, b);
}
public static int max(int a, int b, int c) {
return max(max(a, b), c);
}
public static long max(int a, long b) {
return Math.max((long) a, b);
}
public static long max(long a, long b) {
return Math.max(a, b);
}
public static double max(int a, double b) {
return Math.max((double) a, b);
}
public static float max(float a, float b) {
return Math.max(a, b);
}
public static double max(double a, double b) {
return Math.max(a, b);
}
public static int max(Collection c) {
int x = Integer.MIN_VALUE;
for (int i : c) x = max(x, i);
return x;
}
public 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;
}
public 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;
}
public static byte max(byte[] c) {
byte x = -128;
for (byte d : c) if (d > x)
x = d;
return x;
}
public static short max(short[] c) {
short x = -0x8000;
for (short d : c) if (d > x)
x = d;
return x;
}
public static int max(int[] c) {
int x = Integer.MIN_VALUE;
for (int d : c) if (d > x)
x = d;
return x;
}
public static int min(int a, int b) {
return Math.min(a, b);
}
public static long min(long a, long b) {
return Math.min(a, b);
}
public static float min(float a, float b) {
return Math.min(a, b);
}
public static float min(float a, float b, float c) {
return min(min(a, b), c);
}
public static double min(double a, double b) {
return Math.min(a, b);
}
public static double min(double[] c) {
double x = Double.MAX_VALUE;
for (double d : c) x = Math.min(x, d);
return x;
}
public static float min(float[] c) {
float x = Float.MAX_VALUE;
for (float d : c) x = Math.min(x, d);
return x;
}
public static byte min(byte[] c) {
byte x = 127;
for (byte d : c) if (d < x)
x = d;
return x;
}
public static short min(short[] c) {
short x = 0x7FFF;
for (short d : c) if (d < x)
x = d;
return x;
}
public static int min(int[] c) {
int x = Integer.MAX_VALUE;
for (int d : c) if (d < x)
x = d;
return x;
}
public static int l(Object[] a) {
return a == null ? 0 : a.length;
}
public static int l(boolean[] a) {
return a == null ? 0 : a.length;
}
public static int l(byte[] a) {
return a == null ? 0 : a.length;
}
public static int l(short[] a) {
return a == null ? 0 : a.length;
}
public static int l(long[] a) {
return a == null ? 0 : a.length;
}
public static int l(int[] a) {
return a == null ? 0 : a.length;
}
public static int l(float[] a) {
return a == null ? 0 : a.length;
}
public static int l(double[] a) {
return a == null ? 0 : a.length;
}
public static int l(char[] a) {
return a == null ? 0 : a.length;
}
public static int l(Collection c) {
return c == null ? 0 : c.size();
}
public static int l(Iterator i) {
return iteratorCount_int_close(i);
}
public static int l(Map m) {
return m == null ? 0 : m.size();
}
public static int l(CharSequence s) {
return s == null ? 0 : s.length();
}
public static long l(File f) {
return f == null ? 0 : f.length();
}
public static int l(Object o) {
return o == null ? 0 : o instanceof String ? l((String) o) : o instanceof Map ? l((Map) o) : o instanceof Collection ? l((Collection) o) : o instanceof Object[] ? l((Object[]) o) : o instanceof boolean[] ? l((boolean[]) o) : o instanceof byte[] ? l((byte[]) o) : o instanceof char[] ? l((char[]) o) : o instanceof short[] ? l((short[]) o) : o instanceof int[] ? l((int[]) o) : o instanceof float[] ? l((float[]) o) : o instanceof double[] ? l((double[]) o) : o instanceof long[] ? l((long[]) o) : (Integer) call(o, "size");
}
public static int asInt(Object o) {
return toInt(o);
}
public static int iround(double d) {
return (int) Math.round(d);
}
public static int iround(Number n) {
return iround(toDouble(n));
}
public static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
public static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
public static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
public static Field getOpt_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
public static Field makeAccessible(Field f) {
try {
f.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, f);
}
return f;
}
public static Method makeAccessible(Method m) {
try {
m.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, m);
}
return m;
}
public static Constructor makeAccessible(Constructor c) {
try {
c.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, c);
}
return c;
}
public static BufferedImage newBufferedImage(int w, int h) {
return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
}
public static BufferedImage newBufferedImage(int w, int h, RGB rgb) {
return newBufferedImage(w, h, toColor(rgb));
}
public static BufferedImage newBufferedImage(int w, int h, Color color) {
BufferedImage img = newBufferedImage(w, h);
Graphics2D g = img.createGraphics();
g.setColor(color);
g.fillRect(0, 0, w, h);
return img;
}
public static BufferedImage newBufferedImage(Pt p, Color color) {
return newBufferedImage(p.x, p.y, color);
}
public static int integralImage_sumRect(IntegralImage img, int x1, int y1, int x2, int y2, int channel) {
int bottomLeft = img.get(x1 - 1, y2 - 1, channel);
int bottomRight = img.get(x2 - 1, y2 - 1, channel);
int topLeft = img.get(x1 - 1, y1 - 1, channel);
int topRight = img.get(x2 - 1, y1 - 1, channel);
return bottomRight + topLeft - topRight - bottomLeft;
}
public static String classNameToVM(String name) {
return name.replace(".", "$");
}
public static Set allFields(Object o) {
TreeSet fields = new TreeSet();
Class _c = _getClass(o);
do {
for (Field f : _c.getDeclaredFields()) fields.add(f.getName());
_c = _c.getSuperclass();
} while (_c != null);
return fields;
}
public static boolean neq(Object a, Object b) {
return !eq(a, b);
}
public static Object getOpt(Object o, String field) {
return getOpt_cached(o, field);
}
public static Object getOpt(String field, Object o) {
return getOpt_cached(o, field);
}
public static Object getOpt_raw(Object o, String field) {
try {
Field f = getOpt_findField(o.getClass(), field);
if (f == null)
return null;
makeAccessible(f);
return f.get(o);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static Object getOpt(Class c, String field) {
try {
if (c == null)
return null;
Field f = getOpt_findStaticField(c, field);
if (f == null)
return null;
makeAccessible(f);
return f.get(null);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static Field getOpt_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
public static int stdHash(Object a, String... fields) {
if (a == null)
return 0;
int hash = getClassName(a).hashCode();
for (String field : fields) hash = boostHashCombine(hash, hashCode(getOpt(a, field)));
return hash;
}
public static String[] toStringArray(Collection c) {
String[] a = new String[l(c)];
Iterator it = c.iterator();
for (int i = 0; i < l(a); i++) a[i] = it.next();
return a;
}
public static String[] toStringArray(Object o) {
if (o instanceof String[])
return (String[]) o;
else if (o instanceof Collection)
return toStringArray((Collection) o);
else
throw fail("Not a collection or array: " + getClassName(o));
}
public static boolean eq(Object a, Object b) {
return a == b || (a == null ? b == null : b != null && a.equals(b));
}
public static int iteratorCount_int_close(Iterator i) {
try {
int n = 0;
if (i != null)
while (i.hasNext()) {
i.next();
++n;
}
if (i instanceof AutoCloseable)
((AutoCloseable) i).close();
return n;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static Object call(Object o) {
return callF(o);
}
public static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] { arg });
}
public static Object call(Object o, String method, Object... args) {
return call_withVarargs(o, method, args);
}
public static int toInt(Object o) {
if (o == null)
return 0;
if (o instanceof Number)
return ((Number) o).intValue();
if (o instanceof String)
return parseInt((String) o);
throw fail("woot not int: " + getClassName(o));
}
public static int toInt(long l) {
if (l != (int) l)
throw fail("Too large for int: " + l);
return (int) l;
}
public static double toDouble(Object o) {
if (o instanceof Number)
return ((Number) o).doubleValue();
if (o instanceof BigInteger)
return ((BigInteger) o).doubleValue();
if (o == null)
return 0.0;
throw fail(o);
}
public static void _handleError(Error e) {
call(javax(), "_handleError", e);
}
public static void vmBus_send(String msg, Object... args) {
Object arg = vmBus_wrapArgs(args);
pcallFAll(vm_busListeners_live(), msg, arg);
pcallFAll(vm_busListenersByMessage_live().get(msg), msg, arg);
}
public static void vmBus_send(String msg) {
vmBus_send(msg, (Object) null);
}
public static Color toColor(RGB rgb) {
return rgb == null ? null : rgb.getColor();
}
public static Color toColor(int rgb) {
return new Color(rgb);
}
public static Color toColor(String hex) {
return awtColor(hex);
}
public static Class> _getClass(String name) {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
return null;
}
}
public static Class _getClass(Object o) {
return o == null ? null : o instanceof Class ? (Class) o : o.getClass();
}
public static Class _getClass(Object realm, String name) {
try {
return getClass(realm).getClassLoader().loadClass(classNameToVM(name));
} catch (ClassNotFoundException e) {
return null;
}
}
public static class getOpt_Map extends WeakHashMap {
public getOpt_Map() {
if (getOpt_special == null)
getOpt_special = new HashMap();
clear();
}
public void clear() {
super.clear();
put(Class.class, getOpt_special);
put(String.class, getOpt_special);
}
}
public static final Map> getOpt_cache = _registerDangerousWeakMap(synchroMap(new getOpt_Map()));
public static HashMap getOpt_special;
public static Object getOpt_cached(Object o, String field) {
try {
if (o == null)
return null;
Class c = o.getClass();
HashMap map;
synchronized (getOpt_cache) {
map = getOpt_cache.get(c);
if (map == null)
map = getOpt_makeCache(c);
}
if (map == getOpt_special) {
if (o instanceof Class)
return getOpt((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
}
Field f = map.get(field);
if (f != null)
return f.get(o);
return null;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static HashMap getOpt_makeCache(Class c) {
HashMap map;
if (isSubtypeOf(c, Map.class))
map = getOpt_special;
else {
map = new HashMap();
if (!reflection_classesNotToScan().contains(c.getName())) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields()) {
makeAccessible(f);
String name = f.getName();
if (!map.containsKey(name))
map.put(name, f);
}
_c = _c.getSuperclass();
} while (_c != null);
}
}
if (getOpt_cache != null)
getOpt_cache.put(c, map);
return map;
}
public static String getClassName(Object o) {
return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName();
}
public static int boostHashCombine(int a, int b) {
return a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2));
}
public static int hashCode(Object a) {
return a == null ? 0 : a.hashCode();
}
public static String str(Object o) {
return o == null ? "null" : o.toString();
}
public static String str(char[] c) {
return new String(c);
}
public static Map> callF_cache = newDangerousWeakHashMap();
public static void callF(VF1 f, A a) {
if (f != null)
f.get(a);
}
public static Object callF(Object f, Object... args) {
try {
if (f instanceof String)
return callMC((String) f, args);
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
}
if (f == null)
return null;
Class c = f.getClass();
ArrayList methods;
synchronized (callF_cache) {
methods = callF_cache.get(c);
if (methods == null)
methods = callF_makeCache(c);
}
int n = l(methods);
if (n == 0) {
throw fail("No get method in " + getClassName(c));
}
if (n == 1)
return invokeMethod(methods.get(0), f, args);
for (int i = 0; i < n; i++) {
Method m = methods.get(i);
if (call_checkArgs(m, args, false))
return invokeMethod(m, f, args);
}
throw fail("No matching get method in " + getClassName(c));
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static ArrayList callF_makeCache(Class c) {
ArrayList l = new ArrayList();
Class _c = c;
do {
for (Method m : _c.getDeclaredMethods()) if (m.getName().equals("get")) {
makeAccessible(m);
l.add(m);
}
if (!l.isEmpty())
break;
_c = _c.getSuperclass();
} while (_c != null);
callF_cache.put(c, l);
return l;
}
public static Object call_withVarargs(Object o, String method, Object... args) {
try {
if (o == null)
return null;
if (o instanceof Class) {
Class c = (Class) o;
_MethodCache cache = callOpt_getCache(c);
Method me = cache.findStaticMethod(method, args);
if (me != null)
return invokeMethod(me, null, args);
List methods = cache.cache.get(method);
if (methods != null)
methodSearch: for (Method m : methods) {
{
if (!(m.isVarArgs()))
continue;
}
{
if (!(isStaticMethod(m)))
continue;
}
Object[] newArgs = massageArgsForVarArgsCall(m, args);
if (newArgs != null)
return invokeMethod(m, null, newArgs);
}
throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found");
} else {
Class c = o.getClass();
_MethodCache cache = callOpt_getCache(c);
Method me = cache.findMethod(method, args);
if (me != null)
return invokeMethod(me, o, args);
List methods = cache.cache.get(method);
if (methods != null)
methodSearch: for (Method m : methods) {
{
if (!(m.isVarArgs()))
continue;
}
Object[] newArgs = massageArgsForVarArgsCall(m, args);
if (newArgs != null)
return invokeMethod(m, o, newArgs);
}
throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found in " + c);
}
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static int parseInt(String s) {
return empty(s) ? 0 : Integer.parseInt(s);
}
public static int parseInt(char c) {
return Integer.parseInt(str(c));
}
public static Class javax() {
return getJavaX();
}
public static Object vmBus_wrapArgs(Object... args) {
return empty(args) ? null : l(args) == 1 ? args[0] : args;
}
public static void pcallFAll(Collection l, Object... args) {
if (l != null)
for (Object f : cloneList(l)) pcallF(f, args);
}
public static void pcallFAll(Iterator it, Object... args) {
while (it.hasNext()) pcallF(it.next(), args);
}
public static Set vm_busListeners_live_cache;
public static Set vm_busListeners_live() {
if (vm_busListeners_live_cache == null)
vm_busListeners_live_cache = vm_busListeners_live_load();
return vm_busListeners_live_cache;
}
public static Set vm_busListeners_live_load() {
return vm_generalIdentityHashSet("busListeners");
}
public static Map vm_busListenersByMessage_live_cache;
public static Map vm_busListenersByMessage_live() {
if (vm_busListenersByMessage_live_cache == null)
vm_busListenersByMessage_live_cache = vm_busListenersByMessage_live_load();
return vm_busListenersByMessage_live_cache;
}
public static Map vm_busListenersByMessage_live_load() {
return vm_generalHashMap("busListenersByMessage");
}
public static java.awt.Color awtColor(String hex) {
byte[] b = bytesFromHex(dropPrefix("#", hex));
return new Color(ubyteToInt(b[0]), ubyteToInt(b[1]), ubyteToInt(b[2]));
}
public static void clear(Collection c) {
if (c != null)
c.clear();
}
public static void put(Map map, A a, B b) {
if (map != null)
map.put(a, b);
}
public static void put(List l, int i, A a) {
if (l != null && i >= 0 && i < l(l))
l.set(i, a);
}
public static List _registerDangerousWeakMap_preList;
public static A _registerDangerousWeakMap(A map) {
return _registerDangerousWeakMap(map, null);
}
public static A _registerDangerousWeakMap(A map, Object init) {
callF(init, map);
if (init instanceof String) {
final String f = (String) init;
init = new VF1