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