Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
imageop_math.h
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2016 Dan Torop.
4 Copyright (C) 2016 Pedro Côrte-Real.
5 Copyright (C) 2016, 2018 Roman Lebedev.
6 Copyright (C) 2016 Tobias Ellinghaus.
7 Copyright (C) 2019 Andreas Schneider.
8 Copyright (C) 2019-2021, 2025 Aurélien PIERRE.
9 Copyright (C) 2019 Marcus Rückert.
10 Copyright (C) 2020 Diederik Ter Rahe.
11 Copyright (C) 2020 Pascal Obry.
12 Copyright (C) 2020-2021 Ralf Brown.
13 Copyright (C) 2022 Martin Bařinka.
14 Copyright (C) 2025 Alynx Zhou.
15
16 darktable is free software: you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation, either version 3 of the License, or
19 (at your option) any later version.
20
21 darktable is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with darktable. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#pragma once
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#ifdef HAVE_OPENCL
36#include <CL/cl.h> // for cl_mem
37#endif
38
39#include "common/image.h" // for dt_image_t, dt_image_orientation_t
40#include "develop/imageop.h" // for dt_iop_roi_t
41#include <glib.h> // for inline
42#include <math.h> // for log, logf, powf
43#include <stddef.h> // for size_t, NULL
44#include <stdint.h> // for uint8_t, uint16_t, uint32_t
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
51void dt_iop_flip_and_zoom_8(const uint8_t *in, int32_t iw, int32_t ih, uint8_t *out, int32_t ow, int32_t oh,
52 const dt_image_orientation_t orientation, uint32_t *width, uint32_t *height);
53
55void dt_iop_clip_and_zoom(float *out, const float *const in, const struct dt_iop_roi_t *const roi_out,
56 const struct dt_iop_roi_t *const roi_in, const int32_t out_stride,
57 const int32_t in_stride);
58
60void dt_iop_clip_and_zoom_roi(float *out, const float *const in, const struct dt_iop_roi_t *const roi_out,
61 const struct dt_iop_roi_t *const roi_in, const int32_t out_stride,
62 const int32_t in_stride);
63#ifdef HAVE_OPENCL
64int dt_iop_clip_and_zoom_cl(int devid, cl_mem dev_out, cl_mem dev_in,
65 const struct dt_iop_roi_t *const roi_out,
66 const struct dt_iop_roi_t *const roi_in);
67
68int dt_iop_clip_and_zoom_roi_cl(int devid, cl_mem dev_out, cl_mem dev_in,
69 const struct dt_iop_roi_t *const roi_out,
70 const struct dt_iop_roi_t *const roi_in);
71#endif
72
73void dt_iop_clip_and_zoom_mosaic_half_size_f(float *const out, const float *const in,
74 const dt_iop_roi_t *const roi_out, const dt_iop_roi_t *const roi_in,
75 const int32_t out_stride, const int32_t in_stride,
76 const uint32_t filters);
77
78void dt_iop_clip_and_zoom_mosaic_half_size(uint16_t *const out, const uint16_t *const in,
79 const dt_iop_roi_t *const roi_out, const dt_iop_roi_t *const roi_in,
80 const int32_t out_stride, const int32_t in_stride,
81 const uint32_t filters);
82
83void dt_iop_clip_and_zoom_mosaic_third_size_xtrans(uint16_t *const out, const uint16_t *const in,
84 const dt_iop_roi_t *const roi_out,
85 const dt_iop_roi_t *const roi_in, const int32_t out_stride,
86 const int32_t in_stride, const uint8_t (*const xtrans)[6]);
87
88void dt_iop_clip_and_zoom_mosaic_third_size_xtrans_f(float *const out, const float *const in,
89 const dt_iop_roi_t *const roi_out,
90 const dt_iop_roi_t *const roi_in, const int32_t out_stride,
91 const int32_t in_stride, const uint8_t (*const xtrans)[6]);
92
94 const struct dt_iop_roi_t *const roi_out,
95 const struct dt_iop_roi_t *const roi_in,
96 const int32_t out_stride,
97 const int32_t in_stride);
98
99void dt_iop_clip_and_zoom_demosaic_half_size_f(float *out, const float *const in,
100 const struct dt_iop_roi_t *const roi_out,
101 const struct dt_iop_roi_t *const roi_in, const int32_t out_stride,
102 const int32_t in_stride, const uint32_t filters);
103
107 const struct dt_iop_roi_t *const roi_out,
108 const struct dt_iop_roi_t *const roi_in,
109 const int32_t out_stride, const int32_t in_stride,
110 const uint8_t (*const xtrans)[6]);
111
113void dt_iop_clip_and_zoom_8(const uint8_t *i, int32_t ix, int32_t iy, int32_t iw, int32_t ih, int32_t ibw,
114 int32_t ibh, uint8_t *o, int32_t ox, int32_t oy, int32_t ow, int32_t oh,
115 int32_t obw, int32_t obh);
116
117void dt_iop_YCbCr_to_RGB(const dt_aligned_pixel_t yuv, dt_aligned_pixel_t rgb);
118void dt_iop_RGB_to_YCbCr(const dt_aligned_pixel_t rgb, dt_aligned_pixel_t yuv);
119
122void dt_iop_estimate_cubic(const float x[4], const float y[4], float a[4]);
123
125static inline float dt_iop_eval_cubic(const float *const a, const float x)
126{
127 // could be sse4.1 _mm_dot_ps
128 const float x4[4] = { x * x * x, x * x, x, 1.0f };
129 return a[3] * x4[3] + a[2] * x4[2] + a[1] * x4[1] + a[0] * x4[0];
130}
131
134static inline void dt_iop_estimate_exp(const float *const x, const float *const y, const int num, float *coeff)
135{
136 // map every thing to y = y0*(x/x0)^g
137 // and fix (x0,y0) as the last point.
138 // assume (x,y) pairs are ordered by ascending x, so this is the last point:
139 const float x0 = x[num - 1], y0 = y[num - 1];
140
141 float g = 0.0f;
142 int cnt = 0;
143 // solving for g yields
144 // g = log(y/y0)/log(x/x0)
145 //
146 // average that over the course of the other samples:
147 for(int k = 0; k < num - 1; k++)
148 {
149 const float yy = y[k] / y0, xx = x[k] / x0;
150 if(yy > 0.0f && xx > 0.0f)
151 {
152 const float gg = logf(y[k] / y0) / logf(x[k] / x0);
153 g += gg;
154 cnt++;
155 }
156 }
157 if(cnt)
158 g *= 1.0f / cnt;
159 else
160 g = 1.0f;
161 coeff[0] = 1.0f / x0;
162 coeff[1] = y0;
163 coeff[2] = g;
164}
165
166
168#ifdef _OPENMP
169#pragma omp declare simd
170#endif
171static inline float dt_iop_eval_exp(const float *const coeff, const float x)
172{
173 return coeff[1] * powf(x * coeff[0], coeff[2]);
174}
175
176
178#ifdef _OPENMP
179#pragma omp declare simd uniform(width, height) aligned(ivoid, ovoid:64)
180#endif
181static inline void dt_iop_alpha_copy(const void *const ivoid,
182 void *const ovoid,
183 const size_t width, const size_t height)
184{
185 const float *const __restrict__ in = (const float *const)ivoid;
186 float *const __restrict__ out = (float *const)ovoid;
187
188#ifdef _OPENMP
189#pragma omp parallel for simd default(none) aligned(out, in:64)\
190 dt_omp_firstprivate(height, width, out, in) \
191 schedule(static)
192#endif
193 for(size_t k = 3; k < width * height * 4; k += 4)
194 out[k] = in[k];
195}
196
198static inline int FC(const size_t row, const size_t col, const uint32_t filters)
199{
200 return filters >> (((row << 1 & 14) + (col & 1)) << 1) & 3;
201}
202
203#define RED 0
204#define GREEN 1
205#define BLUE 2
206#define ALPHA 3
207
209static inline int FCxtrans(const int row, const int col, const dt_iop_roi_t *const roi,
210 const uint8_t (*const xtrans)[6])
211{
212 // Add +600 (which must be a multiple of CFA width 6) as offset can
213 // be negative and need to ensure a non-negative array index. The
214 // negative offsets in current code come from the demosaic iop:
215 // Markesteijn 1-pass (-12), Markesteijn 3-pass (-17), and VNG (-2).
216 int irow = row + 600;
217 int icol = col + 600;
218 assert(irow >= 0 && icol >= 0);
219
220 if(roi)
221 {
222 irow += roi->y;
223 icol += roi->x;
224 }
225
226 return xtrans[irow % 6][icol % 6];
227}
228
229
230#ifdef _OPENMP
231#pragma omp declare simd
232#endif
233static inline int fcol(const int row, const int col, const uint32_t filters, const uint8_t (*const xtrans)[6])
234{
235 if(filters == 9)
236 return FCxtrans(row, col, NULL, xtrans);
237 else
238 return FC(row, col, filters);
239}
240
241#ifdef __cplusplus
242}
243#endif
244
245// clang-format off
246// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
247// vim: shiftwidth=2 expandtab tabstop=2 cindent
248// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
249// clang-format on
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
const float i
Definition colorspaces_inline_conversions.h:669
const float g
Definition colorspaces_inline_conversions.h:925
const float a
Definition colorspaces_inline_conversions.h:1292
static const dt_colormatrix_t dt_aligned_pixel_t out
Definition colorspaces_inline_conversions.h:184
static const int row
Definition colorspaces_inline_conversions.h:175
static dt_aligned_pixel_t rgb
Definition colorspaces_inline_conversions.h:530
dt_image_orientation_t
Definition common/image.h:169
void dt_iop_clip_and_zoom_8(const uint8_t *i, int32_t ix, int32_t iy, int32_t iw, int32_t ih, int32_t ibw, int32_t ibh, uint8_t *o, int32_t ox, int32_t oy, int32_t ow, int32_t oh, int32_t obw, int32_t obh)
Definition imageop_math.c:101
void dt_iop_clip_and_zoom_demosaic_passthrough_monochrome_f(float *out, const float *const in, const struct dt_iop_roi_t *const roi_out, const struct dt_iop_roi_t *const roi_in, const int32_t out_stride, const int32_t in_stride)
void dt_iop_clip_and_zoom(float *out, const float *const in, const struct dt_iop_roi_t *const roi_out, const struct dt_iop_roi_t *const roi_in, const int32_t out_stride, const int32_t in_stride)
void dt_iop_clip_and_zoom_mosaic_half_size_f(float *const out, const float *const in, const dt_iop_roi_t *const roi_out, const dt_iop_roi_t *const roi_in, const int32_t out_stride, const int32_t in_stride, const uint32_t filters)
Definition imageop_math.c:252
void dt_iop_clip_and_zoom_demosaic_third_size_xtrans_f(float *out, const float *const in, const struct dt_iop_roi_t *const roi_out, const struct dt_iop_roi_t *const roi_in, const int32_t out_stride, const int32_t in_stride, const uint8_t(*const xtrans)[6])
static int FC(const size_t row, const size_t col, const uint32_t filters)
Definition imageop_math.h:198
static void dt_iop_alpha_copy(const void *const ivoid, void *const ovoid, const size_t width, const size_t height)
Definition imageop_math.h:181
static float dt_iop_eval_cubic(const float *const a, const float x)
Definition imageop_math.h:125
void dt_iop_YCbCr_to_RGB(const dt_aligned_pixel_t yuv, dt_aligned_pixel_t rgb)
Definition imageop_math.c:911
void dt_iop_RGB_to_YCbCr(const dt_aligned_pixel_t rgb, dt_aligned_pixel_t yuv)
Definition imageop_math.c:904
void dt_iop_flip_and_zoom_8(const uint8_t *in, int32_t iw, int32_t ih, uint8_t *out, int32_t ow, int32_t oh, const dt_image_orientation_t orientation, uint32_t *width, uint32_t *height)
Definition imageop_math.c:36
static int FCxtrans(const int row, const int col, const dt_iop_roi_t *const roi, const uint8_t(*const xtrans)[6])
Definition imageop_math.h:209
void dt_iop_clip_and_zoom_mosaic_half_size(uint16_t *const out, const uint16_t *const in, const dt_iop_roi_t *const roi_out, const dt_iop_roi_t *const roi_in, const int32_t out_stride, const int32_t in_stride, const uint32_t filters)
Definition imageop_math.c:180
static void dt_iop_estimate_exp(const float *const x, const float *const y, const int num, float *coeff)
Definition imageop_math.h:134
void dt_iop_estimate_cubic(const float x[4], const float y[4], float a[4])
Definition imageop_math.c:995
static int fcol(const int row, const int col, const uint32_t filters, const uint8_t(*const xtrans)[6])
Definition imageop_math.h:233
void dt_iop_clip_and_zoom_demosaic_half_size_f(float *out, const float *const in, const struct dt_iop_roi_t *const roi_out, const struct dt_iop_roi_t *const roi_in, const int32_t out_stride, const int32_t in_stride, const uint32_t filters)
static float dt_iop_eval_exp(const float *const coeff, const float x)
Definition imageop_math.h:171
void dt_iop_clip_and_zoom_mosaic_third_size_xtrans(uint16_t *const out, const uint16_t *const in, const dt_iop_roi_t *const roi_out, const dt_iop_roi_t *const roi_in, const int32_t out_stride, const int32_t in_stride, const uint8_t(*const xtrans)[6])
Definition imageop_math.c:444
void dt_iop_clip_and_zoom_roi(float *out, const float *const in, const struct dt_iop_roi_t *const roi_out, const struct dt_iop_roi_t *const roi_in, const int32_t out_stride, const int32_t in_stride)
void dt_iop_clip_and_zoom_mosaic_third_size_xtrans_f(float *const out, const float *const in, const dt_iop_roi_t *const roi_out, const dt_iop_roi_t *const roi_in, const int32_t out_stride, const int32_t in_stride, const uint8_t(*const xtrans)[6])
Definition imageop_math.c:488
static const float x
Definition iop_profile.h:239
static const float *const const float coeff[3]
Definition iop_profile.h:246
Definition imageop.h:67
int x
Definition imageop.h:68
int y
Definition imageop.h:68