Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
equalizer_eaw.h
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2009-2011 johannes hanika.
4 Copyright (C) 2011 Henrik Andersson.
5 Copyright (C) 2012 Richard Wonka.
6 Copyright (C) 2014, 2016 Roman Lebedev.
7 Copyright (C) 2014, 2016 Tobias Ellinghaus.
8 Copyright (C) 2014 Ulrich Pegelow.
9 Copyright (C) 2019 Andreas Schneider.
10 Copyright (C) 2020 Hubert Kowalski.
11 Copyright (C) 2020-2021 Pascal Obry.
12 Copyright (C) 2021 Ralf Brown.
13 Copyright (C) 2022 Martin Bařinka.
14 Copyright (C) 2025-2026 Aurélien PIERRE.
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
32// edge-avoiding wavelet:
33#define gweight(i, j, ii, jj) \
34 1.0 / (fabsf(weight_a[l][(size_t)wd * ((j) >> (l - 1)) + ((i) >> (l - 1))] \
35 - weight_a[l][(size_t)wd * ((jj) >> (l - 1)) + ((ii) >> (l - 1))]) + 1.e-5)
36// #define gweight(i, j, ii, jj) 1.0/(powf(fabsf(weight_a[l][wd*((j)>>(l-1)) + ((i)>>(l-1))] -
37// weight_a[l][wd*((jj)>>(l-1)) + ((ii)>>(l-1))]),0.8)+1.e-5)
38// std cdf(2,2) wavelet:
39// #define gweight(i, j, ii, jj) (wd ? 1.0 : 1.0) //1.0
40#define gbuf(BUF, A, B) ((BUF)[4 * ((size_t)width * ((B)) + ((A))) + ch])
41
42
43static int dt_iop_equalizer_wtf(float *const buf, float **weight_a, const int l, const int width, const int height)
44{
45 const int wd = (int)(1 + (width >> (l - 1))), ht = (int)(1 + (height >> (l - 1)));
46 int ch = 0;
47 // store weights for luma channel only, chroma uses same basis.
48 for(int j = 0; j < ht - 1; j++)
49 {
50 for(int i = 0; i < wd - 1; i++) weight_a[l][(size_t)j * wd + i] = gbuf(buf, i << (l - 1), j << (l - 1));
51 weight_a[l][j * wd + (wd - 1)] = 0.0f; // zero out right-most column
52 }
53 for(int i = 0; i < wd; i++) // zero out the bottom row
54 weight_a[l][(ht-1) * wd + i] = 0.0f;
55
56 const int step = 1 << l;
57 const int st = step / 2;
58
59 size_t scratch_size;
60 float *const restrict tmp_width_buf = dt_pixelpipe_cache_alloc_perthread_float(width, &scratch_size);
61 if(IS_NULL_PTR(tmp_width_buf)) return 1;
62 __OMP_PARALLEL_FOR__(private(ch) )
63 for(int j = 0; j < height; j++)
64 {
65 // rows
66 // precompute weights:
67 float *tmp = dt_get_perthread(tmp_width_buf, scratch_size);
68 for(int i = 0; i < width - st; i += st) tmp[i] = gweight(i, j, i + st, j);
69 // predict, get detail
70 int i = st;
71 for(; i < width - st; i += step)
72 for(ch = 0; ch < 3; ch++)
73 gbuf(buf, i, j) -= (tmp[i - st] * gbuf(buf, i - st, j) + tmp[i] * gbuf(buf, i + st, j))
74 / (tmp[i - st] + tmp[i]);
75 if(i < width)
76 for(ch = 0; ch < 3; ch++) gbuf(buf, i, j) -= gbuf(buf, i - st, j);
77 // update coarse
78 for(ch = 0; ch < 3; ch++) gbuf(buf, 0, j) += gbuf(buf, st, j) * 0.5f;
79 for(i = step; i < width - st; i += step)
80 for(ch = 0; ch < 3; ch++)
81 gbuf(buf, i, j) += (tmp[i - st] * gbuf(buf, i - st, j) + tmp[i] * gbuf(buf, i + st, j))
82 / (2.0 * (tmp[i - st] + tmp[i]));
83 if(i < width)
84 for(ch = 0; ch < 3; ch++) gbuf(buf, i, j) += gbuf(buf, i - st, j) * .5f;
85 }
86
87 dt_pixelpipe_cache_free_align(tmp_width_buf);
88
89 float *const restrict tmp_height_buf = dt_pixelpipe_cache_alloc_perthread_float(height, &scratch_size);
90 if(IS_NULL_PTR(tmp_height_buf)) return 1;
91 __OMP_PARALLEL_FOR__(private(ch) )
92 for(int i = 0; i < width; i++)
93 {
94 // cols
95 // precompute weights:
96 float *const tmp = dt_get_perthread(tmp_height_buf, scratch_size);
97 for(int j = 0; j < height - st; j += st) tmp[j] = gweight(i, j, i, j + st);
98 int j = st;
99 // predict, get detail
100 for(; j < height - st; j += step)
101 for(ch = 0; ch < 3; ch++)
102 gbuf(buf, i, j) -= (tmp[j - st] * gbuf(buf, i, j - st) + tmp[j] * gbuf(buf, i, j + st))
103 / (tmp[j - st] + tmp[j]);
104 if(j < height)
105 for(ch = 0; ch < 3; ch++) gbuf(buf, i, j) -= gbuf(buf, i, j - st);
106 // update
107 for(ch = 0; ch < 3; ch++) gbuf(buf, i, 0) += gbuf(buf, i, st) * 0.5;
108 for(j = step; j < height - st; j += step)
109 for(ch = 0; ch < 3; ch++)
110 gbuf(buf, i, j) += (tmp[j - st] * gbuf(buf, i, j - st) + tmp[j] * gbuf(buf, i, j + st))
111 / (2.0 * (tmp[j - st] + tmp[j]));
112 if(j < height)
113 for(ch = 0; ch < 3; ch++) gbuf(buf, i, j) += gbuf(buf, i, j - st) * .5f;
114 }
115
116 dt_pixelpipe_cache_free_align(tmp_height_buf);
117 return 0;
118}
119
120static int dt_iop_equalizer_iwtf(float *buf, float **weight_a, const int l, const int width, const int height)
121{
122 const int step = 1 << l;
123 const int st = step / 2;
124 const int wd = (int)(1 + (width >> (l - 1)));
125
126 size_t scratch_size;
127 float *const restrict tmp_height_buf = dt_pixelpipe_cache_alloc_perthread_float(height, &scratch_size);
128 if(IS_NULL_PTR(tmp_height_buf)) return 1;
130 for(int i = 0; i < width; i++)
131 {
132 // cols
133 float *const restrict tmp = dt_get_perthread(tmp_height_buf, scratch_size);
134 int j;
135 for(j = 0; j < height - st; j += st) tmp[j] = gweight(i, j, i, j + st);
136 // update coarse
137 for(int ch = 0; ch < 3; ch++) gbuf(buf, i, 0) -= gbuf(buf, i, st) * 0.5f;
138 for(j = step; j < height - st; j += step)
139 for(int ch = 0; ch < 3; ch++)
140 gbuf(buf, i, j) -= (tmp[j - st] * gbuf(buf, i, j - st) + tmp[j] * gbuf(buf, i, j + st))
141 / (2.0 * (tmp[j - st] + tmp[j]));
142 if(j < height)
143 for(int ch = 0; ch < 3; ch++) gbuf(buf, i, j) -= gbuf(buf, i, j - st) * .5f;
144 // predict
145 for(j = st; j < height - st; j += step)
146 for(int ch = 0; ch < 3; ch++)
147 gbuf(buf, i, j) += (tmp[j - st] * gbuf(buf, i, j - st) + tmp[j] * gbuf(buf, i, j + st))
148 / (tmp[j - st] + tmp[j]);
149 if(j < height)
150 for(int ch = 0; ch < 3; ch++) gbuf(buf, i, j) += gbuf(buf, i, j - st);
151 }
152
153 dt_pixelpipe_cache_free_align(tmp_height_buf);
154
155 float *const restrict tmp_width_buf = dt_pixelpipe_cache_alloc_perthread_float(width, &scratch_size);
156 if(IS_NULL_PTR(tmp_width_buf)) return 1;
158 for(int j = 0; j < height; j++)
159 {
160 // rows
161 float *const restrict tmp = dt_get_perthread(tmp_width_buf, scratch_size);
162 for(int i = 0; i < width - st; i += st) tmp[i] = gweight(i, j, i + st, j);
163 // update
164 for(int ch = 0; ch < 3; ch++) gbuf(buf, 0, j) -= gbuf(buf, st, j) * 0.5f;
165 int i;
166 for(i = step; i < width - st; i += step)
167 for(int ch = 0; ch < 3; ch++)
168 gbuf(buf, i, j) -= (tmp[i - st] * gbuf(buf, i - st, j) + tmp[i] * gbuf(buf, i + st, j))
169 / (2.0 * (tmp[i - st] + tmp[i]));
170 if(i < width)
171 for(int ch = 0; ch < 3; ch++) gbuf(buf, i, j) -= gbuf(buf, i - st, j) * 0.5f;
172 // predict
173 for(i = st; i < width - st; i += step)
174 for(int ch = 0; ch < 3; ch++)
175 gbuf(buf, i, j) += (tmp[i - st] * gbuf(buf, i - st, j) + tmp[i] * gbuf(buf, i + st, j))
176 / (tmp[i - st] + tmp[i]);
177 if(i < width)
178 for(int ch = 0; ch < 3; ch++) gbuf(buf, i, j) += gbuf(buf, i - st, j);
179 }
180
181 dt_pixelpipe_cache_free_align(tmp_width_buf);
182 return 0;
183}
184
185#undef gbuf
186#undef gweight
187// clang-format off
188// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
189// vim: shiftwidth=2 expandtab tabstop=2 cindent
190// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
191// clang-format on
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
const float i
Definition colorspaces_inline_conversions.h:440
#define dt_pixelpipe_cache_free_align(mem)
Definition darktable.h:452
#define dt_get_perthread(buf, padsize)
Definition darktable.h:1035
#define __OMP_PARALLEL_FOR__(...)
Definition darktable.h:257
#define dt_pixelpipe_cache_alloc_perthread_float(n, padded_size)
Definition darktable.h:1030
#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:280
#define gweight(i, j, ii, jj)
Definition equalizer_eaw.h:33
static int dt_iop_equalizer_wtf(float *const buf, float **weight_a, const int l, const int width, const int height)
Definition equalizer_eaw.h:43
#define gbuf(BUF, A, B)
Definition equalizer_eaw.h:40
static int dt_iop_equalizer_iwtf(float *buf, float **weight_a, const int l, const int width, const int height)
Definition equalizer_eaw.h:120
float *const restrict const size_t const size_t ch
Definition luminance_mask.h:84