persistable sclass Image2BAsInts is IBinaryImage { int w, h; int[] pixels; *(int *w, int *h, int[] *pixels) { cleanPixelArray(); } *(Image2B img) { w = img.getWidth(); h = img.getHeight(); pixels = intArrayFromBytes_littleEndian_flexLength(img.pixels); } *(Image2BAsInts img) { w = img.getWidth(); h = img.getHeight(); pixels = cloneIntArray(img.pixels); } *(RGBImage img) { w = img.getWidth(); h = img.getHeight(); pixels = new int[(w*h+31)/32]; for y to h: for x to w: if (img.getPixel(x, y).getBrightness() >= 0.5f) { int i = y*w+x; pixels[i/32] |= 1 << (i & 31); } } *(BWImage img) { this(img, 1232); } // >= threshold *(BWImage img, int threshold) { w = img.w(); h = img.h(); int n = w*h; int nOut = (n+31)/32; int[] pixels = this.pixels = new int[nOut]; byte[] bwPixels = img.pixels; int iIn = 0; // do the bulk for (int iOut = 0; iOut < nOut-1; iOut++) { int value = 0; for bit to 32: { value >>>= 1; if (ubyteToInt(bwPixels[iIn++]) >= threshold) value |= 0x80000000; } pixels[iOut] = value; } // do last (up to 31) bits for (; iIn < n; iIn++) if (ubyteToInt(bwPixels[iIn]) >= threshold) pixels[nOut-1] |= 1 << (iIn & 31); } *(BufferedImage img) { this(img, 128); } *(BufferedImage img, int threshold) { this(BWImage(img), threshold); } // initializes with black *(int *w, int *h) { pixels = new int[(w*h+31)/32]; } RGBImage toRGB() { RGBImage img = new RGBImage(w, h, Color.black); for y to h: for x to w: { int i = y*w+x; if ((pixels[i/32] & (1 << (i & 31))) != 0) img.setPixel(x, y, Color.white); } ret img; } BWImage toBW() { BWImage img = new BWImage(w, h, 0f); for y to h: for x to w: { int i = y*w+x; if ((pixels[i/32] & (1 << (i & 31))) != 0) img.setPixel(x, y, 1f); } ret img; } public BufferedImage getBufferedImage() { ret toBW().getBufferedImage(); } // x and y must be inside the image public bool getPixel aka getBoolPixel(int x, int y) { int i = y*w+x; ret (pixels[i/32] & (1 << (i & 31))) != 0; } // defaultColor is color outside of image public bool getPixel aka getBoolPixel(int x, int y, bool defaultColor) { if (x < 0 || y < 0 || x >= w || y >= h) ret defaultColor; ret getPixel(x, y); } public void setPixel(int x, int y, bool b) { int i = y*w+x; int val = pixels[i/32], shifted = (int) (1 << (i & 31)); val = (int) (b ? val | shifted : val & ~shifted); pixels[i/32] = val; } void setPixel(int x, int y) { int i = y*w+x; pixels[i/32] |= 1 << (i & 31); } public int getWidth() { ret w; } public int getHeight() { ret h; } toString { ret "Image2B " + str_px(w, h); } // clear unused bits in pixel array after we received // a possibly dirty array void cleanPixelArray { int n = w*h; if ((n & 31) != 0) pixels[n/32] &= (1 << (n & 31))-1; } }