Libraryless. Click here for Pure Java version (6162L/38K).
1 | final sclass BWIntegralImage implements MakesBufferedImage, IBWIntegralImage { |
2 | int w, h; |
3 | int[] data; // 1 entry per pixel |
4 | ifdef BWIntegralImage_CountAccesses |
5 | long accesses; |
6 | endifdef |
7 | |
8 | *() {} |
9 | *(File f) { this(loadImage2(f)); } |
10 | |
11 | *(MakesBufferedImage img) { this(toBufferedImage(img)); } |
12 | |
13 | //*(BufferedImage img) { this(BWImage(img)); } |
14 | |
15 | *(BufferedImage image) ctex { |
16 | alloc(image.getWidth(), image.getHeight()); |
17 | // Just grab directly into data array |
18 | PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, w, h, data, 0, w); |
19 | if (!pixelGrabber.grabPixels()) |
20 | fail("Could not grab pixels"); |
21 | |
22 | int sum = 0; |
23 | for x to w: { |
24 | int packed = data[x]; |
25 | int brightness = packedToBrightness(packed); |
26 | data[x] = (sum += brightness); |
27 | } |
28 | |
29 | ifdef BWIntegralImage_useVectorAPI |
30 | import jdk.incubator.vector.*; |
31 | VectorSpecies<Int> species = IntVector.SPECIES_PREFERRED; |
32 | int upperBound = species.loopBound(w); |
33 | endifdef |
34 | |
35 | int i = w; |
36 | for (int y = 1; y < h; y++) { |
37 | sum = 0; |
38 | ifdef BWIntegralImage_useVectorAPI |
39 | int x = 0; |
40 | for (; x < upperBound; x += species.length(), i += species.length()) { |
41 | for (int sub = 0; sub < species.length(); sub++) { |
42 | sum += packedToBrightness(data[i+sub]); |
43 | data[i+sub] = sum; |
44 | } |
45 | |
46 | IntVector v = IntVector.fromArray(species, data, i); |
47 | IntVector v_last = IntVector.fromArray(species, data, i-w); |
48 | v = v.add(v_last); |
49 | v.intoArray(data, i); |
50 | } |
51 | |
52 | for (; x < w; x++, i++) { |
53 | int packed = data[i]; |
54 | int brightness = packedToBrightness(packed); |
55 | sum += brightness; |
56 | data[i] = sum + data[i-w]; |
57 | } |
58 | endifdef |
59 | |
60 | ifndef BWIntegralImage_useVectorAPI |
61 | for x to w: { |
62 | int packed = data[i]; |
63 | int brightness = packedToBrightness(packed); |
64 | sum += brightness; |
65 | data[i] = sum + data[i-w]; |
66 | i++; |
67 | } |
68 | endifndef |
69 | } // for y |
70 | } |
71 | |
72 | *(BWImage img) { |
73 | alloc(img.w(), img.h()); |
74 | data = new int[w*h]; |
75 | int i = 0; |
76 | for y to h: { |
77 | int sum = 0; |
78 | for x to w: { |
79 | sum += img.getByte(x, y) & 0xFF; |
80 | data[i] = y > 0 ? sum + data[i-w] : sum; |
81 | i++; |
82 | } |
83 | } |
84 | } |
85 | |
86 | private void alloc(int w, int h) { |
87 | this.w = w; |
88 | this.h = h; |
89 | if (w*h > 8*1024*1024) |
90 | fail("Image too large (more than 8 MP): " + w + "*" + h); |
91 | data = new int[w*h]; |
92 | } |
93 | |
94 | // pixels outside of image are considered black |
95 | int get(int x, int y) { |
96 | ifdef BWIntegralImage_CountAccesses |
97 | ++accesses; |
98 | endifdef |
99 | ret x < 0 || y < 0 || x >= w || y >= h ? 0 |
100 | : data[min(y, h-1)*w+min(x, w-1)]; |
101 | } |
102 | |
103 | public int getIIValue(int x, int y) { ret get(x, y); } |
104 | |
105 | public double getPixelAverage(int x1, int y1, int x2, int y2) { |
106 | int area = (x2-x1)*(y2-y1); |
107 | ret doubleRatio(bwIntegralImage_sumRect(this, x1, y1, x2, y2), area); |
108 | } |
109 | |
110 | int getPixel(int x, int y) { |
111 | ret bwIntegralImage_sumRect(this, x, y, x+1, y+1); |
112 | } |
113 | |
114 | int getPixel(Pt p) { ret getPixel(p.x, p.y); } |
115 | |
116 | public int getWidth() { ret w; } |
117 | public int getHeight() { ret h; } |
118 | |
119 | // unoptimized |
120 | public BufferedImage getBufferedImage() { |
121 | ret scaleDownUsingIntegralImageBW(this, w).getBufferedImage(); |
122 | } |
123 | |
124 | int packedToBrightness(int packed) { |
125 | int b = (packed & 0xFF); |
126 | ifdef BWIntegralImage_brightnessCheat |
127 | ret b; |
128 | endifdef |
129 | ifndef BWIntegralImage_brightnessCheat |
130 | int r = ((packed >> 16) & 0xFF); |
131 | int g = ((packed >> 8) & 0xFF); |
132 | ret (r+g+b+1)/3; |
133 | endifndef |
134 | } |
135 | } |
Began life as a copy of #1019595
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1030545 |
Snippet name: | BWIntegralImage [backup before GrabbablePixels] |
Eternal ID of this version: | #1030545/1 |
Text MD5: | 9b80cf932d95a371c73e811552e5d1f8 |
Transpilation MD5: | a5d35c40c00259a827fd1c84d57e5a95 |
Author: | stefan |
Category: | javax / imaging |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-01-07 01:05:37 |
Source code size: | 3809 bytes / 135 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 209 / 283 |
Referenced in: | [show references] |