2
* ***** BEGIN GPL LICENSE BLOCK *****
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19
* All rights reserved.
21
* The Original Code is: all of this file.
23
* Contributor(s): none yet.
25
* ***** END GPL LICENSE BLOCK *****
28
/** \file blender/editors/sculpt_paint/paint_image_2d.c
34
#include "MEM_guardedalloc.h"
36
#include "DNA_brush_types.h"
37
#include "DNA_scene_types.h"
39
#include "BKE_brush.h"
43
#include "IMB_imbuf.h"
44
#include "IMB_imbuf_types.h"
46
#include "RE_shader_ext.h"
48
/* Brush Painting for 2D image editor */
50
typedef struct BrushPainterCache {
53
int size; /* size override, if 0 uses 2*BKE_brush_size_get(brush) */
54
short flt; /* need float imbuf? */
55
short texonly; /* no alpha, color or fallof, only texture in imbuf */
70
float lastmousepos[2]; /* mouse position of last paint call */
72
float accumdistance; /* accumulated distance of brush since last paint op */
73
float lastpaintpos[2]; /* position of last paint op */
74
float startpaintpos[2]; /* position of first paint */
76
double accumtime; /* accumulated time since last paint op (airbrush) */
77
double lasttime; /* time of last update */
81
short firsttouch; /* first paint op */
88
BrushPainterCache cache;
91
BrushPainter *BKE_brush_painter_new(Scene *scene, Brush *brush)
93
BrushPainter *painter = MEM_callocN(sizeof(BrushPainter), "BrushPainter");
95
painter->brush = brush;
96
painter->scene = scene;
97
painter->firsttouch = 1;
98
painter->cache.lastsize = -1; /* force ibuf create in refresh */
100
painter->startsize = BKE_brush_size_get(scene, brush);
101
painter->startalpha = BKE_brush_alpha_get(scene, brush);
102
painter->startjitter = brush->jitter;
103
painter->startspacing = brush->spacing;
109
static void brush_pressure_apply(BrushPainter *painter, Brush *brush, float pressure)
111
if (BKE_brush_use_alpha_pressure(painter->scene, brush))
112
BKE_brush_alpha_set(painter->scene, brush, max_ff(0.0f, painter->startalpha * pressure));
113
if (BKE_brush_use_size_pressure(painter->scene, brush))
114
BKE_brush_size_set(painter->scene, brush, max_ff(1.0f, painter->startsize * pressure));
115
if (brush->flag & BRUSH_JITTER_PRESSURE)
116
brush->jitter = max_ff(0.0f, painter->startjitter * pressure);
117
if (brush->flag & BRUSH_SPACING_PRESSURE)
118
brush->spacing = max_ff(1.0f, painter->startspacing * (1.5f - pressure));
122
void BKE_brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
124
if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
125
((painter->cache.texonly != texonly) && texonly))
127
if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
128
if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
129
painter->cache.ibuf = painter->cache.maskibuf = NULL;
130
painter->cache.lastsize = -1; /* force ibuf create in refresh */
133
if (painter->cache.flt != flt) {
134
if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
135
painter->cache.texibuf = NULL;
136
painter->cache.lastsize = -1; /* force ibuf create in refresh */
139
painter->cache.size = size;
140
painter->cache.flt = flt;
141
painter->cache.texonly = texonly;
142
painter->cache.enabled = 1;
145
void BKE_brush_painter_free(BrushPainter *painter)
147
Brush *brush = painter->brush;
149
BKE_brush_size_set(painter->scene, brush, painter->startsize);
150
BKE_brush_alpha_set(painter->scene, brush, painter->startalpha);
151
brush->jitter = painter->startjitter;
152
brush->spacing = painter->startspacing;
154
if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
155
if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
156
if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
160
static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf,
161
int x, int y, int w, int h, int xt, int yt,
164
Scene *scene = painter->scene;
165
Brush *brush = painter->brush;
166
ImBuf *ibuf, *maskibuf, *texibuf;
167
float *bf, *mf, *tf, *otf = NULL, xoff, yoff, xy[2], rgba[4];
168
unsigned char *b, *m, *t, *ot = NULL;
169
int dotexold, origx = x, origy = y;
170
const int radius = BKE_brush_size_get(painter->scene, brush);
172
xoff = -radius + 0.5f;
173
yoff = -radius + 0.5f;
174
xoff += (int)pos[0] - (int)painter->startpaintpos[0];
175
yoff += (int)pos[1] - (int)painter->startpaintpos[1];
177
ibuf = painter->cache.ibuf;
178
texibuf = painter->cache.texibuf;
179
maskibuf = painter->cache.maskibuf;
181
dotexold = (oldtexibuf != NULL);
183
/* not sure if it's actually needed or it's a mistake in coords/sizes
184
* calculation in brush_painter_fixed_tex_partial_update(), but without this
185
* limitation memory gets corrupted at fast strokes with quite big spacing (sergey) */
186
w = min_ii(w, ibuf->x);
187
h = min_ii(h, ibuf->y);
189
if (painter->cache.flt) {
191
bf = ibuf->rect_float + (y * ibuf->x + origx) * 4;
192
tf = texibuf->rect_float + (y * texibuf->x + origx) * 4;
193
mf = maskibuf->rect_float + (y * maskibuf->x + origx) * 4;
196
otf = oldtexibuf->rect_float + ((y - origy + yt) * oldtexibuf->x + xt) * 4;
198
for (x = origx; x < w; x++, bf += 4, mf += 4, tf += 4) {
207
BKE_brush_sample_tex_2D(scene, brush, xy, tf, 0);
210
bf[0] = tf[0] * mf[0];
211
bf[1] = tf[1] * mf[1];
212
bf[2] = tf[2] * mf[2];
213
bf[3] = tf[3] * mf[3];
219
b = (unsigned char *)ibuf->rect + (y * ibuf->x + origx) * 4;
220
t = (unsigned char *)texibuf->rect + (y * texibuf->x + origx) * 4;
221
m = (unsigned char *)maskibuf->rect + (y * maskibuf->x + origx) * 4;
224
ot = (unsigned char *)oldtexibuf->rect + ((y - origy + yt) * oldtexibuf->x + xt) * 4;
226
for (x = origx; x < w; x++, b += 4, m += 4, t += 4) {
238
BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
239
rgba_float_to_uchar(t, rgba);
242
b[0] = t[0] * m[0] / 255;
243
b[1] = t[1] * m[1] / 255;
244
b[2] = t[2] * m[2] / 255;
245
b[3] = t[3] * m[3] / 255;
251
static void brush_painter_tiled_tex_partial_update(BrushPainter *painter, const float pos[2])
253
const Scene *scene = painter->scene;
254
Brush *brush = painter->brush;
255
BrushPainterCache *cache = &painter->cache;
256
ImBuf *oldtexibuf, *ibuf;
257
int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
258
const int diameter = 2 * BKE_brush_size_get(scene, brush);
260
imbflag = (cache->flt) ? IB_rectfloat : IB_rect;
262
cache->ibuf = IMB_allocImBuf(diameter, diameter, 32, imbflag);
265
oldtexibuf = cache->texibuf;
266
cache->texibuf = IMB_allocImBuf(diameter, diameter, 32, imbflag);
269
destx = (int)painter->lastpaintpos[0] - (int)pos[0];
270
desty = (int)painter->lastpaintpos[1] - (int)pos[1];
274
IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
287
/* blend existing texture in new position */
288
if ((x1 < x2) && (y1 < y2))
289
brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
292
IMB_freeImBuf(oldtexibuf);
294
/* sample texture in new areas */
295
if ((0 < x1) && (0 < ibuf->y))
296
brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
297
if ((x2 < ibuf->x) && (0 < ibuf->y))
298
brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
299
if ((x1 < x2) && (0 < y1))
300
brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
301
if ((x1 < x2) && (y2 < ibuf->y))
302
brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
305
static void brush_painter_refresh_cache(BrushPainter *painter, const float pos[2], int use_color_correction)
307
const Scene *scene = painter->scene;
308
Brush *brush = painter->brush;
309
BrushPainterCache *cache = &painter->cache;
310
MTex *mtex = &brush->mtex;
313
const int diameter = 2 * BKE_brush_size_get(scene, brush);
314
const float alpha = BKE_brush_alpha_get(scene, brush);
315
const bool do_tiled = ELEM(brush->mtex.brush_map_mode, MTEX_MAP_MODE_TILED, MTEX_MAP_MODE_3D);
317
if (diameter != cache->lastsize ||
318
alpha != cache->lastalpha ||
319
brush->jitter != cache->lastjitter)
322
IMB_freeImBuf(cache->ibuf);
325
if (cache->maskibuf) {
326
IMB_freeImBuf(cache->maskibuf);
327
cache->maskibuf = NULL;
331
size = (cache->size) ? cache->size : diameter;
334
BKE_brush_imbuf_new(scene, brush, flt, 3, size, &cache->maskibuf, use_color_correction);
335
brush_painter_tiled_tex_partial_update(painter, pos);
338
BKE_brush_imbuf_new(scene, brush, flt, 2, size, &cache->ibuf, use_color_correction);
340
cache->lastsize = diameter;
341
cache->lastalpha = alpha;
342
cache->lastjitter = brush->jitter;
344
else if (do_tiled && mtex && mtex->tex) {
345
int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
346
int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
348
if ((dx != 0) || (dy != 0))
349
brush_painter_tiled_tex_partial_update(painter, pos);
353
void BKE_brush_painter_break_stroke(BrushPainter *painter)
355
painter->firsttouch = 1;
359
int BKE_brush_painter_paint(BrushPainter *painter, BrushFunc func, const float pos[2], double time, float pressure,
360
void *user, int use_color_correction)
362
Scene *scene = painter->scene;
363
Brush *brush = painter->brush;
366
if (pressure == 0.0f) {
367
if (painter->lastpressure) // XXX - hack, operator misses
368
pressure = painter->lastpressure;
370
pressure = 1.0f; /* zero pressure == not using tablet */
372
if (painter->firsttouch) {
373
/* paint exactly once on first touch */
374
painter->startpaintpos[0] = pos[0];
375
painter->startpaintpos[1] = pos[1];
377
brush_pressure_apply(painter, brush, pressure);
378
if (painter->cache.enabled)
379
brush_painter_refresh_cache(painter, pos, use_color_correction);
380
totpaintops += func(user, painter->cache.ibuf, pos, pos);
382
painter->lasttime = time;
383
painter->firsttouch = 0;
384
painter->lastpaintpos[0] = pos[0];
385
painter->lastpaintpos[1] = pos[1];
388
else if (painter->brush->flag & BRUSH_AIRBRUSH) {
389
float spacing, step, paintpos[2], dmousepos[2], len;
390
double starttime, curtime = time;
392
/* compute brush spacing adapted to brush size */
393
spacing = brush->rate; //radius*brush->spacing * 0.01f;
395
/* setup starting time, direction vector and accumulated time */
396
starttime = painter->accumtime;
397
sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
398
len = normalize_v2(dmousepos);
399
painter->accumtime += curtime - painter->lasttime;
401
/* do paint op over unpainted time distance */
402
while (painter->accumtime >= spacing) {
403
step = (spacing - starttime) * len;
404
paintpos[0] = painter->lastmousepos[0] + dmousepos[0] * step;
405
paintpos[1] = painter->lastmousepos[1] + dmousepos[1] * step;
407
if (painter->cache.enabled)
408
brush_painter_refresh_cache(painter);
409
totpaintops += func(user, painter->cache.ibuf,
410
painter->lastpaintpos, paintpos);
412
painter->lastpaintpos[0] = paintpos[0];
413
painter->lastpaintpos[1] = paintpos[1];
414
painter->accumtime -= spacing;
415
starttime -= spacing;
418
painter->lasttime = curtime;
422
float startdistance, spacing, step, paintpos[2], dmousepos[2], finalpos[2];
424
const int radius = BKE_brush_size_get(scene, brush);
426
/* compute brush spacing adapted to brush radius, spacing may depend
427
* on pressure, so update it */
428
brush_pressure_apply(painter, brush, painter->lastpressure);
429
spacing = max_ff(1.0f, radius) * brush->spacing * 0.01f;
431
/* setup starting distance, direction vector and accumulated distance */
432
startdistance = painter->accumdistance;
433
sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
434
len = normalize_v2(dmousepos);
435
painter->accumdistance += len;
437
if (brush->flag & BRUSH_SPACE) {
438
/* do paint op over unpainted distance */
439
while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
440
step = spacing - startdistance;
441
paintpos[0] = painter->lastmousepos[0] + dmousepos[0] * step;
442
paintpos[1] = painter->lastmousepos[1] + dmousepos[1] * step;
445
press = (1.0f - t) * painter->lastpressure + t * pressure;
446
brush_pressure_apply(painter, brush, press);
447
spacing = max_ff(1.0f, radius) * brush->spacing * 0.01f;
449
BKE_brush_jitter_pos(scene, brush, paintpos, finalpos);
451
if (painter->cache.enabled)
452
brush_painter_refresh_cache(painter, finalpos, use_color_correction);
455
func(user, painter->cache.ibuf, painter->lastpaintpos, finalpos);
457
painter->lastpaintpos[0] = paintpos[0];
458
painter->lastpaintpos[1] = paintpos[1];
459
painter->accumdistance -= spacing;
460
startdistance -= spacing;
464
BKE_brush_jitter_pos(scene, brush, pos, finalpos);
466
if (painter->cache.enabled)
467
brush_painter_refresh_cache(painter, finalpos, use_color_correction);
469
totpaintops += func(user, painter->cache.ibuf, pos, finalpos);
471
painter->lastpaintpos[0] = pos[0];
472
painter->lastpaintpos[1] = pos[1];
473
painter->accumdistance = 0;
476
/* do airbrush paint ops, based on the number of paint ops left over
477
* from regular painting. this is a temporary solution until we have
478
* accurate time stamps for mouse move events */
479
if (brush->flag & BRUSH_AIRBRUSH) {
480
double curtime = time;
481
double painttime = brush->rate * totpaintops;
483
painter->accumtime += curtime - painter->lasttime;
484
if (painter->accumtime <= painttime)
485
painter->accumtime = 0.0;
487
painter->accumtime -= painttime;
489
while (painter->accumtime >= (double)brush->rate) {
490
brush_pressure_apply(painter, brush, pressure);
492
BKE_brush_jitter_pos(scene, brush, pos, finalpos);
494
if (painter->cache.enabled)
495
brush_painter_refresh_cache(painter, finalpos, use_color_correction);
498
func(user, painter->cache.ibuf, painter->lastmousepos, finalpos);
499
painter->accumtime -= (double)brush->rate;
502
painter->lasttime = curtime;
506
painter->lastmousepos[0] = pos[0];
507
painter->lastmousepos[1] = pos[1];
508
painter->lastpressure = pressure;
510
BKE_brush_alpha_set(scene, brush, painter->startalpha);
511
BKE_brush_size_set(scene, brush, painter->startsize);
512
brush->jitter = painter->startjitter;
513
brush->spacing = painter->startspacing;
519
/* TODO: should probably be unified with BrushPainter stuff? */
520
unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side)
522
unsigned int *texcache = NULL;
523
MTex *mtex = &br->mtex;
524
TexResult texres = {0};
526
int side = half_side * 2;
529
float x, y, step = 2.0 / side, co[3];
531
texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
533
/*do normalized cannonical view coords for texture*/
534
for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
535
for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
540
/* This is copied from displace modifier code */
541
hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 0, &texres, NULL);
543
/* if the texture gave an RGB value, we assume it didn't give a valid
544
* intensity, so calculate one (formula from do_material_tex).
545
* if the texture didn't give an RGB value, copy the intensity across
547
if (hasrgb & TEX_RGB)
548
texres.tin = rgb_to_grayscale(&texres.tr);
550
((char *)texcache)[(iy * side + ix) * 4] =
551
((char *)texcache)[(iy * side + ix) * 4 + 1] =
552
((char *)texcache)[(iy * side + ix) * 4 + 2] =
553
((char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(texres.tin * 255.0f);