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 java.util.function.*;
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 java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import java.awt.image.DataBufferByte;
import java.awt.geom.*;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import static x30_pkg.x30_util.DynamicObject;
import java.text.*;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.nio.charset.Charset;
class main {
static BWIntegralImage bwIntegralImage_withMeta(BufferedImage img) { return bwIntegralImage_withMeta(img, null); }
static BWIntegralImage bwIntegralImage_withMeta(BufferedImage img, Decolorizer decolorizer) {
return setMetaSrc(bwIntegralImage(img, decolorizer), img);
}
static BWIntegralImage bwIntegralImage_withMeta(BWImage img) {
return setMetaSrc(bwIntegralImage(img), img);
}
static A setMetaSrc(A a, Object src) {
setMeta(a, "src", src);
return a;
}
static A setMetaSrc(A a, Object src) {
setMeta(a, "src", src);
return a;
}
static BWIntegralImage bwIntegralImage(BufferedImage img) { return bwIntegralImage(img, null); }
static BWIntegralImage bwIntegralImage(BufferedImage img, Decolorizer decolorizer) {
return img == null ? null : new BWIntegralImage(img, decolorizer);
}
static BWIntegralImage bwIntegralImage(BWImage img) {
return img == null ? null : new BWIntegralImage(img);
}
static void setMeta(IMeta o, Object key, Object value) {
metaMapPut(o, key, value);
}
static void setMeta(Object o, Object key, Object value) {
metaMapPut(o, key, value);
}
static void metaMapPut(IMeta o, Object key, Object value) {
{ if (o != null) o.metaPut(key, value); }
}
static void metaMapPut(Object o, Object key, Object value) {
var meta = initIMeta(o);
{ if (meta != null) meta.metaPut(key, value); }
}
static IMeta initIMeta(Object o) {
if (o == null) return null;
if (o instanceof IMeta) return ((IMeta) o);
if (o instanceof JComponent) return initMetaOfJComponent((JComponent) o);
if (o instanceof BufferedImage) return optCast(IMeta.class, ((BufferedImage) o).getProperty("meta"));
return null;
}
static IMeta initMetaOfJComponent(JComponent c) {
if (c == null) return null;
IMeta meta = (IMeta) (c.getClientProperty(IMeta.class));
if (meta == null)
c.putClientProperty(IMeta.class, meta = new Meta());
return meta;
}
static A optCast(Class c, Object o) {
return isInstance(c, o) ? (A) o : null;
}
static boolean isInstance(Class type, Object arg) {
return type.isInstance(arg);
}
// Meta - a "minimal" approach to adding meta-level to Java objects
static class Meta implements IMeta {
// Meta - a "minimal" approach to adding meta-level to Java objects
// (implementing the interface IMeta)
// We allocate one extra field for each Java object to make it
// reasoning-compatible (reasoning-compatible = extensible with
// fields of any name at runtime).
//
// We couldn't go for 0 extra fields (meta values must be linked
// directly from the object) 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 YOU HAVE BEEN WAITING FOR!
// [We also have IMeta to retrofit foreign classes (rare but
// probably useful).]
//////////////////////
// The "meta" field //
//////////////////////
// Generic meta value of any kind, but the typical case is it's a
// Map with extra field values for the object etc.
// "meta" is volatile to avoid synchronization; but you can also synchronize on
// _tempMetaMutex() which is usually the object itself. Collections
// and maps are exempt from using the collections's monitor as the meta
// mutex because their monitor tends to be held for long operations
// (e.g. cloneList). For those we use a substantially more complex
// algorithm using a weakMap. Probably overkill. I may reconsider.
volatile Object meta;
// The meta field is not transient, thus by default it will be
// persisted like anything else unless you customize your object
// to suppress or modulate this.
// ...and the interface methods
public void _setMeta(Object meta) { this.meta = meta; }
public Object _getMeta() { return meta; }
// MOST functions are implemented in IMeta (default implementations)
}
// grayscale, actually
static class BWIntegralImage extends Meta implements IBWIntegralImage, IIntegralImage {
int w, h;
int[] data; // 1 entry per pixel
// constructors
BWIntegralImage() {}
BWIntegralImage(File f) { this(loadImage2(f)); }
BWIntegralImage(MakesBufferedImage img) { this(toBufferedImage(img)); }
BWIntegralImage(BufferedImage image) { grab(image); }
BWIntegralImage(BufferedImage image, Decolorizer decolorizer) { grab(image, decolorizer); }
BWIntegralImage(GrabbableIntPixels gp) { grab(gp); }
BWIntegralImage(GrabbableGrayPixels gp) { grab(gp); }
BWIntegralImage(BWImage img) {
grab(new GrabbableGrayPixels(img.pixels, img.width, img.height,
0, img.width));
}
// grab functions
void grab(BufferedImage image) { grab(image, null); }
void grab(BufferedImage image, Decolorizer decolorizer) {
if (image.getType() == BufferedImage.TYPE_BYTE_GRAY)
grab(grabbableGrayPixels(image));
else // rgb image
grab(grabbableIntPixels_fastOrSlow(image), decolorizer);
}
void grab(GrabbableIntPixels gp, Decolorizer decolorizer) {
if (decolorizer == null) { grab(gp); return; }
alloc(gp.w, gp.h);
int w = this.w, h = this.h;
int offset = gp.offset, sum = 0;
int[] image = gp.data;
for (int x = 0; x < w; x++) {
int packed = image[offset+x];
int brightness = decolorizer.toGrayScale(packed);
data[x] = (sum += brightness);
}
var ping = pingSource();
int scanlineExtra = gp.scanlineStride-w;
int iImage = offset+gp.scanlineStride, i = w;
for (int y = 1; y < h; y++) {
sum = 0;
for (int x = 0; x < w; x++) {
int packed = image[iImage];
int brightness = decolorizer.toGrayScale(packed);
sum += brightness;
data[i] = sum + data[i-w];
iImage++; i++;
}
iImage += scanlineExtra;
{ if (ping != null) ping.get(); }
} // for y
}
void grab(GrabbableIntPixels gp) {
alloc(gp.w, gp.h);
int w = this.w, h = this.h;
int offset = gp.offset, sum = 0;
int[] image = gp.data;
for (int x = 0; x < w; x++) {
int packed = image[offset+x];
int brightness = packedToBrightness(packed);
data[x] = (sum += brightness);
}
var ping = pingSource();
int scanlineExtra = gp.scanlineStride-w;
int iImage = offset+gp.scanlineStride, i = w;
for (int y = 1; y < h; y++) {
sum = 0;
for (int x = 0; x < w; x++) {
int packed = image[iImage];
int brightness = packedToBrightness(packed);
sum += brightness;
data[i] = sum + data[i-w];
iImage++; i++;
}
iImage += scanlineExtra;
{ if (ping != null) ping.get(); }
} // for y
}
void grab(GrabbableGrayPixels gp) {
alloc(gp.w, gp.h);
int offset = gp.offset, sum = 0;
byte[] image = gp.data;
for (int x = 0; x < w; x++) {
int brightness = image[offset+x];
data[x] = (sum += brightness);
}
var ping = pingSource();
int scanlineExtra = gp.scanlineStride-w;
int iImage = offset+gp.scanlineStride, i = w;
for (int y = 1; y < h; y++) {
sum = 0;
for (int x = 0; x < w; x++) {
int brightness = image[iImage];
sum += brightness;
data[i] = sum + data[i-w];
iImage++; i++;
}
iImage += scanlineExtra;
{ if (ping != null) ping.get(); }
} // for y
}
private void alloc(int w, int h) {
this.w = w;
this.h = h;
if (w*h > 8*1024*1024)
throw fail("Image too large (more than 8 MP): " + w + "*" + h);
data = new int[w*h];
}
// pixels outside of image are considered black
int get(int x, int y) {
return x < 0 || y < 0 ? 0
: data[min(y, h-1)*w+min(x, w-1)];
}
// precise version with anti-aliasing [TO TEST!]
public double getIIValue(double x, double y) {
int xFloor = ifloor(x), yFloor = ifloor(y);
double val = getIIValue(xFloor, yFloor);
// at integer coordinate?
if (xFloor == x && yFloor == y)
return val;
// at non-integer coordinate, perform subpixel calculation
double val2 = getIIValue(xFloor+1, yFloor);
double val3 = getIIValue(xFloor , yFloor+1);
double val4 = getIIValue(xFloor+1, yFloor+1);
return blend2D(val, val2, val3, val4, x-xFloor, y-yFloor);
}
public int getIIValue(int x, int y) { return get(x, y); }
public double getIntegralValue(int x, int y, int channel) {
return get(x, y);
}
public double getPixelAverage(int x1, int y1, int x2, int y2) {
int area = (x2-x1)*(y2-y1);
return doubleRatio(bwIntegralImage_sumRect(this, x1, y1, x2, y2), area);
}
public int getWidth() { return w; }
public int getHeight() { return h; }
int packedToBrightness(int packed) {
int b = (packed & 0xFF);
int r = ((packed >> 16) & 0xFF);
int g = ((packed >> 8) & 0xFF);
return (r+g+b+1)/3;
}
// returns RGB pixel without alpha
public 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);
}
public BufferedImage getBufferedImage() {
//ret scaleDownUsingIntegralImageBW(this, w).getBufferedImage();
Object src = metaGet("src", this);
if (src instanceof BufferedImage) return ((BufferedImage) src);
return grayImageFromIBWIntegralImage(this);
}
}
static interface Decolorizer {
int toGrayScale(int rgb);
static class Red implements Decolorizer {
public int toGrayScale(int rgb) { return (rgb >> 16) & 0xFF; }
}
static class Green implements Decolorizer {
public int toGrayScale(int rgb) { return (rgb >> 8) & 0xFF; }
}
static class Blue implements Decolorizer {
public int toGrayScale(int rgb) { return rgb & 0xFF; }
}
static class Alpha implements Decolorizer {
public int toGrayScale(int rgb) { return (rgb >> 24) & 0xFF; }
}
// just the average of the 3 channels
static class Simple implements Decolorizer {
public int toGrayScale(int rgb) {
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = rgb & 0xFF;
return (r+g+b+1)/3;
}
}
}
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() {}
};
}
// actually query another object
default public Object getMeta(Object obj, Object key){ return metaGet(obj, key); }
default public Object metaGet(Object obj, Object key) {
// call global function
return metaMapGet(obj, key);
}
default public Object metaGet(String key, Object obj) {
// call global function
return metaMapGet(obj, key);
}
default public Object getMeta(Object key){ return metaGet(key); }
default public Object metaGet(Object key) {
if (key == null) return null;
Object meta = _getMeta();
if (meta instanceof Map) return ((Map) meta).get(key);
return null;
}
default public void metaSet(IMeta obj, Object key, Object value){ metaPut(obj, key, value); }
default public void metaPut(IMeta obj, Object key, Object value) {
// call global function
metaMapPut(obj, key, value);
}
default public void metaSet(Object key, Object value){ metaPut(key, value); }
default public void metaPut(Object key, Object value) {
if (key == null) return;
Map map = convertObjectMetaToMap(this);
syncMapPutOrRemove(map, key, value);
}
}
static final class BWImage extends Meta implements MakesBufferedImage, IBWImage {
int width, height;
byte[] pixels;
// color returned when getPixel is called with a position outside the actual image
float borderColor = 0.0f;
// for unstructure()
BWImage() {}
// BLACK!
BWImage(int width, int height) {
this.height = height;
this.width = width;
pixels = new byte[width*height];
}
BWImage(int width, int height, float brightness) {
this.height = height;
this.width = width;
pixels = new byte[width*height];
fillArrayUnlessZero(pixels, _toByte(brightness));
}
BWImage(int width, int height, float[] pixels) {
this.pixels = new byte[pixels.length];
this.height = height;
this.width = width;
for (int i = 0; i < pixels.length; i++)
this.pixels[i] = _toByte(pixels[i]);
}
public BWImage(int width, int height, byte[] pixels) {
this.height = height;
this.width = width;
this.pixels = pixels;
}
public BWImage(BWImage image) {
width = image.getWidth();
height = image.getHeight();
byte[] pixels = this.pixels = new byte[width*height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
pixels[y*width+x] = image.getByte(x, y);
}
// TODO: optimize!
BWImage(RGBImage image) {
width = image.getWidth();
height = image.getHeight();
byte[] pixels = this.pixels = new byte[height*width];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
RGB rgb = image.getRGB(x, y);
pixels[y*width+x] = BWImage._toByte(rgb.getBrightness());
}
}
/*public BWImage(BufferedImage image) {
this(new RGBImage(image));
}*/
BWImage(BufferedImage image) { try {
width = image.getWidth();
height = image.getHeight();
int[] pixels = new int[width*height];
byte[] bytePixels = this.pixels = new byte[width*height];
PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
if (!pixelGrabber.grabPixels())
throw fail("Could not grab pixels");
int n = width*height;
for (int i = 0; i < n; i++) {
//bytePixels[i] = pixelToByte(pixels[i]);
int packed = pixels[i];
/*float r = ((packed >> 16) & 0xFF)/255f;
float g = ((packed >> 8) & 0xFF)/255f;
float b = (packed & 0xFF)/255f;
bytePixels[i] = (byte) iround((r+g+b)/3.0f*255f);*/
int r = ((packed >> 16) & 0xFF);
int g = ((packed >> 8) & 0xFF);
int b = (packed & 0xFF);
bytePixels[i] = (byte) ((r+g+b+1)/3);
}
} catch (Exception __e) { throw rethrow(__e); } }
// TODO: does it exactly match the other method? (asRGB+getBrightness+_toByte)
static byte pixelToByte(int packed) {
/*int r = (packed >> 16) & 0xFF;
int g = (packed >> 8) & 0xFF;
int b = packed & 0xFF;
ret (byte) ((r+g+b)/3.0f);*/
float r = ((packed >> 16) & 0xFF)/255f;
float g = ((packed >> 8) & 0xFF)/255f;
float b = (packed & 0xFF)/255f;
return (byte) ((r+g+b)/3.0f*255f);
}
public byte getByte(int x, int y) {
return inRange(x, y) ? getByte_noRangeCheck(x, y) : _toByte(borderColor);
}
// pretty bad function name
// gets brightness (0 to 255) at pixel
int getInt(int x, int y) {
return ubyteToInt(getByte(x, y));
}
public double averageBrightness() {
double sum = 0;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
sum += getPixel(x, y);
return (sum/(double) (height*width));
}
public float minimumBrightness() {
float min = 1;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
min = Math.min(min, getPixel(x, y));
return min;
}
public float maximumBrightness() {
float max = 0;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
max = Math.max(max, getPixel(x, y));
return max;
}
float getPixel(int x, int y) {
return inRange(x, y) ? _toFloat(getByte(x,y )) : borderColor;
}
public float getFloatPixel(int x, int y) { return getPixel(x, y); }
float getPixel(Pt p) { return getPixel(p.x, p.y); }
static byte _toByte(float pixel) {
return (byte) (pixel*255f);
}
static float _toFloat(byte pixel) {
return (((int) pixel) & 255)/255f;
}
private boolean inRange(int x, int y) {
return x >= 0 && x < width && y >= 0 && y < height;
}
public int getWidth() { return width; }
int w() { return width; }
public int getHeight() { return height; }
int h() { return height; }
public RGBImage toRGB() {
int[] rgbs = new int[width*height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
int b = getByte(x, y) & 0xFF;
rgbs[y*width+x] = 0xFF000000 | b*0x010101;
}
return new RGBImage(width, height, rgbs);
}
public RGBImage toRGB_slow() {
RGB[] rgbs = new RGB[width*height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
float p = getPixel(x, y);
rgbs[y*width+x] = new RGB(p, p, p);
}
return new RGBImage(width, height, rgbs);
}
public BWImage clip(int x, int y, int w, int h) {
return clip(new Rectangle(x, y, w, h));
}
private Rectangle fixClipRect(Rectangle r) {
return r.intersection(new Rectangle(0, 0, width, height));
}
BWImage clip(Rect r) {
return clip(r.getRectangle());
}
/** this should be multithread-safe */
public BWImage clip(Rectangle r) {
r = fixClipRect(r);
byte[] newPixels = new byte[r.height*r.width];
for (int y = 0; y < r.height; y++)
for (int x = 0; x < r.width; x++)
newPixels[y*r.width+x] = getByte(r.x+x, r.y+y);
return new BWImage(r.width, r.height, newPixels);
}
public void setPixel(int x, int y, float brightness) {
setByte(x, y, _toByte(fixPixel(brightness)));
}
// i = 0 to 255
public void setInt(int x, int y, int i) {
setByte(x, y, (byte) limitToUByte(i));
}
public void setByte(int x, int y, byte b) {
if (x >= 0 && x < width && y >= 0 && y < height)
pixels[y*width+x] = b;
}
byte getByte_noRangeCheck(int x, int y) {
return pixels[y*width+x];
}
public void setByte(int x, int y, int brightness) {
setByte(x, y, (byte) brightness);
}
private float fixPixel(float pixel) {
return Math.max(0, Math.min(1, pixel));
}
public float getBorderColor() {
return borderColor;
}
public void setBorderColor(float borderColor) {
this.borderColor = borderColor;
}
public boolean anyPixelBrighterThan(double threshold) {
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
if (getPixel(x, y) > threshold)
return true;
return false;
}
int[] getRGBPixels() {
int n = width*height;
int[] out = new int[n];
for (int i = 0; i < n; i++) {
var b = ubyteToInt(pixels[i]);
b |= (b << 8) | (b << 16);
out[i] = b | 0xFF000000;
}
return out;
}
public BufferedImage getBufferedImage() {
return bufferedImage(getRGBPixels(), width, height);
}
byte[] getBytes() {
return pixels;
}
}
static class GrabbableIntPixels implements IFieldsToList{
static final String _fieldOrder = "data w h offset scanlineStride";
int[] data;
int w;
int h;
int offset;
int scanlineStride;
GrabbableIntPixels() {}
GrabbableIntPixels(int[] data, int w, int h, int offset, int scanlineStride) {
this.scanlineStride = scanlineStride;
this.offset = offset;
this.h = h;
this.w = w;
this.data = data;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + data + ", " + w + ", " + h + ", " + offset + ", " + scanlineStride + ")"; }
public boolean equals(Object o) {
if (!(o instanceof GrabbableIntPixels)) return false;
GrabbableIntPixels __1 = (GrabbableIntPixels) o;
return eq(data, __1.data) && w == __1.w && h == __1.h && offset == __1.offset && scanlineStride == __1.scanlineStride;
}
public int hashCode() {
int h = -1183022196;
h = boostHashCombine(h, _hashCode(data));
h = boostHashCombine(h, _hashCode(w));
h = boostHashCombine(h, _hashCode(h));
h = boostHashCombine(h, _hashCode(offset));
h = boostHashCombine(h, _hashCode(scanlineStride));
return h;
}
public Object[] _fieldsToList() { return new Object[] {data, w, h, offset, scanlineStride}; }
}
static interface IBWIntegralImage extends MakesBufferedImage, IBWImage, IMeta {
public int getWidth();
public int getHeight();
// implement one of the following two methods!
// integer version (both coordinates & brightnesses are ints)
public default int getIIValue(int x, int y) {
return iround(getIIValue((double) x, (double) y));
}
// floating point version (both coordinates & brightnesses are doubles)
public default double getIIValue(double x, double y) {
return getIIValue(ifloor(x), ifloor(y));
}
// 0.0 to 255.0
default public double getPixelAverage(int x1, int y1, int x2, int y2) {
int area = (x2-x1)*(y2-y1);
return doubleRatio(getPixelSum(x1, y1, x2, y2), area);
}
default public double getPixelAverage(double x1, double y1, double x2, double y2) {
double area = (x2-x1)*(y2-y1);
return doubleRatio(getPixelSum(x1, y1, x2, y2), area);
}
default public double getPixelAverage(Rect r) {
return getPixelAverage(r.x, r.y, r.x2(), r.y2());
}
default public int getPixelSum(int x1, int y1, int x2, int y2) {
return bwIntegralImage_sumRect(this, x1, y1, x2, y2);
}
default public double getPixelSum(double x1, double y1, double x2, double y2) {
return bwIntegralImage_sumRect(this, x1, y1, x2, y2);
}
default public int getPixelBrightness(int x, int y) {
return getPixelSum(x, y, x+1, y+1);
}
default public int getPixelBrightness(Pt p) { return getPixelBrightness(p.x, p.y); }
public default BufferedImage getBufferedImage() {
//ret scaleDownUsingIntegralImageBW(this, w).getBufferedImage();
return grayImageFromIBWIntegralImage(this);
}
// implementing IBWImage
public default float getFloatPixel(int x, int y) {
return getPixelBrightness(x, y)/255f;
}
}
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 contains(Rectangle r) {
return rectContains(this, r);
}
boolean empty() { return w <= 0 || h <= 0; }
int getWidth() { return w; }
int getHeight() { return h; }
}
static interface IBWImage extends MakesBufferedImage {
float getFloatPixel(int x, int y); // usually between 0 and 1
default float getFloatPixel(Pt p) { return getFloatPixel(p.x, p.y); }
public default BufferedImage getBufferedImage() {
return grayImageFromIBWImage(this);
}
}
static class Pt implements Comparable, IDoublePt {
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;
}
double length() { return sqrt(x*x+y*y); }
Pt minus(Pt p) { return ptMinus(this, p); }
public double x_double() { return x; }
public double y_double() { return y; }
}
static class GrabbableGrayPixels implements IFieldsToList{
static final String _fieldOrder = "data w h offset scanlineStride";
byte[] data;
int w;
int h;
int offset;
int scanlineStride;
GrabbableGrayPixels() {}
GrabbableGrayPixels(byte[] data, int w, int h, int offset, int scanlineStride) {
this.scanlineStride = scanlineStride;
this.offset = offset;
this.h = h;
this.w = w;
this.data = data;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + data + ", " + w + ", " + h + ", " + offset + ", " + scanlineStride + ")"; }
public boolean equals(Object o) {
if (!(o instanceof GrabbableGrayPixels)) return false;
GrabbableGrayPixels __1 = (GrabbableGrayPixels) o;
return eq(data, __1.data) && w == __1.w && h == __1.h && offset == __1.offset && scanlineStride == __1.scanlineStride;
}
public int hashCode() {
int h = 1984426208;
h = boostHashCombine(h, _hashCode(data));
h = boostHashCombine(h, _hashCode(w));
h = boostHashCombine(h, _hashCode(h));
h = boostHashCombine(h, _hashCode(offset));
h = boostHashCombine(h, _hashCode(scanlineStride));
return h;
}
public Object[] _fieldsToList() { return new Object[] {data, w, h, offset, scanlineStride}; }
}
static interface IAutoCloseableF0 extends IF0, AutoCloseable {}
static class RGB {
// usually in range [0, 1]
public float r, g, b; // can't be final cause persistence
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(double[] rgb) {
this(rgb[0], rgb[1], rgb[2]);
}
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;
}
// TODO: 3-char version
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 int getInt(int i) {
return i == 0 ? redInt() : i == 1 ? greenInt() : blueInt();
}
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)));
}
int asInt() { return getColor().getRGB() & 0xFFFFFF; }
int getInt() { return getColor().getRGB() & 0xFFFFFF; }
int asIntWithAlpha() { return rgbInt(redInt(), greenInt(), blueInt()) | 0xFF000000; }
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();
}
int redInt() { return iround(r*255); }
int greenInt() { return iround(g*255); }
int blueInt() { return iround(b*255); }
static float brightnessToFloat(int brightness) { return brightness/255f; }
}
static class RGBImage implements MakesBufferedImage {
transient BufferedImage bufferedImage;
File file;
int width, height;
int[] pixels;
RGBImage() {}
RGBImage(BufferedImage image) {
this(image, null);
}
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(); // set upper byte to 0
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/** We assume it's a file name to load from */
RGBImage(String file) throws IOException {
this(new File(file));
}
RGBImage(Dimension size, Color color) {
this(size.width, size.height, color);
}
RGBImage(Dimension size, RGB color) {
this(size.width, size.height, color);
}
private void cleanPixels() {
for (int i = 0; i < pixels.length; i++)
pixels[i] &= 0xFFFFFF;
}
RGBImage(int width, int height, int[] pixels) {
this.width = width;
this.height = height;
this.pixels = pixels;
}
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);
}
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;
}
RGBImage(RGBImage image) {
this(image.width, image.height, copyPixels(image.pixels));
}
RGBImage(int width, int height, Color color) {
this(width, height, new RGB(color));
}
RGBImage(File file) throws IOException {
this(javax.imageio.ImageIO.read(file));
}
RGBImage(MakesBufferedImage img) {
this(toBufferedImage(img));
}
private 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);
}
/** alias of getRGB - I kept typing getPixel instead of getRGB all the time, so I finally created it */
RGB getPixel(int x, int y) {
return getRGB(x, y);
}
RGB getPixel(Pt p) { return getPixel(p.x, p.y); }
public int getWidth() { return width; }
public int getHeight() { return height; }
int w() { return width; }
int h() { return height; }
/** Attention: cached, i.e. does not change when image itself changes */
/** @NotNull */
public BufferedImage getBufferedImage() {
if (bufferedImage == null) {
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//bufferedImage.setData(Raster.createRaster(new SampleModel()));
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
bufferedImage.setRGB(x, y, pixels[y*width+x]);
}
return bufferedImage;
}
RGBImage clip(Rect r) {
return r == null ? null : clip(r.getRectangle());
}
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);
}
private 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;
}
/** can now also do GIF (not just JPEG) */
public static RGBImage load(String fileName) {
return load(new File(fileName));
}
/** can now also do GIF (not just JPEG) */
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;
}
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();
}
final public void set(int x, int y, Color color){ setPixel(x, y, color); }
public void setPixel(int x, int y, Color color) {
setPixel(x, y, new RGB(color));
}
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;
}
void setPixel(Pt p, RGB rgb) { setPixel(p.x, p.y, rgb); }
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;
}
void uncacheBufferedImage() {
bufferedImage = null;
}
}
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 defaultChannel() { return 0; }
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);
default double getIntegralValue(double x, double y, int channel) {
return getIntegralValue(ifloor(x), ifloor(y), channel);
}
// gets summed 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(double x1, double y1, double x2, double y2, int channel) {
return rectSum(iround(x1), iround(y1), iround(x2), iround(y2), channel);
}
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 double pixelSum(DoubleRect r) {
return rectSum(toRect_floor(r), defaultChannel());
}
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 RGB averageRGB() {
int w = getWidth(), h = getHeight();
double factor = 1/(255.0*(w*h));
return new RGB(
rectSum(0, 0, w, h, 0)*factor,
rectSum(0, 0, w, h, 1)*factor,
rectSum(0, 0, w, h, 2)*factor);
}
// normal range: (0, 255)
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 int nPixels() { return getWidth()*getHeight(); }
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);
}
}
static interface IF0 {
A get();
}
static interface IFieldsToList {
Object[] _fieldsToList();
}
interface IDoublePt {
public double x_double();
public double y_double();
}
static interface WidthAndHeight {
int getWidth();
int getHeight();
public default Rect bounds() { return rect(0, 0, getWidth(), getHeight()); }
}
static class DoubleRect {
double x, y, w, h;
DoubleRect() {}
DoubleRect(Rectangle r) {
x = r.x;
y = r.y;
w = r.width;
h = r.height;
}
DoubleRect(double x, double y, double w, double h) {
this.h = h;
this.w = w;
this.y = y;
this.x = x;}
// Huh. not implementing equals()/hashCode? Stefan is mysterious
boolean eq(Object o) {
if (!(o instanceof DoubleRect)) return false;
if (o == this) return true;
DoubleRect r = (DoubleRect) o;
return x == r.x && y == r.y && w == r.w && h == r.h;
}
public String toString() {
return x + "," + y + " / " + w + "," + h;
}
double x1() { return x; }
double y1() { return y; }
double x2() { return x + w; }
double y2() { return y + h; }
boolean contains(Pt p) {
return contains(p.x, p.y);
}
boolean contains(double _x, double _y) {
return _x >= x && _y >= y && _x < x+w && _y < y+h;
}
boolean empty() { return w <= 0 || h <= 0; }
}
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; }
public boolean has() { return value != null; }
public boolean set_trueIfChanged(A a) {
if (eq(a, value)) return false;
unindex();
value = a;
index();
return true;
}
public void set(A a) {
set_trueIfChanged(a);
}
void setIfEmpty(A a) {
if (!has()) set(a);
}
public void set(IRef ref) { set(ref == null ? null : ref.get()); }
public 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 IHasBackRefs {
public void _registerBackRef(IRef ref);
public void _unregisterBackRef(IRef ref);
}
// could almost add IVar here - but void set() and bool set() are incompatible in some places
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 BufferedImage loadImage2(String snippetIDOrURL) {
return loadBufferedImage(snippetIDOrURL);
}
static BufferedImage loadImage2(File file) {
return loadBufferedImage(file);
}
static BufferedImage toBufferedImage(Object o) {
return toBufferedImageOpt(o);
}
static Boolean grabbableGrayPixels_succeeded;
static Throwable grabbableGrayPixels_error;
static boolean grabbableGrayPixels_enable = true;
static GrabbableGrayPixels grabbableGrayPixels(BufferedImage img) {
if (img == null || !grabbableGrayPixels_enable) return null;
try {
var result = grabbableGrayPixels_impl(img);
grabbableGrayPixels_succeeded = result != null;
return result;
} catch (Throwable e) { grabbableGrayPixels_error = e;
grabbableGrayPixels_succeeded = false;
throw rethrow(e); }
}
static GrabbableGrayPixels grabbableGrayPixels_impl(BufferedImage img) {
Raster raster = img.getRaster();
SampleModel sampleModel = raster.getSampleModel();
if (!(sampleModel instanceof PixelInterleavedSampleModel)) return null;
DataBufferByte dataBuffer = (DataBufferByte) (raster.getDataBuffer());
assertEquals(1, dataBuffer.getNumBanks());
assertEquals(DataBuffer.TYPE_BYTE, dataBuffer.getDataType());
// Let's at this point assume the raster data is gray and not a palette
int w = img.getWidth(), h = img.getHeight();
int scanlineStride = ((PixelInterleavedSampleModel) sampleModel).getScanlineStride();
byte[] pixels = dataBuffer.getData();
int offset = dataBuffer.getOffset();
int translateX = raster.getSampleModelTranslateX();
int translateY = raster.getSampleModelTranslateY();
offset += -translateX-translateY*scanlineStride;
return new GrabbableGrayPixels(pixels, w, h, offset, scanlineStride);
}
static GrabbableIntPixels grabbableIntPixels_fastOrSlow(BufferedImage image) { try {
try {
GrabbableIntPixels gp = grabbableIntPixels(image);
if (gp != null) return gp;
} catch (Throwable __e) { printStackTrace(__e); }
// Use pixelGrabber if quick method fails
int w = image.getWidth(), h = image.getHeight();
int[] data = new int[w*h];
PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, w, h, data, 0, w);
if (!pixelGrabber.grabPixels())
throw fail("Could not grab pixels");
return new GrabbableIntPixels(data, w, h, 0, w);
} catch (Exception __e) { throw rethrow(__e); } }
// assumptions:
// -pingSource() stays constant within a stack frame (so you can cache it)
// -all changes happen with tempSetPingSource
// -multiple threads can share a PingSource
static PingSource pingSource() {
return pingSource_tl().get();
}
static PingSource pingSource(Thread thread) {
return pingSource_tl().get(thread);
}
static RuntimeException fail() { throw new RuntimeException("fail"); }
static RuntimeException fail(Throwable e) { throw asRuntimeException(e); }
static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); }
static RuntimeException fail(Object... objects) { throw new Fail(objects); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
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 short min(short[] c) {
short x = 0x7FFF;
for (short d : c) if (d < x) x = d;
return x;
}
static int min(int[] c) {
int x = Integer.MAX_VALUE;
for (int d : c) if (d < x) x = d;
return x;
}
static int ifloor(double d) {
return (int) Math.floor(d);
}
static IntRange ifloor(DoubleRange r) {
return r == null ? null : intRange(ifloor(r.start), ifloor(r.end));
}
static double blend2D(double val, double val2, double val3, double val4,
double xFrac, double yFrac) {
double upper = blend(val, val2, xFrac);
double lower = blend(val3, val4, xFrac);
return blend(upper, lower, yFrac);
}
// 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 double doubleRatio(double x, double y) {
return y == 0 ? 0 : x/y;
}
static double doubleRatio(Seconds x, Seconds y) {
return doubleRatio(x.get(), y.get());
}
static int bwIntegralImage_sumRect(BWIntegralImage img, int x1, int y1, int x2, int y2) {
int bottomLeft = img.getIIValue(x1-1, y2-1);
int bottomRight = img.getIIValue(x2-1, y2-1);
int topLeft = img.getIIValue(x1-1, y1-1);
int topRight = img.getIIValue(x2-1, y1-1);
return bottomRight+topLeft-topRight-bottomLeft;
}
static int bwIntegralImage_sumRect(IBWIntegralImage img, int x1, int y1, int x2, int y2) {
int bottomLeft = img.getIIValue(x1-1, y2-1);
int bottomRight = img.getIIValue(x2-1, y2-1);
int topLeft = img.getIIValue(x1-1, y1-1);
int topRight = img.getIIValue(x2-1, y1-1);
return bottomRight+topLeft-topRight-bottomLeft;
}
static double bwIntegralImage_sumRect(IBWIntegralImage img, double x1, double y1, double x2, double y2) {
double bottomLeft = img.getIIValue(x1-1, y2-1);
double bottomRight = img.getIIValue(x2-1, y2-1);
double topLeft = img.getIIValue(x1-1, y1-1);
double topRight = img.getIIValue(x2-1, y1-1);
return bottomRight+topLeft-topRight-bottomLeft;
}
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 Object metaGet(IMeta o, Object key) {
return metaMapGet(o, key);
}
static Object metaGet(Object o, Object key) {
return metaMapGet(o, key);
}
static Object metaGet(String key, IMeta o) {
return metaMapGet(o, key);
}
static Object metaGet(String key, Object o) {
return metaMapGet(o, key);
}
static BufferedImage grayImageFromIBWIntegralImage(IBWIntegralImage img) {
int w = img.getWidth(), h = img.getHeight();
byte[] pixels = new byte[w*h];
int i = 0;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
int pixel = img.getPixelBrightness(x, y);
pixels[i++] = clampToUByte(pixel);
}
return newGrayBufferedImage(w, h, pixels);
}
static Object metaMapGet(IMeta o, Object key) {
return o == null ? null : o.metaGet(key); // We now let the object itself do it (overridable!)
}
static Object metaMapGet(Object o, Object key) {
return metaMapGet(toIMeta(o), key);
}
static void metaPut(IMeta o, Object key, Object value) {
metaMapPut(o, key, value);
}
static void metaPut(Object o, Object key, Object value) {
metaMapPut(o, key, value);
}
static Map convertObjectMetaToMap(IMeta o) { return convertObjectMetaToMap(o, () -> makeObjectMetaMap()); }
static Map convertObjectMetaToMap(IMeta o, IF0