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  self->properties = cpl_propertylist_load(filename, position);
508 
509  if (self->properties == NULL) {
510  return 1;
511  }
512 
513  return 0;
514 
515 }
516 
517 
538 cxint
539 giraffe_image_load(GiImage *self, const cxchar *filename, cxint position)
540 {
541 
542  cx_assert(self != NULL);
543 
544  if (giraffe_image_load_pixels(self, filename, position, 0) != 0) {
545  return 1;
546  }
547 
548  if (giraffe_image_load_properties(self, filename, position) != 0) {
549  return 1;
550  }
551 
552  return 0;
553 
554 }
555 
556 
572 cxint
573 giraffe_image_save(GiImage *self, const cxchar *filename)
574 {
575 
576  const cxchar *fctid = "giraffe_image_save";
577 
578 
579  if (filename == NULL) {
580  return 1;
581  }
582 
583  if (self) {
584 
585  cxint code;
586  cxint bits_per_pixel;
587 
588 
589  switch (self->type) {
590  case CPL_TYPE_INT:
591  bits_per_pixel = CPL_BPP_32_SIGNED;
592  break;
593 
594  case CPL_TYPE_FLOAT:
595  bits_per_pixel = CPL_BPP_IEEE_FLOAT;
596  break;
597 
598  case CPL_TYPE_DOUBLE:
599 
600  /* CPL_TYPE_DOUBLE images are saved as float */
601 
602  bits_per_pixel = CPL_BPP_IEEE_FLOAT;
603  break;
604 
605  default:
606 
607  /*
608  * If self was properly created using the constructors we
609  * should never reach this point.
610  */
611 
612  cpl_error_set(fctid, CPL_ERROR_INVALID_TYPE);
613  return 1;
614  break;
615  }
616 
617  code = cpl_image_save(self->pixels, filename, bits_per_pixel,
618  self->properties, CPL_IO_DEFAULT);
619 
620  if (code != CPL_ERROR_NONE) {
621  return 1;
622  }
623  }
624 
625  return 0;
626 
627 }
628 
629 
655 cxint
656 giraffe_image_paste(GiImage *self, const GiImage *image, cxint x, cxint y,
657  cxbool clip)
658 {
659 
660  const cxchar *fctid = "giraffe_image_paste";
661 
662 
663  cx_assert(self != NULL);
664 
665  if (x < 0 || y < 0) {
666  cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
667  return -1;
668  }
669 
670  if (image != NULL) {
671 
672  cpl_image *_self = giraffe_image_get(self);
673  cpl_image *_image = giraffe_image_get((GiImage *) image);
674 
675  cxint i;
676  cxint nx = cpl_image_get_size_x(_self);
677  cxint ny = cpl_image_get_size_y(_self);
678  cxint sx = cpl_image_get_size_x(_image);
679  cxint sy = cpl_image_get_size_y(_image);
680  cxint ys = y * nx;
681 
682  cxptr _spixel = cpl_image_get_data(_self);
683  cxptr _ipixel = cpl_image_get_data(_image);
684 
685  cpl_type type = cpl_image_get_type(_self);
686 
687 
688  if (type != cpl_image_get_type(_image)) {
689  cpl_error_set(fctid, CPL_ERROR_TYPE_MISMATCH);
690  return -4;
691  }
692 
693  if ((x + sx) > nx) {
694  if (clip == FALSE) {
695  cpl_error_set(fctid, CPL_ERROR_ACCESS_OUT_OF_RANGE);
696  return -2;
697  }
698 
699  sx -= nx - x;
700  }
701 
702  if ((y + sy) > ny) {
703  if (clip == FALSE) {
704  cpl_error_set(fctid, CPL_ERROR_ACCESS_OUT_OF_RANGE);
705  return -3;
706  }
707 
708  sy -= ny - y;
709  }
710 
711  for (i = 0; i < sy; i++) {
712 
713  cxint bytes = cpl_type_get_sizeof(type);
714  cxint soffset = (ys + nx * i + x) * bytes;
715  cxint ioffset = (sx * i) * bytes;
716  cxint sz = sx * bytes;
717 
718  memcpy((cxchar*)_spixel + soffset,
719  (cxchar*)_ipixel + ioffset, sz);
720 
721  }
722 
723  }
724 
725  return 0;
726 
727 }
728 
729 
743 void
744 giraffe_image_print(GiImage *self)
745 {
746 
747  if (self) {
748  cx_print("Resources for Giraffe image at %p:", self);
749  cx_print(" properties at %p", self->properties);
750  cx_print(" list size: %" CPL_SIZE_FORMAT "\n",
751  cpl_propertylist_get_size(self->properties));
752  cx_print(" pixels at %p:", cpl_image_get_data(self->pixels));
753  cx_print(" type: %02x", cpl_image_get_type(self->pixels));
754  cx_print(" x-size: %" CPL_SIZE_FORMAT,
755  cpl_image_get_size_x(self->pixels));
756  cx_print(" y-size: %" CPL_SIZE_FORMAT "\n",
757  cpl_image_get_size_y(self->pixels));
758  }
759  else {
760  cx_print("Invalid input image at %p", self);
761  }
762 
763  return;
764 
765 }
766 
767 
775 cxint
776 giraffe_image_add_info(GiImage *image, const GiRecipeInfo *info ,
777  const cpl_frameset *set)
778 {
779 
780  cxint status = 0;
781 
782  cpl_propertylist *properties = NULL;
783 
784 
785  if (image == NULL) {
786  return -1;
787  }
788 
789  properties = giraffe_image_get_properties(image);
790 
791  if (properties == NULL) {
792  return -2;
793  }
794 
795  if (info != NULL) {
796  status = giraffe_add_recipe_info(properties, info);
797 
798  if (status != 0) {
799  return -3;
800  }
801 
802  if (set != NULL) {
803  status = giraffe_add_frameset_info(properties, set,
804  info->sequence);
805 
806  if (status != 0) {
807  return -4;
808  }
809  }
810  }
811 
812  return 0;
813 
814 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.12.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Mon Mar 24 2014 11:43:52 by doxygen 1.8.2 written by Dimitri van Heesch, © 1997-2004