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