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
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#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include "common/imagebuf.h"
25#include "common/opencl.h"
26#include "develop/blend.h"
27#include "develop/develop.h"
28#include "develop/imageop.h"
29#include "develop/masks.h"
30#include "develop/pixelpipe.h"
31#include "iop/iop_api.h"
32#include <glib/gi18n.h>
33
35
40
42
43const char *name()
44{
45 return _("detail mask");
46}
47
49{
51}
52
57
62
65{
66 default_input_format(self, pipe, piece, dsc);
67 dsc->channels = 4;
68 dsc->datatype = TYPE_FLOAT;
70}
71
74{
75 default_output_format(self, pipe, piece, dsc);
76 dsc->channels = 4;
77 dsc->datatype = TYPE_FLOAT;
79}
80
81void distort_mask(struct dt_iop_module_t *self, const struct dt_dev_pixelpipe_t *pipe,
82 struct dt_dev_pixelpipe_iop_t *piece, const float *const in, float *const out,
83 const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
84{
85 if((roi_in->scale == roi_out->scale)
86 && (roi_in->width == roi_out->width)
87 && (roi_in->height == roi_out->height)
88 && (roi_in->x == roi_out->x)
89 && (roi_in->y == roi_out->y))
90 {
91 dt_iop_copy_image_roi(out, in, 1, roi_in, roi_out, TRUE);
92 return;
93 }
94
96 dt_interpolation_resample_roi_1c(itor, out, roi_out, in, roi_in);
97}
98
99int process(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece,
100 const void *const ivoid, void *const ovoid)
101{
102 dt_dev_pixelpipe_t *const mutable_pipe = (dt_dev_pixelpipe_t *)pipe;
103 const dt_iop_roi_t *const roi_out = &piece->roi_out;
104 const uint64_t mask_hash = dt_dev_pixelpipe_rawdetail_mask_hash(piece);
105 const int width = roi_out->width;
106 const int height = roi_out->height;
107 dt_pixel_cache_entry_t *entry = NULL;
108 void *cache_data = NULL;
109 float *mask = NULL;
110 float *tmp = NULL;
111 int created = 0;
112
114 dt_dev_clear_rawdetail_mask(mutable_pipe);
115
116 created = dt_dev_pixelpipe_cache_get(darktable.pixelpipe_cache, mask_hash, sizeof(float) * (size_t)width * height,
117 "detailmask rawdetail", pipe->type, TRUE, &cache_data, &entry);
118 mask = (float *)cache_data;
119 if(IS_NULL_PTR(mask) || IS_NULL_PTR(entry)) goto error;
120
121 mutable_pipe->rawdetail_mask_hash = mask_hash;
122 memcpy(&mutable_pipe->rawdetail_mask_roi, roi_out, sizeof(dt_iop_roi_t));
123 if(!created) return 0;
124
126 if(IS_NULL_PTR(tmp)) goto error;
127
128 dt_aligned_pixel_t wb = { 1.0f, 1.0f, 1.0f };
129 if(piece->dsc_in.temperature.enabled)
130 {
131 wb[0] = piece->dsc_in.temperature.coeffs[0];
132 wb[1] = piece->dsc_in.temperature.coeffs[1];
133 wb[2] = piece->dsc_in.temperature.coeffs[2];
134 }
135
136 /* This stage always sees tightly-packed RGBA float pixels, so the luminance
137 * input uses an explicit 4-float stride while the Scharr operator runs on the
138 * 1-float-per-pixel temporary buffer. No CFA layout survives past demosaic. */
139 dt_masks_calc_rawdetail_mask((float *const)ovoid, mask, tmp, width, height, wb);
142 dt_print(DT_DEBUG_MASKS, "[detailmask process] (%ix%i)\n", width, height);
143
144 return 0;
145
146error:
147 fprintf(stderr, "[detailmask process] couldn't write detail mask\n");
148 if(created && !IS_NULL_PTR(entry))
150 dt_dev_clear_rawdetail_mask(mutable_pipe);
151 if(!IS_NULL_PTR(entry))
152 {
154 }
156 return 1;
157}
158
159#ifdef HAVE_OPENCL
160int process_cl(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece,
161 cl_mem dev_in, cl_mem dev_out)
162{
163 dt_dev_pixelpipe_t *const mutable_pipe = (dt_dev_pixelpipe_t *)pipe;
164 const dt_iop_roi_t *const roi_out = &piece->roi_out;
165 const int devid = pipe->devid;
166 const uint64_t mask_hash = dt_dev_pixelpipe_rawdetail_mask_hash(piece);
167 const int width = roi_out->width;
168 const int height = roi_out->height;
169 dt_pixel_cache_entry_t *entry = NULL;
170 void *cache_data = NULL;
171 cl_mem detail = NULL;
172 cl_mem mask_dev = NULL;
173 float *mask = NULL;
174 cl_int err = CL_SUCCESS;
175 int created = 0;
176
177 size_t origin[3] = { 0, 0, 0 };
178 size_t region[3] = { (size_t)width, (size_t)height, 1 };
179 if(dt_opencl_enqueue_copy_image(devid, dev_in, dev_out, origin, origin, region) != CL_SUCCESS)
180 return FALSE;
181
182 dt_dev_clear_rawdetail_mask(mutable_pipe);
183 created = dt_dev_pixelpipe_cache_get(darktable.pixelpipe_cache, mask_hash, sizeof(float) * (size_t)width * height,
184 "detailmask rawdetail", pipe->type, TRUE, &cache_data, &entry);
185 mask = (float *)cache_data;
186 if(IS_NULL_PTR(mask) || IS_NULL_PTR(entry)) goto error;
187
188 mutable_pipe->rawdetail_mask_hash = mask_hash;
189 memcpy(&mutable_pipe->rawdetail_mask_roi, roi_out, sizeof(dt_iop_roi_t));
190 if(!created) return TRUE;
191
192 detail = dt_opencl_alloc_device_buffer(devid, sizeof(float) * width * height);
193 if(IS_NULL_PTR(detail)) goto error;
194 mask_dev = dt_opencl_alloc_device_buffer(devid, sizeof(float) * width * height);
195 if(IS_NULL_PTR(mask_dev)) goto error;
196
197 {
199 dt_aligned_pixel_t wb = { 1.0f, 1.0f, 1.0f };
200 if(piece->dsc_in.temperature.enabled)
201 {
202 wb[0] = piece->dsc_in.temperature.coeffs[0];
203 wb[1] = piece->dsc_in.temperature.coeffs[1];
204 wb[2] = piece->dsc_in.temperature.coeffs[2];
205 }
206
207 size_t sizes[3] = { ROUNDUPDWD(width, devid), ROUNDUPDHT(height, devid), 1 };
208 dt_opencl_set_kernel_arg(devid, kernel, 0, sizeof(cl_mem), &detail);
209 dt_opencl_set_kernel_arg(devid, kernel, 1, sizeof(cl_mem), &dev_out);
210 dt_opencl_set_kernel_arg(devid, kernel, 2, sizeof(int), &width);
211 dt_opencl_set_kernel_arg(devid, kernel, 3, sizeof(int), &height);
212 dt_opencl_set_kernel_arg(devid, kernel, 4, sizeof(float), &wb[0]);
213 dt_opencl_set_kernel_arg(devid, kernel, 5, sizeof(float), &wb[1]);
214 dt_opencl_set_kernel_arg(devid, kernel, 6, sizeof(float), &wb[2]);
215 err = dt_opencl_enqueue_kernel_2d(devid, kernel, sizes);
216 if(err != CL_SUCCESS) goto error;
217 }
218
219 {
221 size_t sizes[3] = { ROUNDUPDWD(width, devid), ROUNDUPDHT(height, devid), 1 };
222 dt_opencl_set_kernel_arg(devid, kernel, 0, sizeof(cl_mem), &detail);
223 dt_opencl_set_kernel_arg(devid, kernel, 1, sizeof(cl_mem), &mask_dev);
224 dt_opencl_set_kernel_arg(devid, kernel, 2, sizeof(int), &width);
225 dt_opencl_set_kernel_arg(devid, kernel, 3, sizeof(int), &height);
226 err = dt_opencl_enqueue_kernel_2d(devid, kernel, sizes);
227 if(err != CL_SUCCESS) goto error;
228 }
229
230 err = dt_opencl_read_buffer_from_device(devid, mask, mask_dev, 0, sizeof(float) * (size_t)width * height, CL_TRUE);
231 if(err != CL_SUCCESS) goto error;
232
236 dt_print(DT_DEBUG_MASKS, "[detailmask process_cl] (%ix%i)\n", width, height);
237
238 return TRUE;
239
240error:
241 fprintf(stderr, "[detailmask process_cl] couldn't write detail mask: %i\n", err);
242 if(created && !IS_NULL_PTR(entry))
244 dt_dev_clear_rawdetail_mask(mutable_pipe);
245 if(!IS_NULL_PTR(entry))
246 {
248 }
251 return FALSE;
252}
253#endif
254
257{
258 memcpy(piece->data, params, sizeof(dt_iop_detailmask_params_t));
259 piece->process_tiling_ready = 0;
260}
261
263{
265 piece->data_size = sizeof(dt_iop_detailmask_data_t);
266 piece->enabled = FALSE;
267}
268
270{
271 dt_free_align(piece->data);
272 piece->data = NULL;
273}
274
276{
277 dt_iop_default_init(module);
278 module->default_enabled = 0;
279}
280
281
282// clang-format off
283// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
284// vim: shiftwidth=2 expandtab tabstop=2 cindent
285// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
286// 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
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
@ IOP_CS_RGB
const dt_colormatrix_t dt_aligned_pixel_t out
darktable_t darktable
Definition darktable.c:181
void dt_print(dt_debug_thread_t thread, const char *msg,...)
Definition darktable.c:1542
#define dt_free_align(ptr)
Definition darktable.h:481
static void * dt_calloc_align(size_t size)
Definition darktable.h:488
@ DT_DEBUG_MASKS
Definition darktable.h:727
#define dt_pixelpipe_cache_alloc_align_float_cache(pixels, id)
Definition darktable.h:447
#define DT_MODULE_INTROSPECTION(MODVER, PARAMSTYPE)
Definition darktable.h:151
#define dt_pixelpipe_cache_free_align(mem)
Definition darktable.h:453
#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
void init(dt_iop_module_t *module)
Definition detailmask.c:275
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:81
int default_group()
Definition detailmask.c:48
struct dt_iop_detailmask_params_t dt_iop_detailmask_data_t
Definition detailmask.c:41
const char * name()
Definition detailmask.c:43
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:72
void cleanup_pipe(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
Definition detailmask.c:269
int default_colorspace(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece)
Definition detailmask.c:58
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:63
int flags()
Definition detailmask.c:53
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:255
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:99
void init_pipe(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
Definition detailmask.c:262
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:160
void dt_iop_params_t
Definition dev_history.h:41
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)
Definition format.c:75
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)
Definition format.c:57
void dt_iop_buffer_dsc_update_bpp(dt_iop_buffer_dsc_t *dsc)
Definition format.c:28
@ TYPE_FLOAT
Definition format.h:46
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:159
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:87
void dt_iop_default_init(dt_iop_module_t *module)
Definition imageop.c:316
@ IOP_FLAGS_HIDDEN
Definition imageop.h:170
@ IOP_FLAGS_ONE_INSTANCE
Definition imageop.h:172
@ IOP_FLAGS_NO_HISTORY_STACK
Definition imageop.h:174
@ IOP_GROUP_TECHNICAL
Definition imageop.h:143
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)
void dt_masks_calc_rawdetail_mask(float *const src, float *const out, float *const tmp, const int width, const int height, const dt_aligned_pixel_t wb)
float dt_aligned_pixel_t[4]
int dt_opencl_enqueue_kernel_2d(const int dev, const int kernel, const size_t *sizes)
Definition opencl.c:2136
void * dt_opencl_alloc_device_buffer(const int devid, const size_t size)
Definition opencl.c:2544
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:2261
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:2309
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:2127
void dt_opencl_release_mem_object(cl_mem mem)
Definition opencl.c:2383
#define ROUNDUPDHT(a, b)
Definition opencl.h:82
#define ROUNDUPDWD(a, b)
Definition opencl.h:81
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
int dt_dev_pixelpipe_cache_get(dt_dev_pixelpipe_cache_t *cache, 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.
int dt_dev_pixelpipe_cache_remove(dt_dev_pixelpipe_cache_t *cache, const gboolean force, dt_pixel_cache_entry_t *cache_entry)
Arbitrarily remove the cache entry matching hash. Entries having a reference count > 0 (inter-thread ...
void dt_dev_pixelpipe_cache_wrlock_entry(dt_dev_pixelpipe_cache_t *cache, gboolean lock, dt_pixel_cache_entry_t *cache_entry)
Lock or release the write lock on the entry.
unsigned __int64 uint64_t
Definition strptime.c:75
struct dt_dev_pixelpipe_cache_t * pixelpipe_cache
Definition darktable.h:790
struct dt_opencl_t * opencl
Definition darktable.h:785
dt_iop_buffer_dsc_t dsc_in
struct dt_iop_module_t *void * data
dt_dev_pixelpipe_type_t type
uint64_t rawdetail_mask_hash
struct dt_iop_roi_t rawdetail_mask_roi
dt_aligned_pixel_t coeffs
Definition format.h:81
struct dt_iop_buffer_dsc_t::@30 temperature
unsigned int channels
Definition format.h:54
dt_iop_buffer_type_t datatype
Definition format.h:56
Region of interest passed through the pixelpipe.
Definition imageop.h:72
double scale
Definition imageop.h:74
struct dt_blendop_cl_global_t * blendop
Definition opencl.h:251