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