merge trunk 16118 -> 116886
[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
363 void brush_imbuf_new(Brush *brush, short flt, short texfall, int size, ImBuf **outbuf)
364 {
365         ImBuf *ibuf;
366         float xy[2], dist, rgba[4], *dstf;
367         int x, y, rowbytes, xoff, yoff, imbflag;
368         char *dst, crgb[3];
369
370         imbflag= (flt)? IB_rectfloat: IB_rect;
371         xoff = -size/2.0f + 0.5f;
372         yoff = -size/2.0f + 0.5f;
373         rowbytes= size*4;
374
375         if (*outbuf)
376                 ibuf= *outbuf;
377         else
378                 ibuf= IMB_allocImBuf(size, size, 32, imbflag, 0);
379
380         if (flt) {
381                 for (y=0; y < ibuf->y; y++) {
382                         dstf = ibuf->rect_float + y*rowbytes;
383
384                         for (x=0; x < ibuf->x; x++, dstf+=4) {
385                                 xy[0] = x + xoff;
386                                 xy[1] = y + yoff;
387
388                                 if (texfall == 0) {
389                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
390
391                                         VECCOPY(dstf, brush->rgb);
392                                         dstf[3]= brush_sample_falloff(brush, dist);
393                                 }
394                                 else if (texfall == 1) {
395                                         brush_sample_tex(brush, xy, dstf);
396                                 }
397                                 else {
398                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
399
400                                         brush_sample_tex(brush, xy, rgba);
401
402                                         dstf[0] = rgba[0]*brush->rgb[0];
403                                         dstf[1] = rgba[1]*brush->rgb[1];
404                                         dstf[2] = rgba[2]*brush->rgb[2];
405                                         dstf[3] = rgba[3]*brush_sample_falloff(brush, dist);
406                                 }
407                         }
408                 }
409         }
410         else {
411                 crgb[0]= FTOCHAR(brush->rgb[0]);
412                 crgb[1]= FTOCHAR(brush->rgb[1]);
413                 crgb[2]= FTOCHAR(brush->rgb[2]);
414
415                 for (y=0; y < ibuf->y; y++) {
416                         dst = (char*)ibuf->rect + y*rowbytes;
417
418                         for (x=0; x < ibuf->x; x++, dst+=4) {
419                                 xy[0] = x + xoff;
420                                 xy[1] = y + yoff;
421
422                                 if (texfall == 0) {
423                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
424
425                                         dst[0]= crgb[0];
426                                         dst[1]= crgb[1];
427                                         dst[2]= crgb[2];
428                                         dst[3]= FTOCHAR(brush_sample_falloff(brush, dist));
429                                 }
430                                 else if (texfall == 1) {
431                                         brush_sample_tex(brush, xy, rgba);
432                                         dst[0]= FTOCHAR(rgba[0]);
433                                         dst[1]= FTOCHAR(rgba[1]);
434                                         dst[2]= FTOCHAR(rgba[2]);
435                                         dst[3]= FTOCHAR(rgba[3]);
436                                 }
437                                 else {
438                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
439
440                                         brush_sample_tex(brush, xy, rgba);
441                                         dst[0] = FTOCHAR(rgba[0]*brush->rgb[0]);
442                                         dst[1] = FTOCHAR(rgba[1]*brush->rgb[1]);
443                                         dst[2] = FTOCHAR(rgba[2]*brush->rgb[2]);
444                                         dst[3] = FTOCHAR(rgba[3]*brush_sample_falloff(brush, dist));
445                                 }
446                         }
447                 }
448         }
449
450         *outbuf= ibuf;
451 }
452
453 /* Brush Painting */
454
455 typedef struct BrushPainterCache {
456         short enabled;
457
458         int size;                       /* size override, if 0 uses brush->size */
459         short flt;                      /* need float imbuf? */
460         short texonly;          /* no alpha, color or fallof, only texture in imbuf */
461
462         int lastsize;
463         float lastalpha;
464         float lastinnerradius;
465
466         ImBuf *ibuf;
467         ImBuf *texibuf;
468         ImBuf *maskibuf;
469 } BrushPainterCache;
470
471 struct BrushPainter {
472         Brush *brush;
473
474         float lastmousepos[2];  /* mouse position of last paint call */
475
476         float accumdistance;    /* accumulated distance of brush since last paint op */
477         float lastpaintpos[2];  /* position of last paint op */
478         float startpaintpos[2]; /* position of first paint */
479
480         double accumtime;               /* accumulated time since last paint op (airbrush) */
481         double lasttime;                /* time of last update */
482
483         float lastpressure;
484
485         short firsttouch;               /* first paint op */
486
487         float startsize;
488         float startalpha;
489         float startinnerradius;
490         float startspacing;
491
492         BrushPainterCache cache;
493 };
494
495 BrushPainter *brush_painter_new(Brush *brush)
496 {
497         BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
498
499         painter->brush= brush;
500         painter->firsttouch= 1;
501         painter->cache.lastsize= -1; /* force ibuf create in refresh */
502
503         painter->startsize = brush->size;
504         painter->startalpha = brush->alpha;
505         painter->startinnerradius = brush->innerradius;
506         painter->startspacing = brush->spacing;
507
508         return painter;
509 }
510
511 void brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
512 {
513         if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
514                 ((painter->cache.texonly != texonly) && texonly)) {
515                 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
516                 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
517                 painter->cache.ibuf= painter->cache.maskibuf= NULL;
518                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
519         }
520
521         if (painter->cache.flt != flt) {
522                 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
523                 painter->cache.texibuf= NULL;
524                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
525         }
526
527         painter->cache.size= size;
528         painter->cache.flt= flt;
529         painter->cache.texonly= texonly;
530         painter->cache.enabled= 1;
531 }
532
533 void brush_painter_free(BrushPainter *painter)
534 {
535         Brush *brush = painter->brush;
536
537         brush->size = painter->startsize;
538         brush->alpha = painter->startalpha;
539         brush->innerradius = painter->startinnerradius;
540         brush->spacing = painter->startspacing;
541
542         if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
543         if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
544         if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
545         MEM_freeN(painter);
546 }
547
548 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, int x, int y, int w, int h, int xt, int yt, float *pos)
549 {
550         Brush *brush= painter->brush;
551         ImBuf *ibuf, *maskibuf, *texibuf;
552         float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
553         char *b, *m, *t, *ot= NULL;
554         int dotexold, origx= x, origy= y;
555
556         xoff = -brush->size/2.0f + 0.5f;
557         yoff = -brush->size/2.0f + 0.5f;
558         xoff += (int)pos[0] - (int)painter->startpaintpos[0];
559         yoff += (int)pos[1] - (int)painter->startpaintpos[1];
560
561         ibuf = painter->cache.ibuf;
562         texibuf = painter->cache.texibuf;
563         maskibuf = painter->cache.maskibuf;
564
565         dotexold = (oldtexibuf != NULL);
566
567         if (painter->cache.flt) {
568                 for (; y < h; y++) {
569                         bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
570                         tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
571                         mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
572
573                         if (dotexold)
574                                 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
575
576                         for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
577                                 if (dotexold) {
578                                         VECCOPY(tf, otf);
579                                         tf[3] = otf[3];
580                                         otf += 4;
581                                 }
582                                 else {
583                                         xy[0] = x + xoff;
584                                         xy[1] = y + yoff;
585
586                                         brush_sample_tex(brush, xy, tf);
587                                 }
588
589                                 bf[0] = tf[0]*mf[0];
590                                 bf[1] = tf[1]*mf[1];
591                                 bf[2] = tf[2]*mf[2];
592                                 bf[3] = tf[3]*mf[3];
593                         }
594                 }
595         }
596         else {
597                 for (; y < h; y++) {
598                         b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
599                         t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
600                         m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
601
602                         if (dotexold)
603                                 ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
604
605                         for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
606                                 if (dotexold) {
607                                         t[0] = ot[0];
608                                         t[1] = ot[1];
609                                         t[2] = ot[2];
610                                         t[3] = ot[3];
611                                         ot += 4;
612                                 }
613                                 else {
614                                         xy[0] = x + xoff;
615                                         xy[1] = y + yoff;
616
617                                         brush_sample_tex(brush, xy, rgba);
618                                         t[0]= FTOCHAR(rgba[0]);
619                                         t[1]= FTOCHAR(rgba[1]);
620                                         t[2]= FTOCHAR(rgba[2]);
621                                         t[3]= FTOCHAR(rgba[3]);
622                                 }
623
624                                 b[0] = t[0]*m[0]/255;
625                                 b[1] = t[1]*m[1]/255;
626                                 b[2] = t[2]*m[2]/255;
627                                 b[3] = t[3]*m[3]/255;
628                         }
629                 }
630         }
631 }
632
633 void brush_painter_fixed_tex_partial_update(BrushPainter *painter, float *pos)
634 {
635         Brush *brush= painter->brush;
636         BrushPainterCache *cache= &painter->cache;
637         ImBuf *oldtexibuf, *ibuf;
638         int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
639
640         imbflag= (cache->flt)? IB_rectfloat: IB_rect;
641         if (!cache->ibuf)
642                 cache->ibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
643         ibuf= cache->ibuf;
644
645         oldtexibuf= cache->texibuf;
646         cache->texibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
647
648         if (oldtexibuf) {
649                 srcx= srcy= 0;
650                 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
651                 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
652                 w= oldtexibuf->x;
653                 h= oldtexibuf->y;
654
655                 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
656         }
657         else {
658                 srcx= srcy= 0;
659                 destx= desty= 0;
660                 w= h= 0;
661         }
662         
663         x1= destx;
664         y1= desty;
665         x2= destx+w;
666         y2= desty+h;
667
668         /* blend existing texture in new position */
669         if ((x1 < x2) && (y1 < y2))
670                 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
671
672         if (oldtexibuf)
673                 IMB_freeImBuf(oldtexibuf);
674
675         /* sample texture in new areas */
676         if ((0 < x1) && (0 < ibuf->y))
677                 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
678         if ((x2 < ibuf->x) && (0 < ibuf->y))
679                 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
680         if ((x1 < x2) && (0 < y1))
681                 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
682         if ((x1 < x2) && (y2 < ibuf->y))
683                 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
684 }
685
686 static void brush_painter_refresh_cache(BrushPainter *painter, float *pos)
687 {
688         Brush *brush= painter->brush;
689         BrushPainterCache *cache= &painter->cache;
690         MTex *mtex= brush->mtex[brush->texact];
691         int size;
692         short flt;
693
694         if ((brush->size != cache->lastsize) || (brush->alpha != cache->lastalpha)
695             || (brush->innerradius != cache->lastinnerradius)) {
696                 if (cache->ibuf) {
697                         IMB_freeImBuf(cache->ibuf);
698                         cache->ibuf= NULL;
699                 }
700                 if (cache->maskibuf) {
701                         IMB_freeImBuf(cache->maskibuf);
702                         cache->maskibuf= NULL;
703                 }
704
705                 flt= cache->flt;
706                 size= (cache->size)? cache->size: brush->size;
707
708                 if (!(mtex && mtex->tex) || (mtex->tex->type==0)) {
709                         brush_imbuf_new(brush, flt, 0, size, &cache->ibuf);
710                 }
711                 else if (brush->flag & BRUSH_FIXED_TEX) {
712                         brush_imbuf_new(brush, flt, 0, size, &cache->maskibuf);
713                         brush_painter_fixed_tex_partial_update(painter, pos);
714                 }
715                 else
716                         brush_imbuf_new(brush, flt, 2, size, &cache->ibuf);
717
718                 cache->lastsize= brush->size;
719                 cache->lastalpha= brush->alpha;
720                 cache->lastinnerradius= brush->innerradius;
721         }
722         else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
723                 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
724                 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
725
726                 if ((dx != 0) || (dy != 0))
727                         brush_painter_fixed_tex_partial_update(painter, pos);
728         }
729 }
730
731 void brush_painter_break_stroke(BrushPainter *painter)
732 {
733         painter->firsttouch= 1;
734 }
735
736 static void brush_apply_pressure(BrushPainter *painter, Brush *brush, float pressure)
737 {
738         if (brush->flag & BRUSH_ALPHA_PRESSURE) 
739                 brush->alpha = MAX2(0.0, painter->startalpha*pressure);
740         if (brush->flag & BRUSH_SIZE_PRESSURE)
741                 brush->size = MAX2(1.0, painter->startsize*pressure);
742         if (brush->flag & BRUSH_RAD_PRESSURE)
743                 brush->innerradius = MAX2(0.0, painter->startinnerradius*pressure);
744         if (brush->flag & BRUSH_SPACING_PRESSURE)
745                 brush->spacing = MAX2(1.0, painter->startspacing*(1.5f-pressure));
746 }
747
748 int brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user)
749 {
750         Brush *brush= painter->brush;
751         int totpaintops= 0;
752
753         if (pressure == 0.0f)
754                 pressure = 1.0f;        /* zero pressure == not using tablet */
755
756         if (painter->firsttouch) {
757                 /* paint exactly once on first touch */
758                 painter->startpaintpos[0]= pos[0];
759                 painter->startpaintpos[1]= pos[1];
760
761                 brush_apply_pressure(painter, brush, pressure);
762                 if (painter->cache.enabled)
763                         brush_painter_refresh_cache(painter, pos);
764                 totpaintops += func(user, painter->cache.ibuf, pos, pos);
765                 
766                 painter->lasttime= time;
767                 painter->firsttouch= 0;
768                 painter->lastpaintpos[0]= pos[0];
769                 painter->lastpaintpos[1]= pos[1];
770         }
771 #if 0
772         else if (painter->brush->flag & BRUSH_AIRBRUSH) {
773                 float spacing, step, paintpos[2], dmousepos[2], len;
774                 double starttime, curtime= time;
775
776                 /* compute brush spacing adapted to brush size */
777                 spacing= brush->rate; //brush->size*brush->spacing*0.01f;
778
779                 /* setup starting time, direction vector and accumulated time */
780                 starttime= painter->accumtime;
781                 Vec2Subf(dmousepos, pos, painter->lastmousepos);
782                 len= Normalize2(dmousepos);
783                 painter->accumtime += curtime - painter->lasttime;
784
785                 /* do paint op over unpainted time distance */
786                 while (painter->accumtime >= spacing) {
787                         step= (spacing - starttime)*len;
788                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
789                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
790
791                         if (painter->cache.enabled)
792                                 brush_painter_refresh_cache(painter);
793                         totpaintops += func(user, painter->cache.ibuf,
794                                 painter->lastpaintpos, paintpos);
795
796                         painter->lastpaintpos[0]= paintpos[0];
797                         painter->lastpaintpos[1]= paintpos[1];
798                         painter->accumtime -= spacing;
799                         starttime -= spacing;
800                 }
801                 
802                 painter->lasttime= curtime;
803         }
804 #endif
805         else {
806                 float startdistance, spacing, step, paintpos[2], dmousepos[2];
807                 float t, len, press;
808
809                 /* compute brush spacing adapted to brush size, spacing may depend
810                    on pressure, so update it */
811                 brush_apply_pressure(painter, brush, painter->lastpressure);
812                 spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
813
814                 /* setup starting distance, direction vector and accumulated distance */
815                 startdistance= painter->accumdistance;
816                 Vec2Subf(dmousepos, pos, painter->lastmousepos);
817                 len= Normalize2(dmousepos);
818                 painter->accumdistance += len;
819
820                 /* do paint op over unpainted distance */
821                 while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
822                         step= spacing - startdistance;
823                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
824                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
825
826                         t = step/len;
827                         press= (1.0f-t)*painter->lastpressure + t*pressure;
828                         brush_apply_pressure(painter, brush, press);
829                         spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
830
831                         if (painter->cache.enabled)
832                                 brush_painter_refresh_cache(painter, paintpos);
833
834                         totpaintops +=
835                                 func(user, painter->cache.ibuf, painter->lastpaintpos, paintpos);
836
837                         painter->lastpaintpos[0]= paintpos[0];
838                         painter->lastpaintpos[1]= paintpos[1];
839                         painter->accumdistance -= spacing;
840                         startdistance -= spacing;
841                 }
842
843                 /* do airbrush paint ops, based on the number of paint ops left over
844                    from regular painting. this is a temporary solution until we have
845                    accurate time stamps for mouse move events */
846                 if (brush->flag & BRUSH_AIRBRUSH) {
847                         double curtime= time;
848                         double painttime= brush->rate*totpaintops;
849
850                         painter->accumtime += curtime - painter->lasttime;
851                         if (painter->accumtime <= painttime)
852                                 painter->accumtime= 0.0;
853                         else
854                                 painter->accumtime -= painttime;
855
856                         while (painter->accumtime >= brush->rate) {
857                                 brush_apply_pressure(painter, brush, pressure);
858                                 if (painter->cache.enabled)
859                                         brush_painter_refresh_cache(painter, paintpos);
860                                 totpaintops +=
861                                         func(user, painter->cache.ibuf, painter->lastmousepos, pos);
862                                 painter->accumtime -= brush->rate;
863                         }
864
865                         painter->lasttime= curtime;
866                 }
867         }
868
869         painter->lastmousepos[0]= pos[0];
870         painter->lastmousepos[1]= pos[1];
871         painter->lastpressure= pressure;
872
873         brush->alpha = painter->startalpha;
874         brush->size = painter->startsize;
875         brush->innerradius = painter->startinnerradius;
876         brush->spacing = painter->startspacing;
877
878         return totpaintops;
879 }
880
881