Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
detailmask.c
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Aurélien PIERRE.
4 Copyright (C) 2026 Guillaume Stutin.
5
6 Ansel is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Ansel is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Ansel. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifdef HAVE_CONFIG_H
21#include "config.h"
23#endif
24
25#include "common/imagebuf.h"
26#include "pixel/interpolation.h"
27#include "common/opencl.h"
28#include "develop/blend.h"
29#include "develop/develop.h"
30#include "system/macros.h"
31#include "system/mem_alloc.h"
32#include "system/simd.h"
33#include "common/logging.h"
35#include "develop/imageop.h"
37#include "develop/pixelpipe.h"
38#include "iop/iop_api.h"
39#include <glib/gi18n.h>
40
42
47
49
50const char *name()
51{
52 return _("detail mask");
53}
54
56{
58}
59
64
69
78
87
88void distort_mask(struct dt_iop_module_t *self, const struct dt_dev_pixelpipe_t *pipe,
89 struct dt_dev_pixelpipe_iop_t *piece, const float *const in, float *const out,
90 const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
91{
92 if((roi_in->scale == roi_out->scale)
93 && (roi_in->width == roi_out->width)
94 && (roi_in->height == roi_out->height)
95 && (roi_in->x == roi_out->x)
96 && (roi_in->y == roi_out->y))
97 {
98 dt_iop_copy_image_roi(out, in, 1, roi_in, roi_out, TRUE);
99 return;
100 }
101
103 dt_interpolation_resample_roi_1c(itor, out, roi_out, in, roi_in);
104}
105
107 const void *const ivoid, void *const ovoid)
108{
109 dt_dev_pixelpipe_t *const mutable_pipe = (dt_dev_pixelpipe_t *)pipe;
110 const dt_iop_roi_t *const roi_out = &piece->roi_out;
112 const int width = roi_out->width;
113 const int height = roi_out->height;
114 dt_pixel_cache_entry_t *entry = NULL;
115 void *cache_data = NULL;
116 float *mask = NULL;
117 float *tmp = NULL;
118 int created = 0;
119
121 dt_dev_clear_rawdetail_mask(mutable_pipe);
122
123 created = dt_dev_pixelpipe_cache_get(mask_hash, sizeof(float) * (size_t)width * height,
124 "detailmask rawdetail", pipe->type, TRUE, &cache_data, &entry);
125 mask = (float *)cache_data;
126 if(IS_NULL_PTR(mask) || IS_NULL_PTR(entry)) goto error;
127
128 mutable_pipe->rawdetail_mask_hash = mask_hash;
129 memcpy(&mutable_pipe->rawdetail_mask_roi, roi_out, sizeof(dt_iop_roi_t));
130 if(!created) return 0;
131
133 if(IS_NULL_PTR(tmp)) goto error;
134
135 dt_aligned_pixel_t wb = { 1.0f, 1.0f, 1.0f };
136 if(piece->dsc_in.temperature.enabled)
137 {
138 wb[0] = piece->dsc_in.temperature.coeffs[0];
139 wb[1] = piece->dsc_in.temperature.coeffs[1];
140 wb[2] = piece->dsc_in.temperature.coeffs[2];
141 }
142
143 /* This stage always sees tightly-packed RGBA float pixels, so the luminance
144 * input uses an explicit 4-float stride while the Scharr operator runs on the
145 * 1-float-per-pixel temporary buffer. No CFA layout survives past demosaic. */
146 dt_masks_calc_rawdetail_mask((float *const)ovoid, mask, tmp, width, height, wb);
149 dt_print(DT_DEBUG_MASKS, "[detailmask process] (%ix%i)\n", width, height);
150
151 return 0;
152
153error:
154 fprintf(stderr, "[detailmask process] couldn't write detail mask\n");
155 // Flagged while still held and before the write lock goes, so the release removes it.
156 if(created && !IS_NULL_PTR(entry))
157 {
160 }
161 // The reference is the pipe's once it records the mask's hash, and still ours before that.
162 if(mutable_pipe->rawdetail_mask_hash == mask_hash)
163 dt_dev_clear_rawdetail_mask(mutable_pipe);
164 else if(!IS_NULL_PTR(entry))
167 return 1;
168}
169
170#ifdef HAVE_OPENCL
172 cl_mem dev_in, cl_mem dev_out)
173{
174 dt_dev_pixelpipe_t *const mutable_pipe = (dt_dev_pixelpipe_t *)pipe;
175 const dt_iop_roi_t *const roi_out = &piece->roi_out;
176 const int devid = pipe->devid;
178 const int width = roi_out->width;
179 const int height = roi_out->height;
180 dt_pixel_cache_entry_t *entry = NULL;
181 void *cache_data = NULL;
182 cl_mem detail = NULL;
183 cl_mem mask_dev = NULL;
184 float *mask = NULL;
185 cl_int err = CL_SUCCESS;
186 int created = 0;
187
188 size_t origin[3] = { 0, 0, 0 };
189 size_t region[3] = { (size_t)width, (size_t)height, 1 };
190 if(dt_opencl_enqueue_copy_image(devid, dev_in, dev_out, origin, origin, region) != CL_SUCCESS)
191 return FALSE;
192
193 dt_dev_clear_rawdetail_mask(mutable_pipe);
194 created = dt_dev_pixelpipe_cache_get(mask_hash, sizeof(float) * (size_t)width * height,
195 "detailmask rawdetail", pipe->type, TRUE, &cache_data, &entry);
196 mask = (float *)cache_data;
197 if(IS_NULL_PTR(mask) || IS_NULL_PTR(entry)) goto error;
198
199 mutable_pipe->rawdetail_mask_hash = mask_hash;
200 memcpy(&mutable_pipe->rawdetail_mask_roi, roi_out, sizeof(dt_iop_roi_t));
201 if(!created) return TRUE;
202
203 detail = dt_opencl_alloc_device_buffer(devid, sizeof(float) * width * height);
204 if(IS_NULL_PTR(detail)) goto error;
205 mask_dev = dt_opencl_alloc_device_buffer(devid, sizeof(float) * width * height);
206 if(IS_NULL_PTR(mask_dev)) goto error;
207
208 {
210 dt_aligned_pixel_t wb = { 1.0f, 1.0f, 1.0f };
211 if(piece->dsc_in.temperature.enabled)
212 {
213 wb[0] = piece->dsc_in.temperature.coeffs[0];
214 wb[1] = piece->dsc_in.temperature.coeffs[1];
215 wb[2] = piece->dsc_in.temperature.coeffs[2];
216 }
217
218 size_t sizes[3] = { ROUNDUPDWD(width, devid), ROUNDUPDHT(height, devid), 1 };
219 dt_opencl_set_kernel_arg(devid, kernel, 0, sizeof(cl_mem), &detail);
220 dt_opencl_set_kernel_arg(devid, kernel, 1, sizeof(cl_mem), &dev_out);
221 dt_opencl_set_kernel_arg(devid, kernel, 2, sizeof(int), &width);
222 dt_opencl_set_kernel_arg(devid, kernel, 3, sizeof(int), &height);
223 dt_opencl_set_kernel_arg(devid, kernel, 4, sizeof(float), &wb[0]);
224 dt_opencl_set_kernel_arg(devid, kernel, 5, sizeof(float), &wb[1]);
225 dt_opencl_set_kernel_arg(devid, kernel, 6, sizeof(float), &wb[2]);
226 err = dt_opencl_enqueue_kernel_2d(devid, kernel, sizes);
227 if(err != CL_SUCCESS) goto error;
228 }
229
230 {
232 size_t sizes[3] = { ROUNDUPDWD(width, devid), ROUNDUPDHT(height, devid), 1 };
233 dt_opencl_set_kernel_arg(devid, kernel, 0, sizeof(cl_mem), &detail);
234 dt_opencl_set_kernel_arg(devid, kernel, 1, sizeof(cl_mem), &mask_dev);
235 dt_opencl_set_kernel_arg(devid, kernel, 2, sizeof(int), &width);
236 dt_opencl_set_kernel_arg(devid, kernel, 3, sizeof(int), &height);
237 err = dt_opencl_enqueue_kernel_2d(devid, kernel, sizes);
238 if(err != CL_SUCCESS) goto error;
239 }
240
241 err = dt_opencl_read_buffer_from_device(devid, mask, mask_dev, 0, sizeof(float) * (size_t)width * height, CL_TRUE);
242 if(err != CL_SUCCESS) goto error;
243
247 dt_print(DT_DEBUG_MASKS, "[detailmask process_cl] (%ix%i)\n", width, height);
248
249 return TRUE;
250
251error:
252 fprintf(stderr, "[detailmask process_cl] couldn't write detail mask: %i\n", err);
253 // Flagged while still held and before the write lock goes, so the release removes it.
254 if(created && !IS_NULL_PTR(entry))
255 {
258 }
259 // The reference is the pipe's once it records the mask's hash, and still ours before that.
260 if(mutable_pipe->rawdetail_mask_hash == mask_hash)
261 dt_dev_clear_rawdetail_mask(mutable_pipe);
262 else if(!IS_NULL_PTR(entry))
266 return FALSE;
267}
268#endif
269
272{
273 memcpy(piece->data, params, sizeof(dt_iop_detailmask_params_t));
274 piece->process_tiling_ready = 0;
275}
276
283
289
291{
292 dt_iop_default_init(module);
293 module->default_enabled = 0;
294}
295
296
297// clang-format off
298// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
299// vim: shiftwidth=2 expandtab tabstop=2 cindent
300// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
301// clang-format on
static void error(char *msg)
Definition ashift_lsd.c:202
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
dt_blendop_cl_global_t * dt_develop_blend_get_cl_global(void)
Definition blend.c:72
@ IOP_CS_RGB
const dt_colormatrix_t dt_aligned_pixel_t out
__DT_CLONE_TARGETS__ void dt_masks_calc_rawdetail_mask(float *const restrict src, float *const restrict mask, float *const restrict tmp, const int width, const int height, const dt_aligned_pixel_t wb)
Definition detail.c:289
void init(dt_iop_module_t *module)
Definition detailmask.c:290
void distort_mask(struct dt_iop_module_t *self, const struct dt_dev_pixelpipe_t *pipe, struct dt_dev_pixelpipe_iop_t *piece, const float *const in, float *const out, const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
Definition detailmask.c:88
int default_group()
Definition detailmask.c:55
struct dt_iop_detailmask_params_t dt_iop_detailmask_data_t
Definition detailmask.c:48
const char * name()
Definition detailmask.c:50
void output_format(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece, dt_iop_buffer_dsc_t *dsc)
Definition detailmask.c:79
void cleanup_pipe(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
Definition detailmask.c:284
int default_colorspace(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece)
Definition detailmask.c:65
void input_format(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece, dt_iop_buffer_dsc_t *dsc)
Definition detailmask.c:70
int flags()
Definition detailmask.c:60
void commit_params(dt_iop_module_t *self, dt_iop_params_t *params, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
Definition detailmask.c:270
int process(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, const void *const ivoid, void *const ovoid)
Definition detailmask.c:106
void init_pipe(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
Definition detailmask.c:277
int process_cl(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out)
Definition detailmask.c:171
void dt_iop_params_t
Definition dev_history.h:43
void default_output_format(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece, dt_iop_buffer_dsc_t *dsc)
void default_input_format(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece, dt_iop_buffer_dsc_t *dsc)
@ TYPE_FLOAT
Definition format.h:56
void dt_iop_copy_image_roi(float *const __restrict__ out, const float *const __restrict__ in, const size_t ch, const dt_iop_roi_t *const __restrict__ roi_in, const dt_iop_roi_t *const __restrict__ roi_out, const int zero_pad)
Definition imagebuf.c:163
static void dt_iop_image_copy_by_size(float *const __restrict__ out, const float *const __restrict__ in, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.h:91
void dt_iop_default_init(dt_iop_module_t *module)
Definition imageop.c:308
@ IOP_FLAGS_HIDDEN
Definition imageop.h:189
@ IOP_FLAGS_ONE_INSTANCE
Definition imageop.h:191
@ IOP_FLAGS_NO_HISTORY_STACK
Definition imageop.h:193
@ IOP_GROUP_TECHNICAL
Definition imageop.h:162
void *const ovoid
const struct dt_interpolation * dt_interpolation_new(enum dt_interpolation_type type)
void dt_interpolation_resample_roi_1c(const struct dt_interpolation *itor, float *out, const dt_iop_roi_t *const roi_out, const float *const in, const dt_iop_roi_t *const roi_in)
@ DT_INTERPOLATION_USERPREF_WARP
static float kernel(const float *x, const float *y)
static const dt_dev_pixelpipe_t * pipe
Definition layers.h:49
static const dt_dev_pixelpipe_t const dt_dev_pixelpipe_iop_t * piece
Definition layers.h:50
static const dt_iop_drawlayer_params_t * params
Definition layers.h:80
@ DT_DEBUG_MASKS
Definition logging.h:62
void dt_print(dt_debug_thread_t thread, const char *msg,...) __attribute__((format(printf
Print to stdout when thread is enabled, prefixed with seconds since startup.
#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
The detail-mask pixel math: a Scharr-style detail estimate over a raw or scene-referred buffer,...
#define dt_free_align(ptr)
Release memory from dt_alloc_align() and set ptr to NULL.
Definition mem_alloc.h:214
static void * dt_calloc_align(size_t size)
dt_alloc_align() followed by a zero fill.
Definition mem_alloc.h:225
uint32_t width
Definition mipmap_cache.c:0
uint32_t height
Definition mipmap_cache.c:1
#define DT_MODULE_INTROSPECTION(MODVER, PARAMSTYPE)
DT_MODULE() for a module whose params struct is introspected.
int dt_opencl_enqueue_kernel_2d(const int dev, const int kernel, const size_t *sizes)
Definition opencl.c:2612
void * dt_opencl_alloc_device_buffer(const int devid, const size_t size)
Definition opencl.c:3028
int dt_opencl_enqueue_copy_image(const int devid, cl_mem src, cl_mem dst, size_t *orig_src, size_t *orig_dst, size_t *region)
Definition opencl.c:2737
int dt_opencl_read_buffer_from_device(const int devid, void *host, void *device, const size_t offset, const size_t size, const int blocking)
Definition opencl.c:2785
int dt_opencl_set_kernel_arg(const int dev, const int kernel, const int num, const size_t size, const void *arg)
Definition opencl.c:2603
void dt_opencl_release_mem_object(cl_mem mem)
Definition opencl.c:2863
#define ROUNDUPDHT(a, b)
Definition opencl.h:86
#define ROUNDUPDWD(a, b)
Definition opencl.h:85
void dt_iop_buffer_dsc_update_bpp(dt_iop_buffer_dsc_t *dsc)
uint64_t dt_dev_pixelpipe_rawdetail_mask_hash(const dt_dev_pixelpipe_iop_t *piece)
Definition pixelpipe.c:45
void dt_dev_clear_rawdetail_mask(dt_dev_pixelpipe_t *pipe)
Release the side-band detail mask cache reference currently owned by the pipeline.
Definition pixelpipe.c:64
void dt_dev_pixelpipe_cache_wrlock_entry(gboolean lock, dt_pixel_cache_entry_t *cache_entry)
Lock or release the write lock on the entry.
void dt_dev_pixelpipe_cache_ref_count_entry(gboolean lock, dt_pixel_cache_entry_t *cache_entry)
Take or release one reference on a cache line. A referenced line is never evicted,...
int dt_dev_pixelpipe_cache_get(const uint64_t hash, const size_t size, const char *name, const int id, const gboolean alloc, void **data, dt_pixel_cache_entry_t **entry)
Get a cache line from the cache.
void dt_dev_pixelpipe_cache_flag_auto_destroy(dt_pixel_cache_entry_t *cache_entry)
Flag a cache line as disposable (auto_destroy), for short-lived lines nothing will need again: the re...
#define dt_pixelpipe_cache_alloc_align_float_cache(pixels, id)
#define dt_pixelpipe_cache_free_align(mem)
DT_ALIGNED_PIXEL float dt_aligned_pixel_t[4]
Definition simd.h:53
unsigned __int64 uint64_t
Definition strptime.c:75
dt_dev_pixelpipe_type_t type
uint64_t rawdetail_mask_hash
struct dt_iop_roi_t rawdetail_mask_roi
unsigned int channels
Definition format.h:83
dt_iop_buffer_type_t datatype
Definition format.h:85
Region of interest passed through the pixelpipe.
Definition format.h:49
double scale
Definition format.h:51
int width
Definition format.h:50
int height
Definition format.h:50