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-2020 darktable developers.
4
5 darktable 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 darktable 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 darktable. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#pragma once
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#ifdef HAVE_OPENCL
25#include <CL/cl.h> // for cl_mem
26#endif
27
28#include "common/image.h" // for dt_image_t, dt_image_orientation_t
29#include "develop/imageop.h" // for dt_iop_roi_t
30#include <glib.h> // for inline
31#include <math.h> // for log, logf, powf
32#include <stddef.h> // for size_t, NULL
33#include <stdint.h> // for uint8_t, uint16_t, uint32_t
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
40void 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,
41 const dt_image_orientation_t orientation, uint32_t *width, uint32_t *height);
42
44void dt_iop_clip_and_zoom(float *out, const float *const in, const struct dt_iop_roi_t *const roi_out,
45 const struct dt_iop_roi_t *const roi_in, const int32_t out_stride,
46 const int32_t in_stride);
47
49void dt_iop_clip_and_zoom_roi(float *out, const float *const in, const struct dt_iop_roi_t *const roi_out,
50 const struct dt_iop_roi_t *const roi_in, const int32_t out_stride,
51 const int32_t in_stride);
52#ifdef HAVE_OPENCL
53int dt_iop_clip_and_zoom_cl(int devid, cl_mem dev_out, cl_mem dev_in,
54 const struct dt_iop_roi_t *const roi_out,
55 const struct dt_iop_roi_t *const roi_in);
56
57int dt_iop_clip_and_zoom_roi_cl(int devid, cl_mem dev_out, cl_mem dev_in,
58 const struct dt_iop_roi_t *const roi_out,
59 const struct dt_iop_roi_t *const roi_in);
60#endif
61
62void dt_iop_clip_and_zoom_mosaic_half_size_f(float *const out, const float *const in,
63 const dt_iop_roi_t *const roi_out, const dt_iop_roi_t *const roi_in,
64 const int32_t out_stride, const int32_t in_stride,
65 const uint32_t filters);
66
67void dt_iop_clip_and_zoom_mosaic_half_size(uint16_t *const out, const uint16_t *const in,
68 const dt_iop_roi_t *const roi_out, const dt_iop_roi_t *const roi_in,
69 const int32_t out_stride, const int32_t in_stride,
70 const uint32_t filters);
71
72void dt_iop_clip_and_zoom_mosaic_third_size_xtrans(uint16_t *const out, const uint16_t *const in,
73 const dt_iop_roi_t *const roi_out,
74 const dt_iop_roi_t *const roi_in, const int32_t out_stride,
75 const int32_t in_stride, const uint8_t (*const xtrans)[6]);
76
77void dt_iop_clip_and_zoom_mosaic_third_size_xtrans_f(float *const out, const float *const in,
78 const dt_iop_roi_t *const roi_out,
79 const dt_iop_roi_t *const roi_in, const int32_t out_stride,
80 const int32_t in_stride, const uint8_t (*const xtrans)[6]);
81
82void dt_iop_clip_and_zoom_demosaic_passthrough_monochrome_f(float *out, const float *const in,
83 const struct dt_iop_roi_t *const roi_out,
84 const struct dt_iop_roi_t *const roi_in,
85 const int32_t out_stride,
86 const int32_t in_stride);
87
88void dt_iop_clip_and_zoom_demosaic_half_size_f(float *out, const float *const in,
89 const struct dt_iop_roi_t *const roi_out,
90 const struct dt_iop_roi_t *const roi_in, const int32_t out_stride,
91 const int32_t in_stride, const uint32_t filters);
92
95void dt_iop_clip_and_zoom_demosaic_third_size_xtrans_f(float *out, const float *const in,
96 const struct dt_iop_roi_t *const roi_out,
97 const struct dt_iop_roi_t *const roi_in,
98 const int32_t out_stride, const int32_t in_stride,
99 const uint8_t (*const xtrans)[6]);
100
102void 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,
103 int32_t ibh, uint8_t *o, int32_t ox, int32_t oy, int32_t ow, int32_t oh,
104 int32_t obw, int32_t obh);
105
106void dt_iop_YCbCr_to_RGB(const dt_aligned_pixel_t yuv, dt_aligned_pixel_t rgb);
107void dt_iop_RGB_to_YCbCr(const dt_aligned_pixel_t rgb, dt_aligned_pixel_t yuv);
108
111void dt_iop_estimate_cubic(const float x[4], const float y[4], float a[4]);
112
114static inline float dt_iop_eval_cubic(const float *const a, const float x)
115{
116 // could be sse4.1 _mm_dot_ps
117 const float x4[4] = { x * x * x, x * x, x, 1.0f };
118 return a[3] * x4[3] + a[2] * x4[2] + a[1] * x4[1] + a[0] * x4[0];
119}
120
123static inline void dt_iop_estimate_exp(const float *const x, const float *const y, const int num, float *coeff)
124{
125 // map every thing to y = y0*(x/x0)^g
126 // and fix (x0,y0) as the last point.
127 // assume (x,y) pairs are ordered by ascending x, so this is the last point:
128 const float x0 = x[num - 1], y0 = y[num - 1];
129
130 float g = 0.0f;
131 int cnt = 0;
132 // solving for g yields
133 // g = log(y/y0)/log(x/x0)
134 //
135 // average that over the course of the other samples:
136 for(int k = 0; k < num - 1; k++)
137 {
138 const float yy = y[k] / y0, xx = x[k] / x0;
139 if(yy > 0.0f && xx > 0.0f)
140 {
141 const float gg = logf(y[k] / y0) / logf(x[k] / x0);
142 g += gg;
143 cnt++;
144 }
145 }
146 if(cnt)
147 g *= 1.0f / cnt;
148 else
149 g = 1.0f;
150 coeff[0] = 1.0f / x0;
151 coeff[1] = y0;
152 coeff[2] = g;
153}
154
155
157#ifdef _OPENMP
158#pragma omp declare simd
159#endif
160static inline float dt_iop_eval_exp(const float *const coeff, const float x)
161{
162 return coeff[1] * powf(x * coeff[0], coeff[2]);
163}
164
165
167#ifdef _OPENMP
168#pragma omp declare simd uniform(width, height) aligned(ivoid, ovoid:64)
169#endif
170static inline void dt_iop_alpha_copy(const void *const ivoid,
171 void *const ovoid,
172 const size_t width, const size_t height)
173{
174 const float *const __restrict__ in = (const float *const)ivoid;
175 float *const __restrict__ out = (float *const)ovoid;
176
177#ifdef _OPENMP
178#pragma omp parallel for simd default(none) aligned(out, in:64)\
179 dt_omp_firstprivate(height, width, out, in) \
180 schedule(static)
181#endif
182 for(size_t k = 3; k < width * height * 4; k += 4)
183 out[k] = in[k];
184}
185
187static inline int FC(const size_t row, const size_t col, const uint32_t filters)
188{
189 return filters >> (((row << 1 & 14) + (col & 1)) << 1) & 3;
190}
191
192#define RED 0
193#define GREEN 1
194#define BLUE 2
195#define ALPHA 3
196
198static inline int FCxtrans(const int row, const int col, const dt_iop_roi_t *const roi,
199 const uint8_t (*const xtrans)[6])
200{
201 // Add +600 (which must be a multiple of CFA width 6) as offset can
202 // be negative and need to ensure a non-negative array index. The
203 // negative offsets in current code come from the demosaic iop:
204 // Markesteijn 1-pass (-12), Markesteijn 3-pass (-17), and VNG (-2).
205 int irow = row + 600;
206 int icol = col + 600;
207 assert(irow >= 0 && icol >= 0);
208
209 if(roi)
210 {
211 irow += roi->y;
212 icol += roi->x;
213 }
214
215 return xtrans[irow % 6][icol % 6];
216}
217
218
219#ifdef _OPENMP
220#pragma omp declare simd
221#endif
222static inline int fcol(const int row, const int col, const uint32_t filters, const uint8_t (*const xtrans)[6])
223{
224 if(filters == 9)
225 return FCxtrans(row, col, NULL, xtrans);
226 else
227 return FC(row, col, filters);
228}
229
230#ifdef __cplusplus
231}
232#endif
233
234// clang-format off
235// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
236// vim: shiftwidth=2 expandtab tabstop=2 cindent
237// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
238// clang-format on
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
dt_image_orientation_t
Definition common/image.h:123
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:93
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:244
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:187
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:170
static float dt_iop_eval_cubic(const float *const a, const float x)
Definition imageop_math.h:114
void dt_iop_YCbCr_to_RGB(const dt_aligned_pixel_t yuv, dt_aligned_pixel_t rgb)
Definition imageop_math.c:903
void dt_iop_RGB_to_YCbCr(const dt_aligned_pixel_t rgb, dt_aligned_pixel_t yuv)
Definition imageop_math.c:896
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:28
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:198
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:172
static void dt_iop_estimate_exp(const float *const x, const float *const y, const int num, float *coeff)
Definition imageop_math.h:123
void dt_iop_estimate_cubic(const float x[4], const float y[4], float a[4])
Definition imageop_math.c:987
static int fcol(const int row, const int col, const uint32_t filters, const uint8_t(*const xtrans)[6])
Definition imageop_math.h:222
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:160
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:436
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:480
Definition imageop.h:32
int x
Definition imageop.h:33
int y
Definition imageop.h:33