GIRAFFE Pipeline Reference Manual

giimage.c
1 /* $Id$
2  *
3  * This file is part of the GIRAFFE Pipeline
4  * Copyright (C) 2002-2007 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$
23  * $Date$
24  * $Revision$
25  * $Name$
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 #include <cxmemory.h>
33 #include <cxmessages.h>
34 
35 #include <cpl_type.h>
36 #include <cpl_image.h>
37 #include <cpl_propertylist.h>
38 #include <cpl_error.h>
39 
40 #include "gialias.h"
41 #include "giimage.h"
42 
43 
56 struct GiImage {
57  cpl_image *pixels;
58  cpl_propertylist *properties;
59  cpl_type type;
60 };
61 
62 
72 GiImage *
73 giraffe_image_new(cpl_type type)
74 {
75 
76  GiImage *self = cx_calloc(1, sizeof *self);
77 
78  self->pixels = NULL;
79  self->properties = NULL;
80  self->type = type;
81 
82  return self;
83 
84 }
85 
86 
102 GiImage *
103 giraffe_image_create(cpl_type type, cxint nx, cxint ny)
104 {
105 
106  GiImage *self = giraffe_image_new(type);
107 
108  if (self) {
109 
110  self->pixels = cpl_image_new(nx, ny, self->type);
111 
112  if (self->pixels == NULL) {
113  giraffe_image_delete(self);
114  self = NULL;
115  }
116  else {
117 
118  self->properties = cpl_propertylist_new();
119 
120  if (self->properties == NULL) {
121  giraffe_image_delete(self);
122  self = NULL;
123  }
124 
125  }
126 
127  }
128 
129  return self;
130 
131 }
132 
133 
146 GiImage *
147 giraffe_image_duplicate(const GiImage *self)
148 {
149 
150  GiImage *clone = NULL;
151 
152 
153  if (self) {
154 
155  clone = giraffe_image_new(self->type);
156 
157  if (clone != NULL) {
158 
159  if (self->pixels != NULL) {
160  cx_assert(self->type == cpl_image_get_type(self->pixels));
161  clone->pixels = cpl_image_duplicate(self->pixels);
162  }
163 
164  if (self->properties != NULL) {
165  clone->properties =
166  cpl_propertylist_duplicate(self->properties);
167  }
168 
169  }
170 
171  }
172 
173  return clone;
174 
175 }
176 
177 
188 void
189 giraffe_image_delete(GiImage *self)
190 {
191 
192  if (self != NULL) {
193 
194  if (self->pixels != NULL) {
195  cpl_image_delete(self->pixels);
196  self->pixels = NULL;
197  }
198 
199  if (self->properties != NULL) {
200  cpl_propertylist_delete(self->properties);
201  self->properties = NULL;
202  }
203 
204  cx_free(self);
205 
206  }
207 
208  return;
209 
210 }
211 
212 
225 cpl_image *
226 giraffe_image_get(const GiImage *self)
227 {
228 
229  if (self == NULL) {
230  return NULL;
231  }
232 
233  return self->pixels;
234 
235 }
236 
251 cxint
252 giraffe_image_set(GiImage *self, cpl_image *image)
253 {
254 
255  cx_assert(self != NULL);
256 
257  if (image == NULL) {
258  return 1;
259  }
260 
261  if (self->type != cpl_image_get_type(image)) {
262  return 1;
263  }
264 
265  if (self->pixels != NULL) {
266  cpl_image_delete(self->pixels);
267  self->pixels = NULL;
268  }
269 
270  self->pixels = cpl_image_duplicate(image);
271 
272  return self->pixels ? 0 : 1;
273 
274 }
275 
276 
289 cpl_propertylist *
290 giraffe_image_get_properties(const GiImage *self)
291 {
292 
293  if (self == NULL) {
294  return NULL;
295  }
296 
297  return self->properties;
298 
299 }
300 
301 
319 cxint
320 giraffe_image_set_properties(GiImage *self, cpl_propertylist *properties)
321  {
322 
323  if (self == NULL) {
324  return 1;
325  }
326 
327  if (self->properties) {
328  cpl_propertylist_delete(self->properties);
329  self->properties = NULL;
330  }
331 
332  self->properties = cpl_propertylist_duplicate(properties);
333 
334  return self->properties ? 0 : 1;
335 
336 }
337 
338 
352 cxint
353 giraffe_image_copy_matrix(GiImage *self, cpl_matrix *matrix)
354 {
355 
356  const cxchar *const fctid = "giraffe_image_copy_matrix";
357 
358  cxint nrow = 0;
359  cxint ncol = 0;
360 
361  cxdouble *elements = NULL;
362 
363 
364  cx_assert(self != NULL);
365 
366  if (matrix == NULL) {
367  return 1;
368  }
369 
370  nrow = cpl_matrix_get_nrow(matrix);
371  ncol = cpl_matrix_get_ncol(matrix);
372  cx_assert(nrow > 0 && ncol > 0);
373 
374  elements = cpl_matrix_get_data(matrix);
375  cx_assert(elements != NULL);
376 
377  if (self->pixels != NULL) {
378  if (cpl_image_get_size_x(self->pixels) != ncol ||
379  cpl_image_get_size_y(self->pixels) != nrow) {
380  cpl_image_delete(self->pixels);
381  self->pixels = cpl_image_new(ncol, nrow, self->type);
382  }
383  }
384  else {
385  self->pixels = cpl_image_new(ncol, nrow, self->type);
386  }
387 
388  switch (self->type) {
389  case CPL_TYPE_INT:
390  {
391  cxsize i;
392  cxsize sz = nrow * ncol;
393 
394  cxint *pixels = cpl_image_get_data_int(self->pixels);
395 
396 
397  for (i = 0; i < sz; i++) {
398  pixels[i] = (cxint) elements[i];
399  }
400  break;
401  }
402 
403  case CPL_TYPE_FLOAT:
404  {
405  cxsize i;
406  cxsize sz = nrow * ncol;
407 
408  cxfloat *pixels = cpl_image_get_data_float(self->pixels);
409 
410 
411  for (i = 0; i < sz; i++) {
412  pixels[i] = (cxfloat) elements[i];
413  }
414  break;
415  }
416 
417  case CPL_TYPE_DOUBLE:
418  {
419  cxsize sz = nrow * ncol * sizeof(cxdouble);
420 
421  cxptr pixels = cpl_image_get_data(self->pixels);
422 
423 
424  memcpy(pixels, elements, sz);
425  break;
426  }
427 
428  default:
429  cpl_error_set(fctid, CPL_ERROR_INVALID_TYPE);
430  return 1;
431  break;
432  }
433 
434  return 0;
435 
436 }
437 
438 
458 cxint
459 giraffe_image_load_pixels(GiImage *self, const cxchar *filename,
460  cxint position, cxint plane)
461 {
462 
463  cx_assert(self != NULL);
464 
465  if (self->pixels != NULL) {
466  cpl_image_delete(self->pixels);
467  self->pixels = NULL;
468  }
469 
470  self->pixels = cpl_image_load(filename, self->type, plane, position);
471 
472  return self->pixels ? 0 : 1;
473 
474 }
475 
476 
495 cxint
496 giraffe_image_load_properties(GiImage *self, const cxchar *filename,
497  cxint position)
498 {
499 
500  cx_assert(self != NULL);
501 
502  if (self->properties) {
503  cpl_propertylist_delete(self->properties);
504  self->properties = NULL;
505  }
506 
507  /*
508  * Strip comments during load
509  */
510 
511  self->properties = cpl_propertylist_load_regexp(filename, position,
512  "^COMMENT$", TRUE);
513 
514  if (self->properties == NULL) {
515  return 1;
516  }
517 
518  return 0;
519 
520 }
521 
522 
543 cxint
544 giraffe_image_load(GiImage *self, const cxchar *filename, cxint position)
545 {
546 
547  cx_assert(self != NULL);
548 
549  if (giraffe_image_load_pixels(self, filename, position, 0) != 0) {
550  return 1;
551  }
552 
553  if (giraffe_image_load_properties(self, filename, position) != 0) {
554  return 1;
555  }
556 
557  return 0;
558 
559 }
560 
561 
577 cxint
578 giraffe_image_save(GiImage *self, const cxchar *filename)
579 {
580 
581  const cxchar *fctid = "giraffe_image_save";
582 
583 
584  if (filename == NULL) {
585  return 1;
586  }
587 
588  if (self) {
589 
590  cxint code;
591  cxint bits_per_pixel;
592 
593 
594  switch (self->type) {
595  case CPL_TYPE_INT:
596  bits_per_pixel = CPL_BPP_32_SIGNED;
597  break;
598 
599  case CPL_TYPE_FLOAT:
600  bits_per_pixel = CPL_BPP_IEEE_FLOAT;
601  break;
602 
603  case CPL_TYPE_DOUBLE:
604 
605  /* CPL_TYPE_DOUBLE images are saved as float */
606 
607  bits_per_pixel = CPL_BPP_IEEE_FLOAT;
608  break;
609 
610  default:
611 
612  /*
613  * If self was properly created using the constructors we
614  * should never reach this point.
615  */
616 
617  cpl_error_set(fctid, CPL_ERROR_INVALID_TYPE);
618  return 1;
619  break;
620  }
621 
622  code = cpl_image_save(self->pixels, filename, bits_per_pixel,
623  self->properties, CPL_IO_DEFAULT);
624 
625  if (code != CPL_ERROR_NONE) {
626  return 1;
627  }
628  }
629 
630  return 0;
631 
632 }
633 
634 
660 cxint
661 giraffe_image_paste(GiImage *self, const GiImage *image, cxint x, cxint y,
662  cxbool clip)
663 {
664 
665  const cxchar *fctid = "giraffe_image_paste";
666 
667 
668  cx_assert(self != NULL);
669 
670  if (x < 0 || y < 0) {
671  cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
672  return -1;
673  }
674 
675  if (image != NULL) {
676 
677  cpl_image *_self = giraffe_image_get(self);
678  cpl_image *_image = giraffe_image_get((GiImage *) image);
679 
680  cxint i;
681  cxint nx = cpl_image_get_size_x(_self);
682  cxint ny = cpl_image_get_size_y(_self);
683  cxint sx = cpl_image_get_size_x(_image);
684  cxint sy = cpl_image_get_size_y(_image);
685  cxint ys = y * nx;
686 
687  cxptr _spixel = cpl_image_get_data(_self);
688  cxptr _ipixel = cpl_image_get_data(_image);
689 
690  cpl_type type = cpl_image_get_type(_self);
691 
692 
693  if (type != cpl_image_get_type(_image)) {
694  cpl_error_set(fctid, CPL_ERROR_TYPE_MISMATCH);
695  return -4;
696  }
697 
698  if ((x + sx) > nx) {
699  if (clip == FALSE) {
700  cpl_error_set(fctid, CPL_ERROR_ACCESS_OUT_OF_RANGE);
701  return -2;
702  }
703 
704  sx -= nx - x;
705  }
706 
707  if ((y + sy) > ny) {
708  if (clip == FALSE) {
709  cpl_error_set(fctid, CPL_ERROR_ACCESS_OUT_OF_RANGE);
710  return -3;
711  }
712 
713  sy -= ny - y;
714  }
715 
716  for (i = 0; i < sy; i++) {
717 
718  cxint bytes = cpl_type_get_sizeof(type);
719  cxint soffset = (ys + nx * i + x) * bytes;
720  cxint ioffset = (sx * i) * bytes;
721  cxint sz = sx * bytes;
722 
723  memcpy((cxchar*)_spixel + soffset,
724  (cxchar*)_ipixel + ioffset, sz);
725 
726  }
727 
728  }
729 
730  return 0;
731 
732 }
733 
734 
748 void
749 giraffe_image_print(GiImage *self)
750 {
751 
752  if (self) {
753  cx_print("Resources for Giraffe image at %p:", self);
754  cx_print(" properties at %p", self->properties);
755  cx_print(" list size: %" CPL_SIZE_FORMAT "\n",
756  cpl_propertylist_get_size(self->properties));
757  cx_print(" pixels at %p:", cpl_image_get_data(self->pixels));
758  cx_print(" type: %02x", cpl_image_get_type(self->pixels));
759  cx_print(" x-size: %" CPL_SIZE_FORMAT,
760  cpl_image_get_size_x(self->pixels));
761  cx_print(" y-size: %" CPL_SIZE_FORMAT "\n",
762  cpl_image_get_size_y(self->pixels));
763  }
764  else {
765  cx_print("Invalid input image at %p", self);
766  }
767 
768  return;
769 
770 }
771 
772 
780 cxint
781 giraffe_image_add_info(GiImage *image, const GiRecipeInfo *info ,
782  const cpl_frameset *set)
783 {
784 
785  cxint status = 0;
786 
787  cpl_propertylist *properties = NULL;
788 
789 
790  if (image == NULL) {
791  return -1;
792  }
793 
794  properties = giraffe_image_get_properties(image);
795 
796  if (properties == NULL) {
797  return -2;
798  }
799 
800  if (info != NULL) {
801  status = giraffe_add_recipe_info(properties, info);
802 
803  if (status != 0) {
804  return -3;
805  }
806 
807  if (set != NULL) {
808  status = giraffe_add_frameset_info(properties, set,
809  info->sequence);
810 
811  if (status != 0) {
812  return -4;
813  }
814  }
815  }
816 
817  return 0;
818 
819 }
void giraffe_image_print(GiImage *self)
Prints status information about a Giraffe image.
Definition: giimage.c:749
cxint giraffe_image_add_info(GiImage *image, const GiRecipeInfo *info, const cpl_frameset *set)
Add additional frame information to an image.
Definition: giimage.c:781
cxint giraffe_image_set(GiImage *self, cpl_image *image)
Sets the image data.
Definition: giimage.c:252
cxint giraffe_add_frameset_info(cpl_propertylist *plist, const cpl_frameset *set, cxint sequence)
Add frameset specific information to a property list.
Definition: giutils.c:587
cxint giraffe_image_paste(GiImage *self, const GiImage *image, cxint x, cxint y, cxbool clip)
Paste an image into another at a given position.
Definition: giimage.c:661
cxint giraffe_image_load_pixels(GiImage *self, const cxchar *filename, cxint position, cxint plane)
Gets image data from a file.
Definition: giimage.c:459
cxint giraffe_image_load(GiImage *self, const cxchar *filename, cxint position)
Gets image data and properties from a file.
Definition: giimage.c:544
cxint giraffe_image_save(GiImage *self, const cxchar *filename)
Write a Giraffe image to a file.
Definition: giimage.c:578
void giraffe_image_delete(GiImage *self)
Destroys an image.
Definition: giimage.c:189
GiImage * giraffe_image_duplicate(const GiImage *self)
Creates a copy of an image.
Definition: giimage.c:147
GiImage * giraffe_image_new(cpl_type type)
Creates an empty image container.
Definition: giimage.c:73
GiImage * giraffe_image_create(cpl_type type, cxint nx, cxint ny)
Creates an image container of a given type.
Definition: giimage.c:103
cpl_image * giraffe_image_get(const GiImage *self)
Gets the image data.
Definition: giimage.c:226
cxint giraffe_add_recipe_info(cpl_propertylist *plist, const GiRecipeInfo *info)
Add recipe specific information to a property list.
Definition: giutils.c:489
cxint giraffe_image_set_properties(GiImage *self, cpl_propertylist *properties)
Attaches a property list to an image.
Definition: giimage.c:320
cxint giraffe_image_load_properties(GiImage *self, const cxchar *filename, cxint position)
Gets image properties from a file.
Definition: giimage.c:496
cxint giraffe_image_copy_matrix(GiImage *self, cpl_matrix *matrix)
Copies matrix elements into an image.
Definition: giimage.c:353
cpl_propertylist * giraffe_image_get_properties(const GiImage *self)
Get the properties of an image.
Definition: giimage.c:290

This file is part of the GIRAFFE Pipeline Reference Manual 2.14.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Wed Mar 11 2015 13:19:41 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2004