-
Notifications
You must be signed in to change notification settings - Fork 2
/
Image_Processing.pde
170 lines (150 loc) · 7.39 KB
/
Image_Processing.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
void copy_input_image_to_resized() {
resized_input_image = createImage(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT, RGB);
resized_input_image.copy(input_image, 0, 0, input_image.width, input_image.height, 0, 0, resized_input_image.width, resized_input_image.height);
}
void copy_resized_image_to_brightness_layers() {
for (int i=0; i<brightness_layer_images.length; i++) {
brightness_layer_images[i] = createImage(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT, RGB);
brightness_layer_images[i].copy(resized_input_image, 0, 0, resized_input_image.width, resized_input_image.height, 0, 0, brightness_layer_images[i].width, brightness_layer_images[i].height);
}
}
void init_BRD_layers() {
for (int i=0; i<BRD_layer_images.length; i++) {
BRD_layer_images[i] = createImage(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT, RGB);
}
}
void init_output_preview() {
output_preview_image = createImage(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT, RGB);
}
PImage dither(PImage _input_for_dither, float _input_value) {
_input_for_dither.copy(resized_input_image, 0, 0, resized_input_image.width, resized_input_image.height, 0, 0, _input_for_dither.width, _input_for_dither.height);
_input_for_dither.loadPixels();
for (int y=0; y<_input_for_dither.height-1; y++) {
for (int x=0; x<_input_for_dither.width-1; x++) {
int oldpixel = constrain((_input_for_dither.pixels[x+y*_input_for_dither.width] >> 16 & 0xFF)+int(255.0*(1-_input_value)-127.0),0,255);
int newpixel = 255*round((oldpixel)/(255.0));
_input_for_dither.pixels[x+y*_input_for_dither.width] = color(newpixel);
int quant_error = oldpixel - newpixel;
float color_1 = (((_input_for_dither.pixels[(x+1)+(y+0)*_input_for_dither.width]>> 16 & 0xFF) + quant_error * 7/ 16));
float color_2 = (((_input_for_dither.pixels[(x-1)+(y+1)*_input_for_dither.width]>> 16 & 0xFF) + quant_error * 3/ 16));
float color_3 = (((_input_for_dither.pixels[(x+0)+(y+1)*_input_for_dither.width]>> 16 & 0xFF) + quant_error * 5/ 16));
float color_4 = (((_input_for_dither.pixels[(x+1)+(y+1)*_input_for_dither.width]>> 16 & 0xFF) + quant_error * 1 / 16));
_input_for_dither.pixels[(x+1)+(y+0)*_input_for_dither.width] = color(color_1);
_input_for_dither.pixels[(x-1)+(y+1)*_input_for_dither.width] = color(color_2);
_input_for_dither.pixels[(x+0)+(y+1)*_input_for_dither.width] = color(color_3);
_input_for_dither.pixels[(x+1)+(y+1)*_input_for_dither.width] = color(color_4);
}
}
_input_for_dither.updatePixels();
return(_input_for_dither);
}
PImage run_threshold(PImage _source_image, PImage _input_image, float _input_value) {
float temp_brightness=0;
_input_image.loadPixels();
for (int i = 0; i < _source_image.width*_source_image.height; i++) {
temp_brightness = (_source_image.pixels[i] >> 16 & 0xFF);
if (temp_brightness>_input_value) {
_input_image.pixels[i] = color(255);
} else {
_input_image.pixels[i] = color(0);
}
}
_input_image.updatePixels();
return(_input_image);
}
void compose_BRD_layers() {
BRD_layer_images[1].loadPixels(); //composing tStop
for (int i = 0; i < BRD_layer_images[1].width*BRD_layer_images[1].height; i++) {
BRD_layer_images[1].pixels[i] =
(~((~brightness_layer_images[1].pixels[i])
|(brightness_layer_images[2].pixels[i])
|(brightness_layer_images[3].pixels[i])
|(brightness_layer_images[4].pixels[i])
|(brightness_layer_images[5].pixels[i])
|(brightness_layer_images[6].pixels[i])))
&((~brightness_layer_images[0].pixels[i])
|(~brightness_layer_images[7].pixels[i]));
}
BRD_layer_images[1].updatePixels();
BRD_layer_images[0].loadPixels(); //composing bStop
for (int i = 0; i < BRD_layer_images[0].width*BRD_layer_images[0].height; i++) {
BRD_layer_images[0].pixels[i] =
(~((brightness_layer_images[3].pixels[i])
|(brightness_layer_images[4].pixels[i])
|(brightness_layer_images[5].pixels[i])))
&((~brightness_layer_images[0].pixels[i])
|(~brightness_layer_images[1].pixels[i]));
}
BRD_layer_images[0].updatePixels();
BRD_layer_images[2].loadPixels(); //composing TOP
for (int i = 0; i < BRD_layer_images[2].width*BRD_layer_images[2].height; i++) {
BRD_layer_images[2].pixels[i] =
~(~((~brightness_layer_images[0].pixels[i])
|(~brightness_layer_images[1].pixels[i])
|(brightness_layer_images[3].pixels[i])
|(brightness_layer_images[4].pixels[i])
|(brightness_layer_images[5].pixels[i])
|(brightness_layer_images[6].pixels[i])))
&((brightness_layer_images[2].pixels[i])
|(~brightness_layer_images[7].pixels[i]));
}
BRD_layer_images[2].updatePixels();
BRD_layer_images[3].loadPixels(); //composing BOTTOM
for (int i = 0; i < BRD_layer_images[3].width*BRD_layer_images[3].height; i++) {
BRD_layer_images[3].pixels[i] =
~(~((~brightness_layer_images[0].pixels[i])
|(~brightness_layer_images[1].pixels[i])
|(brightness_layer_images[3].pixels[i])
|(brightness_layer_images[4].pixels[i])))
&(brightness_layer_images[5].pixels[i]);
}
BRD_layer_images[3].updatePixels();
BRD_layer_images[4].loadPixels(); //composing tPlace
for (int i = 0; i < BRD_layer_images[4].width*BRD_layer_images[4].height; i++) {
BRD_layer_images[4].pixels[i] =
~(~((~brightness_layer_images[0].pixels[i])
|(~brightness_layer_images[1].pixels[i])
|(brightness_layer_images[2].pixels[i])
|(brightness_layer_images[3].pixels[i])
|(brightness_layer_images[4].pixels[i])
|(brightness_layer_images[5].pixels[i])
|(~brightness_layer_images[7].pixels[i])))
&(brightness_layer_images[6].pixels[i]);
}
BRD_layer_images[4].updatePixels();
BRD_layer_images[5].loadPixels(); //composing bPlace
for (int i = 0; i < BRD_layer_images[5].width*BRD_layer_images[5].height; i++) {
BRD_layer_images[5].pixels[i] =
~(~((~brightness_layer_images[0].pixels[i])
|(~brightness_layer_images[1].pixels[i])
|(brightness_layer_images[3].pixels[i])
|(brightness_layer_images[5].pixels[i])))
&(brightness_layer_images[4].pixels[i]);
}
BRD_layer_images[5].updatePixels();
}
void compose_preview() {
output_preview_image.loadPixels(); //composing preview
float temp_red = 0;
float temp_green = 0;
float temp_blue = 0;
for (int i = 0; i < output_preview_image.width*output_preview_image.height; i++) {
temp_red+=(255-(BRD_layer_images[0].pixels[i] & 0xFF))*0.2;
temp_red+=(255-(BRD_layer_images[1].pixels[i] & 0xFF))*0.2;
temp_red+=(BRD_layer_images[2].pixels[i] & 0xFF)*0.2;
temp_red+=(BRD_layer_images[3].pixels[i] & 0xFF)*0.2;
temp_red/=4;
temp_green+=(255-(BRD_layer_images[0].pixels[i] & 0xFF))*0.5;
temp_green+=(255-(BRD_layer_images[1].pixels[i] & 0xFF))*0.5;
temp_green+=(BRD_layer_images[2].pixels[i] & 0xFF)*0.5;
temp_green+=(BRD_layer_images[3].pixels[i] & 0xFF)*0.5;
temp_green/=4;
temp_blue+=(255-(BRD_layer_images[0].pixels[i] & 0xFF))*0.1;
temp_blue+=(255-(BRD_layer_images[1].pixels[i] & 0xFF))*0.1;
temp_blue+=(BRD_layer_images[2].pixels[i] & 0xFF)*0.1;
temp_blue+=(BRD_layer_images[3].pixels[i] & 0xFF)*0.1;
temp_blue/=4;
output_preview_image.pixels[i] = color(temp_red, temp_green, temp_blue);
}
output_preview_image.updatePixels();
}