updated copyright statement
[gpiv.git] / src / io.c
blobc84aa6075d5c2e065c717daa65e5649db8fbcb5d
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
6 libraries.
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)
15 any later version.
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)
29 * $Log: io.c,v $
30 * Revision 1.1 2008-09-16 11:21:27 gerber
31 * added io
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38 #include "support.h"
39 #include "gpiv_gui.h"
40 #include "console.h"
41 #include "utils.h"
42 #include "display_image.h"
44 #define BYTES_TO_PROCESS 8192
46 static gint
47 select_action_from_uri (GpivConsole *gpiv,
48 GnomeVFSURI *uri);
50 static void
51 select_action_from_fname (GpivConsole *gpiv,
52 gchar *fname_in);
54 static GpivImage *
55 open_img (char *fname_in
58 static int
59 open_piv (char *fname_in,
60 PivData *pida
63 static gchar *
64 load_buffer (GpivConsole *gpiv,
65 gchar *fname_in,
66 gchar *fname_base,
67 gchar *fname_ext,
68 guint ibuf,
69 enum ClistPut clist_put
72 static void
73 write_from_tmp_to_uri (const gchar *uri_string_tmp,
74 const gchar *uri_string_out);
76 static void
77 move_to_uri (GnomeVFSURI *uri_out,
78 const gchar *fname_out);
80 static void
81 write_ascii_scalardata (GnomeVFSURI *uri_out,
82 GpivScalarData *scalar_data,
83 const char *fname_out_nosuf,
84 const gchar *suffix);
88 * Program-wide public functions
92 * Retrieving functions
94 void
95 select_action_from_name (GpivConsole *gpiv,
96 gchar *name)
97 /* ----------------------------------------------------------------------------
98 * Checks on validity of uri and whether local file or not.
99 * Calls apropiate functions.
102 gint result;
103 gchar *text_uri;
104 GnomeVFSURI* uri = NULL;
107 text_uri = gnome_vfs_make_uri_from_shell_arg (name);
108 uri = gnome_vfs_uri_new (text_uri);
109 g_free (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);
114 } else {
115 if ((result = select_action_from_uri (gpiv, uri)) != 0) {
116 g_error("select_action_from_name: select_action_from_uri");
120 } else {
121 warning_gpiv ("Uri or filename of input image does not exist");
128 * Saving functions
130 void
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,
138 GPIV_EXT_GPIV);
141 if (gpiv_par->verbose) g_message ("writing to %s", fname_out);
143 if ((err_msg =
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) {
149 if ((err_msg =
150 gpiv_img_fwrite_hdf5_parameters (fname_out,
151 display_act->img->image->header))
152 != NULL) error_gpiv ("%s", err_msg);
154 if ((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) {
163 if ((err_msg =
164 gpiv_fwrite_hdf5_pivdata (fname_out,
165 display_act->pida->piv_data_scaled,
166 "PIV",
167 FALSE))
168 != NULL) error_gpiv ("%s", err_msg);
170 } else {
171 if ((err_msg =
172 gpiv_fwrite_hdf5_pivdata (fname_out,
173 display_act->pida->piv_data,
174 "PIV",
175 FALSE))
176 != NULL) error_gpiv ("%s", err_msg);
180 display_act->pida->saved_piv = TRUE;
182 if ((err_msg =
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) {
190 if ((err_msg =
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 ) {
199 if ((err_msg =
200 gpiv_fwrite_hdf5_scdata (fname_out,
201 display_act->pida->vor_data_scaled,
202 "VORTICITY",
203 FALSE))
204 != NULL) error_gpiv ("%s", err_msg);
205 } else {
206 if ((err_msg =
207 gpiv_fwrite_hdf5_scdata (fname_out,
208 display_act->pida->vor_data,
209 "VORTICITY",
210 FALSE))
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 ) {
219 if ((err_msg =
220 gpiv_fwrite_hdf5_scdata (fname_out,
221 display_act->pida->sstrain_data_scaled,
222 "SHEAR_STRAIN",
223 FALSE))
224 != NULL) error_gpiv ("%s", err_msg);
225 } else {
226 if ((err_msg =
227 gpiv_fwrite_hdf5_scdata (fname_out,
228 display_act->pida->sstrain_data,
229 "SHEAR_STRAIN",
230 FALSE))
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 ) {
239 if ((err_msg =
240 gpiv_fwrite_hdf5_scdata (fname_out,
241 display_act->pida->nstrain_data_scaled,
242 "NORMAL_STRAIN",
243 FALSE))
244 != NULL) error_gpiv ("%s", err_msg);
245 } else {
246 if ((err_msg =
247 gpiv_fwrite_hdf5_scdata (fname_out,
248 display_act->pida->nstrain_data,
249 "NORMAL_STRAIN",
250 FALSE))
251 != NULL) error_gpiv ("%s", err_msg);
253 display_act->pida->saved_nstrain = TRUE;
257 if ((err_msg =
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);
264 g_free (fname_out);
269 void
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;
277 gchar *fname_out;
278 FILE *fp_out;
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),
285 GPIV_EXT_PNG_IMAGE);
287 if ((fp_out = fopen (fname_out, "wb")) == NULL) {
288 err_msg = "fopen";
289 error_gpiv ("gpiv: write_img: fopen (%s, \"wb\")) == NULL",
290 fname_out);
292 if ((err_msg = gpiv_write_png_image (fp_out,
293 display_act->img->image,
294 FALSE))
295 != NULL) {
296 gpiv_error ("gpiv: write_img: %s\n", err_msg);
298 fclose (fp_out);
299 move_to_uri (uri_out, fname_out);
300 g_free (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),
305 GPIV_EXT_GPIV);
306 if (gpiv_par->verbose) g_message ("writing image to %s", fname_out);
307 if ((err_msg =
308 gpiv_fcreate_hdf5 (fname_out))
309 != NULL) error_gpiv ("%s", err_msg);
310 if ((err_msg =
311 gpiv_img_fwrite_hdf5_parameters (fname_out,
312 display_act->img->image->header))
313 != NULL) error_gpiv ("%s", err_msg);
314 if ((err_msg =
315 gpiv_fwrite_hdf5_image (fname_out,
316 display_act->img->image,
317 FALSE))
318 != NULL) error_gpiv ("%s", err_msg);
319 move_to_uri (uri_out, fname_out);
320 g_free (fname_out);
322 } else if (gpiv_par->img_fmt == IMG_FMT_RAW) {
324 * Saving ASCII header
326 FILE *fp_header;
327 gchar *fname_header = g_strdup_printf ("%s%s" ,
328 replace_tilde_with_home_dir (fname_out_nosuf),
329 GPIV_EXT_HEADER);
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);
337 fclose (fp_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;
350 void
351 write_ascii_parameters (const char *fname_out_nosuf,
352 GnomeVFSURI *uri_out,
353 const gchar *suffix)
354 /*-----------------------------------------------------------------------------
355 * Saves parameters in ascii format
358 FILE *fp;
359 gint flag_vor = 0;
360 gchar *fname = g_strdup_printf ("%s%s" ,
361 replace_tilde_with_home_dir (fname_out_nosuf),
362 suffix);
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) {
392 if (flag_vor == 0) {
393 gpiv_post_print_parameters (stdout, display_act->pida->post_par);
394 gpiv_post_print_parameters (fp, display_act->pida->post_par);
395 flag_vor = 1;
400 * Saving scalar parameters for shear strain
402 if (display_act->pida->exist_sstrain) {
403 if (flag_vor == 0) {
404 gpiv_post_print_parameters (fp, display_act->pida->post_par);
405 flag_vor = 1;
410 * Saving scalar parameters for normal strain
412 if (display_act->pida->exist_nstrain) {
413 if (flag_vor == 0) {
414 gpiv_post_print_parameters (fp, display_act->pida->post_par);
415 flag_vor = 1;
419 fclose (fp);
420 move_to_uri (uri_out, fname);
421 g_free (fname);
426 void
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
431 * different files
434 char *err_msg = NULL;
435 gchar *fname;
436 FILE *fp = NULL;
440 * Saving PIV data
442 if (display_act->pida->exist_piv) {
443 fname = g_strdup_printf ("%s%s",
444 replace_tilde_with_home_dir (fname_out_nosuf),
445 GPIV_EXT_PIV);
447 if ((fp = fopen (fname, "wb")) == NULL) {
448 warning_gpiv ("write_ascii_data: failing fopen %s", fname);
449 return;
452 if (display_act->pida->scaled_piv) {
453 if ((err_msg =
454 gpiv_write_pivdata (fp,
455 display_act->pida->piv_data_scaled,
456 FALSE))
457 != NULL) error_gpiv ("%s", err_msg);
458 } else {
459 if ((err_msg =
460 gpiv_write_pivdata (fp,
461 display_act->pida->piv_data,
462 FALSE))
463 != NULL) error_gpiv ("%s", err_msg);
466 fclose (fp);
467 display_act->pida->saved_piv = TRUE;
468 move_to_uri (uri_out, fname);
469 g_free (fname);
473 * Saving histogram
475 if (display_act->pida->exist_histo) {
476 fname = g_strdup_printf ("%s%s" ,
477 replace_tilde_with_home_dir (fname_out_nosuf),
478 GPIV_EXT_UVHISTO);
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);
483 return;
486 gpiv_print_histo (fp,
487 display_act->pida->klass,
488 FALSE);
490 fclose (fp);
491 display_act->pida->saved_histo = TRUE;
492 move_to_uri (uri_out, fname);
493 g_free (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
544 static gint
545 select_action_from_uri (GpivConsole *gpiv,
546 GnomeVFSURI *uri)
547 /* ----------------------------------------------------------------------------
548 * checks filename extension on valid image/data name, on existence of buffer,
549 * loads data
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
553 * functions.
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 ();
564 FILE *fp;
566 gchar *dirname = gnome_vfs_uri_extract_dirname (uri);
567 gchar *basename = gnome_vfs_uri_extract_short_name (uri);
568 gchar *command;
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",
575 dirname, basename);
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",
582 output_uri_string);
585 if ((result = gnome_vfs_open_uri (&read_handle, uri, GNOME_VFS_OPEN_READ))
586 != GNOME_VFS_OK)
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,
596 &bytes_read);
598 if ((result_while = gnome_vfs_seek (write_handle, GNOME_VFS_SEEK_END, 0))
599 != GNOME_VFS_OK)
600 gpiv_error ("select_action_from_uri: gnome_vfs_seek");
602 if ((result_while = gnome_vfs_write (write_handle, buffer, bytes_read,
603 &bytes_written))
604 != GNOME_VFS_OK)
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
612 * Cleaning up tmp
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",
637 command);
641 g_free (output_uri_string);
642 g_free (dirname);
643 g_free (basename);
644 g_free (command);
645 return 0;
649 static void
650 select_action_from_fname (GpivConsole *gpiv,
651 gchar *fname_in)
652 /* ----------------------------------------------------------------------------
653 * checks filename suffux on valid image/data name, on existence of buffer,
654 * loads data
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 */
663 *fname_home;
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))); */
685 g_free (fname_home);
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,
692 fname_base, NULL));
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")
714 exist_buf = FALSE;
715 if (nbufs == 0) {
716 sensitive (gpiv, IMG, TRUE);
717 sensitive (gpiv, EVAL, TRUE);
718 } else {
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),
726 ibuf);
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) {
730 return;
733 exist_buf = TRUE;
734 break;
736 } else if (ibuf < display_act->id) {
737 if ((err_msg =
738 load_buffer (gpiv, fname_in, fname_base, fname_nosuf,
739 ibuf, INSERT)) != NULL) {
740 g_warning ("select_action_from_fname: failing load_buffer");
741 return;
743 exist_buf = TRUE;
744 nbufs++;
745 break;
746 } else {
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
757 if (!exist_buf) {
758 nbufs++;
759 ibuf = nbufs - 1;
760 load_buffer (gpiv, fname_in, fname_base, fname_nosuf,
761 ibuf, APPEND);
764 } else {
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);
773 #endif
775 g_free (suf);
776 g_free (dirname);
777 g_free (fname_base);
778 g_free (fname_nosuf);
783 static GpivImage *
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);
796 return;
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;
808 return image;
813 static gint
814 open_piv (char *fname_in,
815 PivData *pida
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);
833 * Reads hdf format
835 if (strcmp (ext, GPIV_EXT_GPIV) == 0) {
836 if ((pida->piv_data =
837 gpiv_fread_hdf5_pivdata (fname_in, "PIV"))
838 == NULL) {
839 warning_gpiv ("OPEN_PIV: failing piv_fread_hdf5_pivdata %s",
840 fname_in);
841 return -1;
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
853 FILE *fp;
854 gchar *dirname = g_strdup (g_dirname (fname_in)),
855 *fname_base = g_strdup (g_basename (fname_in)),
856 *fname_ext,
857 *fname_piv;
859 /* BUGFIX
860 * check on existance of piv data file
862 strtok (fname_base, ".");
863 fname_ext = g_strdup (g_strconcat (dirname, G_DIR_SEPARATOR_S,
864 fname_base, NULL));
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");
873 return -1;
876 fclose (fp);
877 pida->exist_piv = TRUE;
878 pida->averaged_piv = FALSE;
880 g_free (dirname);
881 g_free (fname_base);
882 g_free (fname_ext);
883 g_free (fname_piv);
886 } else {
887 if (gpiv_par->verbose) g_message ("open_piv: no PIV data");
888 return -1;
892 g_free (ext);
893 return 0;
898 static gchar *
899 load_buffer (GpivConsole *gpiv,
900 gchar *fname_in,
901 gchar *fname_base,
902 gchar *fname_ext,
903 guint ibuf,
904 enum ClistPut clist_put
906 /*-----------------------------------------------------------------------------
907 * create display and its (popup) menu, load image, piv data and puts
908 * buffername into clist
911 gint return_val = 0;
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";
925 return err_msg;
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),
979 "fill_color", color,
980 NULL);
982 } else {
983 gchar *color = "black";
984 if (display_act->gci_bg != NULL) {
985 gnome_canvas_item_set (GNOME_CANVAS_ITEM (display_act->gci_bg),
986 "fill_color", color,
987 NULL);
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
1012 * images
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]);
1026 } else {
1027 error_gpiv ("non-existent CLIST enumerate");
1030 gtk_clist_set_row_data (GTK_CLIST (gpiv->clist_buf), ibuf,
1031 display_act);
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);
1042 #endif
1043 return err_msg;
1049 * Local functions on writing
1052 static void
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];
1062 gchar *command;
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,
1075 &bytes_read);
1077 if ((result_while = gnome_vfs_write (write_handle, buffer, bytes_read,
1078 &bytes_written))
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",
1087 command);
1091 g_free (command);
1092 return;
1096 static void
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,
1113 suffix));
1114 write_from_tmp_to_uri (uri_string_tmp, uri_string_out);
1115 g_free (uri_string_tmp);
1116 g_free (uri_string_out);
1119 g_free (suffix);
1123 static void
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
1135 (fname_out_nosuf),
1136 suffix);
1137 FILE *fp = NULL;
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);
1144 return;
1148 if ((err_msg =
1149 gpiv_write_scdata (fp, scalar_data, FALSE))
1150 != NULL) error_gpiv ("%s", err_msg);
1153 fclose (fp);
1154 move_to_uri (uri_out, fname_out);
1155 g_free (fname_out);