CR2RE Pipeline Reference Manual 1.6.7
hdrl_frameiter-test.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2016 European Southern Observatory
4 *
5 * This program 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 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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 this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include "hdrl_frameiter.h"
29
30#include <cpl.h>
31
32#ifndef ARRAY_LEN
33#define ARRAY_LEN(a) sizeof((a))/sizeof((a)[0])
34#endif
35
36/*----------------------------------------------------------------------------*/
40/*----------------------------------------------------------------------------*/
41
42static void check_strides(cpl_frameset * frames, intptr_t naxes,
43 intptr_t offsets[], intptr_t strides[],
44 intptr_t dims[], intptr_t values[], intptr_t len,
45 int swap)
46{
47 hdrl_iter * it;
48 char * soffset = cpl_sprintf("%s", "(");
49 char * sstride = cpl_sprintf("%s", "(");
50 char * sdim = cpl_sprintf("%s", "(");
51 for (intptr_t i = 0; i < naxes; i++) {
52 char * t = cpl_sprintf("%s%zd%s", soffset, offsets[i],
53 i == naxes - 1 ? ")" : ",");
54 cpl_free(soffset);
55 soffset = t;
56 t = cpl_sprintf("%s%zd%s", sstride, strides ? strides[i] : 0,
57 i == naxes - 1 ? ")" : ",");
58 cpl_free(sstride);
59 sstride = t;
60 t = cpl_sprintf("%s%zd%s", sdim, dims ? dims[i] : -1,
61 i == naxes - 1 ? ")" : ",");
62 cpl_free(sdim);
63 sdim = t;
64 }
65 cpl_msg_info(cpl_func, "testing offset %s, stride %s, dim %s, len %zd, "
66 "swap %d", soffset, sstride, sdim, len, swap);
67 cpl_free(soffset);
68 cpl_free(sstride);
69 cpl_free(sdim);
70
71 intptr_t axes[] = {HDRL_FRAMEITER_AXIS_FRAME, HDRL_FRAMEITER_AXIS_EXT};
72 if (swap) {
73 intptr_t tmp;
74 tmp = axes[0];
75 axes[0] = axes[1];
76 axes[1] = tmp;
77 }
78 it = hdrl_frameiter_new(frames, 0, naxes, axes, offsets, strides, dims);
79 cpl_test_eq(hdrl_iter_length(it), len);
80 int d;
81 int cnt = 0;
82 for (hdrl_frameiter_data * h = hdrl_iter_next(it); h != NULL; h = hdrl_iter_next(it)) {
83 cpl_test_eq(cpl_image_get(h->image, 1, 1, &d), values[cnt]);
84 cpl_image_delete(h->image);
85 cpl_propertylist_delete(h->plist);
86 cnt++;
87 }
88 cpl_test_eq(hdrl_iter_length(it), cnt);
89 hdrl_iter_delete(it);
90}
91
92static void test_basic(void)
93{
94 size_t nframes = 5;
95 size_t next = 4;
96 cpl_frameset * frames = cpl_frameset_new();
97 for (size_t i = 0; i < nframes; i++) {
98 char * fn = cpl_sprintf("hdrl_frameiter-test_%zu_%zd.fits", i, (intptr_t)getpid());
99 cpl_propertylist * plist = cpl_propertylist_new();
100 cpl_propertylist_update_string(plist, "TAG", fn);
101 cpl_propertylist_save(plist, fn, CPL_IO_CREATE);
102 cpl_propertylist_delete(plist);
103 for (size_t j = 1; j < next + 1; j++) {
104 cpl_image * img = cpl_image_new(50, 70, CPL_TYPE_INT);
105 cpl_image_add_scalar(img, i * next + j);
106 cpl_image_save(img, fn, CPL_TYPE_INT, NULL, CPL_IO_EXTEND);
107 cpl_image_delete(img);
108 }
109 cpl_frame * frm = cpl_frame_new();
110 cpl_frame_set_filename(frm, fn);
111 cpl_frame_set_tag(frm, "RAW");
112 cpl_frameset_insert(frames, frm);
113 cpl_free(fn);
114 }
115
116 /* empty iterator test */
117 {
118 cpl_frameset * empty = cpl_frameset_new();
119 hdrl_iter * it =
120 hdrl_frameiter_new(empty, 0, 1, (intptr_t[]){HDRL_FRAMEITER_AXIS_EXT},
121 NULL, NULL, NULL);
122 cpl_test_eq(hdrl_iter_length(it), 0);
123 cpl_test_null(hdrl_iter_next(it));
124 cpl_test_null(hdrl_iter_next(it));
125 cpl_test_null(hdrl_iter_next(it));
126 cpl_frameset_delete(empty);
127 hdrl_iter_delete(it);
128 }
129 /* one iteration axis */
130 {
131 intptr_t values[next];
132 for (size_t i = 0; i < ARRAY_LEN(values); i++) {
133 values[i] = i + 1;
134 }
135 intptr_t offsets[] = {1};
136 check_strides(frames, 1, offsets, NULL, NULL, values, ARRAY_LEN(values), 1);
137 }
138 /* two iteration axis basic */
139 {
140 intptr_t values[nframes * next];
141 for (size_t i = 0; i < ARRAY_LEN(values); i++) {
142 values[i] = i + 1;
143 }
144 intptr_t offsets[] = {0, 1};
145 intptr_t strides[] = {1, 1};
146 check_strides(frames, 2, offsets, strides, NULL, values,
147 ARRAY_LEN(values), 0);
148 }
149 /* two iteration axis offset (1,1) */
150 {
151 intptr_t values[(nframes - 1) * next];
152 for (size_t i = 0; i < ARRAY_LEN(values); i++) {
153 values[i] = i + 1 + next;
154 }
155 intptr_t offsets[] = {1, 1};
156 intptr_t strides[] = {1, 1};
157 check_strides(frames, 2, offsets, strides, NULL, values,
158 ARRAY_LEN(values), 0);
159 }
160 /* two iteration axis stride (1,2) */
161 {
162 intptr_t values[nframes * (next / 2 + next % 2)];
163 intptr_t c = 0;
164 for (size_t i = 0; i < nframes; i++) {
165 for (size_t j = 1; j < next + 1; j+=2) {
166 values[c++] = i * next + j;
167 }
168 }
169 cpl_test_eq(ARRAY_LEN(values), c);
170 intptr_t offsets[] = {0, 1};
171 intptr_t strides[] = {1, 2};
172 check_strides(frames, 2, offsets, strides, NULL, values,
173 ARRAY_LEN(values), 0);
174 }
175 /* two iteration axis stride (2,1) */
176 {
177 intptr_t values[(nframes / 2 + nframes % 2) * (next)];
178 intptr_t c = 0;
179 for (size_t i = 0; i < nframes; i+=2) {
180 for (size_t j = 1; j < next + 1; j++) {
181 values[c++] = i * next + j;
182 }
183 }
184 cpl_test_eq(ARRAY_LEN(values), c);
185 intptr_t offsets[] = {0, 1};
186 intptr_t strides[] = {2, 1};
187 check_strides(frames, 2, offsets, strides, NULL, values,
188 ARRAY_LEN(values), 0);
189 }
190 /* two iteration axis stride (2,2) */
191 {
192 intptr_t values[(nframes / 2 + nframes % 2) * (next / 2 + next % 2)];
193 intptr_t c = 0;
194 for (size_t i = 0; i < nframes; i+=2) {
195 for (size_t j = 1; j < next + 1; j+=2) {
196 values[c++] = i * next + j;
197 }
198 }
199 cpl_test_eq(ARRAY_LEN(values), c);
200 intptr_t offsets[] = {0, 1};
201 intptr_t strides[] = {2, 2};
202 check_strides(frames, 2, offsets, strides, NULL, values,
203 ARRAY_LEN(values), 0);
204 }
205 /* stride 1,0 */
206 {
207 intptr_t values[nframes * next];
208 for (size_t i = 0; i < ARRAY_LEN(values); i++) {
209 values[i] = (i / next) * next + 1;
210 }
211 intptr_t offsets[] = {0, 1};
212 intptr_t strides[] = {1, 0};
213 check_strides(frames, 2, offsets, strides, NULL, values,
214 ARRAY_LEN(values), 0);
215 }
216 /* stride 0,1 */
217 {
218 intptr_t values[nframes * next];
219 for (size_t i = 0; i < ARRAY_LEN(values); i++) {
220 values[i] = (i % next) + 1;
221 }
222 intptr_t offsets[] = {0, 1};
223 intptr_t strides[] = {0, 1};
224 check_strides(frames, 2, offsets, strides, NULL, values,
225 ARRAY_LEN(values), 0);
226 }
227 /* stride 0,0 */
228 {
229 intptr_t values[nframes * next];
230 for (size_t i = 0; i < ARRAY_LEN(values); i++) {
231 values[i] = 1;
232 }
233 intptr_t offsets[] = {0, 1};
234 intptr_t strides[] = {0, 0};
235 check_strides(frames, 2, offsets, strides, NULL, values,
236 ARRAY_LEN(values), 0);
237 }
238 /* swapped axes */
239 {
240 intptr_t values[nframes * next];
241 intptr_t c = 0;
242 for (size_t j = 1; j < next + 1; j++) {
243 for (size_t i = 0; i < nframes; i++) {
244 values[c++] = i * next + j;
245 }
246 }
247 cpl_test_eq(ARRAY_LEN(values), c);
248 intptr_t offsets[] = {1, 0};
249 check_strides(frames, 2, offsets, NULL, NULL, values,
250 ARRAY_LEN(values), 1);
251 }
252 /* dim -1, -1 */
253 {
254 intptr_t values[nframes * next];
255 cpl_msg_debug(cpl_func,"The related valgrind error present if compiled"
256 " with O3 optimisation is most probably a false "
257 "positive - adding this message suppresses the error.");
258 for (size_t i = 0; i < ARRAY_LEN(values); i++) {
259 values[i] = i + 1;
260 }
261 intptr_t offsets[] = {0, 1};
262 intptr_t dims[] = {-1, -1};
263 check_strides(frames, 2, offsets, NULL, dims, values,
264 ARRAY_LEN(values), 0);
265 }
266 /* dim -1, x*/
267 {
268 intptr_t values[nframes * 3];
269 intptr_t c = 0;
270 for (size_t i = 0; i < nframes; i++) {
271 for (size_t j = 1; j < 3 + 1; j++) {
272 values[c++] = i * next + j;
273 }
274 }
275 cpl_test_eq(ARRAY_LEN(values), c);
276 intptr_t offsets[] = {0, 1};
277 intptr_t dims[] = {-1, 3};
278 check_strides(frames, 2, offsets, NULL, dims, values,
279 ARRAY_LEN(values), 0);
280 }
281 /* dim x,-1*/
282 {
283 intptr_t values[4 * next];
284 intptr_t c = 0;
285 for (size_t i = 0; i < 4; i++) {
286 for (size_t j = 1; j < next + 1; j++) {
287 values[c++] = i * next + j;
288 }
289 }
290 cpl_test_eq(ARRAY_LEN(values), c);
291 intptr_t offsets[] = {0, 1};
292 intptr_t dims[] = {4, -1};
293 check_strides(frames, 2, offsets, NULL, dims, values,
294 ARRAY_LEN(values), 0);
295 }
296
297 cpl_frameset_delete(frames);
298 cpl_test_zero(system("rm -f hdrl_frameiter-test_*fits"));
299}
300
301/*----------------------------------------------------------------------------*/
305/*----------------------------------------------------------------------------*/
306int main(void)
307{
308 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
309
310 test_basic();
311
312 return cpl_test_end(0);
313}
hdrl_iter * hdrl_frameiter_new(const cpl_frameset *frames, hdrl_iter_flags flags, intptr_t naxes, intptr_t *axes, intptr_t *offsets, intptr_t *strides, intptr_t *dims)
create iterator over cpl_frameset