CR2RE Pipeline Reference Manual 1.6.2
hdrl_imagelist_basic-test.c
1/* $Id: hdrl_imagelist_basic-test.c,v 1.6 2013-10-02 12:49:29 yjung Exp $
2 *
3 * This file is part of the HDRL
4 * Copyright (C) 2013 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * $Author: yjung $
23 * $Date: 2013-10-02 12:49:29 $
24 * $Revision: 1.6 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32/*-----------------------------------------------------------------------------
33 Includes
34 -----------------------------------------------------------------------------*/
35
36#include "hdrl_imagelist.h"
37#include "hdrl_test.h"
38
39#include <cpl.h>
40
41#include <math.h>
42#include <float.h>
43#include <stdlib.h>
44#include <stdio.h>
45
46#ifndef ARRAY_LEN
47#define ARRAY_LEN(a) sizeof((a))/sizeof((a)[0])
48#endif
49#define IMAGESZ 265
50#define IMAGENB 10
51
52/*----------------------------------------------------------------------------*/
57/*----------------------------------------------------------------------------*/
58
59void test_image_basic_operations(void)
60{
61 /* Initialize values */
62 hdrl_value value = {100., 10.};
63 hdrl_value scalar = {1000., 100.};
64 hdrl_value exponent = {2., 1.};
65
66
67 /* Initialize images */
68 hdrl_image *himg1 = hdrl_image_new(IMAGESZ, IMAGESZ);
69 hdrl_image *himg2 = hdrl_image_new(IMAGESZ, IMAGESZ);
70 hdrl_image *himg3 = hdrl_image_new(IMAGESZ, IMAGESZ);
71
72
73 /* Operations with images */
74 hdrl_image_add_scalar(himg1, scalar);
75 hdrl_image_sub_scalar(himg1, value);
76 cpl_test_error(CPL_ERROR_NONE);
77
78
79 /* Initialize imageLists and add images */
80 hdrl_imagelist *himlist1 = hdrl_imagelist_new();
81 hdrl_imagelist_add_image(himlist1, himg1);
82 cpl_test_error(CPL_ERROR_NONE);
83
84 hdrl_imagelist *himlist2 = hdrl_imagelist_new();
85 hdrl_imagelist_add_image(himlist2, himg2);
86 hdrl_imagelist_sub_image(himlist2, himg2);
87 hdrl_imagelist_add_image(himlist2, himg2);
88 cpl_test_error(CPL_ERROR_NONE);
89
90 hdrl_imagelist_add_imagelist(himlist1, himlist2);
91 hdrl_imagelist_sub_imagelist(himlist1, himlist2);
92 hdrl_imagelist_add_imagelist(himlist1, himlist2);
93 cpl_test_error(CPL_ERROR_NONE);
94
95
96 /* Complex operations with imageList: Div, Mul, Pow */
97 hdrl_imagelist_div_scalar(himlist1, scalar);
98 hdrl_imagelist_div_image(himlist1, himg1);
99 hdrl_imagelist_div_imagelist(himlist1, himlist2);
100 cpl_test_error(CPL_ERROR_NONE);
101
102 hdrl_imagelist_mul_scalar(himlist1, scalar);
103 hdrl_imagelist_mul_image(himlist1, himg1);
104 hdrl_imagelist_mul_imagelist(himlist1, himlist2);
105 cpl_test_error(CPL_ERROR_NONE);
106
107 hdrl_imagelist_pow_scalar(himlist1, exponent);
108 cpl_test_error(CPL_ERROR_NONE);
109
110 hdrl_imagelist_add_image(himlist2, himg3);
111 hdrl_imagelist_add_imagelist(himlist1, himlist2);
112 hdrl_imagelist_pow_scalar(himlist1, exponent);
113 cpl_test_error(CPL_ERROR_NONE);
114
115
116 /* Clean up */
117 hdrl_image_delete(himg1);
118 hdrl_image_delete(himg2);
119 hdrl_image_delete(himg3);
120 hdrl_imagelist_delete(himlist1) ;
121 hdrl_imagelist_delete(himlist2) ;
122}
123
124/*----------------------------------------------------------------------------*/
128/*----------------------------------------------------------------------------*/
129int main(void)
130{
131 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
132
133 test_image_basic_operations();
134
135 hdrl_imagelist * himlist ;
136 hdrl_image * himg ;
137 hdrl_image * himg2 ;
138 cpl_image * contrib ;
139 cpl_image * contrib2 ;
140 cpl_size nimages = IMAGENB ;
141
142 /* Create an image list */
143 himlist = hdrl_imagelist_new() ;
144 for (cpl_size i = 0 ; i < nimages ; i++) {
145 /*
146 What about having an
147
148 hdrl_image_load_image(hdrl_image * hdrlimage
149 const char * filename,
150 cpl_type im_type,
151 cpl_size pnum,
152 cpl_size xtnum);
153
154 hdrl_image_load_error(hdrl_image * hdrlimage
155 const char * filename,
156 cpl_type im_type,
157 cpl_size pnum,
158 cpl_size xtnum);
159
160 with the same API like cpl?
161 two functions are better in the case the error is in another fitsfile
162 */
163
164 himg = hdrl_image_new(IMAGESZ, IMAGESZ);
165 if (i == nimages / 2)
166 hdrl_image_add_scalar(himg, (hdrl_value){1000, 100});
167 else
168 hdrl_image_add_scalar(himg, (hdrl_value){i, 1});
169 hdrl_imagelist_set(himlist, himg, i);
170 }
171
172 /* Mean collapse */
173 hdrl_imagelist_collapse_mean(himlist, &himg, &contrib);
174 hdrl_imagelist_collapse(himlist, HDRL_COLLAPSE_MEAN, &himg2, &contrib2);
175 hdrl_test_image_abs(himg, himg2, 0);
176 cpl_test_image_abs(contrib, contrib2, 0);
177 cpl_image_delete(contrib) ;
178 hdrl_image_delete(himg) ;
179 cpl_image_delete(contrib2) ;
180 hdrl_image_delete(himg2) ;
181
182 /* Weighted Mean collapse */
183 hdrl_imagelist_collapse_weighted_mean(himlist, &himg, &contrib);
184 hdrl_imagelist_collapse(himlist, HDRL_COLLAPSE_WEIGHTED_MEAN, &himg2, &contrib2);
185 hdrl_test_image_abs(himg, himg2, 0);
186 cpl_test_image_abs(contrib, contrib2, 0);
187 cpl_image_delete(contrib) ;
188 hdrl_image_delete(himg) ;
189 cpl_image_delete(contrib2) ;
190 hdrl_image_delete(himg2) ;
191
192 /* Median collapse */
193 hdrl_imagelist_collapse_median(himlist, &himg, &contrib);
194 hdrl_imagelist_collapse(himlist, HDRL_COLLAPSE_MEDIAN, &himg2, &contrib2);
195 hdrl_test_image_abs(himg, himg2, 0);
196 cpl_test_image_abs(contrib, contrib2, 0);
197 cpl_image_delete(contrib) ;
198 hdrl_image_delete(himg) ;
199 cpl_image_delete(contrib2) ;
200 hdrl_image_delete(himg2) ;
201
202 /* Sigclip collapse */
203 hdrl_parameter *psc = hdrl_collapse_sigclip_parameter_create(1., 3., 10);
204 hdrl_imagelist_collapse_sigclip(himlist, 1., 3., 10, &himg, &contrib, NULL, NULL);
205 hdrl_imagelist_collapse(himlist, psc, &himg2, &contrib2);
206 hdrl_test_image_abs(himg, himg2, 0);
207 cpl_test_image_abs(contrib, contrib2, 0);
208 cpl_image_delete(contrib);
209 hdrl_image_delete(himg);
210 cpl_image_delete(contrib2);
211 hdrl_image_delete(himg2);
213
214 /* MinMax collapse */
215 hdrl_parameter *pmm = hdrl_collapse_minmax_parameter_create(1., 3.);
216 hdrl_imagelist_collapse_minmax(himlist, 1., 3., &himg, &contrib, NULL, NULL);
217 hdrl_imagelist_collapse(himlist, pmm, &himg2, &contrib2);
218 hdrl_test_image_abs(himg, himg2, 0);
219 cpl_image_delete(contrib) ;
220 hdrl_image_delete(himg) ;
221 cpl_image_delete(contrib2) ;
222 hdrl_image_delete(himg2) ;
224
225 /* Mode collapse */
226 hdrl_parameter *pmode = hdrl_collapse_mode_parameter_create(10., 1., 0., HDRL_MODE_MEDIAN, 0);
227 hdrl_imagelist_collapse_mode(himlist, 10., 1., 0., HDRL_MODE_MEDIAN, 0, &himg, &contrib);
228 hdrl_imagelist_collapse(himlist, pmode, &himg2, &contrib2);
229 hdrl_test_image_abs(himg, himg2, 0);
230 cpl_test_image_abs(contrib, contrib2, 0);
231 cpl_image_delete(contrib) ;
232 hdrl_image_delete(himg) ;
233 cpl_image_delete(contrib2) ;
234 hdrl_image_delete(himg2) ;
236
237
238 /* Unknown collapse mode */
239 typedef hdrl_parameter_empty hdrl_collapse_unknown_parameter;
240 static hdrl_parameter_typeobj hdrl_collapse_unknown_parameter_type = {
241 -1, /* type */
242 (hdrl_alloc *)&cpl_malloc, /* fp_alloc */
243 (hdrl_free *)&cpl_free, /* fp_free */
244 NULL, /* fp_destroy */
245 sizeof(hdrl_collapse_unknown_parameter), /* obj_size */
246 };
247 hdrl_collapse_unknown_parameter *unk = (hdrl_collapse_unknown_parameter *)
248 hdrl_parameter_new(&hdrl_collapse_unknown_parameter_type);
249 hdrl_parameter *punk = (hdrl_parameter *)unk;
250 hdrl_imagelist_collapse(himlist, punk, &himg2, &contrib2);
252
253
254 /* Collapse Direct */
255 cpl_image *rej_low;
256 cpl_image *rej_high;
257
258 /* Sigclip */
259 hdrl_imagelist_collapse_sigclip(NULL, 1.0, 3.0, 10, &himg, &contrib, &rej_low, &rej_high);
260 cpl_test_error(CPL_ERROR_NULL_INPUT);
261
262 hdrl_imagelist_collapse_sigclip(himlist, 1.0, 3.0, 10, &himg, &contrib, NULL, NULL );
263 cpl_test_error(CPL_ERROR_NONE);
264 cpl_image_delete(contrib );
265 hdrl_image_delete(himg);
266
267 hdrl_imagelist_collapse_sigclip(himlist, 1.0, 3.0, 10, &himg, &contrib, &rej_low, NULL );
268 cpl_test_error(CPL_ERROR_NONE);
269 cpl_image_delete(contrib );
270 cpl_image_delete(rej_low );
271 hdrl_image_delete(himg);
272
273 hdrl_imagelist_collapse_sigclip(himlist, 1.0, 3.0, 10, &himg, &contrib, NULL, &rej_high);
274 cpl_test_error(CPL_ERROR_NONE);
275 cpl_image_delete(contrib );
276 cpl_image_delete(rej_high);
277 hdrl_image_delete(himg);
278
279 hdrl_imagelist_collapse_sigclip(himlist, 1.0, 3.0, 10, &himg, &contrib, &rej_low, &rej_high);
280 cpl_test_error(CPL_ERROR_NONE);
281 cpl_image_delete(contrib );
282 cpl_image_delete(rej_low );
283 cpl_image_delete(rej_high);
284 hdrl_image_delete(himg);
285
286 /* MinMax */
287 hdrl_imagelist_collapse_minmax(NULL, 1.0, 3.0, &himg, &contrib, &rej_low, &rej_high);
288 cpl_test_error(CPL_ERROR_NULL_INPUT);
289
290 hdrl_imagelist_collapse_minmax(himlist, 1.0, 3.0, &himg, &contrib, NULL, NULL );
291 cpl_test_error(CPL_ERROR_NONE);
292 cpl_image_delete(contrib );
293 hdrl_image_delete(himg);
294
295 hdrl_imagelist_collapse_minmax(himlist, 1.0, 3.0, &himg, &contrib, &rej_low, NULL );
296 cpl_test_error(CPL_ERROR_NONE);
297 cpl_image_delete(contrib );
298 cpl_image_delete(rej_low );
299 hdrl_image_delete(himg);
300
301 hdrl_imagelist_collapse_minmax(himlist, 1.0, 3.0, &himg, &contrib, NULL, &rej_high);
302 cpl_test_error(CPL_ERROR_NONE);
303 cpl_image_delete(contrib );
304 cpl_image_delete(rej_high);
305 hdrl_image_delete(himg);
306
307 hdrl_imagelist_collapse_minmax(himlist, 1.0, 3.0, &himg, &contrib, &rej_low, &rej_high);
308 cpl_test_error(CPL_ERROR_NONE);
309 cpl_image_delete(contrib );
310 cpl_image_delete(rej_low );
311 cpl_image_delete(rej_high);
312 hdrl_image_delete(himg);
313
314
315
316
317
318
319 /* Mode */
320 hdrl_imagelist_collapse_mode(NULL, 10., 1., 0., HDRL_MODE_MEDIAN, 10, &himg, &contrib);
321 cpl_test_error(CPL_ERROR_NULL_INPUT);
322
323 /* auto determine histogram */
324 hdrl_imagelist_collapse_mode(himlist, 10., 1., 0., HDRL_MODE_MEDIAN, 10, &himg, &contrib);
325 cpl_test_error(CPL_ERROR_NONE);
326 cpl_test_eq(cpl_image_count_rejected(hdrl_image_get_image(himg)), 0);
327 cpl_test_eq(cpl_image_count_rejected(hdrl_image_get_error(himg)), 0);
328 cpl_test_eq(cpl_image_get_sqflux(contrib), (IMAGENB * IMAGENB) * IMAGESZ * IMAGESZ);
329 cpl_image_delete(contrib );
330 hdrl_image_delete(himg);
331
332 /* determine histogram by user */
333 hdrl_imagelist_collapse_mode(himlist, 1., 20., 1., HDRL_MODE_MEDIAN, 10, &himg, &contrib);
334 cpl_test_error(CPL_ERROR_NONE);
335 cpl_test_eq(cpl_image_count_rejected(hdrl_image_get_image(himg)), 0);
336 cpl_test_eq(cpl_image_count_rejected(hdrl_image_get_error(himg)), 0);
337 cpl_test_eq(cpl_image_get_sqflux(contrib), (IMAGENB * IMAGENB) * IMAGESZ * IMAGESZ);
338 cpl_image_delete(contrib );
339 hdrl_image_delete(himg);
340
341 /* data outside histogram -> no error but all pixels rejected */
342 hdrl_imagelist_collapse_mode(himlist, -1000., -100., 0., HDRL_MODE_MEDIAN, 0, &himg, &contrib);
343 cpl_test_error(CPL_ERROR_NONE);
344 cpl_test_eq(cpl_image_count_rejected(hdrl_image_get_image(himg)), IMAGESZ * IMAGESZ);
345 cpl_test_eq(cpl_image_count_rejected(hdrl_image_get_error(himg)), IMAGESZ * IMAGESZ);
346 cpl_test_eq(cpl_image_get_sqflux(contrib), 0.);
347 cpl_image_delete(contrib );
348 hdrl_image_delete(himg);
349
350 /* Clean up */
351 hdrl_imagelist_delete(himlist);
352
353 return cpl_test_end(0);
354}
355
hdrl_parameter * hdrl_collapse_sigclip_parameter_create(double kappa_low, double kappa_high, int niter)
create a parameter object for sigclipped mean
hdrl_parameter * hdrl_collapse_mode_parameter_create(double histo_min, double histo_max, double bin_size, hdrl_mode_type mode_method, cpl_size error_niter)
create a parameter object for the mode
hdrl_parameter * hdrl_collapse_minmax_parameter_create(double nlow, double nhigh)
create a parameter object for min-max rejected mean
cpl_error_code hdrl_image_add_scalar(hdrl_image *self, hdrl_value value)
Elementwise addition of a scalar to an image.
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:131
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:105
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_image_sub_scalar(hdrl_image *self, hdrl_value value)
Elementwise subtraction of a scalar from an image.
cpl_error_code hdrl_imagelist_collapse_sigclip(const hdrl_imagelist *himlist, double kappa_low, double kappa_high, int niter, hdrl_image **out, cpl_image **contrib, cpl_image **reject_low, cpl_image **reject_high)
Sigma-clipped collapsing of image list.
cpl_error_code hdrl_imagelist_div_imagelist(hdrl_imagelist *himlist1, const hdrl_imagelist *himlist2)
Divide two image lists, the first one is replaced by the result.
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.
void hdrl_imagelist_delete(hdrl_imagelist *himlist)
Free all memory used by a hdrl_imagelist object including the images.
cpl_error_code hdrl_imagelist_add_image(hdrl_imagelist *himlist, const hdrl_image *himg)
Add an image to an image list.
cpl_error_code hdrl_imagelist_pow_scalar(hdrl_imagelist *himlist, hdrl_value exponent)
Compute the elementwise power of each image in the himlist.
cpl_error_code hdrl_imagelist_div_scalar(hdrl_imagelist *himlist, hdrl_value value)
Elementwise division by a scalar to each image in the himlist.
cpl_error_code hdrl_imagelist_sub_image(hdrl_imagelist *himlist, const hdrl_image *himg)
Subtract an image from an image list.
cpl_error_code hdrl_imagelist_collapse(const hdrl_imagelist *himlist, const hdrl_parameter *param, hdrl_image **out, cpl_image **contrib)
collapsing of image list
cpl_error_code hdrl_imagelist_mul_scalar(hdrl_imagelist *himlist, hdrl_value value)
Elementwise multiplication of a scalar to each image in the himlist.
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
cpl_error_code hdrl_imagelist_collapse_median(const hdrl_imagelist *himlist, hdrl_image **out, cpl_image **contrib)
Median collapsing of image list.
cpl_error_code hdrl_imagelist_mul_imagelist(hdrl_imagelist *himlist1, const hdrl_imagelist *himlist2)
Multiply two image lists, the first one is replaced by the result.
cpl_error_code hdrl_imagelist_add_imagelist(hdrl_imagelist *himlist1, const hdrl_imagelist *himlist2)
Add two image lists, the first one is replaced by the result.
cpl_error_code hdrl_imagelist_collapse_mode(const hdrl_imagelist *himlist, double histo_min, double histo_max, double bin_size, hdrl_mode_type mode_method, cpl_size error_niter, hdrl_image **out, cpl_image **contrib)
Mode collapsing of image list.
cpl_error_code hdrl_imagelist_sub_imagelist(hdrl_imagelist *himlist1, const hdrl_imagelist *himlist2)
Subtract two image lists, the first one is replaced by the result.
cpl_error_code hdrl_imagelist_collapse_minmax(const hdrl_imagelist *himlist, double nlow, double nhigh, hdrl_image **out, cpl_image **contrib, cpl_image **reject_low, cpl_image **reject_high)
Minmax-clipped collapsing of image list.
cpl_error_code hdrl_imagelist_collapse_weighted_mean(const hdrl_imagelist *himlist, hdrl_image **out, cpl_image **contrib)
Weighted Mean collapsing of image list.
cpl_error_code hdrl_imagelist_mul_image(hdrl_imagelist *himlist, const hdrl_image *himg)
Multiply an image by an image list.
cpl_error_code hdrl_imagelist_div_image(hdrl_imagelist *himlist, const hdrl_image *himg)
Divide an image from an image list.
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter