Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
testimg.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2020 Hubert Kowalski.
4 Copyright (C) 2020 Martin Burri.
5 Copyright (C) 2020 Pascal Obry.
6 Copyright (C) 2022 Martin Baƙinka.
7
8 darktable is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 darktable is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with darktable. If not, see <http://www.gnu.org/licenses/>.
20*/
21#include "common/darktable.h"
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <math.h>
26#include <float.h>
27
28#include "tracing.h"
29#include "testimg.h"
30
31// epsilon for floating point comparison (1e-6 is approximately 20 EV below pure
32// white):
33#define E 1e-6f
34
35Testimg *testimg_alloc(const int width, const int height)
36{
37 Testimg *ti = calloc(sizeof(Testimg), 1);
38 ti->width = width;
39 ti->height = height;
40 ti->pixels = calloc((size_t)4 * width * height, sizeof(float));
41 ti->name = "";
42 return ti;
43}
44
45void testimg_free(Testimg *const ti)
46{
47 dt_free(ti->pixels);
48 dt_free(ti);
49}
50
51void testimg_print_chan(const Testimg *const ti, int chan_idx)
52{
53 switch (chan_idx) {
54 case 0: TR_DEBUG("RED"); break;
55 case 1: TR_DEBUG("GREEN"); break;
56 case 2: TR_DEBUG("BLUE"); break;
57 case 3: TR_DEBUG("MASK"); break;
58 default: return;
59 }
61 {
62 printf(" %+.2e", p[chan_idx]);
63 if (x == ti->width-1) printf("\n");
64 }
65}
66
67void testimg_print_by_chan(const Testimg *const ti)
68{
69 TR_DEBUG("TEST IMAGE");
70 TR_DEBUG("name=%s, width=%i, height=%i", ti->name, ti->width, ti->height);
71
72 for (int c = 0; c < 4; c += 1) {
73 testimg_print_chan(ti, c);
74 }
75}
76
77void testimg_print_by_pixel(const Testimg *const ti)
78{
79 TR_DEBUG("TEST IMAGE");
80 TR_DEBUG("name=%s, width=%i, height=%i", ti->name, ti->width, ti->height);
81
82 for (int y = 0; y < ti->height; y += 1)
83 {
84 printf("y = %i\n", y);
85 for (int c = 0; c < 4; c += 1)
86 {
87 for (int x = 0; x < ti->width; x += 1)
88 {
89 float *p = get_pixel(ti, x, y);
90 printf(" %+.2e", p[c]);
91 }
92 printf("\n");
93 }
94 }
95}
96
98{
100 {
101 for (int c = 0; c < 3; c += 1)
102 {
103 p[c] = testimg_val_to_log(p[c]);
104 }
105 }
106 return ti;
107}
108
109float testimg_val_to_log(const float val)
110{
111 return 1.0f - log2f(1.0f / val) / TESTIMG_STD_DYN_RANGE_EV;
112}
113
115{
117 {
118 for (int c = 0; c < 3; c += 1)
119 {
120 p[c] = testimg_val_to_exp(p[c]);
121 }
122 }
123 return ti;
124}
125
126float testimg_val_to_exp(const float val)
127{
128 return exp2f(TESTIMG_STD_DYN_RANGE_EV * (val - 1.0f));
129}
130
132 const float value)
133{
135 ti->name = "all grey";
136
138 {
139 p[0] = p[1] = p[2] = value;
140 }
141 return ti;
142}
143
145{
147 ti->name = "all black";
148 return ti;
149}
150
152{
154 ti->name = "all white";
155 return ti;
156}
157
159{
160 const int height = 1;
162 ti->name = "grey space";
163
165 {
166 float val = (float)(x) / (float)(width-1);
167 p[0] = p[1] = p[2] = testimg_val_to_exp(val);
168 }
169 return ti;
170}
171
172Testimg *testimg_gen_single_color_space(const int width, const int color_index)
173{
174 const int height = 1;
176 ti->name = "single color space";
177
179 {
180 float val = (float)(x) / (float)(width-1);
181 p[color_index] = testimg_val_to_exp(val);
182 }
183 return ti;
184}
185
187{
188 const int height = 3;
190 ti->name = "three color space";
191
193 {
194 float val = (float)(x) / (float)(width-1);
195 p[y] = testimg_val_to_exp(val);
196 }
197 return ti;
198}
199
201{
202 const int height = width * width;
204 ti->name = "rgb space";
205 float *tmp = calloc(width, sizeof(float));
206
207 for (int x = 0; x < width; x += 1)
208 {
209 float val = (float)(x) / (float)(width-1);
210 tmp[x] = testimg_val_to_exp(val);
211 }
212
214 {
215 p[0] = tmp[x];
216 p[1] = tmp[y / width];
217 p[2] = tmp[y % width];
218 }
219 dt_free(tmp);
220 return ti;
221}
222
224{
225 const int width = 10;
226 const int height = 1;
228 ti->name = "grey max dr";
229
231 {
232 switch (x)
233 {
234 case 0: p[0] = p[1] = p[2] = FLT_MIN; break;
235 case 1: p[0] = p[1] = p[2] = 1e-20f; break;
236 case 2: p[0] = p[1] = p[2] = 1e-10f; break;
237 case 3: p[0] = p[1] = p[2] = 1e-5f; break;
238 case 4: p[0] = p[1] = p[2] = 1e-1f; break;
239 case 5: p[0] = p[1] = p[2] = 1.0f; break;
240 case 6: p[0] = p[1] = p[2] = 1e5f; break;
241 case 7: p[0] = p[1] = p[2] = 1e10f; break;
242 case 8: p[0] = p[1] = p[2] = 1e20f; break;
243 case 9: p[0] = p[1] = p[2] = FLT_MAX; break;
244 }
245 }
246 return ti;
247}
248
250{
252 Testimg *ti = testimg_alloc(tmp->width + 1, tmp->height);
253 ti->name = "grey max dr neg";
254
255 // copy values from testimg_grey_max_dr() in reverse order
256 // and make them negative:
258 {
259 float *p_tmp = get_pixel(tmp, tmp->width - x - 1, y);
260 p[0] = p[1] = p[2] = -p_tmp[0];
261 }
262 // fill last value with 0.0:
263 float *p = get_pixel(ti, ti->width - 1, 0);
264 p[0] = p[1] = p[2] = -0.0f;
265
266 testimdt_free(tmp);
267 return ti;
268}
269
271{
272 Testimg *ti = testimg_alloc(width, 3);
273 ti->name = "grey with rgb clipping";
274
276 {
277 float val = 0.9f + (float)(x) / (float)(width-1) / 10.0f;
278 p[0] = testimg_val_to_exp(val);
279 p[1] = testimg_val_to_exp(val);
280 p[2] = testimg_val_to_exp(val);
281 p[y] *= 1.25f; // add some
282 }
283 return ti;
284}
285// clang-format off
286// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
287// vim: shiftwidth=2 expandtab tabstop=2 cindent
288// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
289// clang-format on
290
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
static const dt_aligned_pixel_simd_t const dt_adaptation_t const float p
#define dt_free(ptr)
Definition darktable.h:456
static const dt_aligned_pixel_simd_t value
Definition darktable.h:577
static const float x
int width
Definition testimg.h:29
const char * name
Definition testimg.h:32
float * pixels
Definition testimg.h:31
int height
Definition testimg.h:30
void testimg_print_chan(const Testimg *const ti, int chan_idx)
Definition testimg.c:51
Testimg * testimg_gen_all_black(const int width, const int height)
Definition testimg.c:144
void testimg_free(Testimg *const ti)
Definition testimg.c:45
Testimg * testimg_gen_grey_max_dr_neg()
Definition testimg.c:249
float testimg_val_to_log(const float val)
Definition testimg.c:109
Testimg * testimg_to_log(Testimg *ti)
Definition testimg.c:97
Testimg * testimg_gen_all_white(const int width, const int height)
Definition testimg.c:151
Testimg * testimg_gen_three_color_space(const int width)
Definition testimg.c:186
Testimg * testimg_gen_grey_space(const int width)
Definition testimg.c:158
void testimg_print_by_chan(const Testimg *const ti)
Definition testimg.c:67
Testimg * testimg_gen_grey_with_rgb_clipping(const int width)
Definition testimg.c:270
Testimg * testimg_alloc(const int width, const int height)
Definition testimg.c:35
void testimg_print_by_pixel(const Testimg *const ti)
Definition testimg.c:77
Testimg * testimg_gen_grey_max_dr()
Definition testimg.c:223
Testimg * testimg_gen_rgb_space(const int width)
Definition testimg.c:200
Testimg * testimg_to_exp(Testimg *ti)
Definition testimg.c:114
float testimg_val_to_exp(const float val)
Definition testimg.c:126
Testimg * testimg_gen_single_color_space(const int width, const int color_index)
Definition testimg.c:172
Testimg * testimg_gen_all_grey(const int width, const int height, const float value)
Definition testimg.c:131
#define for_testimg_pixels_p_yx(ti)
Definition testimg.h:71
float * get_pixel(const Testimg *const ti, const int x, const int y)
Definition testimg.h:59
#define for_testimg_pixels_p_xy(ti)
Definition testimg.h:66
#define TESTIMG_STD_DYN_RANGE_EV
Definition testimg.h:41
#define TR_DEBUG(msg,...)
Definition tracing.h:53