Merge ../libgpiv-omp into fft-omp
[libgpiv.git] / lib / image.c
blob2597f8f75e64118e6adc74832e36d8420a901c37
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*
4 libgpiv - library for Particle Image Velocimetry
6 Copyright (C) 2011 Gerber van der Graaf
8 This file is part of libgpiv.
9 Libgpiv is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ------------------------------------------------------------------------------*/
27 #include <gpiv.h>
28 #include "my_utils.h"
31 #define PNM_GET1(x) PPM_GETB(x)
32 #define PPM_GETB(x) ((x) & 0x3ff)
33 #define PNG_BYTES_TO_CHECK 4
34 #define PNG_KEY__PIV_XCORR "Piv: Xcorr (TRUE/FALSE)"
35 #define PNG_KEY__PIV_DT "Piv: Delta_t (milliseconds)"
38 static png_uint_16
39 get_png_val (png_byte **pp,
40 int bit_depth
43 static png_byte **
44 matrix_png_byte (long nr,
45 long nc
48 static void
49 free_matrix_png_byte (png_byte **m
52 static void
53 get_imgpar_from_textchunks (GpivImagePar *image_par,
54 png_structp png_ptr,
55 png_infop info_ptr
57 static void
58 set_textchunks_from_imgpar (GpivImagePar *image_par,
59 png_structp png_ptr,
60 png_infop info_ptr
64 static void
65 print_img_parameters_set (GpivImagePar *gpiv_image_par);
67 static void
68 read_raw_image_frames (FILE *fp,
69 GpivImage *image
72 static void
73 write_raw_image_frames (FILE *fp,
74 GpivImage *image
79 * Public functions
81 GpivImage *
82 gpiv_read_image (FILE *fp
84 /*---------------------------------------------------------------------------*/
85 /**
86 * Reads image from fp, png formatted.
89 GpivImage *image = NULL;
92 if (fp == NULL) fp = stdin;
94 if ((image = gpiv_read_png_image (fp)) == NULL) {
95 g_message ("gpiv_read_image: gpiv_read_png_image failed");
96 return NULL;
100 return image;
104 GpivImage *
105 gpiv_fread_image (const gchar *fname
107 /* ----------------------------------------------------------------------------
108 * checks filename extension on valid image / data name,
109 * loads image header and data
112 GpivImage *image = NULL;
114 char *err_msg = NULL;
115 gchar *ext, *ext_ORG = NULL,
116 *dirname,
117 *fname_base,
118 *fname_ext,
119 *fname_org = g_strdup(fname);
120 gchar command[2 * GPIV_MAX_CHARS];
122 FILE *fp = NULL;
123 gchar tmp_textfile[GPIV_MAX_CHARS];
124 /* BUGFIX: for the moment use the variable: cleanup_tmp_image */
125 gboolean cleanup_tmp_image = TRUE;
128 if (fname == NULL) {
129 gpiv_warning ("gpiv_fread_image: failing \"fname == NULL\"");
130 return NULL;
133 if (g_file_test (fname, G_FILE_TEST_EXISTS) == FALSE) {
134 gpiv_warning ("gpiv_fread_image: file does not exist");
135 return NULL;
139 * Stripping file name to examine suffix
142 ext = g_strdup(strrchr(fname, '.'));
143 dirname = g_strdup(g_path_get_dirname(fname));
144 fname_base = g_strdup(g_path_get_basename(fname));
145 strtok(fname_base, ".");
146 fname_ext = g_strdup (g_strconcat (dirname, G_DIR_SEPARATOR_S,
147 fname_base, NULL));
148 #ifdef DEBUG
149 g_message("gpiv_fread_image:: dirname = %s\n fname_base = %s\n fname_ext = %s",
150 dirname, fname_base, fname_ext);
151 #endif /* DEBUG */
154 * Converting image from pgm, gif, tif, bmp, ppm format to png
155 * Creating tempory text file to include as required comment in PNG image
156 * for use in gpiv software
158 if (g_str_has_suffix (fname, GPIV_EXT_PGM_IMAGE)
159 || g_str_has_suffix (fname, GPIV_EXT_PGM_IMAGE_UPCASE)) {
160 /* if (strcmp(ext, GPIV_EXT_PGM_IMAGE) == 0) { */
161 g_snprintf (command, 2 * GPIV_MAX_CHARS,
162 "pnmtopng < %s > %s%s",
163 fname, fname_ext, GPIV_EXT_PNG_IMAGE);
165 if (system (command) != 0) {
166 gpiv_warning ("gpiv_fread_image: could not exec shell command \"%s\"",
167 command);
170 ext_ORG = g_strdup(ext);
171 g_free(ext);
172 ext = g_strdup(GPIV_EXT_PNG_IMAGE);
173 fname = g_strdup_printf("%s%s", fname_ext, ext);
176 } else if (g_str_has_suffix (fname, ".gif")
177 || g_str_has_suffix (fname, ".GIF")) {
178 g_snprintf (command, 2 * GPIV_MAX_CHARS,
179 "giftopnm < %s | pnmtopng > %s%s",
180 fname, fname_ext, GPIV_EXT_PNG_IMAGE);
181 if (system (command) != 0) {
182 gpiv_warning ("gpiv_fread_image: could not exec shell command \"%s\"",
183 command);
185 ext_ORG = g_strdup(ext);
186 g_free(ext);
187 ext = g_strdup(GPIV_EXT_PNG_IMAGE);
188 fname = g_strdup_printf("%s%s", fname_ext, ext);
191 } else if (g_str_has_suffix (fname, ".tif")
192 || g_str_has_suffix (fname, ".TIF")) {
193 g_snprintf (command, 2 * GPIV_MAX_CHARS,
194 "tifftopnm < %s | pnmtopng > %s%s",
195 fname, fname_ext, GPIV_EXT_PNG_IMAGE);
197 if (system (command) != 0) {
198 gpiv_warning ("gpiv_fread_image: could not exec shell command \"%s\"",
199 command);
202 ext_ORG = g_strdup(ext);
203 g_free(ext);
204 ext = g_strdup(GPIV_EXT_PNG_IMAGE);
205 fname = g_strdup_printf("%s%s", fname_ext, ext);
208 } else if (g_str_has_suffix (fname, ".bmp")
209 || g_str_has_suffix (fname, ".BMP")) {
210 g_snprintf (command, 2 * GPIV_MAX_CHARS,
211 "bmptoppm < %s | pnmtopng > %s%s",
212 fname, fname_ext, GPIV_EXT_PNG_IMAGE);
214 if (system (command) != 0) {
215 gpiv_warning ("gpiv_fread_image: could not exec shell command \"%s\"",
216 command);
219 ext_ORG = g_strdup(ext);
220 g_free(ext);
221 ext = g_strdup(GPIV_EXT_PNG_IMAGE);
222 fname = g_strdup_printf("%s%s", fname_ext, ext);
224 } else if (g_str_has_suffix (fname, ".ppm")
225 || g_str_has_suffix (fname, ".PPM")) {
226 g_snprintf (command, 2 * GPIV_MAX_CHARS,
227 "pnmtopng < %s > %s%s",
228 fname, fname_ext, GPIV_EXT_PNG_IMAGE);
230 if (system (command) != 0) {
231 gpiv_warning ("gpiv_fread_image: could not exec shell command \"%s\"",
232 command);
235 ext_ORG = g_strdup(ext);
236 g_free(ext);
237 ext = g_strdup(GPIV_EXT_PNG_IMAGE);
238 fname = g_strdup_printf("%s%s", fname_ext, ext);
242 * reading image data from raw, PNG, hdf or Davis(tm) format
244 if (strcmp (ext, GPIV_EXT_RAW_IMAGE) == 0
245 || strcmp (ext, GPIV_EXT_PNG_IMAGE) == 0
246 || strcmp (ext, GPIV_EXT_PNG_IMAGE_UPCASE) == 0
247 || strcmp (ext, GPIV_EXT_GPIV) == 0
248 || strcmp (ext, GPIV_EXT_GPIV_UPCASE) == 0
249 || strcmp (ext, GPIV_EXT_DAVIS) == 0
250 || strcmp (ext, GPIV_EXT_DAVIS_UPCASE) == 0) {
252 #ifdef DEBUG
253 g_message ("gpiv_fread_image:: my_utils_open_img");
254 #endif
255 if ((image = my_utils_open_img (fname)) == NULL) {
256 gpiv_warning ("gpiv_fread_image: failure open_img");
257 return NULL;
260 } else {
261 gpiv_warning ("gpiv_fread_image: image format not recognised");
262 return NULL;
266 * Remove original image if it has temporarly been converted to png
268 if (ext_ORG != NULL
269 && (strcmp(ext_ORG, GPIV_EXT_PGM_IMAGE) == 0
270 || strcmp(ext_ORG, ".pgm") == 0
271 || strcmp(ext_ORG, ".PGM") == 0
272 || strcmp(ext_ORG, ".gif") == 0
273 || strcmp(ext_ORG, ".GIF") == 0
274 || strcmp(ext_ORG, ".tif") == 0
275 || strcmp(ext_ORG, ".TIF") == 0
276 || strcmp(ext_ORG, ".bmp") == 0
277 || strcmp(ext_ORG, ".BMP") == 0
278 || strcmp(ext_ORG, ".ppm") == 0
279 || strcmp(ext_ORG, ".PPM") == 0
280 )) {
282 if (cleanup_tmp_image) {
283 g_snprintf (command, 2 * GPIV_MAX_CHARS, "rm %s%s",
284 fname_ext, GPIV_EXT_PNG_IMAGE);
285 } else {
286 g_snprintf (command, 2 * GPIV_MAX_CHARS, "rm %s ", fname_org);
288 if (system (command) != 0) {
289 gpiv_warning ("gpiv_fread_image: could not exec shell command \"%s\"",
290 command);
295 g_free (ext_ORG);
296 g_free (ext);
297 g_free (dirname);
298 g_free (fname_base);
299 g_free (fname_ext);
300 g_free (fname_org);
302 return image;
307 GpivImage *
308 gpiv_read_png_image (FILE *fp
310 /*---------------------------------------------------------------------------*/
312 * Reads png formatted image.
314 * @param[in] fp input file
315 * @return GpivImage
317 /*---------------------------------------------------------------------------*/
319 GpivImage *gpiv_image = NULL;
320 GpivImagePar *gpiv_image_par = g_new0 (GpivImagePar, 1);
322 char buf[PNG_BYTES_TO_CHECK];
324 png_structp png_ptr;
325 png_infop info_ptr, end_info_ptr;
326 unsigned int sig_read = 0;
327 png_uint_32 width, height;
328 gint bit_depth, color_type, interlace_type, compression_type,
329 unit_type_offs, filter_method;
331 gint row, column;
332 png_int_32 x_offset = 0, y_offset = 0;
333 double res_x = 0;
334 gint linesize;
335 png_byte **png_image = NULL;
336 png_byte *png_rowpointer;
339 /* @param[in] allocate boolean whether to allocate memory for image arrays
340 * @param[in] x_corr__set boolean if x_corr has been defined
341 * @param[in] x_corr boolean whether image contains two frames for cross-correlation.
342 * Will only be used if absent in header of input image and if
343 * x_corr__set = TRUE
346 #ifdef PNG_tIME_SUPPORTED
347 png_timep mod_time;
348 #endif /* PNG_tIME_SUPPORTED */
350 gpiv_img_parameters_set (gpiv_image_par, FALSE);
351 #ifdef DEBUG
352 g_message ("gpiv_read_png_image:: 00");
353 #endif
355 /* Read in some of the signature bytes */
356 if (fread (buf, 1, PNG_BYTES_TO_CHECK, fp) != PNG_BYTES_TO_CHECK) {
357 g_warning ("gpiv_read_png_image: failing fread (buf, 1, PNG_BYTES_TO_CHECK, fp)");
358 return NULL;
361 /* Testing the signature bytes */
362 if (png_sig_cmp ((png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK) != 0) {
363 g_warning ("gpiv_read_png_image: failing signature bytes test");
364 return NULL;
365 } else {
366 sig_read = 1;
369 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
370 png_voidp_NULL, png_error_ptr_NULL,
371 png_error_ptr_NULL);
372 if (png_ptr == NULL) {
373 g_warning ("gpiv_read_png_image: failing png_create_read_struct()");
374 return NULL;
377 info_ptr = png_create_info_struct (png_ptr);
378 if (info_ptr == NULL) {
379 fclose (fp);
380 png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL);
381 g_warning ("gpiv_read_png_image: failing png_create_info_struct (png_ptr)");
382 return NULL;
385 end_info_ptr = png_create_info_struct (png_ptr);
387 if (setjmp (png_jmpbuf (png_ptr))) {
388 /* Free all of the memory associated with the png_ptr and info_ptr */
389 /* If we get here, we had a problem reading the file */
390 png_destroy_read_struct (&png_ptr, &info_ptr,
391 /* NULL */ &end_info_ptr );
392 g_warning ("gpiv_read_png_image: failing setjmp (png_jmpbuf (png_ptr)");
393 return NULL;
396 png_init_io (png_ptr, fp);
398 /* If we have already read some of the signature */
399 if (sig_read == 1) {
400 png_set_sig_bytes (png_ptr, PNG_BYTES_TO_CHECK);
403 #ifdef HILEVEL
404 png_read_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, png_voidp_NULL);
405 #else
406 png_read_info (png_ptr, info_ptr);
407 #endif /* HILEVEL */
410 /* Quering image info: */
411 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
412 &interlace_type, &compression_type, &filter_method);
414 /* Other metadata chunks: */
415 #ifdef PNG_oFFs_SUPPORTED
416 png_get_oFFs (png_ptr, info_ptr, &x_offset, &y_offset, &unit_type_offs);
417 #endif /* PNG_oFFs_SUPPORTED */
419 #ifdef PNG_pCAL_SUPPORTED
420 res_x = png_get_x_pixels_per_meter (png_ptr, info_ptr);
421 #endif /* PNG_pCAL_SUPPORTED */
423 #ifdef PNG_tIME_SUPPORTED
424 png_get_tIME (png_ptr, info_ptr, &mod_time);
425 #endif /* PNG_tIME_SUPPORTED */
428 #ifdef PNG_TEXT_SUPPORTED
429 get_imgpar_from_textchunks (gpiv_image_par, png_ptr, info_ptr);
430 #endif /* PNG_TEXT_SUPPORTED */
432 /* if (gpiv_image_par.x_corr__set == FALSE) { */
433 /* g_warning ("GPIV_READ_PNG_IMAGE: couldn't read or x_corr"); */
434 /* return NULL; */
435 /* } */
437 #ifdef DEBUG
438 printf ("\n");
439 printf ("Image info:\nwidth = %d height = %d depth = %d \n",
440 (int)width,
441 (int)height,
442 (int)bit_depth
445 if (color_type == PNG_COLOR_TYPE_GRAY) {
446 printf ("color_type = PNG_COLOR_TYPE_GRAY\n");
447 } else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
448 printf ("color_type = PNG_COLOR_TYPE_GRAY_ALPHA\n");
449 } else if (color_type == PNG_COLOR_TYPE_PALETTE) {
450 printf ("color_type = PNG_COLOR_TYPE_PALETTE\n");
451 } else if (color_type == PNG_COLOR_TYPE_RGB) {
452 printf ("color_type = PNG_COLOR_TYPE_RGB\n");
453 } else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
454 printf ("color_type = PNG_COLOR_TYPE_RGB_ALPHA\n");
455 } else if (color_type == PNG_COLOR_MASK_PALETTE) {
456 printf ("color_type = PNG_COLOR_MASK_PALETTE\n");
457 } else if (color_type == PNG_COLOR_MASK_COLOR) {
458 printf ("color_type = PNG_COLOR_MASK_COLOR\n");
459 } else if (color_type == PNG_COLOR_MASK_ALPHA) {
460 printf ("color_type = PNG_COLOR_MASK_ALPHA\n");
461 } else {
462 g_warning ("gpiv_read_png_image: no existing color type");
463 return NULL;
466 if (compression_type == PNG_COMPRESSION_TYPE_DEFAULT) {
467 printf ("compression_type = PNG_COMPRESSION_TYPE_DEFAULT\n");
468 } else {
469 g_warning ("gpiv_read_png_image: no existing compression type");
470 return NULL;
473 if (interlace_type == PNG_INTERLACE_NONE) {
474 printf ("interlace_type = PNG_INTERLACE_NONE\n");
475 } else if (interlace_type == PNG_INTERLACE_ADAM7) {
476 printf ("interlace_type = PNG_INTERLACE_ADAM7\n");
477 } else {
478 g_warning ("gpiv_read_png_image: no existing interlace type");
479 return NULL;
483 #ifdef PNG_oFFs_SUPPORTED
484 printf ("x_offset = %d y_offset = %d\n",
485 (int)x_offset, (int)y_offset);
486 #endif /* PNG_oFFs_SUPPORTED */
488 #ifdef PNG_pCAL_SUPPORTED
489 printf ("resolution (pixels/meter) res_x = %d \n",
490 (int)res_x);
491 #endif
493 #ifdef PNG_tIME_SUPPORTED
494 printf ("Modification GMT time: (Y/M/D: h/m/s) %d/%d/%d: %d/%d/%d \n",
495 mod_time->year, mod_time->month, mod_time->day,
496 mod_time->hour, mod_time->minute, mod_time->second
498 #endif /* PNG_tIME_SUPPORTED */
500 printf ("\n");
501 #endif /* DEBUG */
505 * If the image is of correct format GRAY and has no INTERLACE, apply header
506 * info to gpiv_image structure
508 gpiv_image_par->depth = bit_depth;
509 gpiv_image_par->depth__set = TRUE;
511 if (color_type != PNG_COLOR_TYPE_GRAY) {
512 g_warning ("gpiv_read_png_image: image is not PNG_COLOR_TYPE_GRAY");
513 return NULL;
516 if (interlace_type != PNG_INTERLACE_NONE) {
517 g_warning ("gpiv_read_png_image: image is not PNG_INTERLACE_NONE");
518 return NULL;
521 if (info_ptr->bit_depth == 16) {
522 linesize = 2 * width;
523 } else {
524 linesize = width;
526 gpiv_image_par->ncolumns = width;
527 gpiv_image_par->ncolumns__set = TRUE;
530 #ifdef GPIV_IMG_PARAM_RESOURCES
531 #ifdef DEBUG
532 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, gpiv_image_par, TRUE);
533 #else
534 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, gpiv_image_par, FALSE);
535 #endif /* DEBUG */
536 #endif /* GPIV_IMG_PARAM_RESOURCES */
538 if (gpiv_image_par->x_corr__set == FALSE) {
539 g_warning ("gpiv_read_png_image: couldn't read or x_corr");
540 return NULL;
543 if (gpiv_image_par->x_corr) {
544 gpiv_image_par->nrows = height / 2;
545 } else {
546 gpiv_image_par->nrows = height;
548 gpiv_image_par->nrows__set = TRUE;
551 if (res_x != 0.0) {
552 gpiv_image_par->s_scale = res_x;
553 gpiv_image_par->s_scale__set = TRUE;
554 } else {
555 gpiv_image_par->s_scale__set = FALSE;
560 * Allocating (png) image
562 gpiv_image = gpiv_alloc_img (gpiv_image_par);
564 if ((png_image = matrix_png_byte (height, linesize)) == NULL) {
565 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info_ptr);
566 g_warning ("gpiv_read_png_image: couldn't allocate space for image");
567 return NULL;
572 * Obtaining image frame data
574 #ifdef HILEVEL
575 png_image = png_get_rows (png_ptr, info_ptr);
576 #else
577 png_read_image (png_ptr, png_image);
578 png_read_end (png_ptr, info_ptr);
579 #endif /* HILEVEL */
583 * Copy png image data to img1, img2
585 for (row = 0; row < gpiv_image->header->nrows; row++) {
586 #ifdef DEBUG2
587 printf ("\ngpiv_read_png_image: IMG1 row = %d img_png: \n", row);
588 #endif /* DEBUG2 */
589 png_rowpointer = png_image[row];
590 for (column = 0; column < gpiv_image->header->ncolumns; column++) {
591 gpiv_image->frame1[row][column] =
592 get_png_val (&png_rowpointer, bit_depth);
593 #ifdef DEBUG2
594 printf ("%d ", (int) gpiv_image->frame1[row][column]);
595 #endif /* DEBUG2 */
599 if (gpiv_image->header->x_corr) {
600 for (row = 0; row < gpiv_image->header->nrows; row++) {
601 #ifdef DEBUG2
602 printf ("\ngpiv_read_png_image: IMG2 row = %d img_png: \n",
603 row);
604 #endif /* DEBUG2 */
605 png_rowpointer = png_image[row + gpiv_image->header->nrows];
606 for (column = 0; column < gpiv_image->header->ncolumns;
607 column++) {
608 gpiv_image->frame2[row][column] =
609 get_png_val (&png_rowpointer, bit_depth);
610 #ifdef DEBUG2
611 printf ("%d ", (int) gpiv_image->frame2[row][column]);
612 #endif /* DEBUG2 */
618 * clean up after the read, and free any memory allocated
620 free_matrix_png_byte (png_image);
623 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info_ptr);
624 return gpiv_image;
629 gchar *
630 gpiv_fwrite_png_image (const gchar *fname,
631 GpivImage *gpiv_image,
632 const gboolean free
634 /*------------------------------------------------------------------------------
635 * DESCRIPTION:
636 * Writes image data and header to png formatted image.
637 * Frees allocated image data.
639 *--------------------------------------------------------------------------- */
641 FILE *fp = NULL;
642 gchar *err_msg = NULL;
645 if (fname == NULL) {
646 gpiv_warning ("gpiv_fwrite_png_image: failing \"fname == NULL\"");
647 return "gpiv_fwrite_png_image: failing \"fname == NULL\"";
650 if ((fp = fopen (fname, "wb")) == NULL) {
651 gpiv_warning ("LIBGPIV internal error: gpiv_fwrite_png_image: failure \
652 opening %s", fname);
653 return "gpiv_fwrite_png_image: failing fopen";
656 if ((err_msg = gpiv_write_png_image (fp, gpiv_image, free)) != NULL) {
657 fclose (fp);
658 return err_msg;
662 fclose (fp);
663 return NULL;
668 gchar *
669 gpiv_write_png_image (FILE *fp,
670 GpivImage *gpiv_image,
671 const gboolean free
673 /*------------------------------------------------------------------------------
674 * DESCRIPTION:
675 * Writes image data and header to png formatted image.
676 * Frees allocated image data.
678 *--------------------------------------------------------------------------- */
680 gchar *err_msg = NULL;
681 /* guint16 **img1, **img2; */
683 png_structp png_ptr;
684 png_infop info_ptr;
685 png_uint_32 width = gpiv_image->header->ncolumns, height;
686 gint bit_depth = gpiv_image->header->depth;
687 gint color_type = PNG_COLOR_TYPE_GRAY;
688 gint interlace_type = PNG_INTERLACE_NONE;
689 gint compression_type = PNG_COMPRESSION_TYPE_BASE;
690 gint filter_method = PNG_FILTER_TYPE_BASE;
691 gint linesize = 2 * width;
693 #ifdef PNG_tIME_SUPPORTED
694 time_t ltime_t;
695 png_time mod_time;
696 #endif /* PNG_tIME_SUPPORTED */
697 gint i = 0, j = 0;
700 if (gpiv_image->frame1 == NULL) {
701 err_msg = "gpiv_write_png_image: gpiv_image->frame1 == NULL";
702 return (err_msg);
704 if (gpiv_image->header->x_corr
705 && gpiv_image->frame2 == NULL) {
706 err_msg = "gpiv_write_png_image: gpiv_image->frame2 == NULL";
707 return (err_msg);
711 * Allocate png_ structures
713 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
714 png_voidp_NULL, png_error_ptr_NULL,
715 png_error_ptr_NULL);
716 if (png_ptr == NULL) {
717 err_msg = "gpiv_write_png_image: failing png_create_write_struct()";
718 return err_msg;
721 info_ptr = png_create_info_struct (png_ptr);
722 if (info_ptr == NULL) {
723 png_destroy_write_struct (&png_ptr, &info_ptr);
724 err_msg = "gpiv_write_png_image: failing png_create_info_struct (png_ptr)";
725 return err_msg;
728 if (setjmp (png_jmpbuf (png_ptr))) {
729 png_destroy_write_struct (&png_ptr, &info_ptr);
730 err_msg = "gpiv_write_png_image: failing at setjmp(png_jmpbuf (png_ptr)";
731 return err_msg;
735 png_init_io (png_ptr, fp);
738 * Apply gpiv_image->header to png header
740 if (gpiv_image->header->x_corr) {
741 height = 2 * gpiv_image->header->nrows;
742 } else {
743 height = gpiv_image->header->nrows;
746 png_set_compression_level (png_ptr, Z_BEST_COMPRESSION);
747 png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
748 interlace_type, compression_type, filter_method);
751 #ifdef PNG_pCAL_SUPPORTED
752 if (gpiv_image->header->s_scale__set) {
753 png_set_sCAL (png_ptr, info_ptr, PNG_SCALE_METER,
754 (png_int_32) gpiv_image->header->s_scale * width,
755 (png_int_32) gpiv_image->header->s_scale * height);
757 #endif /* PNG_pCAL_SUPPORTED */
759 #ifdef PNG_oFFs_SUPPORTED
760 if (gpiv_image->header->z_off_x__set
761 && gpiv_image->header->z_off_y__set) {
762 /* / gpiv_image->header.sscale * 1.0e3 */ /* x_offset*/
763 /* / gpiv_image->header.sscale * 1.0e3 */ /* y_offset*/
764 png_set_oFFs (png_ptr, info_ptr,
765 (png_uint_32) gpiv_image->header->z_off_x,
766 (png_uint_32) gpiv_image->header->z_off_y,
767 PNG_OFFSET_PIXEL);
769 #endif /* PNG_oFFs_SUPPORTED */
772 #ifdef PNG_tIME_SUPPORTED
773 ltime_t = time (&ltime_t);
774 png_convert_from_time_t (&mod_time, ltime_t);
775 png_set_tIME (png_ptr, info_ptr, &mod_time);
776 #endif /* PNG_tIME_SUPPORTED */
779 #ifdef PNG_TEXT_SUPPORTED
780 set_textchunks_from_imgpar (gpiv_image->header, png_ptr, info_ptr);
781 #endif /* PNG_TEXT_SUPPORTED */
783 png_write_info (png_ptr, info_ptr);
786 * Write image to png struct
788 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY) {
789 #ifdef WRITE_IMAGEDATA_LINE
790 png_byte *line;
791 #else
792 png_byte **png_image;
793 #endif /* WRITE_IMAGEDATA_LINE */
794 unsigned long p_png; /* (pi)xel */
795 png_byte *pp;
797 #ifdef WRITE_IMAGEDATA_LINE
798 if ((line = (png_byte *) g_malloc (linesize)) == NULL) {
799 png_destroy_write_struct (&png_ptr, &info_ptr);
800 err_msg = "gpiv_write_png_image: out of memory allocating PNG row buffer";
801 return (err_msg);
803 #else
804 if ((png_image = matrix_png_byte (gpiv_image->header->nrows, linesize))
805 == NULL) {
806 png_destroy_write_struct (&png_ptr, &info_ptr);
807 err_msg = "gpiv_write_png_image: out of memory allocating PNG row buffer";
808 return (err_msg);
810 #endif /* WRITE_IMAGEDATA_LINE */
813 for (i = 0; i < gpiv_image->header->nrows; i++) {
814 #ifdef WRITE_IMAGEDATA_LINE
815 pp = line;
816 #else
817 pp = png_image[i];
818 #endif /* WRITE_IMAGEDATA_LINE */
819 for (j = 0; j < width; j++) {
820 p_png = gpiv_image->frame1[i][j];
821 if (bit_depth == 16) *pp++ = PNM_GET1 (p_png) >> 8;
822 *pp++ = PNM_GET1 (p_png) & 0xff;
824 #ifdef WRITE_IMAGEDATA_LINE
825 png_write_row (png_ptr, line);
826 #endif /* WRITE_IMAGEDATA_LINE */
828 #ifndef WRITE_IMAGEDATA_LINE
829 png_write_rows (png_ptr, png_image, gpiv_image->header->nrows);
830 #endif /* WRITE_IMAGEDATA_LINE */
833 if (gpiv_image->header->x_corr) {
834 for (i = 0; i < gpiv_image->header->nrows; i++) {
835 #ifdef WRITE_IMAGEDATA_LINE
836 pp = line;
837 #else
838 pp = png_image[i];
839 #endif /* WRITE_IMAGEDATA_LINE */
840 for (j = 0; j < width; j++) {
841 p_png = gpiv_image->frame2[i][j];
842 if (bit_depth == 16) *pp++ = PNM_GET1 (p_png) >> 8;
843 *pp++ = PNM_GET1 (p_png) & 0xff;
845 #ifdef WRITE_IMAGEDATA_LINE
846 png_write_row (png_ptr, line);
847 #endif /* WRITE_IMAGEDATA_LINE */
849 #ifndef WRITE_IMAGEDATA_LINE
850 png_write_rows (png_ptr, png_image, gpiv_image->header->nrows);
851 #endif /* WRITE_IMAGEDATA_LINE */
855 #ifdef WRITE_IMAGEDATA_LINE
856 g_free (line);
857 #else
858 free_matrix_png_byte (png_image);
859 #endif /* WRITE_IMAGEDATA_LINE */
861 } else {
862 png_destroy_write_struct (&png_ptr, &info_ptr);
863 err_msg = "gpiv_write_png_image: color_type is not PNG_COLOR_TYPE_GRAY";
864 return (err_msg);
867 png_write_end (png_ptr, info_ptr);
868 fflush (fp);
871 * Freeing image memory
873 if (free == TRUE) {
874 gpiv_free_img (gpiv_image);
878 png_destroy_write_struct (&png_ptr, &info_ptr);
879 return err_msg;
883 * Local functions
885 static png_uint_16
886 get_png_val (png_byte **pp,
887 int bit_depth
889 /*-----------------------------------------------------------------------------
892 png_uint_16 c = 0;
895 if (bit_depth == 16) {
896 c = (*((*pp)++)) << 8;
898 c |= (*((*pp)++));
900 /* if (maxval > maxmaxval) */
901 /* c /= ((maxval + 1) / (maxmaxval + 1)); */
904 return c;
909 static png_byte **
910 matrix_png_byte (long nr,
911 long nc
913 /*-----------------------------------------------------------------------------
914 * DESCRIPTION:
915 * allocate a png_byte matrix with subscript range m[0..nr][0..nc]
917 * INPUTS:
918 * nr: number of rows
919 * nc: number of columns
921 * OUTPUTS:
923 * RETURNS:
924 * matrix
925 *---------------------------------------------------------------------------*/
927 long i;
928 png_byte **m;
931 m = (png_byte **) g_malloc0 (nr * sizeof (png_byte*));
932 m[0] = (png_byte *) g_malloc0 (nr * nc * sizeof (png_byte));
933 for (i = 1; i < nr; i++) {
934 m[i] = m[0] + i * nc;
938 return m;
943 static void
944 free_matrix_png_byte (png_byte **m
946 /*-----------------------------------------------------------------------------
947 * DESCRIPTION:
948 * free a png_byte matrix allocated by matrix_png_byte
950 * INPUTS:
951 * m: matrix
953 * OUTPUTS:
954 * m: NULL pointer
956 * RETURNS:
957 *---------------------------------------------------------------------------*/
959 g_return_if_fail (m[0] != NULL);
960 g_free (m[0]);
961 g_free (m);
962 m = NULL;
967 static void
968 get_imgpar_from_textchunks (GpivImagePar *image_par,
969 png_structp png_ptr,
970 png_infop info_ptr
972 /*------------------------------------------------------------------------------
973 * DESCRIPTION:
974 * Obtains image paramameters from text chunks in png image
976 * INPUTS:
977 * png_ptr: png structure
978 * info_ptr: png info structure
980 * OUTPUTS:
981 * image_par: image parameter structure
983 * RETURNS:
984 *--------------------------------------------------------------------------- */
986 png_textp text;
987 gint num_text = 0, i = 0;
988 png_uint_32 num_comments = 0;
991 num_comments = png_get_text (png_ptr, info_ptr, &text, &num_text);
993 for (i = 0; i < num_comments; i++) {
994 if (strcmp (text[i].key, PNG_KEY__PIV_XCORR) == 0) {
995 sscanf (text[i].text, "%d", &image_par->x_corr);
996 image_par->x_corr__set = TRUE;
999 if (strcmp (text[i].key, PNG_KEY__PIV_DT) == 0) {
1000 sscanf (text[i].text, "%f", &image_par->t_scale);
1001 image_par->t_scale__set = TRUE;
1005 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__TITLE) == 0) {
1006 g_snprintf (image_par->title, GPIV_MAX_CHARS, "%s", text[i].text);
1007 image_par->title__set = TRUE;
1010 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__AUTHOR) == 0) {
1011 g_snprintf (image_par->author, GPIV_MAX_CHARS, "%s", text[i].text);
1012 image_par->author__set = TRUE;
1015 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__USERTEXT) == 0) {
1016 g_snprintf (image_par->usertext, GPIV_MAX_CHARS, "%s", text[i].text);
1017 image_par->usertext__set = TRUE;
1020 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__SOFTWARE) == 0) {
1021 g_snprintf (image_par->software, GPIV_MAX_CHARS, "%s", text[i].text);
1022 image_par->software__set = TRUE;
1025 if (strcmp (text[i].key,GPIV_IMGPAR_KEY__DISCLAIMER ) == 0) {
1026 g_snprintf (image_par->disclaimer, GPIV_MAX_CHARS, "%s", text[i].text);
1027 image_par->disclaimer__set = TRUE;
1030 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__WARNING) == 0) {
1031 g_snprintf (image_par->warning, GPIV_MAX_CHARS, "%s", text[i].text);
1032 image_par->warning__set = TRUE;
1035 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__SOURCE) == 0) {
1036 g_snprintf (image_par->source, GPIV_MAX_CHARS, "%s", text[i].text);
1037 image_par->source__set = TRUE;
1040 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__COMMENT) == 0) {
1041 g_snprintf (image_par->comment, GPIV_MAX_CHARS, "%s", text[i].text);
1042 image_par->comment__set = TRUE;
1045 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__CREATION_DATE) == 0) {
1046 g_snprintf (image_par->creation_date, GPIV_MAX_CHARS, "%s", text[i].text);
1047 image_par->creation_date__set = TRUE;
1050 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__COPYRIGHT) == 0) {
1051 g_snprintf (image_par->copyright, GPIV_MAX_CHARS, "%s", text[i].text);
1052 image_par->copyright__set = TRUE;
1055 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__EMAIL) == 0) {
1056 g_snprintf (image_par->email, GPIV_MAX_CHARS, "%s", text[i].text);
1057 image_par->email__set = TRUE;
1060 if (strcmp (text[i].key, GPIV_IMGPAR_KEY__URL) == 0) {
1061 g_snprintf (image_par->url, GPIV_MAX_CHARS, "%s", text[i].text);
1062 image_par->url__set = TRUE;
1067 #ifdef DEBUG
1068 printf ("num_comments = %d\n", (int) num_comments);
1069 for (i = 0; i < num_comments; i++) {
1070 printf ("key[%d] = %s: text[%d] = %s\n",
1071 i, text[i].key, i, text[i].text);
1073 #endif /* DEBUG */
1080 static void
1081 set_textchunks_from_imgpar (GpivImagePar *image_par,
1082 png_structp png_ptr,
1083 png_infop info_ptr
1085 /*------------------------------------------------------------------------------
1086 * DESCRIPTION:
1087 * Set text chunks for png image from image paramameters
1089 * INPUTS:
1090 * image_par: image parameter structure
1092 * OUTPUTS:
1093 * png_ptr: png structure
1094 * info_ptr: png info structure
1096 * RETURNS:
1097 *--------------------------------------------------------------------------- */
1099 gint num_text = 0;
1100 png_text text[14];
1104 * Additional header text, needed for (G)PIV
1106 if (image_par->x_corr__set == TRUE) {
1107 text[num_text].key = PNG_KEY__PIV_XCORR;
1108 text[num_text].text = g_strdup_printf ("%d", image_par->x_corr);
1109 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1110 ++num_text;
1112 #ifdef PNG_iTXt_SUPPORTED
1113 text[0].lang = NULL;
1114 #endif
1116 if (image_par->t_scale__set == TRUE) {
1117 text[num_text].key = PNG_KEY__PIV_DT;
1118 text[num_text].text = g_strdup_printf ("%f", image_par->t_scale);
1119 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1120 ++num_text;
1122 #ifdef PNG_iTXt_SUPPORTED
1123 text[1].lang = NULL;
1124 #endif
1127 * non required header info
1128 * PNG recognized header text
1130 if (image_par->title__set == TRUE) {
1131 text[num_text].key = GPIV_IMGPAR_KEY__TITLE;
1132 text[num_text].text = image_par->title;
1133 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1134 ++num_text;
1136 #ifdef PNG_iTXt_SUPPORTED
1137 text[2].lang = NULL;
1138 #endif
1140 if (image_par->author__set == TRUE) {
1141 text[num_text].key = GPIV_IMGPAR_KEY__AUTHOR;
1142 text[num_text].text = image_par->author;
1143 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1144 ++num_text;
1146 #ifdef PNG_iTXt_SUPPORTED
1147 text[3].lang = NULL;
1148 #endif
1150 if (image_par->usertext__set == TRUE) {
1151 text[num_text].key = GPIV_IMGPAR_KEY__USERTEXT;
1152 text[num_text].text = image_par->usertext;
1153 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1154 ++num_text;
1156 #ifdef PNG_iTXt_SUPPORTED
1157 text[4].lang = NULL;
1158 #endif
1160 if (image_par->software__set == TRUE) {
1161 text[num_text].key = GPIV_IMGPAR_KEY__SOFTWARE;
1162 text[num_text].text = image_par->software;
1163 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1164 ++num_text;
1166 #ifdef PNG_iTXt_SUPPORTED
1167 text[5].lang = NULL;
1168 #endif
1170 if (image_par->disclaimer__set == TRUE) {
1171 text[num_text].key = GPIV_IMGPAR_KEY__DISCLAIMER;
1172 text[num_text].text = image_par->disclaimer;
1173 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1174 ++num_text;
1176 #ifdef PNG_iTXt_SUPPORTED
1177 text[6].lang = NULL;
1178 #endif
1180 if (image_par->warning__set == TRUE) {
1181 text[num_text].key = GPIV_IMGPAR_KEY__WARNING;
1182 text[num_text].text = image_par->warning;
1183 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1184 ++num_text;
1185 #ifdef PNG_iTXt_SUPPORTED
1186 text[7].lang = NULL;
1187 #endif
1190 if (image_par->source__set == TRUE) {
1191 text[num_text].key = GPIV_IMGPAR_KEY__SOURCE;
1192 text[num_text].text = image_par->source;
1193 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1194 ++num_text;
1196 #ifdef PNG_iTXt_SUPPORTED
1197 text[8].lang = NULL;
1198 #endif
1200 if (image_par->comment__set == TRUE) {
1201 text[num_text].key = GPIV_IMGPAR_KEY__COMMENT;
1202 text[num_text].text = image_par->comment;
1203 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1204 ++num_text;
1206 #ifdef PNG_iTXt_SUPPORTED
1207 text[9].lang = NULL;
1208 #endif
1210 if (image_par->creation_date__set == TRUE) {
1211 text[num_text].key = GPIV_IMGPAR_KEY__CREATION_DATE;
1212 text[num_text].text = image_par->creation_date;
1213 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1214 ++num_text;
1216 #ifdef PNG_iTXt_SUPPORTED
1217 text[10].lang = NULL;
1218 #endif
1221 if (image_par->copyright__set == TRUE) {
1222 text[num_text].key = GPIV_IMGPAR_KEY__COPYRIGHT;
1223 text[num_text].text = image_par->copyright;
1224 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1225 ++num_text;
1227 #ifdef PNG_iTXt_SUPPORTED
1228 text[11].lang = NULL;
1229 #endif
1231 if (image_par->email__set == TRUE) {
1232 text[num_text].key = GPIV_IMGPAR_KEY__EMAIL;
1233 text[num_text].text = image_par->email;
1234 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1235 ++num_text;
1237 #ifdef PNG_iTXt_SUPPORTED
1238 text[12].lang = NULL;
1239 #endif
1241 if (image_par->url__set == TRUE) {
1242 text[num_text].key = GPIV_IMGPAR_KEY__URL;
1243 text[num_text].text = image_par->url;
1244 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
1245 ++num_text;
1247 #ifdef PNG_iTXt_SUPPORTED
1248 text[13].lang = NULL;
1249 #endif
1252 png_set_text (png_ptr, info_ptr, text, num_text);
1255 #ifdef DEBUG
1256 #undef DEBUG
1257 #endif
1259 #ifdef DEBUG2
1260 #undef DEBUG2
1261 #endif
1263 GpivImage *
1264 gpiv_fread_hdf5_image (const gchar *fname
1266 /*-----------------------------------------------------------------------------
1267 * DESCRIPTION:
1268 * Opens an image from hdf file
1269 * Expects the arrray(s) have not been allocated yet
1271 *--------------------------------------------------------------------------- */
1273 GpivImage *gpiv_image = NULL;
1274 GpivImagePar *gpiv_image_par = g_new0 (GpivImagePar, 1);
1275 gchar *err_msg = NULL;
1276 int i, j, rank_x, rank_y, rank_vx, rank_vy;
1277 float *point_x_hdf = NULL, *point_y_hdf = NULL;
1280 * HDF declarations
1282 hid_t file_id, group_id, attribute_id, dataset_id,
1283 dataspace_id;
1284 hsize_t dims[2];
1285 herr_t status;
1289 if (fname == NULL) {
1290 gpiv_warning ("gpiv_fread_hdf5_image: failing \"fname == NULL\"");
1291 return NULL;
1294 if ((i = H5Fis_hdf5 (fname)) == 0) {
1295 gpiv_warning ("gpiv_fread_hdf5_image: not an hdf5 file");
1296 return NULL;
1300 gpiv_img_parameters_set (gpiv_image_par, FALSE);
1301 if ((gpiv_image_par = gpiv_img_fread_hdf5_parameters (fname))
1302 == NULL) {
1303 gpiv_warning ("gpiv_fread_hdf5_image: failing gpiv_img_fread_hdf5_parameters");
1304 return NULL;
1307 #ifdef GPIV_IMG_PARAM_RESOURCES
1308 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, &gpiv_image_par, TRUE);
1309 #endif
1310 if ((err_msg =
1311 gpiv_img_check_header_required_read (gpiv_image_par))
1312 != NULL) {
1313 gpiv_warning (err_msg);
1314 return NULL;
1317 gpiv_image = gpiv_alloc_img (gpiv_image_par);
1320 file_id = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
1321 group_id = H5Gopen (file_id, "IMAGE", H5P_DEFAULT);
1324 * image #1
1327 dataset_id = H5Dopen (group_id, "#1", H5P_DEFAULT);
1328 dataspace_id = H5Dget_space (dataset_id);
1329 #ifdef HD5_IMAGE_INT
1330 if ((status = H5Dread (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
1331 H5P_DEFAULT, gpiv_image->frame1[0])) < 0) {
1332 gpiv_warning ("gpiv_fread_hdf5_image: failing H5Dread");
1333 return NULL;
1335 #else
1336 if ((status = H5Dread(dataset_id, H5T_NATIVE_USHORT, H5S_ALL, H5S_ALL,
1337 H5P_DEFAULT, gpiv_image->frame1[0])) < 0) {
1338 gpiv_warning ("gpiv_fread_hdf5_image: failing H5Dread");
1339 return NULL;
1341 #endif
1342 /* msg_error = "H5Dread: no image data";
1343 * GpivIo
1344 * gpiv_io
1346 status = H5Sclose(dataspace_id);
1347 status = H5Dclose(dataset_id);
1350 * image #2
1352 if (gpiv_image->header->x_corr) {
1353 dataset_id = H5Dopen(group_id, "#2", H5P_DEFAULT);
1354 dataspace_id = H5Dget_space(dataset_id);
1355 #ifdef HD5_IMAGE_INT
1356 status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
1357 H5P_DEFAULT, gpiv_image->frame2[0]);
1358 #else
1359 status = H5Dread(dataset_id, H5T_NATIVE_USHORT, H5S_ALL, H5S_ALL,
1360 H5P_DEFAULT, gpiv_image->frame2[0]);
1361 #endif
1362 status = H5Sclose(dataspace_id);
1363 status = H5Dclose(dataset_id);
1364 } else {
1365 gpiv_image->frame2 = gpiv_image->frame1;
1370 H5Gclose (group_id);
1371 status = H5Fclose(file_id);
1373 return gpiv_image;
1377 gchar *
1378 gpiv_fwrite_hdf5_image (const gchar *fname,
1379 GpivImage *image,
1380 gboolean free
1382 /*-----------------------------------------------------------------------------
1383 * DESCRIPTION:
1384 * Writes IMAGE data to file in hdf version 5 format
1386 *---------------------------------------------------------------------------*/
1388 gchar *err_msg = NULL;
1389 gint i, j, k, line_nr=0, image_white_is_zero = 0;
1390 gfloat *point_x_hdf = NULL;
1392 * HDF file, dataset,-space identifier
1394 hid_t file_id, group_id, dataset_id, dataspace_id,
1395 attribute_id, atype;
1396 hsize_t dims[2];
1397 herr_t status;
1400 g_return_val_if_fail (image->frame1[0] != NULL, "frame1[0] != NULL");
1401 if (image->header->x_corr) g_return_val_if_fail (image->frame2[0] != NULL, "frame2[0] != NULL");
1405 if ((err_msg = gpiv_img_fwrite_hdf5_parameters (fname, image->header))
1406 != NULL) {
1407 return err_msg;
1411 if ((i = H5Fis_hdf5(fname)) == 0) {
1412 err_msg = "gpiv_fwrite_hdf5_image: not an hdf5 file";
1413 gpiv_warning("%s", err_msg);
1414 return err_msg;
1416 file_id = H5Fopen(fname, H5F_ACC_RDWR, H5P_DEFAULT);
1417 group_id = H5Gopen (file_id, "IMAGE", H5P_DEFAULT);
1418 /* H5Gset_comment (group_id, ".", RCSID); */
1421 * Hdf required image specifications
1423 dims[0] = 1;
1425 dataspace_id = H5Screate(H5S_SCALAR);
1426 atype = H5Tcopy(H5T_C_S1);
1427 H5Tset_size(atype, 5);
1428 attribute_id = H5Acreate(group_id, "CLASS", atype,
1429 dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1430 status = H5Awrite(attribute_id, atype,
1431 "IMAGE");
1432 status = H5Aclose(attribute_id);
1433 status = H5Sclose(dataspace_id);
1436 dataspace_id = H5Screate(H5S_SCALAR);
1437 atype = H5Tcopy(H5T_C_S1);
1438 H5Tset_size(atype, 15);
1439 attribute_id = H5Acreate(group_id, "SUBCLASS", atype,
1440 dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1441 status = H5Awrite(attribute_id, atype,
1442 "IMAGE_GRAYSCALE");
1443 status = H5Aclose(attribute_id);
1444 status = H5Sclose(dataspace_id);
1447 dataspace_id = H5Screate_simple(1, dims, NULL);
1448 attribute_id = H5Acreate(group_id, "IMAGE_WHITE_IS_ZERO",
1449 H5T_NATIVE_USHORT, dataspace_id,
1450 H5P_DEFAULT, H5P_DEFAULT);
1451 status = H5Awrite(attribute_id, H5T_NATIVE_USHORT, &image_white_is_zero);
1452 status = H5Aclose(attribute_id);
1453 status = H5Sclose(dataspace_id);
1456 dataspace_id = H5Screate(H5S_SCALAR);
1457 atype = H5Tcopy(H5T_C_S1);
1458 H5Tset_size(atype, 3);
1459 attribute_id = H5Acreate(group_id, "IMAGE_VERSION", atype,
1460 dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1461 status = H5Awrite(attribute_id, atype,
1462 "1.2");
1463 status = H5Aclose(attribute_id);
1464 status = H5Sclose(dataspace_id);
1468 * Create the data space.
1470 dims[0] = image->header->ncolumns;
1471 dims[1] = image->header->nrows;
1472 dataspace_id = H5Screate_simple(2, dims, NULL);
1476 * image #1
1478 #ifdef HD5_IMAGE_INT
1479 dataset_id = H5Dcreate (group_id, "#1", H5T_NATIVE_INT,
1480 dataspace_id, H5P_DEFAULT);
1481 status = H5Dwrite (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
1482 H5P_DEFAULT, image->frame1[0][0]);
1483 #else
1484 /* dataset_id = H5Dcreate (group_id, "#1", H5T_NATIVE_USHORT, */
1485 /* dataspace_id, H5P_DEFAULT); */
1486 /* status = H5Dwrite (dataset_id, H5T_NATIVE_USHORT, H5S_ALL, H5S_ALL, */
1487 /* H5P_DEFAULT, image->frame1[0][0]); */
1488 #endif
1489 status = H5Dclose (dataset_id);
1493 * image #2
1495 if (image->header->x_corr) {
1496 #ifdef HD5_IMAGE_INT
1497 dataset_id = H5Dcreate(group_id, "#2", H5T_NATIVE_INT,
1498 dataspace_id, H5P_DEFAULT);
1499 status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
1500 H5P_DEFAULT, &image->frame2[0][0]);
1501 #else
1502 /* dataset_id = H5Dcreate(group_id, "#2", H5T_NATIVE_USHORT, */
1503 /* dataspace_id, H5P_DEFAULT); */
1504 /* status = H5Dwrite(dataset_id, H5T_NATIVE_USHORT, H5S_ALL, H5S_ALL, */
1505 /* H5P_DEFAULT, &image->frame2[0][0]); */
1506 #endif
1507 status = H5Dclose(dataset_id);
1511 status = H5Sclose(dataspace_id);
1512 status = H5Gclose (group_id);
1513 status = H5Fclose(file_id);
1514 if (free == TRUE) gpiv_free_img (image);
1515 return err_msg;
1520 GpivImage *
1521 gpiv_read_davis_image (FILE *fp
1523 /*-----------------------------------------------------------------------------
1524 * DESCRIPTION:
1525 * Reads Davis formatted image, with ext .IMG from file
1527 * PROTOTYPE LOCATATION:
1528 * io.h
1530 * INPUTS:
1531 * fp: pointer to input file
1532 * RETURNS:
1533 * NULL on success or *err_msg on failure
1534 *---------------------------------------------------------------------------*/
1536 GpivImage *gpiv_image = NULL;
1537 GpivImagePar *gpiv_image_par = g_new0 (GpivImagePar, 1);
1538 gchar *err_msg = NULL;
1542 * Obtaining image dimensions
1544 fseek (fp, 10, SEEK_SET);
1545 fread (&gpiv_image_par->nrows, sizeof (guint16), 1, fp);
1546 fread (&gpiv_image_par->ncolumns, sizeof (guint16), 1, fp);
1549 * Obtaining other requitered parameters, like depth and x_corr.
1550 * These will definitely no be present in the image header.
1551 * So searching unconditionally.
1553 #ifdef DEBUG
1554 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, &gpiv_image_par, TRUE);
1555 #else
1556 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, &gpiv_image_par, FALSE);
1557 #endif /* DEBUG */
1559 if ((err_msg = gpiv_img_check_header_required_read (gpiv_image_par))
1560 != NULL) {
1561 gpiv_warning ("gpiv_read_davis_image: %s", err_msg);
1562 return NULL;
1566 * Reading image data
1568 gpiv_image = gpiv_alloc_img (gpiv_image_par);
1569 fseek (fp, 256, SEEK_SET);
1570 fread (gpiv_image->frame1[0], sizeof (guint16)
1571 * gpiv_image->header->ncolumns
1572 * gpiv_image->header->nrows, 1, fp);
1573 fread (gpiv_image->frame2[0], sizeof (guint16)
1574 * gpiv_image->header->ncolumns
1575 * gpiv_image->header->nrows, 1, fp);
1578 return gpiv_image;
1582 GpivImage *
1583 gpiv_read_raw_image (FILE *fp
1585 /*-----------------------------------------------------------------------------
1586 * DESCRIPTION:
1587 * Read raw image data from fp.
1588 * Frames memory will be allocated here.
1590 * INPUTS:
1591 * fname: pointer to complete filename to be written
1593 * RETURNS:
1594 * GpivImage on success or NULL on failure
1595 *---------------------------------------------------------------------------*/
1597 GpivImage *image = NULL;
1598 GpivImagePar *image_par = g_new0 (GpivImagePar, 1);
1599 gint i, j;
1600 gchar *err_msg = NULL;
1603 * Read header info. If required parameters are absent, scan from resource
1604 * files, check again if present, else bail out.
1606 gpiv_img_parameters_set (image_par, FALSE);
1607 gpiv_img_read_header (fp, image_par, FALSE, FALSE, NULL);
1609 #ifdef GPIV_IMG_PARAM_RESOURCES
1610 if ((err_msg = gpiv_img_check_header_required_read (image_par))
1611 != NULL) {
1612 #ifdef DEBUG
1613 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, image_par, TRUE);
1614 #else
1615 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, image_par, FALSE);
1616 #endif /* DEBUG */
1618 #endif /* GPIV_IMG_PARAM_RESOURCES */
1621 if ((err_msg = gpiv_img_check_header_required_read (image_par))
1622 != NULL) {
1623 gpiv_warning ("%s", err_msg);
1624 return NULL;
1628 * From header info memory allocation of image arrays can be done
1630 image = gpiv_alloc_img (image_par);
1631 read_raw_image_frames (fp, image);
1634 return image;
1638 GpivImage *
1639 gpiv_fread_raw_image (const gchar *fname
1641 /*-----------------------------------------------------------------------------
1642 * DESCRIPTION:
1643 * Reads an image from raw binary file.
1644 * Expects the arrray(s) have not been allocated yet
1645 * Reads image header data from file.h
1647 * INPUTS:
1648 * fname: file name
1650 * OUTPUTS:
1651 * image1_p: pointer to first 2-dimensional image array
1652 * image1_p: pointer to second 2-dimensional image array
1653 * image_par: pointer to image parameter structure
1655 * RETURNS:
1656 * GpivImage on success or NULL on failure
1657 *--------------------------------------------------------------------------- */
1659 GpivImage *image = NULL;
1660 GpivImagePar *image_par = g_new0 (GpivImagePar, 1);
1661 gchar *err_msg = NULL;
1662 gchar fname_header[GPIV_MAX_CHARS];
1663 gchar *dirname, *fname_base, *fname_nosuffix;
1664 FILE *fp;
1667 if (fname == NULL) {
1668 gpiv_warning ("gpiv_fread_raw_image: failing \"fname == NULL\"");
1669 return NULL;
1672 /* image->header = image_par; */
1673 gpiv_img_parameters_set (image_par, FALSE);
1676 * Stripping fname and create header filename
1678 dirname = g_strdup (g_path_get_dirname (fname));
1679 fname_base = g_strdup (g_path_get_basename (fname));
1680 strtok (fname_base, ".");
1681 fname_nosuffix = g_strdup (g_strconcat (dirname, G_DIR_SEPARATOR_S,
1682 fname_base, NULL));
1683 #ifdef DEBUG
1684 g_message ("gpiv_fread_raw_image: dirname = %s\n fname_base = %s\n fname_nosuffix = %s",
1685 dirname, fname_base, fname_nosuffix);
1686 #endif /* DEBUG */
1688 g_snprintf(fname_header, GPIV_MAX_CHARS, "%s%s",
1689 fname_nosuffix, GPIV_EXT_HEADER);
1691 #ifdef DEBUG
1692 g_message ("gpiv_fread_raw_image: image header is: %s", fname_header);
1693 #endif /* DEBUG */
1696 * Read header info. If required parameters are absent, scan from resource files,
1697 * check again if present, else bail out.
1699 if ((fp = fopen (fname_header, "rb")) == NULL) {
1700 gpiv_warning ("gpiv_fread_raw_image: failure opening %s for input", fname_header);
1701 return NULL;
1703 gpiv_img_read_header (fp, image_par, FALSE, FALSE, NULL);
1704 fclose (fp);
1707 #ifdef GPIV_IMG_PARAM_RESOURCES
1708 if ((err_msg = gpiv_img_check_header_required_read (image_par))
1709 != NULL) {
1710 #ifdef DEBUG
1711 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, image_par, TRUE);
1712 #else
1713 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, image_par, FALSE);
1714 #endif /* DEBUG */
1716 #endif /* GPIV_IMG_PARAM_RESOURCES */
1719 if ((err_msg = gpiv_img_check_header_required_read (image_par))
1720 != NULL) {
1721 gpiv_warning ("%s", err_msg);
1722 return NULL;
1727 * reads image data from file in binary format
1729 if ((fp = fopen (fname, "rb")) == NULL) {
1730 err_msg = "gpiv_fread_raw_image: failure opening for input";
1731 gpiv_warning ("%s", err_msg);
1732 return NULL;
1735 image = gpiv_alloc_img (image_par);
1736 read_raw_image_frames (fp, image);
1737 fclose (fp);
1740 g_free(dirname);
1741 g_free(fname_base);
1742 g_free(fname_nosuffix);
1743 return image;
1747 gchar *
1748 gpiv_write_raw_image (FILE *fp,
1749 GpivImage *image
1751 /*---------------------------------------------------------------------------
1752 * Writes raw binary image to fp
1754 *---------------------------------------------------------------------------*/
1756 gchar *err_msg = NULL;
1759 gpiv_img_print_header (fp, image->header);
1760 write_raw_image_frames (fp, image);
1763 return err_msg;
1767 gchar *
1768 gpiv_fwrite_raw_image (const gchar *fname,
1769 GpivImage *image
1771 /*-----------------------------------------------------------------------------
1772 * DESCRIPTION:
1773 * Writess binary image to file fname.r and header to fname.h
1775 *---------------------------------------------------------------------------*/
1777 gchar *err_msg = NULL;
1778 gchar fname_header[GPIV_MAX_CHARS];
1779 gchar *dirname, *fname_base, *fname_nosuffix;
1780 FILE *fp;
1783 if (fname == NULL) {
1784 gpiv_warning ("gpiv_fwrite_raw_image: failing \"fname == NULL\"");
1785 return NULL;
1788 g_return_if_fail (image->frame1[0] != NULL);
1789 g_return_if_fail (image->frame2[0] != NULL);
1794 * Stripping file name, create header filename and print header info
1796 dirname = g_strdup (g_path_get_dirname (fname));
1797 fname_base = g_strdup (g_path_get_basename (fname));
1798 strtok (fname_base, ".");
1799 fname_nosuffix = g_strdup (g_strconcat (dirname, G_DIR_SEPARATOR_S,
1800 fname_base, NULL));
1801 g_snprintf(fname_header, GPIV_MAX_CHARS, "%s%s",
1802 fname_nosuffix, GPIV_EXT_HEADER);
1804 if ((fp = fopen (fname_header, "wb")) == NULL) {
1805 gpiv_warning ("gpiv_fwrite_raw_image: failure opening %s for output",
1806 fname_header);
1807 return "PIV_FWRITE_RAW_IMAGE: failure opening header for output";
1810 gpiv_img_print_header (fp, image->header);
1811 fclose (fp);
1814 * Write image frames
1816 if ((fp = fopen (fname, "wb")) == NULL) {
1817 gpiv_warning ("gpiv_fwrite_raw_image: failure opening %s for output",
1818 fname);
1819 return "gpiv_fwrite_raw_image: failure opening image for output";
1822 write_raw_image_frames (fp, image);
1823 fclose (fp);
1826 return err_msg;
1832 * Local functions
1835 static void
1836 print_img_parameters_set (GpivImagePar *gpiv_image_par)
1838 printf ("ncolumns__set = %d \n", gpiv_image_par->ncolumns__set);
1839 printf ("nrows__set = %d \n", gpiv_image_par->nrows__set);
1840 printf ("x_corr__set = %d \n", gpiv_image_par->x_corr__set);
1841 printf ("depth__set = %d \n", gpiv_image_par->depth__set);
1842 printf ("s_scale__set = %d \n", gpiv_image_par->s_scale__set);
1843 printf ("t_scale__set = %d \n", gpiv_image_par->t_scale__set);
1844 printf ("z_off_x__set = %d \n", gpiv_image_par->z_off_x__set);
1845 printf ("z_off_y__set = %d \n", gpiv_image_par->z_off_y__set);
1846 printf ("title__set = %d \n", gpiv_image_par->title__set);
1847 printf ("creation_date__set = %d \n", gpiv_image_par->creation_date__set);
1848 printf ("location__set = %d \n", gpiv_image_par->location__set);
1849 printf ("author__set = %d \n", gpiv_image_par->author__set);
1850 printf ("software__set = %d \n", gpiv_image_par->software__set);
1851 printf ("source__set = %d \n", gpiv_image_par->source__set);
1852 printf ("usertext__set = %d \n", gpiv_image_par->usertext__set);
1853 printf ("warning__set = %d \n", gpiv_image_par->warning__set);
1854 printf ("disclaimer__set = %d \n", gpiv_image_par->disclaimer__set);
1855 printf ("comment__set = %d \n", gpiv_image_par->comment__set);
1856 printf ("copyright__set = %d \n", gpiv_image_par->copyright__set);
1857 printf ("email__set = %d \n", gpiv_image_par->email__set);
1858 printf ("url__set = %d \n", gpiv_image_par->url__set);
1862 static void
1863 read_raw_image_frames (FILE *fp,
1864 GpivImage *image
1866 /*-----------------------------------------------------------------------------
1867 * Read raw image data frames from fp.
1870 guint8 **lo_img1, **lo_img2;
1871 gint i, j;
1874 if (fp == stdin || fp == NULL) {
1875 if (image->header->depth <= 8) {
1876 lo_img1 = gpiv_matrix_guint8 (image->header->nrows,
1877 image->header->ncolumns);
1878 read (0, lo_img1[0], image->header->ncolumns
1879 * image->header->nrows * (sizeof(guint8)));
1881 if (image->header->x_corr) {
1882 lo_img2 = gpiv_matrix_guint8(image->header->nrows,
1883 image->header->ncolumns);
1884 read (0, lo_img2[0], image->header->ncolumns *
1885 image->header->nrows * (sizeof(guint8)));
1886 } else {
1887 lo_img2 = lo_img1;
1891 for (i = 0; i < image->header->nrows; i++) {
1892 for (j = 0; j < image->header->ncolumns; j++) {
1893 image->frame1[i][j] = lo_img1[i][j];
1894 image->frame2[i][j] = lo_img2[i][j];
1898 gpiv_free_matrix_guint8 (lo_img1);
1899 if (image->header->x_corr) {
1900 gpiv_free_matrix_guint8 (lo_img2);
1903 } else {
1904 read (0, image->frame1[0], image->header->ncolumns
1905 * image->header->nrows
1906 * (sizeof(guint16)));
1908 if (image->header->x_corr) {
1909 read (0, image->frame2[0], image->header->ncolumns
1910 * image->header->nrows
1911 * (sizeof(guint16)));
1912 } else {
1913 image->frame2 = image->frame1;
1919 } else {
1920 if (image->header->depth <= 8) {
1921 lo_img1 = gpiv_matrix_guint8 (image->header->nrows,
1922 image->header->ncolumns);
1923 fread (lo_img1[0], image->header->ncolumns
1924 * image->header->nrows * (sizeof(guint8)), 1, fp);
1926 if (image->header->x_corr) {
1927 lo_img2 = gpiv_matrix_guint8(image->header->nrows,
1928 image->header->ncolumns);
1929 fread (lo_img2[0], image->header->ncolumns *
1930 image->header->nrows * (sizeof(guint8)), 1, fp);
1931 } else {
1932 lo_img2 = lo_img1;
1936 for (i = 0; i < image->header->nrows; i++) {
1937 for (j = 0; j < image->header->ncolumns; j++) {
1938 image->frame1[i][j] = lo_img1[i][j];
1939 image->frame2[i][j] = lo_img2[i][j];
1943 gpiv_free_matrix_guint8 (lo_img1);
1944 if (image->header->x_corr) {
1945 gpiv_free_matrix_guint8 (lo_img2);
1949 } else {
1950 fread (image->frame1[0], image->header->ncolumns
1951 * image->header->nrows
1952 * (sizeof(guint16)), 1, fp);
1954 if (image->header->x_corr) {
1955 fread (image->frame2[0], image->header->ncolumns
1956 * image->header->nrows
1957 * (sizeof(guint16)), 1, fp);
1958 } else {
1959 image->frame2 = image->frame1;
1966 static void
1967 write_raw_image_frames (FILE *fp,
1968 GpivImage *image
1970 /*---------------------------------------------------------------------------
1971 * Writes raw binary image frames to fp
1974 guint ncolumns = image->header->ncolumns;
1975 guint nrows = image->header->nrows;
1978 if (fp == stdout || fp == NULL) {
1979 if (image->header->depth <= 8) {
1980 write (1, image->frame1[0], ncolumns * nrows * (sizeof (guint8)));
1982 if (image->header->x_corr) {
1983 write (1, image->frame2[0], ncolumns * nrows * (sizeof (guint8)));
1987 } else {
1988 write (1, image->frame1[0], ncolumns * nrows * (sizeof (guint16)));
1990 if (image->header->x_corr) {
1991 write (1, image->frame2[0], ncolumns * nrows * (sizeof (guint16)));
1995 } else {
1996 if (image->header->depth <= 8) {
1997 fwrite (image->frame1[0], ncolumns * nrows * (sizeof (guint8)), 1, fp);
1999 if (image->header->x_corr) {
2000 fwrite (image->frame2[0], ncolumns * nrows * (sizeof (guint8)), 1, fp);
2004 } else {
2005 fwrite (image->frame1[0], ncolumns * nrows * (sizeof (guint16)), 1,
2006 fp);
2008 if (image->header->x_corr) {
2009 fwrite (image->frame2[0], ncolumns * nrows * (sizeof (guint16)), 1,
2010 fp);
2017 * From gpiv_img_utils.h
2019 void
2020 gpiv_alloc_imgframe(GpivImage *image)
2021 /*-----------------------------------------------------------------------------
2022 * Allocates GpivImage frame data
2024 { image->frame1 = gpiv_matrix_guint16 (image->header->nrows,
2025 image->header->ncolumns);
2026 if (image->header->x_corr) {
2027 image->frame2 = gpiv_matrix_guint16 (image->header->nrows,
2028 image->header->ncolumns);
2029 } else {
2030 image->frame2 = image->frame1;
2035 GpivImage *
2036 gpiv_alloc_img (const GpivImagePar *image_par
2038 /*-----------------------------------------------------------------------------
2039 * DESCRIPTION:
2040 * Allocates memory for GpivImage frames
2042 * INPUTS:
2043 * image_par: image parameters
2045 * OUTPUTS:
2047 * RETURNS:
2048 * img: 2-dim image data arry
2049 *---------------------------------------------------------------------------*/
2051 GpivImage *image = g_new0 (GpivImage, 1);;
2052 GpivImagePar *image_par_dest = NULL;
2055 image_par_dest = gpiv_img_cp_parameters (image_par);
2056 image->header = image_par_dest;
2058 gpiv_alloc_imgframe (image);
2061 return image;
2066 gchar *
2067 gpiv_check_alloc_img (const GpivImage *image
2069 /*-----------------------------------------------------------------------------
2070 * Check if gpiv_image frames have been allocated
2073 gchar *err_msg = NULL;
2074 guint x;
2076 g_return_val_if_fail (image->frame1 != NULL,
2077 "gpiv_check_alloc_image: frame1 != NULL");
2078 g_return_val_if_fail (image->frame2 != NULL,
2079 "gpiv_check_alloc_image: frame2 != NULL");
2081 /* g_message ("gpiv_check_alloc_image: sizeof frame = %d", */
2082 /* sizeof (image->frame1)); */
2083 /* g_message ("gpiv_check_alloc_image: sizeof guint16 x nrows x ncolumns = %d x %d x %d = %d", */
2084 /* sizeof (guint16), */
2085 /* image->header->nrows, */
2086 /* image->header->ncolumns, */
2087 /* (sizeof (guint16)) * image->header->nrows * image->header->ncolumns */
2088 /* ); */
2090 return err_msg;
2094 GpivImage *
2095 gpiv_cp_img (const GpivImage *image
2097 /*-----------------------------------------------------------------------------
2098 * DESCRIPTION:
2099 * Duplicates img. The returned image will have to be freed with
2100 * gpiv_free_img when no longer needed.
2102 * INPUTS:
2103 * image_par: image parameters
2104 * img_src: 2-dim image data arry
2106 * OUTPUTS:
2107 * img_dest: 2-dim image data arry
2109 * RETURNS:
2110 *---------------------------------------------------------------------------*/
2112 gint i, j;
2113 GpivImage *image_dest = NULL;
2116 if (image->frame1[0] == NULL) {
2117 gpiv_warning ("gpiv_cp_img: image->frame1 not allocated");
2118 return NULL;
2121 if (image->header->x_corr__set == FALSE) {
2122 gpiv_warning ("gpiv_cp_img: x_corr not set");
2123 return NULL;
2126 if (image->header->x_corr) {
2127 if (image->frame2[0] == NULL) {
2128 gpiv_warning ("gpiv_cp_img: x_corr && image->frame2 not allocated");
2129 return NULL;
2134 image_dest = gpiv_alloc_img (image->header);
2137 for (i = 0; i < image_dest->header->nrows; i++) {
2138 for (j = 0; j < image_dest->header->ncolumns; j++) {
2139 image_dest->frame1[i][j] = image->frame1[i][j];
2143 if (image->header->x_corr) {
2144 for (i = 0; i < image_dest->header->nrows; i++) {
2145 for (j = 0; j < image_dest->header->ncolumns; j++) {
2146 image_dest->frame2[i][j] = image->frame2[i][j];
2149 } else {
2150 image_dest->frame2[0] = image->frame1[0];
2154 return image_dest;
2159 gchar *
2160 gpiv_cp_img_data (const GpivImage *image_src,
2161 GpivImage *image_dest
2163 /*-----------------------------------------------------------------------------
2164 * DESCRIPTION:
2165 * Copies contents of image_src to image_dest. image_src and image_dest
2166 * will have to be allocated with gpiv_alloc_img before and will have to
2167 * be freed with gpiv_free_img when no longer needed.
2169 * INPUTS:
2170 * image_par: image parameters
2171 * image_src: 2-dim image data arry
2173 * OUTPUTS:
2174 * image_dest: 2-dim image data arry
2176 * RETURNS:
2177 *---------------------------------------------------------------------------*/
2179 gchar *err_msg = NULL;
2180 gint i, j;
2183 if (image_src->frame1[0] == NULL) {
2184 err_msg = "gpiv_cp_img_data: image_src not allocated";
2185 gpiv_warning ("%s", err_msg);
2186 return err_msg;
2189 if (image_dest->frame1[0] == NULL) {
2190 err_msg = "gpiv_cp_img_data: image_dest not allocated";
2191 gpiv_warning ("%s", err_msg);
2192 return err_msg;
2195 if (image_src->header->x_corr__set == FALSE) {
2196 err_msg = "gpiv_cp_img_data: image_src: x_corr not set";
2197 gpiv_warning ("%s", err_msg);
2198 return err_msg;
2201 if (image_dest->header->x_corr__set == FALSE) {
2202 err_msg = "gpiv_cp_img_data: image_dest: x_corr not set";
2203 gpiv_warning ("%s", err_msg);
2204 return err_msg;
2207 if ((image_src->header->x_corr == FALSE
2208 && image_dest->header->x_corr == TRUE)
2209 || (image_src->header->x_corr == TRUE
2210 && image_dest->header->x_corr == FALSE)) {
2211 err_msg = "gpiv_cp_img_data: image_src and image_dest: x_corr unequal";
2212 gpiv_warning ("%s", err_msg);
2213 return err_msg;
2216 if (image_src->header->nrows != image_dest->header->nrows) {
2217 gpiv_warning ("gpiv_cp_img_data: unequal dimensions: image_src->header->nrows = %d image_dest->header->nrows = %d",
2218 image_src->header->nrows,
2219 image_dest->header->nrows);
2220 err_msg = "gpiv_cp_img_data: unequal dimensions of image_src and image_dest";
2221 return err_msg;
2224 if (image_src->header->ncolumns != image_dest->header->ncolumns) {
2225 gpiv_warning ("gpiv_cp_img_data: unequal dimensions: image_src->header->ncolumns = %d image_dest->header->ncolumns = %d",
2226 image_src->header->ncolumns,
2227 image_dest->header->ncolumns);
2228 err_msg = "gpiv_cp_img_data: unequal dimensions of image_src and image_dest";
2229 return err_msg;
2233 if (image_src->header->x_corr) {
2234 if (image_src->frame2[0] == NULL) {
2235 err_msg = "gpiv_cp_img_data: x_corr && image_src->frame2 not allocated";
2236 gpiv_warning ("%s", err_msg);
2237 return err_msg;
2240 if (image_dest->frame2[0] == NULL) {
2241 err_msg = "gpiv_cp_img_data: x_corr && image_dest->frame2 not allocated";
2242 gpiv_warning ("%s", err_msg);
2243 return err_msg;
2249 for (i = 0; i < image_src->header->nrows; i++) {
2250 for (j = 0; j < image_src->header->ncolumns; j++) {
2251 #ifdef DEBUG2
2252 g_message ("gpiv_cp_img_data:: frame1 i = %d j = %d", i, j);
2253 #endif
2254 image_dest->frame1[i][j] = image_src->frame1[i][j];
2258 if (image_src->header->x_corr) {
2259 for (i = 0; i < image_src->header->nrows; i++) {
2260 for (j = 0; j < image_src->header->ncolumns; j++) {
2261 #ifdef DEBUG2
2262 g_message ("gpiv_cp_img_data:: frame2 i = %d j = %d", i, j);
2263 #endif
2264 image_dest->frame2[i][j] = image_src->frame2[i][j];
2270 return NULL;
2275 void
2276 gpiv_free_img (GpivImage *image
2278 /*-----------------------------------------------------------------------------
2279 * DESCRIPTION:
2280 * Frees memory for image
2282 * INPUTS:
2283 * image_par: image parameters
2284 * img: 2-dim image data arry
2286 * OUTPUTS:
2288 * RETURNS:
2289 *---------------------------------------------------------------------------*/
2291 g_return_if_fail (image->frame1[0] != NULL); /* gpiv_error ("img not allocated"); */
2293 gpiv_free_matrix_guint16 (image->frame1);
2294 image->frame1 = NULL;
2296 if (image->header->x_corr == TRUE) {
2297 g_return_if_fail (image->frame2[0] != NULL); /* gpiv_error ("img not allocated"); */
2298 gpiv_free_matrix_guint16 (image->frame2);
2299 image->frame2 = NULL;
2304 #ifdef ENABLE_MPI
2305 void
2306 gpiv_img_mpi_bcast_image (GpivImage *image,
2307 const gboolean alloc_frame
2309 /*-----------------------------------------------------------------------------
2310 * Broadcasts image
2313 int rank = 0;
2316 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
2317 gpiv_img_mpi_bcast_imgpar (image->header);
2318 if (alloc_frame && rank != 0) gpiv_alloc_imgframe (image);
2319 gpiv_img_mpi_bcast_imgframe (image);
2321 #ifdef DEBUG
2322 my_utils_write_tmp_image (image,
2323 g_strdup_printf ("%s%d", "bcast_rank", rank),
2324 "Writing mpi-bcasted image to: ")
2325 #endif
2329 void
2330 gpiv_img_mpi_bcast_imgframe (GpivImage *image
2332 /*-----------------------------------------------------------------------------
2333 * Broadcasts image frame data
2336 guint size = image->header->ncolumns * image->header->nrows;
2339 if (MPI_Bcast(*image->frame1, size, MPI_SHORT, 0, MPI_COMM_WORLD)
2340 != MPI_SUCCESS
2342 gpiv_error("@&%#$!, mpi_bcast_img_frame: An error ocurred in MPI_Bcast");
2345 if (image->header->x_corr) {
2346 if (MPI_Bcast(*image->frame2, size, MPI_SHORT, 0, MPI_COMM_WORLD)
2347 != MPI_SUCCESS) {
2348 gpiv_error("@&%#$!, mpi_bcast_img_frame: An error ocurred in MPI_Bcast");
2354 #endif /* ENABLE_MPI */