Patch #34204: [Render Animation] Fails with "Error: Specified sample_fmt is not suppo...
[blender.git] / source / blender / blenkernel / intern / brush.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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/brush.c
29  *  \ingroup bke
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_brush_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_object_types.h"
37
38 #include "BLI_math.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_rand.h"
41
42 #include "BKE_brush.h"
43 #include "BKE_colortools.h"
44 #include "BKE_global.h"
45 #include "BKE_image.h"
46 #include "BKE_library.h"
47 #include "BKE_main.h"
48 #include "BKE_paint.h"
49 #include "BKE_texture.h"
50 #include "BKE_icons.h"
51
52 #include "IMB_imbuf.h"
53 #include "IMB_imbuf_types.h"
54
55 #include "RE_render_ext.h" /* externtex */
56 #include "RE_shader_ext.h"
57
58 static void brush_defaults(Brush *brush)
59 {
60         brush->blend = 0;
61         brush->flag = 0;
62
63         brush->ob_mode = OB_MODE_ALL_PAINT;
64
65         /* BRUSH SCULPT TOOL SETTINGS */
66         brush->weight = 1.0f; /* weight of brush 0 - 1.0 */
67         brush->size = 35; /* radius of the brush in pixels */
68         brush->alpha = 0.5f; /* brush strength/intensity probably variable should be renamed? */
69         brush->autosmooth_factor = 0.0f;
70         brush->crease_pinch_factor = 0.5f;
71         brush->sculpt_plane = SCULPT_DISP_DIR_AREA;
72         brush->plane_offset = 0.0f; /* how far above or below the plane that is found by averaging the faces */
73         brush->plane_trim = 0.5f;
74         brush->clone.alpha = 0.5f;
75         brush->normal_weight = 0.0f;
76         brush->flag |= BRUSH_ALPHA_PRESSURE;
77
78         /* BRUSH PAINT TOOL SETTINGS */
79         brush->rgb[0] = 1.0f; /* default rgb color of the brush when painting - white */
80         brush->rgb[1] = 1.0f;
81         brush->rgb[2] = 1.0f;
82
83         /* BRUSH STROKE SETTINGS */
84         brush->flag |= (BRUSH_SPACE | BRUSH_SPACE_ATTEN);
85         brush->spacing = 10; /* how far each brush dot should be spaced as a percentage of brush diameter */
86
87         brush->smooth_stroke_radius = 75;
88         brush->smooth_stroke_factor = 0.9f;
89
90         brush->rate = 0.1f; /* time delay between dots of paint or sculpting when doing airbrush mode */
91
92         brush->jitter = 0.0f;
93
94         /* BRUSH TEXTURE SETTINGS */
95         default_mtex(&brush->mtex);
96
97         brush->texture_sample_bias = 0; /* value to added to texture samples */
98         brush->texture_overlay_alpha = 33;
99
100         /* brush appearance  */
101
102         brush->add_col[0] = 1.00; /* add mode color is light red */
103         brush->add_col[1] = 0.39;
104         brush->add_col[2] = 0.39;
105
106         brush->sub_col[0] = 0.39; /* subtract mode color is light blue */
107         brush->sub_col[1] = 0.39;
108         brush->sub_col[2] = 1.00;
109 }
110
111 /* Datablock add/copy/free/make_local */
112
113 Brush *BKE_brush_add(Main *bmain, const char *name)
114 {
115         Brush *brush;
116
117         brush = BKE_libblock_alloc(&bmain->brush, ID_BR, name);
118
119         /* enable fake user by default */
120         brush->id.flag |= LIB_FAKEUSER;
121
122         brush_defaults(brush);
123
124         brush->sculpt_tool = SCULPT_TOOL_DRAW; /* sculpting defaults to the draw tool for new brushes */
125
126         /* the default alpha falloff curve */
127         BKE_brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
128
129         return brush;
130 }
131
132 Brush *BKE_brush_copy(Brush *brush)
133 {
134         Brush *brushn;
135         
136         brushn = BKE_libblock_copy(&brush->id);
137
138         if (brush->mtex.tex)
139                 id_us_plus((ID *)brush->mtex.tex);
140
141         if (brush->icon_imbuf)
142                 brushn->icon_imbuf = IMB_dupImBuf(brush->icon_imbuf);
143
144         brushn->preview = NULL;
145
146         brushn->curve = curvemapping_copy(brush->curve);
147
148         /* enable fake user by default */
149         if (!(brushn->id.flag & LIB_FAKEUSER)) {
150                 brushn->id.flag |= LIB_FAKEUSER;
151                 brushn->id.us++;
152         }
153         
154         return brushn;
155 }
156
157 /* not brush itself */
158 void BKE_brush_free(Brush *brush)
159 {
160         if (brush->mtex.tex)
161                 brush->mtex.tex->id.us--;
162
163         if (brush->icon_imbuf)
164                 IMB_freeImBuf(brush->icon_imbuf);
165
166         BKE_previewimg_free(&(brush->preview));
167
168         curvemapping_free(brush->curve);
169 }
170
171 static void extern_local_brush(Brush *brush)
172 {
173         id_lib_extern((ID *)brush->mtex.tex);
174         id_lib_extern((ID *)brush->clone.image);
175 }
176
177 void BKE_brush_make_local(Brush *brush)
178 {
179
180         /* - only lib users: do nothing
181          * - only local users: set flag
182          * - mixed: make copy
183          */
184
185         Main *bmain = G.main;
186         Scene *scene;
187         int is_local = FALSE, is_lib = FALSE;
188
189         if (brush->id.lib == NULL) return;
190
191         if (brush->clone.image) {
192                 /* special case: ima always local immediately. Clone image should only
193                  * have one user anyway. */
194                 id_clear_lib_data(bmain, &brush->clone.image->id);
195                 extern_local_brush(brush);
196         }
197
198         for (scene = bmain->scene.first; scene && ELEM(0, is_lib, is_local); scene = scene->id.next) {
199                 if (paint_brush(&scene->toolsettings->imapaint.paint) == brush) {
200                         if (scene->id.lib) is_lib = TRUE;
201                         else is_local = TRUE;
202                 }
203         }
204
205         if (is_local && is_lib == FALSE) {
206                 id_clear_lib_data(bmain, &brush->id);
207                 extern_local_brush(brush);
208
209                 /* enable fake user by default */
210                 if (!(brush->id.flag & LIB_FAKEUSER)) {
211                         brush->id.flag |= LIB_FAKEUSER;
212                         brush->id.us++;
213                 }
214         }
215         else if (is_local && is_lib) {
216                 Brush *brush_new = BKE_brush_copy(brush);
217                 brush_new->id.us = 1; /* only keep fake user */
218                 brush_new->id.flag |= LIB_FAKEUSER;
219
220                 /* Remap paths of new ID using old library as base. */
221                 BKE_id_lib_local_paths(bmain, brush->id.lib, &brush_new->id);
222                 
223                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
224                         if (paint_brush(&scene->toolsettings->imapaint.paint) == brush) {
225                                 if (scene->id.lib == NULL) {
226                                         paint_brush_set(&scene->toolsettings->imapaint.paint, brush_new);
227                                 }
228                         }
229                 }
230         }
231 }
232
233 void BKE_brush_debug_print_state(Brush *br)
234 {
235         /* create a fake brush and set it to the defaults */
236         Brush def = {{NULL}};
237         brush_defaults(&def);
238         
239 #define BR_TEST(field, t)                                       \
240         if (br->field != def.field)                             \
241                 printf("br->" #field " = %" #t ";\n", br->field)
242
243 #define BR_TEST_FLAG(_f)                                                        \
244         if ((br->flag & _f) && !(def.flag & _f))                \
245                 printf("br->flag |= " #_f ";\n");                       \
246         else if (!(br->flag & _f) && (def.flag & _f))   \
247                 printf("br->flag &= ~" #_f ";\n")
248         
249
250         /* print out any non-default brush state */
251         BR_TEST(normal_weight, f);
252
253         BR_TEST(blend, d);
254         BR_TEST(size, d);
255
256         /* br->flag */
257         BR_TEST_FLAG(BRUSH_AIRBRUSH);
258         BR_TEST_FLAG(BRUSH_TORUS);
259         BR_TEST_FLAG(BRUSH_ALPHA_PRESSURE);
260         BR_TEST_FLAG(BRUSH_SIZE_PRESSURE);
261         BR_TEST_FLAG(BRUSH_JITTER_PRESSURE);
262         BR_TEST_FLAG(BRUSH_SPACING_PRESSURE);
263         BR_TEST_FLAG(BRUSH_RAKE);
264         BR_TEST_FLAG(BRUSH_ANCHORED);
265         BR_TEST_FLAG(BRUSH_DIR_IN);
266         BR_TEST_FLAG(BRUSH_SPACE);
267         BR_TEST_FLAG(BRUSH_SMOOTH_STROKE);
268         BR_TEST_FLAG(BRUSH_PERSISTENT);
269         BR_TEST_FLAG(BRUSH_ACCUMULATE);
270         BR_TEST_FLAG(BRUSH_LOCK_ALPHA);
271         BR_TEST_FLAG(BRUSH_ORIGINAL_NORMAL);
272         BR_TEST_FLAG(BRUSH_OFFSET_PRESSURE);
273         BR_TEST_FLAG(BRUSH_SPACE_ATTEN);
274         BR_TEST_FLAG(BRUSH_ADAPTIVE_SPACE);
275         BR_TEST_FLAG(BRUSH_LOCK_SIZE);
276         BR_TEST_FLAG(BRUSH_TEXTURE_OVERLAY);
277         BR_TEST_FLAG(BRUSH_EDGE_TO_EDGE);
278         BR_TEST_FLAG(BRUSH_RESTORE_MESH);
279         BR_TEST_FLAG(BRUSH_INVERSE_SMOOTH_PRESSURE);
280         BR_TEST_FLAG(BRUSH_RANDOM_ROTATION);
281         BR_TEST_FLAG(BRUSH_PLANE_TRIM);
282         BR_TEST_FLAG(BRUSH_FRONTFACE);
283         BR_TEST_FLAG(BRUSH_CUSTOM_ICON);
284
285         BR_TEST(jitter, f);
286         BR_TEST(spacing, d);
287         BR_TEST(smooth_stroke_radius, d);
288         BR_TEST(smooth_stroke_factor, f);
289         BR_TEST(rate, f);
290
291         BR_TEST(alpha, f);
292
293         BR_TEST(sculpt_plane, d);
294
295         BR_TEST(plane_offset, f);
296
297         BR_TEST(autosmooth_factor, f);
298
299         BR_TEST(crease_pinch_factor, f);
300
301         BR_TEST(plane_trim, f);
302
303         BR_TEST(texture_sample_bias, f);
304         BR_TEST(texture_overlay_alpha, d);
305
306         BR_TEST(add_col[0], f);
307         BR_TEST(add_col[1], f);
308         BR_TEST(add_col[2], f);
309         BR_TEST(sub_col[0], f);
310         BR_TEST(sub_col[1], f);
311         BR_TEST(sub_col[2], f);
312
313         printf("\n");
314         
315 #undef BR_TEST
316 #undef BR_TEST_FLAG
317 }
318
319 void BKE_brush_sculpt_reset(Brush *br)
320 {
321         /* enable this to see any non-default
322          * settings used by a brush: */
323         // BKE_brush_debug_print_state(br);
324
325         brush_defaults(br);
326         BKE_brush_curve_preset(br, CURVE_PRESET_SMOOTH);
327
328         switch (br->sculpt_tool) {
329                 case SCULPT_TOOL_CLAY:
330                         br->flag |= BRUSH_FRONTFACE;
331                         break;
332                 case SCULPT_TOOL_CREASE:
333                         br->flag |= BRUSH_DIR_IN;
334                         br->alpha = 0.25;
335                         break;
336                 case SCULPT_TOOL_FILL:
337                         br->add_col[1] = 1;
338                         br->sub_col[0] = 0.25;
339                         br->sub_col[1] = 1;
340                         break;
341                 case SCULPT_TOOL_FLATTEN:
342                         br->add_col[1] = 1;
343                         br->sub_col[0] = 0.25;
344                         br->sub_col[1] = 1;
345                         break;
346                 case SCULPT_TOOL_INFLATE:
347                         br->add_col[0] = 0.750000;
348                         br->add_col[1] = 0.750000;
349                         br->add_col[2] = 0.750000;
350                         br->sub_col[0] = 0.250000;
351                         br->sub_col[1] = 0.250000;
352                         br->sub_col[2] = 0.250000;
353                         break;
354                 case SCULPT_TOOL_NUDGE:
355                         br->add_col[0] = 0.250000;
356                         br->add_col[1] = 1.000000;
357                         br->add_col[2] = 0.250000;
358                         break;
359                 case SCULPT_TOOL_PINCH:
360                         br->add_col[0] = 0.750000;
361                         br->add_col[1] = 0.750000;
362                         br->add_col[2] = 0.750000;
363                         br->sub_col[0] = 0.250000;
364                         br->sub_col[1] = 0.250000;
365                         br->sub_col[2] = 0.250000;
366                         break;
367                 case SCULPT_TOOL_SCRAPE:
368                         br->add_col[1] = 1.000000;
369                         br->sub_col[0] = 0.250000;
370                         br->sub_col[1] = 1.000000;
371                         break;
372                 case SCULPT_TOOL_ROTATE:
373                         br->alpha = 1.0;
374                         break;
375                 case SCULPT_TOOL_SMOOTH:
376                         br->flag &= ~BRUSH_SPACE_ATTEN;
377                         br->spacing = 5;
378                         br->add_col[0] = 0.750000;
379                         br->add_col[1] = 0.750000;
380                         br->add_col[2] = 0.750000;
381                         break;
382                 case SCULPT_TOOL_GRAB:
383                 case SCULPT_TOOL_SNAKE_HOOK:
384                 case SCULPT_TOOL_THUMB:
385                         br->size = 75;
386                         br->flag &= ~BRUSH_ALPHA_PRESSURE;
387                         br->flag &= ~BRUSH_SPACE;
388                         br->flag &= ~BRUSH_SPACE_ATTEN;
389                         br->add_col[0] = 0.250000;
390                         br->add_col[1] = 1.000000;
391                         br->add_col[2] = 0.250000;
392                         break;
393                 default:
394                         break;
395         }
396 }
397
398 /* Library Operations */
399 void BKE_brush_curve_preset(Brush *b, /*CurveMappingPreset*/ int preset)
400 {
401         CurveMap *cm = NULL;
402
403         if (!b->curve)
404                 b->curve = curvemapping_add(1, 0, 0, 1, 1);
405
406         cm = b->curve->cm;
407         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
408
409         b->curve->preset = preset;
410         curvemap_reset(cm, &b->curve->clipr, b->curve->preset, CURVEMAP_SLOPE_NEGATIVE);
411         curvemapping_changed(b->curve, FALSE);
412 }
413
414 int BKE_brush_texture_set_nr(Brush *brush, int nr)
415 {
416         ID *idtest, *id = NULL;
417
418         id = (ID *)brush->mtex.tex;
419
420         idtest = (ID *)BLI_findlink(&G.main->tex, nr - 1);
421         if (idtest == NULL) { /* new tex */
422                 if (id) idtest = (ID *)BKE_texture_copy((Tex *)id);
423                 else idtest = (ID *)add_texture(G.main, "Tex");
424                 idtest->us--;
425         }
426         if (idtest != id) {
427                 BKE_brush_texture_delete(brush);
428
429                 brush->mtex.tex = (Tex *)idtest;
430                 id_us_plus(idtest);
431
432                 return 1;
433         }
434
435         return 0;
436 }
437
438 int BKE_brush_texture_delete(Brush *brush)
439 {
440         if (brush->mtex.tex)
441                 brush->mtex.tex->id.us--;
442
443         return 1;
444 }
445
446 int BKE_brush_clone_image_set_nr(Brush *brush, int nr)
447 {
448         if (brush && nr > 0) {
449                 Image *ima = (Image *)BLI_findlink(&G.main->image, nr - 1);
450
451                 if (ima) {
452                         BKE_brush_clone_image_delete(brush);
453                         brush->clone.image = ima;
454                         id_us_plus(&ima->id);
455                         brush->clone.offset[0] = brush->clone.offset[1] = 0.0f;
456
457                         return 1;
458                 }
459         }
460
461         return 0;
462 }
463
464 int BKE_brush_clone_image_delete(Brush *brush)
465 {
466         if (brush && brush->clone.image) {
467                 brush->clone.image->id.us--;
468                 brush->clone.image = NULL;
469                 return 1;
470         }
471
472         return 0;
473 }
474
475 /* Brush Sampling for 3d brushes. Currently used for texture painting only, but should be generalized */
476 void BKE_brush_sample_tex(const Scene *scene, Brush *brush, const float sampleco[3], float rgba[4], const int thread, struct ImagePool *pool)
477 {
478         MTex *mtex = &brush->mtex;
479
480         if (mtex && mtex->tex) {
481                 float tin, tr, tg, tb, ta;
482                 int hasrgb;
483                 const int radius = BKE_brush_size_get(scene, brush);
484
485                 if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_3D) {
486                         hasrgb = externtex(mtex, sampleco, &tin, &tr, &tg, &tb, &ta, thread, pool);
487                 }
488                 else {
489                         float co[3];
490
491                         co[0] = sampleco[0] / radius;
492                         co[1] = sampleco[1] / radius;
493                         co[2] = 0.0f;
494
495                         hasrgb = externtex(mtex, co, &tin, &tr, &tg, &tb, &ta, thread, pool);
496                 }
497
498                 if (hasrgb) {
499                         rgba[0] = tr;
500                         rgba[1] = tg;
501                         rgba[2] = tb;
502                         rgba[3] = ta;
503                 }
504                 else {
505                         rgba[0] = tin;
506                         rgba[1] = tin;
507                         rgba[2] = tin;
508                         rgba[3] = 1.0f;
509                 }
510         }
511         else {
512                 rgba[0] = rgba[1] = rgba[2] = rgba[3] = 1.0f;
513         }
514 }
515
516 /* Brush Sampling for 2D brushes. when we unify the brush systems this will be necessarily a separate function */
517 void BKE_brush_sample_tex_2D(const Scene *scene, Brush *brush, const float xy[2], float rgba[4], const int thread)
518 {
519         MTex *mtex = &brush->mtex;
520
521         if (mtex && mtex->tex) {
522                 float co[3], tin, tr, tg, tb, ta;
523                 int hasrgb;
524                 const int radius = BKE_brush_size_get(scene, brush);
525
526                 co[0] = xy[0] / radius;
527                 co[1] = xy[1] / radius;
528                 co[2] = 0.0f;
529
530                 hasrgb = externtex(mtex, co, &tin, &tr, &tg, &tb, &ta, thread, NULL);
531
532                 if (hasrgb) {
533                         rgba[0] = tr;
534                         rgba[1] = tg;
535                         rgba[2] = tb;
536                         rgba[3] = ta;
537                 }
538                 else {
539                         rgba[0] = tin;
540                         rgba[1] = tin;
541                         rgba[2] = tin;
542                         rgba[3] = 1.0f;
543                 }
544         }
545         else {
546                 rgba[0] = rgba[1] = rgba[2] = rgba[3] = 1.0f;
547         }
548 }
549
550 /* TODO, use define for 'texfall' arg
551  * NOTE: only used for 2d brushes currently! */
552 void BKE_brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall, int bufsize, ImBuf **outbuf, int use_color_correction)
553 {
554         ImBuf *ibuf;
555         float xy[2], rgba[4], *dstf;
556         int x, y, rowbytes, xoff, yoff, imbflag;
557         const int radius = BKE_brush_size_get(scene, brush);
558         unsigned char *dst, crgb[3];
559         const float alpha = BKE_brush_alpha_get(scene, brush);
560         float brush_rgb[3];
561
562         imbflag = (flt) ? IB_rectfloat : IB_rect;
563         xoff = -bufsize / 2.0f + 0.5f;
564         yoff = -bufsize / 2.0f + 0.5f;
565         rowbytes = bufsize * 4;
566
567         if (*outbuf)
568                 ibuf = *outbuf;
569         else
570                 ibuf = IMB_allocImBuf(bufsize, bufsize, 32, imbflag);
571
572         if (flt) {
573                 copy_v3_v3(brush_rgb, brush->rgb);
574                 if (use_color_correction) {
575                         srgb_to_linearrgb_v3_v3(brush_rgb, brush_rgb);
576                 }
577
578                 for (y = 0; y < ibuf->y; y++) {
579                         dstf = ibuf->rect_float + y * rowbytes;
580
581                         for (x = 0; x < ibuf->x; x++, dstf += 4) {
582                                 xy[0] = x + xoff;
583                                 xy[1] = y + yoff;
584
585                                 if (texfall == 0) {
586                                         copy_v3_v3(dstf, brush_rgb);
587                                         dstf[3] = alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
588                                 }
589                                 else if (texfall == 1) {
590                                         BKE_brush_sample_tex_2D(scene, brush, xy, dstf, 0);
591                                 }
592                                 else if (texfall == 2) {
593                                         BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
594                                         mul_v3_v3v3(dstf, rgba, brush_rgb);
595                                         dstf[3] = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
596                                 }
597                                 else {
598                                         BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
599                                         copy_v3_v3(dstf, brush_rgb);
600                                         dstf[3] = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
601                                 }
602                         }
603                 }
604         }
605         else {
606                 float alpha_f; /* final float alpha to convert to char */
607                 rgb_float_to_uchar(crgb, brush->rgb);
608
609                 for (y = 0; y < ibuf->y; y++) {
610                         dst = (unsigned char *)ibuf->rect + y * rowbytes;
611
612                         for (x = 0; x < ibuf->x; x++, dst += 4) {
613                                 xy[0] = x + xoff;
614                                 xy[1] = y + yoff;
615
616                                 if (texfall == 0) {
617                                         alpha_f = alpha * BKE_brush_curve_strength(brush, len_v2(xy), radius);
618
619                                         dst[0] = crgb[0];
620                                         dst[1] = crgb[1];
621                                         dst[2] = crgb[2];
622                                         dst[3] = FTOCHAR(alpha_f);
623                                 }
624                                 else if (texfall == 1) {
625                                         BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
626                                         rgba_float_to_uchar(dst, rgba);
627                                 }
628                                 else if (texfall == 2) {
629                                         BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
630                                         mul_v3_v3(rgba, brush->rgb);
631                                         alpha_f = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
632
633                                         rgb_float_to_uchar(dst, rgba);
634
635                                         dst[3] = FTOCHAR(alpha_f);
636                                 }
637                                 else {
638                                         BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
639                                         alpha_f = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
640
641                                         dst[0] = crgb[0];
642                                         dst[1] = crgb[1];
643                                         dst[2] = crgb[2];
644                                         dst[3] = FTOCHAR(alpha_f);
645                                 }
646                         }
647                 }
648         }
649
650         *outbuf = ibuf;
651 }
652
653 /* Unified Size and Strength */
654
655 /* XXX: be careful about setting size and unprojected radius
656  * because they depend on one another
657  * these functions do not set the other corresponding value
658  * this can lead to odd behavior if size and unprojected
659  * radius become inconsistent.
660  * the biggest problem is that it isn't possible to change
661  * unprojected radius because a view context is not
662  * available.  my ussual solution to this is to use the
663  * ratio of change of the size to change the unprojected
664  * radius.  Not completely convinced that is correct.
665  * In any case, a better solution is needed to prevent
666  * inconsistency. */
667
668 void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
669 {
670         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
671         
672         size = (int)((float)size / U.pixelsize);
673         
674         if (ups->flag & UNIFIED_PAINT_SIZE)
675                 ups->size = size;
676         else
677                 brush->size = size;
678 }
679
680 int BKE_brush_size_get(const Scene *scene, Brush *brush)
681 {
682         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
683         int size = (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
684         
685         return (int)((float)size * U.pixelsize);
686 }
687
688 int BKE_brush_use_locked_size(const Scene *scene, Brush *brush)
689 {
690         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
691
692         return (us_flag & UNIFIED_PAINT_SIZE) ?
693                (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
694                (brush->flag & BRUSH_LOCK_SIZE);
695 }
696
697 int BKE_brush_use_size_pressure(const Scene *scene, Brush *brush)
698 {
699         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
700
701         return (us_flag & UNIFIED_PAINT_SIZE) ?
702                (us_flag & UNIFIED_PAINT_BRUSH_SIZE_PRESSURE) :
703                (brush->flag & BRUSH_SIZE_PRESSURE);
704 }
705
706 int BKE_brush_use_alpha_pressure(const Scene *scene, Brush *brush)
707 {
708         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
709
710         return (us_flag & UNIFIED_PAINT_ALPHA) ?
711                (us_flag & UNIFIED_PAINT_BRUSH_ALPHA_PRESSURE) :
712                (brush->flag & BRUSH_ALPHA_PRESSURE);
713 }
714
715 void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
716 {
717         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
718
719         if (ups->flag & UNIFIED_PAINT_SIZE)
720                 ups->unprojected_radius = unprojected_radius;
721         else
722                 brush->unprojected_radius = unprojected_radius;
723 }
724
725 float BKE_brush_unprojected_radius_get(const Scene *scene, Brush *brush)
726 {
727         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
728
729         return (ups->flag & UNIFIED_PAINT_SIZE) ?
730                ups->unprojected_radius :
731                brush->unprojected_radius;
732 }
733
734 void BKE_brush_alpha_set(Scene *scene, Brush *brush, float alpha)
735 {
736         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
737
738         if (ups->flag & UNIFIED_PAINT_ALPHA)
739                 ups->alpha = alpha;
740         else
741                 brush->alpha = alpha;
742 }
743
744 float BKE_brush_alpha_get(const Scene *scene, Brush *brush)
745 {
746         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
747
748         return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
749 }
750
751 float BKE_brush_weight_get(const Scene *scene, Brush *brush)
752 {
753         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
754
755         return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
756 }
757
758 void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
759 {
760         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
761
762         if (ups->flag & UNIFIED_PAINT_WEIGHT)
763                 ups->weight = value;
764         else
765                 brush->weight = value;
766 }
767
768 /* scale unprojected radius to reflect a change in the brush's 2D size */
769 void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
770                                         int new_brush_size,
771                                         int old_brush_size)
772 {
773         float scale = new_brush_size;
774         /* avoid division by zero */
775         if (old_brush_size != 0)
776                 scale /= (float)old_brush_size;
777         (*unprojected_radius) *= scale;
778 }
779
780 /* scale brush size to reflect a change in the brush's unprojected radius */
781 void BKE_brush_scale_size(int *r_brush_size,
782                           float new_unprojected_radius,
783                           float old_unprojected_radius)
784 {
785         float scale = new_unprojected_radius;
786         /* avoid division by zero */
787         if (old_unprojected_radius != 0)
788                 scale /= new_unprojected_radius;
789         (*r_brush_size) = (int)((float)(*r_brush_size) * scale);
790 }
791
792 void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
793 {
794         int use_jitter = (brush->flag & BRUSH_ABSOLUTE_JITTER) ?
795                 (brush->jitter_absolute != 0) : (brush->jitter != 0);
796
797         /* jitter-ed brush gives weird and unpredictable result for this
798          * kinds of stroke, so manually disable jitter usage (sergey) */
799         use_jitter &= (brush->flag & (BRUSH_RESTORE_MESH | BRUSH_ANCHORED)) == 0;
800
801         if (use_jitter) {
802                 float rand_pos[2];
803                 float spread;
804                 int diameter;
805
806                 do {
807                         rand_pos[0] = BLI_frand() - 0.5f;
808                         rand_pos[1] = BLI_frand() - 0.5f;
809                 } while (len_v2(rand_pos) > 0.5f);
810
811
812                 if (brush->flag & BRUSH_ABSOLUTE_JITTER) {
813                         diameter = 2 * brush->jitter_absolute;
814                         spread = 1.0;
815                 }
816                 else {
817                         diameter = 2 * BKE_brush_size_get(scene, brush);
818                         spread = brush->jitter;
819                 }
820                 /* find random position within a circle of diameter 1 */
821                 jitterpos[0] = pos[0] + 2 * rand_pos[0] * diameter * spread;
822                 jitterpos[1] = pos[1] + 2 * rand_pos[1] * diameter * spread;
823         }
824         else {
825                 copy_v2_v2(jitterpos, pos);
826         }
827 }
828
829 /* Uses the brush curve control to find a strength value between 0 and 1 */
830 float BKE_brush_curve_strength_clamp(Brush *br, float p, const float len)
831 {
832         if (p >= len) return 0;
833         else p = p / len;
834
835         curvemapping_initialize(br->curve);
836         p = curvemapping_evaluateF(br->curve, 0, p);
837
838         if (p < 0.0f) p = 0.0f;
839         else if (p > 1.0f) p = 1.0f;
840         return p;
841 }
842 /* same as above but can return negative values if the curve enables
843  * used for sculpt only */
844 float BKE_brush_curve_strength(Brush *br, float p, const float len)
845 {
846         if (p >= len)
847                 p = 1.0f;
848         else
849                 p = p / len;
850
851         curvemapping_initialize(br->curve);
852         return curvemapping_evaluateF(br->curve, 0, p);
853 }
854
855 /* TODO: should probably be unified with BrushPainter stuff? */
856 unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side)
857 {
858         unsigned int *texcache = NULL;
859         MTex *mtex = &br->mtex;
860         TexResult texres = {0};
861         int hasrgb, ix, iy;
862         int side = half_side * 2;
863
864         if (mtex->tex) {
865                 float x, y, step = 2.0 / side, co[3];
866
867                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
868
869                 /*do normalized cannonical view coords for texture*/
870                 for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
871                         for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
872                                 co[0] = x;
873                                 co[1] = y;
874                                 co[2] = 0.0f;
875
876                                 /* This is copied from displace modifier code */
877                                 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 0, &texres, NULL);
878
879                                 /* if the texture gave an RGB value, we assume it didn't give a valid
880                                  * intensity, so calculate one (formula from do_material_tex).
881                                  * if the texture didn't give an RGB value, copy the intensity across
882                                  */
883                                 if (hasrgb & TEX_RGB)
884                                         texres.tin = rgb_to_grayscale(&texres.tr);
885
886                                 ((char *)texcache)[(iy * side + ix) * 4] =
887                                 ((char *)texcache)[(iy * side + ix) * 4 + 1] =
888                                 ((char *)texcache)[(iy * side + ix) * 4 + 2] =
889                                 ((char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(texres.tin * 255.0f);
890                         }
891                 }
892         }
893
894         return texcache;
895 }
896
897
898 /**** Radial Control ****/
899 struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br)
900 {
901         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
902         unsigned int *texcache;
903         int side = 128;
904         int half = side / 2;
905         int i, j;
906
907         texcache = BKE_brush_gen_texture_cache(br, half);
908         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
909         im->x = im->y = side;
910
911         for (i = 0; i < side; ++i) {
912                 for (j = 0; j < side; ++j) {
913                         float magn = sqrtf(powf(i - half, 2) + powf(j - half, 2));
914                         im->rect_float[i * side + j] = BKE_brush_curve_strength_clamp(br, magn, half);
915                 }
916         }
917
918         /* Modulate curve with texture */
919         if (texcache) {
920                 for (i = 0; i < side; ++i) {
921                         for (j = 0; j < side; ++j) {
922                                 const int col = texcache[i * side + j];
923                                 im->rect_float[i * side + j] *= (((char *)&col)[0] + ((char *)&col)[1] + ((char *)&col)[2]) / 3.0f / 255.0f;
924                         }
925                 }
926
927                 MEM_freeN(texcache);
928         }
929
930         return im;
931 }