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 static x30_pkg.x30_util.DynamicObject;
class main {
static class SemanticImage extends Var implements MakesBufferedImage {
SemanticImage() {}
IIntegralImage image;
SemanticImage(IIntegralImage image, A value) {
super(value);
this.image = image;
}
SemanticImage(A value, IIntegralImage image) {
super(value);
this.image = image;
}
public int getWidth() { return image.getWidth(); }
public int getHeight() { return image.getHeight(); }
public BufferedImage getBufferedImage() { return image.getBufferedImage(); }
IIntegralImage img() { return image(); }
IIntegralImage getImage() { return image(); }
IIntegralImage image() { return image; }
}
static class Var implements IVar, ISetter {
Var() {}
Var(A v) {
this.v = v;}
A v; // you can access this directly if you use one thread
public synchronized void set(A a) {
if (v != a) {
v = a;
notifyAll();
}
}
public synchronized A get() { return v; }
public synchronized boolean has() { return v != null; }
public synchronized void clear() { v = null; }
public String toString() { return str(this.get()); }
}
static interface MakesBufferedImage extends WidthAndHeight {
BufferedImage getBufferedImage();
public default void drawAt(Graphics2D g, int x, int y) {
g.drawImage(getBufferedImage(), x, y, null);
}
}
static interface IIntegralImage extends MakesBufferedImage {
public int getWidth();
public int getHeight();
default public Pt getSize() { return pt(getWidth(), getHeight()); }
default public int nChannels() { return 3; }
// get value for 1 channel
// normal range [0; pixelCount*256)
public double getIntegralValue(int x, int y, int channel);
// gets value of the 3 channels
// normal range [0; pixelCount*256*3)
default double getIntegralValue(int x, int y) {
return getIntegralValue(x, y, 0)
+ getIntegralValue(x, y, 1)
+ getIntegralValue(x, y, 2);
}
default double rectSum(int x1, int y1, int x2, int y2, int channel) {
double bottomLeft = getIntegralValue(x1-1, y2-1, channel);
double bottomRight = getIntegralValue(x2-1, y2-1, channel);
double topLeft = getIntegralValue(x1-1, y1-1, channel);
double topRight = getIntegralValue(x2-1, y1-1, channel);
return bottomRight+topLeft-topRight-bottomLeft;
}
default double rectSum(int x1, int y1, int x2, int y2) {
double bottomLeft = getIntegralValue(x1-1, y2-1);
double bottomRight = getIntegralValue(x2-1, y2-1);
double topLeft = getIntegralValue(x1-1, y1-1);
double topRight = getIntegralValue(x2-1, y1-1);
return bottomRight+topLeft-topRight-bottomLeft;
}
default double rectAverage(int x1, int y1, int x2, int y2, int channel) {
return doubleRatio(rectSum(x1, y1, x2, y2, channel), areaFromPoints(x1, y1, x2, y2));
}
default double rectAverage(Rect r, int channel) {
return doubleRatio(rectSum(r, channel), rectArea(r));
}
default double rectSum(Rect r) {
return rectSum(r.x, r.y, r.x2(), r.y2());
}
default double rectSum(Rect r, int channel) {
return rectSum(r.x, r.y, r.x2(), r.y2(), channel);
}
default IIntegralImage clip(int x, int y, int w, int h) {
return new IIVirtualClip(this, x, y, w, h);
}
default double averageBrightness() {
int w = getWidth(), h = getHeight();
return doubleRatio(getIntegralValue(w-1, h-1), w*h*3*255.0);
}
default double getPixel(int x, int y, int channel) {
return rectSum(x, y, x+1, y+1, channel);
}
// returns RGB pixel without alpha
default int getPixel(int x, int y) {
int r = iround(rectSum(x, y, x+1, y+1, 0));
int g = iround(rectSum(x, y, x+1, y+1, 1));
int b = iround(rectSum(x, y, x+1, y+1, 2));
return rgbInt(r, g, b);
}
default BufferedImage getBufferedImage() {
int w = getWidth(), h = getHeight();
int[] pixels = new int[w*h];
int i = 0;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
pixels[i++] = getPixel(x, y) | fullAlphaMask();
return intArrayToBufferedImage(pixels, w, h);
}
// minimum and maximum brightness possible in image
// Better not to use because without this information
// you have a more general recognition algorithm.
//default DoubleRange colorRange(int channel) { ret doubleRange(0, 256); }
}
static class IIVirtualClip extends Meta implements IIntegralImage {
RegisteredReference < IIntegralImage > fullImage = new RegisteredReference<>(this);
int x1, y1, w, h;
IIVirtualClip() {}
IIVirtualClip(IIntegralImage fullImage, int x1, int y1, int w, int h) {
this.fullImage.set(fullImage);
this.h = h;
this.w = w;
this.y1 = y1;
this.x1 = x1;
}
public int getWidth() { return w; }
public int getHeight() { return h; }
public double getIntegralValue(int x, int y, int channel) {
return fullImage.get().getIntegralValue(x+x1, y+y1, channel);
}
public double getIntegralValue(int x, int y) {
return fullImage.get().getIntegralValue(x+x1, y+y1);
}
public BufferedImage getBufferedImage() {
return clipBufferedImage(fullImage.get().getBufferedImage(), x1, y1, w, h);
}
}
final static class Rect implements IFieldsToList{
static final String _fieldOrder = "x y w h";
int x;
int y;
int w;
int h;
Rect() {}
Rect(int x, int y, int w, int h) {
this.h = h;
this.w = w;
this.y = y;
this.x = x;}
public boolean equals(Object o) {
if (!(o instanceof Rect)) return false;
Rect __1 = (Rect) o;
return x == __1.x && y == __1.y && w == __1.w && h == __1.h;
}
public int hashCode() {
int h = 2543108;
h = boostHashCombine(h, _hashCode(x));
h = boostHashCombine(h, _hashCode(y));
h = boostHashCombine(h, _hashCode(w));
h = boostHashCombine(h, _hashCode(h));
return h;
}
public Object[] _fieldsToList() { return new Object[] {x, y, w, h}; }
Rect(Rectangle r) {
x = r.x;
y = r.y;
w = r.width;
h = r.height;
}
Rect(Pt p, int w, int h) {
this.h = h;
this.w = w; x = p.x; y = p.y; }
Rect(Rect r) { x = r.x; y = r.y; w = r.w; h = r.h; }
Rectangle getRectangle() {
return new Rectangle(x, y, w, h);
}
public String toString() {
return x + "," + y + " / " + w + "," + h;
}
int x1() { return x; }
int y1() { return y; }
int x2() { return x + w; }
int y2() { return y + h; }
boolean contains(Pt p) {
return contains(p.x, p.y);
}
boolean contains(int _x, int _y) {
return _x >= x && _y >= y && _x < x+w && _y < y+h;
}
boolean empty() { return w <= 0 || h <= 0; }
}
static class Pt implements Comparable {
int x, y;
Pt() {}
Pt(Point p) {
x = p.x;
y = p.y;
}
Pt(int x, int y) {
this.y = y;
this.x = x;}
Point getPoint() {
return new Point(x, y);
}
public boolean equals(Object o) {
return o instanceof Pt && x == ((Pt) o).x && y == ((Pt) o).y;
}
public int hashCode() {
return boostHashCombine(x, y);
}
// compare in scan order
public int compareTo(Pt p) {
if (y != p.y) return cmp(y, p.y);
return cmp(x, p.x);
}
public String toString() {
return x + ", " + y;
}
}
static interface ISetter {
void set(A a);
}
static interface IVar extends IF0 {
void set(A a);
A get();
default boolean has() { return get() != null; }
default void clear() { set(null); }
}
static interface WidthAndHeight {
int getWidth();
int getHeight();
}
// Meta - a "minimal" approach to adding meta-level to Java objects
static class Meta implements IMeta {
// We allocate one extra field for each Java object to make it
// reasoning-compatible. We couldn't go for 0 extra fields and
// there are no half fields in Java... so there you go.
// Also, if you don't use any meta data, you are probably not
// reasoning about anything. The point of reasoning in JavaX is
// to attach information to objects directly used in the program.
// Possible information contained in the meta field:
// Origin, destination, security level, sender, cost center,
// purpose, list of reifications, ...
// So here it is. The field.
// We also have IMeta to retrofit foreign classes (rare but
// probably useful)
// generic meta value of any kind (e.g. Map with extra field values
// for the object).
// volatile to avoid synchronization; but you can also synchronize on
// _tempMetaMutex() which is usually the object itself. Still considering
// whether that is actually a good idea.
volatile Object meta;
// ...and the interface methods
public void _setMeta(Object meta) { this.meta = meta; }
public Object _getMeta() { return meta; }
}
static interface IF0 {
A get();
}
static interface IFieldsToList {
Object[] _fieldsToList();
}
static class RegisteredReference implements IRef {
Meta owner; // all we require is a meta field
A value;
RegisteredReference() {
//if (!dynamicObjectIsLoading()) registerRef();
}
/*void registerRef {
vmBus_send registeringReference(this);
}*/
// Note that the single-argument constructor takes an owner, not a value
RegisteredReference(Meta owner) {
this(owner, null);
}
RegisteredReference(Meta owner, A value) {
this.value = value;
this.owner = owner;
index();
//registerRef();
}
// get owning object (source)
Meta owner() { return owner; }
// get target
public A get() { return value; }
boolean has() { return value != null; }
boolean set(A a) {
if (a == value) return false;
unindex();
value = a;
index();
return true;
}
void setIfEmpty(A a) {
if (!has()) set(a);
}
void set(IRef ref) { set(ref == null ? null : ref.get()); }
void clear() { set((A) null); }
// can override
boolean validRef() { return true; }
// TODO: sync all the indexing and unindexing!?
void index() {
if (!validRef()) return;
var br = lookupInterface(IHasBackRefs.class, get());
{ if (br != null) br._registerBackRef(this); }
}
void unindex() {
if (!validRef()) return;
var br = lookupInterface(IHasBackRefs.class, get());
{ if (br != null) br._unregisterBackRef(this); }
}
// not used yet
void change() {}
public String toString() {
return
str(value);
}
}
static interface IMeta {
// see class "Meta" for the bla bla
public void _setMeta(Object meta);
public Object _getMeta();
default public IAutoCloseableF0 _tempMetaMutex() {
return new IAutoCloseableF0() {
public Object get() { return IMeta.this; }
public void close() {}
};
}
}
static interface IHasBackRefs {
public void _registerBackRef(IRef ref);
public void _unregisterBackRef(IRef ref);
}
static interface IRef extends IF0 {
// called by the referencee to atomically replace itself
// Passing oldValue to avoid race conditions
public default void replaceValue(A oldValue, A newValue) {}
}
static interface IAutoCloseableF0 extends IF0, AutoCloseable {}
static Pt pt(int x, int y) {
return new Pt(x, y);
}
static Pt pt(int x) {
return new Pt(x, x);
}
static int getWidth(Component c) {
return c == null ? 0 : (int) swingCall(c, "getWidth");
}
static int getHeight(Component c) {
return c == null ? 0 : (int) swingCall(c, "getHeight");
}
static double doubleRatio(double x, double y) {
return y == 0 ? 0 : x/y;
}
static int areaFromPoints(int x1, int y1, int x2, int y2) {
return (x1-x1)*(y2-y1);
}
static int rectArea(Rect r) {
return r == null ? 0 : r.w*r.h;
}
static int iround(double d) {
return (int) Math.round(d);
}
static int iround(Number n) {
return iround(toDouble(n));
}
static int rgbInt(int r, int g, int b) {
return (clamp(r, 0, 255) << 16) | (clamp(g, 0, 255) << 8) | clamp(b, 0, 255);
}
static int getPixel(BufferedImage img, int x, int y) {
return img.getRGB(x, y);
}
static int getPixel(BufferedImage img, Pt p) {
return img.getRGB(p.x, p.y);
}
static int fullAlphaMask() {
return 0xFF000000;
}
// from: https://stackoverflow.com/questions/14416107/int-array-to-bufferedimage
// pixels are RGB pixels
static BufferedImage intArrayToBufferedImage(int[] pixels, int w, int h) {
int[] bitMasks = new int[]{0xFF0000, 0xFF00, 0xFF, 0xFF000000};
SinglePixelPackedSampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, w, h, bitMasks);
DataBufferInt db = new DataBufferInt(pixels, pixels.length);
WritableRaster wr = Raster.createWritableRaster(sm, db, new Point());
return new BufferedImage(ColorModel.getRGBdefault(), wr, false, null);
}
static BufferedImage clipBufferedImage(BufferedImage src, Rectangle clip) {
return clipBufferedImage(src, new Rect(clip));
}
static BufferedImage clipBufferedImage(BufferedImage src, Rect r) {
if (src == null || r == null) return null;
// fixClipRect
r = intersectRects(r, new Rect(0, 0, src.getWidth(), src.getHeight()));
if (rectEmpty(r)) return null; // can't make zero-sized BufferedImage
return src.getSubimage(r.x, r.y, r.w, r.h);
}
static BufferedImage clipBufferedImage(BufferedImage src, int x, int y, int w, int h) {
return clipBufferedImage(src, new Rect(x, y, w, h));
}
static int boostHashCombine(int a, int b) {
return a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2));
}
static int _hashCode(Object a) {
return a == null ? 0 : a.hashCode();
}
static boolean contains(Collection c, Object o) {
return c != null && c.contains(o);
}
static boolean contains(Object[] x, Object o) {
if (x != null)
for (Object a : x)
if (eq(a, o))
return true;
return false;
}
static boolean contains(String s, char c) {
return s != null && s.indexOf(c) >= 0;
}
static boolean contains(String s, String b) {
return s != null && s.indexOf(b) >= 0;
}
static boolean contains(BitSet bs, int i) {
return bs != null && bs.get(i);
}
static int cmp(Number a, Number b) {
return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue());
}
static int cmp(double a, double b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(int a, int b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(long a, long b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(Object a, Object b) {
if (a == null) return b == null ? 0 : -1;
if (b == null) return 1;
return ((Comparable) a).compareTo(b);
}
// get purpose 1: access a list/array/map (safer version of x.get(y))
static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
// seems to conflict with other signatures
/*static B get(Map map, A key) {
ret map != null ? map.get(key) : null;
}*/
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
// get purpose 2: access a field by reflection or a map
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);
}
if (o instanceof DynamicObject)
return getOptDynOnly(((DynamicObject) o), field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(String field, Object o) {
return get_raw(o, field);
}
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); } }
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);
}
}
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());
}
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());
}
static Object get(String field, Object o) {
return get(o, field);
}
static boolean get(BitSet bs, int idx) {
return bs != null && bs.get(idx);
}
static A set(A o, String field, Object value) {
if (o == null) return null;
if (o instanceof Class) set((Class) o, field, value);
else try {
Field f = set_findField(o.getClass(), field);
makeAccessible(f);
smartSet(f, o, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
return o;
}
static void set(Class c, String field, Object value) {
if (c == null) return;
try {
Field f = set_findStaticField(c, field);
makeAccessible(f);
smartSet(f, null, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field set_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());
}
static Field set_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());
}
static void set(BitSet bs, int idx) {
{ if (bs != null) bs.set(idx); }
}
static boolean has(String a, String b, String c) {
return false;
}
static boolean has(T3 t) {
return false;
}
// We even allow overriding existing interface implementations
static A lookupInterface(Class intrface, Object o) {
return lookupDynamicInterface(intrface, o);
}
static A lookupInterface(Object o, Class intrface) {
return lookupDynamicInterface(o, intrface);
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static Object swingCall(final Object o, final String method, final Object... args) {
return swing(new F0