1 | persistable sclass HexagonalIntegralImage {
|
2 | final int w, h; |
3 | |
4 | // 3 ints per pixel. data[0] is the sharp edge, data[1] the blunt edge |
5 | final int[2][] data; |
6 | |
7 | // the column sums in three directions |
8 | // north, north west, north east |
9 | final int[][] columnSums = new int[3][]; |
10 | |
11 | *(BufferedImage img) { this(RGBImage img); }
|
12 | |
13 | *(RGBImage img) {
|
14 | w = img.w(); |
15 | h = img.h(); |
16 | data1 = new int[w*h*3]; |
17 | int i = 0; |
18 | for y to h: {
|
19 | int sumR = 0, sumG = 0, sumB = 0; |
20 | int shift = iround(y*sqrtAThird()); |
21 | for x to w: {
|
22 | // grab pixels |
23 | int rgb = img.getInt(x, y); |
24 | sumR += (rgb >> 16) & 0xFF; |
25 | sumG += (rgb >> 8) & 0xFF; |
26 | sumB += rgb & 0xFF; |
27 | |
28 | // calc wedges |
29 | data[0][i] = sumR; |
30 | data[0][i+1] = sumG; |
31 | data[0][i+2] = sumB; |
32 | |
33 | if (y != 0) {
|
34 | if (x >= shift) {
|
35 | data[0][i] += data[0][i-(w+shift)*3]; |
36 | data[0][i+1] += data[0][i-(w+shift)*3+1]; |
37 | data[0][i+2] += data[0][i-(w+shift)*3+2]; |
38 | } |
39 | |
40 | if (x+shift < w) {
|
41 | data[1][i] += data[1][i-(w-shift)*3]; |
42 | data[1][i+1] += data[1][i-(w-shift)*3+1]; |
43 | data[1][i+2] += data[1][i-(w-shift)*3+2]; |
44 | } |
45 | } |
46 | |
47 | i += 3; |
48 | } |
49 | } |
50 | |
51 | columnSums[0] = new int[h]; |
52 | for y to h: |
53 | for x to w: {
|
54 | c |
55 | |
56 | for i over columnSums: |
57 | columnSums[i] = new int[max(w, h)*2]; // TODO: find actual upper bound |
58 | |
59 | int i = 0, j = 0, sumR = 0, sumG = 0, sumB = 0; |
60 | for x to w: {
|
61 | int rgb = pixels[j++]; |
62 | data[i++] = (sumR += (rgb >> 16) & 0xFF); |
63 | data[i++] = (sumG += (rgb >> 8) & 0xFF); |
64 | data[i++] = (sumB += rgb & 0xFF); |
65 | } |
66 | for (int y = 1; y < h; y++) {
|
67 | sumR = sumG = sumB = 0; |
68 | for x to w: {
|
69 | int rgb = pixels[j++]; |
70 | sumR += (rgb >> 16) & 0xFF; |
71 | sumG += (rgb >> 8) & 0xFF; |
72 | sumB += rgb & 0xFF; |
73 | data[i] = sumR + data[i-w*3]; |
74 | data[i+1] = sumG + data[i-w*3+1]; |
75 | data[i+2] = sumB + data[i-w*3+2]; |
76 | i += 3; |
77 | } |
78 | } |
79 | } |
80 | |
81 | // gets the integral value at x/y for given RGB channel |
82 | int get(int x, int y, int channel) {
|
83 | ret x < 0 || y < 0 || x >= w || y >= h ? 0 |
84 | : data[(y*w+x)*3+channel]; |
85 | } |
86 | |
87 | // gets sum of the 3 channels |
88 | int get(int x, int y) {
|
89 | if (x < 0 || y < 0 || x >= w || y >= h) ret 0; |
90 | int i = (y*w+x)*3; |
91 | ret data[i]+data[i+1]+data[i+2]; |
92 | } |
93 | |
94 | double averageBrightness() {
|
95 | ret doubleRatio(get(w-1, h-1), w*h*3*255.0); |
96 | } |
97 | |
98 | toString {
|
99 | ret "HexagonalIntegralImage " + w + "*" + h + ", brightness: " + averageBrightness(); |
100 | } |
101 | |
102 | public int getWidth() { ret w; }
|
103 | public int getHeight() { ret h; }
|
104 | int w() { ret w; }
|
105 | int h() { ret h; }
|
106 | } |
Began life as a copy of #1019588
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1031360 |
| Snippet name: | HexagonalIntegralImage [RGB, dev.] |
| Eternal ID of this version: | #1031360/4 |
| Text MD5: | 33b22b7507365d784f0828e6073308fb |
| Author: | stefan |
| Category: | javax / gui |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2021-06-07 07:52:36 |
| Source code size: | 2902 bytes / 106 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 366 / 409 |
| Version history: | 3 change(s) |
| Referenced in: | [show references] |