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