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