style cleanup, brackets in else/if, some indentation.
[blender.git] / source / blender / blenkernel / intern / image_gen.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Matt Ebb, Campbell Barton, Shuvro Sarker
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/blenkernel/intern/image_gen.c
24  *  \ingroup bke
25  */
26
27
28 #include <math.h>
29 #include <stdlib.h>
30
31 #include "BKE_image.h"
32 #include "BLI_math_color.h"
33 #include "BLI_math_base.h"
34 #include "BLF_api.h"
35
36 void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, const float color[4])
37 {
38         int x, y;
39
40         /* blank image */
41         if(rect_float) {
42                 for(y= 0; y<height; y++) {
43                         for(x= 0; x<width; x++) {
44                                 copy_v4_v4(rect_float, color);
45                                 rect_float+= 4;
46                         }
47                 }
48         }
49         
50         if(rect) {
51                 unsigned char ccol[4];
52
53                 rgba_float_to_uchar(ccol, color);
54
55                 for(y= 0; y<height; y++) {
56                         for(x= 0; x<width; x++) {
57                                 
58                                 rect[0]= ccol[0];
59                                 rect[1]= ccol[1];
60                                 rect[2]= ccol[2];
61                                 rect[3]= ccol[3];
62                                 rect+= 4;
63                         }
64                 }
65         }
66 }
67
68
69 void BKE_image_buf_fill_checker(unsigned char *rect, float *rect_float, int width, int height)
70 {
71         /* these two passes could be combined into one, but it's more readable and 
72          * easy to tweak like this, speed isn't really that much of an issue in this situation... */
73  
74         int checkerwidth= 32, dark= 1;
75         int x, y;
76
77         unsigned char *rect_orig= rect;
78         float *rect_float_orig= rect_float;
79
80         
81         float h=0.0, hoffs=0.0, hue=0.0, s=0.9, v=0.9, r, g, b;
82
83         /* checkers */
84         for(y= 0; y<height; y++) {
85                 dark= powf(-1.0f, floorf(y / checkerwidth));
86                 
87                 for(x= 0; x<width; x++) {
88                         if (x % checkerwidth == 0) dark= -dark;
89                         
90                         if (rect_float) {
91                                 if (dark > 0) {
92                                         rect_float[0]= rect_float[1]= rect_float[2]= 0.25f;
93                                         rect_float[3]= 1.0f;
94                                 } else {
95                                         rect_float[0]= rect_float[1]= rect_float[2]= 0.58f;
96                                         rect_float[3]= 1.0f;
97                                 }
98                                 rect_float+= 4;
99                         }
100                         else {
101                                 if (dark > 0) {
102                                         rect[0]= rect[1]= rect[2]= 64;
103                                         rect[3]= 255;
104                                 } else {
105                                         rect[0]= rect[1]= rect[2]= 150;
106                                         rect[3]= 255;
107                                 }
108                                 rect+= 4;
109                         }
110                 }
111         }
112
113         rect= rect_orig;
114         rect_float= rect_float_orig;
115
116         /* 2nd pass, colored + */
117         for(y= 0; y<height; y++) {
118                 hoffs= 0.125f * floorf(y / checkerwidth);
119                 
120                 for(x= 0; x<width; x++) {
121                         h= 0.125f * floorf(x / checkerwidth);
122                         
123                         if ((fabs((x % checkerwidth) - (checkerwidth / 2)) < 4) &&
124                                 (fabs((y % checkerwidth) - (checkerwidth / 2)) < 4)) {
125                                 
126                                 if ((fabs((x % checkerwidth) - (checkerwidth / 2)) < 1) ||
127                                         (fabs((y % checkerwidth) - (checkerwidth / 2)) < 1)) {
128                                         
129                                         hue= fmodf(fabs(h-hoffs), 1.0f);
130                                         hsv_to_rgb(hue, s, v, &r, &g, &b);
131                                         
132                                         if (rect) {
133                                                 rect[0]= (char)(r * 255.0f);
134                                                 rect[1]= (char)(g * 255.0f);
135                                                 rect[2]= (char)(b * 255.0f);
136                                                 rect[3]= 255;
137                                         }
138                                         
139                                         if (rect_float) {
140                                                 rect_float[0]= r;
141                                                 rect_float[1]= g;
142                                                 rect_float[2]= b;
143                                                 rect_float[3]= 1.0f;
144                                         }
145                                 }
146                         }
147
148                         if (rect_float) rect_float+= 4;
149                         if (rect) rect+= 4;
150                 }
151         }
152 }
153
154
155 /* Utility functions for BKE_image_buf_fill_checker_color */
156
157 #define BLEND_FLOAT(real, add)  (real+add <= 1.0f) ? (real+add) : 1.0f
158 #define BLEND_CHAR(real, add) ((real + (char)(add * 255.0f)) <= 255) ? (real + (char)(add * 255.0f)) : 255
159
160 static void checker_board_color_fill(unsigned char *rect, float *rect_float, int width, int height)
161 {
162         int hue_step, y, x;
163         float hue, val, sat, r, g, b;
164
165         sat= 1.0;
166
167         hue_step= power_of_2_max_i(width / 8);
168         if(hue_step < 8) hue_step= 8;
169
170         for(y= 0; y < height; y++)
171         {
172
173                 val= 0.1 + (y * (0.4 / height)); /* use a number lower then 1.0 else its too bright */
174                 for(x= 0; x < width; x++)
175                 {
176                         hue= (float)((double)(x/hue_step) * 1.0 / width * hue_step);
177                         hsv_to_rgb(hue, sat, val, &r, &g, &b);
178
179                         if (rect) {
180                                 rect[0]= (char)(r * 255.0f);
181                                 rect[1]= (char)(g * 255.0f);
182                                 rect[2]= (char)(b * 255.0f);
183                                 rect[3]= 255;
184                                 
185                                 rect += 4;
186                         }
187
188                         if (rect_float) {
189                                 rect_float[0]= r;
190                                 rect_float[1]= g;
191                                 rect_float[2]= b;
192                                 rect_float[3]= 1.0f;
193                                 
194                                 rect_float += 4;
195                         }
196                 }
197         }
198 }
199
200 static void checker_board_color_tint(unsigned char *rect, float *rect_float, int width, int height, int size, float blend)
201 {
202         int x, y;
203         float blend_half= blend * 0.5f;
204
205         for (y= 0; y < height; y++) {
206                 for (x= 0; x < width; x++) {
207                         if (((y / size) % 2 == 1 && (x / size) % 2 == 1 ) || ( (y / size) % 2 == 0 && (x / size) % 2 == 0 )) {
208                                 if (rect) {
209                                         rect[0]= (char)BLEND_CHAR(rect[0], blend);
210                                         rect[1]= (char)BLEND_CHAR(rect[1], blend);
211                                         rect[2]= (char)BLEND_CHAR(rect[2], blend);
212                                         rect[3]= 255;
213                                 
214                                         rect += 4;
215                                 }
216                                 if (rect_float) {
217                                         rect_float[0]= BLEND_FLOAT(rect_float[0], blend);
218                                         rect_float[1]= BLEND_FLOAT(rect_float[1], blend);
219                                         rect_float[2]= BLEND_FLOAT(rect_float[2], blend);
220                                         rect_float[3]= 1.0f;
221                                 
222                                         rect_float += 4;
223                                 }
224                         }
225                         else {
226                                 if (rect) {
227                                         rect[0]= (char)BLEND_CHAR(rect[0], blend_half);
228                                         rect[1]= (char)BLEND_CHAR(rect[1], blend_half);
229                                         rect[2]= (char)BLEND_CHAR(rect[2], blend_half);
230                                         rect[3]= 255;
231                                 
232                                         rect += 4;
233                                 }
234                                 if (rect_float) {
235                                         rect_float[0]= BLEND_FLOAT(rect_float[0], blend_half);
236                                         rect_float[1]= BLEND_FLOAT(rect_float[1], blend_half);
237                                         rect_float[2]= BLEND_FLOAT(rect_float[2], blend_half);
238                                         rect_float[3]= 1.0f;
239                                 
240                                         rect_float += 4;
241                                 }
242                         }
243                         
244                 }
245         }       
246 }
247
248 static void checker_board_grid_fill(unsigned char *rect, float *rect_float, int width, int height, float blend)
249 {
250         int x, y;
251         for(y= 0; y < height; y++) {
252                 for(x= 0; x < width; x++) {
253                         if (((y % 32) == 0) || ((x % 32) == 0)  || x == 0) {
254                                 if (rect) {
255                                         rect[0]= BLEND_CHAR(rect[0], blend);
256                                         rect[1]= BLEND_CHAR(rect[1], blend);
257                                         rect[2]= BLEND_CHAR(rect[2], blend);
258                                         rect[3]= 255;
259
260                                         rect += 4;
261                                 }
262                                 if (rect_float) {
263                                         rect_float[0]= BLEND_FLOAT(rect_float[0], blend);
264                                         rect_float[1]= BLEND_FLOAT(rect_float[1], blend);
265                                         rect_float[2]= BLEND_FLOAT(rect_float[2], blend);
266                                         rect_float[3]= 1.0f;
267                                 
268                                         rect_float += 4;
269                                 }
270                         }
271                         else {
272                                 if(rect_float) rect_float += 4;
273                                 if(rect) rect += 4;
274                         }
275                 }
276         }
277 }
278
279 /* defined in image.c */
280
281 static void checker_board_text(unsigned char *rect, float *rect_float, int width, int height, int step, int outline)
282 {
283         int x, y;
284         int pen_x, pen_y;
285         char text[3]= {'A', '1', '\0'};
286         const int mono= blf_mono_font;
287
288         BLF_size(mono, 54, 72); /* hard coded size! */
289
290         BLF_buffer(mono, rect_float, rect, width, height, 4);
291
292         for(y= 0; y < height; y+=step)
293         {
294                 text[1]= '1';
295
296                 for(x= 0; x < width; x+=step)
297                 {
298                         /* hard coded offset */
299                         pen_x = x + 33;
300                         pen_y = y + 44;
301
302                         /* terribly crappy outline font! */
303                         BLF_buffer_col(mono, 1.0, 1.0, 1.0, 1.0);
304
305                         BLF_position(mono, pen_x-outline, pen_y, 0.0);
306                         BLF_draw_buffer(mono, text);
307                         BLF_position(mono, pen_x+outline, pen_y, 0.0);
308                         BLF_draw_buffer(mono, text);
309                         BLF_position(mono, pen_x, pen_y-outline, 0.0);
310                         BLF_draw_buffer(mono, text);
311                         BLF_position(mono, pen_x, pen_y+outline, 0.0);
312                         BLF_draw_buffer(mono, text);
313
314                         BLF_position(mono, pen_x-outline, pen_y-outline, 0.0);
315                         BLF_draw_buffer(mono, text);
316                         BLF_position(mono, pen_x+outline, pen_y+outline, 0.0);
317                         BLF_draw_buffer(mono, text);
318                         BLF_position(mono, pen_x-outline, pen_y+outline, 0.0);
319                         BLF_draw_buffer(mono, text);
320                         BLF_position(mono, pen_x+outline, pen_y-outline, 0.0);
321                         BLF_draw_buffer(mono, text);
322
323                         BLF_buffer_col(mono, 0.0, 0.0, 0.0, 1.0);
324                         BLF_position(mono, pen_x, pen_y, 0.0);
325                         BLF_draw_buffer(mono, text);
326
327                         text[1]++;
328                 }
329                 text[0]++;
330         }
331
332         /* cleanup the buffer. */
333         BLF_buffer(mono, NULL, NULL, 0, 0, 0);
334 }
335
336 void BKE_image_buf_fill_checker_color(unsigned char *rect, float *rect_float, int width, int height)
337 {
338         checker_board_color_fill(rect, rect_float, width, height);
339         checker_board_color_tint(rect, rect_float, width, height, 1, 0.03f);
340         checker_board_color_tint(rect, rect_float, width, height, 4, 0.05f);
341         checker_board_color_tint(rect, rect_float, width, height, 32, 0.07f);
342         checker_board_color_tint(rect, rect_float, width, height, 128, 0.15f);
343         checker_board_grid_fill(rect, rect_float, width, height, 1.0f/4.0f);
344
345         checker_board_text(rect, rect_float, width, height, 128, 2);
346 }