Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
iop/drawlayer/brush.c
Go to the documentation of this file.
1/*
2 This file is part of the Ansel project.
3 Copyright (C) 2026 Aurélien PIERRE.
4
5 Ansel is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 Ansel is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Ansel. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/*
20 * drawlayer public brush API implementation
21 *
22 * Self-contained dab-level rasterization and profile math.
23 * This file is intentionally independent from drawlayer module internals.
24 */
25
26#include "iop/drawlayer/brush.h"
27#include "iop/drawlayer/cache.h"
28#include "iop/drawlayer/paint.h"
30
31#include "system/macros.h"
32#include "system/simd.h"
33#include "iop/noise_generator.h"
34
35#include <math.h>
36#include <string.h>
37
39static inline float _clamp01(const float v)
40{
41 return fminf(fmaxf(v, 0.0f), 1.0f);
42}
43
45static inline float _lerpf(const float a, const float b, const float t)
46{
47 return a + (b - a) * t;
48}
49
51static inline float _cell_hash01_from_seed(const uint64_t cell_seed, const uint64_t salt)
52{
53 return (float)splitmix32(cell_seed ^ salt) / (float)UINT32_MAX;
54}
55
57static inline float _cellular_grain_2d(const uint64_t seed, const float x, const float y)
58{
59 const int cell_x = (int)floorf(x);
60 const int cell_y = (int)floorf(y);
61 float accum = 0.0f;
62 float weight_sum = 0.0f;
63
64 for(int oy = -1; oy <= 1; oy++)
65 {
66 for(int ox = -1; ox <= 1; ox++)
67 {
68 const int ix = cell_x + ox;
69 const int iy = cell_y + oy;
70 const uint64_t cell_seed = seed
71 ^ ((uint64_t)(uint32_t)ix * 0x9e3779b185ebca87ull)
72 ^ ((uint64_t)(uint32_t)iy * 0xc2b2ae3d27d4eb4full);
73 const float jitter_x = _cell_hash01_from_seed(cell_seed, 0x94d049bb133111ebull);
74 const float jitter_y = _cell_hash01_from_seed(cell_seed, 0xbf58476d1ce4e5b9ull);
75 const float grain_gain = 0.65f + 0.35f * _cell_hash01_from_seed(cell_seed, 0xda942042e4dd58b5ull);
76 const float dx = x - ((float)ix + jitter_x);
77 const float dy = y - ((float)iy + jitter_y);
78 const float dist2 = dx * dx + dy * dy;
79 const float radius = 0.42f + 0.22f * _cell_hash01_from_seed(cell_seed, 0x369dea0f31a53f85ull);
80 const float grain = fmaxf(0.0f, 1.0f - dist2 / (radius * radius));
81 const float shaped = grain * grain * (3.0f - 2.0f * grain);
82 accum += grain_gain * shaped;
83 weight_sum += grain_gain;
84 }
85 }
86
87 return (weight_sum > 1e-6f) ? _clamp01(accum / weight_sum) : 0.0f;
88}
89
91static inline void _sprinkle_octave_weights(const float coarseness, float *w0, float *w1, float *w2)
92{
93 const float c = 1.0f - _clamp01(coarseness);
94 if(c <= 0.5f)
95 {
96 const float t = c * 2.0f;
97 if(!IS_NULL_PTR(w0)) *w0 = _lerpf(1.0f, 1.0f / 3.0f, t);
98 if(!IS_NULL_PTR(w1)) *w1 = _lerpf(0.0f, 1.0f / 3.0f, t);
99 if(!IS_NULL_PTR(w2)) *w2 = _lerpf(0.0f, 1.0f / 3.0f, t);
100 }
101 else
102 {
103 const float t = (c - 0.5f) * 2.0f;
104 if(!IS_NULL_PTR(w0)) *w0 = _lerpf(1.0f / 3.0f, 0.0f, t);
105 if(!IS_NULL_PTR(w1)) *w1 = _lerpf(1.0f / 3.0f, 0.0f, t);
106 if(!IS_NULL_PTR(w2)) *w2 = _lerpf(1.0f / 3.0f, 1.0f, t);
107 }
108}
109
111static inline float _sprinkle_noise_at_pixel_precomputed(const float px, const float py,
112 const float scale, const float strength,
113 const float w0, const float w1, const float w2,
114 const uint64_t seed0, const uint64_t seed1, const uint64_t seed2)
115{
116 if(strength <= 1e-6f) return 1.0f;
117 const float x = px * scale;
118 const float y = py * scale;
119 const float g0 = (w0 > 1e-6f) ? _cellular_grain_2d(seed0, x, y) : 0.0f;
120 const float g1 = (w1 > 1e-6f) ? _cellular_grain_2d(seed1, x * 1.93f + 4.7f, y * 1.93f - 2.9f) : 0.0f;
121 const float g2 = (w2 > 1e-6f) ? _cellular_grain_2d(seed2, x * 3.71f - 6.2f, y * 3.71f + 8.4f) : 0.0f;
122 const float field = w0 * g0 + w1 * g1 + w2 * g2;
123 const float centered = 2.0f * field - 1.0f;
124 return fmaxf(0.0f, 1.0f + strength * centered);
125}
126
140
142 const float center_x, const float center_y,
143 const float radius,
145{
147 return;
148
150 .scale = 1.0f,
151 .strength = 0.0f,
152 .w0 = 0.0f,
153 .w1 = 0.0f,
154 .w2 = 0.0f,
155 .seed0 = 0u,
156 .seed1 = 0u,
157 .seed2 = 0u,
158 .gain = 1.0f,
159 .enabled = FALSE,
160 };
161
162 if(IS_NULL_PTR(dab) || dab->sprinkles <= 1e-6f)
163 return;
164
165 preview->scale = 1.0f / fmaxf(dab->sprinkle_size, 1.0f);
166 preview->strength = _clamp01(dab->sprinkles);
168 preview->seed0 = ((uint64_t)dab->stroke_batch << 32) ^ 0x7f4a7c159e3779b9ull;
169 preview->seed1 = preview->seed0 ^ 0xbf58476d1ce4e5b9ull;
170 preview->seed2 = preview->seed0 ^ 0x94d049bb133111ebull;
171 preview->enabled = TRUE;
172
173 float noise_sum = 0.0f;
174 int noise_count = 0;
175 for(int sy = -2; sy <= 2; sy++)
176 {
177 for(int sx = -2; sx <= 2; sx++)
178 {
179 const float nx = 0.4f * (float)sx;
180 const float ny = 0.4f * (float)sy;
181 if(nx * nx + ny * ny > 1.0f) continue;
182 const int pixel_x = (int)lrintf(center_x + nx * radius);
183 const int pixel_y = (int)lrintf(center_y + ny * radius);
184 noise_sum += _sprinkle_noise_at_pixel_precomputed(pixel_x, pixel_y,
185 preview->scale, preview->strength,
186 preview->w0, preview->w1, preview->w2,
187 preview->seed0, preview->seed1, preview->seed2);
188 noise_count++;
189 }
190 }
191
192 if(noise_count > 0)
193 {
194 const float mean_noise = noise_sum / (float)noise_count;
195 preview->gain = (mean_noise > 1e-6f) ? (1.0f / mean_noise) : 1.0f;
196 }
197}
198
200 const float px, const float py)
201{
202 if(IS_NULL_PTR(preview) || !preview->enabled) return 1.0f;
204 preview->scale, preview->strength,
205 preview->w0, preview->w1, preview->w2,
206 preview->seed0, preview->seed1, preview->seed2) * preview->gain;
207}
208
235
237{
238 /* Per-pixel analytic evaluation (profile and resolved alpha controls). */
239 float profile;
245
251 const dt_drawlayer_brush_dab_t *dab,
252 const int origin_x, const int origin_y,
253 const float scale,
255{
256 if(!stroke || IS_NULL_PTR(dab) || IS_NULL_PTR(view) || !stroke->bounds.valid) return FALSE;
257 if(stroke->bounds.se[0] <= stroke->bounds.nw[0]
258 || stroke->bounds.se[1] <= stroke->bounds.nw[1])
259 return FALSE;
260
261 const float dir_len = hypotf(dab->dir_x, dab->dir_y);
262 float sprinkle_w0 = 0.0f, sprinkle_w1 = 0.0f, sprinkle_w2 = 0.0f;
263 _sprinkle_octave_weights(dab->sprinkle_coarseness, &sprinkle_w0, &sprinkle_w1, &sprinkle_w2);
264 const uint64_t sprinkle_seed0 = ((uint64_t)dab->stroke_batch << 32) ^ 0x7f4a7c159e3779b9ull;
265
267 .dab = dab,
268 .bounds = stroke->bounds,
269 .sample_origin_x = origin_x,
270 .sample_origin_y = origin_y,
271 .scaled_radius = fmaxf(dab->radius * scale, 0.5f),
272 .center_x = dab->x * scale - (float)origin_x,
273 .center_y = dab->y * scale - (float)origin_y,
274 .inv_radius = 0.0f,
275 .tx = (dir_len > 1e-6f) ? (dab->dir_x / dir_len) : 0.0f,
276 .ty = (dir_len > 1e-6f) ? (dab->dir_y / dir_len) : 1.0f,
277 .alpha_noise_gain = 1.0f,
278 .sprinkle_coord_scale = 1.0f / fmaxf(scale, 1e-6f),
279 .sprinkle_scale = 1.0f / fmaxf(dab->sprinkle_size, 1.0f),
280 .sprinkle_strength = _clamp01(dab->sprinkles),
281 .sprinkle_w0 = sprinkle_w0,
282 .sprinkle_w1 = sprinkle_w1,
283 .sprinkle_w2 = sprinkle_w2,
284 .sprinkle_seed0 = sprinkle_seed0,
285 .sprinkle_seed1 = sprinkle_seed0 ^ 0xbf58476d1ce4e5b9ull,
286 .sprinkle_seed2 = sprinkle_seed0 ^ 0x94d049bb133111ebull,
287 .use_stroke_mask = (dab->mode == DT_DRAWLAYER_BRUSH_MODE_PAINT || dab->mode == DT_DRAWLAYER_BRUSH_MODE_ERASE),
288 .have_sprinkles = (dab->sprinkles > 1e-6f),
289 };
290 view->inv_radius = 1.0f / view->scaled_radius;
291 return TRUE;
292}
293
300 const int pixel_x, const int pixel_y)
301{
302 float alpha_noise = 1.0f;
303 if(!IS_NULL_PTR(view) && view->have_sprinkles)
304 {
305 const float layer_x = ((float)pixel_x + 0.5f) * view->sprinkle_coord_scale;
306 const float layer_y = ((float)pixel_y + 0.5f) * view->sprinkle_coord_scale;
307 alpha_noise *= _sprinkle_noise_at_pixel_precomputed(layer_x, layer_y,
308 view->sprinkle_scale,
309 view->sprinkle_strength,
310 view->sprinkle_w0,
311 view->sprinkle_w1,
312 view->sprinkle_w2,
313 view->sprinkle_seed0,
314 view->sprinkle_seed1,
315 view->sprinkle_seed2);
316 }
317 return alpha_noise;
318}
319
323{
324 if(IS_NULL_PTR(dab) || IS_NULL_PTR(view) || !view->have_sprinkles) return 1.0f;
325
326 float noise_sum = 0.0f;
327 int noise_count = 0;
328 for(int sy = -2; sy <= 2; sy++)
329 {
330 for(int sx = -2; sx <= 2; sx++)
331 {
332 const float nx = 0.4f * (float)sx;
333 const float ny = 0.4f * (float)sy;
334 if(nx * nx + ny * ny > 1.0f) continue;
335
336 const int pixel_x = (int)lrintf(view->sample_origin_x + view->center_x + nx * view->scaled_radius);
337 const int pixel_y = (int)lrintf(view->sample_origin_y + view->center_y + ny * view->scaled_radius);
338 noise_sum += _sample_alpha_noise_raw(dab, view, pixel_x, pixel_y);
339 noise_count++;
340 }
341 }
342
343 if(noise_count <= 0) return 1.0f;
344 const float mean_noise = noise_sum / (float)noise_count;
345 return (mean_noise > 1e-6f) ? (1.0f / mean_noise) : 1.0f;
346}
347
355static inline float _stroke_flow_alpha(const dt_drawlayer_brush_dab_t *dab, const float opacity, const float flow,
356 const float sample_opacity_scale, const float profile, const float brush_alpha,
357 const float old_alpha, const float stroke_old_alpha,
358 const gboolean have_stroke_alpha)
359{
360 (void)profile;
361 const float opacity_scale
362 = isfinite(sample_opacity_scale) ? CLAMP(sample_opacity_scale, 1e-6f, 1.0f) : 1.0f;
363
366 {
367 /* Replacement modes mix their sampled source against the destination, they
368 * do not build over destination alpha like paint mode. */
369 const float normalized = 1.0f - powf(fmaxf(1.0f - brush_alpha, 0.0f), opacity_scale);
370 return _clamp01(normalized);
371 }
372
373 const float flow_ref_alpha = have_stroke_alpha ? stroke_old_alpha
374 : ((dab->mode == DT_DRAWLAYER_BRUSH_MODE_ERASE) ? 0.0f : old_alpha);
375 /* Capped watercolor path:
376 * a stroke may build locally, but the stroke-local alpha must never exceed
377 * the user-requested stroke opacity. The current dab may only contribute its
378 * own local brush alpha, clipped by the remaining headroom to that cap. */
379 const float stroke_cap = _clamp01(opacity);
380 const float remaining_to_cap = fmaxf(stroke_cap - flow_ref_alpha, 0.0f);
381 const float capped_alpha = fminf(_clamp01(brush_alpha),
382 remaining_to_cap / fmaxf(1.0f - flow_ref_alpha, 1e-6f));
383 const float accum_alpha = 1.0f - powf(fmaxf(1.0f - brush_alpha, 0.0f), opacity_scale);
384 /* Internal flow convention is inverse of UI flow:
385 * - internal flow=0 (UI 100%) -> union/capped watercolor behavior,
386 * - internal flow=1 (UI 0%) -> accumulative highlighter behavior. */
387 return _clamp01(_lerpf(capped_alpha, accum_alpha, flow));
388}
389
395 const float sample_opacity_scale,
396 float *stroke_mask, const int stroke_mask_width,
397 const int stroke_mask_height,
398 const int x, const int y, const float old_alpha,
400{
401 if(IS_NULL_PTR(view) || IS_NULL_PTR(pixel_eval)) return FALSE;
402 const dt_drawlayer_brush_dab_t *dab = view->dab;
403
404 const float dy = ((float)y + 0.5f - view->center_y) * view->inv_radius;
405 const float dx = ((float)x + 0.5f - view->center_x) * view->inv_radius;
406 const float norm2 = dx * dx + dy * dy;
407 pixel_eval->profile = dt_drawlayer_brush_profile_eval(dab, norm2);
408 if(pixel_eval->profile <= 0.0f) return FALSE;
409
410 const float alpha_noise = fmaxf(0.0f, _sample_alpha_noise_raw(dab, view,
411 view->sample_origin_x + x,
412 view->sample_origin_y + y)
413 * view->alpha_noise_gain);
414
415 pixel_eval->brush_alpha = _clamp01(dab->opacity * pixel_eval->profile * alpha_noise);
416 if(pixel_eval->brush_alpha <= 0.0f) return FALSE;
417
418 // Flow caps the stroke-wise opacity to the user-specified value
419 // For this reason, we need to resolve first the stroke over transparent content,
420 // then slap the transparent layer over the background. Aka temporary buffer.
421 if(view->use_stroke_mask)
422 {
423 pixel_eval->stroke_alpha = stroke_mask + (size_t)y * stroke_mask_width + x;
424 pixel_eval->stroke_old_alpha = _clamp01(*pixel_eval->stroke_alpha);
425 }
426
427 pixel_eval->src_alpha
428 = _stroke_flow_alpha(dab, dab->opacity, dab->flow, sample_opacity_scale,
429 pixel_eval->profile, pixel_eval->brush_alpha, old_alpha,
430 pixel_eval->stroke_old_alpha, !IS_NULL_PTR(pixel_eval->stroke_alpha));
431 return pixel_eval->src_alpha > 0.0f;
432}
433
438static gboolean _prepare_blur_context(dt_aligned_pixel_simd_t *blur_px, const float *buffer, const int width,
439 const int height, const int source_origin_x, const int source_origin_y,
440 const int patch_origin_x, const int patch_origin_y,
442{
443 if(IS_NULL_PTR(blur_px)) return FALSE;
444 float blur_weight_sum = 0.0f;
445 dt_aligned_pixel_simd_t blur_sum = dt_simd_set1(0.0f);
446 const dt_drawlayer_brush_dab_t *dab = view->dab;
447
448 for(int y = view->bounds.nw[1]; y < view->bounds.se[1]; y++)
449 {
450 const float dy = ((float)y + 0.5f - view->center_y) * view->inv_radius;
451 const float dy2 = dy * dy;
452 for(int x = view->bounds.nw[0]; x < view->bounds.se[0]; x++)
453 {
454 const float dx = ((float)x + 0.5f - view->center_x) * view->inv_radius;
455 const float blur_weight = dt_drawlayer_brush_profile_eval(dab, dx * dx + dy2);
456 if(blur_weight <= 0.0f) continue;
457
458 const int source_x = x + patch_origin_x - source_origin_x;
459 const int source_y = y + patch_origin_y - source_origin_y;
460 if(source_x < 0 || source_y < 0 || source_x >= width || source_y >= height) continue;
461 const float *pixel = buffer + 4 * ((size_t)source_y * width + source_x);
462 blur_sum += dt_load_simd(pixel) * dt_simd_set1(blur_weight);
463 blur_weight_sum += blur_weight;
464 }
465 }
466
467 if(blur_weight_sum <= 1e-8f) return FALSE;
468 *blur_px = blur_sum * dt_simd_set1(1.0f / blur_weight_sum);
469 return TRUE;
470}
471
472
474static inline float _smudge_hash_signed(const int x, const int y, const int lane)
475{
476 guint32 h = (guint32)(x * 73856093u) ^ (guint32)(y * 19349663u) ^ (guint32)(lane * 83492791u);
477 h ^= h >> 13;
478 h *= 1274126177u;
479 h ^= h >> 16;
480 return ((h & 0xffffu) / 32767.5f) - 1.0f;
481}
482
487static inline __attribute__((always_inline)) dt_aligned_pixel_simd_t
488_sample_rgba_float_bilinear(const float *buffer, const int width, const int height, const float x,
489 const float y)
490{
491 if(IS_NULL_PTR(buffer) || width <= 0 || height <= 0) return dt_simd_set1(0.0f);
492
493 const float fx = CLAMP(x, 0.0f, (float)(width - 1));
494 const float fy = CLAMP(y, 0.0f, (float)(height - 1));
495 const int x0 = (int)floorf(fx);
496 const int y0 = (int)floorf(fy);
497 const int x1 = MIN(width - 1, x0 + 1);
498 const int y1 = MIN(height - 1, y0 + 1);
499 const float tx = fx - x0;
500 const float ty = fy - y0;
501
502 const float *p00 = buffer + 4 * ((size_t)y0 * width + x0);
503 const float *p10 = buffer + 4 * ((size_t)y0 * width + x1);
504 const float *p01 = buffer + 4 * ((size_t)y1 * width + x0);
505 const float *p11 = buffer + 4 * ((size_t)y1 * width + x1);
506 const dt_aligned_pixel_simd_t p00v = dt_load_simd(p00);
507 const dt_aligned_pixel_simd_t p10v = dt_load_simd(p10);
508 const dt_aligned_pixel_simd_t p01v = dt_load_simd(p01);
509 const dt_aligned_pixel_simd_t p11v = dt_load_simd(p11);
510 const dt_aligned_pixel_simd_t txv = dt_simd_set1(tx);
511 const dt_aligned_pixel_simd_t tyv = dt_simd_set1(ty);
512 const dt_aligned_pixel_simd_t av = p00v + (p10v - p00v) * txv;
513 const dt_aligned_pixel_simd_t bv = p01v + (p11v - p01v) * txv;
514 return av + (bv - av) * tyv;
515}
516
522static inline __attribute__((always_inline)) dt_aligned_pixel_simd_t
523_sample_smudge_source_float(const float *buffer, const int width, const int height, const float sx,
524 const float sy, const float motion_dx, const float motion_dy,
525 const int jitter_x, const int jitter_y)
526{
527 dt_aligned_pixel_simd_t rgba_sum = dt_simd_set1(0.0f);
528 if(IS_NULL_PTR(buffer) || width <= 0 || height <= 0) return rgba_sum;
529
530 float dir_x = motion_dx;
531 float dir_y = motion_dy;
532 const float motion = hypotf(dir_x, dir_y);
533 if(motion > 1e-6f)
534 {
535 dir_x /= motion;
536 dir_y /= motion;
537 }
538 else
539 {
540 dir_x = 1.0f;
541 dir_y = 0.0f;
542 }
543
544 const float perp_x = -dir_y;
545 const float perp_y = dir_x;
546 const float jitter = 0.60f * _smudge_hash_signed(jitter_x, jitter_y, 0);
547 const float side = 0.90f + 0.30f * _smudge_hash_signed(jitter_x, jitter_y, 1);
548 const float trail = 0.80f + 0.25f * _smudge_hash_signed(jitter_x, jitter_y, 2);
549
550 const float taps[7][3] = {
551 { 0.00f, jitter, 0.24f },
552 { -trail, 0.25f + jitter, 0.18f },
553 { -0.45f, -0.35f + jitter, 0.15f },
554 { -0.15f, side + jitter, 0.11f },
555 { -0.15f, -side + jitter, 0.11f },
556 { 0.25f, 0.45f * side + jitter, 0.11f },
557 { 0.25f, -0.45f * side + jitter, 0.10f },
558 };
559
560 float weight_sum = 0.0f;
561 for(int i = 0; i < 7; i++)
562 {
563 const float px = sx + dir_x * taps[i][0] + perp_x * taps[i][1];
564 const float py = sy + dir_y * taps[i][0] + perp_y * taps[i][1];
565 const float w = taps[i][2];
566 rgba_sum += _sample_rgba_float_bilinear(buffer, width, height, px, py) * dt_simd_set1(w);
567 weight_sum += w;
568 }
569
570 if(weight_sum > 1e-8f)
571 rgba_sum *= dt_simd_set1(1.0f / weight_sum);
572 return rgba_sum;
573}
574
576static inline float _smudge_deposit_alpha(const float src_alpha, const float carried_alpha, const float opacity)
577{
578 const float carry = _clamp01(carried_alpha);
579 const float base = _clamp01(opacity);
580 const float influence = base + (1.0f - base) * carry;
581 return _clamp01(src_alpha * influence);
582}
583
588static dt_aligned_pixel_simd_t _apply_smudge_stroke_mode(const float *source_buffer, const int source_width,
589 const int source_height, const int source_origin_x,
590 const int source_origin_y,
591 dt_drawlayer_paint_stroke_t *runtime_private,
593 const float scale, const int patch_origin_x,
594 const int patch_origin_y, const int x, const int y,
595 const float src_alpha,
596 const dt_aligned_pixel_simd_t old_px)
597{
598 const dt_drawlayer_brush_dab_t *dab = view->dab;
599 const float source_x_offset = (float)(patch_origin_x - source_origin_x);
600 const float source_y_offset = (float)(patch_origin_y - source_origin_y);
601 const float center_abs_x = view->center_x + (float)patch_origin_x;
602 const float center_abs_y = view->center_y + (float)patch_origin_y;
603 float sample_x = (float)x + source_x_offset;
604 float sample_y = (float)y + source_y_offset;
605 float motion_dx = 0.0f;
606 float motion_dy = 0.0f;
607 float *smudge_pixels = dt_drawlayer_paint_runtime_smudge_pixels(runtime_private);
608 const int smudge_width = dt_drawlayer_paint_runtime_smudge_width(runtime_private);
609
610 if(runtime_private && dt_drawlayer_paint_runtime_have_smudge_pickup(runtime_private))
611 {
612 /* Smudge samples from a lagging pickup point that follows dab centers. */
613 float pickup_center_x = 0.0f;
614 float pickup_center_y = 0.0f;
615 dt_drawlayer_paint_runtime_get_smudge_pickup(runtime_private, &pickup_center_x, &pickup_center_y);
616 pickup_center_x *= scale;
617 pickup_center_y *= scale;
618 sample_x += pickup_center_x - center_abs_x;
619 sample_y += pickup_center_y - center_abs_y;
620 motion_dx = center_abs_x - pickup_center_x;
621 motion_dy = center_abs_y - pickup_center_y;
622 }
623
624 const dt_aligned_pixel_simd_t sampled_px
625 = _sample_smudge_source_float(source_buffer, source_width, source_height, sample_x, sample_y,
626 motion_dx, motion_dy,
627 x - view->bounds.nw[0], y - view->bounds.nw[1]);
628
629 if(IS_NULL_PTR(smudge_pixels) || smudge_width <= 0) return old_px;
630 float *carry = smudge_pixels + 4 * ((size_t)(y - view->bounds.nw[1]) * smudge_width + (x - view->bounds.nw[0]));
631 const dt_aligned_pixel_simd_t carried_px = dt_load_simd(carry);
632 const float pickup_blend = _clamp01(dab->opacity);
633 const float carried_alpha = _clamp01(carry[3]);
634 const float deposit_alpha = _smudge_deposit_alpha(src_alpha, carried_alpha, dab->opacity);
635 const float inv_alpha = 1.0f - deposit_alpha;
636
637 const dt_aligned_pixel_simd_t out_px = carried_px * dt_simd_set1(deposit_alpha)
638 + old_px * dt_simd_set1(inv_alpha);
639 const dt_aligned_pixel_simd_t next_carry = carried_px
640 + (sampled_px - carried_px) * dt_simd_set1(pickup_blend);
641 dt_store_simd(carry, next_carry);
642 return out_px;
643}
644
650 dt_drawlayer_cache_patch_t *patch, const float scale,
651 const dt_drawlayer_brush_dab_t *dab,
652 const float sample_opacity_scale,
653 dt_drawlayer_cache_patch_t *stroke_mask,
654 dt_drawlayer_paint_stroke_t *runtime_private)
655{
656 /* Entry point for dab-level rasterization.
657 * Steps:
658 * 1) precompute dab bounds and orientation,
659 * 2) resolve per-pixel alpha (profile/noise/flow),
660 * 3) blend into target buffer and update stroke-local alpha mask. */
661 if(IS_NULL_PTR(patch) || IS_NULL_PTR(patch->pixels) || IS_NULL_PTR(dab) || !runtime_private) return FALSE;
662 if(dab->radius <= 0.0f || dab->opacity <= 0.0f || scale <= 0.0f) return FALSE;
663
664 float *const buffer = patch->pixels;
665 const int width = patch->width;
666 const int height = patch->height;
667 const int origin_x = patch->x;
668 const int origin_y = patch->y;
669 const dt_drawlayer_cache_patch_t *const source_patch
670 = (sample_patch && sample_patch->pixels) ? sample_patch : patch;
671 const float *const source_buffer = source_patch->pixels;
672 const int source_width = source_patch->width;
673 const int source_height = source_patch->height;
674 const int source_origin_x = source_patch->x;
675 const int source_origin_y = source_patch->y;
676 float *const stroke_mask_pixels = stroke_mask ? stroke_mask->pixels : NULL;
677 const int stroke_mask_width = stroke_mask ? stroke_mask->width : 0;
678 const int stroke_mask_height = stroke_mask ? stroke_mask->height : 0;
679
680 dt_drawlayer_brush_dab_t prepared = *dab;
681 prepared.opacity = _clamp01(prepared.opacity);
682 /* Internal flow convention is inverse of UI flow. */
683 prepared.flow = 1.0f - _clamp01(prepared.flow);
684
685 if(!dt_drawlayer_paint_runtime_prepare_dab_context(runtime_private, &prepared, width, height,
686 origin_x, origin_y, scale))
687 return FALSE;
688
690 if(!_brush_runtime_view_from_state(runtime_private, &prepared, origin_x, origin_y, scale, &view))
691 return FALSE;
693
694 dt_aligned_pixel_simd_t blur_px = dt_simd_set1(0.0f);
695 switch(view.dab->mode)
696 {
698 if(!_prepare_blur_context(&blur_px, source_buffer, source_width, source_height, source_origin_x,
699 source_origin_y, origin_x, origin_y, &view))
700 return FALSE;
701 break;
704 view.bounds.se[0] - view.bounds.nw[0],
705 view.bounds.se[1] - view.bounds.nw[1]))
706 return FALSE;
707 break;
710 default:
711 break;
712 }
713
714 if(view.dab->mode == DT_DRAWLAYER_BRUSH_MODE_SMUDGE)
715 {
716 for(int y = view.bounds.nw[1]; y < view.bounds.se[1]; y++)
717 {
718 for(int x = view.bounds.nw[0]; x < view.bounds.se[0]; x++)
719 {
720 float *pixel = buffer + 4 * ((size_t)y * width + x);
721 const float old_alpha = _clamp01(pixel[3]);
722 const dt_aligned_pixel_simd_t old_px = (old_alpha > 1e-8f) ? dt_load_simd(pixel) : dt_simd_set1(0.0f);
723 dt_drawlayer_brush_pixel_eval_t pixel_eval = { 0 };
724 if(!_prepare_analytic_pixel_context(&view, sample_opacity_scale,
725 stroke_mask_pixels, stroke_mask_width, stroke_mask_height,
726 x, y, old_alpha, &pixel_eval))
727 continue;
728
729 const dt_aligned_pixel_simd_t out_px
730 = _apply_smudge_stroke_mode(source_buffer, source_width, source_height, source_origin_x,
731 source_origin_y, runtime_private, &view,
732 scale, origin_x, origin_y,
733 x, y, pixel_eval.src_alpha, old_px);
734 dt_store_simd(pixel, out_px);
735
736 if(pixel_eval.stroke_alpha)
737 {
738 *pixel_eval.stroke_alpha
739 = pixel_eval.src_alpha
740 + pixel_eval.stroke_old_alpha * (1.0f - pixel_eval.src_alpha);
741 }
742 }
743 }
744 }
745 else
746 {
747#if defined(_OPENMP) && !OUTER_LOOP
748#pragma omp parallel for default(firstprivate) collapse(2)
749#endif
750 for(int y = view.bounds.nw[1]; y < view.bounds.se[1]; y++)
751 {
752 for(int x = view.bounds.nw[0]; x < view.bounds.se[0]; x++)
753 {
754 float *pixel = buffer + 4 * ((size_t)y * width + x);
755 const float old_alpha = _clamp01(pixel[3]);
756 const dt_aligned_pixel_simd_t old_px = (old_alpha > 1e-8f) ? dt_load_simd(pixel) : dt_simd_set1(0.0f);
757 dt_drawlayer_brush_pixel_eval_t pixel_eval = { 0 };
758 if(!_prepare_analytic_pixel_context(&view, sample_opacity_scale,
759 stroke_mask_pixels, stroke_mask_width, stroke_mask_height,
760 x, y, old_alpha, &pixel_eval))
761 continue;
762
763 dt_aligned_pixel_simd_t out_px;
764 const dt_aligned_pixel_simd_t inv_alpha = dt_simd_set1(1.0f - pixel_eval.src_alpha);
765 switch(view.dab->mode)
766 {
768 out_px = old_px * inv_alpha;
769 break;
771 out_px = blur_px * dt_simd_set1(pixel_eval.src_alpha) + old_px * inv_alpha;
772 break;
774 default:
775 out_px = dt_load_simd(view.dab->color) * dt_simd_set1(pixel_eval.src_alpha) + old_px * inv_alpha;
776 break;
777 }
778
779 dt_store_simd(pixel, out_px);
780
781 if(pixel_eval.stroke_alpha)
782 {
783 *pixel_eval.stroke_alpha
784 = pixel_eval.src_alpha
785 + pixel_eval.stroke_old_alpha * (1.0f - pixel_eval.src_alpha);
786 }
787 }
788 }
789 }
790
791 return TRUE;
792}
793
799 const int width, const int height, const int stride,
800 const float center_x, const float center_y,
801 const float opacity_multiplier)
802{
803 /* Lightweight GUI preview dab renderer (ARGB8), independent from stroke logic. */
804 if(IS_NULL_PTR(dab) || IS_NULL_PTR(argb) || width <= 0 || height <= 0 || stride < 4 * width) return FALSE;
805
806 memset(argb, 0, (size_t)stride * height);
807 if(dab->radius <= 0.0f) return FALSE;
808
810 preview.opacity = _clamp01(preview.opacity * _clamp01(opacity_multiplier));
811 if(preview.opacity <= 0.0f) return FALSE;
812
813 const float radius = fmaxf(preview.radius, 0.5f);
814 const float inv_radius = 1.0f / radius;
815 const int x0 = (int)fmaxf(0.0f, floorf(center_x - radius));
816 const int y0 = (int)fmaxf(0.0f, floorf(center_y - radius));
817 const int x1 = (int)fminf((float)width, ceilf(center_x + radius) + 1.0f);
818 const int y1 = (int)fminf((float)height, ceilf(center_y + radius) + 1.0f);
819
820 const float disp_r = _clamp01(preview.display_color[0]);
821 const float disp_g = _clamp01(preview.display_color[1]);
822 const float disp_b = _clamp01(preview.display_color[2]);
823 dt_drawlayer_sprinkle_preview_t sprinkle = { 0 };
824 _prepare_sprinkle_preview(&preview, center_x, center_y, radius, &sprinkle);
825
826 for(int y = y0; y < y1; y++)
827 {
828 const float dy = ((float)y + 0.5f - center_y) * inv_radius;
829 const float dy2 = dy * dy;
830 for(int x = x0; x < x1; x++)
831 {
832 const float dx = ((float)x + 0.5f - center_x) * inv_radius;
833 const float profile = dt_drawlayer_brush_profile_eval(&preview, dx * dx + dy2);
834 if(profile <= 0.0f) continue;
835
836 const float alpha_noise = _sample_sprinkle_preview(&sprinkle, x, y);
837 const float alpha = _clamp01(preview.opacity * profile * alpha_noise);
838 if(alpha <= 0.0f) continue;
839
840 uint8_t *pixel = argb + (size_t)y * stride + 4 * x;
841 pixel[0] = (uint8_t)fminf(fmaxf(roundf(255.0f * _clamp01(disp_b * alpha)), 0.0f), 255.0f);
842 pixel[1] = (uint8_t)fminf(fmaxf(roundf(255.0f * _clamp01(disp_g * alpha)), 0.0f), 255.0f);
843 pixel[2] = (uint8_t)fminf(fmaxf(roundf(255.0f * _clamp01(disp_r * alpha)), 0.0f), 255.0f);
844 pixel[3] = (uint8_t)fminf(fmaxf(roundf(255.0f * alpha), 0.0f), 255.0f);
845 }
846 }
847
848 return TRUE;
849}
850
852 const int width, const int height,
853 const float center_x, const float center_y,
854 const float opacity_multiplier,
855 const float background_rgb[3])
856{
857 if(IS_NULL_PTR(dab) || IS_NULL_PTR(rgba) || width <= 0 || height <= 0) return FALSE;
858
859 const float bg_r = background_rgb ? _clamp01(background_rgb[0]) : 1.0f;
860 const float bg_g = background_rgb ? _clamp01(background_rgb[1]) : 1.0f;
861 const float bg_b = background_rgb ? _clamp01(background_rgb[2]) : 1.0f;
862 for(int i = 0; i < width * height; i++)
863 {
864 rgba[4 * i + 0] = bg_r;
865 rgba[4 * i + 1] = bg_g;
866 rgba[4 * i + 2] = bg_b;
867 rgba[4 * i + 3] = 1.0f;
868 }
869
870 if(dab->radius <= 0.0f) return FALSE;
871
873 preview.opacity = _clamp01(preview.opacity * _clamp01(opacity_multiplier));
874 if(preview.opacity <= 0.0f) return FALSE;
875
876 const float radius = fmaxf(preview.radius, 0.5f);
877 const float inv_radius = 1.0f / radius;
878 const int x0 = (int)fmaxf(0.0f, floorf(center_x - radius));
879 const int y0 = (int)fmaxf(0.0f, floorf(center_y - radius));
880 const int x1 = (int)fminf((float)width, ceilf(center_x + radius) + 1.0f);
881 const int y1 = (int)fminf((float)height, ceilf(center_y + radius) + 1.0f);
882
883 const float src_r = _clamp01(preview.color[0]);
884 const float src_g = _clamp01(preview.color[1]);
885 const float src_b = _clamp01(preview.color[2]);
886 dt_drawlayer_sprinkle_preview_t sprinkle = { 0 };
887 _prepare_sprinkle_preview(&preview, center_x, center_y, radius, &sprinkle);
888
889 for(int y = y0; y < y1; y++)
890 {
891 const float dy = ((float)y + 0.5f - center_y) * inv_radius;
892 const float dy2 = dy * dy;
893 for(int x = x0; x < x1; x++)
894 {
895 const float dx = ((float)x + 0.5f - center_x) * inv_radius;
896 const float profile = dt_drawlayer_brush_profile_eval(&preview, dx * dx + dy2);
897 if(profile <= 0.0f) continue;
898
899 const float alpha_noise = _sample_sprinkle_preview(&sprinkle, x, y);
900 const float alpha = _clamp01(preview.opacity * profile * alpha_noise);
901 if(alpha <= 0.0f) continue;
902
903 float *pixel = rgba + 4 * ((size_t)y * width + x);
904 const float inv_alpha = 1.0f - alpha;
905 pixel[0] = src_r * alpha + pixel[0] * inv_alpha;
906 pixel[1] = src_g * alpha + pixel[1] * inv_alpha;
907 pixel[2] = src_b * alpha + pixel[2] * inv_alpha;
908 pixel[3] = 1.0f;
909 }
910 }
911
912 return TRUE;
913}
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
Dab-level brush rasterization API for drawlayer.
@ DT_DRAWLAYER_BRUSH_MODE_ERASE
Definition brush.h:50
@ DT_DRAWLAYER_BRUSH_MODE_PAINT
Definition brush.h:49
@ DT_DRAWLAYER_BRUSH_MODE_BLUR
Definition brush.h:51
@ DT_DRAWLAYER_BRUSH_MODE_SMUDGE
Definition brush.h:52
Inline brush profile and mass primitives shared by paint/brush code.
static float dt_drawlayer_brush_profile_eval(const dt_drawlayer_brush_dab_t *dab, const float norm2)
Evaluate normalized brush profile at squared normalized radius.
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
return vector dt_simd_set1(valid ?(scaling+NORM_MIN) :NORM_MIN)
static const float x
const int t
const float v
static float strength(float value, float strength)
Definition colorzones.c:431
static unsigned int splitmix32(const unsigned long seed)
GtkWidget * preview
what the selected row actually captures
GtkWidget * view
the tree view
static gboolean _brush_runtime_view_from_state(const dt_drawlayer_paint_stroke_t *stroke, const dt_drawlayer_brush_dab_t *dab, const int origin_x, const int origin_y, const float scale, dt_drawlayer_brush_runtime_view_t *view)
Build immutable per-dab raster view from stroke runtime state.
static float _cellular_grain_2d(const uint64_t seed, const float x, const float y)
Grain-like round cellular field. Peaks at grain centers, falls off radially.
gboolean dt_drawlayer_brush_rasterize_dab_rgbaf(const dt_drawlayer_brush_dab_t *dab, float *rgba, const int width, const int height, const float center_x, const float center_y, const float opacity_multiplier, const float background_rgb[3])
Rasterize a single dab preview in linear float RGBA over an opaque background.
static float _sample_alpha_noise_raw(const dt_drawlayer_brush_dab_t *dab, const dt_drawlayer_brush_runtime_view_t *view, const int pixel_x, const int pixel_y)
Resolve multiplicative alpha noise at one pixel.
static void _prepare_sprinkle_preview(const dt_drawlayer_brush_dab_t *dab, const float center_x, const float center_y, const float radius, dt_drawlayer_sprinkle_preview_t *preview)
static gboolean _prepare_blur_context(dt_aligned_pixel_simd_t *blur_px, const float *buffer, const int width, const int height, const int source_origin_x, const int source_origin_y, const int patch_origin_x, const int patch_origin_y, const dt_drawlayer_brush_runtime_view_t *view)
Build blur gather color for current dab footprint.
static float _smudge_hash_signed(const int x, const int y, const int lane)
Stable signed pseudo-random helper in [-1,1].
static float _clamp01(const float v)
Clamp scalar to [0,1].
static float _smudge_deposit_alpha(const float src_alpha, const float carried_alpha, const float opacity)
Resolve effective smudge deposit alpha for one pixel.
static float _stroke_flow_alpha(const dt_drawlayer_brush_dab_t *dab, const float opacity, const float flow, const float sample_opacity_scale, const float profile, const float brush_alpha, const float old_alpha, const float stroke_old_alpha, const gboolean have_stroke_alpha)
Compute per-pixel source alpha from opacity/flow model.
gboolean dt_drawlayer_brush_rasterize_dab_argb8(const dt_drawlayer_brush_dab_t *dab, uint8_t *argb, const int width, const int height, const int stride, const float center_x, const float center_y, const float opacity_multiplier)
Render one dab to 8-bit ARGB surface for GUI cursor preview.
static void _sprinkle_octave_weights(const float coarseness, float *w0, float *w1, float *w2)
Resolve octave weights from coarseness control.
static float _sprinkle_noise_at_pixel_precomputed(const float px, const float py, const float scale, const float strength, const float w0, const float w1, const float w2, const uint64_t seed0, const uint64_t seed1, const uint64_t seed2)
Evaluate sprinkle modulation from precomputed dab constants.
static float _estimate_alpha_noise_gain(const dt_drawlayer_brush_dab_t *dab, const dt_drawlayer_brush_runtime_view_t *view)
Estimate per-dab texture gain so noise preserves average opacity across samples.
static float _sample_sprinkle_preview(const dt_drawlayer_sprinkle_preview_t *preview, const float px, const float py)
static float _cell_hash01_from_seed(const uint64_t cell_seed, const uint64_t salt)
Stable scalar hash in [0,1] from precomputed cell seed.
static float _lerpf(const float a, const float b, const float t)
Linear interpolation helper.
static dt_aligned_pixel_simd_t _apply_smudge_stroke_mode(const float *source_buffer, const int source_width, const int source_height, const int source_origin_x, const int source_origin_y, dt_drawlayer_paint_stroke_t *runtime_private, const dt_drawlayer_brush_runtime_view_t *view, const float scale, const int patch_origin_x, const int patch_origin_y, const int x, const int y, const float src_alpha, const dt_aligned_pixel_simd_t old_px)
Apply smudge mode for one pixel and update carried sample.
static gboolean _prepare_analytic_pixel_context(const dt_drawlayer_brush_runtime_view_t *view, const float sample_opacity_scale, float *stroke_mask, const int stroke_mask_width, const int stroke_mask_height, const int x, const int y, const float old_alpha, dt_drawlayer_brush_pixel_eval_t *pixel_eval)
Compute full analytic per-pixel brush context.
gboolean dt_drawlayer_brush_rasterize(const dt_drawlayer_cache_patch_t *sample_patch, dt_drawlayer_cache_patch_t *patch, const float scale, const dt_drawlayer_brush_dab_t *dab, const float sample_opacity_scale, dt_drawlayer_cache_patch_t *stroke_mask, dt_drawlayer_paint_stroke_t *runtime_private)
Public dab rasterization entry point.
Patch/cache helpers for drawlayer process and preview buffers.
float * dt_drawlayer_paint_runtime_smudge_pixels(dt_drawlayer_paint_stroke_t *state)
Get smudge carry buffer pointer (RGBA float).
gboolean dt_drawlayer_paint_runtime_have_smudge_pickup(const dt_drawlayer_paint_stroke_t *state)
Query whether smudge pickup coordinates are initialized.
void dt_drawlayer_paint_runtime_get_smudge_pickup(const dt_drawlayer_paint_stroke_t *state, float *x, float *y)
Read smudge pickup coordinates.
int dt_drawlayer_paint_runtime_smudge_width(const dt_drawlayer_paint_stroke_t *state)
Get smudge carry buffer width.
gboolean dt_drawlayer_paint_runtime_prepare_dab_context(dt_drawlayer_paint_stroke_t *state, const dt_drawlayer_brush_dab_t *dab, const int width, const int height, const int origin_x, const int origin_y, const float scale)
Compute current dab footprint bounds in target buffer coordinates.
gboolean dt_drawlayer_paint_runtime_ensure_smudge_pixels(dt_drawlayer_paint_stroke_t *state, const int width, const int height)
Ensure smudge carry buffer allocation for given footprint dimensions.
Stroke-level path sampling and runtime-state API for drawlayer.
#define w2
Definition lmmse.c:60
#define w1
Definition lmmse.c:59
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:96
uint32_t width
Definition mipmap_cache.c:0
uint32_t height
Definition mipmap_cache.c:1
dt_store_simd(out, value)
float dt_aligned_pixel_simd_t __attribute__((vector_size(16), aligned(16)))
Apply one channel's tone curve to each of the three colour channels, or pass the channel through unto...
Definition simd.h:55
unsigned __int64 uint64_t
Definition strptime.c:75
Fully resolved input dab descriptor.
Definition brush.h:65
uint32_t stroke_batch
Definition brush.h:85
dt_drawlayer_damaged_rect_t bounds
const dt_drawlayer_brush_dab_t * dab
Generic float RGBA patch stored either in malloc memory or pixel cache.
Integer axis-aligned rectangle in buffer coordinates.
Mutable stroke runtime state owned by worker/backend code.
dt_drawlayer_damaged_rect_t bounds
#define MIN(a, b)
Definition thinplate.c:32