CR2RE Pipeline Reference Manual 1.6.8
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/*----------------------------------------------------------------------------*/
45/*----------------------------------------------------------------------------*/
46
47void test_create(void)
48{
49
50 /* create data initial data */
51 cpl_imagelist * data = cpl_imagelist_new();
52 cpl_imagelist * errs = cpl_imagelist_new();
53 cpl_image * img = cpl_image_new(64, 64, CPL_TYPE_DOUBLE);
54 cpl_image * err = cpl_image_new(64, 64, CPL_TYPE_DOUBLE);
55 cpl_image_add_scalar(img, 1. );
56 cpl_image_add_scalar(err, 0.05);
57
58 /* create expected results (err / sqrt(nz) for mean) */
59 size_t n = 5;
60 for (size_t i = 0; i < n; i++) {
61 cpl_imagelist_set(data, cpl_image_duplicate(img), cpl_imagelist_get_size(data));
62 cpl_imagelist_set(errs, cpl_image_duplicate(err), cpl_imagelist_get_size(errs));
63 }
64
65 /* new hdrl_imagelist */
66 hdrl_imagelist * hl = hdrl_imagelist_create(data, errs);
67 cpl_test_eq(hdrl_imagelist_get_size(hl), n);
68 cpl_test_error(CPL_ERROR_NONE);
69
70 /* Clean up */
71 cpl_image_delete(img);
72 cpl_image_delete(err);
73 cpl_imagelist_delete(data);
74 cpl_imagelist_delete(errs);
76}
77
78void test_get(void)
79{
80 hdrl_imagelist * hl = NULL;
81 cpl_test_eq(hdrl_imagelist_get_size_x(hl), -1);
82 cpl_test_error(CPL_ERROR_NULL_INPUT);
83 cpl_test_eq(hdrl_imagelist_get_size_y(hl), -1);
84 cpl_test_error(CPL_ERROR_NULL_INPUT);
85 cpl_test_eq(hdrl_imagelist_get_size(hl), -1);
86 cpl_test_error(CPL_ERROR_NULL_INPUT);
87
88 hl = hdrl_imagelist_new();
89 cpl_test_eq(hdrl_imagelist_get_size(hl), 0);
90 cpl_test_error(CPL_ERROR_NONE);
91 cpl_test_eq(hdrl_imagelist_get_size_x(hl), -1);
92 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
93 cpl_test_eq(hdrl_imagelist_get_size_y(hl), -1);
94 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
95
97 cpl_test_eq(hdrl_imagelist_get_size(hl), 1);
98 cpl_test_error(CPL_ERROR_NONE);
99 cpl_test_eq(hdrl_imagelist_get_size_x(hl), 5);
100 cpl_test_error(CPL_ERROR_NONE);
101 cpl_test_eq(hdrl_imagelist_get_size_y(hl), 6);
102 cpl_test_error(CPL_ERROR_NONE);
103
105}
106
107cpl_error_code test_interface(void)
108{
109 hdrl_imagelist * hl = hdrl_imagelist_new();
110 hdrl_imagelist * h;
111 int c = 0;
112 hdrl_iter * it = hdrl_imagelist_get_iter_row_slices(hl, 1, 0,
113 HDRL_ITER_OWNS_DATA);
114 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
115 cpl_test_null(it);
116
117 it = hdrl_imagelist_get_iter_row_slices(NULL, 1, 0, HDRL_ITER_OWNS_DATA);
118 cpl_test_error(CPL_ERROR_NULL_INPUT);
119 cpl_test_null(it);
120
122 it = hdrl_imagelist_get_iter_row_slices(hl, 0, 0, HDRL_ITER_OWNS_DATA);
123 cpl_test_error(CPL_ERROR_NONE);
124 cpl_test_nonnull(it);
125 for (h = hdrl_iter_next(it); h != NULL; h = hdrl_iter_next(it)) {
126 c++;
127 }
128 cpl_test_eq(c, 5);
129 hdrl_iter_delete(it);
130
132 it = hdrl_imagelist_get_iter_row_slices(hl, 3, 0, 0);
133 cpl_test_error(CPL_ERROR_NONE);
134 cpl_test_nonnull(it);
135 c = 0;
136 for (h = hdrl_iter_next(it); h != NULL; h = hdrl_iter_next(it)) {
137 c++;
139 }
140 cpl_test_eq(c, 2);
141 hdrl_iter_delete(it);
142
144 it = hdrl_imagelist_get_iter_row_slices(hl, 3000, 0, HDRL_ITER_OWNS_DATA);
145 cpl_test_error(CPL_ERROR_NONE);
146 cpl_test_nonnull(it);
147 c = 0;
148 for (h = hdrl_iter_next(it); h != NULL; h = hdrl_iter_next(it)) {
149 c++;
150 }
151 cpl_test_eq(c, 1);
152 hdrl_iter_delete(it);
153
155
156 return cpl_error_get_code();
157}
158
159
160cpl_error_code test_iter(void)
161{
162 const cpl_size nx = 500;
163 const cpl_size ny = 571;
164 const cpl_size nz = 121;
165 const cpl_size yslice = 23;
166 hdrl_imagelist * hl = hdrl_imagelist_new();
167 for (cpl_size i = 0; i < nz; i++) {
168 hdrl_image * img = hdrl_image_new(nx, ny);
169 hdrl_image_add_scalar(img, (hdrl_value){i, i});
170 hdrl_image_reject(img, 1, 5);
171 if ((i % 5) == 0) {
172 hdrl_image_reject(img, 2, 5);
173 }
174 hdrl_imagelist_set(hl, img, i);
175 }
176
177 for (cpl_size y = 1; y < ny + 1; y++) {
178
179 hdrl_imagelist * lview = hdrl_imagelist_row_view(hl, y, y);
180
181 hdrl_imagelist_add_scalar(lview, (hdrl_value){y, y});
182 hdrl_imagelist_sub_scalar(lview, (hdrl_value){y, y});
183
184 int isConsistent = hdrl_imagelist_is_consistent(lview);
185 cpl_msg_debug(cpl_func, "Imagelist consistent? %d", isConsistent);
186
187 /* Show the structure, the first time */
188 if(y == 1){
189
190 hdrl_imagelist_dump_structure(lview, stdout);
191
192 cpl_size llx = 1;
193 cpl_size lly = 1;
194 cpl_size urx = y;
195 cpl_size ury = y;
196 hdrl_imagelist_dump_window(lview, llx, lly, urx, ury, stdout);
197 }
198
200 }
201
202 cpl_size ysum = 1;
203 hdrl_image * mean;
204 cpl_image * contrib;
205 hdrl_imagelist_collapse_mean(hl, &mean, &contrib);
206 hdrl_image * slicemean = hdrl_image_new(nx, ny);
207 cpl_image * slicecontrib = cpl_image_new(nx, ny, CPL_TYPE_INT);
208
209 hdrl_iter * it = hdrl_imagelist_get_iter_row_slices(hl, yslice, 0,
210 HDRL_ITER_OWNS_DATA);
211 for (hdrl_imagelist * h = hdrl_iter_next(it); h != NULL;
212 h = hdrl_iter_next(it)) {
213 cpl_test_eq(hdrl_imagelist_get_size(h), nz);
214 hdrl_image * img = hdrl_imagelist_get(h, 0);
215 cpl_test_eq(hdrl_image_get_size_x(img), nx);
216 cpl_test_leq(hdrl_image_get_size_y(img), yslice);
217 hdrl_image * m;
218 cpl_image * c;
220 hdrl_image_copy(slicemean, m, 1, ysum);
221 cpl_image_copy(slicecontrib, c, 1, ysum);
223 cpl_image_delete(c);
224 ysum += hdrl_image_get_size_y(img);
225 }
226
227 cpl_test_eq(ysum - 1, ny);
228 hdrl_test_image_abs(slicemean, mean, nx * ny * DBL_EPSILON);
229 cpl_test_image_abs(slicecontrib, contrib, 0);
230 hdrl_iter_delete(it);
231
232 /* test overlap iterator */
233 ysum = 1;
234 cpl_size overlap = 5;
235 cpl_image_multiply_scalar(slicecontrib, 0);
236 hdrl_image_mul_scalar(slicemean, (hdrl_value){0., 0.});
237 it = hdrl_imagelist_get_iter_row_slices(hl, yslice, overlap,
238 HDRL_ITER_OWNS_DATA);
239 for (hdrl_imagelist * h = hdrl_iter_next(it); h != NULL;
240 h = hdrl_iter_next(it)) {
241 cpl_test_eq(hdrl_imagelist_get_size(h), nz);
242 hdrl_image * img = hdrl_imagelist_get(h, 0);
243 cpl_test_eq(hdrl_image_get_size_x(img), nx);
244
245 if (ysum == 1) {
246 cpl_test_leq(hdrl_image_get_size_y(img), yslice + overlap);
247 } else {
248 cpl_test_leq(hdrl_image_get_size_y(img), yslice + overlap * 2);
249 }
250
251 hdrl_image * m;
252 cpl_image * c;
254 hdrl_il_rowsliceiter_data pdata =
255 hdrl_imagelist_iter_row_slices_get_data(it);
256
257 hdrl_image * rm = hdrl_image_extract(m, 1, pdata.ly, nx, pdata.uy);
258 cpl_image * rc = cpl_image_extract(c, 1, pdata.ly, nx, pdata.uy);
260
261 hdrl_image_copy(slicemean, rm, 1, ysum);
262 cpl_image_copy(slicecontrib, rc, 1, ysum);
263 ysum += hdrl_image_get_size_y(rm);
265 cpl_image_delete(rc);
266 cpl_image_delete(c);
267 }
268
269 cpl_test_eq(ysum - 1, ny);
270 hdrl_test_image_abs(slicemean, mean, nx * ny * DBL_EPSILON);
271 cpl_test_image_abs(slicecontrib, contrib, 0);
272
273 hdrl_iter_delete(it);
275 hdrl_image_delete(slicemean);
276 cpl_image_delete(slicecontrib);
277 hdrl_image_delete(mean);
278 cpl_image_delete(contrib);
279
280 return cpl_error_get_code();
281}
282
283/*----------------------------------------------------------------------------*/
287/*----------------------------------------------------------------------------*/
288int main(void)
289{
290 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
291
292 test_create();
293 test_get();
294 test_interface();
295 test_iter();
296
297 return cpl_test_end(0);
298}
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.