111350bb1bbeeaeeed59d22de45e20e9a3ab04eb
[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         {
207                 for(x= 0; x < width; x++)
208                 {
209                         if( ( (y/size)%2 == 1 && (x/size)%2 == 1 ) || ( (y/size)%2 == 0 && (x/size)%2 == 0 ) )
210                         {
211                                 if (rect) {
212                                         rect[0]= (char)BLEND_CHAR(rect[0], blend);
213                                         rect[1]= (char)BLEND_CHAR(rect[1], blend);
214                                         rect[2]= (char)BLEND_CHAR(rect[2], blend);
215                                         rect[3]= 255;
216                                 
217                                         rect += 4;
218                                 }
219                                 if (rect_float) {
220                                         rect_float[0]= BLEND_FLOAT(rect_float[0], blend);
221                                         rect_float[1]= BLEND_FLOAT(rect_float[1], blend);
222                                         rect_float[2]= BLEND_FLOAT(rect_float[2], blend);
223                                         rect_float[3]= 1.0f;
224                                 
225                                         rect_float += 4;
226                                 }
227                         }
228                         else {
229                                 if (rect) {
230                                         rect[0]= (char)BLEND_CHAR(rect[0], blend_half);
231                                         rect[1]= (char)BLEND_CHAR(rect[1], blend_half);
232                                         rect[2]= (char)BLEND_CHAR(rect[2], blend_half);
233                                         rect[3]= 255;
234                                 
235                                         rect += 4;
236                                 }
237                                 if (rect_float) {
238                                         rect_float[0]= BLEND_FLOAT(rect_float[0], blend_half);
239                                         rect_float[1]= BLEND_FLOAT(rect_float[1], blend_half);
240                                         rect_float[2]= BLEND_FLOAT(rect_float[2], blend_half);
241                                         rect_float[3]= 1.0f;
242                                 
243                                         rect_float += 4;
244                                 }
245                         }
246                         
247                 }
248         }       
249 }
250
251 static void checker_board_grid_fill(unsigned char *rect, float *rect_float, int width, int height, float blend)
252 {
253         int x, y;
254         for(y= 0; y < height; y++)
255         {
256                 for(x= 0; x < width; x++)
257                 {
258                         if( ((y % 32) == 0) || ((x % 32) == 0)  || x == 0 )
259                         {
260                                 if (rect) {
261                                         rect[0]= BLEND_CHAR(rect[0], blend);
262                                         rect[1]= BLEND_CHAR(rect[1], blend);
263                                         rect[2]= BLEND_CHAR(rect[2], blend);
264                                         rect[3]= 255;
265
266                                         rect += 4;
267                                 }
268                                 if (rect_float) {
269                                         rect_float[0]= BLEND_FLOAT(rect_float[0], blend);
270                                         rect_float[1]= BLEND_FLOAT(rect_float[1], blend);
271                                         rect_float[2]= BLEND_FLOAT(rect_float[2], blend);
272                                         rect_float[3]= 1.0f;
273                                 
274                                         rect_float += 4;
275                                 }
276                         }
277                         else {
278                                 if(rect_float) rect_float += 4;
279                                 if(rect) rect += 4;
280                         }
281                 }
282         }
283 }
284
285 /* defined in image.c */
286
287 static void checker_board_text(unsigned char *rect, float *rect_float, int width, int height, int step, int outline)
288 {
289         int x, y;
290         int pen_x, pen_y;
291         char text[3]= {'A', '1', '\0'};
292         const int mono= blf_mono_font;
293
294         BLF_size(mono, 54, 72); /* hard coded size! */
295
296         BLF_buffer(mono, rect_float, rect, width, height, 4);
297
298         for(y= 0; y < height; y+=step)
299         {
300                 text[1]= '1';
301
302                 for(x= 0; x < width; x+=step)
303                 {
304                         /* hard coded offset */
305                         pen_x = x + 33;
306                         pen_y = y + 44;
307
308                         /* terribly crappy outline font! */
309                         BLF_buffer_col(mono, 1.0, 1.0, 1.0, 1.0);
310
311                         BLF_position(mono, pen_x-outline, pen_y, 0.0);
312                         BLF_draw_buffer(mono, text);
313                         BLF_position(mono, pen_x+outline, pen_y, 0.0);
314                         BLF_draw_buffer(mono, text);
315                         BLF_position(mono, pen_x, pen_y-outline, 0.0);
316                         BLF_draw_buffer(mono, text);
317                         BLF_position(mono, pen_x, pen_y+outline, 0.0);
318                         BLF_draw_buffer(mono, text);
319
320                         BLF_position(mono, pen_x-outline, pen_y-outline, 0.0);
321                         BLF_draw_buffer(mono, text);
322                         BLF_position(mono, pen_x+outline, pen_y+outline, 0.0);
323                         BLF_draw_buffer(mono, text);
324                         BLF_position(mono, pen_x-outline, pen_y+outline, 0.0);
325                         BLF_draw_buffer(mono, text);
326                         BLF_position(mono, pen_x+outline, pen_y-outline, 0.0);
327                         BLF_draw_buffer(mono, text);
328
329                         BLF_buffer_col(mono, 0.0, 0.0, 0.0, 1.0);
330                         BLF_position(mono, pen_x, pen_y, 0.0);
331                         BLF_draw_buffer(mono, text);
332
333                         text[1]++;
334                 }
335                 text[0]++;
336         }
337
338         /* cleanup the buffer. */
339         BLF_buffer(mono, NULL, NULL, 0, 0, 0);
340 }
341
342 void BKE_image_buf_fill_checker_color(unsigned char *rect, float *rect_float, int width, int height)
343 {
344         checker_board_color_fill(rect, rect_float, width, height);
345         checker_board_color_tint(rect, rect_float, width, height, 1, 0.03f);
346         checker_board_color_tint(rect, rect_float, width, height, 4, 0.05f);
347         checker_board_color_tint(rect, rect_float, width, height, 32, 0.07f);
348         checker_board_color_tint(rect, rect_float, width, height, 128, 0.15f);
349         checker_board_grid_fill(rect, rect_float, width, height, 1.0f/4.0f);
350
351         checker_board_text(rect, rect_float, width, height, 128, 2);
352 }