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