2.5/Paint:
[blender.git] / source / blender / blenkernel / intern / brush.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_brush_types.h"
36 #include "DNA_color_types.h"
37 #include "DNA_image_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_texture_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_windowmanager_types.h"
42
43 #include "RNA_access.h"
44
45 #include "BLI_arithb.h"
46 #include "BLI_blenlib.h"
47
48 #include "BKE_brush.h"
49 #include "BKE_colortools.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_library.h"
53 #include "BKE_main.h"
54 #include "BKE_texture.h"
55 #include "BKE_utildefines.h"
56
57 #include "IMB_imbuf.h"
58 #include "IMB_imbuf_types.h"
59
60 #include "RE_render_ext.h" /* externtex */
61 #include "RE_shader_ext.h"
62
63 /* Datablock add/copy/free/make_local */
64
65 Brush *add_brush(char *name)
66 {
67         Brush *brush;
68
69         brush= alloc_libblock(&G.main->brush, ID_BR, name);
70
71         brush->rgb[0]= 1.0f;
72         brush->rgb[1]= 1.0f;
73         brush->rgb[2]= 1.0f;
74         brush->alpha= 0.2f;
75         brush->size= 25;
76         brush->spacing= 10.0f;
77         brush->rate= 0.1f;
78         brush->innerradius= 0.5f;
79         brush->clone.alpha= 0.5;
80         brush->sculpt_tool = SCULPT_TOOL_DRAW;
81
82         brush_curve_preset(brush, BRUSH_PRESET_SHARP);
83
84         /* enable fake user by default */
85         brush->id.flag |= LIB_FAKEUSER;
86         brush_toggled_fake_user(brush);
87         
88         return brush;   
89 }
90
91 Brush *copy_brush(Brush *brush)
92 {
93         Brush *brushn;
94         MTex *mtex;
95         int a;
96         
97         brushn= copy_libblock(brush);
98
99         for(a=0; a<MAX_MTEX; a++) {
100                 mtex= brush->mtex[a];
101                 if(mtex) {
102                         brushn->mtex[a]= MEM_dupallocN(mtex);
103                         if(mtex->tex) id_us_plus((ID*)mtex->tex);
104                 }
105         }
106
107         brushn->curve= curvemapping_copy(brush->curve);
108
109         /* enable fake user by default */
110         if (!(brushn->id.flag & LIB_FAKEUSER)) {
111                 brushn->id.flag |= LIB_FAKEUSER;
112                 brush_toggled_fake_user(brushn);
113         }
114         
115         return brushn;
116 }
117
118 /* not brush itself */
119 void free_brush(Brush *brush)
120 {
121         MTex *mtex;
122         int a;
123
124         for(a=0; a<MAX_MTEX; a++) {
125                 mtex= brush->mtex[a];
126                 if(mtex) {
127                         if(mtex->tex) mtex->tex->id.us--;
128                         MEM_freeN(mtex);
129                 }
130         }
131
132         curvemapping_free(brush->curve);
133 }
134
135 void make_local_brush(Brush *brush)
136 {
137         /* - only lib users: do nothing
138                 * - only local users: set flag
139                 * - mixed: make copy
140                 */
141         
142         Brush *brushn;
143         Scene *scene;
144         int local= 0, lib= 0;
145
146         if(brush->id.lib==0) return;
147
148         if(brush->clone.image) {
149                 /* special case: ima always local immediately */
150                 brush->clone.image->id.lib= 0;
151                 brush->clone.image->id.flag= LIB_LOCAL;
152                 new_id(0, (ID *)brush->clone.image, 0);
153         }
154
155         for(scene= G.main->scene.first; scene; scene=scene->id.next)
156                 if(scene->toolsettings->imapaint.brush==brush) {
157                         if(scene->id.lib) lib= 1;
158                         else local= 1;
159                 }
160         
161         if(local && lib==0) {
162                 brush->id.lib= 0;
163                 brush->id.flag= LIB_LOCAL;
164                 new_id(0, (ID *)brush, 0);
165
166                 /* enable fake user by default */
167                 if (!(brush->id.flag & LIB_FAKEUSER)) {
168                         brush->id.flag |= LIB_FAKEUSER;
169                         brush_toggled_fake_user(brush);
170                 }
171         }
172         else if(local && lib) {
173                 brushn= copy_brush(brush);
174                 brushn->id.us= 1; /* only keep fake user */
175                 brushn->id.flag |= LIB_FAKEUSER;
176                 
177                 for(scene= G.main->scene.first; scene; scene=scene->id.next)
178                         if(scene->toolsettings->imapaint.brush==brush)
179                                 if(scene->id.lib==0) {
180                                         scene->toolsettings->imapaint.brush= brushn;
181                                         brushn->id.us++;
182                                         brush->id.us--;
183                                 }
184         }
185 }
186
187 /* Library Operations */
188
189 Brush **current_brush_source(Scene *sce)
190 {
191         Object *ob = sce->basact ? sce->basact->object : NULL;
192
193         if(ob) {
194                 if(ob->mode & OB_MODE_SCULPT)
195                         return &sce->toolsettings->sculpt->brush;
196                 else if(ob->mode & OB_MODE_VERTEX_PAINT)
197                         return &sce->toolsettings->vpaint->brush;
198                 else if(ob->mode & OB_MODE_WEIGHT_PAINT)
199                         return &sce->toolsettings->wpaint->brush;
200         }
201         else if(G.f & G_TEXTUREPAINT)
202                 return &sce->toolsettings->imapaint.brush;
203         return NULL;
204 }
205
206 int brush_set_nr(Brush **current_brush, int nr)
207 {
208         ID *idtest, *id;
209         
210         id= (ID*)(*current_brush);
211         idtest= (ID*)BLI_findlink(&G.main->brush, nr-1);
212         
213         if(idtest==0) { /* new brush */
214                 if(id) idtest= (ID *)copy_brush((Brush *)id);
215                 else idtest= (ID *)add_brush("Brush");
216                 idtest->us--;
217         }
218         if(idtest!=id) {
219                 brush_delete(current_brush);
220                 *current_brush= (Brush *)idtest;
221                 id_us_plus(idtest);
222
223                 return 1;
224         }
225
226         return 0;
227 }
228
229 int brush_delete(Brush **current_brush)
230 {
231         if (*current_brush) {
232                 (*current_brush)->id.us--;
233                 *current_brush= NULL;
234
235                 return 1;
236         }
237
238         return 0;
239 }
240
241 void brush_toggled_fake_user(Brush *brush)
242 {
243         ID *id= (ID*)brush;
244         if(id) {
245                 if(id->flag & LIB_FAKEUSER) {
246                         id_us_plus(id);
247                 } else {
248                         id->us--;
249                 }
250         }
251 }
252
253 void brush_curve_preset(Brush *b, BrushCurvePreset preset)
254 {
255         CurveMap *cm = NULL;
256
257         if(!b->curve)
258                 b->curve = curvemapping_add(1, 0, 0, 1, 1);
259
260         cm = b->curve->cm;
261
262         if(cm->curve)
263                 MEM_freeN(cm->curve);
264
265         if(preset == BRUSH_PRESET_SHARP)
266                 cm->totpoint= 3;
267         if(preset == BRUSH_PRESET_SMOOTH)
268                 cm->totpoint= 6;
269         if(preset == BRUSH_PRESET_MAX)
270                 cm->totpoint= 2;
271
272
273         cm->curve= MEM_callocN(cm->totpoint*sizeof(CurveMapPoint), "curve points");
274         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
275
276         if(preset == BRUSH_PRESET_SHARP) {
277                 cm->curve[0].x= 0;
278                 cm->curve[0].y= 1;
279                 cm->curve[1].x= 0.33;
280                 cm->curve[1].y= 0.33;
281                 cm->curve[2].x= 1;
282                 cm->curve[2].y= 0;
283         }
284         else if(preset == BRUSH_PRESET_SMOOTH) {
285                 cm->curve[0].x= 0;
286                 cm->curve[0].y= 1;
287                 cm->curve[1].x= 0.1;
288                 cm->curve[1].y= 0.97553;
289                 cm->curve[2].x= 0.3;
290                 cm->curve[2].y= 0.79389;
291                 cm->curve[3].x= 0.9;
292                 cm->curve[3].y= 0.02447;
293                 cm->curve[4].x= 0.7;
294                 cm->curve[4].y= 0.20611;
295                 cm->curve[5].x= 1;
296                 cm->curve[5].y= 0;
297         }
298         else if(preset == BRUSH_PRESET_MAX) {
299                 cm->curve[0].x= 0;
300                 cm->curve[0].y= 1;
301                 cm->curve[1].x= 1;
302                 cm->curve[1].y= 1;
303         }
304
305         curvemapping_changed(b->curve, 0);
306 }
307
308 static MTex *brush_active_texture(Brush *brush)
309 {
310         if(brush && brush->texact >= 0)
311                 return brush->mtex[brush->texact];
312         return NULL;
313 }
314
315 int brush_texture_set_nr(Brush *brush, int nr)
316 {
317         ID *idtest, *id=NULL;
318
319         if(brush->mtex[brush->texact])
320                 id= (ID *)brush->mtex[brush->texact]->tex;
321
322         idtest= (ID*)BLI_findlink(&G.main->tex, nr-1);
323         if(idtest==0) { /* new tex */
324                 if(id) idtest= (ID *)copy_texture((Tex *)id);
325                 else idtest= (ID *)add_texture("Tex");
326                 idtest->us--;
327         }
328         if(idtest!=id) {
329                 brush_texture_delete(brush);
330
331                 if(brush->mtex[brush->texact]==NULL) {
332                         brush->mtex[brush->texact]= add_mtex();
333                         brush->mtex[brush->texact]->r = 1.0f;
334                         brush->mtex[brush->texact]->g = 1.0f;
335                         brush->mtex[brush->texact]->b = 1.0f;
336                 }
337                 brush->mtex[brush->texact]->tex= (Tex*)idtest;
338                 id_us_plus(idtest);
339
340                 return 1;
341         }
342
343         return 0;
344 }
345
346 int brush_texture_delete(Brush *brush)
347 {
348         if(brush->mtex[brush->texact]) {
349                 if(brush->mtex[brush->texact]->tex)
350                         brush->mtex[brush->texact]->tex->id.us--;
351                 MEM_freeN(brush->mtex[brush->texact]);
352                 brush->mtex[brush->texact]= NULL;
353
354                 return 1;
355         }
356
357         return 0;
358 }
359
360 int brush_clone_image_set_nr(Brush *brush, int nr)
361 {
362         if(brush && nr > 0) {
363                 Image *ima= (Image*)BLI_findlink(&G.main->image, nr-1);
364
365                 if(ima) {
366                         brush_clone_image_delete(brush);
367                         brush->clone.image= ima;
368                         id_us_plus(&ima->id);
369                         brush->clone.offset[0]= brush->clone.offset[1]= 0.0f;
370
371                         return 1;
372                 }
373         }
374
375         return 0;
376 }
377
378 int brush_clone_image_delete(Brush *brush)
379 {
380         if (brush && brush->clone.image) {
381                 brush->clone.image->id.us--;
382                 brush->clone.image= NULL;
383                 return 1;
384         }
385
386         return 0;
387 }
388
389 void brush_check_exists(Brush **brush)
390 {
391         if(*brush==NULL)
392                 brush_set_nr(brush, 1);
393 }
394
395 /* Brush Sampling */
396
397 /*static float taylor_approx_cos(float f)
398 {
399         f = f*f;
400         f = 1.0f - f/2.0f + f*f/24.0f;
401         return f;
402 }*/
403
404 float brush_sample_falloff(Brush *brush, float dist)
405 {
406         float a, outer, inner;
407
408         outer = brush->size >> 1;
409         inner = outer*brush->innerradius;
410
411         if (dist <= inner) {
412                 return brush->alpha;
413         }
414         else if ((dist < outer) && (inner < outer)) {
415                 a = sqrt((dist - inner)/(outer - inner));
416                 return (1 - a)*brush->alpha;
417
418                 /* formula used by sculpt, with taylor approx 
419                 a = 0.5f*(taylor_approx_cos(3.0f*(dist - inner)/(outer - inner)) + 1.0f);
420                 return a*brush->alpha; */
421         }
422         else 
423                 return 0.0f;
424 }
425
426 float brush_sample_falloff_noalpha(Brush *brush, float dist)
427 {
428         float outer, inner;
429
430         outer = brush->size >> 1;
431         inner = outer*brush->innerradius;
432
433         if (dist <= inner) {
434                 return 1.0f;
435         }
436         else if ((dist < outer) && (inner < outer)) {
437                 return 1.0f - sqrt((dist - inner)/(outer - inner));
438         }
439         else 
440                 return 0.0f;
441 }
442
443 void brush_sample_tex(Brush *brush, float *xy, float *rgba)
444 {
445         MTex *mtex= brush->mtex[brush->texact];
446
447         if (mtex && mtex->tex) {
448                 float co[3], tin, tr, tg, tb, ta;
449                 int hasrgb;
450                 
451                 co[0]= xy[0]/(brush->size >> 1);
452                 co[1]= xy[1]/(brush->size >> 1);
453                 co[2]= 0.0f;
454
455                 hasrgb= externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
456
457                 if (hasrgb) {
458                         rgba[0]= tr;
459                         rgba[1]= tg;
460                         rgba[2]= tb;
461                         rgba[3]= ta;
462                 }
463                 else {
464                         rgba[0]= tin;
465                         rgba[1]= tin;
466                         rgba[2]= tin;
467                         rgba[3]= 1.0f;
468                 }
469         }
470         else if (rgba)
471                 rgba[0]= rgba[1]= rgba[2]= rgba[3]= 1.0f;
472 }
473
474
475 void brush_imbuf_new(Brush *brush, short flt, short texfall, int size, ImBuf **outbuf)
476 {
477         ImBuf *ibuf;
478         float xy[2], dist, rgba[4], *dstf;
479         int x, y, rowbytes, xoff, yoff, imbflag;
480         char *dst, crgb[3];
481
482         imbflag= (flt)? IB_rectfloat: IB_rect;
483         xoff = -size/2.0f + 0.5f;
484         yoff = -size/2.0f + 0.5f;
485         rowbytes= size*4;
486
487         if (*outbuf)
488                 ibuf= *outbuf;
489         else
490                 ibuf= IMB_allocImBuf(size, size, 32, imbflag, 0);
491
492         if (flt) {
493                 for (y=0; y < ibuf->y; y++) {
494                         dstf = ibuf->rect_float + y*rowbytes;
495
496                         for (x=0; x < ibuf->x; x++, dstf+=4) {
497                                 xy[0] = x + xoff;
498                                 xy[1] = y + yoff;
499
500                                 if (texfall == 0) {
501                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
502
503                                         VECCOPY(dstf, brush->rgb);
504                                         dstf[3]= brush_sample_falloff(brush, dist);
505                                 }
506                                 else if (texfall == 1) {
507                                         brush_sample_tex(brush, xy, dstf);
508                                 }
509                                 else {
510                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
511
512                                         brush_sample_tex(brush, xy, rgba);
513
514                                         dstf[0] = rgba[0]*brush->rgb[0];
515                                         dstf[1] = rgba[1]*brush->rgb[1];
516                                         dstf[2] = rgba[2]*brush->rgb[2];
517                                         dstf[3] = rgba[3]*brush_sample_falloff(brush, dist);
518                                 }
519                         }
520                 }
521         }
522         else {
523                 crgb[0]= FTOCHAR(brush->rgb[0]);
524                 crgb[1]= FTOCHAR(brush->rgb[1]);
525                 crgb[2]= FTOCHAR(brush->rgb[2]);
526
527                 for (y=0; y < ibuf->y; y++) {
528                         dst = (char*)ibuf->rect + y*rowbytes;
529
530                         for (x=0; x < ibuf->x; x++, dst+=4) {
531                                 xy[0] = x + xoff;
532                                 xy[1] = y + yoff;
533
534                                 if (texfall == 0) {
535                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
536
537                                         dst[0]= crgb[0];
538                                         dst[1]= crgb[1];
539                                         dst[2]= crgb[2];
540                                         dst[3]= FTOCHAR(brush_sample_falloff(brush, dist));
541                                 }
542                                 else if (texfall == 1) {
543                                         brush_sample_tex(brush, xy, rgba);
544                                         dst[0]= FTOCHAR(rgba[0]);
545                                         dst[1]= FTOCHAR(rgba[1]);
546                                         dst[2]= FTOCHAR(rgba[2]);
547                                         dst[3]= FTOCHAR(rgba[3]);
548                                 }
549                                 else {
550                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
551
552                                         brush_sample_tex(brush, xy, rgba);
553                                         dst[0] = FTOCHAR(rgba[0]*brush->rgb[0]);
554                                         dst[1] = FTOCHAR(rgba[1]*brush->rgb[1]);
555                                         dst[2] = FTOCHAR(rgba[2]*brush->rgb[2]);
556                                         dst[3] = FTOCHAR(rgba[3]*brush_sample_falloff(brush, dist));
557                                 }
558                         }
559                 }
560         }
561
562         *outbuf= ibuf;
563 }
564
565 /* Brush Painting */
566
567 typedef struct BrushPainterCache {
568         short enabled;
569
570         int size;                       /* size override, if 0 uses brush->size */
571         short flt;                      /* need float imbuf? */
572         short texonly;          /* no alpha, color or fallof, only texture in imbuf */
573
574         int lastsize;
575         float lastalpha;
576         float lastinnerradius;
577
578         ImBuf *ibuf;
579         ImBuf *texibuf;
580         ImBuf *maskibuf;
581 } BrushPainterCache;
582
583 struct BrushPainter {
584         Brush *brush;
585
586         float lastmousepos[2];  /* mouse position of last paint call */
587
588         float accumdistance;    /* accumulated distance of brush since last paint op */
589         float lastpaintpos[2];  /* position of last paint op */
590         float startpaintpos[2]; /* position of first paint */
591
592         double accumtime;               /* accumulated time since last paint op (airbrush) */
593         double lasttime;                /* time of last update */
594
595         float lastpressure;
596
597         short firsttouch;               /* first paint op */
598
599         float startsize;
600         float startalpha;
601         float startinnerradius;
602         float startspacing;
603
604         BrushPainterCache cache;
605 };
606
607 BrushPainter *brush_painter_new(Brush *brush)
608 {
609         BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
610
611         painter->brush= brush;
612         painter->firsttouch= 1;
613         painter->cache.lastsize= -1; /* force ibuf create in refresh */
614
615         painter->startsize = brush->size;
616         painter->startalpha = brush->alpha;
617         painter->startinnerradius = brush->innerradius;
618         painter->startspacing = brush->spacing;
619
620         return painter;
621 }
622
623 void brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
624 {
625         if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
626                 ((painter->cache.texonly != texonly) && texonly)) {
627                 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
628                 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
629                 painter->cache.ibuf= painter->cache.maskibuf= NULL;
630                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
631         }
632
633         if (painter->cache.flt != flt) {
634                 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
635                 painter->cache.texibuf= NULL;
636                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
637         }
638
639         painter->cache.size= size;
640         painter->cache.flt= flt;
641         painter->cache.texonly= texonly;
642         painter->cache.enabled= 1;
643 }
644
645 void brush_painter_free(BrushPainter *painter)
646 {
647         Brush *brush = painter->brush;
648
649         brush->size = painter->startsize;
650         brush->alpha = painter->startalpha;
651         brush->innerradius = painter->startinnerradius;
652         brush->spacing = painter->startspacing;
653
654         if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
655         if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
656         if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
657         MEM_freeN(painter);
658 }
659
660 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, int x, int y, int w, int h, int xt, int yt, float *pos)
661 {
662         Brush *brush= painter->brush;
663         ImBuf *ibuf, *maskibuf, *texibuf;
664         float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
665         char *b, *m, *t, *ot= NULL;
666         int dotexold, origx= x, origy= y;
667
668         xoff = -brush->size/2.0f + 0.5f;
669         yoff = -brush->size/2.0f + 0.5f;
670         xoff += (int)pos[0] - (int)painter->startpaintpos[0];
671         yoff += (int)pos[1] - (int)painter->startpaintpos[1];
672
673         ibuf = painter->cache.ibuf;
674         texibuf = painter->cache.texibuf;
675         maskibuf = painter->cache.maskibuf;
676
677         dotexold = (oldtexibuf != NULL);
678
679         if (painter->cache.flt) {
680                 for (; y < h; y++) {
681                         bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
682                         tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
683                         mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
684
685                         if (dotexold)
686                                 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
687
688                         for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
689                                 if (dotexold) {
690                                         VECCOPY(tf, otf);
691                                         tf[3] = otf[3];
692                                         otf += 4;
693                                 }
694                                 else {
695                                         xy[0] = x + xoff;
696                                         xy[1] = y + yoff;
697
698                                         brush_sample_tex(brush, xy, tf);
699                                 }
700
701                                 bf[0] = tf[0]*mf[0];
702                                 bf[1] = tf[1]*mf[1];
703                                 bf[2] = tf[2]*mf[2];
704                                 bf[3] = tf[3]*mf[3];
705                         }
706                 }
707         }
708         else {
709                 for (; y < h; y++) {
710                         b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
711                         t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
712                         m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
713
714                         if (dotexold)
715                                 ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
716
717                         for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
718                                 if (dotexold) {
719                                         t[0] = ot[0];
720                                         t[1] = ot[1];
721                                         t[2] = ot[2];
722                                         t[3] = ot[3];
723                                         ot += 4;
724                                 }
725                                 else {
726                                         xy[0] = x + xoff;
727                                         xy[1] = y + yoff;
728
729                                         brush_sample_tex(brush, xy, rgba);
730                                         t[0]= FTOCHAR(rgba[0]);
731                                         t[1]= FTOCHAR(rgba[1]);
732                                         t[2]= FTOCHAR(rgba[2]);
733                                         t[3]= FTOCHAR(rgba[3]);
734                                 }
735
736                                 b[0] = t[0]*m[0]/255;
737                                 b[1] = t[1]*m[1]/255;
738                                 b[2] = t[2]*m[2]/255;
739                                 b[3] = t[3]*m[3]/255;
740                         }
741                 }
742         }
743 }
744
745 void brush_painter_fixed_tex_partial_update(BrushPainter *painter, float *pos)
746 {
747         Brush *brush= painter->brush;
748         BrushPainterCache *cache= &painter->cache;
749         ImBuf *oldtexibuf, *ibuf;
750         int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
751
752         imbflag= (cache->flt)? IB_rectfloat: IB_rect;
753         if (!cache->ibuf)
754                 cache->ibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
755         ibuf= cache->ibuf;
756
757         oldtexibuf= cache->texibuf;
758         cache->texibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
759
760         if (oldtexibuf) {
761                 srcx= srcy= 0;
762                 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
763                 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
764                 w= oldtexibuf->x;
765                 h= oldtexibuf->y;
766
767                 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
768         }
769         else {
770                 srcx= srcy= 0;
771                 destx= desty= 0;
772                 w= h= 0;
773         }
774         
775         x1= destx;
776         y1= desty;
777         x2= destx+w;
778         y2= desty+h;
779
780         /* blend existing texture in new position */
781         if ((x1 < x2) && (y1 < y2))
782                 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
783
784         if (oldtexibuf)
785                 IMB_freeImBuf(oldtexibuf);
786
787         /* sample texture in new areas */
788         if ((0 < x1) && (0 < ibuf->y))
789                 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
790         if ((x2 < ibuf->x) && (0 < ibuf->y))
791                 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
792         if ((x1 < x2) && (0 < y1))
793                 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
794         if ((x1 < x2) && (y2 < ibuf->y))
795                 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
796 }
797
798 static void brush_painter_refresh_cache(BrushPainter *painter, float *pos)
799 {
800         Brush *brush= painter->brush;
801         BrushPainterCache *cache= &painter->cache;
802         MTex *mtex= brush->mtex[brush->texact];
803         int size;
804         short flt;
805
806         if ((brush->size != cache->lastsize) || (brush->alpha != cache->lastalpha)
807             || (brush->innerradius != cache->lastinnerradius)) {
808                 if (cache->ibuf) {
809                         IMB_freeImBuf(cache->ibuf);
810                         cache->ibuf= NULL;
811                 }
812                 if (cache->maskibuf) {
813                         IMB_freeImBuf(cache->maskibuf);
814                         cache->maskibuf= NULL;
815                 }
816
817                 flt= cache->flt;
818                 size= (cache->size)? cache->size: brush->size;
819
820                 if (!(mtex && mtex->tex) || (mtex->tex->type==0)) {
821                         brush_imbuf_new(brush, flt, 0, size, &cache->ibuf);
822                 }
823                 else if (brush->flag & BRUSH_FIXED_TEX) {
824                         brush_imbuf_new(brush, flt, 0, size, &cache->maskibuf);
825                         brush_painter_fixed_tex_partial_update(painter, pos);
826                 }
827                 else
828                         brush_imbuf_new(brush, flt, 2, size, &cache->ibuf);
829
830                 cache->lastsize= brush->size;
831                 cache->lastalpha= brush->alpha;
832                 cache->lastinnerradius= brush->innerradius;
833         }
834         else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
835                 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
836                 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
837
838                 if ((dx != 0) || (dy != 0))
839                         brush_painter_fixed_tex_partial_update(painter, pos);
840         }
841 }
842
843 void brush_painter_break_stroke(BrushPainter *painter)
844 {
845         painter->firsttouch= 1;
846 }
847
848 static void brush_apply_pressure(BrushPainter *painter, Brush *brush, float pressure)
849 {
850         if (brush->flag & BRUSH_ALPHA_PRESSURE) 
851                 brush->alpha = MAX2(0.0, painter->startalpha*pressure);
852         if (brush->flag & BRUSH_SIZE_PRESSURE)
853                 brush->size = MAX2(1.0, painter->startsize*pressure);
854         if (brush->flag & BRUSH_RAD_PRESSURE)
855                 brush->innerradius = MAX2(0.0, painter->startinnerradius*pressure);
856         if (brush->flag & BRUSH_SPACING_PRESSURE)
857                 brush->spacing = MAX2(1.0, painter->startspacing*(1.5f-pressure));
858 }
859
860 int brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user)
861 {
862         Brush *brush= painter->brush;
863         int totpaintops= 0;
864
865         if (pressure == 0.0f)
866                 pressure = 1.0f;        /* zero pressure == not using tablet */
867
868         if (painter->firsttouch) {
869                 /* paint exactly once on first touch */
870                 painter->startpaintpos[0]= pos[0];
871                 painter->startpaintpos[1]= pos[1];
872
873                 brush_apply_pressure(painter, brush, pressure);
874                 if (painter->cache.enabled)
875                         brush_painter_refresh_cache(painter, pos);
876                 totpaintops += func(user, painter->cache.ibuf, pos, pos);
877                 
878                 painter->lasttime= time;
879                 painter->firsttouch= 0;
880                 painter->lastpaintpos[0]= pos[0];
881                 painter->lastpaintpos[1]= pos[1];
882         }
883 #if 0
884         else if (painter->brush->flag & BRUSH_AIRBRUSH) {
885                 float spacing, step, paintpos[2], dmousepos[2], len;
886                 double starttime, curtime= time;
887
888                 /* compute brush spacing adapted to brush size */
889                 spacing= brush->rate; //brush->size*brush->spacing*0.01f;
890
891                 /* setup starting time, direction vector and accumulated time */
892                 starttime= painter->accumtime;
893                 Vec2Subf(dmousepos, pos, painter->lastmousepos);
894                 len= Normalize2(dmousepos);
895                 painter->accumtime += curtime - painter->lasttime;
896
897                 /* do paint op over unpainted time distance */
898                 while (painter->accumtime >= spacing) {
899                         step= (spacing - starttime)*len;
900                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
901                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
902
903                         if (painter->cache.enabled)
904                                 brush_painter_refresh_cache(painter);
905                         totpaintops += func(user, painter->cache.ibuf,
906                                 painter->lastpaintpos, paintpos);
907
908                         painter->lastpaintpos[0]= paintpos[0];
909                         painter->lastpaintpos[1]= paintpos[1];
910                         painter->accumtime -= spacing;
911                         starttime -= spacing;
912                 }
913                 
914                 painter->lasttime= curtime;
915         }
916 #endif
917         else {
918                 float startdistance, spacing, step, paintpos[2], dmousepos[2];
919                 float t, len, press;
920
921                 /* compute brush spacing adapted to brush size, spacing may depend
922                    on pressure, so update it */
923                 brush_apply_pressure(painter, brush, painter->lastpressure);
924                 spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
925
926                 /* setup starting distance, direction vector and accumulated distance */
927                 startdistance= painter->accumdistance;
928                 Vec2Subf(dmousepos, pos, painter->lastmousepos);
929                 len= Normalize2(dmousepos);
930                 painter->accumdistance += len;
931
932                 /* do paint op over unpainted distance */
933                 while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
934                         step= spacing - startdistance;
935                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
936                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
937
938                         t = step/len;
939                         press= (1.0f-t)*painter->lastpressure + t*pressure;
940                         brush_apply_pressure(painter, brush, press);
941                         spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
942
943                         if (painter->cache.enabled)
944                                 brush_painter_refresh_cache(painter, paintpos);
945
946                         totpaintops +=
947                                 func(user, painter->cache.ibuf, painter->lastpaintpos, paintpos);
948
949                         painter->lastpaintpos[0]= paintpos[0];
950                         painter->lastpaintpos[1]= paintpos[1];
951                         painter->accumdistance -= spacing;
952                         startdistance -= spacing;
953                 }
954
955                 /* do airbrush paint ops, based on the number of paint ops left over
956                    from regular painting. this is a temporary solution until we have
957                    accurate time stamps for mouse move events */
958                 if (brush->flag & BRUSH_AIRBRUSH) {
959                         double curtime= time;
960                         double painttime= brush->rate*totpaintops;
961
962                         painter->accumtime += curtime - painter->lasttime;
963                         if (painter->accumtime <= painttime)
964                                 painter->accumtime= 0.0;
965                         else
966                                 painter->accumtime -= painttime;
967
968                         while (painter->accumtime >= brush->rate) {
969                                 brush_apply_pressure(painter, brush, pressure);
970                                 if (painter->cache.enabled)
971                                         brush_painter_refresh_cache(painter, paintpos);
972                                 totpaintops +=
973                                         func(user, painter->cache.ibuf, painter->lastmousepos, pos);
974                                 painter->accumtime -= brush->rate;
975                         }
976
977                         painter->lasttime= curtime;
978                 }
979         }
980
981         painter->lastmousepos[0]= pos[0];
982         painter->lastmousepos[1]= pos[1];
983         painter->lastpressure= pressure;
984
985         brush->alpha = painter->startalpha;
986         brush->size = painter->startsize;
987         brush->innerradius = painter->startinnerradius;
988         brush->spacing = painter->startspacing;
989
990         return totpaintops;
991 }
992
993 /* Uses the brush curve control to find a strength value between 0 and 1 */
994 float brush_curve_strength(Brush *br, float p, const float len)
995 {
996         if(p > len) p= len;
997         return curvemapping_evaluateF(br->curve, 0, p/len);
998 }
999
1000 /* TODO: should probably be unified with BrushPainter stuff? */
1001 unsigned int *brush_gen_texture_cache(Brush *br, int half_side)
1002 {
1003         unsigned int *texcache = NULL;
1004         MTex *mtex = br->mtex[br->texact];
1005         TexResult texres;
1006         int hasrgb, ix, iy;
1007         int side = half_side * 2;
1008
1009         memset(&texres, 0, sizeof(TexResult));
1010         
1011         if(mtex && mtex->tex) {
1012                 float x, y, step = 2.0 / side, co[3];
1013
1014                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
1015
1016                 BKE_image_get_ibuf(mtex->tex->ima, NULL);
1017                 
1018                 /*do normalized cannonical view coords for texture*/
1019                 for (y=-1.0, iy=0; iy<side; iy++, y += step) {
1020                         for (x=-1.0, ix=0; ix<side; ix++, x += step) {
1021                                 co[0]= x;
1022                                 co[1]= y;
1023                                 co[2]= 0.0f;
1024                                 
1025                                 /* This is copied from displace modifier code */
1026                                 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 1, &texres);
1027                         
1028                                 /* if the texture gave an RGB value, we assume it didn't give a valid
1029                                  * intensity, so calculate one (formula from do_material_tex).
1030                                  * if the texture didn't give an RGB value, copy the intensity across
1031                                  */
1032                                 if(hasrgb & TEX_RGB)
1033                                         texres.tin = (0.35 * texres.tr + 0.45 *
1034                                                       texres.tg + 0.2 * texres.tb);
1035
1036                                 texres.tin = texres.tin * 255.0;
1037                                 ((char*)texcache)[(iy*side+ix)*4] = (char)texres.tin;
1038                                 ((char*)texcache)[(iy*side+ix)*4+1] = (char)texres.tin;
1039                                 ((char*)texcache)[(iy*side+ix)*4+2] = (char)texres.tin;
1040                                 ((char*)texcache)[(iy*side+ix)*4+3] = (char)texres.tin;
1041                         }
1042                 }
1043         }
1044
1045         return texcache;
1046 }
1047
1048 /**** Radial Control ****/
1049 static struct ImBuf *brush_gen_radial_control_imbuf(Brush *br)
1050 {
1051         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1052         unsigned int *texcache;
1053         int side = 128;
1054         int half = side / 2;
1055         int i, j;
1056
1057         texcache = brush_gen_texture_cache(br, half);
1058         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1059         im->x = im->y = side;
1060
1061         for(i=0; i<side; ++i) {
1062                 for(j=0; j<side; ++j) {
1063                         float magn= sqrt(pow(i - half, 2) + pow(j - half, 2));
1064                         im->rect_float[i*side + j]= brush_curve_strength(br, magn, half);
1065                 }
1066         }
1067
1068         /* Modulate curve with texture */
1069         if(texcache) {
1070                 for(i=0; i<side; ++i) {
1071                         for(j=0; j<side; ++j) {
1072                                 const int col= texcache[i*side+j];
1073                                 im->rect_float[i*side+j]*= (((char*)&col)[0]+((char*)&col)[1]+((char*)&col)[2])/3.0f/255.0f;
1074                         }
1075                 }
1076
1077                 MEM_freeN(texcache);
1078         }
1079
1080         return im;
1081 }
1082
1083 void brush_radial_control_invoke(wmOperator *op, Brush *br, float size_weight)
1084 {
1085         int mode = RNA_int_get(op->ptr, "mode");
1086         float original_value= 0;
1087
1088         if(mode == WM_RADIALCONTROL_SIZE)
1089                 original_value = br->size * size_weight;
1090         else if(mode == WM_RADIALCONTROL_STRENGTH)
1091                 original_value = br->alpha;
1092         else if(mode == WM_RADIALCONTROL_ANGLE) {
1093                 MTex *mtex = brush_active_texture(br);
1094                 if(mtex)
1095                         original_value = mtex->rot;
1096         }
1097
1098         RNA_float_set(op->ptr, "initial_value", original_value);
1099         op->customdata = brush_gen_radial_control_imbuf(br);
1100 }
1101
1102 int brush_radial_control_exec(wmOperator *op, Brush *br, float size_weight)
1103 {
1104         int mode = RNA_int_get(op->ptr, "mode");
1105         float new_value = RNA_float_get(op->ptr, "new_value");
1106         const float conv = 0.017453293;
1107
1108         if(mode == WM_RADIALCONTROL_SIZE)
1109                 br->size = new_value * size_weight;
1110         else if(mode == WM_RADIALCONTROL_STRENGTH)
1111                 br->alpha = new_value;
1112         else if(mode == WM_RADIALCONTROL_ANGLE) {
1113                 MTex *mtex = brush_active_texture(br);
1114                 if(mtex)
1115                         mtex->rot = new_value * conv;
1116         }
1117
1118         return OPERATOR_FINISHED;
1119 }