-
Notifications
You must be signed in to change notification settings - Fork 3
/
paint_canvas_layer.c
332 lines (277 loc) · 7.18 KB
/
paint_canvas_layer.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "header.h"
void paint_canvas_layer(
int *reference_rgb_image_arr,
int width,
int height,
int brush_radius,
double opacity_strength,
double bumpity_strength,
texture_struct *texture_arr,
int texture_nbr,
double error_threshold,
double grid_size_factor,
int texture_ind,
int *canvas_rgb_image_arr,
int *canvas_bumpity_image_arr
)
{
brush_stroke_struct brush_stroke_data;
int brush_stroke_nbr;
double grid_dbl;
int grid;
int i;
int j;
int local_pixel_nbr;
int i2;
int j2;
int local_width;
int local_height;
int *local_rgb_image_arr;
int local_pixel;
int radius_i;
int radius_j;
int i3;
int j3;
int pixel3;
int cind;
int brush_stroke_color[3];
int debug_flag= 0;
int err_flag;
int local_origin_x;
int local_origin_y;
texture_struct texture_data;
double diff_dbl;
int reference_rgb[3];
int canvas_rgb[3];
int diff2;
double xc;
double yc;
double theta;
double rectw;
double rectl;
int *local_rgb_image_arr2;
brush_stroke_nbr= 0;
/*
Get the grid cell size (grid)
*/
grid= 2*brush_radius+1;
grid_dbl= grid_size_factor*(double)grid;
/*
grid= (int)round(grid_dbl);
*/
grid= (int)grid_dbl;
/*
For each grid cell,
determine where (xc,yc) and how (theta,rectw,rectl)
the brush stroke should be applied
*/
for ( i= 0 ; i< height ; i+= grid ) {
for ( j= 0 ; j< width ; j+= grid ) {
/*
Get the median color in the grid cell
looking at the reference image
*/
get_grid_cell_median_color(
reference_rgb_image_arr,
width,
height,
i,
j,
grid,
reference_rgb
);
/*
Get the median color in the grid cell
looking at the canvas image
*/
get_grid_cell_median_color(
canvas_rgb_image_arr,
width,
height,
i,
j,
grid,
canvas_rgb
);
diff2= 0;
for ( cind= 0 ; cind< 3 ; cind++ ) {
diff2+= (canvas_rgb[cind]-reference_rgb[cind])*
(canvas_rgb[cind]-reference_rgb[cind]);
}
diff_dbl= sqrt((double)diff2);
if ( diff_dbl < error_threshold ) {
/*
Do nothing!
*/
continue;
}
/*
If here,
a brush stroke needs to be applied for that grid cell
*/
/*
Let's consider the grid cell center
as the center of the local reference image
*/
i2= i+grid/2;
j2= j+grid/2;
/*
Define the local reference image centered at pixel (i2,j2)
*/
local_width= 2*brush_radius+1;
local_height= 2*brush_radius+1;
local_rgb_image_arr= (int *)calloc(local_width*local_height,3*sizeof(int));
local_pixel= 0;
local_pixel_nbr= 0;
for ( radius_i= -brush_radius ; radius_i<= +brush_radius ; radius_i++ ) {
i3= i2+radius_i;
for ( radius_j= -brush_radius ; radius_j<= +brush_radius ; radius_j++ ) {
j3= j2+radius_j;
pixel3= i3*width+j3;
if ( !(i3 >= 0) ) {
local_pixel++;
continue;
}
if ( !(i3 < height) ) {
local_pixel++;
continue;
}
if ( !(j3 >= 0) ) {
local_pixel++;
continue;
}
if ( !(j3 < width) ) {
local_pixel++;
continue;
}
for ( cind= 0 ; cind< 3 ; cind++ )
local_rgb_image_arr[3*local_pixel+cind]= reference_rgb_image_arr[3*pixel3+cind];
local_pixel++;
local_pixel_nbr++;
}
}
if ( !(local_pixel == local_width*local_height) ) {
error_handler((char *)"paint_canvas_layer");
}
/*
Do not consider a brush stroke for that grid cell
if the local reference image is not fully within the reference image
*/
if ( !(local_pixel_nbr == local_width*local_height) ) {
/*
Free local_rgb_image_arr
*/
free(local_rgb_image_arr);
continue;
}
if ( debug_flag == 1 ) {
/*
Print the local image
*/
err_flag= write_rgb_image(
(char *)"local_rgb_image.png",
local_rgb_image_arr,
local_width,
local_height
);
if ( err_flag == 1 ) {
error_handler((char *)"paint_canvas_layer");
}
}
if ( debug_flag == 1 ) {
/*
Print the local image replacing actual color with dominant color
*/
local_rgb_image_arr2= (int *)calloc(local_width*local_height,3*sizeof(int));
for ( i3= 0 ; i3< local_height ; i3++ ) {
for ( j3= 0 ; j3< local_width ; j3++ ) {
pixel3= i3*local_width+j3;
for ( cind= 0 ; cind< 3 ; cind++ )
local_rgb_image_arr2[3*pixel3+cind]= reference_rgb[cind];
}
}
err_flag= write_rgb_image(
(char *)"local_rgb_image2.png",
local_rgb_image_arr2,
local_width,
local_height
);
if ( err_flag == 1 ) {
error_handler((char *)"paint_canvas_layer");
}
free(local_rgb_image_arr2);
}
/*
Get the brush stroke color
*/
for ( cind= 0 ; cind< 3 ; cind++ )
brush_stroke_color[cind]= reference_rgb[cind];
/*
Define the origin of the local reference image, that is,
its top-left corner
*/
local_origin_x= j2-brush_radius;
local_origin_y= i2-brush_radius;
/*
Make the brush stroke
*/
make_brush_stroke(
local_rgb_image_arr,
local_width,
local_height,
local_origin_x,
local_origin_y,
brush_radius,
brush_stroke_color,
&xc,
&yc,
&theta,
&rectw,
&rectl,
&err_flag
);
/*
Free local_rgb_image_arr
*/
free(local_rgb_image_arr);
if ( err_flag == 1 ) {
/*
We were not able to compute the equivalent rectangle
*/
continue;
}
brush_stroke_data.dummy_int= 0;
brush_stroke_data.origin_x= local_origin_x;
brush_stroke_data.origin_y= local_origin_y;
brush_stroke_data.xc= xc;
brush_stroke_data.yc= yc;
brush_stroke_data.theta= theta;
brush_stroke_data.rectw= rectw;
brush_stroke_data.rectl= rectl;
for ( cind= 0 ; cind< 3 ; cind++ )
brush_stroke_data.color[cind]= brush_stroke_color[cind];
/*
Get brush texture corresponding to texture_ind
*/
texture_data= texture_arr[texture_ind];
/*
Paint the brush stroke on canvas
*/
paint_brush_stroke(
reference_rgb_image_arr,
width,
height,
brush_radius,
brush_stroke_data,
texture_data,
opacity_strength,
bumpity_strength,
canvas_rgb_image_arr,
canvas_bumpity_image_arr
);
brush_stroke_nbr++;
}
}
fprintf(stdout,"Number of brush strokes created = %d\n",
brush_stroke_nbr);
}