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 static x30_pkg.x30_util.DynamicObject;
import java.awt.geom.*;
class main {
static class RawBufferedImageDistance_sameSize implements IFieldsToList{
BufferedImage img1;
BufferedImage img2;
RawBufferedImageDistance_sameSize() {}
RawBufferedImageDistance_sameSize(BufferedImage img1, BufferedImage img2) {
this.img2 = img2;
this.img1 = img1;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + img1 + ", " + img2 + ")"; }public Object[] _fieldsToList() { return new Object[] {img1, img2}; }
final public RawBufferedImageDistance_sameSize setStoppingValue(long stoppingValue){ return stoppingValue(stoppingValue); }
public RawBufferedImageDistance_sameSize stoppingValue(long stoppingValue) { this.stoppingValue = stoppingValue; return this; } final public long getStoppingValue(){ return stoppingValue(); }
public long stoppingValue() { return stoppingValue; }
long stoppingValue;
int w, h;
int x, y; // where we stopped calculating
long sum;
boolean completed() { return w != 0 && x == w && y == h; }
long get() {
int w = this.w = img1.getWidth(), h = this.h = img1.getHeight();
assertSameSize(img1, img2);
var gp1 = grabbableIntPixels_fastOrSlow(img1);
var gp2 = grabbableIntPixels_fastOrSlow(img2);
int iPixels1 = gp1.offset, iPixels2 = gp2.offset;
int[] pixels1 = gp1.data, pixels2 = gp2.data;
long sum = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
sum += rawRGBDiff(
pixels1[iPixels1+x],
pixels2[iPixels2+x]);
// cancel if difference out of interest range
if (sum > stoppingValue) {
this.x = x; this.y = y;
this.sum = sum;
return sum;
}
}
iPixels1 += gp1.scanlineStride;
iPixels2 += gp2.scanlineStride;
}
this.x = w; this.y = h;
return this.sum = sum;
}
}
static String shortClassName_dropNumberPrefix(Object o) {
return dropNumberPrefix(shortClassName(o));
}
static void assertSameSize(BufferedImage a, BufferedImage b) {
assertEquals(
pair(a.getWidth(), a.getHeight()),
pair(b.getWidth(), b.getHeight()));
}
static void assertSameSize(WidthAndHeight a, WidthAndHeight b) {
assertEquals(
pair(a.getWidth(), a.getHeight()),
pair(b.getWidth(), b.getHeight()));
}
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); } }
// 0xRRGGBB
static int rawRGBDiff(int a, int b) {
int r1 = (a >> 16) & 0xFF;
int g1 = (a >> 8) & 0xFF;
int b1 = a & 0xFF;
int r2 = (b >> 16) & 0xFF;
int g2 = (b >> 8) & 0xFF;
int b2 = b & 0xFF;
return Math.abs(r1-r2) + Math.abs(g1-g2) + Math.abs(b1-b2);
}
static String dropNumberPrefix(String s) {
return dropFirst(s, indexOfNonDigit(s));
}
static String shortClassName(Object o) {
if (o == null) return null;
Class c = o instanceof Class ? (Class) o : o.getClass();
String name = c.getName();
return shortenClassName(name);
}
static A assertEquals(Object x, A y) {
return assertEquals("", x, y);
}
static A assertEquals(String msg, Object x, A y) {
if (assertVerbose()) return assertEqualsVerbose(msg, x, y);
if (!(x == null ? y == null : x.equals(y)))
throw fail((msg != null ? msg + ": " : "") + y + " != " + x);
return y;
}
static Pair pair(A a, B b) {
return new Pair(a, b);
}
static Pair pair(A a) {
return new Pair(a, a);
}
static Boolean grabbableIntPixels_succeeded;
static boolean grabbableIntPixels_enable = true;
static GrabbableIntPixels grabbableIntPixels(BufferedImage img) {
if (img == null || !grabbableIntPixels_enable) return null;
try {
var result = grabbableIntPixels_impl(img);
grabbableIntPixels_succeeded = result != null;
return result;
} catch (Throwable _e) {
grabbableIntPixels_succeeded = false;
throw rethrow(_e); }
}
static GrabbableIntPixels grabbableIntPixels_impl(BufferedImage img) {
Raster raster = img.getRaster();
SampleModel _sampleModel = raster.getSampleModel();
if (!(_sampleModel instanceof SinglePixelPackedSampleModel)) return null;
SinglePixelPackedSampleModel sampleModel = (SinglePixelPackedSampleModel) _sampleModel;
DataBufferInt dataBuffer = (DataBufferInt) (raster.getDataBuffer());
assertEquals(1, dataBuffer.getNumBanks());
assertEquals(DataBuffer.TYPE_INT, dataBuffer.getDataType());
// Let's at this point assume the raster data is what we
// think it is... (TODO: test on unusual platforms)
int w = img.getWidth(), h = img.getHeight();
int scanlineStride = sampleModel.getScanlineStride();
int[] pixels = dataBuffer.getData();
int offset = dataBuffer.getOffset();
int translateX = raster.getSampleModelTranslateX();
int translateY = raster.getSampleModelTranslateY();
offset += -translateX-translateY*scanlineStride;
return new GrabbableIntPixels(pixels, w, h, offset, scanlineStride);
}
static A printStackTrace(A e) {
// we go to system.out now - system.err is nonsense
if (e != null) print(getStackTrace(e));
return e;
}
static void printStackTrace() {
printStackTrace(new Throwable());
}
static void printStackTrace(String msg) {
printStackTrace(new Throwable(msg));
}
static void printStackTrace(String msg, Throwable e) {
printStackTrace(new Throwable(msg, e));
}
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 RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static String[] dropFirst(int n, String[] a) {
return drop(n, a);
}
static String[] dropFirst(String[] a) {
return drop(1, a);
}
static Object[] dropFirst(Object[] a) {
return drop(1, a);
}
static List dropFirst(List l) {
return dropFirst(1, l);
}
static List dropFirst(int n, Iterable i) { return dropFirst(n, toList(i)); }
static List dropFirst(Iterable i) { return dropFirst(toList(i)); }
static List dropFirst(int n, List l) {
return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size()));
}
static List dropFirst(List l, int n) {
return dropFirst(n, l);
}
static String dropFirst(int n, String s) { return substring(s, n); }
static String dropFirst(String s, int n) { return substring(s, n); }
static String dropFirst(String s) { return substring(s, 1); }
static int indexOfNonDigit(String s) {
int n = l(s);
for (int i = 0; i < n; i++)
if (!isDigit(s.charAt(i)))
return i;
return -1;
}
static String shortenClassName(String name) {
if (name == null) return null;
int i = lastIndexOf(name, "$");
if (i < 0) i = lastIndexOf(name, ".");
return i < 0 ? name : substring(name, i+1);
}
static ThreadLocal assertVerbose_value = new ThreadLocal();
static void assertVerbose(boolean b) {
assertVerbose_value.set(b);
}
static boolean assertVerbose() { return isTrue(assertVerbose_value.get()); }
static A assertEqualsVerbose(Object x, A y) {
assertEqualsVerbose((String) null, x, y);
return y;
}
// x = expected, y = actual
static A assertEqualsVerbose(String msg, Object x, A y) {
if (!eq(x, y)) {
throw fail((nempty(msg) ? msg + ": " : "") + "expected: "+ x + ", got: " + y);
} else
print("OK" + (empty(msg) ? "" : " " + msg) + ": " + /*sfu*/(x));
return y;
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
// a little kludge for stuff like eq(symbol, "$X")
static boolean eq(Symbol a, String b) {
return eq(str(a), b);
}
static String nullIfEmpty(String s) {
return isEmpty(s) ? null : s;
}
static Map nullIfEmpty(Map map) {
return isEmpty(map) ? null : map;
}
static List nullIfEmpty(List l) {
return isEmpty(l) ? null : l;
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal