CR2RE Pipeline Reference Manual 1.6.2
hdrl_imagelist_view.c
1/* $Id: hdrl_imagelist_view.c,v 1.8 2013-10-23 09:13:56 jtaylor 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: jtaylor $
23 * $Date: 2013-10-23 09:13:56 $
24 * $Revision: 1.8 $
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_imagelist_defs.h"
38#include "hdrl_imagelist_view.h"
39#include "hdrl_image_defs.h"
40#include "hdrl_image.h"
41
42#include <cpl.h>
43#include <assert.h>
44#include <string.h>
45
46/*-----------------------------------------------------------------------------
47 Define
48 -----------------------------------------------------------------------------*/
49
50/*-----------------------------------------------------------------------------
51 Static Prototypes
52 -----------------------------------------------------------------------------*/
53
54/* destructor unwrapping data and error */
55static void hdrl_image_view_delete(hdrl_image * view)
56{
57 if (view) {
58 hdrl_image * himg = (hdrl_image*)view;
59 cpl_mask_unwrap(cpl_image_unset_bpm(hdrl_image_get_image(himg)));
60 cpl_mask_unwrap(cpl_image_unset_bpm(hdrl_image_get_error(himg)));
61 cpl_image_unwrap(hdrl_image_get_image(himg));
62 cpl_image_unwrap(hdrl_image_get_error(himg));
63 hdrl_image_unwrap(himg);
64 }
65}
66
67/* destructor unwrapping data and deleting error */
68static void hdrl_image_imgview_delete(hdrl_image * view)
69{
70 if (view) {
71 hdrl_image * himg = (hdrl_image*)view;
72 cpl_mask_unwrap(cpl_image_unset_bpm(hdrl_image_get_image(himg)));
73 cpl_image_unwrap(hdrl_image_get_image(himg));
74 cpl_image_delete(hdrl_image_get_error(himg));
75 hdrl_image_unwrap(himg);
76 }
77}
78
79
82/*-----------------------------------------------------------------------------
83 Function codes
84 -----------------------------------------------------------------------------*/
85
86/* ---------------------------------------------------------------------------*/
95/* ---------------------------------------------------------------------------*/
96static hdrl_image *
97hdrl_image_row_view_create(hdrl_image * himg,
98 cpl_size ly,
99 cpl_size uy)
100{
101 cpl_image * oimg = hdrl_image_get_image(himg);
102 cpl_image * oerr = hdrl_image_get_error(himg);
103 const size_t dsz = cpl_type_get_sizeof(cpl_image_get_type(oimg));
104 const size_t esz = cpl_type_get_sizeof(cpl_image_get_type(oerr));
105 const cpl_size nx = hdrl_image_get_size_x(himg);
106 char * d = cpl_image_get_data(oimg);
107 char * e = cpl_image_get_data(oerr);
108 size_t offset = (ly - 1) * nx;
109 cpl_size nny = uy - ly + 1;
110 cpl_image * img = cpl_image_wrap(nx, nny, cpl_image_get_type(oimg),
111 d + offset * dsz);
112 cpl_image * err = cpl_image_wrap(nx, nny, cpl_image_get_type(oerr),
113 e + offset * esz);
114
115 /* we must create a mask in the original in order for views to work */
116 cpl_mask * omask = hdrl_image_get_mask(himg);
117 cpl_mask * mask = cpl_mask_wrap(nx, nny, cpl_mask_get_data(omask) + offset);
118 cpl_mask_delete(hcpl_image_set_bpm(img, mask));
119 omask = cpl_image_get_bpm(himg->error);
120 mask = cpl_mask_wrap(nx, nny, cpl_mask_get_data(omask) + offset);
121 cpl_mask_delete(hcpl_image_set_bpm(err, mask));
122
123 return hdrl_image_wrap(img, err, (hdrl_free*)&hdrl_image_view_delete,
124 CPL_FALSE);
125}
126
127/* ---------------------------------------------------------------------------*/
144/* ---------------------------------------------------------------------------*/
145static const hdrl_image *
146hdrl_image_const_row_view_create(const hdrl_image * himg,
147 cpl_size ly,
148 cpl_size uy,
149 hdrl_free * destructor)
150{
151 const cpl_image * oimg = hdrl_image_get_image_const(himg);
152 const cpl_image * oerr = hdrl_image_get_error_const(himg);
153 const size_t dsz = cpl_type_get_sizeof(cpl_image_get_type(oimg));
154 const size_t esz = cpl_type_get_sizeof(cpl_image_get_type(oerr));
155 const cpl_size nx = hdrl_image_get_size_x(himg);
156 const char * d = cpl_image_get_data_const(oimg);
157 const char * e = cpl_image_get_data_const(oerr);
158 size_t offset = (ly - 1) * nx;
159 cpl_size nny = uy - ly + 1;
160
161 CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
162
163 cpl_image * img = cpl_image_wrap(nx, nny, cpl_image_get_type(oimg),
164 (char*)d + offset * dsz);
165 cpl_image * err = cpl_image_wrap(nx, nny, cpl_image_get_type(oerr),
166 (char *)e + offset * esz);
167
168 /* we must create a mask in the original in order for views to work */
169 const cpl_mask * omask = hdrl_image_get_mask_const(himg);
170 if (omask) {
171 cpl_mask * mask = cpl_mask_wrap(nx, nny,
172 (cpl_binary*)cpl_mask_get_data_const(omask) + offset);
173 cpl_mask_delete(hcpl_image_set_bpm(img, mask));
174 }
175 else {
176 if (cpl_image_get_bpm_const(himg->error) != NULL) {
177 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
178 "Inconsistent HDRL image, data image has no"
179 " BPM but error image does");
180 cpl_image_unwrap(img);
181 cpl_image_unwrap(err);
182 return NULL;
183 }
184 }
185 omask = cpl_image_get_bpm_const(himg->error);
186 if (omask) {
187 cpl_mask * mask = cpl_mask_wrap(nx, nny,
188 (cpl_binary*)cpl_mask_get_data_const(omask) + offset);
189 cpl_mask_delete(hcpl_image_set_bpm(err, mask));
190 }
191
192 CPL_DIAG_PRAGMA_POP;
193
194 return hdrl_image_wrap(img, err, destructor, CPL_FALSE);
195}
196
197/* ---------------------------------------------------------------------------*/
214/* ---------------------------------------------------------------------------*/
215hdrl_imagelist * hdrl_imagelist_row_view(
216 hdrl_imagelist * hl,
217 cpl_size ly,
218 cpl_size uy)
219{
220 cpl_ensure(hl, CPL_ERROR_NULL_INPUT, NULL);
221 cpl_ensure(uy >= ly, CPL_ERROR_ILLEGAL_INPUT, NULL);
222 cpl_ensure(ly > 0, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
223 size_t n = hdrl_imagelist_get_size(hl);
224 cpl_ensure(n > 0, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
225 cpl_ensure(uy <= hdrl_imagelist_get_size_y(hl),
226 CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
227
228 hdrl_imagelist * viewlist = hdrl_imagelist_new();
229 for (size_t i = 0; i < n; i++) {
230 hdrl_image * img = hdrl_imagelist_get(hl, i);
231 hdrl_image * view = hdrl_image_row_view_create(img, ly, uy);
232 if (view == NULL) {
233 hdrl_imagelist_delete(viewlist);
234 return NULL;
235 }
236 hdrl_imagelist_set(viewlist, view, i);
237 }
238
239 return viewlist;
240}
241
242/* ---------------------------------------------------------------------------*/
261/* ---------------------------------------------------------------------------*/
262const hdrl_imagelist * hdrl_imagelist_const_row_view(
263 const hdrl_imagelist * hl,
264 cpl_size ly,
265 cpl_size uy)
266{
267 cpl_ensure(hl, CPL_ERROR_NULL_INPUT, NULL);
268 cpl_ensure(uy >= ly, CPL_ERROR_ILLEGAL_INPUT, NULL);
269 cpl_ensure(ly > 0, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
270 size_t n = hdrl_imagelist_get_size(hl);
271 cpl_ensure(n > 0, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
272 cpl_ensure(uy <= hdrl_imagelist_get_size_y(hl),
273 CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
274
275 hdrl_imagelist * viewlist = hdrl_imagelist_new();
276 for (size_t i = 0; i < n; i++) {
277 hdrl_image * img = hdrl_imagelist_get(hl, i);
278 const hdrl_image * view = hdrl_image_const_row_view_create(img, ly, uy,
279 (hdrl_free*)&hdrl_image_view_delete);
280 if (view == NULL) {
281 hdrl_imagelist_delete(viewlist);
282 return NULL;
283 }
284 CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
285 hdrl_imagelist_set(viewlist, (hdrl_image*)view, i);
286 CPL_DIAG_PRAGMA_POP;
287 }
288
289 return viewlist;
290}
291
292/* ---------------------------------------------------------------------------*/
308/* ---------------------------------------------------------------------------*/
309hdrl_imagelist * hdrl_imagelist_image_view(
310 hdrl_imagelist * hl,
311 cpl_size lz,
312 cpl_size uz)
313{
314 cpl_ensure(hl, CPL_ERROR_NULL_INPUT, NULL);
315 cpl_ensure(uz > lz, CPL_ERROR_ILLEGAL_INPUT, NULL);
316 size_t n = hdrl_imagelist_get_size(hl);
317 cpl_ensure(lz >= 0, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
318 cpl_ensure(uz <= (cpl_size)n, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
319
320 hdrl_imagelist * viewlist = hdrl_imagelist_new();
321 for (size_t i = lz; i < (size_t)uz; i++) {
322 hdrl_image * img = hdrl_imagelist_get(hl, i);
323 size_t ny = hdrl_image_get_size_y(img);
324 hdrl_image * view = hdrl_image_row_view_create(img, 1, ny);
325 if (view == NULL) {
326 hdrl_imagelist_delete(viewlist);
327 return NULL;
328 }
329 hdrl_imagelist_set(viewlist, view, i - lz);
330 }
331
332 return viewlist;
333}
334
335/* ---------------------------------------------------------------------------*/
353/* ---------------------------------------------------------------------------*/
354const hdrl_imagelist * hdrl_imagelist_const_cpl_row_view(
355 const cpl_imagelist * imglist,
356 const cpl_imagelist * errlist,
357 cpl_size ly,
358 cpl_size uy)
359{
360 cpl_ensure(imglist, CPL_ERROR_NULL_INPUT, NULL);
361 cpl_ensure(uy >= ly, CPL_ERROR_ILLEGAL_INPUT, NULL);
362 cpl_ensure(ly > 0, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
363 size_t n = cpl_imagelist_get_size(imglist);
364 cpl_ensure(n > 0, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
365 cpl_ensure(uy <= cpl_image_get_size_y(cpl_imagelist_get_const(imglist, 0)),
366 CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
367 cpl_ensure(errlist == NULL || n <= (size_t)cpl_imagelist_get_size(errlist),
368 CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
369
370 hdrl_imagelist * viewlist = hdrl_imagelist_new();
371 if (errlist) {
372 const cpl_image * img = cpl_imagelist_get_const(imglist, 0);
373 const cpl_image * err = cpl_imagelist_get_const(errlist, 0);
374
375 if (cpl_image_get_type(img) != HDRL_TYPE_DATA ||
376 cpl_image_get_type(err) != HDRL_TYPE_ERROR) {
377 hdrl_imagelist_delete(viewlist);
378 cpl_error_set_message(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
379 "Can only view image pairs with type "
380 "HDRL_TYPE_DATA and HDRL_TYPE_ERROR");
381 return NULL;
382 }
383 if (cpl_image_get_size_x(img) != cpl_image_get_size_x(err) ||
384 cpl_image_get_size_y(img) != cpl_image_get_size_y(err)) {
385 hdrl_imagelist_delete(viewlist);
386 cpl_error_set_message(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
387 "Image and Error not consistent");
388 return NULL;
389 }
390 for (size_t i = 0; i < n; i++) {
391 img = cpl_imagelist_get_const(imglist, i);
392 err = cpl_imagelist_get_const(errlist, i);
393 if ((cpl_image_get_bpm_const(img) && !cpl_image_get_bpm_const(err)) ||
394 (!cpl_image_get_bpm_const(img) && cpl_image_get_bpm_const(err))) {
395 hdrl_imagelist_delete(viewlist);
396 cpl_error_set_message(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
397 "Image and error bad pixel mask "
398 "not equal");
399 return NULL;
400 }
401 if (cpl_image_get_bpm_const(img) && cpl_image_get_bpm_const(err)) {
402 const cpl_binary * dibpm =
403 cpl_mask_get_data_const(cpl_image_get_bpm_const(img));
404 const cpl_binary * debpm =
405 cpl_mask_get_data_const(cpl_image_get_bpm_const(err));
406 if (memcmp(dibpm, debpm, hdrl_get_image_npix(img)) != 0) {
407 hdrl_imagelist_delete(viewlist);
408 cpl_error_set_message(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
409 "Image and error bad pixel mask "
410 "not equal");
411 return NULL;
412 }
413 }
414 /* TODO could wrap directly */
415 CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
416 hdrl_image * himg = hdrl_image_wrap((cpl_image*)img, (cpl_image*)err,
417 (hdrl_free*)&hdrl_image_view_delete,
418 CPL_FALSE);
419 const hdrl_image * view =
420 hdrl_image_const_row_view_create(himg, ly, uy,
421 (hdrl_free*)&hdrl_image_view_delete);
422 if (view == NULL) {
423 hdrl_imagelist_delete(viewlist);
424 return NULL;
425 }
426 hdrl_image_unwrap(himg);
427 hdrl_imagelist_set(viewlist, (hdrl_image*)view, i);
428 CPL_DIAG_PRAGMA_POP;
429 }
430 }
431 else {
432 const cpl_image * img = cpl_imagelist_get_const(imglist, 0);
433 if (cpl_image_get_type(img) != HDRL_TYPE_DATA) {
434 hdrl_imagelist_delete(viewlist);
435 cpl_error_set_message(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
436 "Can only view images with type "
437 "HDRL_TYPE_DATA");
438 return NULL;
439 }
440 for (size_t i = 0; i < n; i++) {
441 img = cpl_imagelist_get_const(imglist, i);
442 /* can't share a single image as it needs the same bpm as img
443 * TODO the bpm of img could probably be shared */
444 cpl_image * err = cpl_image_new(cpl_image_get_size_x(img),
445 cpl_image_get_size_y(img),
446 HDRL_TYPE_ERROR);
447 if (cpl_image_get_bpm_const(img)) {
448 cpl_image_reject_from_mask(err, cpl_image_get_bpm_const(img));
449 }
450 /* TODO could wrap directly */
451 CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
452 hdrl_image * himg = hdrl_image_wrap((cpl_image*)img, err,
453 (hdrl_free*)&hdrl_image_imgview_delete,
454 CPL_FALSE);
455 const hdrl_image * view = hdrl_image_const_row_view_create(himg, ly, uy,
456 (hdrl_free*)&hdrl_image_imgview_delete);
457 if (view == NULL) {
458 hdrl_imagelist_delete(viewlist);
459 return NULL;
460 }
461 hdrl_image_unwrap(himg);
462 cpl_mask_unwrap(cpl_image_unset_bpm(err));
463 cpl_image_unwrap(err);
464 hdrl_imagelist_set(viewlist, (hdrl_image*)view, i);
465 CPL_DIAG_PRAGMA_POP;
466 }
467 }
468
469 return viewlist;
470}
471
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
Definition: hdrl_image.c:157
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:131
cpl_size hdrl_image_get_size_y(const hdrl_image *self)
return size of Y dimension of image
Definition: hdrl_image.c:540
const cpl_mask * hdrl_image_get_mask_const(const hdrl_image *himg)
get cpl bad pixel mask from image
Definition: hdrl_image.c:175
cpl_size hdrl_image_get_size_x(const hdrl_image *self)
return size of X dimension of image
Definition: hdrl_image.c:525
const cpl_image * hdrl_image_get_error_const(const hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:144
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:105
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:118
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.
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.
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.