33 #include <cxmessages.h>
36 #include <cpl_image.h>
37 #include <cpl_propertylist.h>
38 #include <cpl_error.h>
58 cpl_propertylist *properties;
76 GiImage *
self = cx_calloc(1,
sizeof *
self);
79 self->properties = NULL;
110 self->pixels = cpl_image_new(nx, ny, self->type);
112 if (self->pixels == NULL) {
118 self->properties = cpl_propertylist_new();
120 if (self->properties == NULL) {
150 GiImage *clone = NULL;
159 if (self->pixels != NULL) {
160 cx_assert(self->type == cpl_image_get_type(self->pixels));
161 clone->pixels = cpl_image_duplicate(self->pixels);
164 if (self->properties != NULL) {
166 cpl_propertylist_duplicate(self->properties);
194 if (self->pixels != NULL) {
195 cpl_image_delete(self->pixels);
199 if (self->properties != NULL) {
200 cpl_propertylist_delete(self->properties);
201 self->properties = NULL;
255 cx_assert(
self != NULL);
261 if (self->type != cpl_image_get_type(image)) {
265 if (self->pixels != NULL) {
266 cpl_image_delete(self->pixels);
270 self->pixels = cpl_image_duplicate(image);
272 return self->pixels ? 0 : 1;
297 return self->properties;
327 if (self->properties) {
328 cpl_propertylist_delete(self->properties);
329 self->properties = NULL;
332 self->properties = cpl_propertylist_duplicate(properties);
334 return self->properties ? 0 : 1;
356 const cxchar *
const fctid =
"giraffe_image_copy_matrix";
361 cxdouble *elements = NULL;
364 cx_assert(
self != NULL);
366 if (matrix == NULL) {
370 nrow = cpl_matrix_get_nrow(matrix);
371 ncol = cpl_matrix_get_ncol(matrix);
372 cx_assert(nrow > 0 && ncol > 0);
374 elements = cpl_matrix_get_data(matrix);
375 cx_assert(elements != NULL);
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);
385 self->pixels = cpl_image_new(ncol, nrow, self->type);
388 switch (self->type) {
392 cxsize sz = nrow * ncol;
394 cxint *pixels = cpl_image_get_data_int(self->pixels);
397 for (i = 0; i < sz; i++) {
398 pixels[i] = (cxint) elements[i];
406 cxsize sz = nrow * ncol;
408 cxfloat *pixels = cpl_image_get_data_float(self->pixels);
411 for (i = 0; i < sz; i++) {
412 pixels[i] = (cxfloat) elements[i];
417 case CPL_TYPE_DOUBLE:
419 cxsize sz = nrow * ncol *
sizeof(cxdouble);
421 cxptr pixels = cpl_image_get_data(self->pixels);
424 memcpy(pixels, elements, sz);
429 cpl_error_set(fctid, CPL_ERROR_INVALID_TYPE);
460 cxint position, cxint plane)
463 cx_assert(
self != NULL);
465 if (self->pixels != NULL) {
466 cpl_image_delete(self->pixels);
470 self->pixels = cpl_image_load(filename, self->type, plane, position);
472 return self->pixels ? 0 : 1;
500 cx_assert(
self != NULL);
502 if (self->properties) {
503 cpl_propertylist_delete(self->properties);
504 self->properties = NULL;
507 self->properties = cpl_propertylist_load(filename, position);
509 if (self->properties == NULL) {
542 cx_assert(
self != NULL);
576 const cxchar *fctid =
"giraffe_image_save";
579 if (filename == NULL) {
586 cxint bits_per_pixel;
589 switch (self->type) {
591 bits_per_pixel = CPL_BPP_32_SIGNED;
595 bits_per_pixel = CPL_BPP_IEEE_FLOAT;
598 case CPL_TYPE_DOUBLE:
602 bits_per_pixel = CPL_BPP_IEEE_FLOAT;
612 cpl_error_set(fctid, CPL_ERROR_INVALID_TYPE);
617 code = cpl_image_save(self->pixels, filename, bits_per_pixel,
618 self->properties, CPL_IO_DEFAULT);
620 if (code != CPL_ERROR_NONE) {
660 const cxchar *fctid =
"giraffe_image_paste";
663 cx_assert(
self != NULL);
665 if (x < 0 || y < 0) {
666 cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
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);
682 cxptr _spixel = cpl_image_get_data(_self);
683 cxptr _ipixel = cpl_image_get_data(_image);
685 cpl_type type = cpl_image_get_type(_self);
688 if (type != cpl_image_get_type(_image)) {
689 cpl_error_set(fctid, CPL_ERROR_TYPE_MISMATCH);
695 cpl_error_set(fctid, CPL_ERROR_ACCESS_OUT_OF_RANGE);
704 cpl_error_set(fctid, CPL_ERROR_ACCESS_OUT_OF_RANGE);
711 for (i = 0; i < sy; i++) {
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;
718 memcpy((cxchar*)_spixel + soffset,
719 (cxchar*)_ipixel + ioffset, sz);
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));
760 cx_print(
"Invalid input image at %p",
self);
777 const cpl_frameset *
set)
782 cpl_propertylist *properties = NULL;
791 if (properties == NULL) {