Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
gradient.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2013, 2016, 2019-2021 Pascal Obry.
4 Copyright (C) 2013-2017 Tobias Ellinghaus.
5 Copyright (C) 2013-2015, 2019-2020 Ulrich Pegelow.
6 Copyright (C) 2014-2016, 2021 Roman Lebedev.
7 Copyright (C) 2016, 2019, 2021 Aldric Renaudin.
8 Copyright (C) 2018 Edgardo Hoszowski.
9 Copyright (C) 2018 johannes hanika.
10 Copyright (C) 2019 Andreas Schneider.
11 Copyright (C) 2019, 2023, 2025-2026 Aurélien PIERRE.
12 Copyright (C) 2020-2022 Chris Elston.
13 Copyright (C) 2020 GrahamByrnes.
14 Copyright (C) 2020-2021 Hubert Kowalski.
15 Copyright (C) 2020 Paolo DePetrillo.
16 Copyright (C) 2020-2021 Ralf Brown.
17 Copyright (C) 2022 Martin Bařinka.
18 Copyright (C) 2025-2026 Guillaume Stutin.
19
20 darktable is free software: you can redistribute it and/or modify
21 it under the terms of the GNU General Public License as published by
22 the Free Software Foundation, either version 3 of the License, or
23 (at your option) any later version.
24
25 darktable is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with darktable. If not, see <http://www.gnu.org/licenses/>.
32*/
33#include "bauhaus/bauhaus.h"
34#include "common/debug.h"
35#include "common/undo.h"
36#include "control/conf.h"
37#include "develop/blend.h"
38#include "develop/imageop.h"
39#include "develop/masks.h"
41
42#define extent_MIN 0.0005f
43#define extent_MAX 1.0f
44#define CURVATURE_MIN -2.0f
45#define CURVATURE_MAX 2.0f
46
47#define BORDER_MIN 0.00005f
48#define BORDER_MAX 0.5f
49
50// Helper function to find the INFINITY separator in border array
51static int _find_border_separator(const float *border, int count)
52{
53
54 if(IS_NULL_PTR(border) || count <= 0) return -1;
55
56#ifdef _OPENMP
57 int found = count;
58#pragma omp parallel for reduction(min:found) if(count > 1000)
59 for(int i = 0; i < count; i++)
60 {
61 if(!isfinite(border[i * 2]) && !isfinite(border[i * 2 + 1]))
62 found = i;
63 }
64 return (found == count) ? -1 : found;
65#else
66 for(int i = 0; i < count; i++)
67 {
68 if(!isfinite(border[i * 2]) && !isfinite(border[i * 2 + 1]))
69 return i;
70 }
71 return -1;
72#endif
73}
74
75
76// Helper function to find closest point on a line segment to a given point
77static void _closest_point_on_segment(float px, float py, float x1, float y1, float x2, float y2,
78 float *closest_x, float *closest_y, float *distance_sq)
79{
80 const float seg_dx = x2 - x1;
81 const float seg_dy = y2 - y1;
82 const float seg_length_sq = seg_dx * seg_dx + seg_dy * seg_dy;
83
84 if(seg_length_sq < 1e-10f)
85 {
86 // Degenerate segment, return first point
87 *closest_x = x1;
88 *closest_y = y1;
89 *distance_sq = (px - x1) * (px - x1) + (py - y1) * (py - y1);
90 return;
91 }
92
93 // Project point onto line segment (clamped to [0,1])
94 const float t = fmaxf(0.0f, fminf(1.0f,
95 ((px - x1) * seg_dx + (py - y1) * seg_dy) / seg_length_sq));
96
97 *closest_x = x1 + t * seg_dx;
98 *closest_y = y1 + t * seg_dy;
99 *distance_sq = (px - *closest_x) * (px - *closest_x) + (py - *closest_y) * (py - *closest_y);
100}
101
102// Helper function to find closest point on a polyline to a given point
103static void _closest_point_on_line(float px, float py, const float *border, int start_idx, int end_idx,
104 float *closest_x, float *closest_y, float *min_distance_sq)
105{
106 *min_distance_sq = FLT_MAX;
107 *closest_x = *closest_y = 0.0f;
108
109 if(start_idx >= end_idx - 1) return;
110
111#ifdef _OPENMP
112 float global_min = FLT_MAX;
113 float global_x = 0.0f, global_y = 0.0f;
114
115#pragma omp parallel
116 {
117 float local_min = FLT_MAX;
118 float local_x = 0.0f, local_y = 0.0f;
119
120#pragma omp for nowait
121 for(int i = start_idx; i < end_idx - 1; i++)
122 {
123 float seg_closest_x, seg_closest_y, seg_dist_sq;
125 border[i * 2], border[i * 2 + 1],
126 border[(i + 1) * 2], border[(i + 1) * 2 + 1],
127 &seg_closest_x, &seg_closest_y, &seg_dist_sq);
128
129 if(seg_dist_sq < local_min)
130 {
131 local_min = seg_dist_sq;
132 local_x = seg_closest_x;
133 local_y = seg_closest_y;
134 }
135 }
136
137 if(local_min < global_min)
138 {
139#pragma omp critical
140 {
141 if(local_min < global_min)
142 {
143 global_min = local_min;
144 global_x = local_x;
145 global_y = local_y;
146 }
147 }
148 }
149 } // end parallel
150
151 *min_distance_sq = global_min;
152 *closest_x = global_x;
153 *closest_y = global_y;
154#else
155 for(int i = start_idx; i < end_idx - 1; i++)
156 {
157 float seg_closest_x, seg_closest_y, seg_dist_sq;
159 border[i * 2], border[i * 2 + 1],
160 border[(i + 1) * 2], border[(i + 1) * 2 + 1],
161 &seg_closest_x, &seg_closest_y, &seg_dist_sq);
162
163 if(seg_dist_sq < *min_distance_sq)
164 {
165 *min_distance_sq = seg_dist_sq;
166 *closest_x = seg_closest_x;
167 *closest_y = seg_closest_y;
168 }
169 }
170#endif
171}
172
174{
175 const float gradient_dx = gpt->points[2] - gpt->points[0];
176 const float gradient_dy = gpt->points[3] - gpt->points[1];
177 return gradient_dx * gradient_dx + gradient_dy * gradient_dy;
178}
179
186
188{
189 values->extent = CLAMPF(dt_conf_get_float("plugins/darkroom/masks/gradient/extent"),
191 values->curvature = CLAMPF(dt_conf_get_float("plugins/darkroom/masks/gradient/curvature"),
193 values->rotation = dt_conf_get_float("plugins/darkroom/masks/gradient/rotation");
194 if(!isfinite(values->rotation)) values->rotation = 0.0f;
195}
196
198{
202 gradient->extent = values.extent;
203 gradient->curvature = values.curvature;
204 gradient->rotation = values.rotation;
205}
206
207static int _gradient_get_points(dt_develop_t *dev, float x, float y, float rotation, float curvature,
208 float **points, int *points_count);
209static int _gradient_get_pts_border(dt_develop_t *dev, float x, float y, float rotation, float distance,
210 float curvature, float **points, int *points_count);
211
212// Gradient creation preview uses the same temp-buffer contract as circle/ellipse,
213// with the shape-specific geometry generation kept here.
215{
218
219 float center[2];
221
222 *preview = (dt_masks_preview_buffers_t){ 0 };
223 int err = _gradient_get_points(darktable.develop, center[0], center[1], values.rotation,
224 values.curvature, &preview->points, &preview->points_count);
225 if(!err && values.extent > 0.0f)
226 err = _gradient_get_pts_border(darktable.develop, center[0], center[1], values.rotation,
227 values.extent, values.curvature, &preview->border,
228 &preview->border_count);
229 return err;
230}
231
232static void _gradient_get_distance(float x, float y, float dist_mouse, dt_masks_form_gui_t *gui, int index,
233 int num_points, int *inside, int *inside_border, int *near,
234 int *inside_source, float *dist)
235{
236 // initialise returned values
237 *inside_source = 0;
238 *inside = 0;
239 *inside_border = 0;
240 *near = -1;
241 *dist = FLT_MAX;
242 const float sqr_dist_mouse = dist_mouse * dist_mouse;
243
244 const dt_masks_form_gui_points_t *gpt = (dt_masks_form_gui_points_t *)g_list_nth_data(gui->points, index);
245 if(IS_NULL_PTR(gpt)) return;
246
247 float min_dist = FLT_MAX;
248
249 // check if we are between the two border lines
250 if(!gui->form_rotating && !gui->form_dragging && gpt->border && gpt->border_count > 6
251 && gpt->points && gpt->points_count >= 4)
252 {
253 const int separator_idx = _find_border_separator(gpt->border, gpt->border_count);
254 if(separator_idx > 0 && separator_idx < gpt->border_count - 1)
255 {
256 // Get gradient direction from segment (points[0],points[1]) to (points[2],points[3])
257 const float gradient_len_sq = _gradient_get_border_len_sq(gpt);
258
259 if(gradient_len_sq > 1e-12f)
260 {
261 // Find closest points on both lines
262 float closest_x1, closest_y1, dist1_sq;
263 float closest_x2, closest_y2, dist2_sq;
264
265 _closest_point_on_line(x, y, gpt->border, 0, separator_idx,
266 &closest_x1, &closest_y1, &dist1_sq);
267
268 _closest_point_on_line(x, y, gpt->border, separator_idx + 1, gpt->border_count,
269 &closest_x2, &closest_y2, &dist2_sq);
270
271 // Check if we have valid closest points to both border lines.
272 if(dist1_sq < FLT_MAX && dist2_sq < FLT_MAX)
273 {
274 // Vectors from mouse to each closest point
275 const float to_line1_x = closest_x1 - x;
276 const float to_line1_y = closest_y1 - y;
277 const float to_line2_x = closest_x2 - x;
278 const float to_line2_y = closest_y2 - y;
279
280 const float gradient_dx = gpt->points[2] - gpt->points[0];
281 const float gradient_dy = gpt->points[3] - gpt->points[1];
282 // Project these vectors onto the (unnormalized) gradient direction.
283 // Using the unnormalized direction preserves sign, so we avoid sqrt().
284 const float proj1 = to_line1_x * gradient_dx + to_line1_y * gradient_dy;
285 const float proj2 = to_line2_x * gradient_dx + to_line2_y * gradient_dy;
286
287 // Mouse is between lines if projections have opposite signs.
288 const gboolean between_lines = (proj1 * proj2 < 0.0f);
289 if(between_lines) *inside_border = 1;
290
291 // Rotation handle: accept hits on the border lines and slightly beyond.
292 const float min_dist_sq = fminf(dist1_sq, dist2_sq);
293 float handle_radius_sq = CLAMPF(gradient_len_sq * 0.125f, sqr_dist_mouse, sqr_dist_mouse * 5);
294
295 if(min_dist_sq <= handle_radius_sq)
296 *inside = 1;
297 }
298 }
299 }
300 }
301
302 // and we check if we are near a segment (single continuous segment starting at gpt->points[3])
303 if(gpt->points && gpt->points_count > 3)
304 {
305 for(int i = 3; i < gpt->points_count; i++)
306 {
307 const float xx = gpt->points[i * 2];
308 const float yy = gpt->points[i * 2 + 1];
309
310 const float dx = x - xx;
311 const float dy = y - yy;
312 const float dd = sqf(dx) + sqf(dy);
313
314 min_dist = fminf(min_dist, dd);
315
316 // only one segment present: if any guide point is within the mouse distance,
317 // mark the (only) segment as near (index 0)
318 if(dd < sqr_dist_mouse)
319 *near = 0;
320 }
321 }
322
323 *dist = min_dist;
324}
325
326static void _gradient_node_position_cb(const dt_masks_form_gui_points_t *gui_points, int node_index,
327 float *node_x, float *node_y, void *user_data)
328{
329 if(node_x) *node_x = NAN;
330 if(node_y) *node_y = NAN;
331}
332
333static void _gradient_distance_cb(float pointer_x, float pointer_y, float cursor_radius,
334 dt_masks_form_gui_t *mask_gui, int form_index, int node_count, int *inside,
335 int *inside_border, int *near, int *inside_source, float *dist, void *user_data)
336{
337 _gradient_get_distance(pointer_x, pointer_y, cursor_radius, mask_gui, form_index, 0, inside,
338 inside_border, near, inside_source, dist);
339}
340
341static void _gradient_post_select_cb(dt_masks_form_gui_t *mask_gui, int inside, int inside_border,
342 int inside_source, void *user_data)
343{
344 if(inside)
345 {
346 mask_gui->border_selected = FALSE;
347 mask_gui->pivot_selected = TRUE;
348 }
349 else if(inside_border)
350 {
351 mask_gui->pivot_selected = FALSE;
352 }
353}
354
355static int _find_closest_handle(dt_masks_form_t *mask_form, dt_masks_form_gui_t *mask_gui, int index)
356{
357 if(mask_gui) mask_gui->pivot_selected = FALSE;
358 return dt_masks_find_closest_handle_common(mask_form, mask_gui, index, 1,
359 NULL, NULL, _gradient_node_position_cb,
361}
362
363
364static int _init_extent(dt_masks_form_t *form, const float amount, const dt_masks_increment_t increment, const int flow)
365{
367 increment, flow, _("extent: %3.2f%%"), 100.0f);
368 return 1;
369}
370
371static int _init_curvature(dt_masks_form_t *form, const float amount, const dt_masks_increment_t increment, const int flow)
372{
374 increment, flow, _("Curvature: %3.2f%%"), 50.f);
375 return 1;
376}
377
378static int _init_opacity(dt_masks_form_t *form, const float amount, const dt_masks_increment_t increment, const int flow)
379{
380 dt_masks_get_set_conf_value_with_toast(form, "opacity", amount, 0.f, 1.f,
381 increment, flow, _("Opacity: %3.2f%%"), 100.f);
382 return 1;
383}
384
385static int _init_rotation(dt_masks_form_t *form, const float amount, const dt_masks_increment_t increment, const int flow)
386{
387 dt_masks_get_set_conf_value_with_toast(form, "rotation", amount, 0.f, 360.f,
388 increment, flow, _("Rotation: %3.2f\302\260"), 1.0f);
389 return 1;
390}
391
393{
394 if(IS_NULL_PTR(form) || IS_NULL_PTR(form->points)) return NAN;
395 const dt_masks_anchor_gradient_t *gradient = (const dt_masks_anchor_gradient_t *)(form->points)->data;
396 if(IS_NULL_PTR(gradient)) return NAN;
397
398 switch(interaction)
399 {
401 return gradient->extent;
403 return gradient->curvature;
404 default:
405 return NAN;
406 }
407}
408
409static gboolean _gradient_get_gravity_center(const dt_masks_form_t *form, float center[2], float *area)
410{
411 if(IS_NULL_PTR(form) || IS_NULL_PTR(form->points) || IS_NULL_PTR(center) || IS_NULL_PTR(area)) return FALSE;
412 const dt_masks_anchor_gradient_t *gradient = (const dt_masks_anchor_gradient_t *)(form->points)->data;
413 if(IS_NULL_PTR(gradient)) return FALSE;
414 center[0] = gradient->center[0];
415 center[1] = gradient->center[1];
416 *area = gradient->extent; // pretend it's a rectangle of unit width
417 return TRUE;
418}
419
420static int _change_extent(dt_masks_form_t *form, dt_masks_form_gui_t *gui, struct dt_iop_module_t *module,
421 int index, const float amount, const dt_masks_increment_t increment, const int flow);
422static int _change_curvature(dt_masks_form_t *form, dt_masks_form_gui_t *gui, struct dt_iop_module_t *module,
423 int index, const float amount, const dt_masks_increment_t increment, const int flow);
424
426 dt_masks_increment_t increment, int flow,
427 dt_masks_form_gui_t *gui, struct dt_iop_module_t *module)
428{
429 if(IS_NULL_PTR(form)) return NAN;
430 const int index = 0;
431
432 switch(interaction)
433 {
435 if(!_change_extent(form, gui, module, index, value, increment, flow)) return NAN;
436 return _gradient_get_interaction_value(form, interaction);
438 if(!_change_curvature(form, gui, module, index, value, increment, flow)) return NAN;
439 return _gradient_get_interaction_value(form, interaction);
440 default:
441 return NAN;
442 }
443}
444
445static int _change_extent(dt_masks_form_t *form, dt_masks_form_gui_t *gui, struct dt_iop_module_t *module, int index, const float amount, const dt_masks_increment_t increment, const int flow)
446{
447 if(IS_NULL_PTR(form) || IS_NULL_PTR(form->points)) return 0;
449 if(IS_NULL_PTR(gradient)) return 0;
450
451 gradient->extent = CLAMPF(dt_masks_apply_increment(gradient->extent, amount, increment, flow),
453
454 _init_extent(form, amount, increment, flow);
455
456 // we recreate the form points
457 dt_masks_gui_form_create(form, gui, index, module);
458
459 return 1;
460}
461
462static int _change_curvature(dt_masks_form_t *form, dt_masks_form_gui_t *gui, struct dt_iop_module_t *module, int index, const float amount, const dt_masks_increment_t increment, const int flow)
463{
464 if(IS_NULL_PTR(form) || IS_NULL_PTR(form->points)) return 0;
466 if(IS_NULL_PTR(gradient)) return 0;
467
468 // Sanitize
469 // do not exceed upper limit of 2.0 and lower limit of -2.0
470 if(amount > 2.0f && (gradient->curvature > 2.0f ))
471 return 1;
472
473 const int node_hovered = gui->node_hovered;
474
475 // bending
476 if(node_hovered == -1 || node_hovered == 0)
477 {
478 gradient->curvature = dt_masks_apply_increment(gradient->curvature, amount, increment, flow);
479 }
480
481 _init_curvature(form, amount, DT_MASKS_INCREMENT_SCALE, flow);
482
483 // we recreate the form points
484 dt_masks_gui_form_create(form, gui, index, module);
485
486 return 1;
487}
488
489static int _change_rotation(dt_masks_form_t *form, dt_masks_form_gui_t *gui, struct dt_iop_module_t *module, int index, const float amount, const dt_masks_increment_t increment, const int flow)
490{
491 if(IS_NULL_PTR(form) || IS_NULL_PTR(form->points)) return 0;
493 if(IS_NULL_PTR(gradient)) return 0;
494
495 // Rotation
496 int flow_increased = (flow > 1) ? (flow - 1) * 5 : flow;
497 gradient->rotation = dt_masks_apply_increment(gradient->rotation, amount, increment, flow_increased);
498
499 // Ensure the rotation value warps within the interval [0, 360)
500 if(gradient->rotation > 360.f) gradient->rotation = fmodf(gradient->rotation, 360.f);
501 else if(gradient->rotation < 0.f) gradient->rotation = 360.f - fmodf(-gradient->rotation, 360.f);
502
503 _init_rotation(form, amount, DT_MASKS_INCREMENT_OFFSET, flow);
504
505 // we recreate the form points
506 dt_masks_gui_form_create(form, gui, index, module);
507
508 return 1;
509}
510
511/* Shape handlers receive widget-space coordinates, while normalized output-image
512 * coordinates come from `gui->rel_pos` and absolute output-image
513 * coordinates come from `gui->pos`. */
514static int _gradient_events_mouse_scrolled(struct dt_iop_module_t *module, double x, double y, int up, const int flow,
515 uint32_t state, dt_masks_form_t *form, int parentid,
516 dt_masks_form_gui_t *gui, int index, dt_masks_interaction_t interaction)
517{
518
519
520
521 if(gui->creation)
522 {
523 if(dt_modifier_is(state, GDK_SHIFT_MASK | GDK_CONTROL_MASK))
524 return _init_rotation(form, (up ? +0.2f : -0.2f), DT_MASKS_INCREMENT_OFFSET, flow);
525 else if(dt_modifier_is(state, GDK_CONTROL_MASK))
526 return _init_opacity(form, up ? +0.02f : -0.02f, DT_MASKS_INCREMENT_OFFSET, flow);
527 else if(dt_modifier_is(state, GDK_SHIFT_MASK))
528 return _init_curvature(form, up ? +0.02f : -0.02f, DT_MASKS_INCREMENT_OFFSET, flow);
529 else
530 return _init_extent(form, (up ? +1.02f : 0.98f), DT_MASKS_INCREMENT_SCALE, flow); // simple scroll to adjust curvature, calling func adjusts opacity with Ctrl
531 }
532 else if(gui->form_selected || gui->seg_selected || gui->pivot_selected)
533 {
534 if(dt_modifier_is(state, GDK_SHIFT_MASK | GDK_CONTROL_MASK))
535 return _change_rotation(form, gui, module, index, (up ? +0.2f : -0.2f), DT_MASKS_INCREMENT_OFFSET, flow);
536 else if(dt_modifier_is(state, GDK_CONTROL_MASK))
537 return dt_masks_form_change_opacity(form, parentid, up, flow);
538 else if(dt_modifier_is(state, GDK_SHIFT_MASK))
539 return _change_curvature(form, gui, module, index, (up ? +0.02f : -0.02f), DT_MASKS_INCREMENT_OFFSET, flow);
540 else
541 return _change_extent(form, gui, module, index, (up ? 1.02f : 0.98f), DT_MASKS_INCREMENT_SCALE, flow);
542 }
543 return 0;
544}
545
546static int _gradient_events_button_pressed(struct dt_iop_module_t *module, double x, double y,
547 double pressure, int which, int type, uint32_t state,
548 dt_masks_form_t *form, int parentid, dt_masks_form_gui_t *gui, int index)
549{
550 if(gui->creation)
551 {
552 if(which == 1)
553 {
554 if(dt_modifier_is(state, GDK_SHIFT_MASK))
555 {
556 gui->gradient_toggling = TRUE;
557 return 1;
558 }
559
560 dt_iop_module_t *crea_module = gui->creation_module;
561 // we create the gradient
563 if(IS_NULL_PTR(gradient)) return 0;
564 _gradient_init_new(gui, gradient);
565
566 form->points = g_list_append(form->points, gradient);
567 dt_masks_gui_form_save_creation(darktable.develop, crea_module, form, gui);
568
569 return 1;
570 }
571 }
572
573 else if(which == 1)
574 {
575 // double-click resets curvature
576 if(type == GDK_2BUTTON_PRESS)
577 {
578 _change_curvature(form, gui, module, index, 0, DT_MASKS_INCREMENT_ABSOLUTE, 0);
579 dt_masks_gui_form_create(form, gui, index, module);
580 return 1;
581 }
582
583 const dt_masks_form_gui_points_t *gpt = (dt_masks_form_gui_points_t *)g_list_nth_data(gui->points, index);
584 if(IS_NULL_PTR(gpt)) return 0;
585
586 else if((gui->form_selected || gui->seg_hovered >= 0 || gui->seg_selected)
587 && gui->edit_mode == DT_MASKS_EDIT_FULL)
588 {
589 // we start the form dragging or rotating
590 if(gui->pivot_selected)
591 gui->form_rotating = TRUE;
592 else if(dt_modifier_is(state, GDK_SHIFT_MASK))
593 gui->border_toggling = TRUE;
594 else if(gui->seg_hovered >= 0 || gui->seg_selected)
595 gui->form_selected = TRUE;
596
597 if(gui->form_rotating)
598 {
599 gui->delta[0] = gui->pos[0];
600 gui->delta[1] = gui->pos[1];
601 }
602 else
603 {
604 gui->delta[0] = gpt->points[0] - gui->pos[0];
605 gui->delta[1] = gpt->points[1] - gui->pos[1];
606 }
607
608 return 1;
609 }
610 }
611
612 return 0;
613}
614
615static int _gradient_events_button_released(struct dt_iop_module_t *module, double x, double y, int which,
616 uint32_t state, dt_masks_form_t *form, int parentid,
617 dt_masks_form_gui_t *gui, int index)
618{
619
620
621
622
623
624
625 if(IS_NULL_PTR(form) || IS_NULL_PTR(form->points)) return 0;
626
627 if(gui->form_dragging && gui->edit_mode == DT_MASKS_EDIT_FULL)
628 {
629 // we end the form dragging
630 return 1;
631 }
632
633 else if(gui->form_rotating && gui->edit_mode == DT_MASKS_EDIT_FULL)
634 {
635 // we end the form rotating
636 gui->form_rotating = FALSE;
637 return 1;
638 }
639 else if(gui->gradient_toggling)
640 {
641 // we get the gradient
642 dt_masks_anchor_gradient_t *gradient = (dt_masks_anchor_gradient_t *)((form->points)->data);
643 if(IS_NULL_PTR(gradient)) return 0;
644 // we end the gradient toggling
646
647 // toggle transition type of gradient
650 else
652
653 dt_conf_set_int("plugins/darkroom/masks/gradient/state", gradient->state);
654
655 // we recreate the form points
656 dt_masks_gui_form_create(form, gui, index, module);
657
658 // we save the new parameters
659
660 return 1;
661 }
662 return 0;
663}
664
665static int _gradient_events_key_pressed(struct dt_iop_module_t *module, GdkEventKey *event, dt_masks_form_t *form,
666 int parentid, dt_masks_form_gui_t *gui, int index)
667{
668 return 0;
669}
670
671static int _gradient_events_mouse_moved(struct dt_iop_module_t *module, double x, double y,
672 double pressure, int which, dt_masks_form_t *form, int parentid,
673 dt_masks_form_gui_t *gui, int index)
674{
675 if(gui->creation)
676 {
677 // Let the cursor motion be redrawn as it moves in GUI
678 return 1;
679 }
680
681 if(IS_NULL_PTR(form) || IS_NULL_PTR(form->points)) return 0;
682
683 // we get the gradient
684 dt_masks_anchor_gradient_t *gradient = (dt_masks_anchor_gradient_t *)((form->points)->data);
685 if(IS_NULL_PTR(gradient)) return 0;
686
687 // we need the reference points
688 dt_masks_form_gui_points_t *gpt = (dt_masks_form_gui_points_t *)g_list_nth_data(gui->points, index);
689 if(IS_NULL_PTR(gpt)) return 0;
690
691 if(gui->form_dragging)
692 {
693 // we change the center value
694 float pts[2];
696
697 gradient->center[0] = pts[0];
698 gradient->center[1] = pts[1];
699
700 // we recreate the form points
701 dt_masks_gui_form_create(form, gui, index, module);
702
703 return 1;
704 }
705
706 //rotation with the mouse
707 if(gui->form_rotating)
708 {
709 const float origin_point[2] = { gpt->points[0], gpt->points[1] };
710 const float angle = - dt_masks_rotate_with_anchor(darktable.develop, gui->pos, origin_point, gui);
711 _change_rotation(form, gui, module, index, angle , DT_MASKS_INCREMENT_OFFSET, 1);
712
713 // we recreate the form points
714 dt_masks_gui_form_create(form, gui, index, module);
715
716 return 1;
717 }
718 return 0;
719}
720
721// check if (x,y) lies within reasonable limits relative to image frame
722static inline gboolean _gradient_is_canonical(const float x, const float y, const float wd, const float ht)
723{
724 return (isnormal(x) && isnormal(y) && (x >= -wd) && (x <= 2 * wd) && (y >= -ht) && (y <= 2 * ht)) ? TRUE : FALSE;
725}
726
735static int _gradient_get_points(dt_develop_t *dev, float x, float y, float rotation, float curvature,
736 float **points, int *points_count)
737{
738 *points = NULL;
739 *points_count = 0;
740
741 const float wd = dev->roi.raw_width;
742 const float ht = dev->roi.raw_height;
743 if(!isfinite(wd) || !isfinite(ht) || wd <= 0.0f || ht <= 0.0f) return 1;
744
745 const float scale = sqrtf(wd * wd + ht * ht);
746 const float distance = 0.1f * fminf(wd, ht);
747
748 const float v = (-rotation / 180.0f) * M_PI;
749 const float cosv = cosf(v);
750 const float sinv = sinf(v);
751
752 const int count = sqrtf(wd * wd + ht * ht) + 3;
753 *points = dt_pixelpipe_cache_alloc_align_float_cache((size_t)2 * count, 0);
754 if(IS_NULL_PTR(*points)) return 1;
755
756 // we set the anchor point
757 float center[2] = { x, y };
759 const float center_x = center[0];
760 const float center_y = center[1];
761 (*points)[0] = center_x;
762 (*points)[1] = center_y;
763
764 // we set the pivot points
765 const float v1 = (-(rotation - 90.0f) / 180.0f) * M_PI;
766 const float x1 = center[0] + distance * cosf(v1);
767 const float y1 = center[1] + distance * sinf(v1);
768 (*points)[2] = x1;
769 (*points)[3] = y1;
770 const float v2 = (-(rotation + 90.0f) / 180.0f) * M_PI;
771 const float x2 = center[0] + distance * cosf(v2);
772 const float y2 = center[1] + distance * sinf(v2);
773 (*points)[4] = x2;
774 (*points)[5] = y2;
775
776 const int nthreads = darktable.num_openmp_threads;
777 size_t c_padded_size;
778 uint32_t *pts_count = dt_pixelpipe_cache_calloc_perthread(1, sizeof(uint32_t), &c_padded_size);
779 size_t pts_padded_size;
780 float *const restrict pts = dt_pixelpipe_cache_alloc_perthread_float((size_t)2 * count, &pts_padded_size);
781 if(IS_NULL_PTR(pts_count) || IS_NULL_PTR(pts))
782 {
786 *points = NULL;
787 *points_count = 0;
788 return 1;
789 }
790
791 // we set the line point
792 const float xstart = fabsf(curvature) > 1.0f ? -sqrtf(1.0f / fabsf(curvature)) : -1.0f;
793 const float xdelta = -2.0f * xstart / (count - 3);
794
795// gboolean in_frame = FALSE;
796 __OMP_PARALLEL_FOR__(if(count > 100) num_threads(nthreads))
797 for(int i = 3; i < count; i++)
798 {
799 const float xi = xstart + (i - 3) * xdelta;
800 const float yi = curvature * xi * xi;
801 const float xii = (cosv * xi + sinv * yi) * scale;
802 const float yii = (sinv * xi - cosv * yi) * scale;
803 const float xiii = xii + center_x;
804 const float yiii = yii + center_y;
805
806 // don't generate guide points if they extend too far beyond the image frame;
807 // this is to avoid that modules like lens correction fail on out of range coordinates
808 if(!(xiii < -wd || xiii > 2 * wd || yiii < -ht || yiii > 2 * ht))
809 {
810 uint32_t *tcount = dt_get_perthread(pts_count, c_padded_size);
811 float *const tpts = dt_get_perthread(pts, pts_padded_size);
812 tpts[*tcount * 2] = xiii;
813 tpts[*tcount * 2 + 1] = yiii;
814 (*tcount)++;
815 }
816 }
817
818 *points_count = 3;
819 for(int thread = 0; thread < nthreads; thread++)
820 {
821 const uint32_t tcount = *(uint32_t *)dt_get_bythread(pts_count, c_padded_size, thread);
822 const float *const tpts = dt_get_bythread(pts, pts_padded_size, thread);
823 // Merge only the retained in-frame samples. The source loop has at most
824 // count - 3 samples, so the three metadata points leave exactly that room.
825 for(uint32_t k = 0; k < tcount && *points_count < count; k++)
826 {
827 (*points)[(*points_count) * 2] = tpts[k * 2];
828 (*points)[(*points_count) * 2 + 1] = tpts[k * 2 + 1];
829 (*points_count)++;
830 }
831 }
832
835
836 // and we transform them with all distorted modules
837 if(!dt_dev_coordinates_raw_abs_to_image_abs(dev, *points, *points_count))
838 {
840 *points = NULL;
841 *points_count = 0;
842 return 1;
843 }
844
845 return 0;
846}
847
848// Helper function to copy points, skipping the first 3 metadata points
849static void _copy_points(float *dest, const float *src, int count, int *k)
850{
851 for(int i = 3; i < count; i++, (*k)++)
852 {
853 dest[(*k) * 2] = src[i * 2];
854 dest[(*k) * 2 + 1] = src[i * 2 + 1];
855 }
856}
857
858static int _gradient_get_pts_border(dt_develop_t *dev, float x, float y, float rotation, float distance,
859 float curvature, float **points, int *points_count)
860{
861 *points = NULL;
862 *points_count = 0;
863 distance = CLAMPF(distance, extent_MIN, extent_MAX);
864
865 // Get border curve dimensions and scaling
866 const float wd = dev->roi.raw_width;
867 const float ht = dev->roi.raw_height;
868 const float scale = sqrtf(wd * wd + ht * ht);
869
870 // Calculate perpendicular offsets (±90 degrees from rotation)
871 const float v1 = (-(rotation - 90.0f) / 180.0f) * M_PI;
872 const float v2 = (-(rotation + 90.0f) / 180.0f) * M_PI;
873
874 // Generate offset positions for both curves
875 float center[2] = { x, y };
877 float offsets[4] = { center[0] + distance * scale * cosf(v1),
878 center[1] + distance * scale * sinf(v1),
879 center[0] + distance * scale * cosf(v2),
880 center[1] + distance * scale * sinf(v2) };
882 const float x1 = offsets[0];
883 const float y1 = offsets[1];
884 const float x2 = offsets[2];
885 const float y2 = offsets[3];
886
887 // Get points for both curves
888 float *points1 = NULL, *points2 = NULL;
889 int points_count1 = 0, points_count2 = 0;
890 const int err1 = _gradient_get_points(dev, x1, y1, rotation, curvature, &points1, &points_count1);
891 const int err2 = _gradient_get_points(dev, x2, y2, rotation, curvature, &points2, &points_count2);
892
893 // Check which curves are valid (need more than 4 points: 3 metadata + at least 1 data)
894 const gboolean valid1 = (err1 == 0) && points_count1 > 4;
895 const gboolean valid2 = (err2 == 0) && points_count2 > 4;
896
897 int err = 1;
898
899 if(valid1 && valid2)
900 {
901 // Both curves valid - combine them with INFINITY separator
902 const int total_points = (points_count1 - 3) + (points_count2 - 3) + 1;
903 *points = dt_pixelpipe_cache_alloc_align_float_cache((size_t)2 * total_points, 0);
904 if(IS_NULL_PTR(*points)) goto cleanup;
905
906 *points_count = total_points;
907 int k = 0;
908
909 _copy_points(*points, points1, points_count1, &k);
910 (*points)[k * 2] = (*points)[k * 2 + 1] = INFINITY; // Separator
911 k++;
912 _copy_points(*points, points2, points_count2, &k);
913 err = 0;
914 }
915 else if(valid1)
916 {
917 // Only first curve valid
918 *points_count = points_count1 - 3;
919 *points = dt_pixelpipe_cache_alloc_align_float_cache((size_t)2 * (*points_count), 0);
920 if(IS_NULL_PTR(*points)) goto cleanup;
921
922 int k = 0;
923 _copy_points(*points, points1, points_count1, &k);
924 err = 0;
925 }
926 else if(valid2)
927 {
928 // Only second curve valid
929 *points_count = points_count2 - 3;
930 *points = dt_pixelpipe_cache_alloc_align_float_cache((size_t)2 * (*points_count), 0);
931 if(IS_NULL_PTR(*points)) goto cleanup;
932
933 int k = 0;
934 _copy_points(*points, points2, points_count2, &k);
935 err = 0;
936 }
937
938cleanup:
941 return err;
942}
943
944static void _gradient_draw_shape(cairo_t *cr, const float *pts_line, const int pts_line_count, const int nb, const gboolean border, const gboolean source)
945{
946 // safeguard in case of malformed arrays of points
947 if(border && pts_line_count <= 3) return;
948 if(!border && pts_line_count <= 4) return;
949
950 const float *points = (border) ? pts_line : pts_line + 6;
951 const int points_count = (border) ? pts_line_count : pts_line_count - 3;
952
953 const float wd = darktable.develop->roi.raw_width;
954 const float ht = darktable.develop->roi.raw_height;
955
956 int i = 0;
957 while(i < points_count)
958 {
959 const float px = points[i * 2];
960 const float py = points[i * 2 + 1];
961
962 if(!isnormal(px) || !_gradient_is_canonical(px, py, wd, ht))
963 {
964 i++;
965 continue;
966 }
967
968 cairo_move_to(cr, px, py);
969 i++;
970
971 // continue the current segment until a non-normal or out-of-range point
972 while(i < points_count)
973 {
974 const float qx = points[i * 2];
975 const float qy = points[i * 2 + 1];
976 if(!isnormal(qx) || !_gradient_is_canonical(qx, qy, wd, ht)) break;
977 cairo_line_to(cr, qx, qy);
978 i++;
979 }
980 }
981}
982
983static void _gradient_draw_arrow(cairo_t *cr, const gboolean selected, const gboolean pivot_selected, const gboolean is_rotating,
984 const float zoom_scale, float *pts, int pts_count)
985{
986 if(pts_count < 3) return;
987
988 const float anchor_x = pts[0];
989 const float anchor_y = pts[1];
990 const float pivot_end_x = pts[2];
991 const float pivot_end_y = pts[3];
992 const float pivot_start_x = pts[4];
993 const float pivot_start_y = pts[5];
994
995 // draw a dotted line across the gradient for better visibility while dragging
996 if(is_rotating)
997 {
998 // extend the axis line beyond the pivot points
999 const float scale = 1 / zoom_scale;
1000 const float dx = pivot_end_x - pivot_start_x;
1001 const float dy = pivot_end_y - pivot_start_y;
1002
1003 const float new_x1 = pivot_start_x - (dx * scale * 0.5f);
1004 const float new_y1 = pivot_start_y - (dy * scale * 0.5f);
1005 const float new_x2 = pivot_end_x + (dx * scale * 0.5f);
1006 const float new_y2 = pivot_end_y + (dy * scale * 0.5f);
1007 cairo_move_to(cr, new_x1, new_y1);
1008 cairo_line_to(cr, new_x2, new_y2);
1009
1010 dt_draw_stroke_line(DT_MASKS_DASH_ROUND, FALSE, cr, FALSE, zoom_scale, CAIRO_LINE_CAP_ROUND);
1011 }
1012
1013 // always draw arrow to clearly display the direction
1014 {
1015 // size & width of the arrow
1016 const float arrow_angle = 0.25f;
1017 const float arrow_length = (DT_DRAW_SCALE_ARROW * 2) / zoom_scale;
1018
1019 // compute direction from anchor toward pivot_end and build an arrow
1020 const float dx = pivot_end_x - anchor_x;
1021 const float dy = pivot_end_y - anchor_y;
1022 const float angle_dir = atan2f(dy, dx); // direction the arrow should point to
1023
1024 // tip of the arrow (ahead of anchor along angle_dir)
1025 const float tip_x = anchor_x + arrow_length * cosf(angle_dir);
1026 const float tip_y = anchor_y + arrow_length * sinf(angle_dir);
1027
1028 // half width of the arrow head
1029 const float half_w = arrow_length * tanf(arrow_angle);
1030
1031 // perpendicular vector to the direction (unit)
1032 const float nx = -sinf(angle_dir);
1033 const float ny = cosf(angle_dir);
1034
1035 // two corner points of the arrow base, centered on (anchor_x, anchor_y)
1036 const float arrow_x1 = anchor_x + nx * half_w;
1037 const float arrow_y1 = anchor_y + ny * half_w;
1038 const float arrow_x2 = anchor_x - nx * half_w;
1039 const float arrow_y2 = anchor_y - ny * half_w;
1040
1041 // we will draw the triangle as tip -> base1 -> base2
1042 cairo_move_to(cr, tip_x, tip_y);
1043 cairo_line_to(cr, arrow_x1, arrow_y1);
1044 cairo_line_to(cr, arrow_x2, arrow_y2);
1045 cairo_close_path(cr);
1046
1048 cairo_fill_preserve(cr);
1049 double line_width = pivot_selected ? (DT_DRAW_SIZE_LINE_SELECTED / zoom_scale) : (DT_DRAW_SIZE_LINE / zoom_scale);
1050 cairo_set_line_width(cr, line_width);
1052 cairo_stroke(cr);
1053 }
1054
1055 // draw the origin anchor point on top of everything
1056 dt_draw_node(cr, FALSE, FALSE, pivot_selected, zoom_scale, anchor_x, anchor_y);
1057}
1058
1059static void _gradient_events_post_expose(cairo_t *cr, float zoom_scale, dt_masks_form_gui_t *gui, int index, int nb)
1060{
1061 // preview gradient creation
1062 if(gui->creation)
1063 {
1065 if(_gradient_get_creation_preview(gui, &preview)) return;
1066
1067 dt_masks_draw_preview_shape(cr, zoom_scale, nb, preview.points, preview.points_count,
1068 preview.border, preview.border_count,
1069 &dt_masks_functions_gradient.draw_shape, CAIRO_LINE_CAP_ROUND,
1070 CAIRO_LINE_CAP_ROUND, FALSE, FALSE);
1071 _gradient_draw_arrow(cr, FALSE, FALSE, gui->form_rotating, zoom_scale, preview.points, preview.points_count);
1073
1074 return;
1075 }
1076
1077 const dt_masks_form_gui_points_t *gpt = (dt_masks_form_gui_points_t *)g_list_nth_data(gui->points, index);
1078 if(IS_NULL_PTR(gpt)) return;
1079
1080 const gboolean seg_selected = (gui->group_selected == index) && gui->seg_selected;
1081 const gboolean all_selected = (gui->group_selected == index) && (gui->form_selected || gui->form_dragging);
1082 // draw main line
1083 if(gpt->points && gpt->points_count > 0)
1084 dt_draw_shape_lines(DT_MASKS_NO_DASH, FALSE, cr, nb, (seg_selected), zoom_scale, gpt->points,
1085 gpt->points_count, &dt_masks_functions_gradient.draw_shape, CAIRO_LINE_CAP_ROUND);
1086 // draw borders
1087 if(gui->group_selected == index)
1088 {
1089 if(gpt->border && gpt->border_count > 0)
1090 dt_draw_shape_lines(DT_MASKS_DASH_STICK, FALSE, cr, nb, (gui->border_selected), zoom_scale, gpt->border,
1091 gpt->border_count, &dt_masks_functions_gradient.draw_shape, CAIRO_LINE_CAP_ROUND);
1092 }
1093
1094 if(gpt->points && gpt->points_count >= 3)
1095 _gradient_draw_arrow(cr, (seg_selected || all_selected), ((gui->group_selected == index) && gui->pivot_selected),
1096 gui->form_rotating, zoom_scale, gpt->points, gpt->points_count);
1097}
1098
1099static int _gradient_get_points_border(dt_develop_t *dev, dt_masks_form_t *form, float **points, int *points_count,
1100 float **border, int *border_count, int source,
1101 const dt_iop_module_t *module)
1102{
1103 // unused arg, keep compiler from complaining
1104 if(IS_NULL_PTR(form) || IS_NULL_PTR(form->points)) return 0;
1106 if(IS_NULL_PTR(gradient)) return 0;
1107 if(_gradient_get_points(dev, gradient->center[0], gradient->center[1], gradient->rotation, gradient->curvature,
1108 points, points_count) != 0)
1109 return 1;
1110 if(border)
1111 return _gradient_get_pts_border(dev, gradient->center[0], gradient->center[1],
1112 gradient->rotation, gradient->extent, gradient->curvature,
1113 border, border_count);
1114 return 0;
1115}
1116
1117static int _gradient_get_area(const dt_iop_module_t *const module, dt_dev_pixelpipe_t *pipe,
1118 const dt_dev_pixelpipe_iop_t *const piece,
1119 dt_masks_form_t *const form,
1120 int *width, int *height, int *posx, int *posy)
1121{
1122 const float wd = pipe->iwidth, ht = pipe->iheight;
1123
1124 float points[8] = { 0.0f, 0.0f, wd, 0.0f, wd, ht, 0.0f, ht };
1125
1126 // and we transform them with all distorted modules
1128 return 1;
1129
1130 // now we search min and max
1131 float xmin = 0.0f, xmax = 0.0f, ymin = 0.0f, ymax = 0.0f;
1132 xmin = ymin = FLT_MAX;
1133 xmax = ymax = FLT_MIN;
1134 for(int i = 0; i < 4; i++)
1135 {
1136 xmin = fminf(points[i * 2], xmin);
1137 xmax = fmaxf(points[i * 2], xmax);
1138 ymin = fminf(points[i * 2 + 1], ymin);
1139 ymax = fmaxf(points[i * 2 + 1], ymax);
1140 }
1141
1142 // and we set values
1143 *posx = xmin;
1144 *posy = ymin;
1145 *width = (xmax - xmin);
1146 *height = (ymax - ymin);
1147 return 0;
1148}
1149
1150// caller needs to make sure that input remains within bounds
1151static inline float dt_gradient_lookup(const float *lut, const float i)
1152{
1153 const int bin0 = i;
1154 const int bin1 = i + 1;
1155 const float f = i - bin0;
1156 return lut[bin1] * f + lut[bin0] * (1.0f - f);
1157}
1158
1159static int _gradient_get_mask(const dt_iop_module_t *const module, dt_dev_pixelpipe_t *pipe,
1160 const dt_dev_pixelpipe_iop_t *const piece,
1161 dt_masks_form_t *const form,
1162 float **buffer, int *width, int *height, int *posx, int *posy)
1163{
1164 if(IS_NULL_PTR(form) || IS_NULL_PTR(form->points)) return 0;
1165 double start2 = 0.0;
1167 // we get the area
1168 if(_gradient_get_area(module, pipe, piece, form, width, height, posx, posy) != 0) return 1;
1169
1171 {
1172 dt_print(DT_DEBUG_MASKS, "[masks %s] gradient area took %0.04f sec\n", form->name,
1173 dt_get_wtime() - start2);
1174 start2 = dt_get_wtime();
1175 }
1176
1177 // we get the gradient values
1178 dt_masks_anchor_gradient_t *gradient = (dt_masks_anchor_gradient_t *)((form->points)->data);
1179 if(IS_NULL_PTR(gradient)) return 0;
1180 // we create a buffer of grid points for later interpolation. mainly in order to reduce memory footprint
1181 const int w = *width;
1182 const int h = *height;
1183 const int px = *posx;
1184 const int py = *posy;
1185 const int grid = 8;
1186 const int gw = (w + grid - 1) / grid + 1;
1187 const int gh = (h + grid - 1) / grid + 1;
1188
1189 float *points = dt_pixelpipe_cache_alloc_align_float_cache((size_t)2 * gw * gh, 0);
1190 if(IS_NULL_PTR(points)) return 1;
1191 __OMP_PARALLEL_FOR__(collapse(2) if((size_t)gw * gh > 50000))
1192 for(int j = 0; j < gh; j++)
1193 for(int i = 0; i < gw; i++)
1194 {
1195 points[(j * gw + i) * 2] = (grid * i + px);
1196 points[(j * gw + i) * 2 + 1] = (grid * j + py);
1197 }
1198
1200 {
1201 dt_print(DT_DEBUG_MASKS, "[masks %s] gradient draw took %0.04f sec\n", form->name,
1202 dt_get_wtime() - start2);
1203 start2 = dt_get_wtime();
1204 }
1205
1206 // we backtransform all these points
1207 if(!dt_dev_distort_backtransform_plus(pipe, module->iop_order, DT_DEV_TRANSFORM_DIR_BACK_INCL, points, (size_t)gw * gh))
1208 {
1210 return 1;
1211 }
1212
1214 {
1215 dt_print(DT_DEBUG_MASKS, "[masks %s] gradient transform took %0.04f sec\n", form->name,
1216 dt_get_wtime() - start2);
1217 start2 = dt_get_wtime();
1218 }
1219
1220 // we calculate the mask at grid points and recycle point buffer to store results
1221 const float wd = pipe->iwidth;
1222 const float ht = pipe->iheight;
1223 const float hwscale = 1.0f / sqrtf(wd * wd + ht * ht);
1224 const float ihwscale = 1.0f / hwscale;
1225 const float v = (-gradient->rotation / 180.0f) * M_PI;
1226 const float sinv = sinf(v);
1227 const float cosv = cosf(v);
1228 const float xoffset = cosv * gradient->center[0] * wd + sinv * gradient->center[1] * ht;
1229 const float yoffset = sinv * gradient->center[0] * wd - cosv * gradient->center[1] * ht;
1230 const float extent = fmaxf(gradient->extent, 0.001f);
1231 const float normf = 1.0f / extent;
1232 const float curvature = gradient->curvature;
1233 const dt_masks_gradient_states_t state = gradient->state;
1234
1235 const int lutmax = ceilf(4 * extent * ihwscale);
1236 const int lutsize = 2 * lutmax + 2;
1238 if(IS_NULL_PTR(lut))
1239 {
1241 return 1;
1242 }
1243 __OMP_PARALLEL_FOR_SIMD__(if(lutsize > 1000) aligned(lut : 64))
1244 for(int n = 0; n < lutsize; n++)
1245 {
1246 const float distance = (n - lutmax) * hwscale;
1247 const float value = 0.5f + 0.5f * ((state == DT_MASKS_GRADIENT_STATE_LINEAR) ? normf * distance: erff(distance / extent));
1248 lut[n] = (value < 0.0f) ? 0.0f : ((value > 1.0f) ? 1.0f : value);
1249 }
1250
1251 // center lut around zero
1252 float *clut = lut + lutmax;
1253
1254
1255 __OMP_PARALLEL_FOR__(collapse(2) if((size_t)gw * gh > 50000))
1256 for(int j = 0; j < gh; j++)
1257 {
1258 for(int i = 0; i < gw; i++)
1259 {
1260 const float x = points[(j * gw + i) * 2];
1261 const float y = points[(j * gw + i) * 2 + 1];
1262
1263 const float x0 = (cosv * x + sinv * y - xoffset) * hwscale;
1264 const float y0 = (sinv * x - cosv * y - yoffset) * hwscale;
1265
1266 const float distance = y0 - curvature * x0 * x0;
1267
1268 points[(j * gw + i) * 2] = (distance <= -4.0f * extent) ? 0.0f :
1269 ((distance >= 4.0f * extent) ? 1.0f : dt_gradient_lookup(clut, distance * ihwscale));
1270 }
1271 }
1272
1274
1275 // we allocate the buffer
1276 float *const bufptr = *buffer = dt_pixelpipe_cache_alloc_align_float_cache((size_t)w * h, 0);
1277 if(IS_NULL_PTR(*buffer))
1278 {
1280 return 1;
1281 }
1282
1283 const float inv_grid2 = 1.0f / (grid * grid);
1284 float w0[8], w1[8];
1285 for(int i = 0; i < grid; i++)
1286 {
1287 w0[i] = (float)(grid - i);
1288 w1[i] = (float)i;
1289 }
1290
1291// we fill the mask buffer by interpolation
1292 __OMP_PARALLEL_FOR__(if((size_t)w * h > 50000))
1293 for(int j = 0; j < h; j++)
1294 {
1295 const int jj = j % grid;
1296 const int mj = j / grid;
1297 const float wj0 = w0[jj];
1298 const float wj1 = w1[jj];
1299 const size_t row_base = (size_t)mj * gw;
1300 float *const row = bufptr + (size_t)j * w;
1301 int ii = 0;
1302 int mi = 0;
1303 for(int i = 0; i < w; i++)
1304 {
1305 const size_t pt_index = row_base + mi;
1306 const float wii0 = w0[ii];
1307 const float wii1 = w1[ii];
1308 row[i] = (points[2 * pt_index] * wii0 * wj0
1309 + points[2 * (pt_index + 1)] * wii1 * wj0
1310 + points[2 * (pt_index + gw)] * wii0 * wj1
1311 + points[2 * (pt_index + gw + 1)] * wii1 * wj1) * inv_grid2;
1312 ii++;
1313 if(ii == grid)
1314 {
1315 ii = 0;
1316 mi++;
1317 }
1318 }
1319 }
1320
1322
1324 dt_print(DT_DEBUG_MASKS, "[masks %s] gradient fill took %0.04f sec\n", form->name,
1325 dt_get_wtime() - start2);
1326
1327 return 0;
1328}
1329
1330
1331static int _gradient_get_mask_roi(const dt_iop_module_t *const module, dt_dev_pixelpipe_t *pipe,
1332 const dt_dev_pixelpipe_iop_t *const piece,
1333 dt_masks_form_t *const form, const dt_iop_roi_t *roi, float *buffer)
1334{
1335 if(IS_NULL_PTR(form) || IS_NULL_PTR(form->points)) return 0;
1336 double start2 = 0.0;
1338 // we get the gradient values
1339 const dt_masks_anchor_gradient_t *gradient = (dt_masks_anchor_gradient_t *)(form->points->data);
1340 if(IS_NULL_PTR(gradient)) return 0;
1341
1342 // we create a buffer of grid points for later interpolation. mainly in order to reduce memory footprint
1343 const int w = roi->width;
1344 const int h = roi->height;
1345 const int px = roi->x;
1346 const int py = roi->y;
1347 const float iscale = 1.0f / roi->scale;
1348 const int grid = CLAMP((10.0f*roi->scale + 2.0f) / 3.0f, 1, 4);
1349 const int gw = (w + grid - 1) / grid + 1;
1350 const int gh = (h + grid - 1) / grid + 1;
1351
1352 float *points = dt_pixelpipe_cache_alloc_align_float_cache((size_t)2 * gw * gh, 0);
1353 if(IS_NULL_PTR(points)) return 1;
1354 __OMP_PARALLEL_FOR__(collapse(2) if((size_t)gw * gh > 50000))
1355 for(int j = 0; j < gh; j++)
1356 for(int i = 0; i < gw; i++)
1357 {
1358
1359 const size_t index = (size_t)j * gw + i;
1360 points[index * 2] = (grid * i + px) * iscale;
1361 points[index * 2 + 1] = (grid * j + py) * iscale;
1362 }
1363
1365 {
1366 dt_print(DT_DEBUG_MASKS, "[masks %s] gradient draw took %0.04f sec\n", form->name,
1367 dt_get_wtime() - start2);
1368 start2 = dt_get_wtime();
1369 }
1370
1371 // we backtransform all these points
1373 (size_t)gw * gh))
1374 {
1376 return 1;
1377 }
1378
1380 {
1381 dt_print(DT_DEBUG_MASKS, "[masks %s] gradient transform took %0.04f sec\n", form->name,
1382 dt_get_wtime() - start2);
1383 start2 = dt_get_wtime();
1384 }
1385
1386 // we calculate the mask at grid points and recycle point buffer to store results
1387 const float wd = pipe->iwidth;
1388 const float ht = pipe->iheight;
1389 const float hwscale = 1.0f / sqrtf(wd * wd + ht * ht);
1390 const float ihwscale = 1.0f / hwscale;
1391 const float v = (-gradient->rotation / 180.0f) * M_PI;
1392 const float sinv = sinf(v);
1393 const float cosv = cosf(v);
1394 const float xoffset = cosv * gradient->center[0] * wd + sinv * gradient->center[1] * ht;
1395 const float yoffset = sinv * gradient->center[0] * wd - cosv * gradient->center[1] * ht;
1396 const float extent = fmaxf(gradient->extent, 0.001f);
1397 const float normf = 1.0f / extent;
1398 const float curvature = gradient->curvature;
1399 const dt_masks_gradient_states_t state = gradient->state;
1400
1401 const int lutmax = ceilf(4 * extent * ihwscale);
1402 const int lutsize = 2 * lutmax + 2;
1404 if(IS_NULL_PTR(lut))
1405 {
1407 return 1;
1408 }
1409 __OMP_PARALLEL_FOR_SIMD__(if(lutsize > 1000) aligned(lut : 64))
1410 for(int n = 0; n < lutsize; n++)
1411 {
1412 const float distance = (n - lutmax) * hwscale;
1413 const float value = 0.5f + 0.5f * ((state == DT_MASKS_GRADIENT_STATE_LINEAR) ? normf * distance: erff(distance / extent));
1414 lut[n] = (value < 0.0f) ? 0.0f : ((value > 1.0f) ? 1.0f : value);
1415 }
1416
1417 // center lut around zero
1418 float *clut = lut + lutmax;
1419
1420 __OMP_PARALLEL_FOR__(collapse(2) if((size_t)gw * gh > 50000))
1421 for(int j = 0; j < gh; j++)
1422 {
1423 for(int i = 0; i < gw; i++)
1424 {
1425 const size_t index = (size_t)j * gw + i;
1426 const float x = points[index * 2];
1427 const float y = points[index * 2 + 1];
1428
1429 const float x0 = (cosv * x + sinv * y - xoffset) * hwscale;
1430 const float y0 = (sinv * x - cosv * y - yoffset) * hwscale;
1431
1432 const float distance = y0 - curvature * x0 * x0;
1433
1434 points[index * 2] = (distance <= -4.0f * extent) ? 0.0f : ((distance >= 4.0f * extent) ? 1.0f : dt_gradient_lookup(clut, distance * ihwscale));
1435 }
1436 }
1437
1439
1440 const float inv_grid2 = 1.0f / (grid * grid);
1441 float w0[8], w1[8];
1442 for(int i = 0; i < grid; i++)
1443 {
1444 w0[i] = (float)(grid - i);
1445 w1[i] = (float)i;
1446 }
1447
1448// we fill the mask buffer by interpolation
1449 __OMP_PARALLEL_FOR__(if((size_t)w * h > 50000))
1450 for(int j = 0; j < h; j++)
1451 {
1452 const int jj = j % grid;
1453 const int mj = j / grid;
1454 const float wj0 = w0[jj];
1455 const float wj1 = w1[jj];
1456 const size_t row_base = (size_t)mj * gw;
1457 float *const row = buffer + (size_t)j * w;
1458 int ii = 0;
1459 int mi = 0;
1460 for(int i = 0; i < w; i++)
1461 {
1462 const size_t mindex = row_base + mi;
1463 const float wii0 = w0[ii];
1464 const float wii1 = w1[ii];
1465 row[i] = (points[mindex * 2] * wii0 * wj0
1466 + points[(mindex + 1) * 2] * wii1 * wj0
1467 + points[(mindex + gw) * 2] * wii0 * wj1
1468 + points[(mindex + gw + 1) * 2] * wii1 * wj1) * inv_grid2;
1469 ii++;
1470 if(ii == grid)
1471 {
1472 ii = 0;
1473 mi++;
1474 }
1475 }
1476 }
1477
1479
1481 dt_print(DT_DEBUG_MASKS, "[masks %s] gradient fill took %0.04f sec\n", form->name,
1482 dt_get_wtime() - start2);
1483
1484 return 0;
1485}
1486
1488{
1489 // we always want to start with no curvature
1490 dt_conf_set_float("plugins/darkroom/masks/gradient/curvature", 0.0f);
1491}
1492
1493static void _gradient_set_form_name(struct dt_masks_form_t *const form, const size_t nb)
1494{
1495 snprintf(form->name, sizeof(form->name), _("gradient #%d"), (int)nb);
1496}
1497
1498static void _gradient_set_hint_message(const dt_masks_form_gui_t *const gui, const dt_masks_form_t *const form,
1499 const int opacity, char *const restrict msgbuf, const size_t msgbuf_len)
1500{
1501 if(gui->creation)
1502 g_snprintf(msgbuf, msgbuf_len, _("<b>Extent</b>: scroll, <b>Curvature</b>: shift+scroll\n"
1503 "<b>Rotate</b>: shift+drag, <b>Opacity</b>: ctrl+scroll (%d%%)"), opacity);
1504 else if(gui->form_selected || gui->seg_selected)
1505 g_snprintf(msgbuf, msgbuf_len, _("<b>Extent</b>: scroll, <b>Curvature</b>: shift+scroll\n"
1506 "<b>Reset curvature</b>: double-click, <b>Opacity</b>: ctrl+scroll (%d%%)"), opacity);
1507}
1508
1510{
1511 // unused arg, keep compiler from complaining
1513}
1514
1515// The function table for gradients. This must be public, i.e. no "static" keyword.
1518 .sanitize_config = _gradient_sanitize_config,
1519 .set_form_name = _gradient_set_form_name,
1520 .set_hint_message = _gradient_set_hint_message,
1521 .duplicate_points = _gradient_duplicate_points,
1522 .get_distance = _gradient_get_distance,
1523 .get_points_border = _gradient_get_points_border,
1524 .get_mask = _gradient_get_mask,
1525 .get_mask_roi = _gradient_get_mask_roi,
1526 .get_area = _gradient_get_area,
1527 .get_gravity_center = _gradient_get_gravity_center,
1528 .get_interaction_value = _gradient_get_interaction_value,
1529 .set_interaction_value = _gradient_set_interaction_value,
1530 .update_hover = _find_closest_handle,
1531 .mouse_moved = _gradient_events_mouse_moved,
1532 .mouse_scrolled = _gradient_events_mouse_scrolled,
1533 .button_pressed = _gradient_events_button_pressed,
1534 .button_released = _gradient_events_button_released,
1535 .key_pressed = _gradient_events_key_pressed,
1536 .post_expose = _gradient_events_post_expose,
1537 .draw_shape = _gradient_draw_shape
1538};
1539
1540// clang-format off
1541// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
1542// vim: shiftwidth=2 expandtab tabstop=2 cindent
1543// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
1544// clang-format on
static double dist(double x1, double y1, double x2, double y2)
Definition ashift_lsd.c:250
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
void cleanup(dt_imageio_module_format_t *self)
Definition avif.c:164
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
const dt_aligned_pixel_t f
static const int row
int type
void dt_conf_set_float(const char *name, float val)
float dt_conf_get_float(const char *name)
void dt_conf_set_int(const char *name, int val)
darktable_t darktable
Definition darktable.c:181
void dt_print(dt_debug_thread_t thread, const char *msg,...)
Definition darktable.c:1542
@ DT_DEBUG_PERF
Definition darktable.h:719
@ DT_DEBUG_MASKS
Definition darktable.h:727
#define dt_get_bythread(buf, padsize, tnum)
Definition darktable.h:1038
#define dt_pixelpipe_cache_alloc_align_float_cache(pixels, id)
Definition darktable.h:447
#define dt_pixelpipe_cache_free_align(mem)
Definition darktable.h:453
#define dt_get_perthread(buf, padsize)
Definition darktable.h:1035
#define __OMP_PARALLEL_FOR__(...)
Definition darktable.h:258
static const dt_aligned_pixel_simd_t value
Definition darktable.h:577
static double dt_get_wtime(void)
Definition darktable.h:914
#define dt_pixelpipe_cache_calloc_perthread(n, objsize, padded_size)
Definition darktable.h:1019
#define __OMP_PARALLEL_FOR_SIMD__(...)
Definition darktable.h:259
static gboolean dt_modifier_is(const GdkModifierType state, const GdkModifierType desired_modifier_mask)
Definition darktable.h:893
#define dt_pixelpipe_cache_alloc_perthread_float(n, padded_size)
Definition darktable.h:1030
#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 darktable.h:281
int dt_dev_coordinates_raw_abs_to_image_abs(dt_develop_t *dev, float *points, size_t points_count)
Definition develop.c:1525
void dt_dev_coordinates_raw_norm_to_raw_abs(dt_develop_t *dev, float *points, size_t num_points)
Definition develop.c:1109
int dt_dev_distort_transform_plus(const dt_dev_pixelpipe_t *pipe, const double iop_order, const int transf_direction, float *points, size_t points_count)
Definition develop.c:1557
int dt_dev_distort_backtransform_plus(const dt_dev_pixelpipe_t *pipe, const double iop_order, const int transf_direction, float *points, size_t points_count)
Definition develop.c:1586
void dt_dev_coordinates_raw_abs_to_raw_norm(dt_develop_t *dev, float *points, size_t num_points)
Definition develop.c:1092
@ DT_DEV_TRANSFORM_DIR_BACK_INCL
Definition develop.h:105
#define DT_DRAW_SIZE_LINE
Definition draw.h:72
static void dt_draw_stroke_line(const dt_draw_dash_type_t dash_type, const gboolean source, cairo_t *cr, const gboolean selected, const float zoom_scale, const cairo_line_cap_t line_cap)
Stroke a line with style.
Definition draw.h:784
#define DT_DRAW_SIZE_LINE_SELECTED
Definition draw.h:73
static void dt_draw_set_color_overlay(cairo_t *cr, gboolean bright, double alpha)
Definition draw.h:106
@ DT_MASKS_DASH_STICK
Definition draw.h:94
@ DT_MASKS_DASH_ROUND
Definition draw.h:95
@ DT_MASKS_NO_DASH
Definition draw.h:93
#define DT_DRAW_SCALE_ARROW
Definition draw.h:80
static void dt_draw_shape_lines(const dt_draw_dash_type_t dash_type, const gboolean source, cairo_t *cr, const int nb, const gboolean selected, const float zoom_scale, const float *points, const int points_count, const shape_draw_function_t *draw_shape_func, const cairo_line_cap_t line_cap)
Draw the lines of a mask shape.
Definition draw.h:734
static void dt_draw_node(cairo_t *cr, const gboolean square, const gboolean point_action, const gboolean selected, const float zoom_scale, const float x, const float y)
Draw an node point of a mask.
Definition draw.h:597
static void _gradient_events_post_expose(cairo_t *cr, float zoom_scale, dt_masks_form_gui_t *gui, int index, int nb)
Definition gradient.c:1059
static float _gradient_set_interaction_value(dt_masks_form_t *form, dt_masks_interaction_t interaction, float value, dt_masks_increment_t increment, int flow, dt_masks_form_gui_t *gui, struct dt_iop_module_t *module)
Definition gradient.c:425
static int _gradient_get_area(const dt_iop_module_t *const module, dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *const piece, dt_masks_form_t *const form, int *width, int *height, int *posx, int *posy)
Definition gradient.c:1117
static void _gradient_get_distance(float x, float y, float dist_mouse, dt_masks_form_gui_t *gui, int index, int num_points, int *inside, int *inside_border, int *near, int *inside_source, float *dist)
Definition gradient.c:232
static int _gradient_events_button_released(struct dt_iop_module_t *module, double x, double y, int which, uint32_t state, dt_masks_form_t *form, int parentid, dt_masks_form_gui_t *gui, int index)
Definition gradient.c:615
static float _gradient_get_interaction_value(const dt_masks_form_t *form, dt_masks_interaction_t interaction)
Definition gradient.c:392
static void _closest_point_on_segment(float px, float py, float x1, float y1, float x2, float y2, float *closest_x, float *closest_y, float *distance_sq)
Definition gradient.c:77
static void _copy_points(float *dest, const float *src, int count, int *k)
Definition gradient.c:849
static void _gradient_node_position_cb(const dt_masks_form_gui_points_t *gui_points, int node_index, float *node_x, float *node_y, void *user_data)
Definition gradient.c:326
static void _gradient_distance_cb(float pointer_x, float pointer_y, float cursor_radius, dt_masks_form_gui_t *mask_gui, int form_index, int node_count, int *inside, int *inside_border, int *near, int *inside_source, float *dist, void *user_data)
Definition gradient.c:333
static float dt_gradient_lookup(const float *lut, const float i)
Definition gradient.c:1151
#define CURVATURE_MIN
Definition gradient.c:44
static int _gradient_events_mouse_moved(struct dt_iop_module_t *module, double x, double y, double pressure, int which, dt_masks_form_t *form, int parentid, dt_masks_form_gui_t *gui, int index)
Definition gradient.c:671
#define extent_MAX
Definition gradient.c:43
static int _gradient_events_key_pressed(struct dt_iop_module_t *module, GdkEventKey *event, dt_masks_form_t *form, int parentid, dt_masks_form_gui_t *gui, int index)
Definition gradient.c:665
static int _gradient_events_button_pressed(struct dt_iop_module_t *module, double x, double y, double pressure, int which, int type, uint32_t state, dt_masks_form_t *form, int parentid, dt_masks_form_gui_t *gui, int index)
Definition gradient.c:546
static void _gradient_draw_shape(cairo_t *cr, const float *pts_line, const int pts_line_count, const int nb, const gboolean border, const gboolean source)
Definition gradient.c:944
static void _gradient_set_hint_message(const dt_masks_form_gui_t *const gui, const dt_masks_form_t *const form, const int opacity, char *const restrict msgbuf, const size_t msgbuf_len)
Definition gradient.c:1498
static int _gradient_get_pts_border(dt_develop_t *dev, float x, float y, float rotation, float distance, float curvature, float **points, int *points_count)
Definition gradient.c:858
static int _find_border_separator(const float *border, int count)
Definition gradient.c:51
static int _change_extent(dt_masks_form_t *form, dt_masks_form_gui_t *gui, struct dt_iop_module_t *module, int index, const float amount, const dt_masks_increment_t increment, const int flow)
Definition gradient.c:445
static int _change_curvature(dt_masks_form_t *form, dt_masks_form_gui_t *gui, struct dt_iop_module_t *module, int index, const float amount, const dt_masks_increment_t increment, const int flow)
Definition gradient.c:462
static void _gradient_get_creation_values(dt_masks_gradient_creation_values_t *values)
Definition gradient.c:187
static void _closest_point_on_line(float px, float py, const float *border, int start_idx, int end_idx, float *closest_x, float *closest_y, float *min_distance_sq)
Definition gradient.c:103
static int _init_opacity(dt_masks_form_t *form, const float amount, const dt_masks_increment_t increment, const int flow)
Definition gradient.c:378
static void _gradient_draw_arrow(cairo_t *cr, const gboolean selected, const gboolean pivot_selected, const gboolean is_rotating, const float zoom_scale, float *pts, int pts_count)
Definition gradient.c:983
static int _find_closest_handle(dt_masks_form_t *mask_form, dt_masks_form_gui_t *mask_gui, int index)
Definition gradient.c:355
static int _init_extent(dt_masks_form_t *form, const float amount, const dt_masks_increment_t increment, const int flow)
Definition gradient.c:364
static int _gradient_get_creation_preview(dt_masks_form_gui_t *gui, dt_masks_preview_buffers_t *preview)
Definition gradient.c:214
static int _init_curvature(dt_masks_form_t *form, const float amount, const dt_masks_increment_t increment, const int flow)
Definition gradient.c:371
static int _gradient_get_points(dt_develop_t *dev, float x, float y, float rotation, float curvature, float **points, int *points_count)
Build the distorted display polyline for a gradient mask.
Definition gradient.c:735
static void _gradient_set_form_name(struct dt_masks_form_t *const form, const size_t nb)
Definition gradient.c:1493
static int _gradient_events_mouse_scrolled(struct dt_iop_module_t *module, double x, double y, int up, const int flow, uint32_t state, dt_masks_form_t *form, int parentid, dt_masks_form_gui_t *gui, int index, dt_masks_interaction_t interaction)
Definition gradient.c:514
static int _gradient_get_mask(const dt_iop_module_t *const module, dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *const piece, dt_masks_form_t *const form, float **buffer, int *width, int *height, int *posx, int *posy)
Definition gradient.c:1159
static float _gradient_get_border_len_sq(const dt_masks_form_gui_points_t *gpt)
Definition gradient.c:173
static gboolean _gradient_get_gravity_center(const dt_masks_form_t *form, float center[2], float *area)
Definition gradient.c:409
static void _gradient_init_new(dt_masks_form_gui_t *gui, dt_masks_anchor_gradient_t *gradient)
Definition gradient.c:197
static int _change_rotation(dt_masks_form_t *form, dt_masks_form_gui_t *gui, struct dt_iop_module_t *module, int index, const float amount, const dt_masks_increment_t increment, const int flow)
Definition gradient.c:489
static void _gradient_duplicate_points(dt_develop_t *dev, dt_masks_form_t *const base, dt_masks_form_t *const dest)
Definition gradient.c:1509
static int _gradient_get_mask_roi(const dt_iop_module_t *const module, dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *const piece, dt_masks_form_t *const form, const dt_iop_roi_t *roi, float *buffer)
Definition gradient.c:1331
static void _gradient_sanitize_config(dt_masks_type_t type)
Definition gradient.c:1487
#define extent_MIN
Definition gradient.c:42
static int _init_rotation(dt_masks_form_t *form, const float amount, const dt_masks_increment_t increment, const int flow)
Definition gradient.c:385
static gboolean _gradient_is_canonical(const float x, const float y, const float wd, const float ht)
Definition gradient.c:722
static void _gradient_post_select_cb(dt_masks_form_gui_t *mask_gui, int inside, int inside_border, int inside_source, void *user_data)
Definition gradient.c:341
const dt_masks_functions_t dt_masks_functions_gradient
Definition gradient.c:1516
static int _gradient_get_points_border(dt_develop_t *dev, dt_masks_form_t *form, float **points, int *points_count, float **border, int *border_count, int source, const dt_iop_module_t *module)
Definition gradient.c:1099
#define CURVATURE_MAX
Definition gradient.c:45
static const float x
const float *const lut
const float const int lutsize
const int t
const float v
#define w1
Definition lmmse.c:59
float *const restrict const size_t k
void dt_masks_gui_form_create(dt_masks_form_t *form, dt_masks_form_gui_t *gui, int index, struct dt_iop_module_t *module)
@ DT_MASKS_EDIT_FULL
Definition masks.h:202
int dt_masks_form_change_opacity(dt_masks_form_t *form, int parentid, int up, const int flow)
void dt_masks_duplicate_points(const dt_masks_form_t *base, dt_masks_form_t *dest, size_t node_size)
Duplicate a points list for a mask using a fixed node size.
void dt_masks_gui_form_save_creation(dt_develop_t *dev, struct dt_iop_module_t *module, dt_masks_form_t *form, dt_masks_form_gui_t *gui)
Save the form creation right after a shape has been finished drawing.
dt_masks_type_t
Definition masks.h:129
dt_masks_interaction_t
Definition masks.h:302
@ DT_MASKS_INTERACTION_HARDNESS
Definition masks.h:305
@ DT_MASKS_INTERACTION_SIZE
Definition masks.h:304
static void dt_masks_gui_cursor_to_raw_norm(dt_develop_t *dev, const dt_masks_form_gui_t *gui, float point[2])
Definition masks.h:603
static void dt_masks_preview_buffers_cleanup(dt_masks_preview_buffers_t *buffers)
Definition masks.h:807
float dt_masks_apply_increment(float current, float amount, dt_masks_increment_t increment, int flow)
Apply a scroll increment to a scalar value.
float dt_masks_get_set_conf_value_with_toast(dt_masks_form_t *form, const char *feature, float amount, float v_min, float v_max, dt_masks_increment_t increment, int flow, const char *toast_fmt, float toast_scale)
Update a mask configuration value and emit a toast message.
float dt_masks_rotate_with_anchor(dt_develop_t *dev, const float anchor[2], const float center[2], dt_masks_form_gui_t *gui)
Rotate a mask shape around its center. WARNING: gui->delta will be updated with the new position afte...
dt_masks_increment_t
Definition masks.h:193
@ DT_MASKS_INCREMENT_SCALE
Definition masks.h:195
@ DT_MASKS_INCREMENT_OFFSET
Definition masks.h:196
@ DT_MASKS_INCREMENT_ABSOLUTE
Definition masks.h:194
dt_masks_gradient_states_t
Definition masks.h:187
@ DT_MASKS_GRADIENT_STATE_SIGMOIDAL
Definition masks.h:189
@ DT_MASKS_GRADIENT_STATE_LINEAR
Definition masks.h:188
static void dt_masks_draw_preview_shape(cairo_t *cr, const float zoom_scale, const int num_points, float *points, const int points_count, float *border, const int border_count, void(*const *draw_shape)(cairo_t *cr, const float *points, const int points_count, const int nb, const gboolean border, const gboolean source), const cairo_line_cap_t shape_cap, const cairo_line_cap_t border_cap, const gboolean save_restore, const gboolean source)
Definition masks.h:773
int dt_masks_find_closest_handle_common(dt_masks_form_t *mask_form, dt_masks_form_gui_t *mask_gui, int form_index, int node_count_override, dt_masks_border_handle_fn border_handle_cb, dt_masks_curve_handle_fn curve_handle_cb, dt_masks_node_position_fn node_position_cb, dt_masks_distance_fn distance_cb, dt_masks_post_select_fn post_select_cb, void *user_data)
Shared selection logic for node/handle/segment hit testing.
static void dt_masks_gui_delta_to_raw_norm(dt_develop_t *dev, const dt_masks_form_gui_t *gui, float point[2])
Definition masks.h:611
#define CLAMPF(a, mn, mx)
Definition math.h:89
#define M_PI
Definition math.h:45
float iscale
Definition mipmap_cache.c:2
static float gh(const float f)
const float uint32_t state[4]
int32_t num_openmp_threads
Definition darktable.h:758
int32_t unmuted
Definition darktable.h:760
struct dt_develop_t * develop
Definition darktable.h:770
int32_t raw_height
Definition develop.h:228
int32_t raw_width
Definition develop.h:228
struct dt_develop_t::@17 roi
Region of interest passed through the pixelpipe.
Definition imageop.h:72
double scale
Definition imageop.h:74
dt_masks_gradient_states_t state
Definition masks.h:277
gboolean border_toggling
Definition masks.h:488
dt_masks_edit_mode_t edit_mode
Definition masks.h:463
dt_iop_module_t * creation_module
Definition masks.h:505
gboolean seg_selected
Definition masks.h:472
gboolean form_dragging
Definition masks.h:485
gboolean gradient_toggling
Definition masks.h:489
gboolean creation
Definition masks.h:503
gboolean form_selected
Definition masks.h:476
gboolean form_rotating
Definition masks.h:487
gboolean border_selected
Definition masks.h:477
gboolean pivot_selected
Definition masks.h:479
float delta[2]
Definition masks.h:455
char name[128]
Definition masks.h:396
GList * points
Definition masks.h:377
void(* draw_shape)(cairo_t *cr, const float *points, const int points_count, const int nb, const gboolean border, const gboolean source)
Definition masks.h:368