Patch from GSR that a) fixes a whole bunch of GPL/BL license
[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
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_brush_types.h"
35 #include "DNA_image_types.h"
36 #include "DNA_texture_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "BLI_arithb.h"
40 #include "BLI_blenlib.h"
41
42 #include "BKE_brush.h"
43 #include "BKE_global.h"
44 #include "BKE_library.h"
45 #include "BKE_main.h"
46 #include "BKE_texture.h"
47 #include "BKE_utildefines.h"
48
49 #include "IMB_imbuf.h"
50 #include "IMB_imbuf_types.h"
51
52 #include "RE_render_ext.h" /* externtex */
53
54 /* Datablock add/copy/free/make_local */
55
56 Brush *add_brush(char *name)
57 {
58         Brush *brush;
59
60         brush= alloc_libblock(&G.main->brush, ID_BR, name);
61
62         brush->rgb[0]= 1.0f;
63         brush->rgb[1]= 1.0f;
64         brush->rgb[2]= 1.0f;
65         brush->alpha= 0.2f;
66         brush->size= 25;
67         brush->spacing= 10.0f;
68         brush->rate= 0.1f;
69         brush->innerradius= 0.5f;
70         brush->clone.alpha= 0.5;
71
72         /* enable fake user by default */
73         brush->id.flag |= LIB_FAKEUSER;
74         brush_toggled_fake_user(brush);
75         
76         return brush;   
77 }
78
79 Brush *copy_brush(Brush *brush)
80 {
81         Brush *brushn;
82         MTex *mtex;
83         int a;
84         
85         brushn= copy_libblock(brush);
86
87         for(a=0; a<MAX_MTEX; a++) {
88                 mtex= brush->mtex[a];
89                 if(mtex) {
90                         brushn->mtex[a]= MEM_dupallocN(mtex);
91                         if(mtex->tex) id_us_plus((ID*)mtex->tex);
92                 }
93         }
94
95         /* enable fake user by default */
96         if (!(brushn->id.flag & LIB_FAKEUSER)) {
97                 brushn->id.flag |= LIB_FAKEUSER;
98                 brush_toggled_fake_user(brushn);
99         }
100         
101         return brushn;
102 }
103
104 /* not brush itself */
105 void free_brush(Brush *brush)
106 {
107         MTex *mtex;
108         int a;
109
110         for(a=0; a<MAX_MTEX; a++) {
111                 mtex= brush->mtex[a];
112                 if(mtex) {
113                         if(mtex->tex) mtex->tex->id.us--;
114                         MEM_freeN(mtex);
115                 }
116         }
117 }
118
119 void make_local_brush(Brush *brush)
120 {
121         /* - only lib users: do nothing
122                 * - only local users: set flag
123                 * - mixed: make copy
124                 */
125         
126         Brush *brushn;
127         Scene *scene;
128         int local= 0, lib= 0;
129
130         if(brush->id.lib==0) return;
131
132         if(brush->clone.image) {
133                 /* special case: ima always local immediately */
134                 brush->clone.image->id.lib= 0;
135                 brush->clone.image->id.flag= LIB_LOCAL;
136                 new_id(0, (ID *)brush->clone.image, 0);
137         }
138
139         for(scene= G.main->scene.first; scene; scene=scene->id.next)
140                 if(scene->toolsettings->imapaint.brush==brush) {
141                         if(scene->id.lib) lib= 1;
142                         else local= 1;
143                 }
144         
145         if(local && lib==0) {
146                 brush->id.lib= 0;
147                 brush->id.flag= LIB_LOCAL;
148                 new_id(0, (ID *)brush, 0);
149
150                 /* enable fake user by default */
151                 if (!(brush->id.flag & LIB_FAKEUSER)) {
152                         brush->id.flag |= LIB_FAKEUSER;
153                         brush_toggled_fake_user(brush);
154                 }
155         }
156         else if(local && lib) {
157                 brushn= copy_brush(brush);
158                 brushn->id.us= 1; /* only keep fake user */
159                 brushn->id.flag |= LIB_FAKEUSER;
160                 
161                 for(scene= G.main->scene.first; scene; scene=scene->id.next)
162                         if(scene->toolsettings->imapaint.brush==brush)
163                                 if(scene->id.lib==0) {
164                                         scene->toolsettings->imapaint.brush= brushn;
165                                         brushn->id.us++;
166                                         brush->id.us--;
167                                 }
168         }
169 }
170
171 /* Library Operations */
172
173 int brush_set_nr(Brush **current_brush, int nr)
174 {
175         ID *idtest, *id;
176         
177         id= (ID*)(*current_brush);
178         idtest= (ID*)BLI_findlink(&G.main->brush, nr-1);
179         
180         if(idtest==0) { /* new brush */
181                 if(id) idtest= (ID *)copy_brush((Brush *)id);
182                 else idtest= (ID *)add_brush("Brush");
183                 idtest->us--;
184         }
185         if(idtest!=id) {
186                 brush_delete(current_brush);
187                 *current_brush= (Brush *)idtest;
188                 id_us_plus(idtest);
189
190                 return 1;
191         }
192
193         return 0;
194 }
195
196 int brush_delete(Brush **current_brush)
197 {
198         if (*current_brush) {
199                 (*current_brush)->id.us--;
200                 *current_brush= NULL;
201
202                 return 1;
203         }
204
205         return 0;
206 }
207
208 void brush_toggled_fake_user(Brush *brush)
209 {
210         ID *id= (ID*)brush;
211         if(id) {
212                 if(id->flag & LIB_FAKEUSER) {
213                         id_us_plus(id);
214                 } else {
215                         id->us--;
216                 }
217         }
218 }
219
220 int brush_texture_set_nr(Brush *brush, int nr)
221 {
222         ID *idtest, *id=NULL;
223
224         if(brush->mtex[brush->texact])
225                 id= (ID *)brush->mtex[brush->texact]->tex;
226
227         idtest= (ID*)BLI_findlink(&G.main->tex, nr-1);
228         if(idtest==0) { /* new tex */
229                 if(id) idtest= (ID *)copy_texture((Tex *)id);
230                 else idtest= (ID *)add_texture("Tex");
231                 idtest->us--;
232         }
233         if(idtest!=id) {
234                 brush_texture_delete(brush);
235
236                 if(brush->mtex[brush->texact]==NULL) {
237                         brush->mtex[brush->texact]= add_mtex();
238                         brush->mtex[brush->texact]->r = 1.0f;
239                         brush->mtex[brush->texact]->g = 1.0f;
240                         brush->mtex[brush->texact]->b = 1.0f;
241                 }
242                 brush->mtex[brush->texact]->tex= (Tex*)idtest;
243                 id_us_plus(idtest);
244
245                 return 1;
246         }
247
248         return 0;
249 }
250
251 int brush_texture_delete(Brush *brush)
252 {
253         if(brush->mtex[brush->texact]) {
254                 if(brush->mtex[brush->texact]->tex)
255                         brush->mtex[brush->texact]->tex->id.us--;
256                 MEM_freeN(brush->mtex[brush->texact]);
257                 brush->mtex[brush->texact]= NULL;
258
259                 return 1;
260         }
261
262         return 0;
263 }
264
265 int brush_clone_image_set_nr(Brush *brush, int nr)
266 {
267         if(brush && nr > 0) {
268                 Image *ima= (Image*)BLI_findlink(&G.main->image, nr-1);
269
270                 if(ima) {
271                         brush_clone_image_delete(brush);
272                         brush->clone.image= ima;
273                         id_us_plus(&ima->id);
274                         brush->clone.offset[0]= brush->clone.offset[1]= 0.0f;
275
276                         return 1;
277                 }
278         }
279
280         return 0;
281 }
282
283 int brush_clone_image_delete(Brush *brush)
284 {
285         if (brush && brush->clone.image) {
286                 brush->clone.image->id.us--;
287                 brush->clone.image= NULL;
288                 return 1;
289         }
290
291         return 0;
292 }
293
294 void brush_check_exists(Brush **brush)
295 {
296         if(*brush==NULL)
297                 brush_set_nr(brush, 1);
298 }
299
300 /* Brush Sampling */
301
302 /*static float taylor_approx_cos(float f)
303 {
304         f = f*f;
305         f = 1.0f - f/2.0f + f*f/24.0f;
306         return f;
307 }*/
308
309 float brush_sample_falloff(Brush *brush, float dist)
310 {
311         float a, outer, inner;
312
313         outer = brush->size >> 1;
314         inner = outer*brush->innerradius;
315
316         if (dist <= inner) {
317                 return brush->alpha;
318         }
319         else if ((dist < outer) && (inner < outer)) {
320                 a = sqrt((dist - inner)/(outer - inner));
321                 return (1 - a)*brush->alpha;
322
323                 /* formula used by sculpt, with taylor approx 
324                 a = 0.5f*(taylor_approx_cos(3.0f*(dist - inner)/(outer - inner)) + 1.0f);
325                 return a*brush->alpha; */
326         }
327         else 
328                 return 0.0f;
329 }
330
331 void brush_sample_tex(Brush *brush, float *xy, float *rgba)
332 {
333         MTex *mtex= brush->mtex[brush->texact];
334
335         if (mtex && mtex->tex) {
336                 float co[3], tin, tr, tg, tb, ta;
337                 int hasrgb;
338                 
339                 co[0]= xy[0]/(brush->size >> 1);
340                 co[1]= xy[1]/(brush->size >> 1);
341                 co[2]= 0.0f;
342
343                 hasrgb= externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
344
345                 if (hasrgb) {
346                         rgba[0]= tr;
347                         rgba[1]= tg;
348                         rgba[2]= tb;
349                         rgba[3]= ta;
350                 }
351                 else {
352                         rgba[0]= tin;
353                         rgba[1]= tin;
354                         rgba[2]= tin;
355                         rgba[3]= 1.0f;
356                 }
357         }
358         else if (rgba)
359                 rgba[0]= rgba[1]= rgba[2]= rgba[3]= 1.0f;
360 }
361
362 #define FTOCHAR(val) val<=0.0f?0: (val>=1.0f?255: (char)(255.0f*val))
363
364 void brush_imbuf_new(Brush *brush, short flt, short texfall, int size, ImBuf **outbuf)
365 {
366         ImBuf *ibuf;
367         float xy[2], dist, rgba[4], *dstf;
368         int x, y, rowbytes, xoff, yoff, imbflag;
369         char *dst, crgb[3];
370
371         imbflag= (flt)? IB_rectfloat: IB_rect;
372         xoff = -size/2.0f + 0.5f;
373         yoff = -size/2.0f + 0.5f;
374         rowbytes= size*4;
375
376         if (*outbuf)
377                 ibuf= *outbuf;
378         else
379                 ibuf= IMB_allocImBuf(size, size, 32, imbflag, 0);
380
381         if (flt) {
382                 for (y=0; y < ibuf->y; y++) {
383                         dstf = ibuf->rect_float + y*rowbytes;
384
385                         for (x=0; x < ibuf->x; x++, dstf+=4) {
386                                 xy[0] = x + xoff;
387                                 xy[1] = y + yoff;
388
389                                 if (texfall == 0) {
390                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
391
392                                         VECCOPY(dstf, brush->rgb);
393                                         dstf[3]= brush_sample_falloff(brush, dist);
394                                 }
395                                 else if (texfall == 1) {
396                                         brush_sample_tex(brush, xy, dstf);
397                                 }
398                                 else {
399                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
400
401                                         brush_sample_tex(brush, xy, rgba);
402
403                                         dstf[0] = rgba[0]*brush->rgb[0];
404                                         dstf[1] = rgba[1]*brush->rgb[1];
405                                         dstf[2] = rgba[2]*brush->rgb[2];
406                                         dstf[3] = rgba[3]*brush_sample_falloff(brush, dist);
407                                 }
408                         }
409                 }
410         }
411         else {
412                 crgb[0]= FTOCHAR(brush->rgb[0]);
413                 crgb[1]= FTOCHAR(brush->rgb[1]);
414                 crgb[2]= FTOCHAR(brush->rgb[2]);
415
416                 for (y=0; y < ibuf->y; y++) {
417                         dst = (char*)ibuf->rect + y*rowbytes;
418
419                         for (x=0; x < ibuf->x; x++, dst+=4) {
420                                 xy[0] = x + xoff;
421                                 xy[1] = y + yoff;
422
423                                 if (texfall == 0) {
424                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
425
426                                         dst[0]= crgb[0];
427                                         dst[1]= crgb[1];
428                                         dst[2]= crgb[2];
429                                         dst[3]= FTOCHAR(brush_sample_falloff(brush, dist));
430                                 }
431                                 else if (texfall == 1) {
432                                         brush_sample_tex(brush, xy, rgba);
433                                         dst[0]= FTOCHAR(rgba[0]);
434                                         dst[1]= FTOCHAR(rgba[1]);
435                                         dst[2]= FTOCHAR(rgba[2]);
436                                         dst[3]= FTOCHAR(rgba[3]);
437                                 }
438                                 else {
439                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
440
441                                         brush_sample_tex(brush, xy, rgba);
442                                         dst[0] = FTOCHAR(rgba[0]*brush->rgb[0]);
443                                         dst[1] = FTOCHAR(rgba[1]*brush->rgb[1]);
444                                         dst[2] = FTOCHAR(rgba[2]*brush->rgb[2]);
445                                         dst[3] = FTOCHAR(rgba[3]*brush_sample_falloff(brush, dist));
446                                 }
447                         }
448                 }
449         }
450
451         *outbuf= ibuf;
452 }
453
454 /* Brush Painting */
455
456 typedef struct BrushPainterCache {
457         short enabled;
458
459         int size;                       /* size override, if 0 uses brush->size */
460         short flt;                      /* need float imbuf? */
461         short texonly;          /* no alpha, color or fallof, only texture in imbuf */
462
463         int lastsize;
464         float lastalpha;
465         float lastinnerradius;
466
467         ImBuf *ibuf;
468         ImBuf *texibuf;
469         ImBuf *maskibuf;
470 } BrushPainterCache;
471
472 struct BrushPainter {
473         Brush *brush;
474
475         float lastmousepos[2];  /* mouse position of last paint call */
476
477         float accumdistance;    /* accumulated distance of brush since last paint op */
478         float lastpaintpos[2];  /* position of last paint op */
479         float startpaintpos[2]; /* position of first paint */
480
481         double accumtime;               /* accumulated time since last paint op (airbrush) */
482         double lasttime;                /* time of last update */
483
484         float lastpressure;
485
486         short firsttouch;               /* first paint op */
487
488         float startsize;
489         float startalpha;
490         float startinnerradius;
491         float startspacing;
492
493         BrushPainterCache cache;
494 };
495
496 BrushPainter *brush_painter_new(Brush *brush)
497 {
498         BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
499
500         painter->brush= brush;
501         painter->firsttouch= 1;
502         painter->cache.lastsize= -1; /* force ibuf create in refresh */
503
504         painter->startsize = brush->size;
505         painter->startalpha = brush->alpha;
506         painter->startinnerradius = brush->innerradius;
507         painter->startspacing = brush->spacing;
508
509         return painter;
510 }
511
512 void brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
513 {
514         if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
515                 ((painter->cache.texonly != texonly) && texonly)) {
516                 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
517                 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
518                 painter->cache.ibuf= painter->cache.maskibuf= NULL;
519                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
520         }
521
522         if (painter->cache.flt != flt) {
523                 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
524                 painter->cache.texibuf= NULL;
525                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
526         }
527
528         painter->cache.size= size;
529         painter->cache.flt= flt;
530         painter->cache.texonly= texonly;
531         painter->cache.enabled= 1;
532 }
533
534 void brush_painter_free(BrushPainter *painter)
535 {
536         Brush *brush = painter->brush;
537
538         brush->size = painter->startsize;
539         brush->alpha = painter->startalpha;
540         brush->innerradius = painter->startinnerradius;
541         brush->spacing = painter->startspacing;
542
543         if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
544         if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
545         if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
546         MEM_freeN(painter);
547 }
548
549 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, int x, int y, int w, int h, int xt, int yt, float *pos)
550 {
551         Brush *brush= painter->brush;
552         ImBuf *ibuf, *maskibuf, *texibuf;
553         float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
554         char *b, *m, *t, *ot= NULL;
555         int dotexold, origx= x, origy= y;
556
557         xoff = -brush->size/2.0f + 0.5f;
558         yoff = -brush->size/2.0f + 0.5f;
559         xoff += (int)pos[0] - (int)painter->startpaintpos[0];
560         yoff += (int)pos[1] - (int)painter->startpaintpos[1];
561
562         ibuf = painter->cache.ibuf;
563         texibuf = painter->cache.texibuf;
564         maskibuf = painter->cache.maskibuf;
565
566         dotexold = (oldtexibuf != NULL);
567
568         if (painter->cache.flt) {
569                 for (; y < h; y++) {
570                         bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
571                         tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
572                         mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
573
574                         if (dotexold)
575                                 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
576
577                         for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
578                                 if (dotexold) {
579                                         VECCOPY(tf, otf);
580                                         tf[3] = otf[3];
581                                         otf += 4;
582                                 }
583                                 else {
584                                         xy[0] = x + xoff;
585                                         xy[1] = y + yoff;
586
587                                         brush_sample_tex(brush, xy, tf);
588                                 }
589
590                                 bf[0] = tf[0]*mf[0];
591                                 bf[1] = tf[1]*mf[1];
592                                 bf[2] = tf[2]*mf[2];
593                                 bf[3] = tf[3]*mf[3];
594                         }
595                 }
596         }
597         else {
598                 for (; y < h; y++) {
599                         b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
600                         t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
601                         m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
602
603                         if (dotexold)
604                                 ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
605
606                         for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
607                                 if (dotexold) {
608                                         t[0] = ot[0];
609                                         t[1] = ot[1];
610                                         t[2] = ot[2];
611                                         t[3] = ot[3];
612                                         ot += 4;
613                                 }
614                                 else {
615                                         xy[0] = x + xoff;
616                                         xy[1] = y + yoff;
617
618                                         brush_sample_tex(brush, xy, rgba);
619                                         t[0]= FTOCHAR(rgba[0]);
620                                         t[1]= FTOCHAR(rgba[1]);
621                                         t[2]= FTOCHAR(rgba[2]);
622                                         t[3]= FTOCHAR(rgba[3]);
623                                 }
624
625                                 b[0] = t[0]*m[0]/255;
626                                 b[1] = t[1]*m[1]/255;
627                                 b[2] = t[2]*m[2]/255;
628                                 b[3] = t[3]*m[3]/255;
629                         }
630                 }
631         }
632 }
633
634 void brush_painter_fixed_tex_partial_update(BrushPainter *painter, float *pos)
635 {
636         Brush *brush= painter->brush;
637         BrushPainterCache *cache= &painter->cache;
638         ImBuf *oldtexibuf, *ibuf;
639         int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
640
641         imbflag= (cache->flt)? IB_rectfloat: IB_rect;
642         if (!cache->ibuf)
643                 cache->ibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
644         ibuf= cache->ibuf;
645
646         oldtexibuf= cache->texibuf;
647         cache->texibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
648
649         if (oldtexibuf) {
650                 srcx= srcy= 0;
651                 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
652                 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
653                 w= oldtexibuf->x;
654                 h= oldtexibuf->y;
655
656                 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
657         }
658         else {
659                 srcx= srcy= 0;
660                 destx= desty= 0;
661                 w= h= 0;
662         }
663         
664         x1= destx;
665         y1= desty;
666         x2= destx+w;
667         y2= desty+h;
668
669         /* blend existing texture in new position */
670         if ((x1 < x2) && (y1 < y2))
671                 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
672
673         if (oldtexibuf)
674                 IMB_freeImBuf(oldtexibuf);
675
676         /* sample texture in new areas */
677         if ((0 < x1) && (0 < ibuf->y))
678                 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
679         if ((x2 < ibuf->x) && (0 < ibuf->y))
680                 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
681         if ((x1 < x2) && (0 < y1))
682                 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
683         if ((x1 < x2) && (y2 < ibuf->y))
684                 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
685 }
686
687 static void brush_painter_refresh_cache(BrushPainter *painter, float *pos)
688 {
689         Brush *brush= painter->brush;
690         BrushPainterCache *cache= &painter->cache;
691         MTex *mtex= brush->mtex[brush->texact];
692         int size;
693         short flt;
694
695         if ((brush->size != cache->lastsize) || (brush->alpha != cache->lastalpha)
696             || (brush->innerradius != cache->lastinnerradius)) {
697                 if (cache->ibuf) {
698                         IMB_freeImBuf(cache->ibuf);
699                         cache->ibuf= NULL;
700                 }
701                 if (cache->maskibuf) {
702                         IMB_freeImBuf(cache->maskibuf);
703                         cache->maskibuf= NULL;
704                 }
705
706                 flt= cache->flt;
707                 size= (cache->size)? cache->size: brush->size;
708
709                 if (!(mtex && mtex->tex) || (mtex->tex->type==0)) {
710                         brush_imbuf_new(brush, flt, 0, size, &cache->ibuf);
711                 }
712                 else if (brush->flag & BRUSH_FIXED_TEX) {
713                         brush_imbuf_new(brush, flt, 0, size, &cache->maskibuf);
714                         brush_painter_fixed_tex_partial_update(painter, pos);
715                 }
716                 else
717                         brush_imbuf_new(brush, flt, 2, size, &cache->ibuf);
718
719                 cache->lastsize= brush->size;
720                 cache->lastalpha= brush->alpha;
721                 cache->lastinnerradius= brush->innerradius;
722         }
723         else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
724                 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
725                 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
726
727                 if ((dx != 0) || (dy != 0))
728                         brush_painter_fixed_tex_partial_update(painter, pos);
729         }
730 }
731
732 void brush_painter_break_stroke(BrushPainter *painter)
733 {
734         painter->firsttouch= 1;
735 }
736
737 static void brush_apply_pressure(BrushPainter *painter, Brush *brush, float pressure)
738 {
739         if (brush->flag & BRUSH_ALPHA_PRESSURE) 
740                 brush->alpha = MAX2(0.0, painter->startalpha*pressure);
741         if (brush->flag & BRUSH_SIZE_PRESSURE)
742                 brush->size = MAX2(1.0, painter->startsize*pressure);
743         if (brush->flag & BRUSH_RAD_PRESSURE)
744                 brush->innerradius = MAX2(0.0, painter->startinnerradius*pressure);
745         if (brush->flag & BRUSH_SPACING_PRESSURE)
746                 brush->spacing = MAX2(1.0, painter->startspacing*(1.5f-pressure));
747 }
748
749 int brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user)
750 {
751         Brush *brush= painter->brush;
752         int totpaintops= 0;
753
754         if (pressure == 0.0f)
755                 pressure = 1.0f;        /* zero pressure == not using tablet */
756
757         if (painter->firsttouch) {
758                 /* paint exactly once on first touch */
759                 painter->startpaintpos[0]= pos[0];
760                 painter->startpaintpos[1]= pos[1];
761
762                 brush_apply_pressure(painter, brush, pressure);
763                 if (painter->cache.enabled)
764                         brush_painter_refresh_cache(painter, pos);
765                 totpaintops += func(user, painter->cache.ibuf, pos, pos);
766                 
767                 painter->lasttime= time;
768                 painter->firsttouch= 0;
769                 painter->lastpaintpos[0]= pos[0];
770                 painter->lastpaintpos[1]= pos[1];
771         }
772 #if 0
773         else if (painter->brush->flag & BRUSH_AIRBRUSH) {
774                 float spacing, step, paintpos[2], dmousepos[2], len;
775                 double starttime, curtime= time;
776
777                 /* compute brush spacing adapted to brush size */
778                 spacing= brush->rate; //brush->size*brush->spacing*0.01f;
779
780                 /* setup starting time, direction vector and accumulated time */
781                 starttime= painter->accumtime;
782                 Vec2Subf(dmousepos, pos, painter->lastmousepos);
783                 len= Normalize2(dmousepos);
784                 painter->accumtime += curtime - painter->lasttime;
785
786                 /* do paint op over unpainted time distance */
787                 while (painter->accumtime >= spacing) {
788                         step= (spacing - starttime)*len;
789                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
790                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
791
792                         if (painter->cache.enabled)
793                                 brush_painter_refresh_cache(painter);
794                         totpaintops += func(user, painter->cache.ibuf,
795                                 painter->lastpaintpos, paintpos);
796
797                         painter->lastpaintpos[0]= paintpos[0];
798                         painter->lastpaintpos[1]= paintpos[1];
799                         painter->accumtime -= spacing;
800                         starttime -= spacing;
801                 }
802                 
803                 painter->lasttime= curtime;
804         }
805 #endif
806         else {
807                 float startdistance, spacing, step, paintpos[2], dmousepos[2];
808                 float t, len, press;
809
810                 /* compute brush spacing adapted to brush size, spacing may depend
811                    on pressure, so update it */
812                 brush_apply_pressure(painter, brush, painter->lastpressure);
813                 spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
814
815                 /* setup starting distance, direction vector and accumulated distance */
816                 startdistance= painter->accumdistance;
817                 Vec2Subf(dmousepos, pos, painter->lastmousepos);
818                 len= Normalize2(dmousepos);
819                 painter->accumdistance += len;
820
821                 /* do paint op over unpainted distance */
822                 while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
823                         step= spacing - startdistance;
824                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
825                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
826
827                         t = step/len;
828                         press= (1.0f-t)*painter->lastpressure + t*pressure;
829                         brush_apply_pressure(painter, brush, press);
830                         spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
831
832                         if (painter->cache.enabled)
833                                 brush_painter_refresh_cache(painter, paintpos);
834
835                         totpaintops +=
836                                 func(user, painter->cache.ibuf, painter->lastpaintpos, paintpos);
837
838                         painter->lastpaintpos[0]= paintpos[0];
839                         painter->lastpaintpos[1]= paintpos[1];
840                         painter->accumdistance -= spacing;
841                         startdistance -= spacing;
842                 }
843
844                 /* do airbrush paint ops, based on the number of paint ops left over
845                    from regular painting. this is a temporary solution until we have
846                    accurate time stamps for mouse move events */
847                 if (brush->flag & BRUSH_AIRBRUSH) {
848                         double curtime= time;
849                         double painttime= brush->rate*totpaintops;
850
851                         painter->accumtime += curtime - painter->lasttime;
852                         if (painter->accumtime <= painttime)
853                                 painter->accumtime= 0.0;
854                         else
855                                 painter->accumtime -= painttime;
856
857                         while (painter->accumtime >= brush->rate) {
858                                 brush_apply_pressure(painter, brush, pressure);
859                                 if (painter->cache.enabled)
860                                         brush_painter_refresh_cache(painter, paintpos);
861                                 totpaintops +=
862                                         func(user, painter->cache.ibuf, painter->lastmousepos, pos);
863                                 painter->accumtime -= brush->rate;
864                         }
865
866                         painter->lasttime= curtime;
867                 }
868         }
869
870         painter->lastmousepos[0]= pos[0];
871         painter->lastmousepos[1]= pos[1];
872         painter->lastpressure= pressure;
873
874         brush->alpha = painter->startalpha;
875         brush->size = painter->startsize;
876         brush->innerradius = painter->startinnerradius;
877         brush->spacing = painter->startspacing;
878
879         return totpaintops;
880 }
881
882