1 | static class BWImage implements MakesBufferedImage { |
2 | private int width, height; |
3 | private BWImageSimpleStorage storage; |
4 | |
5 | // color returned when getPixel is called with a position outside the actual image |
6 | private float borderColor = 0.0f; |
7 | |
8 | // for unstructure() |
9 | BWImage() {} |
10 | |
11 | // BLACK! [Inefficient...] |
12 | public BWImage(int width, int height) { |
13 | this.width = width; |
14 | this.height = height; |
15 | byte[] bytePixels = new byte[width*height]; |
16 | storage = makeStorage(width, height, bytePixels); |
17 | } |
18 | |
19 | public BWImage(int width, int height, float[] pixels) { |
20 | this.width = width; |
21 | this.height = height; |
22 | byte[] bytePixels = new byte[pixels.length]; |
23 | for (int i = 0; i < pixels.length; i++) |
24 | bytePixels[i] = toByte(pixels[i]); |
25 | storage = makeStorage(width, height, bytePixels); |
26 | } |
27 | |
28 | private BWImageSimpleStorage makeStorage(int width, int height, byte[] bytePixels) { |
29 | return new BWImageSimpleStorage(width, height, bytePixels); |
30 | } |
31 | |
32 | public BWImage(int width, int height, byte[] pixels) { |
33 | this.height = height; |
34 | this.width = width; |
35 | storage = makeStorage(width, height, pixels); |
36 | } |
37 | |
38 | public BWImage(BWImage image) { |
39 | width = image.getWidth(); |
40 | height = image.getHeight(); |
41 | byte[] bytePixels = new byte[width*height]; |
42 | for (int y = 0; y < height; y++) |
43 | for (int x = 0; x < width; x++) |
44 | bytePixels[y*width+x] = image.getByte(x, y); |
45 | storage = makeStorage(width, height, bytePixels); |
46 | } |
47 | |
48 | public BWImage(RGBImage image) { |
49 | width = image.getWidth(); |
50 | height = image.getHeight(); |
51 | byte[] result = new byte[height*width]; |
52 | for (int y = 0; y < height; y++) |
53 | for (int x = 0; x < width; x++) { |
54 | RGB rgb = image.getRGB(x, y); |
55 | result[y*width+x] = BWImage.toByte(rgb.getBrightness()); |
56 | } |
57 | storage = makeStorage(width, height, result); |
58 | } |
59 | |
60 | /*public BWImage(BufferedImage image) { |
61 | this(new RGBImage(image)); |
62 | }*/ |
63 | |
64 | *(BufferedImage image) ctex { |
65 | width = image.getWidth(); |
66 | height = image.getHeight(); |
67 | int[] pixels = new int[width*height]; |
68 | byte[] bytePixels = new byte[width*height]; |
69 | PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width); |
70 | if (!pixelGrabber.grabPixels()) |
71 | fail("Could not grab pixels"); |
72 | int n = width*height; |
73 | |
74 | for (int i = 0; i < n; i++) { |
75 | //bytePixels[i] = pixelToByte(pixels[i]); |
76 | int packed = pixels[i]; |
77 | /*float r = ((packed >> 16) & 0xFF)/255f; |
78 | float g = ((packed >> 8) & 0xFF)/255f; |
79 | float b = (packed & 0xFF)/255f; |
80 | bytePixels[i] = (byte) iround((r+g+b)/3.0f*255f);*/ |
81 | int r = ((packed >> 16) & 0xFF); |
82 | int g = ((packed >> 8) & 0xFF); |
83 | int b = (packed & 0xFF); |
84 | bytePixels[i] = (byte) ((r+g+b+1)/3); |
85 | } |
86 | storage = makeStorage(width, height, bytePixels); |
87 | } |
88 | |
89 | // TODO: does it exactly match the other method? (asRGB+getBrightness+toByte) |
90 | static byte pixelToByte(int packed) { |
91 | /*int r = (packed >> 16) & 0xFF; |
92 | int g = (packed >> 8) & 0xFF; |
93 | int b = packed & 0xFF; |
94 | ret (byte) ((r+g+b)/3.0f);*/ |
95 | float r = ((packed >> 16) & 0xFF)/255f; |
96 | float g = ((packed >> 8) & 0xFF)/255f; |
97 | float b = (packed & 0xFF)/255f; |
98 | ret (byte) ((r+g+b)/3.0f*255f); |
99 | } |
100 | |
101 | public byte getByte(int x, int y) { |
102 | return inRange(x, y) ? storage.getByte(x, y) : toByte(borderColor); |
103 | } |
104 | |
105 | public BWImage(int width, int height, float brightness) { |
106 | this.width = width; |
107 | this.height = height; |
108 | byte b = toByte(brightness); |
109 | byte[] pixels = new byte[width*height]; |
110 | for (int i = 0; i < pixels.length; i++) |
111 | pixels[i] = b; |
112 | storage = makeStorage(width, height, pixels); |
113 | } |
114 | |
115 | public double averageBrightness() { |
116 | double sum = 0; |
117 | for (int y = 0; y < height; y++) |
118 | for (int x = 0; x < width; x++) |
119 | sum += getPixel(x, y); |
120 | return (sum/(double) (height*width)); |
121 | } |
122 | |
123 | public float minimumBrightness() { |
124 | float min = 1; |
125 | for (int y = 0; y < height; y++) |
126 | for (int x = 0; x < width; x++) |
127 | min = Math.min(min, getPixel(x, y)); |
128 | return min; |
129 | } |
130 | |
131 | public float maximumBrightness() { |
132 | float max = 0; |
133 | for (int y = 0; y < height; y++) |
134 | for (int x = 0; x < width; x++) |
135 | max = Math.max(max, getPixel(x, y)); |
136 | return max; |
137 | } |
138 | |
139 | float getPixel(int x, int y) { |
140 | return inRange(x, y) ? toFloat(storage.getByte(x,y )) : borderColor; |
141 | } |
142 | |
143 | float getPixel(Pt p) { ret getPixel(p.x, p.y); } |
144 | |
145 | public static byte toByte(float pixel) { |
146 | return (byte) (pixel*255f); |
147 | } |
148 | |
149 | public static float toFloat(byte pixel) { |
150 | return (((int) pixel) & 255)/255f; |
151 | } |
152 | |
153 | private boolean inRange(int x, int y) { |
154 | return x >= 0 && x < width && y >= 0 && y < height; |
155 | } |
156 | |
157 | public int getWidth() { return width; } |
158 | int w() { return width; } |
159 | |
160 | public int getHeight() { return height; } |
161 | int h() { return height; } |
162 | |
163 | public RGBImage toRGB() { |
164 | int[] rgbs = new int[width*height]; |
165 | for (int y = 0; y < height; y++) |
166 | for (int x = 0; x < width; x++) { |
167 | int b = getByte(x, y) & 0xFF; |
168 | rgbs[y*width+x] = 0xFF000000 | b*0x010101; |
169 | } |
170 | return new RGBImage(width, height, rgbs); |
171 | } |
172 | |
173 | public RGBImage toRGB_slow() { |
174 | RGB[] rgbs = new RGB[width*height]; |
175 | for (int y = 0; y < height; y++) |
176 | for (int x = 0; x < width; x++) { |
177 | float p = getPixel(x, y); |
178 | rgbs[y*width+x] = new RGB(p, p, p); |
179 | } |
180 | return new RGBImage(width, height, rgbs); |
181 | } |
182 | |
183 | |
184 | public BWImage clip(int x, int y, int w, int h) { |
185 | return clip(new Rectangle(x, y, w, h)); |
186 | } |
187 | |
188 | private Rectangle fixClipRect(Rectangle r) { |
189 | return r.intersection(new Rectangle(0, 0, width, height)); |
190 | } |
191 | |
192 | BWImage clip(Rect r) { |
193 | ret clip(r.getRectangle()); |
194 | } |
195 | |
196 | /** this should be multithread-safe */ |
197 | public BWImage clip(Rectangle r) { |
198 | r = fixClipRect(r); |
199 | byte[] newPixels = new byte[r.height*r.width]; |
200 | for (int y = 0; y < r.height; y++) |
201 | for (int x = 0; x < r.width; x++) |
202 | newPixels[y*r.width+x] = getByte(r.x+x, r.y+y); |
203 | return new BWImage(r.width, r.height, newPixels); |
204 | } |
205 | |
206 | public void setPixel(int x, int y, float brightness) { |
207 | storage.setByte(x, y, toByte(fixPixel(brightness))); |
208 | } |
209 | |
210 | public void setByte(int x, int y, int brightness) { |
211 | storage.setByte(x, y, (byte) brightness); |
212 | } |
213 | |
214 | private float fixPixel(float pixel) { |
215 | return Math.max(0, Math.min(1, pixel)); |
216 | } |
217 | |
218 | public float getBorderColor() { |
219 | return borderColor; |
220 | } |
221 | |
222 | public void setBorderColor(float borderColor) { |
223 | this.borderColor = borderColor; |
224 | } |
225 | |
226 | public boolean anyPixelBrighterThan(double threshold) { |
227 | for (int y = 0; y < height; y++) |
228 | for (int x = 0; x < width; x++) |
229 | if (getPixel(x, y) > threshold) |
230 | return true; |
231 | return false; |
232 | } |
233 | |
234 | public BufferedImage getBufferedImage() { |
235 | //ret toRGB().getBufferedImage(); |
236 | |
237 | // TYPE_BYTE_GRAY is buggy - see #1015235 |
238 | BufferedImage bufferedImage = new BufferedImage(width, height, /*BufferedImage.TYPE_BYTE_GRAY*/BufferedImage.TYPE_INT_RGB); |
239 | for (int y = 0; y < height; y++) |
240 | for (int x = 0; x < width; x++) { |
241 | int b = ((int) getByte(x, y) & 0xFF); |
242 | bufferedImage.setRGB(x, y, b*0x010101); |
243 | } |
244 | return bufferedImage; |
245 | } |
246 | |
247 | byte[] getBytes() { |
248 | ret storage.pixels; |
249 | } |
250 | } |
251 | |
252 | static final class BWImageSimpleStorage { |
253 | int width, height; |
254 | byte[] pixels; |
255 | |
256 | // for unstructure() |
257 | BWImageSimpleStorage() {} |
258 | |
259 | public BWImageSimpleStorage(int width, int height, byte[] pixels) { |
260 | this.width = width; |
261 | this.height = height; |
262 | this.pixels = pixels; |
263 | } |
264 | |
265 | public void setByte(int x, int y, byte b) { |
266 | pixels[y*width+x] = b; |
267 | } |
268 | |
269 | public byte getByte(int x, int y) { |
270 | return pixels[y*width+x]; |
271 | } |
272 | } |
Began life as a copy of #1004247
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1016912 |
Snippet name: | class BWImage with BWImageSimpleStorage (old) |
Eternal ID of this version: | #1016912/1 |
Text MD5: | 21be331476307553690aa01aa509b7d4 |
Author: | stefan |
Category: | javax / imaging |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-07-07 16:42:53 |
Source code size: | 7985 bytes / 272 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 400 / 415 |
Referenced in: | [show references] |