Blender V5.3
paint_image_2d.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstring>
11
12#include "MEM_guardedalloc.h"
13
14#include "DNA_brush_types.h"
15#include "DNA_scene_types.h"
16#include "DNA_space_types.h"
17
18#include "BLI_bitmap.hh"
19#include "BLI_listbase.hh"
21#include "BLI_math_color_c.hh"
22#include "BLI_stack_c.hh"
23#include "BLI_task_c.hh"
24
25#include "BKE_brush.hh"
26#include "BKE_colorband.hh"
27#include "BKE_context.hh"
28#include "BKE_image.hh"
29#include "BKE_image_gpu.hh"
30#include "BKE_paint.hh"
31#include "BKE_paint_types.hh"
32#include "BKE_report.hh"
33
34#include "DEG_depsgraph.hh"
35
36#include "ED_paint.hh"
37#include "ED_screen.hh"
38
40#include "IMB_imbuf.hh"
41#include "IMB_imbuf_types.hh"
42
43#include "WM_api.hh"
44#include "WM_types.hh"
45
46#include "UI_view2d.hh"
47
48#include "../paint_intern.hh"
49
50namespace blender {
51
52/* Brush Painting for 2D image editor */
53
54/* Defines and Structs */
55
57 bool is_float; /* need float imbuf? */
58 bool is_data; /* is non-color data? */
59 bool is_srgb; /* is the byte colorspace sRGB? */
61 bool invert;
62
65
70
77
79
80 // int image_size[2]; /* UNUSED. */
81};
82
85 const Paint *paint;
87
88 /* Store initial starting points for perlin noise on the beginning of each stroke when using
89 * color jitter. */
90 std::optional<float3> initial_hsv_jitter;
91
92 bool firsttouch; /* first paint op */
93
94 ImagePool *pool; /* image pool */
95 rctf tex_mapping; /* texture coordinate mapping */
96 rctf mask_mapping; /* mask texture coordinate mapping */
97
99};
100
106
112
115 ImBuf *canvas = nullptr;
116 float radius_fac = 0.0f;
117 int size[2] = {};
118 float uv_origin[2] = {}; /* Stores the position of this tile in UV space. */
119 bool need_redraw = false;
121
123
124 float last_paintpos[2] = {}; /* position of last paint op */
125 float start_paintpos[2] = {}; /* position of first paint */
126};
127
149
151 const Paint *paint,
152 Brush *brush,
153 bool invert)
154{
155 BrushPainter *painter = MEM_new<BrushPainter>(__func__);
156
157 painter->brush = brush;
158 painter->scene = scene;
159 painter->paint = paint;
162 }
163 painter->firsttouch = true;
164 painter->cache_invert = invert;
165
166 return painter;
167}
168
171 bool is_float,
172 bool is_data,
173 bool is_srgb,
174 const ColorSpace *byte_colorspace,
175 bool invert)
176{
177 BrushPainterCache *cache = &tile->cache;
178
179 if (cache->is_float != is_float) {
180 if (cache->ibuf) {
181 IMB_freeImBuf(cache->ibuf);
182 }
183 if (cache->tex_mask) {
184 MEM_delete(cache->tex_mask);
185 }
186 if (cache->tex_mask_old) {
187 MEM_delete(cache->tex_mask_old);
188 }
189 cache->ibuf = nullptr;
190 cache->tex_mask = nullptr;
191 cache->lastdiameter = -1; /* force ibuf create in refresh */
192 }
193
194 cache->is_float = is_float;
195 cache->is_data = is_data;
196 cache->is_srgb = is_srgb;
197 cache->byte_colorspace = byte_colorspace;
198 cache->invert = invert;
199 cache->is_texbrush = (brush->mtex.tex &&
200 brush->image_brush_type == IMAGE_PAINT_BRUSH_TYPE_DRAW) ?
201 true :
202 false;
203 cache->is_maskbrush = (brush->mask_mtex.tex) ? true : false;
204}
205
207{
208 if (cache->ibuf) {
209 IMB_freeImBuf(cache->ibuf);
210 }
211 if (cache->texibuf) {
212 IMB_freeImBuf(cache->texibuf);
213 }
215 if (cache->tex_mask) {
216 MEM_delete(cache->tex_mask);
217 }
218 if (cache->tex_mask_old) {
219 MEM_delete(cache->tex_mask_old);
220 }
221}
222
223static void brush_imbuf_tex_co(const rctf *mapping, int x, int y, float texco[3])
224{
225 texco[0] = mapping->xmin + x * mapping->xmax;
226 texco[1] = mapping->ymin + y * mapping->ymax;
227 texco[2] = 0.0f;
228}
229
230/* create a mask with the mask texture */
231static ushort *brush_painter_mask_ibuf_new(BrushPainter *painter, const int size)
232{
233 Brush *brush = painter->brush;
234 rctf mask_mapping = painter->mask_mapping;
235 ImagePool *pool = painter->pool;
236
237 float texco[3];
238 ushort *mask, *m;
239 int x, y, thread = 0;
240
241 mask = MEM_new_array_uninitialized<ushort>(size * size, __func__);
242 m = mask;
243
244 for (y = 0; y < size; y++) {
245 for (x = 0; x < size; x++, m++) {
246 float res;
247 brush_imbuf_tex_co(&mask_mapping, x, y, texco);
248 res = BKE_brush_sample_masktex(painter->paint, brush, texco, thread, pool);
249 *m = ushort(65535.0f * res);
250 }
251 }
252
253 return mask;
254}
255
256/* update rectangular section of the brush image */
259 const ushort *tex_mask_old,
260 int origx,
261 int origy,
262 int w,
263 int h,
264 int xt,
265 int yt,
266 const int diameter)
267{
268 Brush *brush = painter->brush;
269 BrushPainterCache *cache = &tile->cache;
270 rctf tex_mapping = painter->mask_mapping;
271 ImagePool *pool = painter->pool;
272 ushort res;
273
274 bool use_texture_old = (tex_mask_old != nullptr);
275
276 int x, y, thread = 0;
277
278 ushort *tex_mask = cache->tex_mask;
279 ushort *tex_mask_cur = cache->tex_mask_old;
280
281 /* fill pixels */
282 for (y = origy; y < h; y++) {
283 for (x = origx; x < w; x++) {
284 /* sample texture */
285 float texco[3];
286
287 /* handle byte pixel */
288 ushort *b = tex_mask + (y * diameter + x);
289 ushort *t = tex_mask_cur + (y * diameter + x);
290
291 if (!use_texture_old) {
292 brush_imbuf_tex_co(&tex_mapping, x, y, texco);
293 res = ushort(65535.0f *
294 BKE_brush_sample_masktex(painter->paint, brush, texco, thread, pool));
295 }
296
297 /* read from old texture buffer */
298 if (use_texture_old) {
299 res = *(tex_mask_old + ((y - origy + yt) * cache->tex_mask_old_w + (x - origx + xt)));
300 }
301
302 /* write to new texture mask */
303 *t = res;
304 /* write to mask image buffer */
305 *b = res;
306 }
307 }
308}
309
317 const float pos[2],
318 const int diameter)
319{
320 BrushPainterCache *cache = &tile->cache;
321 ushort *tex_mask_old;
322 int destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
323
324 /* create brush image buffer if it didn't exist yet */
325 if (!cache->tex_mask) {
326 cache->tex_mask = MEM_new_array_uninitialized<ushort>(diameter * diameter, __func__);
327 }
328
329 /* create new texture image buffer with coordinates relative to old */
330 tex_mask_old = cache->tex_mask_old;
331 cache->tex_mask_old = MEM_new_array_uninitialized<ushort>(diameter * diameter, __func__);
332
333 if (tex_mask_old) {
334 ImBuf maskibuf;
335 ImBuf maskibuf_old;
336 maskibuf.x = diameter;
337 maskibuf.y = diameter;
338 maskibuf_old.x = cache->tex_mask_old_w;
339 maskibuf_old.y = cache->tex_mask_old_h;
340
341 srcx = srcy = 0;
342 w = cache->tex_mask_old_w;
343 h = cache->tex_mask_old_h;
344 destx = int(floorf(tile->last_paintpos[0])) - int(floorf(pos[0])) + (diameter / 2 - w / 2);
345 desty = int(floorf(tile->last_paintpos[1])) - int(floorf(pos[1])) + (diameter / 2 - h / 2);
346
347 /* hack, use temporary rects so that clipping works */
348 IMB_rectclip(&maskibuf, &maskibuf_old, &destx, &desty, &srcx, &srcy, &w, &h);
349 }
350 else {
351 srcx = srcy = 0;
352 destx = desty = 0;
353 w = h = 0;
354 }
355
356 x1 = min_ii(destx, diameter);
357 y1 = min_ii(desty, diameter);
358 x2 = min_ii(destx + w, diameter);
359 y2 = min_ii(desty + h, diameter);
360
361 /* blend existing texture in new position */
362 if ((x1 < x2) && (y1 < y2)) {
364 painter, tile, tex_mask_old, x1, y1, x2, y2, srcx, srcy, diameter);
365 }
366
367 if (tex_mask_old) {
368 MEM_delete(tex_mask_old);
369 }
370
371 /* sample texture in new areas */
372 if ((0 < x1) && (0 < diameter)) {
373 brush_painter_mask_imbuf_update(painter, tile, nullptr, 0, 0, x1, diameter, 0, 0, diameter);
374 }
375 if ((x2 < diameter) && (0 < diameter)) {
377 painter, tile, nullptr, x2, 0, diameter, diameter, 0, 0, diameter);
378 }
379 if ((x1 < x2) && (0 < y1)) {
380 brush_painter_mask_imbuf_update(painter, tile, nullptr, x1, 0, x2, y1, 0, 0, diameter);
381 }
382 if ((x1 < x2) && (y2 < diameter)) {
383 brush_painter_mask_imbuf_update(painter, tile, nullptr, x1, y2, x2, diameter, 0, 0, diameter);
384 }
385
386 /* through with sampling, now update sizes */
387 cache->tex_mask_old_w = diameter;
388 cache->tex_mask_old_h = diameter;
389}
390
391/* create imbuf with brush color */
393 BrushPainter *painter, ImagePaintTile *tile, const int size, float pressure, float distance)
394{
395 const Paint *paint = painter->paint;
396 Brush *brush = painter->brush;
397 BrushPainterCache *cache = &tile->cache;
398
399 rctf tex_mapping = painter->tex_mapping;
400 ImagePool *pool = painter->pool;
401
402 const bool is_float = cache->is_float;
403 const bool is_texbrush = cache->is_texbrush;
404
405 int x, y, thread = 0;
406 float brush_rgb[3];
407
408 /* allocate image buffer */
409 ImBuf *ibuf = IMB_allocImBuf(
410 size, size, (is_float) ? ImBufFlags::FloatData : ImBufFlags::ByteData);
411
412 /* get brush color */
413 if (brush->image_brush_type == IMAGE_PAINT_BRUSH_TYPE_DRAW) {
415 paint, brush, painter->initial_hsv_jitter, cache->invert, distance, pressure, brush_rgb);
416
417 if (cache->is_srgb) {
419 }
420 else if (cache->byte_colorspace) {
422 }
423 }
424 else {
425 brush_rgb[0] = 1.0f;
426 brush_rgb[1] = 1.0f;
427 brush_rgb[2] = 1.0f;
428 }
429
430 /* fill image buffer */
431 uchar *byte_data = ibuf->byte_data_for_write();
432 float *float_data = ibuf->float_data_for_write();
433 for (y = 0; y < size; y++) {
434 for (x = 0; x < size; x++) {
435 /* sample texture and multiply with brush color */
436 float3 texco;
437 float4 rgba;
438
439 if (is_texbrush) {
440 brush_imbuf_tex_co(&tex_mapping, x, y, texco);
441 const MTex *mtex = &brush->mtex;
442 BKE_brush_sample_tex_3d(painter->paint, brush, mtex, texco, rgba, thread, pool);
443 if (cache->is_srgb) {
445 }
446 else if (cache->byte_colorspace) {
448 }
449
450 mul_v3_v3(rgba, brush_rgb);
451 }
452 else {
453 copy_v3_v3(rgba, brush_rgb);
454 rgba[3] = 1.0f;
455 }
456
457 if (is_float) {
458 /* write to float pixel */
459 float *dstf = float_data + (y * size + x) * 4;
460 mul_v3_v3fl(dstf, rgba, rgba[3]); /* premultiply */
461 dstf[3] = rgba[3];
462 }
463 else {
464 /* write to byte pixel */
465 uchar *dst = byte_data + (y * size + x) * 4;
466
467 rgb_float_to_uchar(dst, rgba);
468 dst[3] = unit_float_to_uchar_clamp(rgba[3]);
469 }
470 }
471 }
472
473 return ibuf;
474}
475
476/* update rectangular section of the brush image */
479 ImBuf *oldtexibuf,
480 int origx,
481 int origy,
482 int w,
483 int h,
484 int xt,
485 int yt)
486{
487 const Paint *paint = painter->paint;
488 Brush *brush = painter->brush;
489 const MTex *mtex = &brush->mtex;
490 BrushPainterCache *cache = &tile->cache;
491
492 rctf tex_mapping = painter->tex_mapping;
493 ImagePool *pool = painter->pool;
494
495 const bool is_float = cache->is_float;
496 const bool is_texbrush = cache->is_texbrush;
497 const bool use_texture_old = (oldtexibuf != nullptr);
498
499 int x, y, thread = 0;
500 float brush_rgb[3];
501
502 ImBuf *ibuf = cache->ibuf;
503 ImBuf *texibuf = cache->texibuf;
504
505 /* get brush color */
506 if (brush->image_brush_type == IMAGE_PAINT_BRUSH_TYPE_DRAW) {
508 paint, brush, painter->initial_hsv_jitter, cache->invert, 0.0f, 1.0f, brush_rgb);
509
510 if (cache->is_srgb) {
512 }
513 else if (cache->byte_colorspace) {
515 }
516 }
517 else {
518 brush_rgb[0] = 1.0f;
519 brush_rgb[1] = 1.0f;
520 brush_rgb[2] = 1.0f;
521 }
522
523 /* fill pixels */
524 uchar *ibuf_byte_data = ibuf->byte_data_for_write();
525 float *ibuf_float_data = ibuf->float_data_for_write();
526 uchar *texibuf_byte_data = texibuf->byte_data_for_write();
527 float *texibuf_float_data = texibuf->float_data_for_write();
528 const uchar *oldtexibuf_byte_data = (oldtexibuf) ? oldtexibuf->byte_data() : nullptr;
529 const float *oldtexibuf_float_data = (oldtexibuf) ? oldtexibuf->float_data() : nullptr;
530 for (y = origy; y < h; y++) {
531 for (x = origx; x < w; x++) {
532 /* sample texture and multiply with brush color */
533 float3 texco;
534 float4 rgba;
535
536 if (!use_texture_old) {
537 if (is_texbrush) {
538 brush_imbuf_tex_co(&tex_mapping, x, y, texco);
539 BKE_brush_sample_tex_3d(painter->paint, brush, mtex, texco, rgba, thread, pool);
540 if (cache->is_srgb) {
542 }
543 else if (cache->byte_colorspace) {
545 }
546
547 mul_v3_v3(rgba, brush_rgb);
548 }
549 else {
550 copy_v3_v3(rgba, brush_rgb);
551 rgba[3] = 1.0f;
552 }
553 }
554
555 if (is_float) {
556 /* handle float pixel */
557 float *bf = ibuf_float_data + (y * ibuf->x + x) * 4;
558 float *tf = texibuf_float_data + (y * texibuf->x + x) * 4;
559
560 /* read from old texture buffer */
561 if (use_texture_old) {
562 const float *otf = oldtexibuf_float_data +
563 ((y - origy + yt) * oldtexibuf->x + (x - origx + xt)) * 4;
564 copy_v4_v4(rgba, otf);
565 }
566
567 /* write to new texture buffer */
568 copy_v4_v4(tf, rgba);
569
570 /* output premultiplied float image, mf was already premultiplied */
571 mul_v3_v3fl(bf, rgba, rgba[3]);
572 bf[3] = rgba[3];
573 }
574 else {
575 uchar crgba[4];
576
577 /* handle byte pixel */
578 uchar *b = ibuf_byte_data + (y * ibuf->x + x) * 4;
579 uchar *t = texibuf_byte_data + (y * texibuf->x + x) * 4;
580
581 /* read from old texture buffer */
582 if (use_texture_old) {
583 const uchar *ot = oldtexibuf_byte_data +
584 ((y - origy + yt) * oldtexibuf->x + (x - origx + xt)) * 4;
585 crgba[0] = ot[0];
586 crgba[1] = ot[1];
587 crgba[2] = ot[2];
588 crgba[3] = ot[3];
589 }
590 else {
591 rgba_float_to_uchar(crgba, rgba);
592 }
593
594 /* write to new texture buffer */
595 t[0] = crgba[0];
596 t[1] = crgba[1];
597 t[2] = crgba[2];
598 t[3] = crgba[3];
599
600 /* write to brush image buffer */
601 b[0] = crgba[0];
602 b[1] = crgba[1];
603 b[2] = crgba[2];
604 b[3] = crgba[3];
605 }
606 }
607 }
608}
609
610/* update the brush image by trying to reuse the cached texture result. this
611 * can be considerably faster for brushes that change size due to pressure or
612 * textures that stick to the surface where only part of the pixels are new */
615 const float pos[2],
616 const int diameter)
617{
618 BrushPainterCache *cache = &tile->cache;
619 ImBuf *oldtexibuf, *ibuf;
620 int destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
621
622 /* create brush image buffer if it didn't exist yet */
624 if (!cache->ibuf) {
625 cache->ibuf = IMB_allocImBuf(diameter, diameter, imbflag);
626 }
627 ibuf = cache->ibuf;
628
629 /* create new texture image buffer with coordinates relative to old */
630 oldtexibuf = cache->texibuf;
631 cache->texibuf = IMB_allocImBuf(diameter, diameter, imbflag);
632
633 if (oldtexibuf) {
634 srcx = srcy = 0;
635 w = oldtexibuf->x;
636 h = oldtexibuf->y;
637 destx = int(floorf(tile->last_paintpos[0])) - int(floorf(pos[0])) + (diameter / 2 - w / 2);
638 desty = int(floorf(tile->last_paintpos[1])) - int(floorf(pos[1])) + (diameter / 2 - h / 2);
639
640 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
641 }
642 else {
643 srcx = srcy = 0;
644 destx = desty = 0;
645 w = h = 0;
646 }
647
648 x1 = min_ii(destx, ibuf->x);
649 y1 = min_ii(desty, ibuf->y);
650 x2 = min_ii(destx + w, ibuf->x);
651 y2 = min_ii(desty + h, ibuf->y);
652
653 /* blend existing texture in new position */
654 if ((x1 < x2) && (y1 < y2)) {
655 brush_painter_imbuf_update(painter, tile, oldtexibuf, x1, y1, x2, y2, srcx, srcy);
656 }
657
658 if (oldtexibuf) {
659 IMB_freeImBuf(oldtexibuf);
660 }
661
662 /* sample texture in new areas */
663 if ((0 < x1) && (0 < ibuf->y)) {
664 brush_painter_imbuf_update(painter, tile, nullptr, 0, 0, x1, ibuf->y, 0, 0);
665 }
666 if ((x2 < ibuf->x) && (0 < ibuf->y)) {
667 brush_painter_imbuf_update(painter, tile, nullptr, x2, 0, ibuf->x, ibuf->y, 0, 0);
668 }
669 if ((x1 < x2) && (0 < y1)) {
670 brush_painter_imbuf_update(painter, tile, nullptr, x1, 0, x2, y1, 0, 0);
671 }
672 if ((x1 < x2) && (y2 < ibuf->y)) {
673 brush_painter_imbuf_update(painter, tile, nullptr, x1, y2, x2, ibuf->y, 0, 0);
674 }
675}
676
679 const int diameter,
680 const float pos[2],
681 const float mouse[2],
682 int mapmode,
683 rctf *r_mapping)
684{
685 float invw = 1.0f / float(tile->canvas->x);
686 float invh = 1.0f / float(tile->canvas->y);
687 float start[2];
688
689 /* find start coordinate of brush in canvas */
690 start[0] = pos[0] - diameter / 2.0f;
691 start[1] = pos[1] - diameter / 2.0f;
692
693 if (mapmode == MTEX_MAP_MODE_STENCIL) {
694 /* map from view coordinates of brush to region coordinates */
695 float xmin, ymin, xmax, ymax;
696 ui::view2d_view_to_region_fl(s->v2d, start[0] * invw, start[1] * invh, &xmin, &ymin);
698 s->v2d, (start[0] + diameter) * invw, (start[1] + diameter) * invh, &xmax, &ymax);
699
700 /* output r_mapping from brush ibuf x/y to region coordinates */
701 r_mapping->xmax = (xmax - xmin) / float(diameter);
702 r_mapping->ymax = (ymax - ymin) / float(diameter);
703 r_mapping->xmin = xmin + (tile->uv_origin[0] * tile->size[0] * r_mapping->xmax);
704 r_mapping->ymin = ymin + (tile->uv_origin[1] * tile->size[1] * r_mapping->ymax);
705 }
706 else if (mapmode == MTEX_MAP_MODE_3D) {
707 /* 3D mapping, just mapping to canvas 0..1. */
708 r_mapping->xmin = 2.0f * (start[0] * invw - 0.5f);
709 r_mapping->ymin = 2.0f * (start[1] * invh - 0.5f);
710 r_mapping->xmax = 2.0f * invw;
711 r_mapping->ymax = 2.0f * invh;
712 }
713 else if (ELEM(mapmode, MTEX_MAP_MODE_VIEW, MTEX_MAP_MODE_RANDOM)) {
714 /* view mapping */
715 r_mapping->xmin = mouse[0] - diameter * 0.5f + 0.5f;
716 r_mapping->ymin = mouse[1] - diameter * 0.5f + 0.5f;
717 r_mapping->xmax = 1.0f;
718 r_mapping->ymax = 1.0f;
719 }
720 else /* if (mapmode == MTEX_MAP_MODE_TILED) */ {
721 r_mapping->xmin = int(-diameter * 0.5) + int(floorf(pos[0])) -
722 int(floorf(tile->start_paintpos[0]));
723 r_mapping->ymin = int(-diameter * 0.5) + int(floorf(pos[1])) -
724 int(floorf(tile->start_paintpos[1]));
725 r_mapping->xmax = 1.0f;
726 r_mapping->ymax = 1.0f;
727 }
728}
729
731 BrushPainter *painter,
733 const float pos[2],
734 const float mouse[2],
735 float pressure,
736 float distance,
737 float size)
738{
739 const bke::PaintRuntime *paint_runtime = painter->paint->runtime;
740 Brush *brush = painter->brush;
741 BrushPainterCache *cache = &tile->cache;
742 /* Adding 4 pixels of padding for brush anti-aliasing. */
743 const int diameter = std::max(1, int(size * 2)) + 4;
744
745 bool do_random = false;
746 bool do_partial_update = false;
747 bool update_color = ((brush->flag & BRUSH_USE_GRADIENT) &&
748 (ELEM(brush->gradient_stroke_mode,
751 (cache->last_pressure != pressure))) ||
753 float tex_rotation = -brush->mtex.rot;
754 float mask_rotation = -brush->mask_mtex.rot;
755
756 painter->pool = BKE_image_pool_new();
757
758 /* determine how can update based on textures used */
759 if (cache->is_texbrush) {
760 if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
761 tex_rotation += paint_runtime->brush_rotation;
762 }
763 else if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_RANDOM) {
764 do_random = true;
765 }
766 else if (!((brush->stroke_method == BRUSH_STROKE_ANCHORED) || update_color)) {
767 do_partial_update = true;
768 }
769
771 s, tile, diameter, pos, mouse, brush->mtex.brush_map_mode, &painter->tex_mapping);
772 }
773
774 if (cache->is_maskbrush) {
775 bool renew_maxmask = false;
776 bool do_partial_update_mask = false;
777 /* invalidate case for all mapping modes */
778 if (brush->mask_mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
779 mask_rotation += paint_runtime->brush_rotation_sec;
780 }
781 else if (brush->mask_mtex.brush_map_mode == MTEX_MAP_MODE_RANDOM) {
782 renew_maxmask = true;
783 }
784 else if (!(brush->stroke_method == BRUSH_STROKE_ANCHORED)) {
785 do_partial_update_mask = true;
786 renew_maxmask = true;
787 }
788 /* explicitly disable partial update even if it has been enabled above */
789 if (brush->mask_pressure) {
790 do_partial_update_mask = false;
791 renew_maxmask = true;
792 }
793
794 if (diameter != cache->lastdiameter || (mask_rotation != cache->last_mask_rotation) ||
795 renew_maxmask)
796 {
797 MEM_SAFE_DELETE(cache->tex_mask);
798
800 s, tile, diameter, pos, mouse, brush->mask_mtex.brush_map_mode, &painter->mask_mapping);
801
802 if (do_partial_update_mask) {
803 brush_painter_mask_imbuf_partial_update(painter, tile, pos, diameter);
804 }
805 else {
806 cache->tex_mask = brush_painter_mask_ibuf_new(painter, diameter);
807 }
808 cache->last_mask_rotation = mask_rotation;
809 }
810 }
811
812 /* Re-initialize the curve mask. Mask is always recreated due to the change of position. */
813 paint_curve_mask_cache_update(&cache->curve_mask_cache, brush, diameter, size, pos);
814
815 /* detect if we need to recreate image brush buffer */
816 if (diameter != cache->lastdiameter || (tex_rotation != cache->last_tex_rotation) || do_random ||
817 update_color)
818 {
819 if (cache->ibuf) {
820 IMB_freeImBuf(cache->ibuf);
821 cache->ibuf = nullptr;
822 }
823
824 if (do_partial_update) {
825 /* do partial update of texture */
826 brush_painter_imbuf_partial_update(painter, tile, pos, diameter);
827 }
828 else {
829 /* create brush from scratch */
830 cache->ibuf = brush_painter_imbuf_new(painter, tile, diameter, pressure, distance);
831 }
832
833 cache->lastdiameter = diameter;
834 cache->last_tex_rotation = tex_rotation;
835 cache->last_pressure = pressure;
836 }
837 else if (do_partial_update) {
838 /* do only partial update of texture */
839 int dx = int(floorf(tile->last_paintpos[0])) - int(floorf(pos[0]));
840 int dy = int(floorf(tile->last_paintpos[1])) - int(floorf(pos[1]));
841
842 if ((dx != 0) || (dy != 0)) {
843 brush_painter_imbuf_partial_update(painter, tile, pos, diameter);
844 }
845 }
846
847 BKE_image_pool_free(painter->pool);
848 painter->pool = nullptr;
849}
850
852{
853 if (i == 0) {
854 return true;
855 }
856 if (i >= s->num_tiles) {
857 return false;
858 }
859
860 if (s->tiles[i].state == PAINT2D_TILE_READY) {
861 return true;
862 }
863 if (s->tiles[i].state == PAINT2D_TILE_MISSING) {
864 return false;
865 }
866
867 s->tiles[i].cache.lastdiameter = -1;
868
869 ImBuf *ibuf = BKE_image_acquire_ibuf(s->image, &s->tiles[i].iuser, nullptr);
870 if (ibuf != nullptr) {
871 if (ibuf->channels != 4) {
873 }
874 else if ((s->tiles[0].canvas->byte_data() && !ibuf->byte_data()) ||
875 (s->tiles[0].canvas->float_data() && !ibuf->float_data()))
876 {
878 }
879 else {
880 s->tiles[i].size[0] = ibuf->x;
881 s->tiles[i].size[1] = ibuf->y;
882 s->tiles[i].radius_fac = sqrtf((float(ibuf->x) * float(ibuf->y)) /
883 (s->tiles[0].size[0] * s->tiles[0].size[1]));
885 }
886 }
887 else {
889 }
890
891 if (s->tiles[i].state == PAINT2D_TILE_MISSING) {
892 BKE_image_release_ibuf(s->image, ibuf, nullptr);
893 return false;
894 }
895
896 s->tiles[i].canvas = ibuf;
897 return true;
898}
899
900/* keep these functions in sync */
901static void paint_2d_ibuf_rgb_get(ImBuf *ibuf, int x, int y, float r_rgb[4])
902{
903 if (ibuf->float_data()) {
904 const float *rrgbf = ibuf->float_data() + (ibuf->x * y + x) * 4;
905 copy_v4_v4(r_rgb, rrgbf);
906 }
907 else {
908 const uchar *rrgb = ibuf->byte_data() + (ibuf->x * y + x) * 4;
910 }
911}
913 ImBuf *ibuf, int x, int y, const bool is_torus, const float rgb[4])
914{
915 if (is_torus) {
916 x %= ibuf->x;
917 if (x < 0) {
918 x += ibuf->x;
919 }
920 y %= ibuf->y;
921 if (y < 0) {
922 y += ibuf->y;
923 }
924 }
925
926 if (float *float_data = ibuf->float_data_for_write()) {
927 float *rrgbf = float_data + (ibuf->x * y + x) * 4;
928 float map_alpha = (rgb[3] == 0.0f) ? rrgbf[3] : rrgbf[3] / rgb[3];
929
930 mul_v3_v3fl(rrgbf, rgb, map_alpha);
931 rrgbf[3] = rgb[3];
932 }
933 else {
934 uchar straight[4];
935 uchar *rrgb = ibuf->byte_data_for_write() + (ibuf->x * y + x) * 4;
936
937 premul_float_to_straight_uchar(straight, rgb);
938 rrgb[0] = straight[0];
939 rrgb[1] = straight[1];
940 rrgb[2] = straight[2];
941 rrgb[3] = straight[3];
942 }
943}
944
945static void paint_2d_ibuf_tile_convert(ImBuf *ibuf, int *x, int *y, short paint_tile)
946{
947 if (paint_tile & PAINT_TILE_X) {
948 *x %= ibuf->x;
949 if (*x < 0) {
950 *x += ibuf->x;
951 }
952 }
953 if (paint_tile & PAINT_TILE_Y) {
954 *y %= ibuf->y;
955 if (*y < 0) {
956 *y += ibuf->y;
957 }
958 }
959}
960
962 ImBuf *ibuf, int x, int y, float *outrgb, short paint_tile, float w)
963{
964 float inrgb[4];
965
966 if (paint_tile) {
967 paint_2d_ibuf_tile_convert(ibuf, &x, &y, paint_tile);
968 }
969 /* need to also do clipping here always since tiled coordinates
970 * are not always within bounds */
971 if (x < ibuf->x && x >= 0 && y < ibuf->y && y >= 0) {
972 paint_2d_ibuf_rgb_get(ibuf, x, y, inrgb);
973 }
974 else {
975 return 0.0f;
976 }
977
978 mul_v4_fl(inrgb, w);
979 add_v4_v4(outrgb, inrgb);
980
981 return w;
982}
983
986 ImBuf *ibuf,
987 ImBuf *ibufb,
988 const int *pos,
989 const short paint_tile)
990{
991 bool sharpen = (tile->cache.invert ^ ((s->brush->flag & BRUSH_DIR_IN) != 0));
992 float threshold = s->brush->sharp_threshold;
993 int x, y, xi, yi, xo, yo, xk, yk;
994 float count;
995 int out_off[2], in_off[2], dim[2];
996 int diff_pos[2];
997 float outrgb[4];
998 float rgba[4];
999 BlurKernel *kernel = s->blurkernel;
1000
1001 dim[0] = ibufb->x;
1002 dim[1] = ibufb->y;
1003 in_off[0] = pos[0];
1004 in_off[1] = pos[1];
1005 out_off[0] = out_off[1] = 0;
1006
1007 if (!paint_tile) {
1008 IMB_rectclip(ibuf, ibufb, &in_off[0], &in_off[1], &out_off[0], &out_off[1], &dim[0], &dim[1]);
1009
1010 if ((dim[0] == 0) || (dim[1] == 0)) {
1011 return;
1012 }
1013 }
1014
1015 /* find offset inside mask buffers to sample them */
1016 sub_v2_v2v2_int(diff_pos, out_off, in_off);
1017
1018 for (y = 0; y < dim[1]; y++) {
1019 for (x = 0; x < dim[0]; x++) {
1020 /* get input pixel */
1021 xi = in_off[0] + x;
1022 yi = in_off[1] + y;
1023
1024 count = 0.0;
1025 if (paint_tile) {
1026 paint_2d_ibuf_tile_convert(ibuf, &xi, &yi, paint_tile);
1027 if (xi < ibuf->x && xi >= 0 && yi < ibuf->y && yi >= 0) {
1028 paint_2d_ibuf_rgb_get(ibuf, xi, yi, rgba);
1029 }
1030 else {
1031 zero_v4(rgba);
1032 }
1033 }
1034 else {
1035 /* coordinates have been clipped properly here, it should be safe to do this */
1036 paint_2d_ibuf_rgb_get(ibuf, xi, yi, rgba);
1037 }
1038 zero_v4(outrgb);
1039
1040 for (yk = 0; yk < kernel->side; yk++) {
1041 for (xk = 0; xk < kernel->side; xk++) {
1042 count += paint_2d_ibuf_add_if(ibuf,
1043 xi + xk - kernel->pixel_len,
1044 yi + yk - kernel->pixel_len,
1045 outrgb,
1046 paint_tile,
1047 kernel->wdata[xk + yk * kernel->side]);
1048 }
1049 }
1050
1051 if (count > 0.0f) {
1052 mul_v4_fl(outrgb, 1.0f / count);
1053
1054 if (sharpen) {
1055 /* subtract blurred image from normal image gives high pass filter */
1056 sub_v3_v3v3(outrgb, rgba, outrgb);
1057
1058 /* Now rgba_ub contains the edge result, but this should be converted to luminance to
1059 * avoid colored speckles appearing in final image, and also to check for threshold. */
1060 outrgb[0] = outrgb[1] = outrgb[2] = IMB_colormanagement_get_luminance(outrgb);
1061 if (fabsf(outrgb[0]) > threshold) {
1062 float mask = BKE_brush_alpha_get(s->paint, s->brush);
1063 float alpha = rgba[3];
1064 rgba[3] = outrgb[3] = mask;
1065
1066 /* add to enhance edges */
1067 blend_color_add_float(outrgb, rgba, outrgb);
1068 outrgb[3] = alpha;
1069 }
1070 else {
1071 copy_v4_v4(outrgb, rgba);
1072 }
1073 }
1074 }
1075 else {
1076 copy_v4_v4(outrgb, rgba);
1077 }
1078 /* write into brush buffer */
1079 xo = out_off[0] + x;
1080 yo = out_off[1] + y;
1081 paint_2d_ibuf_rgb_set(ibufb, xo, yo, false, outrgb);
1082 }
1083 }
1084}
1085
1087 ImagePaintRegion *region, int destx, int desty, int srcx, int srcy, int width, int height)
1088{
1089 region->destx = destx;
1090 region->desty = desty;
1091 region->srcx = srcx;
1092 region->srcy = srcy;
1093 region->width = width;
1094 region->height = height;
1095}
1096
1098 ImBuf *dbuf,
1099 const ImBuf *sbuf,
1100 short paint_tile)
1101{
1102 int destx = region->destx;
1103 int desty = region->desty;
1104 int srcx = region->srcx;
1105 int srcy = region->srcy;
1106 int width = region->width;
1107 int height = region->height;
1108 int origw, origh, w, h, tot = 0;
1109
1110 /* convert destination and source coordinates to be within image */
1111 if (paint_tile & PAINT_TILE_X) {
1112 destx = destx % dbuf->x;
1113 if (destx < 0) {
1114 destx += dbuf->x;
1115 }
1116 srcx = srcx % sbuf->x;
1117 if (srcx < 0) {
1118 srcx += sbuf->x;
1119 }
1120 }
1121 if (paint_tile & PAINT_TILE_Y) {
1122 desty = desty % dbuf->y;
1123 if (desty < 0) {
1124 desty += dbuf->y;
1125 }
1126 srcy = srcy % sbuf->y;
1127 if (srcy < 0) {
1128 srcy += sbuf->y;
1129 }
1130 }
1131 /* clip width of blending area to destination imbuf, to avoid writing the
1132 * same pixel twice */
1133 origw = w = (width > dbuf->x) ? dbuf->x : width;
1134 origh = h = (height > dbuf->y) ? dbuf->y : height;
1135
1136 /* clip within image */
1137 IMB_rectclip(dbuf, sbuf, &destx, &desty, &srcx, &srcy, &w, &h);
1138 paint_2d_set_region(&region[tot++], destx, desty, srcx, srcy, w, h);
1139
1140 /* do 3 other rects if needed */
1141 if ((paint_tile & PAINT_TILE_X) && w < origw) {
1143 &region[tot++], (destx + w) % dbuf->x, desty, (srcx + w) % sbuf->x, srcy, origw - w, h);
1144 }
1145 if ((paint_tile & PAINT_TILE_Y) && h < origh) {
1147 &region[tot++], destx, (desty + h) % dbuf->y, srcx, (srcy + h) % sbuf->y, w, origh - h);
1148 }
1149 if ((paint_tile & PAINT_TILE_X) && (paint_tile & PAINT_TILE_Y) && (w < origw) && (h < origh)) {
1150 paint_2d_set_region(&region[tot++],
1151 (destx + w) % dbuf->x,
1152 (desty + h) % dbuf->y,
1153 (srcx + w) % sbuf->x,
1154 (srcy + h) % sbuf->y,
1155 origw - w,
1156 origh - h);
1157 }
1158
1159 return tot;
1160}
1161
1162static void paint_2d_lift_smear(const ImBuf *ibuf, ImBuf *ibufb, int *pos, short paint_tile)
1163{
1164 ImagePaintRegion region[4];
1165 int a, tot;
1166
1167 paint_2d_set_region(region, 0, 0, pos[0], pos[1], ibufb->x, ibufb->y);
1168 tot = paint_2d_torus_split_region(region, ibufb, ibuf, paint_tile);
1169
1170 for (a = 0; a < tot; a++) {
1171 IMB_copy_rect(ibufb,
1172 ibuf,
1173 int2(region[a].srcx, region[a].srcy),
1174 int2(region[a].destx, region[a].desty),
1175 int2(region[a].width, region[a].height));
1176 }
1177}
1178
1179static ImBuf *paint_2d_lift_clone(ImBuf *ibuf, ImBuf *ibufb, const int *pos)
1180{
1181 /* NOTE: #allocImbuf returns zeroed memory, so regions outside image will
1182 * have zero alpha, and hence not be blended onto the image */
1183 int w = ibufb->x, h = ibufb->y, destx = 0, desty = 0, srcx = pos[0], srcy = pos[1];
1184 ImBufFlags ibflags = ibufb->flags;
1185 if (ibufb->byte_data()) {
1186 ibflags |= ImBufFlags::ByteData;
1187 }
1188 if (ibufb->float_data()) {
1189 ibflags |= ImBufFlags::FloatData;
1190 }
1191 ImBuf *clonebuf = IMB_allocImBuf(w, h, ibflags);
1192 clonebuf->color_mode = ibufb->color_mode;
1193
1194 IMB_rectclip(clonebuf, ibuf, &destx, &desty, &srcx, &srcy, &w, &h);
1195
1196 uint8_t *clonebuf_byte_data = clonebuf->byte_data_for_write();
1197 float *clonebuf_float_data = clonebuf->float_data_for_write();
1198
1199 IMB_rectblend(clonebuf,
1200 clonebuf_byte_data,
1201 clonebuf_float_data,
1202 clonebuf,
1203 ibufb,
1204 nullptr,
1205 nullptr,
1206 nullptr,
1207 0,
1208 destx,
1209 desty,
1210 destx,
1211 desty,
1212 destx,
1213 desty,
1214 w,
1215 h,
1217 false);
1218 IMB_rectblend(clonebuf,
1219 clonebuf_byte_data,
1220 clonebuf_float_data,
1221 clonebuf,
1222 ibuf,
1223 nullptr,
1224 nullptr,
1225 nullptr,
1226 0,
1227 destx,
1228 desty,
1229 destx,
1230 desty,
1231 srcx,
1232 srcy,
1233 w,
1234 h,
1236 false);
1237
1238 return clonebuf;
1239}
1240
1241static void paint_2d_convert_brushco(ImBuf *ibufb, const float pos[2], int ipos[2])
1242{
1243 ipos[0] = int(floorf(pos[0] - ibufb->x / 2));
1244 ipos[1] = int(floorf(pos[1] - ibufb->y / 2));
1245}
1246
1249 ImagePaintRegion *region,
1250 uint8_t *canvas_byte_data,
1251 float *canvas_float_data,
1252 ImBuf *frombuf,
1253 float mask_max,
1254 short blend,
1255 int tilex,
1256 int tiley,
1257 int tilew,
1258 int tileh)
1259{
1260 ImBuf tmpbuf;
1262
1264
1265 for (int ty = tiley; ty <= tileh; ty++) {
1266 for (int tx = tilex; tx <= tilew; tx++) {
1267 /* retrieve original pixels + mask from undo buffer */
1268 ushort *mask;
1269 int origx = region->destx - tx * ED_IMAGE_UNDO_TILE_SIZE;
1270 int origy = region->desty - ty * ED_IMAGE_UNDO_TILE_SIZE;
1271
1273 undo_tiles, s->image, tile->canvas, &tile->iuser, tx, ty, &mask, false);
1274 if (data == nullptr) {
1275 continue;
1276 }
1277 if (tile->canvas->float_data()) {
1278 tmpbuf.float_buffer = data->float_buffer;
1279 }
1280 else {
1281 tmpbuf.byte_buffer = data->byte_buffer;
1282 }
1283
1284 IMB_rectblend(tile->canvas,
1285 canvas_byte_data,
1286 canvas_float_data,
1287 &tmpbuf,
1288 frombuf,
1289 mask,
1290 tile->cache.curve_mask_cache.curve_mask,
1291 tile->cache.tex_mask,
1292 mask_max,
1293 region->destx,
1294 region->desty,
1295 origx,
1296 origy,
1297 region->srcx,
1298 region->srcy,
1299 region->width,
1300 region->height,
1301 IMB_BlendMode(blend),
1302 ((s->brush->flag & BRUSH_ACCUMULATE) != 0));
1303 }
1304 }
1305}
1306
1319
1320static void paint_2d_op_foreach_do(void *__restrict data_v,
1321 const int iter,
1322 const TaskParallelTLS *__restrict /*tls*/)
1323{
1324 Paint2DForeachData *data = static_cast<Paint2DForeachData *>(data_v);
1326 data->tile,
1327 data->region,
1328 data->canvas_byte_data,
1329 data->canvas_float_data,
1330 data->frombuf,
1331 data->mask_max,
1332 data->blend,
1333 data->tilex,
1334 iter,
1335 data->tilew,
1336 iter);
1337}
1338
1339static int paint_2d_op(void *state,
1341 const float lastpos[2],
1342 const float pos[2])
1343{
1344 ImagePaintState *s = (static_cast<ImagePaintState *>(state));
1345 const ImagePaintSettings &image_paint_settings = s->scene->toolsettings->imapaint;
1346 ImBuf *clonebuf = nullptr, *frombuf;
1347 ImBuf *canvas = tile->canvas;
1348 ImBuf *ibufb = tile->cache.ibuf;
1349 ImagePaintRegion region[4];
1350 short paint_tile = s->symmetry & (PAINT_TILE_X | PAINT_TILE_Y);
1351 short blend = s->blend;
1352 const float *offset = image_paint_settings.clone_offset;
1353 float liftpos[2];
1354 float mask_max = BKE_brush_alpha_get(s->paint, s->brush);
1355 int bpos[2], blastpos[2], bliftpos[2];
1356 int a, tot;
1357
1358 paint_2d_convert_brushco(ibufb, pos, bpos);
1359
1360 /* lift from canvas */
1362 paint_2d_lift_soften(s, tile, canvas, ibufb, bpos, paint_tile);
1363 blend = IMB_BLEND_INTERPOLATE;
1364 }
1366 if (lastpos[0] == pos[0] && lastpos[1] == pos[1]) {
1367 return 0;
1368 }
1369
1370 paint_2d_convert_brushco(ibufb, lastpos, blastpos);
1371 paint_2d_lift_smear(canvas, ibufb, blastpos, paint_tile);
1372 blend = IMB_BLEND_INTERPOLATE;
1373 }
1375 liftpos[0] = pos[0] - offset[0] * canvas->x;
1376 liftpos[1] = pos[1] - offset[1] * canvas->y;
1377
1378 paint_2d_convert_brushco(ibufb, liftpos, bliftpos);
1379 clonebuf = paint_2d_lift_clone(s->clonecanvas, ibufb, bliftpos);
1380 }
1381
1382 frombuf = (clonebuf) ? clonebuf : ibufb;
1383
1384 if (paint_tile) {
1385 paint_2d_set_region(region, bpos[0], bpos[1], 0, 0, frombuf->x, frombuf->y);
1386 tot = paint_2d_torus_split_region(region, canvas, frombuf, paint_tile);
1387 }
1388 else {
1389 paint_2d_set_region(region, bpos[0], bpos[1], 0, 0, frombuf->x, frombuf->y);
1390 tot = 1;
1391 }
1392
1393 /* blend into canvas */
1394 for (a = 0; a < tot; a++) {
1396 canvas,
1397 &tile->iuser,
1398 region[a].destx,
1399 region[a].desty,
1400 region[a].width,
1401 region[a].height);
1402
1403 if (s->do_masking) {
1404 /* masking, find original pixels tiles from undo buffer to composite over */
1405 int tilex, tiley, tilew, tileh;
1406
1408 region[a].destx,
1409 region[a].desty,
1410 region[a].width,
1411 region[a].height,
1412 &tilex,
1413 &tiley,
1414 &tilew,
1415 &tileh);
1416
1417 /* Acquire mutable data pointers outside of parallel loop. */
1418 uint8_t *canvas_byte_data = canvas->byte_data_for_write();
1419 float *canvas_float_data = canvas->float_data_for_write();
1420
1421 if (tiley == tileh) {
1423 tile,
1424 &region[a],
1425 canvas_byte_data,
1426 canvas_float_data,
1427 frombuf,
1428 mask_max,
1429 blend,
1430 tilex,
1431 tiley,
1432 tilew,
1433 tileh);
1434 }
1435 else {
1437 data.s = s;
1438 data.tile = tile;
1439 data.region = &region[a];
1440 data.canvas_byte_data = canvas_byte_data;
1441 data.canvas_float_data = canvas_float_data;
1442 data.frombuf = frombuf;
1443 data.mask_max = mask_max;
1444 data.blend = blend;
1445 data.tilex = tilex;
1446 data.tilew = tilew;
1447
1448 TaskParallelSettings settings;
1450 BLI_task_parallel_range(tiley, tileh + 1, &data, paint_2d_op_foreach_do, &settings);
1451 }
1452 }
1453 else {
1454 /* no masking, composite brush directly onto canvas */
1456 canvas,
1457 frombuf,
1458 nullptr,
1459 tile->cache.curve_mask_cache.curve_mask,
1460 tile->cache.tex_mask,
1461 mask_max,
1462 region[a].destx,
1463 region[a].desty,
1464 region[a].destx,
1465 region[a].desty,
1466 region[a].srcx,
1467 region[a].srcy,
1468 region[a].width,
1469 region[a].height,
1470 IMB_BlendMode(blend),
1471 false);
1472 }
1473 }
1474
1475 if (clonebuf) {
1476 IMB_freeImBuf(clonebuf);
1477 }
1478
1479 return 1;
1480}
1481
1483{
1484 /* set clone canvas */
1486 const ImagePaintSettings &image_paint_settings = s->scene->toolsettings->imapaint;
1487 Image *ima = image_paint_settings.clone;
1488 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, nullptr, nullptr);
1489
1490 if (!ima || !ibuf || !(ibuf->byte_data() || ibuf->float_data())) {
1491 BKE_image_release_ibuf(ima, ibuf, nullptr);
1492 return 0;
1493 }
1494
1495 s->clonecanvas = ibuf;
1496
1497 /* temporarily add float rect for cloning */
1498 if (s->tiles[0].canvas->float_data() && !s->clonecanvas->float_data()) {
1500 }
1501 else if (!s->tiles[0].canvas->float_data() && !s->clonecanvas->byte_data()) {
1503 }
1504 }
1505
1506 /* set masking */
1508
1509 return 1;
1510}
1511
1513{
1514 for (int i = 0; i < s->num_tiles; i++) {
1515 BKE_image_release_ibuf(s->image, s->tiles[i].canvas, nullptr);
1516 }
1517 const ImagePaintSettings &image_paint_settings = s->scene->toolsettings->imapaint;
1518 BKE_image_release_ibuf(image_paint_settings.clone, s->clonecanvas, nullptr);
1519
1520 if (s->blurkernel) {
1522 MEM_delete(s->blurkernel);
1523 }
1524}
1525
1526static void paint_2d_transform_mouse(View2D *v2d, const float in[2], float out[2])
1527{
1528 ui::view2d_region_to_view(v2d, in[0], in[1], &out[0], &out[1]);
1529}
1530
1531static bool is_inside_tile(const int size[2], const float pos[2], const float brush[2])
1532{
1533 return (pos[0] >= -brush[0]) && (pos[0] < size[0] + brush[0]) && (pos[1] >= -brush[1]) &&
1534 (pos[1] < size[1] + brush[1]);
1535}
1536
1537static void paint_2d_uv_to_coord(ImagePaintTile *tile, const float uv[2], float coord[2])
1538{
1539 coord[0] = (uv[0] - tile->uv_origin[0]) * tile->size[0];
1540 coord[1] = (uv[1] - tile->uv_origin[1]) * tile->size[1];
1541}
1542
1543void paint_2d_stroke(void *ps,
1544 const float prev_mval[2],
1545 const float mval[2],
1546 const bool eraser,
1547 float pressure,
1548 float distance,
1549 float base_size)
1550{
1551 float new_uv[2], old_uv[2];
1552 ImagePaintState *s = static_cast<ImagePaintState *>(ps);
1553 BrushPainter *painter = s->painter;
1554
1555 s->blend = s->brush->blend;
1556 if (eraser) {
1558 }
1559
1560 ui::view2d_region_to_view(s->v2d, mval[0], mval[1], &new_uv[0], &new_uv[1]);
1561 ui::view2d_region_to_view(s->v2d, prev_mval[0], prev_mval[1], &old_uv[0], &old_uv[1]);
1562
1563 float last_uv[2], start_uv[2];
1564 ui::view2d_region_to_view(s->v2d, 0.0f, 0.0f, &start_uv[0], &start_uv[1]);
1565 if (painter->firsttouch) {
1566 /* paint exactly once on first touch */
1567 copy_v2_v2(last_uv, new_uv);
1568 }
1569 else {
1570 copy_v2_v2(last_uv, old_uv);
1571 }
1572
1573 const float uv_brush_size[2] = {
1574 (s->symmetry & PAINT_TILE_X) ? FLT_MAX : base_size / s->tiles[0].size[0],
1575 (s->symmetry & PAINT_TILE_Y) ? FLT_MAX : base_size / s->tiles[0].size[1]};
1576
1577 for (int i = 0; i < s->num_tiles; i++) {
1578 ImagePaintTile *tile = &s->tiles[i];
1579
1580 /* First test: Project brush into UV space, clip against tile. */
1581 const int uv_size[2] = {1, 1};
1582 float local_new_uv[2], local_old_uv[2];
1583 sub_v2_v2v2(local_new_uv, new_uv, tile->uv_origin);
1584 sub_v2_v2v2(local_old_uv, old_uv, tile->uv_origin);
1585 if (!(is_inside_tile(uv_size, local_new_uv, uv_brush_size) ||
1586 is_inside_tile(uv_size, local_old_uv, uv_brush_size)))
1587 {
1588 continue;
1589 }
1590
1591 /* Lazy tile loading to get size in pixels. */
1592 if (!paint_2d_ensure_tile_canvas(s, i)) {
1593 continue;
1594 }
1595
1596 float size = base_size * tile->radius_fac;
1597
1598 float new_coord[2], old_coord[2];
1599 paint_2d_uv_to_coord(tile, new_uv, new_coord);
1600 paint_2d_uv_to_coord(tile, old_uv, old_coord);
1601 if (painter->firsttouch) {
1602 paint_2d_uv_to_coord(tile, start_uv, tile->start_paintpos);
1603 }
1604 paint_2d_uv_to_coord(tile, last_uv, tile->last_paintpos);
1605
1606 /* Second check in pixel coordinates. */
1607 const float pixel_brush_size[] = {(s->symmetry & PAINT_TILE_X) ? FLT_MAX : size,
1608 (s->symmetry & PAINT_TILE_Y) ? FLT_MAX : size};
1609 if (!(is_inside_tile(tile->size, new_coord, pixel_brush_size) ||
1610 is_inside_tile(tile->size, old_coord, pixel_brush_size)))
1611 {
1612 continue;
1613 }
1614
1615 ImBuf *ibuf = tile->canvas;
1616
1617 const bool is_data = ibuf->colorspace_is_data();
1618 const bool is_float = (ibuf->float_data() != nullptr);
1619 const ColorSpace *byte_colorspace = (is_float || is_data) ? nullptr :
1620 ibuf->byte_buffer.colorspace;
1621 const bool is_srgb = (is_float || is_data) ?
1622 false :
1623 IMB_colormanagement_space_is_srgb(byte_colorspace);
1624
1625 /* OCIO_TODO: float buffers are now always linear, so always use color correction
1626 * this should probably be changed when texture painting color space is supported
1627 */
1629 tile,
1630 (ibuf->float_data() != nullptr),
1631 is_data,
1632 is_srgb,
1633 byte_colorspace,
1634 painter->cache_invert);
1635
1636 brush_painter_2d_refresh_cache(s, painter, tile, new_coord, mval, pressure, distance, size);
1637
1638 if (paint_2d_op(s, tile, old_coord, new_coord)) {
1639 tile->need_redraw = true;
1640 }
1641 }
1642
1643 painter->firsttouch = false;
1644}
1645
1647{
1648 Scene *scene = CTX_data_scene(C);
1650 ToolSettings *settings = scene->toolsettings;
1652 Brush *brush = BKE_paint_brush(&settings->imapaint.paint);
1653
1655
1657 s->v2d = &CTX_wm_region(C)->v2d;
1658 s->scene = scene;
1659 s->paint = paint;
1660
1661 s->brush = brush;
1662 s->brush_type = brush->image_brush_type;
1663 s->blend = brush->blend;
1664
1665 s->image = s->sima->image;
1666 s->symmetry = settings->imapaint.paint.symmetry_flags;
1667
1668 if (s->image == nullptr) {
1669 MEM_delete(s);
1670 return nullptr;
1671 }
1672 if (BKE_image_has_packedfile(s->image) && s->image->rr != nullptr) {
1673 BKE_report(op->reports, RPT_WARNING, "Packed MultiLayer files cannot be painted");
1674 MEM_delete(s);
1675 return nullptr;
1676 }
1677
1678 s->num_tiles = s->image->tiles.count();
1679 s->tiles = MEM_new_array<ImagePaintTile>(s->num_tiles, __func__);
1680 for (int i = 0; i < s->num_tiles; i++) {
1681 s->tiles[i].iuser = sima->iuser;
1682 }
1683
1684 zero_v2(s->tiles[0].uv_origin);
1685
1686 ImBuf *ibuf = BKE_image_acquire_ibuf(s->image, &s->tiles[0].iuser, nullptr);
1687 if (ibuf == nullptr) {
1688 MEM_delete(s->tiles);
1689 MEM_delete(s);
1690 return nullptr;
1691 }
1692
1693 if (ibuf->channels != 4) {
1694 BKE_image_release_ibuf(s->image, ibuf, nullptr);
1695 BKE_report(op->reports, RPT_WARNING, "Image requires 4 color channels to paint");
1696 MEM_delete(s->tiles);
1697 MEM_delete(s);
1698 return nullptr;
1699 }
1700
1701 s->tiles[0].size[0] = ibuf->x;
1702 s->tiles[0].size[1] = ibuf->y;
1703 s->tiles[0].radius_fac = 1.0f;
1704
1705 s->tiles[0].canvas = ibuf;
1707
1708 /* Initialize offsets here, they're needed for the uv space clip test before lazy-loading the
1709 * tile properly. */
1710 int tile_idx = 0;
1711 for (ImageTile *tile = s->image->tiles.first(); tile; tile = tile->next, tile_idx++) {
1712 s->tiles[tile_idx].iuser.tile = tile->tile_number;
1713 s->tiles[tile_idx].uv_origin[0] = ((tile->tile_number - 1001) % 10);
1714 s->tiles[tile_idx].uv_origin[1] = ((tile->tile_number - 1001) / 10);
1715 }
1716
1717 if (!paint_2d_canvas_set(s, paint)) {
1718 MEM_delete(s->tiles);
1719
1720 MEM_delete(s);
1721 return nullptr;
1722 }
1723
1724 if (brush->image_brush_type == IMAGE_PAINT_BRUSH_TYPE_SOFTEN) {
1726 }
1727
1729
1730 /* create painter */
1732
1733 return s;
1734}
1735
1736void paint_2d_redraw(const bContext *C, void *ps, bool final)
1737{
1738 ImagePaintState *s = static_cast<ImagePaintState *>(ps);
1739
1740 bool had_redraw = false;
1741 for (int i = 0; i < s->num_tiles; i++) {
1742 if (s->tiles[i].need_redraw) {
1743 ImBuf *ibuf = BKE_image_acquire_ibuf(s->image, &s->tiles[i].iuser, nullptr);
1744
1746
1747 BKE_image_release_ibuf(s->image, ibuf, nullptr);
1748
1749 s->tiles[i].need_redraw = false;
1750 had_redraw = true;
1751 }
1752 }
1753
1754 if (had_redraw) {
1756 if (s->sima == nullptr || !s->sima->lock) {
1758 }
1759 else {
1761 }
1762 }
1763
1764 if (final) {
1765 /* compositor listener deals with updating */
1767 DEG_id_tag_update(&s->image->id, 0);
1768
1769 /* Ideally, we shouldn't have to tag the object as needing to be recalculated if using this
1770 * paint mode, however, because the image isn't connected as part of the shader nodes, the draw
1771 * code is unaware of the corresponding image tag. See #150957 for more details. */
1772 const Scene *scene = CTX_data_scene(C);
1773 Object *object = CTX_data_active_object(C);
1774 if (object && object->type == OB_MESH && scene &&
1776 {
1778 }
1779 }
1780}
1781
1783{
1784 ImagePaintState *s = static_cast<ImagePaintState *>(ps);
1785
1787 for (int i = 0; i < s->num_tiles; i++) {
1789 }
1790 MEM_delete(s->painter);
1791 MEM_delete(s->tiles);
1793
1794 MEM_delete(s);
1795 WM_paint_cursor_end(cursor);
1796}
1797
1798static void paint_2d_fill_add_pixel_byte(const int x_px,
1799 const int y_px,
1800 ImBuf *ibuf,
1801 BLI_Stack *stack,
1802 BLI_bitmap *touched,
1803 const float color[4],
1804 float threshold_sq)
1805{
1806 size_t coordinate;
1807
1808 if (x_px >= ibuf->x || x_px < 0 || y_px >= ibuf->y || y_px < 0) {
1809 return;
1810 }
1811
1812 coordinate = size_t(y_px) * ibuf->x + x_px;
1813
1814 if (!BLI_BITMAP_TEST(touched, coordinate)) {
1815 float color_f[4];
1816 const uchar *color_b = ibuf->byte_data() + 4 * coordinate;
1817 rgba_uchar_to_float(color_f, color_b);
1818 straight_to_premul_v4(color_f);
1819
1820 if (len_squared_v4v4(color_f, color) <= threshold_sq) {
1821 BLI_stack_push(stack, &coordinate);
1822 }
1823 BLI_BITMAP_SET(touched, coordinate, true);
1824 }
1825}
1826
1827static void paint_2d_fill_add_pixel_float(const int x_px,
1828 const int y_px,
1829 ImBuf *ibuf,
1830 BLI_Stack *stack,
1831 BLI_bitmap *touched,
1832 const float color[4],
1833 float threshold_sq)
1834{
1835 size_t coordinate;
1836
1837 if (x_px >= ibuf->x || x_px < 0 || y_px >= ibuf->y || y_px < 0) {
1838 return;
1839 }
1840
1841 coordinate = size_t(y_px) * ibuf->x + x_px;
1842
1843 if (!BLI_BITMAP_TEST(touched, coordinate)) {
1844 if (len_squared_v4v4(ibuf->float_data() + 4 * coordinate, color) <= threshold_sq) {
1845 BLI_stack_push(stack, &coordinate);
1846 }
1847 BLI_BITMAP_SET(touched, coordinate, true);
1848 }
1849}
1850
1852{
1853 ImageUser *iuser = &s->tiles[0].iuser;
1854 for (int i = 0; i < s->num_tiles; i++) {
1855 if (s->tiles[i].iuser.tile == tile_number) {
1856 if (!paint_2d_ensure_tile_canvas(s, i)) {
1857 return nullptr;
1858 }
1859 iuser = &s->tiles[i].iuser;
1860 break;
1861 }
1862 }
1863
1864 return iuser;
1865}
1866
1868 const float color[3],
1869 Brush *br,
1870 const float mouse_init[2],
1871 const float mouse_final[2],
1872 void *ps)
1873{
1876 Image *ima = sima->image;
1877
1878 ImagePaintState *s = static_cast<ImagePaintState *>(ps);
1879
1880 ImBuf *ibuf;
1881 int x_px, y_px;
1882 uint color_b;
1883 float color_f[4];
1884 float strength = (s && br) ? BKE_brush_alpha_get(paint, br) : 1.0f;
1885
1886 bool do_float;
1887
1888 if (!ima) {
1889 return;
1890 }
1891
1892 View2D *v2d = s ? s->v2d : &CTX_wm_region(C)->v2d;
1893 float uv_origin[2];
1894 float image_init[2];
1895 paint_2d_transform_mouse(v2d, mouse_init, image_init);
1896
1897 int tile_number = BKE_image_get_tile_from_pos(ima, image_init, image_init, uv_origin);
1898
1899 ImageUser local_iuser, *iuser;
1900 if (s != nullptr) {
1901 iuser = paint_2d_get_tile_iuser(s, tile_number);
1902 if (iuser == nullptr) {
1903 return;
1904 }
1905 }
1906 else {
1907 iuser = &local_iuser;
1908 BKE_imageuser_default(iuser);
1909 iuser->tile = tile_number;
1910 }
1911
1912 ibuf = BKE_image_acquire_ibuf(ima, iuser, nullptr);
1913 if (!ibuf) {
1914 return;
1915 }
1916
1917 do_float = (ibuf->float_data() != nullptr);
1918 /* First check if our image is float. If it is we should correct the color to be in linear space.
1919 */
1920 if (!do_float) {
1921 float3 ibuf_color = color;
1923 rgb_float_to_uchar(reinterpret_cast<uchar *>(&color_b), ibuf_color);
1924 *((reinterpret_cast<char *>(&color_b)) + 3) = strength * 255;
1925 }
1926 else {
1927 copy_v3_v3(color_f, color);
1928 color_f[3] = strength;
1929 }
1930
1931 if (!mouse_final || !br) {
1932 /* first case, no image UV, fill the whole image */
1933 ED_imapaint_dirty_region(ima, ibuf, iuser, 0, 0, ibuf->x, ibuf->y);
1934
1935 if (do_float) {
1936 float *float_data = ibuf->float_data_for_write();
1937 for (x_px = 0; x_px < ibuf->x; x_px++) {
1938 for (y_px = 0; y_px < ibuf->y; y_px++) {
1939 blend_color_mix_float(float_data + 4 * (size_t(y_px) * ibuf->x + x_px),
1940 float_data + 4 * (size_t(y_px) * ibuf->x + x_px),
1941 color_f);
1942 }
1943 }
1944 }
1945 else {
1946 uchar *byte_data = ibuf->byte_data_for_write();
1947 for (x_px = 0; x_px < ibuf->x; x_px++) {
1948 for (y_px = 0; y_px < ibuf->y; y_px++) {
1949 blend_color_mix_byte(byte_data + 4 * (size_t(y_px) * ibuf->x + x_px),
1950 byte_data + 4 * (size_t(y_px) * ibuf->x + x_px),
1951 reinterpret_cast<uchar *>(&color_b));
1952 }
1953 }
1954 }
1955 }
1956 else {
1957 /* second case, start sweeping the neighboring pixels, looking for pixels whose
1958 * value is within the brush fill threshold from the fill color */
1959 BLI_Stack *stack;
1960 BLI_bitmap *touched;
1961 size_t coordinate;
1962 int width = ibuf->x;
1963 float pixel_color[4];
1964 /* We are comparing to sum of three squared values
1965 * (assumed in range [0,1]), so need to multiply... */
1966 float threshold_sq = br->fill_threshold * br->fill_threshold * 3;
1967
1968 x_px = image_init[0] * ibuf->x;
1969 y_px = image_init[1] * ibuf->y;
1970
1971 if (x_px >= ibuf->x || x_px < 0 || y_px > ibuf->y || y_px < 0) {
1972 BKE_image_release_ibuf(ima, ibuf, nullptr);
1973 return;
1974 }
1975
1976 /* change image invalidation method later */
1977 ED_imapaint_dirty_region(ima, ibuf, iuser, 0, 0, ibuf->x, ibuf->y);
1978
1979 stack = BLI_stack_new(sizeof(size_t), __func__);
1980 touched = BLI_BITMAP_NEW(size_t(ibuf->x) * ibuf->y, "bucket_fill_bitmap");
1981
1982 coordinate = (size_t(y_px) * ibuf->x + x_px);
1983
1984 if (do_float) {
1985 copy_v4_v4(pixel_color, ibuf->float_data() + 4 * coordinate);
1986 }
1987 else {
1988 const uchar *pixel_color_b = ibuf->byte_data() + 4 * coordinate;
1989 rgba_uchar_to_float(pixel_color, pixel_color_b);
1990 straight_to_premul_v4(pixel_color);
1991 }
1992
1993 BLI_stack_push(stack, &coordinate);
1994 BLI_BITMAP_SET(touched, coordinate, true);
1995
1996 if (do_float) {
1997 while (!BLI_stack_is_empty(stack)) {
1998 BLI_stack_pop(stack, &coordinate);
1999
2000 IMB_blend_color_float(ibuf->float_data_for_write() + 4 * (coordinate),
2001 ibuf->float_data_for_write() + 4 * (coordinate),
2002 color_f,
2003 IMB_BlendMode(br->blend));
2004
2005 /* reconstruct the coordinates here */
2006 x_px = coordinate % width;
2007 y_px = coordinate / width;
2008
2010 x_px - 1, y_px - 1, ibuf, stack, touched, pixel_color, threshold_sq);
2012 x_px - 1, y_px, ibuf, stack, touched, pixel_color, threshold_sq);
2014 x_px - 1, y_px + 1, ibuf, stack, touched, pixel_color, threshold_sq);
2016 x_px, y_px + 1, ibuf, stack, touched, pixel_color, threshold_sq);
2018 x_px, y_px - 1, ibuf, stack, touched, pixel_color, threshold_sq);
2020 x_px + 1, y_px - 1, ibuf, stack, touched, pixel_color, threshold_sq);
2022 x_px + 1, y_px, ibuf, stack, touched, pixel_color, threshold_sq);
2024 x_px + 1, y_px + 1, ibuf, stack, touched, pixel_color, threshold_sq);
2025 }
2026 }
2027 else {
2028 while (!BLI_stack_is_empty(stack)) {
2029 BLI_stack_pop(stack, &coordinate);
2030
2031 IMB_blend_color_byte(ibuf->byte_data_for_write() + 4 * coordinate,
2032 ibuf->byte_data_for_write() + 4 * coordinate,
2033 reinterpret_cast<uchar *>(&color_b),
2034 IMB_BlendMode(br->blend));
2035
2036 /* reconstruct the coordinates here */
2037 x_px = coordinate % width;
2038 y_px = coordinate / width;
2039
2041 x_px - 1, y_px - 1, ibuf, stack, touched, pixel_color, threshold_sq);
2043 x_px - 1, y_px, ibuf, stack, touched, pixel_color, threshold_sq);
2045 x_px - 1, y_px + 1, ibuf, stack, touched, pixel_color, threshold_sq);
2047 x_px, y_px + 1, ibuf, stack, touched, pixel_color, threshold_sq);
2049 x_px, y_px - 1, ibuf, stack, touched, pixel_color, threshold_sq);
2051 x_px + 1, y_px - 1, ibuf, stack, touched, pixel_color, threshold_sq);
2053 x_px + 1, y_px, ibuf, stack, touched, pixel_color, threshold_sq);
2055 x_px + 1, y_px + 1, ibuf, stack, touched, pixel_color, threshold_sq);
2056 }
2057 }
2058
2059 MEM_delete(touched);
2060 BLI_stack_free(stack);
2061 }
2062
2065
2066 BKE_image_release_ibuf(ima, ibuf, nullptr);
2067
2069}
2070
2072 const bContext *C, Brush *br, const float mouse_init[2], const float mouse_final[2], void *ps)
2073{
2075 Image *ima = sima->image;
2076 ImagePaintState *s = static_cast<ImagePaintState *>(ps);
2077
2078 ImBuf *ibuf;
2079 int x_px, y_px;
2080 uint color_b;
2081 float color_f[4];
2082 float image_init[2], image_final[2];
2083 float tangent[2];
2084 float line_len_sq_inv, line_len;
2085 const float brush_alpha = BKE_brush_alpha_get(s->paint, br);
2086
2087 bool do_float;
2088
2089 if (ima == nullptr) {
2090 return;
2091 }
2092
2093 float uv_origin[2];
2094 int tile_number = BKE_image_get_tile_from_pos(ima, image_init, image_init, uv_origin);
2095 ImageUser *iuser = paint_2d_get_tile_iuser(s, tile_number);
2096 if (!iuser) {
2097 return;
2098 }
2099
2100 ibuf = BKE_image_acquire_ibuf(ima, iuser, nullptr);
2101 if (ibuf == nullptr) {
2102 return;
2103 }
2104
2105 paint_2d_transform_mouse(s->v2d, mouse_final, image_final);
2106 paint_2d_transform_mouse(s->v2d, mouse_init, image_init);
2107 sub_v2_v2(image_init, uv_origin);
2108 sub_v2_v2(image_final, uv_origin);
2109
2110 image_final[0] *= ibuf->x;
2111 image_final[1] *= ibuf->y;
2112
2113 image_init[0] *= ibuf->x;
2114 image_init[1] *= ibuf->y;
2115
2116 /* some math to get needed gradient variables */
2117 sub_v2_v2v2(tangent, image_final, image_init);
2118 line_len = len_squared_v2(tangent);
2119 line_len_sq_inv = 1.0f / line_len;
2120 line_len = sqrtf(line_len);
2121
2122 do_float = (ibuf->float_data() != nullptr);
2123
2124 /* this will be substituted by something else when selection is available */
2125 ED_imapaint_dirty_region(ima, ibuf, iuser, 0, 0, ibuf->x, ibuf->y);
2126
2127 if (do_float) {
2128 float *float_data = ibuf->float_data_for_write();
2129 for (x_px = 0; x_px < ibuf->x; x_px++) {
2130 for (y_px = 0; y_px < ibuf->y; y_px++) {
2131 float f;
2132 const float p[2] = {x_px - image_init[0], y_px - image_init[1]};
2133
2134 switch (br->gradient_fill_mode) {
2135 case BRUSH_GRADIENT_LINEAR: {
2136 f = dot_v2v2(p, tangent) * line_len_sq_inv;
2137 break;
2138 }
2140 default: {
2141 f = len_v2(p) / line_len;
2142 break;
2143 }
2144 }
2145 BKE_colorband_evaluate(br->gradient, f, color_f);
2146 /* convert to premultiplied */
2147 mul_v3_fl(color_f, color_f[3]);
2148 color_f[3] *= brush_alpha;
2149 IMB_blend_color_float(float_data + 4 * (size_t(y_px) * ibuf->x + x_px),
2150 float_data + 4 * (size_t(y_px) * ibuf->x + x_px),
2151 color_f,
2152 IMB_BlendMode(br->blend));
2153 }
2154 }
2155 }
2156 else {
2157 uchar *byte_data = ibuf->byte_data_for_write();
2158 for (x_px = 0; x_px < ibuf->x; x_px++) {
2159 for (y_px = 0; y_px < ibuf->y; y_px++) {
2160 float f;
2161 const float p[2] = {x_px - image_init[0], y_px - image_init[1]};
2162
2163 switch (br->gradient_fill_mode) {
2164 case BRUSH_GRADIENT_LINEAR: {
2165 f = dot_v2v2(p, tangent) * line_len_sq_inv;
2166 break;
2167 }
2169 default: {
2170 f = len_v2(p) / line_len;
2171 break;
2172 }
2173 }
2174
2175 BKE_colorband_evaluate(br->gradient, f, color_f);
2177 rgba_float_to_uchar(reinterpret_cast<uchar *>(&color_b), color_f);
2178 (reinterpret_cast<uchar *>(&color_b))[3] *= brush_alpha;
2179 IMB_blend_color_byte(byte_data + 4 * (size_t(y_px) * ibuf->x + x_px),
2180 byte_data + 4 * (size_t(y_px) * ibuf->x + x_px),
2181 reinterpret_cast<uchar *>(&color_b),
2182 IMB_BlendMode(br->blend));
2183 }
2184 }
2185 }
2186
2189
2190 BKE_image_release_ibuf(ima, ibuf, nullptr);
2191
2193}
2194
2195} // namespace blender
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.hh:39
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.hh:63
#define BLI_BITMAP_SET(_bitmap, _index, _set)
#define IMAGEPAINT_MODE_IMAGE
#define ED_IMAGE_UNDO_TILE_SIZE
Definition ED_paint.hh:116
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NA_EDITED
Definition WM_types.hh:634
#define NC_IMAGE
Definition WM_types.hh:397
#define NA_PAINTING
Definition WM_types.hh:641
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define FLT_MAX
float distance(float, float) RET
float2x2 invert(float2x2 mat)
const int state
const ccl_global KernelWorkTile * tile
void view2d_view_to_region_fl(const View2D *v2d, float x, float y, float *r_region_x, float *r_region_y) ATTR_NONNULL()
Definition view2d.cc:1720
void view2d_region_to_view(const View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
Definition view2d.cc:1651
BMesh Iterators.
int BKE_image_get_tile_from_pos(Image *ima, const float uv[2], float r_uv[2], float r_ofs[2])
static int paint_2d_op(void *state, ImagePaintTile *tile, const float lastpos[2], const float pos[2])
@ BRUSH_GRADIENT_RADIAL
@ BRUSH_GRADIENT_LINEAR
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
@ RPT_WARNING
Definition BKE_report.hh:40
static bool is_inside_tile(const int size[2], const float pos[2], const float brush[2])
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
MINLINE float len_squared_v4v4(const float a[4], const float b[4]) ATTR_WARN_UNUSED_RESULT
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:253
static void paint_2d_ibuf_rgb_get(ImBuf *ibuf, int x, int y, float r_rgb[4])
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
static void brush_imbuf_tex_co(const rctf *mapping, int x, int y, float texco[3])
@ BRUSH_STROKE_ANCHORED
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE void blend_color_mix_float(float dst[4], const float src1[4], const float src2[4])
float BKE_brush_sample_tex_3d(const Paint *paint, const Brush *br, const MTex *mtex, const float3 &point, float4 &rgba, int thread, ImagePool *pool)
Definition brush.cc:985
void ED_imapaint_clear_partial_redraw()
static float paint_2d_ibuf_add_if(ImBuf *ibuf, int x, int y, float *outrgb, short paint_tile, float w)
void paint_2d_bucket_fill(const bContext *C, const float color[3], Brush *br, const float mouse_init[2], const float mouse_final[2], void *ps)
void BLI_stack_free(BLI_Stack *stack) ATTR_NONNULL()
Definition stack.cc:99
static ImageUser * paint_2d_get_tile_iuser(ImagePaintState *s, int tile_number)
bool IMB_colormanagement_space_is_srgb(const ColorSpace *colorspace)
@ BRUSH_GRADIENT_SPACING_CLAMP
@ BRUSH_GRADIENT_SPACING_REPEAT
float BKE_brush_sample_masktex(const Paint *paint, Brush *br, const float2 &point, int thread, ImagePool *pool)
Definition brush.cc:1101
void * paint_2d_new_stroke(bContext *C, wmOperator *op, const BrushStrokeMode mode)
MINLINE void copy_v2_v2(float r[2], const float a[2])
static void paint_2d_canvas_free(ImagePaintState *s)
void BLI_stack_pop(BLI_Stack *stack, void *dst) ATTR_NONNULL()
Definition stack.cc:141
static void paint_2d_ibuf_tile_convert(ImBuf *ibuf, int *x, int *y, short paint_tile)
@ IMB_BLEND_COPY_RGB
Definition IMB_imbuf.hh:230
@ IMB_BLEND_COPY_ALPHA
Definition IMB_imbuf.hh:231
@ IMB_BLEND_INTERPOLATE
Definition IMB_imbuf.hh:228
@ IMB_BLEND_ERASE_ALPHA
Definition IMB_imbuf.hh:210
MINLINE void blend_color_mix_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
unsigned short ushort
Definition BKE_text.h:56
static int paint_2d_torus_split_region(ImagePaintRegion region[4], ImBuf *dbuf, const ImBuf *sbuf, short paint_tile)
static bool paint_2d_ensure_tile_canvas(ImagePaintState *s, int i)
static void brush_painter_mask_imbuf_update(BrushPainter *painter, ImagePaintTile *tile, const ushort *tex_mask_old, int origx, int origy, int w, int h, int xt, int yt, const int diameter)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
void imapaint_image_update(ImBuf *ibuf)
bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4])
Definition colorband.cc:399
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
MINLINE void sub_v2_v2v2_int(int r[2], const int a[2], const int b[2])
static void paint_2d_fill_add_pixel_float(const int x_px, const int y_px, ImBuf *ibuf, BLI_Stack *stack, BLI_bitmap *touched, const float color[4], float threshold_sq)
static void paint_2d_convert_brushco(ImBuf *ibufb, const float pos[2], int ipos[2])
bool IMB_initImBuf(ImBuf *ibuf, unsigned int x, unsigned int y, ImBufFlags flags)
void paint_2d_stroke_done(void *ps, wmPaintCursor *cursor)
static void image_init(Image *ima, eImageSource source, eImageType type)
MINLINE void rgb_float_to_uchar(unsigned char r_col[3], const float col_f[3])
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
static void paint_2d_ibuf_rgb_set(ImBuf *ibuf, int x, int y, const bool is_torus, const float rgb[4])
PaintTileMap * ED_image_paint_tile_map_get()
MINLINE void mul_v3_fl(float r[3], float f)
VecBase< float, 4 > float4
static BrushPainter * brush_painter_2d_new(Scene *scene, const Paint *paint, Brush *brush, bool invert)
MINLINE void zero_v4(float r[4])
static void paint_2d_fill_add_pixel_byte(const int x_px, const int y_px, ImBuf *ibuf, BLI_Stack *stack, BLI_bitmap *touched, const float color[4], float threshold_sq)
void IMB_float_from_byte(ImBuf *ibuf)
static void paint_2d_lift_soften(ImagePaintState *s, ImagePaintTile *tile, ImBuf *ibuf, ImBuf *ibufb, const int *pos, const short paint_tile)
void IMB_rectblend(ImBuf *dbuf, uint8_t *dbuf_byte_data, float *dbuf_float_data, const ImBuf *obuf, const ImBuf *sbuf, unsigned short *dmask, const unsigned short *curvemask, const unsigned short *texmask, float mask_max, int destx, int desty, int origx, int origy, int srcx, int srcy, int width, int height, IMB_BlendMode mode, bool accumulate)
Definition rectop.cc:649
void paint_curve_mask_cache_free_data(CurveMaskCache *curve_mask_cache)
MINLINE void blend_color_add_float(float dst[4], const float src1[4], const float src2[4])
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
MINLINE void mul_v3_v3(float r[3], const float a[3])
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void paint_brush_color_get(const Paint *paint, Brush *br, std::optional< float3 > &initial_hsv_jitter, bool invert, float distance, float pressure, float r_color[3])
unsigned char uchar
Definition BKE_text.h:58
void IMB_colormanagement_scene_linear_to_colorspace_v3(float pixel[3], const ColorSpace *colorspace)
static void paint_2d_set_region(ImagePaintRegion *region, int destx, int desty, int srcx, int srcy, int width, int height)
MINLINE void add_v4_v4(float r[4], const float a[4])
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
void paint_2d_redraw(const bContext *C, void *ps, bool final)
float3 BKE_paint_seed_hsv_jitter()
unsigned char uint8_t
Definition BKE_text.h:79
static void brush_painter_2d_refresh_cache(ImagePaintState *s, BrushPainter *painter, ImagePaintTile *tile, const float pos[2], const float mouse[2], float pressure, float distance, float size)
static void paint_2d_op_foreach_do(void *__restrict data_v, const int iter, const TaskParallelTLS *__restrict)
void paint_2d_gradient_fill(const bContext *C, Brush *br, const float mouse_init[2], const float mouse_final[2], void *ps)
@ IMAGE_PAINT_BRUSH_TYPE_SOFTEN
@ IMAGE_PAINT_BRUSH_TYPE_SMEAR
@ IMAGE_PAINT_BRUSH_TYPE_DRAW
@ IMAGE_PAINT_BRUSH_TYPE_CLONE
void IMB_rectclip(ImBuf *dbuf, const ImBuf *sbuf, int *destx, int *desty, int *srcx, int *srcy, int *width, int *height)
Definition rectop.cc:511
static void paint_2d_do_making_brush(ImagePaintState *s, ImagePaintTile *tile, ImagePaintRegion *region, uint8_t *canvas_byte_data, float *canvas_float_data, ImBuf *frombuf, float mask_max, short blend, int tilex, int tiley, int tilew, int tileh)
BlurKernel * paint_new_blur_kernel(Brush *br, bool proj)
static void paint_2d_transform_mouse(View2D *v2d, const float in[2], float out[2])
void IMB_blend_color_float(float dst[4], const float src1[4], const float src2[4], IMB_BlendMode mode)
Definition rectop.cc:118
void paint_brush_exit_tex(Brush *brush)
static ImBuf * paint_2d_lift_clone(ImBuf *ibuf, ImBuf *ibufb, const int *pos)
void BKE_image_pool_free(ImagePool *pool)
bool paint_use_opacity_masking(const Paint *paint, const Brush *brush)
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void imapaint_region_tiles(ImBuf *ibuf, int x, int y, int w, int h, int *tx, int *ty, int *tw, int *th)
const ImBuf * ED_image_paint_tile_find(PaintTileMap *paint_tile_map, Image *image, ImBuf *ibuf, ImageUser *iuser, int x_tile, int y_tile, unsigned short **r_mask, bool validate)
static void brush_painter_2d_tex_mapping(ImagePaintState *s, ImagePaintTile *tile, const int diameter, const float pos[2], const float mouse[2], int mapmode, rctf *r_mapping)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
MINLINE void straight_uchar_to_premul_float(float result[4], const unsigned char color[4])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
void ED_imapaint_dirty_region(Image *ima, ImBuf *ibuf, ImageUser *iuser, int x, int y, int w, int h)
static void brush_painter_imbuf_partial_update(BrushPainter *painter, ImagePaintTile *tile, const float pos[2], const int diameter)
SpaceImage * CTX_wm_space_image(const bContext *C)
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
MINLINE float len_squared_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
void IMB_byte_from_float(ImBuf *ibuf)
void paint_2d_stroke(void *ps, const float prev_mval[2], const float mval[2], const bool eraser, float pressure, float distance, float base_size)
void IMB_rectblend_threaded(ImBuf *dbuf, const ImBuf *obuf, const ImBuf *sbuf, unsigned short *dmask, const unsigned short *curvemask, const unsigned short *texmask, float mask_max, int destx, int desty, int origx, int origy, int srcx, int srcy, int width, int height, IMB_BlendMode mode, bool accumulate)
Definition rectop.cc:1116
Paint * BKE_paint_get_active_from_context(const bContext *C)
static int paint_2d_canvas_set(ImagePaintState *s, const Paint *paint)
static void paint_2d_lift_smear(const ImBuf *ibuf, ImBuf *ibufb, int *pos, short paint_tile)
void paint_delete_blur_kernel(BlurKernel *kernel)
@ ID_RECALC_SHADING
Definition DNA_ID.h:1094
static void brush_painter_2d_require_imbuf(Brush *brush, ImagePaintTile *tile, bool is_float, bool is_data, bool is_srgb, const ColorSpace *byte_colorspace, bool invert)
static ushort * brush_painter_mask_ibuf_new(BrushPainter *painter, const int size)
std::optional< BrushColorJitterSettings > BKE_brush_color_jitter_get_settings(const Paint *paint, const Brush *brush)
Definition brush.cc:1262
Brush * BKE_paint_brush(Paint *paint)
static void brush_painter_cache_2d_free(BrushPainterCache *cache)
void BLI_stack_push(BLI_Stack *stack, const void *src) ATTR_NONNULL()
Definition stack.cc:135
Scene * CTX_data_scene(const bContext *C)
void IMB_blend_color_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4], IMB_BlendMode mode)
Definition rectop.cc:30
static void brush_painter_mask_imbuf_partial_update(BrushPainter *painter, ImagePaintTile *tile, const float pos[2], const int diameter)
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void sub_v2_v2(float r[2], const float a[2])
static void brush_painter_imbuf_update(BrushPainter *painter, ImagePaintTile *tile, ImBuf *oldtexibuf, int origx, int origy, int w, int h, int xt, int yt)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void mul_v4_fl(float r[4], float f)
VecBase< float, 3 > float3
BLI_INLINE void IMB_colormanagement_scene_linear_to_srgb_v3(float srgb[3], const float scene_linear[3])
static void paint_2d_uv_to_coord(ImagePaintTile *tile, const float uv[2], float coord[2])
bool BKE_image_has_packedfile(const Image *image)
static ImBuf * brush_painter_imbuf_new(BrushPainter *painter, ImagePaintTile *tile, const int size, float pressure, float distance)
void BKE_imageuser_default(ImageUser *iuser)
BLI_Stack * BLI_stack_new(size_t elem_size, const char *description) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:85
MINLINE int min_ii(int a, int b)
MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float color[4])
Object * CTX_data_active_object(const bContext *C)
MINLINE void zero_v2(float r[2])
ARegion * CTX_wm_region(const bContext *C)
MINLINE void straight_to_premul_v4(float color[4])
float BKE_brush_alpha_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1454
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
ocio::ColorSpace ColorSpace
Definition BLF_api.hh:42
void paint_brush_init_tex(Brush *brush)
void IMB_copy_rect(float *dst, const int2 &dst_size, const float *src, const int2 &src_size, int channels, const int2 &src_rect_pos, const int2 &dst_rect_pos, const int2 &rect_size)
Definition rectop.cc:414
void paint_curve_mask_cache_update(CurveMaskCache *curve_mask_cache, const Brush *brush, const int diameter, const float radius, const float cursor_position[2])
@ PAINT2D_TILE_UNINITIALIZED
bool WM_paint_cursor_end(wmPaintCursor *handle)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
#define floorf
#define fabsf
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const ColorSpace * byte_colorspace
std::optional< float3 > initial_hsv_jitter
struct ColorBand * gradient
eBrushGradientSourceFill gradient_fill_mode
Caching structure for curve mask.
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
bool colorspace_is_data() const
const float * float_data() const
ImColorMode color_mode
float * float_data_for_write()
Definition allocimbuf.cc:79
BrushPainterCache cache
ImagePaintTileState state
bke::PaintRuntime * runtime
ePaintSymmetryFlags symmetry_flags
struct ToolSettings * toolsettings
struct ImageUser iuser
struct ImagePaintSettings imapaint
struct ReportList * reports