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