-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathplanes.pde
executable file
·191 lines (167 loc) · 4.3 KB
/
planes.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
final static float LOG2 = log(2.0);
final static int CLAMP_NONE = 0;
final static int CLAMP_MOD256 = 1;
int clamp_in(int method, int x) {
switch(method) {
case CLAMP_MOD256:
return x<0?x+256:x>255?x-256:x;
default:
return x;
}
}
int clamp_out(int method, int x) {
switch(method) {
case CLAMP_MOD256:
return x<0?x+256:x>255?x-256:x;
default:
return constrain(x, 0, 255);
}
}
int clamp(int method, int x) {
switch(method) {
case CLAMP_MOD256:
return constrain(x, 0, 255);
default:
return constrain(x, -255, 255);
}
}
class RefColor {
int[] c;
public RefColor() {
c = new int[] {
128, 128, 128, 255
};
}
public RefColor(int r, int g, int b) {
this(color(r, g, b));
}
public RefColor(int r, int g, int b, int cs) {
this(color(r, g, b), cs);
}
public RefColor(color cc) {
c = new int[4];
c[2] = cc & 0xff;
c[1] = (cc >> 8) & 0xff;
c[0] = (cc >> 16) & 0xff;
c[3] = (cc >> 24) & 0xff;
}
public RefColor(color cc, int cs) {
this(toColorspace(cc, cs));
}
}
class Planes {
int ww, hh;
int w, h, cs;
int[][][] channels;
RefColor ref;
public Planes(int[] pxls, int w, int h, int cs, RefColor ref) {
this(w, h, cs, ref);
extractPlanes(pxls);
}
public Planes(int w, int h, int cs) {
this(w, h, cs, new RefColor(ccfg.color_outside, cs));
}
public Planes(int w, int h, int cs, RefColor ref) {
this.w = w;
this.h = h;
this.cs = cs;
ww = 1<<(int)ceil(log(w)/LOG2);
hh = 1<<(int)ceil(log(h)/LOG2);
//channels = new int[4][w][h];
channels = new int[3][w][h];
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
channels[0][x][y] = ref.c[0];
channels[1][x][y] = ref.c[1];
channels[2][x][y] = ref.c[2];
// channels[3][x][y] = ref.c[3];
}
}
this.ref = ref;
}
public Planes(int[] pxls, int w, int h, int cs) {
this(pxls, w, h, cs, new RefColor(ccfg.color_outside, cs));
}
public Planes clone() {
return new Planes(w,h,cs,ref);
}
private void extractPlanes(int[] pxls) {
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
color c = toColorspace(pxls[y*w+x], cs);
channels[2][x][y] = c & 0xff;
channels[1][x][y] = (c >> 8) & 0xff;
channels[0][x][y] = (c >> 16) & 0xff;
// channels[3][x][y] = (c >> 24) & 0xff;
}
}
}
public int[] toPixels() {
int[] pxls = new int[w*h];
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
int off = y*w+x;
pxls[off] = fromColorspace(
(channels[2][x][y] ) |
((channels[1][x][y] ) << 8) |
((channels[0][x][y] ) << 16) |
(img.pixels[off]&0xff000000)
// ((channels[3][x][y] ) << 24)
, cs);
}
}
return pxls;
}
public PImage toImage() {
PImage i = createImage(w, h, ARGB);
i.loadPixels();
i.pixels = toPixels();
i.updatePixels();
return i;
}
public int get(int pno, int x, int y) {
if (x<0 || x>=w || y<0 || y>=h) {
return ref.c[pno];
} else {
return channels[pno][x][y];
}
}
void set(int pno, int x, int y, int val) {
if (x>=0 && x<w && y>=0 && y<h) {
channels[pno][x][y] = val;
}
}
double[][] get(int pno, Segment s) {
double[][] res = new double[s.size][s.size];
for (int x=0; x<s.size; x++) {
for (int y=0; y<s.size; y++) {
res[x][y] = get(pno, x+s.x, y+s.y)/255.0;
}
}
return res;
}
void set(int pno, Segment s, double[][] values, int method) {
for (int x=0; x<s.size; x++) {
for (int y=0; y<s.size; y++) {
// set(pno, x+s.x, y+s.y, clamp(method,round((float)values[x][y])));
set(pno, x+s.x, y+s.y, clamp(method,round((float)(values[x][y]*255.0))));
}
}
}
void subtract(int pno, Segment s, int[][] values, int clamp_method) {
for (int x=0; x<s.size; x++) {
for (int y=0; y<s.size; y++) {
int v = get(pno, x+s.x, y+s.y) - values[x][y];
set(pno, x+s.x, y+s.y, clamp_in(clamp_method, v));
}
}
}
void add(int pno, Segment s, int[][] values, int clamp_method) {
for (int x=0; x<s.size; x++) {
for (int y=0; y<s.size; y++) {
int v = get(pno, x+s.x, y+s.y) + values[x][y];
set(pno, x+s.x, y+s.y, clamp_out(clamp_method, v));
}
}
}
}