11/*
22 This file is part of darktable,
3- Copyright (C) 2013-2024 darktable developers.
3+ Copyright (C) 2013-2025 darktable developers.
44
55 darktable is free software: you can redistribute it and/or modify
66 it under the terms of the GNU General Public License as published by
104104105105*/
106106
107- gboolean dt_masks_calc_scharr_mask (dt_dev_detail_mask_t * details ,
108- float * const restrict src ,
109- const dt_aligned_pixel_t wb )
107+ float * dt_masks_calc_scharr_mask (dt_dev_pixelpipe_t * pipe ,
108+ float * const restrict src ,
109+ const int width ,
110+ const int height ,
111+ const gboolean rawmode )
110112{
111- const int width = details -> roi .width ;
112- const int height = details -> roi .height ;
113- float * mask = details -> data ;
113+ float * mask = dt_iop_image_alloc (width , height , 1 );
114+ float * tmp = dt_iop_image_alloc (width , height , 1 );
114115
115116 const size_t msize = (size_t )width * height ;
116- float * tmp = dt_alloc_align_float (msize );
117- if (!tmp ) return TRUE;
117+ if (!tmp || !mask )
118+ {
119+ dt_free_align (tmp );
120+ dt_free_align (mask );
121+ return NULL ;
122+ }
123+
124+ const gboolean wboff = !pipe -> dsc .temperature .enabled || !rawmode ;
125+ const dt_aligned_pixel_t wb = { wboff ? 1.0f : pipe -> dsc .temperature .coeffs [0 ],
126+ wboff ? 1.0f : pipe -> dsc .temperature .coeffs [1 ],
127+ wboff ? 1.0f : pipe -> dsc .temperature .coeffs [2 ] };
128+
118129
119- DT_OMP_FOR_SIMD (aligned (tmp , src : 64 ))
130+ DT_OMP_FOR_SIMD (aligned (tmp : 64 ))
120131 for (size_t idx = 0 ; idx < msize ; idx ++ )
121132 {
122133 const float val = fmaxf (0.0f , src [4 * idx ] / wb [0 ])
@@ -136,21 +147,38 @@ gboolean dt_masks_calc_scharr_mask(dt_dev_detail_mask_t *details,
136147 const size_t idx = (size_t )irow * width + icol ;
137148
138149 const float gradient_magnitude = scharr_gradient (& tmp [idx ], width );
139- mask [( size_t ) row * width + col ] = fminf ( 1.0f , fmaxf ( 0.0f , gradient_magnitude / 16.0f ) );
150+ mask [row * width + col ] = CLIP ( gradient_magnitude / 16.0f );
140151 }
141152 }
142153 dt_free_align (tmp );
143- return FALSE ;
154+ return mask ;
144155}
145156
146- static inline float _calcBlendFactor (float val , float ithreshold )
157+ static inline float _calcBlendFactor (const float val , const float ithreshold )
147158{
148159 // sigmoid function
149160 // result is in ]0;1] range
150161 // inflexion point is at (x, y) (threshold, 0.5)
151162 return 1.0f / (1.0f + dt_fast_expf (16.0f - ithreshold * val ));
152163}
153164
165+ void dt_masks_calc_detail_blend (float * const restrict src ,
166+ float * out ,
167+ const size_t msize ,
168+ const float threshold ,
169+ const gboolean detail )
170+ {
171+ if (!src || !out ) return ;
172+
173+ const float ithreshold = 16.0f / MAX (1e-7 , threshold );
174+ DT_OMP_FOR_SIMD (aligned (src , out : 64 ))
175+ for (size_t idx = 0 ; idx < msize ; idx ++ )
176+ {
177+ const float blend = CLIP (_calcBlendFactor (src [idx ], ithreshold ));
178+ out [idx ] = detail ? blend : 1.0f - blend ;
179+ }
180+ }
181+
154182float * dt_masks_calc_detail_mask (dt_dev_pixelpipe_iop_t * piece ,
155183 const float threshold ,
156184 const gboolean detail )
@@ -171,17 +199,8 @@ float *dt_masks_calc_detail_mask(dt_dev_pixelpipe_iop_t *piece,
171199 return NULL ;
172200 }
173201
174- const float ithreshold = 16.0f / (fmaxf (1e-7 , threshold ));
175- float * src = details -> data ;
176- DT_OMP_FOR_SIMD (aligned (src , tmp : 64 ))
177- for (size_t idx = 0 ; idx < msize ; idx ++ )
178- {
179- const float blend = CLIP (_calcBlendFactor (src [idx ], ithreshold ));
180- tmp [idx ] = detail ? blend : 1.0f - blend ;
181- }
182- // for very small images the blurring should be slightly less to have an effect at all
183- const float sigma = (MIN (details -> roi .width , details -> roi .height ) < 500 ) ? 1.5f : 2.0f ;
184- dt_gaussian_fast_blur (tmp , mask , details -> roi .width , details -> roi .height , sigma , 0.0f , 1.0f , 1 );
202+ dt_masks_calc_detail_blend (details -> data , tmp , msize , threshold , detail );
203+ dt_gaussian_fast_blur (tmp , mask , details -> roi .width , details -> roi .height , 2.0f , 0.0f , 1.0f , 1 );
185204 dt_free_align (tmp );
186205 return mask ;
187206}
0 commit comments