1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*-----------------------------------------------------------------------------
5 gpiv - Graphic program for Particle Image Velocimetry, based on gtk/gnome
8 Copyright (C) 2007, 2008 Gerber van der Graaf
10 This file is part of gpiv.
12 Gpiv is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software Foundation,
24 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 -----------------------------------------------------------------------------*/
28 * Program wide input / output functions (no callbacks)
30 * Revision 1.1 2008-09-16 11:21:27 gerber
42 #include "display_image.h"
44 #define BYTES_TO_PROCESS 8192
47 select_action_from_uri (GpivConsole
*gpiv
,
51 select_action_from_fname (GpivConsole
*gpiv
,
55 open_img (char *fname_in
59 open_piv (char *fname_in
,
64 load_buffer (GpivConsole
*gpiv
,
69 enum ClistPut clist_put
73 write_from_tmp_to_uri (const gchar
*uri_string_tmp
,
74 const gchar
*uri_string_out
);
77 move_to_uri (GnomeVFSURI
*uri_out
,
78 const gchar
*fname_out
);
81 write_ascii_scalardata (GnomeVFSURI
*uri_out
,
82 GpivScalarData
*scalar_data
,
83 const char *fname_out_nosuf
,
88 * Program-wide public functions
92 * Retrieving functions
95 select_action_from_name (GpivConsole
*gpiv
,
97 /* ----------------------------------------------------------------------------
98 * Checks on validity of uri and whether local file or not.
99 * Calls apropiate functions.
104 GnomeVFSURI
* uri
= NULL
;
107 text_uri
= gnome_vfs_make_uri_from_shell_arg (name
);
108 uri
= gnome_vfs_uri_new (text_uri
);
110 if (gnome_vfs_uri_exists (uri
)) {
111 if (gnome_vfs_uri_is_local (uri
)) {
112 const gchar
*path
= gnome_vfs_uri_get_path (uri
);
113 select_action_from_fname (gpiv
, (gchar
*) path
);
115 if ((result
= select_action_from_uri (gpiv
, uri
)) != 0) {
116 g_error("select_action_from_name: select_action_from_uri");
121 warning_gpiv ("Uri or filename of input image does not exist");
131 write_hdf_img_data (const gchar
*fname_out_nosuf
,
132 GnomeVFSURI
*uri_out
)
133 /*------------------------------------------------------------------------------
136 char *err_msg
= NULL
;
137 gchar
*fname_out
= g_strdup_printf ("%s%s" , fname_out_nosuf
,
141 if (gpiv_par
->verbose
) g_message ("writing to %s", fname_out
);
144 gpiv_fcreate_hdf5 (fname_out
))
145 != NULL
) error_gpiv ("%s", err_msg
);
147 if (gpiv_par
->img_fmt
== IMG_FMT_HDF
148 && display_act
->img
->saved_img
== FALSE
) {
150 gpiv_img_fwrite_hdf5_parameters (fname_out
,
151 display_act
->img
->image
->header
))
152 != NULL
) error_gpiv ("%s", err_msg
);
155 gpiv_fwrite_hdf5_image (fname_out
, display_act
->img
->image
, FALSE
))
156 != NULL
) error_gpiv ("%s", err_msg
);
157 display_act
->img
->saved_img
= TRUE
;
161 if (display_act
->pida
->exist_piv
) {
162 if (display_act
->pida
->scaled_piv
) {
164 gpiv_fwrite_hdf5_pivdata (fname_out
,
165 display_act
->pida
->piv_data_scaled
,
168 != NULL
) error_gpiv ("%s", err_msg
);
172 gpiv_fwrite_hdf5_pivdata (fname_out
,
173 display_act
->pida
->piv_data
,
176 != NULL
) error_gpiv ("%s", err_msg
);
180 display_act
->pida
->saved_piv
= TRUE
;
183 gpiv_piv_fwrite_hdf5_parameters (fname_out
,
184 display_act
->pida
->piv_par
))
185 != NULL
) error_gpiv ("%s", err_msg
);
189 if (display_act
->pida
->exist_valid
) {
191 gpiv_valid_fwrite_hdf5_parameters (fname_out
,
192 display_act
->pida
->valid_par
))
193 != NULL
) error_gpiv ("%s", err_msg
);
197 if (display_act
->pida
->exist_vor
) {
198 if (display_act
->pida
->scaled_piv
) {
200 gpiv_fwrite_hdf5_scdata (fname_out
,
201 display_act
->pida
->vor_data_scaled
,
204 != NULL
) error_gpiv ("%s", err_msg
);
207 gpiv_fwrite_hdf5_scdata (fname_out
,
208 display_act
->pida
->vor_data
,
211 != NULL
) error_gpiv ("%s", err_msg
);
213 display_act
->pida
->saved_vor
= TRUE
;
217 if (display_act
->pida
->exist_sstrain
) {
218 if (display_act
->pida
->scaled_piv
) {
220 gpiv_fwrite_hdf5_scdata (fname_out
,
221 display_act
->pida
->sstrain_data_scaled
,
224 != NULL
) error_gpiv ("%s", err_msg
);
227 gpiv_fwrite_hdf5_scdata (fname_out
,
228 display_act
->pida
->sstrain_data
,
231 != NULL
) error_gpiv ("%s", err_msg
);
233 display_act
->pida
->saved_sstrain
= TRUE
;
237 if (display_act
->pida
->exist_nstrain
) {
238 if (display_act
->pida
->scaled_piv
) {
240 gpiv_fwrite_hdf5_scdata (fname_out
,
241 display_act
->pida
->nstrain_data_scaled
,
244 != NULL
) error_gpiv ("%s", err_msg
);
247 gpiv_fwrite_hdf5_scdata (fname_out
,
248 display_act
->pida
->nstrain_data
,
251 != NULL
) error_gpiv ("%s", err_msg
);
253 display_act
->pida
->saved_nstrain
= TRUE
;
258 gpiv_post_fwrite_hdf5_parameters (fname_out
,
259 display_act
->pida
->post_par
))
260 != NULL
) error_gpiv ("%s", err_msg
);
263 move_to_uri (uri_out
, fname_out
);
270 write_img (const gchar
*fname_out_nosuf
,
271 GnomeVFSURI
*uri_out
)
272 /*------------------------------------------------------------------------------
273 * Store image to RAW, HDF or PNG format
276 char *err_msg
= NULL
;
281 if (display_act
->img
->saved_img
== FALSE
) {
282 if (gpiv_par
->img_fmt
== IMG_FMT_PNG
) {
283 fname_out
= g_strdup_printf ("%s%s" ,
284 replace_tilde_with_home_dir (fname_out_nosuf
),
287 if ((fp_out
= fopen (fname_out
, "wb")) == NULL
) {
289 error_gpiv ("gpiv: write_img: fopen (%s, \"wb\")) == NULL",
292 if ((err_msg
= gpiv_write_png_image (fp_out
,
293 display_act
->img
->image
,
296 gpiv_error ("gpiv: write_img: %s\n", err_msg
);
299 move_to_uri (uri_out
, fname_out
);
302 } else if (gpiv_par
->img_fmt
== IMG_FMT_HDF
) {
303 fname_out
= g_strdup_printf ("%s%s" ,
304 replace_tilde_with_home_dir (fname_out_nosuf
),
306 if (gpiv_par
->verbose
) g_message ("writing image to %s", fname_out
);
308 gpiv_fcreate_hdf5 (fname_out
))
309 != NULL
) error_gpiv ("%s", err_msg
);
311 gpiv_img_fwrite_hdf5_parameters (fname_out
,
312 display_act
->img
->image
->header
))
313 != NULL
) error_gpiv ("%s", err_msg
);
315 gpiv_fwrite_hdf5_image (fname_out
,
316 display_act
->img
->image
,
318 != NULL
) error_gpiv ("%s", err_msg
);
319 move_to_uri (uri_out
, fname_out
);
322 } else if (gpiv_par
->img_fmt
== IMG_FMT_RAW
) {
324 * Saving ASCII header
327 gchar
*fname_header
= g_strdup_printf ("%s%s" ,
328 replace_tilde_with_home_dir (fname_out_nosuf
),
330 if (gpiv_par
->verbose
) g_message ("writing header to %s", fname_header
);
331 if ((fp_header
= fopen (fname_header
, "w")) == NULL
) {
332 error_gpiv (_("Failure opening image header for output"));
334 /* fprintf(fp_par, "#%s\n", gl_image_par->software);
336 gpiv_img_print_header (fp_header
, display_act
->img
->image
->header
);
338 move_to_uri (uri_out
, fname_header
);
339 g_free (fname_header
);
341 * BUGFIX: Saving RAW image
344 display_act
->img
->saved_img
= TRUE
;
351 write_ascii_parameters (const char *fname_out_nosuf
,
352 GnomeVFSURI
*uri_out
,
354 /*-----------------------------------------------------------------------------
355 * Saves parameters in ascii format
360 gchar
*fname
= g_strdup_printf ("%s%s" ,
361 replace_tilde_with_home_dir (fname_out_nosuf
),
365 if ((fp
= fopen (fname
, "w")) == NULL
) {
366 error_gpiv (_("Failure opening parameter file for output"));
370 * Saving image header
372 if (gl_image_par
->software__set
) {
373 fprintf (fp
, "# Software: %s\n", gl_image_par
->software
);
375 gpiv_img_print_header (fp
, display_act
->img
->image
->header
);
378 * Saving PIV parameters
380 if (display_act
->pida
->exist_piv
) {
381 gpiv_piv_print_parameters (fp
, display_act
->pida
->piv_par
);
382 if (display_act
->pida
->exist_valid
) {
383 gpiv_valid_print_parameters (fp
, display_act
->pida
->valid_par
);
389 * Saving scalar parameters for vorticity
391 if (display_act
->pida
->exist_vor
) {
393 gpiv_post_print_parameters (stdout
, display_act
->pida
->post_par
);
394 gpiv_post_print_parameters (fp
, display_act
->pida
->post_par
);
400 * Saving scalar parameters for shear strain
402 if (display_act
->pida
->exist_sstrain
) {
404 gpiv_post_print_parameters (fp
, display_act
->pida
->post_par
);
410 * Saving scalar parameters for normal strain
412 if (display_act
->pida
->exist_nstrain
) {
414 gpiv_post_print_parameters (fp
, display_act
->pida
->post_par
);
420 move_to_uri (uri_out
, fname
);
427 write_ascii_data (const gchar
*fname_out_nosuf
,
428 GnomeVFSURI
*uri_out
)
429 /*------------------------------------------------------------------------------
430 * Store resulting (PIV and derived) data in ASCII format at
434 char *err_msg
= NULL
;
442 if (display_act
->pida
->exist_piv
) {
443 fname
= g_strdup_printf ("%s%s",
444 replace_tilde_with_home_dir (fname_out_nosuf
),
447 if ((fp
= fopen (fname
, "wb")) == NULL
) {
448 warning_gpiv ("write_ascii_data: failing fopen %s", fname
);
452 if (display_act
->pida
->scaled_piv
) {
454 gpiv_write_pivdata (fp
,
455 display_act
->pida
->piv_data_scaled
,
457 != NULL
) error_gpiv ("%s", err_msg
);
460 gpiv_write_pivdata (fp
,
461 display_act
->pida
->piv_data
,
463 != NULL
) error_gpiv ("%s", err_msg
);
467 display_act
->pida
->saved_piv
= TRUE
;
468 move_to_uri (uri_out
, fname
);
475 if (display_act
->pida
->exist_histo
) {
476 fname
= g_strdup_printf ("%s%s" ,
477 replace_tilde_with_home_dir (fname_out_nosuf
),
479 if (gpiv_par
->verbose
) g_message ("writing HISTO data to %s", fname
);
481 if ((fp
= fopen (fname
, "wb")) == NULL
) {
482 warning_gpiv ("write_ascii_data: failing fopen %s", fname
);
486 gpiv_print_histo (fp
,
487 display_act
->pida
->klass
,
491 display_act
->pida
->saved_histo
= TRUE
;
492 move_to_uri (uri_out
, fname
);
497 * Saving scalar vorticity data
499 if (display_act
->pida
->exist_vor_scaled
) {
500 write_ascii_scalardata (uri_out
, display_act
->pida
->vor_data_scaled
,
501 fname_out_nosuf
, GPIV_EXT_VOR
);
502 display_act
->pida
->saved_vor
= TRUE
;
503 } else if (display_act
->pida
->exist_vor
) {
504 write_ascii_scalardata (uri_out
, display_act
->pida
->vor_data
,
505 fname_out_nosuf
, GPIV_EXT_VOR
);
506 display_act
->pida
->saved_vor
= TRUE
;
511 * Saving scalar shear strain data
513 if (display_act
->pida
->exist_sstrain_scaled
) {
514 write_ascii_scalardata (uri_out
, display_act
->pida
->sstrain_data_scaled
,
515 fname_out_nosuf
, GPIV_EXT_SSTR
);
516 display_act
->pida
->saved_sstrain
= TRUE
;
517 } else if (display_act
->pida
->exist_sstrain
) {
518 write_ascii_scalardata (uri_out
, display_act
->pida
->sstrain_data
,
519 fname_out_nosuf
, GPIV_EXT_SSTR
);
520 display_act
->pida
->saved_sstrain
= TRUE
;
524 * Saving scalar normal strain data
526 if (display_act
->pida
->exist_nstrain_scaled
) {
527 write_ascii_scalardata (uri_out
, display_act
->pida
->nstrain_data_scaled
,
528 fname_out_nosuf
, GPIV_EXT_NSTR
);
529 display_act
->pida
->saved_nstrain
= TRUE
;
530 } else if (display_act
->pida
->exist_nstrain
) {
531 write_ascii_scalardata (uri_out
, display_act
->pida
->nstrain_data
,
532 fname_out_nosuf
, GPIV_EXT_NSTR
);
533 display_act
->pida
->saved_nstrain
= TRUE
;
541 * Local functions on retrieving data
545 select_action_from_uri (GpivConsole
*gpiv
,
547 /* ----------------------------------------------------------------------------
548 * checks filename extension on valid image/data name, on existence of buffer,
550 * As i/o from libgpiv (which does not have libgnomevfs) will have to be
551 * be used (applying libpng funcionality), the image (data) are copied to a
552 * tmp directory and subsequtially be loaded with libgpiv i/o
556 GnomeVFSHandle
*read_handle
, *write_handle
;
557 GnomeVFSResult result
, result_while
;
558 GnomeVFSFileSize bytes_read
, bytes_written
;
559 guint buffer
[BYTES_TO_PROCESS
];
560 gchar
*output_uri_string
;
562 const gchar
*tmp_dir
= g_get_tmp_dir ();
563 const gchar
*user_name
= g_get_user_name ();
566 gchar
*dirname
= gnome_vfs_uri_extract_dirname (uri
);
567 gchar
*basename
= gnome_vfs_uri_extract_short_name (uri
);
571 if (gpiv_par
->verbose
) {
572 g_message ("select_action_from_uri:: tmp_dir = %s", tmp_dir
);
573 g_message ("select_action_from_uri:: user_name = %s", user_name
);
574 g_message ("select_action_from_uri:: dirname = %s basename = %s",
578 output_uri_string
= g_strdup_printf ("%s/%s/%s" ,tmp_dir
,
579 user_name
, basename
);
580 if (gpiv_par
->verbose
) {
581 g_message ("select_action_from_uri:: output_uri_string = %s",
585 if ((result
= gnome_vfs_open_uri (&read_handle
, uri
, GNOME_VFS_OPEN_READ
))
587 gpiv_error ("select_action_from_uri: gnome_vfs_open_uri");
589 if ((result
= gnome_vfs_create (&write_handle
, output_uri_string
,
590 GNOME_VFS_OPEN_WRITE
, FALSE
, 0777))
591 != GNOME_VFS_OK
) gpiv_error ("select_action_from_uri: gnome_vfs_create");
593 /* read data from the input uri */
594 while (result
== GNOME_VFS_OK
) {
595 result
= gnome_vfs_read (read_handle
, buffer
, BYTES_TO_PROCESS
,
598 if ((result_while
= gnome_vfs_seek (write_handle
, GNOME_VFS_SEEK_END
, 0))
600 gpiv_error ("select_action_from_uri: gnome_vfs_seek");
602 if ((result_while
= gnome_vfs_write (write_handle
, buffer
, bytes_read
,
605 gpiv_error ("select_action_from_uri: gnome_vfs_write");
609 * add to uri to fname_last;
610 * Reading image from temporarly storage
611 * put correct name in Image tab
614 gpiv_var
->fname_last
= gnome_vfs_uri_to_string (uri
, TRUE
);
615 gnome_config_push_prefix ("/gpiv/RuntimeVariables/");
616 gnome_config_set_string ("fname_last", gpiv_var
->fname_last
);
617 gnome_config_pop_prefix ();
618 gnome_config_sync ();
619 push_list_lastfnames (gpiv_var
->fname_last
);
621 gpiv_var
->fname_last__set
= TRUE
;
622 select_action_from_fname (gpiv
, output_uri_string
);
623 gpiv_var
->fname_last__set
= FALSE
;
625 display_act
->file_uri_name
=
626 g_strdup_printf ("%s",
627 strtok (gnome_vfs_uri_to_string (uri
, TRUE
), "."));
629 update_imgh_entries (gpiv
, display_act
->img
->image
->header
);
631 command
= g_strdup_printf ("rm %s", output_uri_string
);
632 if (gpiv_par
->verbose
) {
633 g_message ("command = %s", command
);
635 if (system (command
) != 0) {
636 g_warning ("select_action_from_uri: could not exec shell command: %s",
641 g_free (output_uri_string
);
650 select_action_from_fname (GpivConsole
*gpiv
,
652 /* ----------------------------------------------------------------------------
653 * checks filename suffux on valid image/data name, on existence of buffer,
657 gchar
*err_msg
= NULL
;
658 gint ibuf
, return_val
;
659 gchar
*suf
, /* filename suffix */
660 *dirname
, /* directory name */
661 *fname_base
, /* Image name without suffix and dirname */
662 *fname_nosuf
, /* Image name without suffix */
664 gboolean exist_buf
= FALSE
;
665 gchar command
[2 * GPIV_MAX_CHARS
];
666 /* gchar *command; */
669 suf
= g_strdup (strrchr (fname_in
, '.'));
670 /* add to push_list_lastfnames (fname_base); */
671 if (!gpiv_var
->fname_last__set
) {
672 gpiv_var
->fname_last
= g_strdup (fname_in
);
673 gnome_config_push_prefix ("/gpiv/RuntimeVariables/");
674 gnome_config_set_string ("fname_last", gpiv_var
->fname_last
);
675 gnome_config_pop_prefix ();
676 gnome_config_sync ();
679 * Stripping file name
680 * We need them for displaying in the image header tab and on the buffer list
682 fname_home
= replace_home_dir_with_tilde (fname_in
);
683 dirname
= g_strdup (g_path_get_dirname (fname_home
));
684 /* (replace_home_dir_with_tilde (fname_in))); */
686 fname_base
= g_strdup (g_path_get_basename (fname_in
));
687 strtok (fname_base
, ".");
688 if (!gpiv_var
->fname_last__set
) {
689 push_list_lastfnames (fname_base
);
691 fname_nosuf
= g_strdup (g_strconcat (dirname
, G_DIR_SEPARATOR_S
,
695 * reading image data from raw, PNG, hdf or Davis(tm) format
696 * Ensure that the tabulator widgets are enabled
698 if (g_str_has_suffix (fname_in
, GPIV_EXT_RAW_IMAGE
)
699 || g_str_has_suffix (fname_in
, GPIV_EXT_PNG_IMAGE
)
700 || g_str_has_suffix (fname_in
, GPIV_EXT_PNG_IMAGE_UPCASE
)
701 || g_str_has_suffix (fname_in
, GPIV_EXT_GPIV
)
702 || g_str_has_suffix (fname_in
, GPIV_EXT_DAVIS
)
703 || g_str_has_suffix (fname_in
, GPIV_EXT_PGM_IMAGE
)
704 || g_str_has_suffix (fname_in
, GPIV_EXT_PGM_IMAGE_UPCASE
)
705 || g_str_has_suffix (fname_in
, ".gif")
706 || g_str_has_suffix (fname_in
, ".GIF")
707 || g_str_has_suffix (fname_in
, ".tif")
708 || g_str_has_suffix (fname_in
, ".TIF")
709 || g_str_has_suffix (fname_in
, ".bmp")
710 || g_str_has_suffix (fname_in
, ".BMP")
716 sensitive (gpiv
, IMG
, TRUE
);
717 sensitive (gpiv
, EVAL
, TRUE
);
721 * Check if file basename (and buffer/display) already exists
722 * or if there is a gap in the numbering of the buffers
724 for (ibuf
= 0; ibuf
< nbufs
; ibuf
++) {
725 display_act
= gtk_clist_get_row_data (GTK_CLIST (gpiv
->clist_buf
),
727 if (ibuf
== display_act
->id
728 && strcmp (fname_nosuf
, display_act
->file_uri_name
) == 0 ) {
729 if ((display_act
->img
->image
= open_img (fname_in
)) == NULL
) {
736 } else if (ibuf
< display_act
->id
) {
738 load_buffer (gpiv
, fname_in
, fname_base
, fname_nosuf
,
739 ibuf
, INSERT
)) != NULL
) {
740 g_warning ("select_action_from_fname: failing load_buffer");
747 /* g_warning ("select_action_from_fname: should not arrive here"); */
753 * All buffers with id between 0 and nbufs already exist.
754 * Create a new buffer and launches a display if file basename doesn't exist
755 * and append to the list
760 load_buffer (gpiv
, fname_in
, fname_base
, fname_nosuf
,
765 warning_gpiv (_("select_action_from_fname: non-valid file name"));
768 gtk_clist_set_selection_mode (GTK_CLIST (gpiv
->clist_buf
),
769 GTK_SELECTION_EXTENDED
);
771 #ifdef ENABLE_IMGPROC
772 set_imgproc_filtervar (gpiv
, GPIV_IMGFI_SUBACK
, nbufs
- 1);
778 g_free (fname_nosuf
);
784 open_img (char *fname_in
786 /*-----------------------------------------------------------------------------
787 * Opens an image from png, hdf or raw-data file (<=> open_dac_img)
790 GpivImage
*image
= NULL
;
791 gchar
*err_msg
= NULL
;
794 if ((image
= gpiv_fread_image (fname_in
)) == NULL
) {
795 warning_gpiv ("OPEN_IMG: failing gpiv_fread_image %s", fname_in
);
799 display_act
->img
->exist_img
= TRUE
;
802 * The loaded image has not yet been modified, so there is no need to save
803 * image or header information
805 display_act
->img
->saved_img
= TRUE
;
814 open_piv (char *fname_in
,
817 /*-----------------------------------------------------------------------------
818 * Opens piv data file and displays
821 char *err_msg
= NULL
, RCSID_DATA
[GPIV_MAX_CHARS
];
822 char *ext
= g_strdup (strrchr (fname_in
, '.'));
823 gint var_scale
= 0, scale
= 0;
827 * parameter initializing of piv data
829 /* pida->piv_par->autokey = 1; */
830 gpiv_piv_parameters_set (pida
->piv_par
, FALSE
);
835 if (strcmp (ext
, GPIV_EXT_GPIV
) == 0) {
836 if ((pida
->piv_data
=
837 gpiv_fread_hdf5_pivdata (fname_in
, "PIV"))
839 warning_gpiv ("OPEN_PIV: failing piv_fread_hdf5_pivdata %s",
843 pida
->exist_piv
= TRUE
;
844 pida
->averaged_piv
= FALSE
;
848 * Reads raw data format
850 } else if (strcmp (ext
, GPIV_EXT_RAW_IMAGE
) == 0
851 || strcmp (ext
, GPIV_EXT_DAVIS
) == 0
854 gchar
*dirname
= g_strdup (g_dirname (fname_in
)),
855 *fname_base
= g_strdup (g_basename (fname_in
)),
860 * check on existance of piv data file
862 strtok (fname_base
, ".");
863 fname_ext
= g_strdup (g_strconcat (dirname
, G_DIR_SEPARATOR_S
,
865 fname_piv
= g_strdup (g_strconcat (fname_ext
, GPIV_EXT_PIV
, NULL
));
867 if ((fp
= fopen (fname_piv
, "r")) == NULL
) {
868 warning_gpiv ("OPEN_PIV: failing fopen %s", fname_piv
);
871 if ((pida
->piv_data
= gpiv_read_pivdata (fp
)) == NULL
) {
872 warning_gpiv ("OPEN_PIV: failing gpiv_read_pivdata");
877 pida
->exist_piv
= TRUE
;
878 pida
->averaged_piv
= FALSE
;
887 if (gpiv_par
->verbose
) g_message ("open_piv: no PIV data");
899 load_buffer (GpivConsole
*gpiv
,
904 enum ClistPut clist_put
906 /*-----------------------------------------------------------------------------
907 * create display and its (popup) menu, load image, piv data and puts
908 * buffername into clist
912 gchar
*err_msg
= NULL
;
913 gchar
*clist_buf_txt
[MAX_BUFS
][2], cl_int
[3];
914 gboolean local_stretch_window
= gpiv_par
->display__stretch_window
;
915 GpivImage
*image
= NULL
;
919 * We need to know the image dimensions before creating the buffer display
920 * So, first load the image
922 if ((image
= gpiv_fread_image (fname_in
)) == NULL
) {
923 warning_gpiv ("load_buffer: failing gpiv_fread_image %s", fname_in
);
924 err_msg
= "failing gpiv_fread_image";
929 * Creating buffer and display
931 display
[ibuf
] = create_display (fname_ext
, image
, ibuf
, gpiv
);
932 display_act
= display
[ibuf
];
933 display_act
->img
->exist_img
= TRUE
;
934 display_act
->img
->saved_img
= TRUE
;
937 * Load eventually existing PIV data
939 open_piv (fname_in
, display
[ibuf
]->pida
);
942 * Suppress creating intregs during load_buffer by toggle menu;
943 * after displaying it will result into the correct effect
945 view_toggle_intregs_flag
= FALSE
;
946 gpiv_par
->display__stretch_window
= TRUE
;
948 display_act
->display_popupmenu
= create_display_popupmenu (display_act
);
949 gtk_signal_connect_object (GTK_OBJECT (display_act
->mwin
),
950 "button_press_event",
951 GTK_SIGNAL_FUNC (on_my_popup_handler
),
952 GTK_OBJECT (display_act
->display_popupmenu
));
953 gpiv_par
->display__stretch_window
= local_stretch_window
;
954 stretch_window (display_act
);
957 * Creating pixbuf image for displaying of image data
959 if ( display_act
->gci_bg
!= NULL
) {
960 destroy_background(display_act
->gci_bg
);
962 display_act
->gci_bg
= create_background (display_act
);
964 create_img (display_act
);
965 if (display_act
->img
->image
->header
->x_corr
966 && display_act
->img
->image
->frame2
967 && display_act
->display_backgrnd
== SHOW_BG_IMG1
) {
968 /* hide_img2 (display_act); */
969 show_img1 (display_act
);
970 } else if (display_act
->display_backgrnd
== SHOW_BG_IMG2
) {
971 /* hide_img1 (display_act); */
972 show_img2 (display_act
);
973 } else if (display_act
->display_backgrnd
== SHOW_BG_DARKBLUE
) {
974 gchar
*color
= "darkblue";
975 /* hide_img1 (display_act); */
976 /* hide_img2 (display_act); */
977 if (display_act
->gci_bg
!= NULL
) {
978 gnome_canvas_item_set (GNOME_CANVAS_ITEM (display_act
->gci_bg
),
983 gchar
*color
= "black";
984 if (display_act
->gci_bg
!= NULL
) {
985 gnome_canvas_item_set (GNOME_CANVAS_ITEM (display_act
->gci_bg
),
993 * Creating PIV vectors
995 if (display_act
->display_piv
) {
996 create_all_vectors (display_act
->pida
);
999 display_act
->img
->img_mean
=
1000 image_mean (display_act
->img
->image
->frame1
,
1001 display_act
->img
->image
->header
->ncolumns
,
1002 display_act
->img
->image
->header
->nrows
);
1005 if (gpiv_par
->display__intregs
== 1) {
1006 show_all_intregs (display_act
);
1008 view_toggle_intregs_flag
= TRUE
;
1011 * Add buffer to clist; the column int the console which shows the names of
1014 g_snprintf (cl_int
, 3, "%d", ibuf
);
1015 clist_buf_txt
[ibuf
][0] = (gchar
*) cl_int
;
1016 clist_buf_txt
[ibuf
][1] = fname_base
;
1017 if (clist_put
== PREPEND
) {
1018 gtk_clist_prepend (GTK_CLIST (gpiv
->clist_buf
),
1019 clist_buf_txt
[ibuf
]);
1020 } else if (clist_put
== INSERT
) {
1021 gtk_clist_insert (GTK_CLIST (gpiv
->clist_buf
), ibuf
,
1022 clist_buf_txt
[ibuf
]);
1023 } else if (clist_put
== APPEND
) {
1024 gtk_clist_append (GTK_CLIST (gpiv
->clist_buf
),
1025 clist_buf_txt
[ibuf
]);
1027 error_gpiv ("non-existent CLIST enumerate");
1030 gtk_clist_set_row_data (GTK_CLIST (gpiv
->clist_buf
), ibuf
,
1034 gtk_widget_set_size_request (display_act
->canvas
,
1035 (gint
) (display_act
->zoom_factor
*
1036 display_act
->img
->image
->header
->ncolumns
),
1037 (gint
) (display_act
->zoom_factor
*
1038 display_act
->img
->image
->header
->nrows
));
1040 #ifdef ENABLE_IMGPROC
1041 set_imgproc_filtervar (gpiv
, GPIV_IMGFI_SUBACK
, nbufs
- 1);
1049 * Local functions on writing
1053 write_from_tmp_to_uri (const gchar
*uri_string_tmp
,
1054 const gchar
*uri_string_out
)
1055 /*-----------------------------------------------------------------------------
1058 GnomeVFSHandle
*read_handle
, *write_handle
;
1059 GnomeVFSResult result
, result_while
;
1060 GnomeVFSFileSize bytes_read
, bytes_written
;
1061 guint buffer
[BYTES_TO_PROCESS
];
1065 if ((result
= gnome_vfs_open (&read_handle
, uri_string_tmp
,
1066 GNOME_VFS_OPEN_READ
))
1067 != GNOME_VFS_OK
) gpiv_error ("write_from_tmp_to_uri: gnome_vfs_open_uri");
1069 if ((result
= gnome_vfs_create (&write_handle
, uri_string_out
,
1070 GNOME_VFS_OPEN_WRITE
, FALSE
, 0777))
1071 != GNOME_VFS_OK
) gpiv_error ("write_from_tmp_to_uri: gnome_vfs_create");
1073 while (result
== GNOME_VFS_OK
) {
1074 result
= gnome_vfs_read (read_handle
, buffer
, BYTES_TO_PROCESS
,
1077 if ((result_while
= gnome_vfs_write (write_handle
, buffer
, bytes_read
,
1079 != GNOME_VFS_OK
) gpiv_error ("write_from_tmp_to_uri: gnome_vfs_write");
1083 command
= g_strdup_printf ("rm %s", gnome_vfs_uri_get_path
1084 (gnome_vfs_uri_new (uri_string_tmp
)));
1085 if (system (command
) != 0) {
1086 warning_gpiv ("write_from_tmp_to_uri: could not exec shell command: %s",
1097 move_to_uri (GnomeVFSURI
*uri_out
,
1098 const gchar
*fname_out
)
1099 /*-----------------------------------------------------------------------------
1100 * Move file to uri if not local
1103 gchar
*suffix
= g_strdup (strrchr (fname_out
, '.'));
1106 if (!gnome_vfs_uri_is_local (uri_out
)) {
1107 gchar
*uri_string_tmp
= gnome_vfs_make_uri_from_input(fname_out
);
1108 gchar
*uri_string_out
= gnome_vfs_make_uri_from_input
1111 (g_strdup_printf ("%s%s" ,
1112 display_act
->file_uri_name
,
1114 write_from_tmp_to_uri (uri_string_tmp
, uri_string_out
);
1115 g_free (uri_string_tmp
);
1116 g_free (uri_string_out
);
1124 write_ascii_scalardata (GnomeVFSURI
*uri_out
,
1125 GpivScalarData
*scalar_data
,
1126 const char *fname_out_nosuf
,
1127 const gchar
*suffix
)
1128 /*-----------------------------------------------------------------------------
1129 * Saves scalar data in ascii format
1132 gchar
*err_msg
= NULL
;
1133 gchar
*fname_out
= g_strdup_printf ("%s%s" ,
1134 replace_tilde_with_home_dir
1140 if (gpiv_par
->verbose
) g_message ("writing ascii data to %s", fname_out
);
1142 if ((fp
= fopen (fname_out
, "w")) == NULL
) {
1143 warning_gpiv ("write_ascii_data: failing fopen %s", fname_out
);
1149 gpiv_write_scdata (fp
, scalar_data
, FALSE
))
1150 != NULL
) error_gpiv ("%s", err_msg
);
1154 move_to_uri (uri_out
, fname_out
);