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