CR2RE Pipeline Reference Manual 1.7.0
hdrl_imagelist_io-test.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2013,2014 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_imagelist.h"
29#include "hdrl_utils.h"
30#include "hdrl_image.h"
31#include "hdrl_test.h"
32
33
34#include <cpl.h>
35
36#include <stdlib.h>
37#include <stdio.h>
38#include <math.h>
39
40/*----------------------------------------------------------------------------*/
44/*----------------------------------------------------------------------------*/
45
46void test_create(void)
47{
48
49 /* create data initial data */
50 cpl_imagelist * data = cpl_imagelist_new();
51 cpl_imagelist * errs = cpl_imagelist_new();
52 cpl_image * img = cpl_image_new(64, 64, CPL_TYPE_DOUBLE);
53 cpl_image * err = cpl_image_new(64, 64, CPL_TYPE_DOUBLE);
54 cpl_image_add_scalar(img, 1. );
55 cpl_image_add_scalar(err, 0.05);
56
57 /* create expected results (err / sqrt(nz) for mean) */
58 size_t n = 5;
59 for (size_t i = 0; i < n; i++) {
60 cpl_imagelist_set(data, cpl_image_duplicate(img), cpl_imagelist_get_size(data));
61 cpl_imagelist_set(errs, cpl_image_duplicate(err), cpl_imagelist_get_size(errs));
62 }
63
64 /* new hdrl_imagelist */
65 hdrl_imagelist * hl = hdrl_imagelist_create(data, errs);
66 cpl_test_eq(hdrl_imagelist_get_size(hl), n);
67 cpl_test_error(CPL_ERROR_NONE);
68
69 /* Clean up */
70 cpl_image_delete(img);
71 cpl_image_delete(err);
72 cpl_imagelist_delete(data);
73 cpl_imagelist_delete(errs);
75}
76
77void test_get(void)
78{
79 hdrl_imagelist * hl = NULL;
80 cpl_test_eq(hdrl_imagelist_get_size_x(hl), -1);
81 cpl_test_error(CPL_ERROR_NULL_INPUT);
82 cpl_test_eq(hdrl_imagelist_get_size_y(hl), -1);
83 cpl_test_error(CPL_ERROR_NULL_INPUT);
84 cpl_test_eq(hdrl_imagelist_get_size(hl), -1);
85 cpl_test_error(CPL_ERROR_NULL_INPUT);
86
87 hl = hdrl_imagelist_new();
88 cpl_test_eq(hdrl_imagelist_get_size(hl), 0);
89 cpl_test_error(CPL_ERROR_NONE);
90 cpl_test_eq(hdrl_imagelist_get_size_x(hl), -1);
91 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
92 cpl_test_eq(hdrl_imagelist_get_size_y(hl), -1);
93 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
94
96 cpl_test_eq(hdrl_imagelist_get_size(hl), 1);
97 cpl_test_error(CPL_ERROR_NONE);
98 cpl_test_eq(hdrl_imagelist_get_size_x(hl), 5);
99 cpl_test_error(CPL_ERROR_NONE);
100 cpl_test_eq(hdrl_imagelist_get_size_y(hl), 6);
101 cpl_test_error(CPL_ERROR_NONE);
102
104}
105
106cpl_error_code test_interface(void)
107{
108 hdrl_imagelist * hl = hdrl_imagelist_new();
109 hdrl_imagelist * h;
110 int c = 0;
111 hdrl_iter * it = hdrl_imagelist_get_iter_row_slices(hl, 1, 0,
112 HDRL_ITER_OWNS_DATA);
113 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
114 cpl_test_null(it);
115
116 it = hdrl_imagelist_get_iter_row_slices(NULL, 1, 0, HDRL_ITER_OWNS_DATA);
117 cpl_test_error(CPL_ERROR_NULL_INPUT);
118 cpl_test_null(it);
119
121 it = hdrl_imagelist_get_iter_row_slices(hl, 0, 0, HDRL_ITER_OWNS_DATA);
122 cpl_test_error(CPL_ERROR_NONE);
123 cpl_test_nonnull(it);
124 for (h = hdrl_iter_next(it); h != NULL; h = hdrl_iter_next(it)) {
125 c++;
126 }
127 cpl_test_eq(c, 5);
128 hdrl_iter_delete(it);
129
131 it = hdrl_imagelist_get_iter_row_slices(hl, 3, 0, 0);
132 cpl_test_error(CPL_ERROR_NONE);
133 cpl_test_nonnull(it);
134 c = 0;
135 for (h = hdrl_iter_next(it); h != NULL; h = hdrl_iter_next(it)) {
136 c++;
138 }
139 cpl_test_eq(c, 2);
140 hdrl_iter_delete(it);
141
143 it = hdrl_imagelist_get_iter_row_slices(hl, 3000, 0, HDRL_ITER_OWNS_DATA);
144 cpl_test_error(CPL_ERROR_NONE);
145 cpl_test_nonnull(it);
146 c = 0;
147 for (h = hdrl_iter_next(it); h != NULL; h = hdrl_iter_next(it)) {
148 c++;
149 }
150 cpl_test_eq(c, 1);
151 hdrl_iter_delete(it);
152
154
155 return cpl_error_get_code();
156}
157
158
159cpl_error_code test_iter(void)
160{
161 const cpl_size nx = 500;
162 const cpl_size ny = 571;
163 const cpl_size nz = 121;
164 const cpl_size yslice = 23;
165 hdrl_imagelist * hl = hdrl_imagelist_new();
166 for (cpl_size i = 0; i < nz; i++) {
167 hdrl_image * img = hdrl_image_new(nx, ny);
168 hdrl_image_add_scalar(img, (hdrl_value){i, i});
169 hdrl_image_reject(img, 1, 5);
170 if ((i % 5) == 0) {
171 hdrl_image_reject(img, 2, 5);
172 }
173 hdrl_imagelist_set(hl, img, i);
174 }
175
176 for (cpl_size y = 1; y < ny + 1; y++) {
177
178 hdrl_imagelist * lview = hdrl_imagelist_row_view(hl, y, y);
179
180 hdrl_imagelist_add_scalar(lview, (hdrl_value){y, y});
181 hdrl_imagelist_sub_scalar(lview, (hdrl_value){y, y});
182
183 int isConsistent = hdrl_imagelist_is_consistent(lview);
184 cpl_msg_debug(cpl_func, "Imagelist consistent? %d", isConsistent);
185
186 /* Show the structure, the first time */
187 if(y == 1){
188
189 hdrl_imagelist_dump_structure(lview, stdout);
190
191 cpl_size llx = 1;
192 cpl_size lly = 1;
193 cpl_size urx = y;
194 cpl_size ury = y;
195 hdrl_imagelist_dump_window(lview, llx, lly, urx, ury, stdout);
196 }
197
199 }
200
201 cpl_size ysum = 1;
202 hdrl_image * mean;
203 cpl_image * contrib;
204 hdrl_imagelist_collapse_mean(hl, &mean, &contrib);
205 hdrl_image * slicemean = hdrl_image_new(nx, ny);
206 cpl_image * slicecontrib = cpl_image_new(nx, ny, CPL_TYPE_INT);
207
208 hdrl_iter * it = hdrl_imagelist_get_iter_row_slices(hl, yslice, 0,
209 HDRL_ITER_OWNS_DATA);
210 for (hdrl_imagelist * h = hdrl_iter_next(it); h != NULL;
211 h = hdrl_iter_next(it)) {
212 cpl_test_eq(hdrl_imagelist_get_size(h), nz);
213 hdrl_image * img = hdrl_imagelist_get(h, 0);
214 cpl_test_eq(hdrl_image_get_size_x(img), nx);
215 cpl_test_leq(hdrl_image_get_size_y(img), yslice);
216 hdrl_image * m;
217 cpl_image * c;
219 hdrl_image_copy(slicemean, m, 1, ysum);
220 cpl_image_copy(slicecontrib, c, 1, ysum);
222 cpl_image_delete(c);
223 ysum += hdrl_image_get_size_y(img);
224 }
225
226 cpl_test_eq(ysum - 1, ny);
227 hdrl_test_image_abs(slicemean, mean, nx * ny * DBL_EPSILON);
228 cpl_test_image_abs(slicecontrib, contrib, 0);
229 hdrl_iter_delete(it);
230
231 /* test overlap iterator */
232 ysum = 1;
233 cpl_size overlap = 5;
234 cpl_image_multiply_scalar(slicecontrib, 0);
235 hdrl_image_mul_scalar(slicemean, (hdrl_value){0., 0.});
236 it = hdrl_imagelist_get_iter_row_slices(hl, yslice, overlap,
237 HDRL_ITER_OWNS_DATA);
238 for (hdrl_imagelist * h = hdrl_iter_next(it); h != NULL;
239 h = hdrl_iter_next(it)) {
240 cpl_test_eq(hdrl_imagelist_get_size(h), nz);
241 hdrl_image * img = hdrl_imagelist_get(h, 0);
242 cpl_test_eq(hdrl_image_get_size_x(img), nx);
243
244 if (ysum == 1) {
245 cpl_test_leq(hdrl_image_get_size_y(img), yslice + overlap);
246 } else {
247 cpl_test_leq(hdrl_image_get_size_y(img), yslice + overlap * 2);
248 }
249
250 hdrl_image * m;
251 cpl_image * c;
253 hdrl_il_rowsliceiter_data pdata =
254 hdrl_imagelist_iter_row_slices_get_data(it);
255
256 hdrl_image * rm = hdrl_image_extract(m, 1, pdata.ly, nx, pdata.uy);
257 cpl_image * rc = cpl_image_extract(c, 1, pdata.ly, nx, pdata.uy);
259
260 hdrl_image_copy(slicemean, rm, 1, ysum);
261 cpl_image_copy(slicecontrib, rc, 1, ysum);
262 ysum += hdrl_image_get_size_y(rm);
264 cpl_image_delete(rc);
265 cpl_image_delete(c);
266 }
267
268 cpl_test_eq(ysum - 1, ny);
269 hdrl_test_image_abs(slicemean, mean, nx * ny * DBL_EPSILON);
270 cpl_test_image_abs(slicecontrib, contrib, 0);
271
272 hdrl_iter_delete(it);
274 hdrl_image_delete(slicemean);
275 cpl_image_delete(slicecontrib);
276 hdrl_image_delete(mean);
277 cpl_image_delete(contrib);
278
279 return cpl_error_get_code();
280}
281
282/*----------------------------------------------------------------------------*/
286/*----------------------------------------------------------------------------*/
287int main(void)
288{
289 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
290
291 test_create();
292 test_get();
293 test_interface();
294 test_iter();
295
296 return cpl_test_end(0);
297}
cpl_error_code hdrl_image_mul_scalar(hdrl_image *self, hdrl_value value)
Elementwise multiplication of an image with a scalar.
cpl_error_code hdrl_image_copy(hdrl_image *dst, const hdrl_image *src, cpl_size xpos, cpl_size ypos)
Copy one image into another.
Definition hdrl_image.c:686
hdrl_image * hdrl_image_extract(const hdrl_image *self, cpl_size llx, cpl_size lly, cpl_size urx, cpl_size ury)
extract copy of window from image
Definition hdrl_image.c:625
cpl_error_code hdrl_image_add_scalar(hdrl_image *self, hdrl_value value)
Elementwise addition of a scalar to an image.
cpl_size hdrl_image_get_size_y(const hdrl_image *self)
return size of Y dimension of image
Definition hdrl_image.c:540
cpl_size hdrl_image_get_size_x(const hdrl_image *self)
return size of X dimension of image
Definition hdrl_image.c:525
cpl_error_code hdrl_image_reject(hdrl_image *self, cpl_size xpos, cpl_size ypos)
mark pixel as bad
Definition hdrl_image.c:427
hdrl_image * hdrl_image_new(cpl_size nx, cpl_size ny)
create new zero filled hdrl image
Definition hdrl_image.c:311
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition hdrl_image.c:379
cpl_error_code hdrl_imagelist_add_scalar(hdrl_imagelist *himlist, hdrl_value value)
Elementwise addition of a scalar to each image in the himlist.
cpl_error_code hdrl_imagelist_collapse_mean(const hdrl_imagelist *himlist, hdrl_image **out, cpl_image **contrib)
Mean collapsing of image list.
cpl_error_code hdrl_imagelist_set(hdrl_imagelist *himlist, hdrl_image *himg, cpl_size pos)
Insert an image into an imagelist.
cpl_size hdrl_imagelist_get_size_y(const hdrl_imagelist *himlist)
Get number of rows of images in the imagelist.
hdrl_imagelist * hdrl_imagelist_create(cpl_imagelist *imlist, cpl_imagelist *errlist)
Create an hdrl_imagelist out of 2 cpl_imagelist.
void hdrl_imagelist_delete(hdrl_imagelist *himlist)
Free all memory used by a hdrl_imagelist object including the images.
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
int hdrl_imagelist_is_consistent(const hdrl_imagelist *himlist)
Determine if an imagelist contains images of equal size and type.
cpl_error_code hdrl_imagelist_dump_window(const hdrl_imagelist *himlist, cpl_size llx, cpl_size lly, cpl_size urx, cpl_size ury, FILE *stream)
Dump pixel values of images in a imagelist.
cpl_error_code hdrl_imagelist_sub_scalar(hdrl_imagelist *himlist, hdrl_value value)
Elementwise subtraction of a scalar to each image in the himlist.
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
cpl_size hdrl_imagelist_get_size_x(const hdrl_imagelist *himlist)
Get number of colums of images in the imagelist.
cpl_error_code hdrl_imagelist_dump_structure(const hdrl_imagelist *himlist, FILE *stream)
Dump structural information of images in an imagelist.
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.