Support for rake in 2D image painting.
[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 /* Generic texture sampler for 3D painting systems. point has to be either in
476  * region space mouse coordinates, or 3d world coordinates for 3D mapping */
477 float BKE_brush_sample_tex_3D(const Scene *scene, Brush *br,
478                               const float point[3],
479                               float rgba[4], const int thread,
480                               struct ImagePool *pool)
481 {
482         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
483         MTex *mtex = &br->mtex;
484         float intensity = 1.0;
485         bool hasrgb = false;
486
487         if (!mtex->tex) {
488                 intensity = 1;
489         }
490         else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
491                 /* Get strength by feeding the vertex
492                  * location directly into a texture */
493                 hasrgb = externtex(mtex, point, &intensity,
494                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
495         }
496         else {
497                 float rotation = -mtex->rot;
498                 float point_2d[2] = {point[0], point[1]};
499                 float x = 0.0f, y = 0.0f; /* Quite warnings */
500                 float radius = 1.0f; /* Quite warnings */
501                 float co[3];
502
503                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
504                         /* keep coordinates relative to mouse */
505
506                         rotation += ups->brush_rotation;
507
508                         point_2d[0] -= ups->tex_mouse[0];
509                         point_2d[1] -= ups->tex_mouse[1];
510
511                         /* use pressure adjusted size for fixed mode */
512                         radius = ups->pixel_radius;
513
514                         x = point_2d[0];
515                         y = point_2d[1];
516                 }
517                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
518                         /* leave the coordinates relative to the screen */
519
520                         /* use unadjusted size for tiled mode */
521                         radius = BKE_brush_size_get(scene, br);
522
523                         x = point_2d[0];
524                         y = point_2d[1];
525                 }
526
527                 x /= radius;
528                 y /= radius;
529
530                 /* it is probably worth optimizing for those cases where
531                  * the texture is not rotated by skipping the calls to
532                  * atan2, sqrtf, sin, and cos. */
533                 if (rotation > 0.001f || rotation < -0.001f) {
534                         const float angle    = atan2f(y, x) + rotation;
535                         const float flen     = sqrtf(x * x + y * y);
536
537                         x = flen * cosf(angle);
538                         y = flen * sinf(angle);
539                 }
540
541                 x *= br->mtex.size[0];
542                 y *= br->mtex.size[1];
543
544                 co[0] = x + br->mtex.ofs[0];
545                 co[1] = y + br->mtex.ofs[1];
546                 co[2] = 0.0f;
547
548                 hasrgb = externtex(mtex, co, &intensity,
549                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
550         }
551
552         intensity += br->texture_sample_bias;
553
554         if (!hasrgb) {
555                 rgba[0] = intensity;
556                 rgba[1] = intensity;
557                 rgba[2] = intensity;
558                 rgba[3] = 1.0f;
559         }
560
561         return intensity;
562 }
563
564
565 /* Brush Sampling for 2D brushes. when we unify the brush systems this will be necessarily a separate function */
566 float BKE_brush_sample_tex_2D(const Scene *scene, Brush *brush, const float xy[2], float rgba[4], struct ImagePool *pool)
567 {
568         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
569         MTex *mtex = &brush->mtex;
570
571         if (mtex && mtex->tex) {
572                 float co[3], tin, tr, tg, tb, ta;
573                 float x = xy[0], y = xy[1];
574                 int hasrgb;
575                 int radius = BKE_brush_size_get(scene, brush);
576                 float rotation = -mtex->rot;
577
578                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
579                         rotation += ups->brush_rotation;
580                         radius = ups->pixel_radius;
581                 }
582
583                 x /= radius;
584                 y /= radius;
585
586                 if (rotation > 0.001f || rotation < -0.001f) {
587                         const float angle    = atan2f(y, x) + rotation;
588                         const float flen     = sqrtf(x * x + y * y);
589
590                         x = flen * cosf(angle);
591                         y = flen * sinf(angle);
592                 }
593
594                 x *= brush->mtex.size[0];
595                 y *= brush->mtex.size[1];
596
597                 co[0] = x + brush->mtex.ofs[0];
598                 co[1] = y + brush->mtex.ofs[1];
599                 co[2] = 0.0f;
600
601                 hasrgb = externtex(mtex, co, &tin, &tr, &tg, &tb, &ta, 0, pool);
602
603                 if (hasrgb) {
604                         rgba[0] = tr;
605                         rgba[1] = tg;
606                         rgba[2] = tb;
607                         rgba[3] = ta;
608                 }
609                 else {
610                         rgba[0] = tin;
611                         rgba[1] = tin;
612                         rgba[2] = tin;
613                         rgba[3] = 1.0f;
614                 }
615                 return tin;
616         }
617         else {
618                 rgba[0] = rgba[1] = rgba[2] = rgba[3] = 1.0f;
619                 return 1.0;
620         }
621 }
622
623 /* TODO, use define for 'texfall' arg
624  * NOTE: only used for 2d brushes currently! */
625 void BKE_brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall, int bufsize, ImBuf **outbuf, int use_color_correction)
626 {
627         ImBuf *ibuf;
628         float xy[2], rgba[4], *dstf;
629         int x, y, rowbytes, xoff, yoff, imbflag;
630         const int radius = BKE_brush_size_get(scene, brush);
631         unsigned char *dst, crgb[3];
632         const float alpha = BKE_brush_alpha_get(scene, brush);
633         float brush_rgb[3];
634
635         imbflag = (flt) ? IB_rectfloat : IB_rect;
636         xoff = -bufsize / 2.0f + 0.5f;
637         yoff = -bufsize / 2.0f + 0.5f;
638         rowbytes = bufsize * 4;
639
640         if (*outbuf)
641                 ibuf = *outbuf;
642         else
643                 ibuf = IMB_allocImBuf(bufsize, bufsize, 32, imbflag);
644
645         if (flt) {
646                 copy_v3_v3(brush_rgb, brush->rgb);
647                 if (use_color_correction) {
648                         srgb_to_linearrgb_v3_v3(brush_rgb, brush_rgb);
649                 }
650
651                 for (y = 0; y < ibuf->y; y++) {
652                         dstf = ibuf->rect_float + y * rowbytes;
653
654                         for (x = 0; x < ibuf->x; x++, dstf += 4) {
655                                 xy[0] = x + xoff;
656                                 xy[1] = y + yoff;
657
658                                 if (texfall == 0) {
659                                         copy_v3_v3(dstf, brush_rgb);
660                                         dstf[3] = alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
661                                 }
662                                 else if (texfall == 1) {
663                                         BKE_brush_sample_tex_2D(scene, brush, xy, dstf, 0);
664                                 }
665                                 else if (texfall == 2) {
666                                         BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
667                                         mul_v3_v3v3(dstf, rgba, brush_rgb);
668                                         dstf[3] = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
669                                 }
670                                 else {
671                                         BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
672                                         copy_v3_v3(dstf, brush_rgb);
673                                         dstf[3] = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
674                                 }
675                         }
676                 }
677         }
678         else {
679                 float alpha_f; /* final float alpha to convert to char */
680                 rgb_float_to_uchar(crgb, brush->rgb);
681
682                 for (y = 0; y < ibuf->y; y++) {
683                         dst = (unsigned char *)ibuf->rect + y * rowbytes;
684
685                         for (x = 0; x < ibuf->x; x++, dst += 4) {
686                                 xy[0] = x + xoff;
687                                 xy[1] = y + yoff;
688
689                                 if (texfall == 0) {
690                                         alpha_f = alpha * BKE_brush_curve_strength(brush, len_v2(xy), radius);
691
692                                         dst[0] = crgb[0];
693                                         dst[1] = crgb[1];
694                                         dst[2] = crgb[2];
695                                         dst[3] = FTOCHAR(alpha_f);
696                                 }
697                                 else if (texfall == 1) {
698                                         BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
699                                         rgba_float_to_uchar(dst, rgba);
700                                 }
701                                 else if (texfall == 2) {
702                                         BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
703                                         mul_v3_v3(rgba, brush->rgb);
704                                         alpha_f = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
705
706                                         rgb_float_to_uchar(dst, rgba);
707
708                                         dst[3] = FTOCHAR(alpha_f);
709                                 }
710                                 else {
711                                         BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
712                                         alpha_f = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
713
714                                         dst[0] = crgb[0];
715                                         dst[1] = crgb[1];
716                                         dst[2] = crgb[2];
717                                         dst[3] = FTOCHAR(alpha_f);
718                                 }
719                         }
720                 }
721         }
722
723         *outbuf = ibuf;
724 }
725
726 /* Unified Size and Strength */
727
728 /* XXX: be careful about setting size and unprojected radius
729  * because they depend on one another
730  * these functions do not set the other corresponding value
731  * this can lead to odd behavior if size and unprojected
732  * radius become inconsistent.
733  * the biggest problem is that it isn't possible to change
734  * unprojected radius because a view context is not
735  * available.  my ussual solution to this is to use the
736  * ratio of change of the size to change the unprojected
737  * radius.  Not completely convinced that is correct.
738  * In any case, a better solution is needed to prevent
739  * inconsistency. */
740
741 void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
742 {
743         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
744         
745         size = (int)((float)size / U.pixelsize);
746         
747         if (ups->flag & UNIFIED_PAINT_SIZE)
748                 ups->size = size;
749         else
750                 brush->size = size;
751 }
752
753 int BKE_brush_size_get(const Scene *scene, Brush *brush)
754 {
755         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
756         int size = (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
757         
758         return (int)((float)size * U.pixelsize);
759 }
760
761 int BKE_brush_use_locked_size(const Scene *scene, Brush *brush)
762 {
763         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
764
765         return (us_flag & UNIFIED_PAINT_SIZE) ?
766                (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
767                (brush->flag & BRUSH_LOCK_SIZE);
768 }
769
770 int BKE_brush_use_size_pressure(const Scene *scene, Brush *brush)
771 {
772         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
773
774         return (us_flag & UNIFIED_PAINT_SIZE) ?
775                (us_flag & UNIFIED_PAINT_BRUSH_SIZE_PRESSURE) :
776                (brush->flag & BRUSH_SIZE_PRESSURE);
777 }
778
779 int BKE_brush_use_alpha_pressure(const Scene *scene, Brush *brush)
780 {
781         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
782
783         return (us_flag & UNIFIED_PAINT_ALPHA) ?
784                (us_flag & UNIFIED_PAINT_BRUSH_ALPHA_PRESSURE) :
785                (brush->flag & BRUSH_ALPHA_PRESSURE);
786 }
787
788 void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
789 {
790         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
791
792         if (ups->flag & UNIFIED_PAINT_SIZE)
793                 ups->unprojected_radius = unprojected_radius;
794         else
795                 brush->unprojected_radius = unprojected_radius;
796 }
797
798 float BKE_brush_unprojected_radius_get(const Scene *scene, Brush *brush)
799 {
800         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
801
802         return (ups->flag & UNIFIED_PAINT_SIZE) ?
803                ups->unprojected_radius :
804                brush->unprojected_radius;
805 }
806
807 void BKE_brush_alpha_set(Scene *scene, Brush *brush, float alpha)
808 {
809         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
810
811         if (ups->flag & UNIFIED_PAINT_ALPHA)
812                 ups->alpha = alpha;
813         else
814                 brush->alpha = alpha;
815 }
816
817 float BKE_brush_alpha_get(const Scene *scene, Brush *brush)
818 {
819         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
820
821         return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
822 }
823
824 float BKE_brush_weight_get(const Scene *scene, Brush *brush)
825 {
826         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
827
828         return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
829 }
830
831 void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
832 {
833         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
834
835         if (ups->flag & UNIFIED_PAINT_WEIGHT)
836                 ups->weight = value;
837         else
838                 brush->weight = value;
839 }
840
841 /* scale unprojected radius to reflect a change in the brush's 2D size */
842 void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
843                                         int new_brush_size,
844                                         int old_brush_size)
845 {
846         float scale = new_brush_size;
847         /* avoid division by zero */
848         if (old_brush_size != 0)
849                 scale /= (float)old_brush_size;
850         (*unprojected_radius) *= scale;
851 }
852
853 /* scale brush size to reflect a change in the brush's unprojected radius */
854 void BKE_brush_scale_size(int *r_brush_size,
855                           float new_unprojected_radius,
856                           float old_unprojected_radius)
857 {
858         float scale = new_unprojected_radius;
859         /* avoid division by zero */
860         if (old_unprojected_radius != 0)
861                 scale /= new_unprojected_radius;
862         (*r_brush_size) = (int)((float)(*r_brush_size) * scale);
863 }
864
865 void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
866 {
867         int use_jitter = (brush->flag & BRUSH_ABSOLUTE_JITTER) ?
868                 (brush->jitter_absolute != 0) : (brush->jitter != 0);
869
870         /* jitter-ed brush gives weird and unpredictable result for this
871          * kinds of stroke, so manually disable jitter usage (sergey) */
872         use_jitter &= (brush->flag & (BRUSH_RESTORE_MESH | BRUSH_ANCHORED)) == 0;
873
874         if (use_jitter) {
875                 float rand_pos[2];
876                 float spread;
877                 int diameter;
878
879                 do {
880                         rand_pos[0] = BLI_frand() - 0.5f;
881                         rand_pos[1] = BLI_frand() - 0.5f;
882                 } while (len_v2(rand_pos) > 0.5f);
883
884
885                 if (brush->flag & BRUSH_ABSOLUTE_JITTER) {
886                         diameter = 2 * brush->jitter_absolute;
887                         spread = 1.0;
888                 }
889                 else {
890                         diameter = 2 * BKE_brush_size_get(scene, brush);
891                         spread = brush->jitter;
892                 }
893                 /* find random position within a circle of diameter 1 */
894                 jitterpos[0] = pos[0] + 2 * rand_pos[0] * diameter * spread;
895                 jitterpos[1] = pos[1] + 2 * rand_pos[1] * diameter * spread;
896         }
897         else {
898                 copy_v2_v2(jitterpos, pos);
899         }
900 }
901
902 /* Uses the brush curve control to find a strength value between 0 and 1 */
903 float BKE_brush_curve_strength_clamp(Brush *br, float p, const float len)
904 {
905         if (p >= len) return 0;
906         else p = p / len;
907
908         curvemapping_initialize(br->curve);
909         p = curvemapping_evaluateF(br->curve, 0, p);
910
911         if (p < 0.0f) p = 0.0f;
912         else if (p > 1.0f) p = 1.0f;
913         return p;
914 }
915 /* same as above but can return negative values if the curve enables
916  * used for sculpt only */
917 float BKE_brush_curve_strength(Brush *br, float p, const float len)
918 {
919         if (p >= len)
920                 p = 1.0f;
921         else
922                 p = p / len;
923
924         curvemapping_initialize(br->curve);
925         return curvemapping_evaluateF(br->curve, 0, p);
926 }
927
928 /* TODO: should probably be unified with BrushPainter stuff? */
929 unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side)
930 {
931         unsigned int *texcache = NULL;
932         MTex *mtex = &br->mtex;
933         TexResult texres = {0};
934         int hasrgb, ix, iy;
935         int side = half_side * 2;
936
937         if (mtex->tex) {
938                 float x, y, step = 2.0 / side, co[3];
939
940                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
941
942                 /*do normalized cannonical view coords for texture*/
943                 for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
944                         for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
945                                 co[0] = x;
946                                 co[1] = y;
947                                 co[2] = 0.0f;
948
949                                 /* This is copied from displace modifier code */
950                                 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 0, &texres, NULL);
951
952                                 /* if the texture gave an RGB value, we assume it didn't give a valid
953                                  * intensity, so calculate one (formula from do_material_tex).
954                                  * if the texture didn't give an RGB value, copy the intensity across
955                                  */
956                                 if (hasrgb & TEX_RGB)
957                                         texres.tin = rgb_to_grayscale(&texres.tr);
958
959                                 ((char *)texcache)[(iy * side + ix) * 4] =
960                                 ((char *)texcache)[(iy * side + ix) * 4 + 1] =
961                                 ((char *)texcache)[(iy * side + ix) * 4 + 2] =
962                                 ((char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(texres.tin * 255.0f);
963                         }
964                 }
965         }
966
967         return texcache;
968 }
969
970
971 /**** Radial Control ****/
972 struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br)
973 {
974         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
975         unsigned int *texcache;
976         int side = 128;
977         int half = side / 2;
978         int i, j;
979
980         texcache = BKE_brush_gen_texture_cache(br, half);
981         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
982         im->x = im->y = side;
983
984         for (i = 0; i < side; ++i) {
985                 for (j = 0; j < side; ++j) {
986                         float magn = sqrtf(powf(i - half, 2) + powf(j - half, 2));
987                         im->rect_float[i * side + j] = BKE_brush_curve_strength_clamp(br, magn, half);
988                 }
989         }
990
991         /* Modulate curve with texture */
992         if (texcache) {
993                 for (i = 0; i < side; ++i) {
994                         for (j = 0; j < side; ++j) {
995                                 const int col = texcache[i * side + j];
996                                 im->rect_float[i * side + j] *= (((char *)&col)[0] + ((char *)&col)[1] + ((char *)&col)[2]) / 3.0f / 255.0f;
997                         }
998                 }
999
1000                 MEM_freeN(texcache);
1001         }
1002
1003         return im;
1004 }