-
Notifications
You must be signed in to change notification settings - Fork 3
/
paint_brush_stroke.c
433 lines (363 loc) · 8.42 KB
/
paint_brush_stroke.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "header.h"
void paint_brush_stroke(
int *reference_rgb_image_arr,
int width,
int height,
int brush_radius,
brush_stroke_struct brush_stroke_data,
texture_struct texture_data,
double opacity_strength,
double bumpity_strength,
int *canvas_rgb_image_arr,
int *canvas_bumpity_image_arr
)
{
int local_origin_x;
int local_origin_y;
double xc;
double yc;
double theta;
double rectw;
double rectl;
int cind;
int brush_stroke_color[3];
int map_width;
int map_height;
int *opacity_map;
int *bumpity_map;
int i;
int j;
int pixel;
int map_width2;
int map_height2;
int *opacity_map2;
int map_origin2_i;
int map_origin2_j;
int *opacity_map3;
int map_origin3_i;
int map_origin3_j;
int map_width3;
int map_height3;
int i3;
int j3;
int pixel3;
int brush_stroke_opacity;
int canvas_color[3];
double alpha_dbl;
double temp_dbl;
int temp_int;
int *bumpity_map2;
int *bumpity_map3;
int canvas_bumpity;
int debug_flag= 0;
int err_flag;
int brush_stroke_bumpity;
char scale_method[256];
char scale_gaussian[256];
char rotate_method[256];
double map_width2height_ratio;
local_origin_x= brush_stroke_data.origin_x;
local_origin_y= brush_stroke_data.origin_y;
xc= brush_stroke_data.xc;
yc= brush_stroke_data.yc;
theta= brush_stroke_data.theta;
rectw= brush_stroke_data.rectw;
rectl= brush_stroke_data.rectl;
for ( cind= 0 ; cind< 3 ; cind++ )
brush_stroke_color[cind]= brush_stroke_data.color[cind];
opacity_map= texture_data.opacity_image_arr;
bumpity_map= texture_data.bumpity_image_arr;
map_width= texture_data.width;
map_height= texture_data.height;
map_width2height_ratio= (double)map_width/(double)map_height;
if ( debug_flag == 1 ) {
/*
Print the opacity map
*/
err_flag= write_image(
(char *)"opacity_map.png",
opacity_map,
map_width,
map_height
);
if ( err_flag == 1 ) {
error_handler((char *)"paint_brush_stroke");
}
}
if ( debug_flag == 1 ) {
/*
Print the bumpity map
*/
err_flag= write_image(
(char *)"bumpity_map.png",
bumpity_map,
map_width,
map_height
);
if ( err_flag == 1 ) {
error_handler((char *)"paint_brush_stroke");
}
}
if ( !(rectl >= 0.0) ) {
error_handler((char *)"paint_brush_stroke");
}
if ( !(rectw >= 0.0) ) {
error_handler((char *)"paint_brush_stroke");
}
/*
Scale the opacity map and the bumpity map
*/
/*
map_width2= (int)round(rectl);
if ( map_width2 == 0 )
map_width2= 1;
*/
map_height2= (int)round(rectw);
if ( map_height2 == 0 )
map_height2= 1;
/*
map_height2= 2*brush_radius+1;
map_width2= (int)round( map_width2height_ratio*(double)map_height2 );
*/
map_width2= (int)round( map_width2height_ratio*(double)map_height2 );
opacity_map2= (int *)calloc(map_width2*map_height2,sizeof(int));
strcpy(scale_method,"cubic");
strcpy(scale_gaussian,"no");
scale_image(
opacity_map,
map_width,
map_height,
opacity_map2,
map_width2,
map_height2,
scale_method,
scale_gaussian
);
if ( debug_flag == 1 ) {
/*
Print the scaled opacity map
*/
err_flag= write_image(
(char *)"opacity_map2.png",
opacity_map2,
map_width2,
map_height2
);
if ( err_flag == 1 ) {
error_handler((char *)"paint_brush_stroke");
}
}
bumpity_map2= (int *)calloc(map_width2*map_height2,sizeof(int));
strcpy(scale_method,"cubic");
strcpy(scale_gaussian,"no");
scale_image(
bumpity_map,
map_width,
map_height,
bumpity_map2,
map_width2,
map_height2,
scale_method,
scale_gaussian
);
if ( debug_flag == 1 ) {
/*
Print the scaled bumpity map
*/
err_flag= write_image(
(char *)"bumpity_map2.png",
bumpity_map2,
map_width2,
map_height2
);
if ( err_flag == 1 ) {
error_handler((char *)"paint_brush_stroke");
}
}
map_origin2_i= (int)round(yc);
map_origin2_j= (int)round(xc);
/*
Rotate the opacity map and the bumpity map
*/
strcpy(rotate_method,"cubic");
rotate_image(
opacity_map2,
map_width2,
map_height2,
theta,
rotate_method,
&opacity_map3,
&map_origin3_i,
&map_origin3_j,
&map_width3,
&map_height3
);
if ( debug_flag == 1 ) {
/*
Print the scaled and rotated opacity map
*/
err_flag= write_image(
(char *)"opacity_map3.png",
opacity_map3,
map_width3,
map_height3
);
if ( err_flag == 1 ) {
error_handler((char *)"paint_brush_stroke");
}
}
strcpy(rotate_method,"cubic");
rotate_image(
bumpity_map2,
map_width2,
map_height2,
theta,
rotate_method,
&bumpity_map3,
&map_origin3_i,
&map_origin3_j,
&map_width3,
&map_height3
);
if ( debug_flag == 1 ) {
/*
Print the scaled and rotated bumpity map
*/
err_flag= write_image(
(char *)"bumpity_map3.png",
bumpity_map3,
map_width3,
map_height3
);
if ( err_flag == 1 ) {
error_handler((char *)"paint_brush_stroke");
}
}
/*
Paint the brush stroke on the canvas
*/
for ( i3= 0 ; i3< map_height3 ; i3++ ) {
for ( j3= 0 ; j3< map_width3 ; j3++ ) {
pixel3= i3*map_width3+j3;
brush_stroke_opacity= opacity_map3[pixel3];
brush_stroke_bumpity= bumpity_map3[pixel3];
/*
Adjust brush_stroke_opacity using bumpity_strength
Recall that the opacity is supposed to be between 0 and 255
*/
brush_stroke_opacity= (int)(opacity_strength*(double)brush_stroke_opacity);
if ( !(brush_stroke_opacity >= 0) )
brush_stroke_opacity= 0;
if ( !(brush_stroke_opacity <= 255) )
brush_stroke_opacity= 255;
/*
Adjust brush_stroke_bumpity using bumpity_strength
Recall that the bumpity is supposed to be between 0 and 255
*/
brush_stroke_bumpity= (int)(bumpity_strength*(double)(brush_stroke_bumpity));
if ( !(brush_stroke_bumpity >= 0) )
brush_stroke_bumpity= 0;
if ( !(brush_stroke_bumpity <= 255) )
brush_stroke_bumpity= 255;
/*
Need to get the corresponding pixel on the canvas
*/
i= i3+map_origin3_i+map_origin2_i+local_origin_y;
j= j3+map_origin3_j+map_origin2_j+local_origin_x;
pixel= i*width+j;
/*
Make sure the pixel falls onto the canvas
*/
if ( !(i >= 0) )
continue;
if ( !(i < height) )
continue;
if ( !(j >= 0) )
continue;
if ( !(j < width) )
continue;
/*
Get canvas color
*/
for ( cind= 0 ; cind< 3 ; cind++ )
canvas_color[cind]= canvas_rgb_image_arr[3*pixel+cind];
/*
Alpha blend the brush stroke color and the canvas color
*/
alpha_dbl= (double)brush_stroke_opacity/255.0;
for ( cind= 0 ; cind< 3 ; cind++ ) {
temp_dbl= (1.0-alpha_dbl)*(double)canvas_color[cind]+
alpha_dbl *(double)brush_stroke_color[cind];
temp_int= (int)round(temp_dbl);
if ( temp_int < 0 )
temp_int= 0;
if ( temp_int > 255 )
temp_int= 255;
canvas_rgb_image_arr[3*pixel+cind]= temp_int;
}
/*
Get canvas bumpity
*/
canvas_bumpity= canvas_bumpity_image_arr[pixel];
/*
Alpha blend the brush stroke bumpity and the canvas bumpity
*/
/*
alpha_dbl= (double)brush_stroke_opacity/255.0;
*/
alpha_dbl= (double)brush_stroke_bumpity/255.0;
temp_dbl= (1.0-alpha_dbl)*(double)canvas_bumpity+
alpha_dbl *(double)brush_stroke_bumpity;
temp_int= (int)round(temp_dbl);
if ( temp_int < 0 )
temp_int= 0;
if ( temp_int > 255 )
temp_int= 255;
canvas_bumpity_image_arr[pixel]= temp_int;
}
}
if ( debug_flag == 1 ) {
/*
Print the canvas image
*/
err_flag= write_rgb_image(
(char *)"canvas_rgb_image.png",
canvas_rgb_image_arr,
width,
height
);
if ( err_flag == 1 ) {
error_handler((char *)"paint_brush_stroke");
}
}
if ( debug_flag == 1 ) {
/*
Print the canvas bumpity image
*/
err_flag= write_image(
(char *)"canvas_bumpity_image.png",
canvas_bumpity_image_arr,
width,
height
);
if ( err_flag == 1 ) {
error_handler((char *)"paint_brush_stroke");
}
}
/*
Free opacity_map2
*/
free(opacity_map2);
/*
Free bumpity_map2
*/
free(bumpity_map2);
/*
Free opacity_map3
*/
free(opacity_map3);
/*
Free bumpity_map3
*/
free(bumpity_map3);
}