Fix for bug #5666:
[blender.git] / source / blender / blenkernel / intern / brush.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <math.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_brush_types.h"
38 #include "DNA_image_types.h"
39 #include "DNA_texture_types.h"
40 #include "DNA_scene_types.h"
41
42 #include "BLI_arithb.h"
43 #include "BLI_blenlib.h"
44
45 #include "BKE_brush.h"
46 #include "BKE_global.h"
47 #include "BKE_library.h"
48 #include "BKE_main.h"
49 #include "BKE_texture.h"
50 #include "BKE_utildefines.h"
51
52 #include "IMB_imbuf.h"
53 #include "IMB_imbuf_types.h"
54
55 #include "RE_render_ext.h" /* externtex */
56
57 /* Datablock add/copy/free/make_local */
58
59 Brush *add_brush(char *name)
60 {
61         Brush *brush;
62
63         brush= alloc_libblock(&G.main->brush, ID_BR, name);
64
65         brush->rgb[0]= 1.0f;
66         brush->rgb[1]= 1.0f;
67         brush->rgb[2]= 1.0f;
68         brush->alpha= 0.2f;
69         brush->size= 25;
70         brush->spacing= 10.0f;
71         brush->rate= 0.1f;
72         brush->innerradius= 0.5f;
73         brush->clone.alpha= 0.5;
74
75         /* enable fake user by default */
76         brush_toggle_fake_user(brush);
77         
78         return brush;   
79 }
80
81 Brush *copy_brush(Brush *brush)
82 {
83         Brush *brushn;
84         MTex *mtex;
85         int a;
86         
87         brushn= copy_libblock(brush);
88
89         for(a=0; a<MAX_MTEX; a++) {
90                 mtex= brush->mtex[a];
91                 if(mtex) {
92                         brushn->mtex[a]= MEM_dupallocN(mtex);
93                         if(mtex->tex) id_us_plus((ID*)mtex->tex);
94                 }
95         }
96
97         /* enable fake user by default */
98         if (!(brushn->id.flag & LIB_FAKEUSER))
99                 brush_toggle_fake_user(brushn);
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_toggle_fake_user(brush);
153         }
154         else if(local && lib) {
155                 brushn= copy_brush(brush);
156                 brushn->id.us= 1; /* only keep fake user */
157                 brushn->id.flag |= LIB_FAKEUSER;
158                 
159                 for(scene= G.main->scene.first; scene; scene=scene->id.next)
160                         if(scene->toolsettings->imapaint.brush==brush)
161                                 if(scene->id.lib==0) {
162                                         scene->toolsettings->imapaint.brush= brushn;
163                                         brushn->id.us++;
164                                         brush->id.us--;
165                                 }
166         }
167 }
168
169 /* Library Operations */
170
171 int brush_set_nr(Brush **current_brush, int nr)
172 {
173         ID *idtest, *id;
174         
175         id= (ID*)(*current_brush);
176         idtest= (ID*)BLI_findlink(&G.main->brush, nr-1);
177         
178         if(idtest==0) { /* new brush */
179                 if(id) idtest= (ID *)copy_brush((Brush *)id);
180                 else idtest= (ID *)add_brush("Brush");
181                 idtest->us--;
182         }
183         if(idtest!=id) {
184                 brush_delete(current_brush);
185                 *current_brush= (Brush *)idtest;
186                 id_us_plus(idtest);
187
188                 return 1;
189         }
190
191         return 0;
192 }
193
194 int brush_delete(Brush **current_brush)
195 {
196         if (*current_brush) {
197                 (*current_brush)->id.us--;
198                 *current_brush= NULL;
199
200                 return 1;
201         }
202
203         return 0;
204 }
205
206 void brush_toggle_fake_user(Brush *brush)
207 {
208         ID *id= (ID*)brush;
209         if(id) {
210                 if(id->flag & LIB_FAKEUSER) {
211                         id->flag -= LIB_FAKEUSER;
212                         id->us--;
213                 } else {
214                         id->flag |= LIB_FAKEUSER;
215                         id_us_plus(id);
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         if (brush->mtex[0] && brush->mtex[0]->tex) {
334                 float co[3], tin, tr, tg, tb, ta;
335                 int hasrgb;
336                 
337                 co[0]= xy[0]/(brush->size >> 1);
338                 co[1]= xy[1]/(brush->size >> 1);
339                 co[2]= 0.0f;
340
341                 hasrgb= externtex(brush->mtex[0], co, &tin, &tr, &tg, &tb, &ta);
342
343                 if (hasrgb) {
344                         rgba[0]= tr;
345                         rgba[1]= tg;
346                         rgba[2]= tb;
347                         rgba[3]= ta;
348                 }
349                 else {
350                         rgba[0]= tin;
351                         rgba[1]= tin;
352                         rgba[2]= tin;
353                         rgba[3]= 1.0f;
354                 }
355         }
356         else if (rgba)
357                 rgba[0]= rgba[1]= rgba[2]= rgba[3]= 1.0f;
358 }
359
360 #define FTOCHAR(val) val<=0.0f?0: (val>=1.0f?255: (char)(255.0f*val))
361
362 void brush_imbuf_new(Brush *brush, short flt, short texfall, int size, ImBuf **outbuf)
363 {
364         ImBuf *ibuf;
365         float xy[2], dist, rgba[3], *dstf;
366         int x, y, rowbytes, xoff, yoff, imbflag;
367         char *dst, crgb[3];
368
369         imbflag= (flt)? IB_rectfloat: IB_rect;
370         xoff = -size/2.0f + 0.5f;
371         yoff = -size/2.0f + 0.5f;
372         rowbytes= size*4;
373
374         if (*outbuf)
375                 ibuf= *outbuf;
376         else
377                 ibuf= IMB_allocImBuf(size, size, 32, imbflag, 0);
378
379         if (flt) {
380                 for (y=0; y < ibuf->y; y++) {
381                         dstf = ibuf->rect_float + y*rowbytes;
382
383                         for (x=0; x < ibuf->x; x++, dstf+=4) {
384                                 xy[0] = x + xoff;
385                                 xy[1] = y + yoff;
386
387                                 if (texfall == 0) {
388                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
389
390                                         VECCOPY(dstf, brush->rgb);
391                                         dstf[3]= brush_sample_falloff(brush, dist);
392                                 }
393                                 else if (texfall == 1) {
394                                         brush_sample_tex(brush, xy, dstf);
395                                 }
396                                 else {
397                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
398
399                                         brush_sample_tex(brush, xy, rgba);
400
401                                         dstf[0] = rgba[0]*brush->rgb[0];
402                                         dstf[1] = rgba[1]*brush->rgb[1];
403                                         dstf[2] = rgba[2]*brush->rgb[2];
404                                         dstf[3] = rgba[3]*brush_sample_falloff(brush, dist);
405                                 }
406                         }
407                 }
408         }
409         else {
410                 crgb[0]= FTOCHAR(brush->rgb[0]);
411                 crgb[1]= FTOCHAR(brush->rgb[1]);
412                 crgb[2]= FTOCHAR(brush->rgb[2]);
413
414                 for (y=0; y < ibuf->y; y++) {
415                         dst = (char*)ibuf->rect + y*rowbytes;
416
417                         for (x=0; x < ibuf->x; x++, dst+=4) {
418                                 xy[0] = x + xoff;
419                                 xy[1] = y + yoff;
420
421                                 if (texfall == 0) {
422                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
423
424                                         dst[0]= crgb[0];
425                                         dst[1]= crgb[1];
426                                         dst[2]= crgb[2];
427                                         dst[3]= FTOCHAR(brush_sample_falloff(brush, dist));
428                                 }
429                                 else if (texfall == 1) {
430                                         brush_sample_tex(brush, xy, rgba);
431                                         dst[0]= FTOCHAR(rgba[0]);
432                                         dst[1]= FTOCHAR(rgba[1]);
433                                         dst[2]= FTOCHAR(rgba[2]);
434                                         dst[3]= FTOCHAR(rgba[3]);
435                                 }
436                                 else {
437                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
438
439                                         brush_sample_tex(brush, xy, rgba);
440                                         dst[0] = FTOCHAR(rgba[0]*brush->rgb[0]);
441                                         dst[1] = FTOCHAR(rgba[1]*brush->rgb[1]);
442                                         dst[2] = FTOCHAR(rgba[2]*brush->rgb[2]);
443                                         dst[3] = FTOCHAR(rgba[3]*brush_sample_falloff(brush, dist));
444                                 }
445                         }
446                 }
447         }
448
449         *outbuf= ibuf;
450 }
451
452 /* Brush Painting */
453
454 typedef struct BrushPainterCache {
455         short enabled;
456
457         int size;                       /* size override, if 0 uses brush->size */
458         short flt;                      /* need float imbuf? */
459         short texonly;          /* no alpha, color or fallof, only texture in imbuf */
460
461         int lastsize;
462         float lastalpha;
463         float lastinnerradius;
464
465         ImBuf *ibuf;
466         ImBuf *texibuf;
467         ImBuf *maskibuf;
468 } BrushPainterCache;
469
470 struct BrushPainter {
471         Brush *brush;
472
473         float lastmousepos[2];  /* mouse position of last paint call */
474
475         float accumdistance;    /* accumulated distance of brush since last paint op */
476         float lastpaintpos[2];  /* position of last paint op */
477         float startpaintpos[2]; /* position of first paint */
478
479         double accumtime;               /* accumulated time since last paint op (airbrush) */
480         double lasttime;                /* time of last update */
481
482         float lastpressure;
483
484         short firsttouch;               /* first paint op */
485
486         float startsize;
487         float startalpha;
488         float startinnerradius;
489         float startspacing;
490
491         BrushPainterCache cache;
492 };
493
494 BrushPainter *brush_painter_new(Brush *brush)
495 {
496         BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
497
498         painter->brush= brush;
499         painter->firsttouch= 1;
500         painter->cache.lastsize= -1; /* force ibuf create in refresh */
501
502         painter->startsize = brush->size;
503         painter->startalpha = brush->alpha;
504         painter->startinnerradius = brush->innerradius;
505         painter->startspacing = brush->spacing;
506
507         return painter;
508 }
509
510 void brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
511 {
512         if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
513                 ((painter->cache.texonly != texonly) && texonly)) {
514                 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
515                 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
516                 painter->cache.ibuf= painter->cache.maskibuf= NULL;
517                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
518         }
519
520         if (painter->cache.flt != flt) {
521                 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
522                 painter->cache.texibuf= NULL;
523                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
524         }
525
526         painter->cache.size= size;
527         painter->cache.flt= flt;
528         painter->cache.texonly= texonly;
529         painter->cache.enabled= 1;
530 }
531
532 void brush_painter_free(BrushPainter *painter)
533 {
534         Brush *brush = painter->brush;
535
536         brush->size = painter->startsize;
537         brush->alpha = painter->startalpha;
538         brush->innerradius = painter->startinnerradius;
539         brush->spacing = painter->startspacing;
540
541         if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
542         if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
543         if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
544         MEM_freeN(painter);
545 }
546
547 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, int x, int y, int w, int h, int xt, int yt, float *pos)
548 {
549         Brush *brush= painter->brush;
550         ImBuf *ibuf, *maskibuf, *texibuf;
551         float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
552         char *b, *m, *t, *ot= NULL;
553         int dotexold, origx= x, origy= y;
554
555         xoff = -brush->size/2.0f + 0.5f;
556         yoff = -brush->size/2.0f + 0.5f;
557         xoff += (int)pos[0] - (int)painter->startpaintpos[0];
558         yoff += (int)pos[1] - (int)painter->startpaintpos[1];
559
560         ibuf = painter->cache.ibuf;
561         texibuf = painter->cache.texibuf;
562         maskibuf = painter->cache.maskibuf;
563
564         dotexold = (oldtexibuf != NULL);
565
566         if (painter->cache.flt) {
567                 for (; y < h; y++) {
568                         bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
569                         tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
570                         mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
571
572                         if (dotexold)
573                                 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
574
575                         for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
576                                 if (dotexold) {
577                                         VECCOPY(tf, otf);
578                                         tf[3] = otf[3];
579                                         otf += 4;
580                                 }
581                                 else {
582                                         xy[0] = x + xoff;
583                                         xy[1] = y + yoff;
584
585                                         brush_sample_tex(brush, xy, tf);
586                                 }
587
588                                 bf[0] = tf[0]*mf[0];
589                                 bf[1] = tf[1]*mf[1];
590                                 bf[2] = tf[2]*mf[2];
591                                 bf[3] = tf[3]*mf[3];
592                         }
593                 }
594         }
595         else {
596                 for (; y < h; y++) {
597                         b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
598                         t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
599                         m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
600
601                         if (dotexold)
602                                 ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
603
604                         for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
605                                 if (dotexold) {
606                                         t[0] = ot[0];
607                                         t[1] = ot[1];
608                                         t[2] = ot[2];
609                                         t[3] = ot[3];
610                                         ot += 4;
611                                 }
612                                 else {
613                                         xy[0] = x + xoff;
614                                         xy[1] = y + yoff;
615
616                                         brush_sample_tex(brush, xy, rgba);
617                                         t[0]= FTOCHAR(rgba[0]);
618                                         t[1]= FTOCHAR(rgba[1]);
619                                         t[2]= FTOCHAR(rgba[2]);
620                                         t[3]= FTOCHAR(rgba[3]);
621                                 }
622
623                                 b[0] = t[0]*m[0]/255;
624                                 b[1] = t[1]*m[1]/255;
625                                 b[2] = t[2]*m[2]/255;
626                                 b[3] = t[3]*m[3]/255;
627                         }
628                 }
629         }
630 }
631
632 void brush_painter_fixed_tex_partial_update(BrushPainter *painter, float *pos)
633 {
634         Brush *brush= painter->brush;
635         BrushPainterCache *cache= &painter->cache;
636         ImBuf *oldtexibuf, *ibuf;
637         int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
638
639         imbflag= (cache->flt)? IB_rectfloat: IB_rect;
640         if (!cache->ibuf)
641                 cache->ibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
642         ibuf= cache->ibuf;
643
644         oldtexibuf= cache->texibuf;
645         cache->texibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
646
647         if (oldtexibuf) {
648                 srcx= srcy= 0;
649                 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
650                 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
651                 w= oldtexibuf->x;
652                 h= oldtexibuf->y;
653
654                 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
655         }
656         else {
657                 srcx= srcy= 0;
658                 destx= desty= 0;
659                 w= h= 0;
660         }
661         
662         x1= destx;
663         y1= desty;
664         x2= destx+w;
665         y2= desty+h;
666
667         /* blend existing texture in new position */
668         if ((x1 < x2) && (y1 < y2))
669                 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
670
671         if (oldtexibuf)
672                 IMB_freeImBuf(oldtexibuf);
673
674         /* sample texture in new areas */
675         if ((0 < x1) && (0 < ibuf->y))
676                 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
677         if ((x2 < ibuf->x) && (0 < ibuf->y))
678                 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
679         if ((x1 < x2) && (0 < y1))
680                 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
681         if ((x1 < x2) && (y2 < ibuf->y))
682                 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
683 }
684
685 static void brush_painter_refresh_cache(BrushPainter *painter, float *pos)
686 {
687         Brush *brush= painter->brush;
688         BrushPainterCache *cache= &painter->cache;
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 (!(brush->mtex[0] && brush->mtex[0]->tex) || (brush->mtex[0]->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) && brush->mtex[0] && brush->mtex[0]->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= Normalise2(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= Normalise2(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