And more UI messages spell check.
[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
33 #include <math.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_brush_types.h"
39 #include "DNA_color_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_windowmanager_types.h"
43
44 #include "WM_types.h"
45
46 #include "RNA_access.h"
47
48 #include "BLI_bpath.h"
49 #include "BLI_math.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_rand.h"
52 #include "BLI_utildefines.h"
53
54 #include "BKE_brush.h"
55 #include "BKE_colortools.h"
56 #include "BKE_global.h"
57 #include "BKE_image.h"
58 #include "BKE_library.h"
59 #include "BKE_main.h"
60 #include "BKE_paint.h"
61 #include "BKE_texture.h"
62 #include "BKE_icons.h"
63
64 #include "IMB_imbuf.h"
65 #include "IMB_imbuf_types.h"
66
67 #include "RE_render_ext.h" /* externtex */
68 #include "RE_shader_ext.h"
69
70 static void brush_defaults(Brush *brush)
71 {
72         brush->blend = 0;
73         brush->flag = 0;
74
75         brush->ob_mode = OB_MODE_ALL_PAINT;
76
77         /* BRUSH SCULPT TOOL SETTINGS */
78         brush->weight = 1.0f; /* weight of brush 0 - 1.0 */
79         brush->size = 35; /* radius of the brush in pixels */
80         brush->alpha = 0.5f; /* brush strength/intensity probably variable should be renamed? */
81         brush->autosmooth_factor = 0.0f;
82         brush->crease_pinch_factor = 0.5f;
83         brush->sculpt_plane = SCULPT_DISP_DIR_AREA;
84         brush->plane_offset = 0.0f; /* how far above or below the plane that is found by averaging the faces */
85         brush->plane_trim = 0.5f;
86         brush->clone.alpha = 0.5f;
87         brush->normal_weight = 0.0f;
88         brush->flag |= BRUSH_ALPHA_PRESSURE;
89
90         /* BRUSH PAINT TOOL SETTINGS */
91         brush->rgb[0] = 1.0f; /* default rgb color of the brush when painting - white */
92         brush->rgb[1] = 1.0f;
93         brush->rgb[2] = 1.0f;
94
95         /* BRUSH STROKE SETTINGS */
96         brush->flag |= (BRUSH_SPACE | BRUSH_SPACE_ATTEN);
97         brush->spacing = 10; /* how far each brush dot should be spaced as a percentage of brush diameter */
98
99         brush->smooth_stroke_radius = 75;
100         brush->smooth_stroke_factor = 0.9f;
101
102         brush->rate = 0.1f; /* time delay between dots of paint or sculpting when doing airbrush mode */
103
104         brush->jitter = 0.0f;
105
106         /* BRUSH TEXTURE SETTINGS */
107         default_mtex(&brush->mtex);
108
109         brush->texture_sample_bias = 0; /* value to added to texture samples */
110         brush->texture_overlay_alpha = 33;
111
112         /* brush appearance  */
113
114         brush->add_col[0] = 1.00; /* add mode color is light red */
115         brush->add_col[1] = 0.39;
116         brush->add_col[2] = 0.39;
117
118         brush->sub_col[0] = 0.39; /* subtract mode color is light blue */
119         brush->sub_col[1] = 0.39;
120         brush->sub_col[2] = 1.00;
121 }
122
123 /* Datablock add/copy/free/make_local */
124
125 Brush *BKE_brush_add(const char *name)
126 {
127         Brush *brush;
128
129         brush = BKE_libblock_alloc(&G.main->brush, ID_BR, name);
130
131         /* enable fake user by default */
132         brush->id.flag |= LIB_FAKEUSER;
133
134         brush_defaults(brush);
135
136         brush->sculpt_tool = SCULPT_TOOL_DRAW; /* sculpting defaults to the draw tool for new brushes */
137
138         /* the default alpha falloff curve */
139         BKE_brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
140
141         return brush;
142 }
143
144 Brush *BKE_brush_copy(Brush *brush)
145 {
146         Brush *brushn;
147         
148         brushn = BKE_libblock_copy(&brush->id);
149
150         if (brush->mtex.tex)
151                 id_us_plus((ID *)brush->mtex.tex);
152
153         if (brush->icon_imbuf)
154                 brushn->icon_imbuf = IMB_dupImBuf(brush->icon_imbuf);
155
156         brushn->preview = NULL;
157
158         brushn->curve = curvemapping_copy(brush->curve);
159
160         /* enable fake user by default */
161         if (!(brushn->id.flag & LIB_FAKEUSER)) {
162                 brushn->id.flag |= LIB_FAKEUSER;
163                 brushn->id.us++;
164         }
165         
166         return brushn;
167 }
168
169 /* not brush itself */
170 void BKE_brush_free(Brush *brush)
171 {
172         if (brush->mtex.tex)
173                 brush->mtex.tex->id.us--;
174
175         if (brush->icon_imbuf)
176                 IMB_freeImBuf(brush->icon_imbuf);
177
178         BKE_previewimg_free(&(brush->preview));
179
180         curvemapping_free(brush->curve);
181 }
182
183 static void extern_local_brush(Brush *brush)
184 {
185         id_lib_extern((ID *)brush->mtex.tex);
186         id_lib_extern((ID *)brush->clone.image);
187 }
188
189 void BKE_brush_make_local(Brush *brush)
190 {
191
192         /* - only lib users: do nothing
193          * - only local users: set flag
194          * - mixed: make copy
195          */
196
197         Main *bmain = G.main;
198         Scene *scene;
199         int is_local = FALSE, is_lib = FALSE;
200
201         if (brush->id.lib == NULL) return;
202
203         if (brush->clone.image) {
204                 /* special case: ima always local immediately. Clone image should only
205                  * have one user anyway. */
206                 id_clear_lib_data(bmain, &brush->clone.image->id);
207                 extern_local_brush(brush);
208         }
209
210         for (scene = bmain->scene.first; scene && ELEM(0, is_lib, is_local); scene = scene->id.next) {
211                 if (paint_brush(&scene->toolsettings->imapaint.paint) == brush) {
212                         if (scene->id.lib) is_lib = TRUE;
213                         else is_local = TRUE;
214                 }
215         }
216
217         if (is_local && is_lib == FALSE) {
218                 id_clear_lib_data(bmain, &brush->id);
219                 extern_local_brush(brush);
220
221                 /* enable fake user by default */
222                 if (!(brush->id.flag & LIB_FAKEUSER)) {
223                         brush->id.flag |= LIB_FAKEUSER;
224                         brush->id.us++;
225                 }
226         }
227         else if (is_local && is_lib) {
228                 Brush *brush_new = BKE_brush_copy(brush);
229                 brush_new->id.us = 1; /* only keep fake user */
230                 brush_new->id.flag |= LIB_FAKEUSER;
231
232                 /* Remap paths of new ID using old library as base. */
233                 BKE_id_lib_local_paths(bmain, brush->id.lib, &brush_new->id);
234                 
235                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
236                         if (paint_brush(&scene->toolsettings->imapaint.paint) == brush) {
237                                 if (scene->id.lib == NULL) {
238                                         paint_brush_set(&scene->toolsettings->imapaint.paint, brush_new);
239                                 }
240                         }
241                 }
242         }
243 }
244
245 void BKE_brush_debug_print_state(Brush *br)
246 {
247         /* create a fake brush and set it to the defaults */
248         Brush def = {{NULL}};
249         brush_defaults(&def);
250         
251 #define BR_TEST(field, t)                                       \
252         if (br->field != def.field)                             \
253                 printf("br->" #field " = %" #t ";\n", br->field)
254
255 #define BR_TEST_FLAG(_f)                                                        \
256         if ((br->flag & _f) && !(def.flag & _f))                \
257                 printf("br->flag |= " #_f ";\n");                       \
258         else if (!(br->flag & _f) && (def.flag & _f))   \
259                 printf("br->flag &= ~" #_f ";\n")
260         
261
262         /* print out any non-default brush state */
263         BR_TEST(normal_weight, f);
264
265         BR_TEST(blend, d);
266         BR_TEST(size, d);
267
268         /* br->flag */
269         BR_TEST_FLAG(BRUSH_AIRBRUSH);
270         BR_TEST_FLAG(BRUSH_TORUS);
271         BR_TEST_FLAG(BRUSH_ALPHA_PRESSURE);
272         BR_TEST_FLAG(BRUSH_SIZE_PRESSURE);
273         BR_TEST_FLAG(BRUSH_JITTER_PRESSURE);
274         BR_TEST_FLAG(BRUSH_SPACING_PRESSURE);
275         BR_TEST_FLAG(BRUSH_FIXED_TEX);
276         BR_TEST_FLAG(BRUSH_RAKE);
277         BR_TEST_FLAG(BRUSH_ANCHORED);
278         BR_TEST_FLAG(BRUSH_DIR_IN);
279         BR_TEST_FLAG(BRUSH_SPACE);
280         BR_TEST_FLAG(BRUSH_SMOOTH_STROKE);
281         BR_TEST_FLAG(BRUSH_PERSISTENT);
282         BR_TEST_FLAG(BRUSH_ACCUMULATE);
283         BR_TEST_FLAG(BRUSH_LOCK_ALPHA);
284         BR_TEST_FLAG(BRUSH_ORIGINAL_NORMAL);
285         BR_TEST_FLAG(BRUSH_OFFSET_PRESSURE);
286         BR_TEST_FLAG(BRUSH_SPACE_ATTEN);
287         BR_TEST_FLAG(BRUSH_ADAPTIVE_SPACE);
288         BR_TEST_FLAG(BRUSH_LOCK_SIZE);
289         BR_TEST_FLAG(BRUSH_TEXTURE_OVERLAY);
290         BR_TEST_FLAG(BRUSH_EDGE_TO_EDGE);
291         BR_TEST_FLAG(BRUSH_RESTORE_MESH);
292         BR_TEST_FLAG(BRUSH_INVERSE_SMOOTH_PRESSURE);
293         BR_TEST_FLAG(BRUSH_RANDOM_ROTATION);
294         BR_TEST_FLAG(BRUSH_PLANE_TRIM);
295         BR_TEST_FLAG(BRUSH_FRONTFACE);
296         BR_TEST_FLAG(BRUSH_CUSTOM_ICON);
297
298         BR_TEST(jitter, f);
299         BR_TEST(spacing, d);
300         BR_TEST(smooth_stroke_radius, d);
301         BR_TEST(smooth_stroke_factor, f);
302         BR_TEST(rate, f);
303
304         BR_TEST(alpha, f);
305
306         BR_TEST(sculpt_plane, d);
307
308         BR_TEST(plane_offset, f);
309
310         BR_TEST(autosmooth_factor, f);
311
312         BR_TEST(crease_pinch_factor, f);
313
314         BR_TEST(plane_trim, f);
315
316         BR_TEST(texture_sample_bias, f);
317         BR_TEST(texture_overlay_alpha, d);
318
319         BR_TEST(add_col[0], f);
320         BR_TEST(add_col[1], f);
321         BR_TEST(add_col[2], f);
322         BR_TEST(sub_col[0], f);
323         BR_TEST(sub_col[1], f);
324         BR_TEST(sub_col[2], f);
325
326         printf("\n");
327         
328 #undef BR_TEST
329 #undef BR_TEST_FLAG
330 }
331
332 void BKE_brush_sculpt_reset(Brush *br)
333 {
334         /* enable this to see any non-default
335          * settings used by a brush: */
336         // BKE_brush_debug_print_state(br);
337
338         brush_defaults(br);
339         BKE_brush_curve_preset(br, CURVE_PRESET_SMOOTH);
340
341         switch (br->sculpt_tool) {
342                 case SCULPT_TOOL_CLAY:
343                         br->flag |= BRUSH_FRONTFACE;
344                         break;
345                 case SCULPT_TOOL_CREASE:
346                         br->flag |= BRUSH_DIR_IN;
347                         br->alpha = 0.25;
348                         break;
349                 case SCULPT_TOOL_FILL:
350                         br->add_col[1] = 1;
351                         br->sub_col[0] = 0.25;
352                         br->sub_col[1] = 1;
353                         break;
354                 case SCULPT_TOOL_FLATTEN:
355                         br->add_col[1] = 1;
356                         br->sub_col[0] = 0.25;
357                         br->sub_col[1] = 1;
358                         break;
359                 case SCULPT_TOOL_INFLATE:
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_NUDGE:
368                         br->add_col[0] = 0.250000;
369                         br->add_col[1] = 1.000000;
370                         br->add_col[2] = 0.250000;
371                         break;
372                 case SCULPT_TOOL_PINCH:
373                         br->add_col[0] = 0.750000;
374                         br->add_col[1] = 0.750000;
375                         br->add_col[2] = 0.750000;
376                         br->sub_col[0] = 0.250000;
377                         br->sub_col[1] = 0.250000;
378                         br->sub_col[2] = 0.250000;
379                         break;
380                 case SCULPT_TOOL_SCRAPE:
381                         br->add_col[1] = 1.000000;
382                         br->sub_col[0] = 0.250000;
383                         br->sub_col[1] = 1.000000;
384                         break;
385                 case SCULPT_TOOL_ROTATE:
386                         br->alpha = 1.0;
387                         break;
388                 case SCULPT_TOOL_SMOOTH:
389                         br->flag &= ~BRUSH_SPACE_ATTEN;
390                         br->spacing = 5;
391                         br->add_col[0] = 0.750000;
392                         br->add_col[1] = 0.750000;
393                         br->add_col[2] = 0.750000;
394                         break;
395                 case SCULPT_TOOL_GRAB:
396                 case SCULPT_TOOL_SNAKE_HOOK:
397                 case SCULPT_TOOL_THUMB:
398                         br->size = 75;
399                         br->flag &= ~BRUSH_ALPHA_PRESSURE;
400                         br->flag &= ~BRUSH_SPACE;
401                         br->flag &= ~BRUSH_SPACE_ATTEN;
402                         br->add_col[0] = 0.250000;
403                         br->add_col[1] = 1.000000;
404                         br->add_col[2] = 0.250000;
405                         break;
406                 default:
407                         break;
408         }
409 }
410
411 /* Library Operations */
412 void BKE_brush_curve_preset(Brush *b, /*CurveMappingPreset*/ int preset)
413 {
414         CurveMap *cm = NULL;
415
416         if (!b->curve)
417                 b->curve = curvemapping_add(1, 0, 0, 1, 1);
418
419         cm = b->curve->cm;
420         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
421
422         b->curve->preset = preset;
423         curvemap_reset(cm, &b->curve->clipr, b->curve->preset, CURVEMAP_SLOPE_NEGATIVE);
424         curvemapping_changed(b->curve, FALSE);
425 }
426
427 int BKE_brush_texture_set_nr(Brush *brush, int nr)
428 {
429         ID *idtest, *id = NULL;
430
431         id = (ID *)brush->mtex.tex;
432
433         idtest = (ID *)BLI_findlink(&G.main->tex, nr - 1);
434         if (idtest == NULL) { /* new tex */
435                 if (id) idtest = (ID *)BKE_texture_copy((Tex *)id);
436                 else idtest = (ID *)add_texture("Tex");
437                 idtest->us--;
438         }
439         if (idtest != id) {
440                 BKE_brush_texture_delete(brush);
441
442                 brush->mtex.tex = (Tex *)idtest;
443                 id_us_plus(idtest);
444
445                 return 1;
446         }
447
448         return 0;
449 }
450
451 int BKE_brush_texture_delete(Brush *brush)
452 {
453         if (brush->mtex.tex)
454                 brush->mtex.tex->id.us--;
455
456         return 1;
457 }
458
459 int BKE_brush_clone_image_set_nr(Brush *brush, int nr)
460 {
461         if (brush && nr > 0) {
462                 Image *ima = (Image *)BLI_findlink(&G.main->image, nr - 1);
463
464                 if (ima) {
465                         BKE_brush_clone_image_delete(brush);
466                         brush->clone.image = ima;
467                         id_us_plus(&ima->id);
468                         brush->clone.offset[0] = brush->clone.offset[1] = 0.0f;
469
470                         return 1;
471                 }
472         }
473
474         return 0;
475 }
476
477 int BKE_brush_clone_image_delete(Brush *brush)
478 {
479         if (brush && brush->clone.image) {
480                 brush->clone.image->id.us--;
481                 brush->clone.image = NULL;
482                 return 1;
483         }
484
485         return 0;
486 }
487
488 /* Brush Sampling */
489 void BKE_brush_sample_tex(const Scene *scene, Brush *brush, const float xy[2], float rgba[4], const int thread)
490 {
491         MTex *mtex = &brush->mtex;
492
493         if (mtex && mtex->tex) {
494                 float co[3], tin, tr, tg, tb, ta;
495                 int hasrgb;
496                 const int radius = BKE_brush_size_get(scene, brush);
497
498                 co[0] = xy[0] / radius;
499                 co[1] = xy[1] / radius;
500                 co[2] = 0.0f;
501
502                 hasrgb = externtex(mtex, co, &tin, &tr, &tg, &tb, &ta, thread);
503
504                 if (hasrgb) {
505                         rgba[0] = tr;
506                         rgba[1] = tg;
507                         rgba[2] = tb;
508                         rgba[3] = ta;
509                 }
510                 else {
511                         rgba[0] = tin;
512                         rgba[1] = tin;
513                         rgba[2] = tin;
514                         rgba[3] = 1.0f;
515                 }
516         }
517         else {
518                 rgba[0] = rgba[1] = rgba[2] = rgba[3] = 1.0f;
519         }
520 }
521
522 /* TODO, use define for 'texfall' arg */
523 void BKE_brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall, int bufsize, ImBuf **outbuf, int use_color_correction)
524 {
525         ImBuf *ibuf;
526         float xy[2], rgba[4], *dstf;
527         int x, y, rowbytes, xoff, yoff, imbflag;
528         const int radius = BKE_brush_size_get(scene, brush);
529         unsigned char *dst, crgb[3];
530         const float alpha = BKE_brush_alpha_get(scene, brush);
531         float brush_rgb[3];
532
533         imbflag = (flt) ? IB_rectfloat : IB_rect;
534         xoff = -bufsize / 2.0f + 0.5f;
535         yoff = -bufsize / 2.0f + 0.5f;
536         rowbytes = bufsize * 4;
537
538         if (*outbuf)
539                 ibuf = *outbuf;
540         else
541                 ibuf = IMB_allocImBuf(bufsize, bufsize, 32, imbflag);
542
543         if (flt) {
544                 copy_v3_v3(brush_rgb, brush->rgb);
545                 if (use_color_correction) {
546                         srgb_to_linearrgb_v3_v3(brush_rgb, brush_rgb);
547                 }
548
549                 for (y = 0; y < ibuf->y; y++) {
550                         dstf = ibuf->rect_float + y * rowbytes;
551
552                         for (x = 0; x < ibuf->x; x++, dstf += 4) {
553                                 xy[0] = x + xoff;
554                                 xy[1] = y + yoff;
555
556                                 if (texfall == 0) {
557                                         copy_v3_v3(dstf, brush_rgb);
558                                         dstf[3] = alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
559                                 }
560                                 else if (texfall == 1) {
561                                         BKE_brush_sample_tex(scene, brush, xy, dstf, 0);
562                                 }
563                                 else {
564                                         BKE_brush_sample_tex(scene, brush, xy, rgba, 0);
565                                         mul_v3_v3v3(dstf, rgba, brush_rgb);
566                                         dstf[3] = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
567                                 }
568                         }
569                 }
570         }
571         else {
572                 float alpha_f; /* final float alpha to convert to char */
573                 rgb_float_to_uchar(crgb, brush->rgb);
574
575                 for (y = 0; y < ibuf->y; y++) {
576                         dst = (unsigned char *)ibuf->rect + y * rowbytes;
577
578                         for (x = 0; x < ibuf->x; x++, dst += 4) {
579                                 xy[0] = x + xoff;
580                                 xy[1] = y + yoff;
581
582                                 if (texfall == 0) {
583                                         alpha_f = alpha * BKE_brush_curve_strength(brush, len_v2(xy), radius);
584
585                                         dst[0] = crgb[0];
586                                         dst[1] = crgb[1];
587                                         dst[2] = crgb[2];
588                                         dst[3] = FTOCHAR(alpha_f);
589                                 }
590                                 else if (texfall == 1) {
591                                         BKE_brush_sample_tex(scene, brush, xy, rgba, 0);
592                                         rgba_float_to_uchar(dst, rgba);
593                                 }
594                                 else if (texfall == 2) {
595                                         BKE_brush_sample_tex(scene, brush, xy, rgba, 0);
596                                         mul_v3_v3(rgba, brush->rgb);
597                                         alpha_f = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
598
599                                         rgb_float_to_uchar(dst, rgba);
600
601                                         dst[3] = FTOCHAR(alpha_f);
602                                 }
603                                 else {
604                                         BKE_brush_sample_tex(scene, brush, xy, rgba, 0);
605                                         alpha_f = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
606
607                                         dst[0] = crgb[0];
608                                         dst[1] = crgb[1];
609                                         dst[2] = crgb[2];
610                                         dst[3] = FTOCHAR(alpha_f);
611                                 }
612                         }
613                 }
614         }
615
616         *outbuf = ibuf;
617 }
618
619 /* Unified Size and Strength */
620
621 // XXX: be careful about setting size and unprojected radius
622 // because they depend on one another
623 // these functions do not set the other corresponding value
624 // this can lead to odd behavior if size and unprojected
625 // radius become inconsistent.
626 // the biggest problem is that it isn't possible to change
627 // unprojected radius because a view context is not
628 // available.  my ussual solution to this is to use the
629 // ratio of change of the size to change the unprojected
630 // radius.  Not completely convinced that is correct.
631 // In anycase, a better solution is needed to prevent
632 // inconsistency.
633
634 void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
635 {
636         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
637
638         if (ups->flag & UNIFIED_PAINT_SIZE)
639                 ups->size = size;
640         else
641                 brush->size = size;
642 }
643
644 int BKE_brush_size_get(const Scene *scene, Brush *brush)
645 {
646         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
647
648         return (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
649 }
650
651 int BKE_brush_use_locked_size(const Scene *scene, Brush *brush)
652 {
653         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
654
655         return (us_flag & UNIFIED_PAINT_SIZE) ?
656                (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
657                (brush->flag & BRUSH_LOCK_SIZE);
658 }
659
660 int BKE_brush_use_size_pressure(const Scene *scene, Brush *brush)
661 {
662         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
663
664         return (us_flag & UNIFIED_PAINT_SIZE) ?
665                (us_flag & UNIFIED_PAINT_BRUSH_SIZE_PRESSURE) :
666                (brush->flag & BRUSH_SIZE_PRESSURE);
667 }
668
669 int BKE_brush_use_alpha_pressure(const Scene *scene, Brush *brush)
670 {
671         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
672
673         return (us_flag & UNIFIED_PAINT_ALPHA) ?
674                (us_flag & UNIFIED_PAINT_BRUSH_ALPHA_PRESSURE) :
675                (brush->flag & BRUSH_ALPHA_PRESSURE);
676 }
677
678 void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
679 {
680         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
681
682         if (ups->flag & UNIFIED_PAINT_SIZE)
683                 ups->unprojected_radius = unprojected_radius;
684         else
685                 brush->unprojected_radius = unprojected_radius;
686 }
687
688 float BKE_brush_unprojected_radius_get(const Scene *scene, Brush *brush)
689 {
690         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
691
692         return (ups->flag & UNIFIED_PAINT_SIZE) ?
693                ups->unprojected_radius :
694                brush->unprojected_radius;
695 }
696
697 static void brush_alpha_set(Scene *scene, Brush *brush, float alpha)
698 {
699         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
700
701         if (ups->flag & UNIFIED_PAINT_ALPHA)
702                 ups->alpha = alpha;
703         else
704                 brush->alpha = alpha;
705 }
706
707 float BKE_brush_alpha_get(const Scene *scene, Brush *brush)
708 {
709         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
710
711         return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
712 }
713
714 float BKE_brush_weight_get(const Scene *scene, Brush *brush)
715 {
716         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
717
718         return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
719 }
720
721 void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
722 {
723         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
724
725         if (ups->flag & UNIFIED_PAINT_WEIGHT)
726                 ups->weight = value;
727         else
728                 brush->weight = value;
729 }
730
731 /* scale unprojected radius to reflect a change in the brush's 2D size */
732 void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
733                                         int new_brush_size,
734                                         int old_brush_size)
735 {
736         float scale = new_brush_size;
737         /* avoid division by zero */
738         if (old_brush_size != 0)
739                 scale /= (float)old_brush_size;
740         (*unprojected_radius) *= scale;
741 }
742
743 /* scale brush size to reflect a change in the brush's unprojected radius */
744 void BKE_brush_scale_size(int *r_brush_size,
745                           float new_unprojected_radius,
746                           float old_unprojected_radius)
747 {
748         float scale = new_unprojected_radius;
749         /* avoid division by zero */
750         if (old_unprojected_radius != 0)
751                 scale /= new_unprojected_radius;
752         (*r_brush_size) = (int)((float)(*r_brush_size) * scale);
753 }
754
755 /* Brush Painting */
756
757 typedef struct BrushPainterCache {
758         short enabled;
759
760         int size;           /* size override, if 0 uses 2*BKE_brush_size_get(brush) */
761         short flt;          /* need float imbuf? */
762         short texonly;      /* no alpha, color or fallof, only texture in imbuf */
763
764         int lastsize;
765         float lastalpha;
766         float lastjitter;
767
768         ImBuf *ibuf;
769         ImBuf *texibuf;
770         ImBuf *maskibuf;
771 } BrushPainterCache;
772
773 struct BrushPainter {
774         Scene *scene;
775         Brush *brush;
776
777         float lastmousepos[2];  /* mouse position of last paint call */
778
779         float accumdistance;    /* accumulated distance of brush since last paint op */
780         float lastpaintpos[2];  /* position of last paint op */
781         float startpaintpos[2]; /* position of first paint */
782
783         double accumtime;       /* accumulated time since last paint op (airbrush) */
784         double lasttime;        /* time of last update */
785
786         float lastpressure;
787
788         short firsttouch;       /* first paint op */
789
790         float startsize;
791         float startalpha;
792         float startjitter;
793         float startspacing;
794
795         BrushPainterCache cache;
796 };
797
798 BrushPainter *BKE_brush_painter_new(Scene *scene, Brush *brush)
799 {
800         BrushPainter *painter = MEM_callocN(sizeof(BrushPainter), "BrushPainter");
801
802         painter->brush = brush;
803         painter->scene = scene;
804         painter->firsttouch = 1;
805         painter->cache.lastsize = -1; /* force ibuf create in refresh */
806
807         painter->startsize = BKE_brush_size_get(scene, brush);
808         painter->startalpha = BKE_brush_alpha_get(scene, brush);
809         painter->startjitter = brush->jitter;
810         painter->startspacing = brush->spacing;
811
812         return painter;
813 }
814
815 void BKE_brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
816 {
817         if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
818             ((painter->cache.texonly != texonly) && texonly))
819         {
820                 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
821                 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
822                 painter->cache.ibuf = painter->cache.maskibuf = NULL;
823                 painter->cache.lastsize = -1; /* force ibuf create in refresh */
824         }
825
826         if (painter->cache.flt != flt) {
827                 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
828                 painter->cache.texibuf = NULL;
829                 painter->cache.lastsize = -1; /* force ibuf create in refresh */
830         }
831
832         painter->cache.size = size;
833         painter->cache.flt = flt;
834         painter->cache.texonly = texonly;
835         painter->cache.enabled = 1;
836 }
837
838 void BKE_brush_painter_free(BrushPainter *painter)
839 {
840         Brush *brush = painter->brush;
841
842         BKE_brush_size_set(painter->scene, brush, painter->startsize);
843         brush_alpha_set(painter->scene, brush, painter->startalpha);
844         brush->jitter = painter->startjitter;
845         brush->spacing = painter->startspacing;
846
847         if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
848         if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
849         if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
850         MEM_freeN(painter);
851 }
852
853 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf,
854                                      int x, int y, int w, int h, int xt, int yt,
855                                      const float pos[2])
856 {
857         Scene *scene = painter->scene;
858         Brush *brush = painter->brush;
859         ImBuf *ibuf, *maskibuf, *texibuf;
860         float *bf, *mf, *tf, *otf = NULL, xoff, yoff, xy[2], rgba[4];
861         unsigned char *b, *m, *t, *ot = NULL;
862         int dotexold, origx = x, origy = y;
863         const int radius = BKE_brush_size_get(painter->scene, brush);
864
865         xoff = -radius + 0.5f;
866         yoff = -radius + 0.5f;
867         xoff += (int)pos[0] - (int)painter->startpaintpos[0];
868         yoff += (int)pos[1] - (int)painter->startpaintpos[1];
869
870         ibuf = painter->cache.ibuf;
871         texibuf = painter->cache.texibuf;
872         maskibuf = painter->cache.maskibuf;
873
874         dotexold = (oldtexibuf != NULL);
875
876         /* not sure if it's actually needed or it's a mistake in coords/sizes
877          * calculation in brush_painter_fixed_tex_partial_update(), but without this
878          * limitation memory gets corrupted at fast strokes with quite big spacing (sergey) */
879         w = MIN2(w, ibuf->x);
880         h = MIN2(h, ibuf->y);
881
882         if (painter->cache.flt) {
883                 for (; y < h; y++) {
884                         bf = ibuf->rect_float + (y * ibuf->x + origx) * 4;
885                         tf = texibuf->rect_float + (y * texibuf->x + origx) * 4;
886                         mf = maskibuf->rect_float + (y * maskibuf->x + origx) * 4;
887
888                         if (dotexold)
889                                 otf = oldtexibuf->rect_float + ((y - origy + yt) * oldtexibuf->x + xt) * 4;
890
891                         for (x = origx; x < w; x++, bf += 4, mf += 4, tf += 4) {
892                                 if (dotexold) {
893                                         copy_v3_v3(tf, otf);
894                                         tf[3] = otf[3];
895                                         otf += 4;
896                                 }
897                                 else {
898                                         xy[0] = x + xoff;
899                                         xy[1] = y + yoff;
900
901                                         BKE_brush_sample_tex(scene, brush, xy, tf, 0);
902                                 }
903
904                                 bf[0] = tf[0] * mf[0];
905                                 bf[1] = tf[1] * mf[1];
906                                 bf[2] = tf[2] * mf[2];
907                                 bf[3] = tf[3] * mf[3];
908                         }
909                 }
910         }
911         else {
912                 for (; y < h; y++) {
913                         b = (unsigned char *)ibuf->rect + (y * ibuf->x + origx) * 4;
914                         t = (unsigned char *)texibuf->rect + (y * texibuf->x + origx) * 4;
915                         m = (unsigned char *)maskibuf->rect + (y * maskibuf->x + origx) * 4;
916
917                         if (dotexold)
918                                 ot = (unsigned char *)oldtexibuf->rect + ((y - origy + yt) * oldtexibuf->x + xt) * 4;
919
920                         for (x = origx; x < w; x++, b += 4, m += 4, t += 4) {
921                                 if (dotexold) {
922                                         t[0] = ot[0];
923                                         t[1] = ot[1];
924                                         t[2] = ot[2];
925                                         t[3] = ot[3];
926                                         ot += 4;
927                                 }
928                                 else {
929                                         xy[0] = x + xoff;
930                                         xy[1] = y + yoff;
931
932                                         BKE_brush_sample_tex(scene, brush, xy, rgba, 0);
933                                         rgba_float_to_uchar(t, rgba);
934                                 }
935
936                                 b[0] = t[0] * m[0] / 255;
937                                 b[1] = t[1] * m[1] / 255;
938                                 b[2] = t[2] * m[2] / 255;
939                                 b[3] = t[3] * m[3] / 255;
940                         }
941                 }
942         }
943 }
944
945 static void brush_painter_fixed_tex_partial_update(BrushPainter *painter, const float pos[2])
946 {
947         const Scene *scene = painter->scene;
948         Brush *brush = painter->brush;
949         BrushPainterCache *cache = &painter->cache;
950         ImBuf *oldtexibuf, *ibuf;
951         int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
952         const int diameter = 2 * BKE_brush_size_get(scene, brush);
953
954         imbflag = (cache->flt) ? IB_rectfloat : IB_rect;
955         if (!cache->ibuf)
956                 cache->ibuf = IMB_allocImBuf(diameter, diameter, 32, imbflag);
957         ibuf = cache->ibuf;
958
959         oldtexibuf = cache->texibuf;
960         cache->texibuf = IMB_allocImBuf(diameter, diameter, 32, imbflag);
961
962         if (oldtexibuf) {
963                 srcx = srcy = 0;
964                 destx = (int)painter->lastpaintpos[0] - (int)pos[0];
965                 desty = (int)painter->lastpaintpos[1] - (int)pos[1];
966                 w = oldtexibuf->x;
967                 h = oldtexibuf->y;
968
969                 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
970         }
971         else {
972                 srcx = srcy = 0;
973                 destx = desty = 0;
974                 w = h = 0;
975         }
976         
977         x1 = destx;
978         y1 = desty;
979         x2 = destx + w;
980         y2 = desty + h;
981
982         /* blend existing texture in new position */
983         if ((x1 < x2) && (y1 < y2))
984                 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
985
986         if (oldtexibuf)
987                 IMB_freeImBuf(oldtexibuf);
988
989         /* sample texture in new areas */
990         if ((0 < x1) && (0 < ibuf->y))
991                 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
992         if ((x2 < ibuf->x) && (0 < ibuf->y))
993                 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
994         if ((x1 < x2) && (0 < y1))
995                 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
996         if ((x1 < x2) && (y2 < ibuf->y))
997                 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
998 }
999
1000 static void brush_painter_refresh_cache(BrushPainter *painter, const float pos[2], int use_color_correction)
1001 {
1002         const Scene *scene = painter->scene;
1003         Brush *brush = painter->brush;
1004         BrushPainterCache *cache = &painter->cache;
1005         MTex *mtex = &brush->mtex;
1006         int size;
1007         short flt;
1008         const int diameter = 2 * BKE_brush_size_get(scene, brush);
1009         const float alpha = BKE_brush_alpha_get(scene, brush);
1010
1011         if (diameter != cache->lastsize ||
1012             alpha != cache->lastalpha ||
1013             brush->jitter != cache->lastjitter)
1014         {
1015                 if (cache->ibuf) {
1016                         IMB_freeImBuf(cache->ibuf);
1017                         cache->ibuf = NULL;
1018                 }
1019                 if (cache->maskibuf) {
1020                         IMB_freeImBuf(cache->maskibuf);
1021                         cache->maskibuf = NULL;
1022                 }
1023
1024                 flt = cache->flt;
1025                 size = (cache->size) ? cache->size : diameter;
1026
1027                 if (brush->flag & BRUSH_FIXED_TEX) {
1028                         BKE_brush_imbuf_new(scene, brush, flt, 3, size, &cache->maskibuf, use_color_correction);
1029                         brush_painter_fixed_tex_partial_update(painter, pos);
1030                 }
1031                 else
1032                         BKE_brush_imbuf_new(scene, brush, flt, 2, size, &cache->ibuf, use_color_correction);
1033
1034                 cache->lastsize = diameter;
1035                 cache->lastalpha = alpha;
1036                 cache->lastjitter = brush->jitter;
1037         }
1038         else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
1039                 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
1040                 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
1041
1042                 if ((dx != 0) || (dy != 0))
1043                         brush_painter_fixed_tex_partial_update(painter, pos);
1044         }
1045 }
1046
1047 void BKE_brush_painter_break_stroke(BrushPainter *painter)
1048 {
1049         painter->firsttouch = 1;
1050 }
1051
1052 static void brush_pressure_apply(BrushPainter *painter, Brush *brush, float pressure)
1053 {
1054         if (BKE_brush_use_alpha_pressure(painter->scene, brush))
1055                 brush_alpha_set(painter->scene, brush, maxf(0.0f, painter->startalpha * pressure));
1056         if (BKE_brush_use_size_pressure(painter->scene, brush))
1057                 BKE_brush_size_set(painter->scene, brush, maxf(1.0f, painter->startsize * pressure));
1058         if (brush->flag & BRUSH_JITTER_PRESSURE)
1059                 brush->jitter = maxf(0.0f, painter->startjitter * pressure);
1060         if (brush->flag & BRUSH_SPACING_PRESSURE)
1061                 brush->spacing = maxf(1.0f, painter->startspacing * (1.5f - pressure));
1062 }
1063
1064 void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
1065 {
1066         int use_jitter = brush->jitter != 0;
1067
1068         /* jitter-ed brush gives weird and unpredictable result for this
1069          * kinds of stroke, so manually disable jitter usage (sergey) */
1070         use_jitter &= (brush->flag & (BRUSH_RESTORE_MESH | BRUSH_ANCHORED)) == 0;
1071
1072         if (use_jitter) {
1073                 float rand_pos[2];
1074                 const int radius = BKE_brush_size_get(scene, brush);
1075                 const int diameter = 2 * radius;
1076
1077                 /* find random position within a circle of diameter 1 */
1078                 do {
1079                         rand_pos[0] = BLI_frand() - 0.5f;
1080                         rand_pos[1] = BLI_frand() - 0.5f;
1081                 } while (len_v2(rand_pos) > 0.5f);
1082
1083                 jitterpos[0] = pos[0] + 2 * rand_pos[0] * diameter * brush->jitter;
1084                 jitterpos[1] = pos[1] + 2 * rand_pos[1] * diameter * brush->jitter;
1085         }
1086         else {
1087                 copy_v2_v2(jitterpos, pos);
1088         }
1089 }
1090
1091 int BKE_brush_painter_paint(BrushPainter *painter, BrushFunc func, const float pos[2], double time, float pressure,
1092                             void *user, int use_color_correction)
1093 {
1094         Scene *scene = painter->scene;
1095         Brush *brush = painter->brush;
1096         int totpaintops = 0;
1097
1098         if (pressure == 0.0f) {
1099                 if (painter->lastpressure) // XXX - hack, operator misses
1100                         pressure = painter->lastpressure;
1101                 else
1102                         pressure = 1.0f;    /* zero pressure == not using tablet */
1103         }
1104         if (painter->firsttouch) {
1105                 /* paint exactly once on first touch */
1106                 painter->startpaintpos[0] = pos[0];
1107                 painter->startpaintpos[1] = pos[1];
1108
1109                 brush_pressure_apply(painter, brush, pressure);
1110                 if (painter->cache.enabled)
1111                         brush_painter_refresh_cache(painter, pos, use_color_correction);
1112                 totpaintops += func(user, painter->cache.ibuf, pos, pos);
1113                 
1114                 painter->lasttime = time;
1115                 painter->firsttouch = 0;
1116                 painter->lastpaintpos[0] = pos[0];
1117                 painter->lastpaintpos[1] = pos[1];
1118         }
1119 #if 0
1120         else if (painter->brush->flag & BRUSH_AIRBRUSH) {
1121                 float spacing, step, paintpos[2], dmousepos[2], len;
1122                 double starttime, curtime = time;
1123
1124                 /* compute brush spacing adapted to brush size */
1125                 spacing = brush->rate; //radius*brush->spacing*0.01f;
1126
1127                 /* setup starting time, direction vector and accumulated time */
1128                 starttime = painter->accumtime;
1129                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
1130                 len = normalize_v2(dmousepos);
1131                 painter->accumtime += curtime - painter->lasttime;
1132
1133                 /* do paint op over unpainted time distance */
1134                 while (painter->accumtime >= spacing) {
1135                         step = (spacing - starttime) * len;
1136                         paintpos[0] = painter->lastmousepos[0] + dmousepos[0] * step;
1137                         paintpos[1] = painter->lastmousepos[1] + dmousepos[1] * step;
1138
1139                         if (painter->cache.enabled)
1140                                 brush_painter_refresh_cache(painter);
1141                         totpaintops += func(user, painter->cache.ibuf,
1142                                             painter->lastpaintpos, paintpos);
1143
1144                         painter->lastpaintpos[0] = paintpos[0];
1145                         painter->lastpaintpos[1] = paintpos[1];
1146                         painter->accumtime -= spacing;
1147                         starttime -= spacing;
1148                 }
1149                 
1150                 painter->lasttime = curtime;
1151         }
1152 #endif
1153         else {
1154                 float startdistance, spacing, step, paintpos[2], dmousepos[2], finalpos[2];
1155                 float t, len, press;
1156                 const int radius = BKE_brush_size_get(scene, brush);
1157
1158                 /* compute brush spacing adapted to brush radius, spacing may depend
1159                  * on pressure, so update it */
1160                 brush_pressure_apply(painter, brush, painter->lastpressure);
1161                 spacing = maxf(1.0f, radius) * brush->spacing * 0.01f;
1162
1163                 /* setup starting distance, direction vector and accumulated distance */
1164                 startdistance = painter->accumdistance;
1165                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
1166                 len = normalize_v2(dmousepos);
1167                 painter->accumdistance += len;
1168
1169                 if (brush->flag & BRUSH_SPACE) {
1170                         /* do paint op over unpainted distance */
1171                         while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
1172                                 step = spacing - startdistance;
1173                                 paintpos[0] = painter->lastmousepos[0] + dmousepos[0] * step;
1174                                 paintpos[1] = painter->lastmousepos[1] + dmousepos[1] * step;
1175
1176                                 t = step / len;
1177                                 press = (1.0f - t) * painter->lastpressure + t * pressure;
1178                                 brush_pressure_apply(painter, brush, press);
1179                                 spacing = maxf(1.0f, radius) * brush->spacing * 0.01f;
1180
1181                                 BKE_brush_jitter_pos(scene, brush, paintpos, finalpos);
1182
1183                                 if (painter->cache.enabled)
1184                                         brush_painter_refresh_cache(painter, finalpos, use_color_correction);
1185
1186                                 totpaintops +=
1187                                     func(user, painter->cache.ibuf, painter->lastpaintpos, finalpos);
1188
1189                                 painter->lastpaintpos[0] = paintpos[0];
1190                                 painter->lastpaintpos[1] = paintpos[1];
1191                                 painter->accumdistance -= spacing;
1192                                 startdistance -= spacing;
1193                         }
1194                 }
1195                 else {
1196                         BKE_brush_jitter_pos(scene, brush, pos, finalpos);
1197
1198                         if (painter->cache.enabled)
1199                                 brush_painter_refresh_cache(painter, finalpos, use_color_correction);
1200
1201                         totpaintops += func(user, painter->cache.ibuf, pos, finalpos);
1202
1203                         painter->lastpaintpos[0] = pos[0];
1204                         painter->lastpaintpos[1] = pos[1];
1205                         painter->accumdistance = 0;
1206                 }
1207
1208                 /* do airbrush paint ops, based on the number of paint ops left over
1209                  * from regular painting. this is a temporary solution until we have
1210                  * accurate time stamps for mouse move events */
1211                 if (brush->flag & BRUSH_AIRBRUSH) {
1212                         double curtime = time;
1213                         double painttime = brush->rate * totpaintops;
1214
1215                         painter->accumtime += curtime - painter->lasttime;
1216                         if (painter->accumtime <= painttime)
1217                                 painter->accumtime = 0.0;
1218                         else
1219                                 painter->accumtime -= painttime;
1220
1221                         while (painter->accumtime >= (double)brush->rate) {
1222                                 brush_pressure_apply(painter, brush, pressure);
1223
1224                                 BKE_brush_jitter_pos(scene, brush, pos, finalpos);
1225
1226                                 if (painter->cache.enabled)
1227                                         brush_painter_refresh_cache(painter, finalpos, use_color_correction);
1228
1229                                 totpaintops +=
1230                                     func(user, painter->cache.ibuf, painter->lastmousepos, finalpos);
1231                                 painter->accumtime -= (double)brush->rate;
1232                         }
1233
1234                         painter->lasttime = curtime;
1235                 }
1236         }
1237
1238         painter->lastmousepos[0] = pos[0];
1239         painter->lastmousepos[1] = pos[1];
1240         painter->lastpressure = pressure;
1241
1242         brush_alpha_set(scene, brush, painter->startalpha);
1243         BKE_brush_size_set(scene, brush, painter->startsize);
1244         brush->jitter = painter->startjitter;
1245         brush->spacing = painter->startspacing;
1246
1247         return totpaintops;
1248 }
1249
1250 /* Uses the brush curve control to find a strength value between 0 and 1 */
1251 float BKE_brush_curve_strength_clamp(Brush *br, float p, const float len)
1252 {
1253         if (p >= len) return 0;
1254         else p = p / len;
1255
1256         curvemapping_initialize(br->curve);
1257         p = curvemapping_evaluateF(br->curve, 0, p);
1258
1259         if (p < 0.0f) p = 0.0f;
1260         else if (p > 1.0f) p = 1.0f;
1261         return p;
1262 }
1263 /* same as above but can return negative values if the curve enables
1264  * used for sculpt only */
1265 float BKE_brush_curve_strength(Brush *br, float p, const float len)
1266 {
1267         if (p >= len)
1268                 p = 1.0f;
1269         else
1270                 p = p / len;
1271
1272         curvemapping_initialize(br->curve);
1273         return curvemapping_evaluateF(br->curve, 0, p);
1274 }
1275
1276 /* TODO: should probably be unified with BrushPainter stuff? */
1277 unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side)
1278 {
1279         unsigned int *texcache = NULL;
1280         MTex *mtex = &br->mtex;
1281         TexResult texres = {0};
1282         int hasrgb, ix, iy;
1283         int side = half_side * 2;
1284         
1285         if (mtex->tex) {
1286                 float x, y, step = 2.0 / side, co[3];
1287
1288                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
1289
1290                 BKE_image_get_ibuf(mtex->tex->ima, NULL);
1291                 
1292                 /*do normalized cannonical view coords for texture*/
1293                 for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
1294                         for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
1295                                 co[0] = x;
1296                                 co[1] = y;
1297                                 co[2] = 0.0f;
1298                                 
1299                                 /* This is copied from displace modifier code */
1300                                 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 0, &texres);
1301                         
1302                                 /* if the texture gave an RGB value, we assume it didn't give a valid
1303                                  * intensity, so calculate one (formula from do_material_tex).
1304                                  * if the texture didn't give an RGB value, copy the intensity across
1305                                  */
1306                                 if (hasrgb & TEX_RGB)
1307                                         texres.tin = rgb_to_grayscale(&texres.tr);
1308
1309                                 ((char *)texcache)[(iy * side + ix) * 4] =
1310                                 ((char *)texcache)[(iy * side + ix) * 4 + 1] =
1311                                 ((char *)texcache)[(iy * side + ix) * 4 + 2] =
1312                                 ((char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(texres.tin * 255.0f);
1313                         }
1314                 }
1315         }
1316
1317         return texcache;
1318 }
1319
1320 /**** Radial Control ****/
1321 struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br)
1322 {
1323         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1324         unsigned int *texcache;
1325         int side = 128;
1326         int half = side / 2;
1327         int i, j;
1328
1329         texcache = BKE_brush_gen_texture_cache(br, half);
1330         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1331         im->x = im->y = side;
1332
1333         for (i = 0; i < side; ++i) {
1334                 for (j = 0; j < side; ++j) {
1335                         float magn = sqrtf(powf(i - half, 2) + powf(j - half, 2));
1336                         im->rect_float[i * side + j] = BKE_brush_curve_strength_clamp(br, magn, half);
1337                 }
1338         }
1339
1340         /* Modulate curve with texture */
1341         if (texcache) {
1342                 for (i = 0; i < side; ++i) {
1343                         for (j = 0; j < side; ++j) {
1344                                 const int col = texcache[i * side + j];
1345                                 im->rect_float[i * side + j] *= (((char *)&col)[0] + ((char *)&col)[1] + ((char *)&col)[2]) / 3.0f / 255.0f;
1346                         }
1347                 }
1348
1349                 MEM_freeN(texcache);
1350         }
1351
1352         return im;
1353 }