srecord noeq RawBufferedImageDistance_sameSize(BufferedImage img1, BufferedImage img2) { settable long stoppingValue = Int.MAX_VALUE; int w, h; int x, y; // where we stopped calculating long sum; bool completed() { ret 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 y to h: { for x to w: { 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; ret sum; } } iPixels1 += gp1.scanlineStride; iPixels2 += gp2.scanlineStride; } this.x = w; this.y = h; ret this.sum = sum; } }