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