Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
test_interpolation_resample_window.c
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Aurélien PIERRE.
4
5 Ansel 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 Ansel 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 Ansel. If not, see <http://www.gnu.org/licenses/>.
17*/
18
38
39#include "pixel/interpolation.h"
40
41#include <setjmp.h>
42#include <stdarg.h>
43#include <stddef.h>
44#include <stdint.h>
45#include <stdlib.h>
46#include <string.h>
47
48#include <cmocka.h>
49
50#ifdef _WIN32
51#include "win/main_wrapper.h"
52#endif
53
55static void _fill_identifiable(float *const pixels, const int width, const int height)
56{
57 for(int y = 0; y < height; y++)
58 for(int x = 0; x < width; x++)
59 {
60 float *const px = pixels + ((size_t)y * width + x) * 4;
61 px[0] = (float)x;
62 px[1] = (float)y;
63 px[2] = 1.0f;
64 px[3] = 1.0f;
65 }
66}
67
69static void _expected_pixel(const int x, const int y, const int dx, const int dy,
70 const int in_width, const int in_height, float *const out)
71{
72 int sx = x + dx;
73 int sy = y + dy;
74 if(sx < 0) sx = 0;
75 if(sy < 0) sy = 0;
76 if(sx > in_width - 1) sx = in_width - 1;
77 if(sy > in_height - 1) sy = in_height - 1;
78 out[0] = (float)sx;
79 out[1] = (float)sy;
80}
81
83static void _check_window(const int in_width, const int in_height,
84 const int out_x, const int out_y,
85 const int out_width, const int out_height)
86{
87 /* Sized to the pixel, not rounded up: an over-read has to land outside the allocation. */
88 float *const in = malloc((size_t)in_width * in_height * 4 * sizeof(float));
89 float *const out = malloc((size_t)out_width * out_height * 4 * sizeof(float));
90 assert_non_null(in);
91 assert_non_null(out);
92 _fill_identifiable(in, in_width, in_height);
93
94 const dt_iop_roi_t roi_in = { .x = 0, .y = 0, .width = in_width, .height = in_height, .scale = 1.0f };
95 const dt_iop_roi_t roi_out = { .x = out_x, .y = out_y, .width = out_width, .height = out_height,
96 .scale = 1.0f };
97
99 dt_interpolation_resample(itor, out, &roi_out, in, &roi_in);
100
101 for(int y = 0; y < out_height; y++)
102 for(int x = 0; x < out_width; x++)
103 {
104 float expected[2] = { 0.0f, 0.0f };
105 _expected_pixel(x, y, out_x, out_y, in_width, in_height, expected);
106 const float *const px = out + ((size_t)y * out_width + x) * 4;
107 assert_float_equal(px[0], expected[0], 0.0f);
108 assert_float_equal(px[1], expected[1], 0.0f);
109 }
110
111 free(in);
112 free(out);
113}
114
116static void test_window_contained(void **state)
117{
118 (void)state;
119 _check_window(64, 48, 8, 6, 32, 24);
120 _check_window(64, 48, 0, 0, 64, 48);
121}
122
125{
126 (void)state;
127 _check_window(64, 48, 0, 0, 64, 96);
128}
129
132{
133 (void)state;
134 _check_window(64, 48, 0, 0, 128, 48);
135}
136
139{
140 (void)state;
141 _check_window(64, 48, 0, 0, 48, 64);
142}
143
146{
147 (void)state;
148 _check_window(64, 48, -16, -12, 32, 24);
149 _check_window(64, 48, -16, -12, 128, 96);
150}
151
154{
155 (void)state;
156 _check_window(64, 48, 200, 200, 16, 16);
157 _check_window(64, 48, -200, -200, 16, 16);
158}
159
162{
163 (void)state;
164 _check_window(1, 1, -2, -2, 8, 8);
165}
166
167int main(int argc, char *argv[])
168{
169 const struct CMUnitTest tests[] = {
170 cmocka_unit_test(test_window_contained),
171 cmocka_unit_test(test_window_taller_than_source),
172 cmocka_unit_test(test_window_wider_than_source),
173 cmocka_unit_test(test_window_transposed_source),
174 cmocka_unit_test(test_window_negative_offset),
175 cmocka_unit_test(test_window_entirely_outside),
176 cmocka_unit_test(test_window_single_pixel_source),
177 };
178 return cmocka_run_group_tests(tests, NULL, NULL);
179}
#define assert_float_equal(a, b, epsilon)
Definition assert.h:35
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
static const float x
const dt_colormatrix_t dt_aligned_pixel_t out
dt_iop_roi_t in
Definition geometry.h:117
const struct dt_interpolation * dt_interpolation_new(enum dt_interpolation_type type)
void dt_interpolation_resample(const struct dt_interpolation *itor, float *out, const dt_iop_roi_t *const roi_out, const float *const in, const dt_iop_roi_t *const roi_in)
@ DT_INTERPOLATION_BILINEAR
uint32_t width
Definition mipmap_cache.c:0
uint32_t height
Definition mipmap_cache.c:1
int main()
Definition prova.c:47
const float uint32_t state[4]
Region of interest passed through the pixelpipe.
Definition format.h:49
static void _fill_identifiable(float *const pixels, const int width, const int height)
A pixel whose channels encode its coordinates, so a misplaced copy is identifiable.
static void test_window_transposed_source(void **state)
static void test_window_entirely_outside(void **state)
static void test_window_single_pixel_source(void **state)
static void _expected_pixel(const int x, const int y, const int dx, const int dy, const int in_width, const int in_height, float *const out)
What BORDER_REPLICATE says the output pixel at (x, y) must be.
static void test_window_contained(void **state)
static void test_window_negative_offset(void **state)
static void _check_window(const int in_width, const int in_height, const int out_x, const int out_y, const int out_width, const int out_height)
Resample one window and check every output pixel against the replicate rule.
static void test_window_taller_than_source(void **state)
static void test_window_wider_than_source(void **state)