Transpiled version (10946L) is out of date.
persistable sclass Image2B is IBinaryImage { int w, h; byte[] pixels; *(int *w, int *h, byte[] *pixels) { cleanPixelArray(); } *(Image2B img) { w = img.getWidth(); h = img.getHeight(); pixels = cloneByteArray(img.pixels); } *(RGBImage img) { w = img.getWidth(); h = img.getHeight(); pixels = new byte[(w*h+7)/8]; for y to h: for x to w: if (img.getPixel(x, y).getBrightness() >= 0.5f) { int i = y*w+x; pixels[i/8] |= 1 << (i & 7); } } *(BWImage img) { this(img, 128); } // >= threshold *(BWImage img, int threshold) { w = img.w(); h = img.h(); int n = w*h; int nOut = (n+7)/8; byte[] pixels = this.pixels = new byte[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 8: { value >>= 1; if (ubyteToInt(bwPixels[iIn++]) >= threshold) value |= 0x80; } pixels[iOut] = (byte) value; } // do last (up to 7) bits for (; iIn < n; iIn++) if (ubyteToInt(bwPixels[iIn]) >= threshold) pixels[nOut-1] |= 1 << (iIn & 7); } *(BufferedImage img) { this(img, 128); } *(BufferedImage img, int threshold) { this(BWImage(img), threshold); } // initializes with black *(int *w, int *h) { pixels = new byte[(w*h+7)/8]; } 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/8] & (1 << (i & 7))) != 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/8] & (1 << (i & 7))) != 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/8] & (1 << (i & 7))) != 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; byte val = pixels[i/8], shifted = (byte) (1 << (i & 7)); val = (byte) (b ? val | shifted : val & ~shifted); pixels[i/8] = val; } void setPixel(int x, int y) { int i = y*w+x; pixels[i/8] |= 1 << (i & 7); } 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 & 7) != 0) pixels[n/8] &= (1 << (n & 7))-1; } public double averageBrightness() { ret doubleRatio(bitCount(pixels), w*h); } }
Began life as a copy of #1005378
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1006576 |
Snippet name: | Image2B - image in 2 bit format (just black and white) |
Eternal ID of this version: | #1006576/37 |
Text MD5: | 2efb245e48159fe641a9b0d3fe99b439 |
Author: | stefan |
Category: | javax / imaging |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-09-08 23:05:10 |
Source code size: | 3191 bytes / 131 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 648 / 1817 |
Version history: | 36 change(s) |
Referenced in: | #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) #1036036 - Image2BAsInts - image in 2 bit format (just black and white) using int[] instead of byte[] (way faster) |