Transpiled version (11264L) is out of date.
1 | persistable sclass Image2BAsLongs is IBinaryImage { |
2 | int w, h; |
3 | gettable long[] pixels; |
4 | |
5 | *(int *w, int *h, long[] *pixels) { cleanPixelArray(); } |
6 | |
7 | *(Image2B img) { |
8 | w = img.getWidth(); |
9 | h = img.getHeight(); |
10 | pixels = longArrayFromBytes_littleEndian_flexLength(img.pixels); |
11 | } |
12 | |
13 | *(Image2BAsLongs img) { |
14 | w = img.getWidth(); |
15 | h = img.getHeight(); |
16 | pixels = cloneLongArray(img.pixels); |
17 | } |
18 | |
19 | *(RGBImage img) { |
20 | w = img.getWidth(); |
21 | h = img.getHeight(); |
22 | pixels = new long[(w*h+63)/64]; |
23 | for y to h: for x to w: |
24 | if (img.getPixel(x, y).getBrightness() >= 0.5f) { |
25 | int i = y*w+x; |
26 | pixels[i/64] |= 1L << (i & 63); |
27 | } |
28 | } |
29 | |
30 | *(BWImage img) { |
31 | this(img, 128); |
32 | } |
33 | |
34 | // >= threshold |
35 | *(BWImage img, int threshold) { |
36 | w = img.w(); h = img.h(); |
37 | int n = w*h; |
38 | int nOut = (n+63)/64; |
39 | long[] pixels = this.pixels = new long[nOut]; |
40 | byte[] bwPixels = img.pixels; |
41 | int iIn = 0; |
42 | |
43 | // do the bulk |
44 | for (int iOut = 0; iOut < nOut-1; iOut++) { |
45 | long value = 0; |
46 | for bit to 64: { |
47 | value >>>= 1; |
48 | if (ubyteToInt(bwPixels[iIn++]) >= threshold) |
49 | value |= 0x80000000; |
50 | } |
51 | pixels[iOut] = value; |
52 | } |
53 | |
54 | // do last (up to 63) bits |
55 | for (; iIn < n; iIn++) |
56 | if (ubyteToInt(bwPixels[iIn]) >= threshold) |
57 | pixels[nOut-1] |= 1L << (iIn & 63); |
58 | } |
59 | |
60 | *(BufferedImage img) { |
61 | this(img, 128); |
62 | } |
63 | |
64 | *(BufferedImage img, int threshold) { |
65 | this(BWImage(img), threshold); |
66 | } |
67 | |
68 | // initializes with black |
69 | *(int *w, int *h) { |
70 | pixels = new long[(w*h+63)/64]; |
71 | } |
72 | |
73 | RGBImage toRGB() { |
74 | RGBImage img = new RGBImage(w, h, Color.black); |
75 | for y to h: for x to w: { |
76 | int i = y*w+x; |
77 | if ((pixels[i/64] & (1L << (i & 63))) != 0) |
78 | img.setPixel(x, y, Color.white); |
79 | } |
80 | ret img; |
81 | } |
82 | |
83 | BWImage toBW() { |
84 | BWImage img = new BWImage(w, h, 0f); |
85 | for y to h: for x to w: { |
86 | int i = y*w+x; |
87 | if ((pixels[i/64] & (1L << (i & 63))) != 0) |
88 | img.setPixel(x, y, 1f); |
89 | } |
90 | ret img; |
91 | } |
92 | |
93 | public BufferedImage getBufferedImage() { ret toBW().getBufferedImage(); } |
94 | |
95 | // x and y must be inside the image |
96 | public bool getPixel aka getBoolPixel(int x, int y) { |
97 | int i = y*w+x; |
98 | ret (pixels[i/64] & (1L << (i & 63))) != 0; |
99 | } |
100 | |
101 | // defaultColor is color outside of image |
102 | public bool getPixel aka getBoolPixel(int x, int y, bool defaultColor) { |
103 | if (x < 0 || y < 0 || x >= w || y >= h) ret defaultColor; |
104 | ret getPixel(x, y); |
105 | } |
106 | |
107 | public void setPixel(int x, int y, bool b) { |
108 | int i = y*w+x; |
109 | long val = pixels[i/64], shifted = 1L << (i & 63); |
110 | val = b ? val | shifted : val & ~shifted; |
111 | pixels[i/64] = val; |
112 | } |
113 | |
114 | void setPixel(int x, int y) { |
115 | int i = y*w+x; |
116 | pixels[i/64] |= 1L << (i & 63); |
117 | } |
118 | |
119 | public int getWidth() { ret w; } |
120 | public int getHeight() { ret h; } |
121 | |
122 | toString { |
123 | ret "Image2B " + str_px(w, h); |
124 | } |
125 | |
126 | // clear unused bits in pixel array after we received |
127 | // a possibly dirty array |
128 | void cleanPixelArray { |
129 | int n = w*h; |
130 | if ((n & 63) != 0) |
131 | pixels[n/64] &= (1L << (n & 63))-1; |
132 | } |
133 | } |
Began life as a copy of #1036036
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): elmgxqgtpvxh, mqqgnosmbjvj, wnsclhtenguj
No comments. add comment
Snippet ID: | #1036040 |
Snippet name: | Image2BAsLongs - image in 2 bit format (just black and white) using long[] instead of byte[] (even faster than int[]?, dev.) |
Eternal ID of this version: | #1036040/5 |
Text MD5: | 30110917e1a62abbbaa2bbac0df260d7 |
Author: | stefan |
Category: | javax / imaging |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-09-03 03:26:11 |
Source code size: | 3287 bytes / 133 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 174 / 252 |
Version history: | 4 change(s) |
Referenced in: | [show references] |