bugrepair VFS, update autotools files
[gpiv.git] / src / io.c
blob7f39954cbf0151e18bcc33cfee1ec617af1e0871
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 g_message("select_action_from_name:: text_uri = %s", text_uri);
109 uri = gnome_vfs_uri_new (text_uri);
110 g_free (text_uri);
111 if (gnome_vfs_uri_exists (uri)) {
112 if (gnome_vfs_uri_is_local (uri)) {
113 const gchar *path = gnome_vfs_uri_get_path (uri);
114 select_action_from_fname (gpiv, (gchar *) path);
115 } else {
116 if ((result = select_action_from_uri (gpiv, uri)) != 0) {
117 g_error("select_action_from_name: select_action_from_uri");
121 } else {
122 warning_gpiv ("Uri or filename of input image does not exist");
129 * Saving functions
131 void
132 write_hdf_img_data (const gchar *fname_out_nosuf,
133 GnomeVFSURI *uri_out)
134 /*------------------------------------------------------------------------------
137 char *err_msg = NULL;
138 gchar *fname_out = g_strdup_printf ("%s%s" , fname_out_nosuf,
139 GPIV_EXT_GPIV);
142 if (gpiv_par->verbose) g_message ("writing to %s", fname_out);
144 if ((err_msg =
145 gpiv_fcreate_hdf5 (fname_out))
146 != NULL) error_gpiv ("%s", err_msg);
148 if (gpiv_par->img_fmt == IMG_FMT_HDF
149 && display_act->img->saved_img == FALSE) {
150 if ((err_msg =
151 gpiv_img_fwrite_hdf5_parameters (fname_out,
152 display_act->img->image->header))
153 != NULL) error_gpiv ("%s", err_msg);
155 if ((err_msg =
156 gpiv_fwrite_hdf5_image (fname_out, display_act->img->image, FALSE))
157 != NULL) error_gpiv ("%s", err_msg);
158 display_act->img->saved_img = TRUE;
162 if (display_act->pida->exist_piv) {
163 if (display_act->pida->scaled_piv) {
164 if ((err_msg =
165 gpiv_fwrite_hdf5_pivdata (fname_out,
166 display_act->pida->piv_data_scaled,
167 "PIV",
168 FALSE))
169 != NULL) error_gpiv ("%s", err_msg);
171 } else {
172 if ((err_msg =
173 gpiv_fwrite_hdf5_pivdata (fname_out,
174 display_act->pida->piv_data,
175 "PIV",
176 FALSE))
177 != NULL) error_gpiv ("%s", err_msg);
181 display_act->pida->saved_piv = TRUE;
183 if ((err_msg =
184 gpiv_piv_fwrite_hdf5_parameters (fname_out,
185 display_act->pida->piv_par))
186 != NULL) error_gpiv ("%s", err_msg);
190 if (display_act->pida->exist_valid) {
191 if ((err_msg =
192 gpiv_valid_fwrite_hdf5_parameters (fname_out,
193 display_act->pida->valid_par))
194 != NULL) error_gpiv ("%s", err_msg);
198 if (display_act->pida->exist_vor) {
199 if (display_act->pida->scaled_piv ) {
200 if ((err_msg =
201 gpiv_fwrite_hdf5_scdata (fname_out,
202 display_act->pida->vor_data_scaled,
203 "VORTICITY",
204 FALSE))
205 != NULL) error_gpiv ("%s", err_msg);
206 } else {
207 if ((err_msg =
208 gpiv_fwrite_hdf5_scdata (fname_out,
209 display_act->pida->vor_data,
210 "VORTICITY",
211 FALSE))
212 != NULL) error_gpiv ("%s", err_msg);
214 display_act->pida->saved_vor = TRUE;
218 if (display_act->pida->exist_sstrain) {
219 if (display_act->pida->scaled_piv ) {
220 if ((err_msg =
221 gpiv_fwrite_hdf5_scdata (fname_out,
222 display_act->pida->sstrain_data_scaled,
223 "SHEAR_STRAIN",
224 FALSE))
225 != NULL) error_gpiv ("%s", err_msg);
226 } else {
227 if ((err_msg =
228 gpiv_fwrite_hdf5_scdata (fname_out,
229 display_act->pida->sstrain_data,
230 "SHEAR_STRAIN",
231 FALSE))
232 != NULL) error_gpiv ("%s", err_msg);
234 display_act->pida->saved_sstrain = TRUE;
238 if (display_act->pida->exist_nstrain) {
239 if (display_act->pida->scaled_piv ) {
240 if ((err_msg =
241 gpiv_fwrite_hdf5_scdata (fname_out,
242 display_act->pida->nstrain_data_scaled,
243 "NORMAL_STRAIN",
244 FALSE))
245 != NULL) error_gpiv ("%s", err_msg);
246 } else {
247 if ((err_msg =
248 gpiv_fwrite_hdf5_scdata (fname_out,
249 display_act->pida->nstrain_data,
250 "NORMAL_STRAIN",
251 FALSE))
252 != NULL) error_gpiv ("%s", err_msg);
254 display_act->pida->saved_nstrain = TRUE;
258 if ((err_msg =
259 gpiv_post_fwrite_hdf5_parameters (fname_out,
260 display_act->pida->post_par))
261 != NULL) error_gpiv ("%s", err_msg);
264 move_to_uri (uri_out, fname_out);
265 g_free (fname_out);
270 void
271 write_img (const gchar *fname_out_nosuf,
272 GnomeVFSURI *uri_out)
273 /*------------------------------------------------------------------------------
274 * Store image to RAW, HDF or PNG format
277 char *err_msg = NULL;
278 gchar *fname_out;
279 FILE *fp_out;
282 if (display_act->img->saved_img == FALSE) {
283 if (gpiv_par->img_fmt == IMG_FMT_PNG) {
284 fname_out = g_strdup_printf ("%s%s" ,
285 replace_tilde_with_home_dir (fname_out_nosuf),
286 GPIV_EXT_PNG_IMAGE);
288 if ((fp_out = fopen (fname_out, "wb")) == NULL) {
289 err_msg = "fopen";
290 error_gpiv ("gpiv: write_img: fopen (%s, \"wb\")) == NULL",
291 fname_out);
293 if ((err_msg = gpiv_write_png_image (fp_out,
294 display_act->img->image,
295 FALSE))
296 != NULL) {
297 gpiv_error ("gpiv: write_img: %s\n", err_msg);
299 fclose (fp_out);
300 move_to_uri (uri_out, fname_out);
301 g_free (fname_out);
303 } else if (gpiv_par->img_fmt == IMG_FMT_HDF) {
304 fname_out = g_strdup_printf ("%s%s" ,
305 replace_tilde_with_home_dir (fname_out_nosuf),
306 GPIV_EXT_GPIV);
307 if (gpiv_par->verbose) g_message ("writing image to %s", fname_out);
308 if ((err_msg =
309 gpiv_fcreate_hdf5 (fname_out))
310 != NULL) error_gpiv ("%s", err_msg);
311 if ((err_msg =
312 gpiv_img_fwrite_hdf5_parameters (fname_out,
313 display_act->img->image->header))
314 != NULL) error_gpiv ("%s", err_msg);
315 if ((err_msg =
316 gpiv_fwrite_hdf5_image (fname_out,
317 display_act->img->image,
318 FALSE))
319 != NULL) error_gpiv ("%s", err_msg);
320 move_to_uri (uri_out, fname_out);
321 g_free (fname_out);
323 } else if (gpiv_par->img_fmt == IMG_FMT_RAW) {
325 * Saving ASCII header
327 FILE *fp_header;
328 gchar *fname_header = g_strdup_printf ("%s%s" ,
329 replace_tilde_with_home_dir (fname_out_nosuf),
330 GPIV_EXT_HEADER);
331 if (gpiv_par->verbose) g_message ("writing header to %s", fname_header);
332 if ((fp_header = fopen (fname_header, "w")) == NULL) {
333 error_gpiv (_("Failure opening image header for output"));
335 /* fprintf(fp_par, "#%s\n", gl_image_par->software);
337 gpiv_img_print_header (fp_header, display_act->img->image->header);
338 fclose (fp_header);
339 move_to_uri (uri_out, fname_header);
340 g_free (fname_header);
342 * BUGFIX: Saving RAW image
345 display_act->img->saved_img = TRUE;
351 void
352 write_ascii_parameters (const char *fname_out_nosuf,
353 GnomeVFSURI *uri_out,
354 const gchar *suffix)
355 /*-----------------------------------------------------------------------------
356 * Saves parameters in ascii format
359 FILE *fp;
360 gint flag_vor = 0;
361 gchar *fname = g_strdup_printf ("%s%s" ,
362 replace_tilde_with_home_dir (fname_out_nosuf),
363 suffix);
366 if ((fp = fopen (fname, "w")) == NULL) {
367 error_gpiv (_("Failure opening parameter file for output"));
371 * Saving image header
373 if (gl_image_par->software__set) {
374 fprintf (fp, "# Software: %s\n", gl_image_par->software);
376 gpiv_img_print_header (fp, display_act->img->image->header);
379 * Saving PIV parameters
381 if (display_act->pida->exist_piv) {
382 gpiv_piv_print_parameters (fp, display_act->pida->piv_par);
383 if (display_act->pida->exist_valid) {
384 gpiv_valid_print_parameters (fp, display_act->pida->valid_par);
390 * Saving scalar parameters for vorticity
392 if (display_act->pida->exist_vor) {
393 if (flag_vor == 0) {
394 gpiv_post_print_parameters (stdout, display_act->pida->post_par);
395 gpiv_post_print_parameters (fp, display_act->pida->post_par);
396 flag_vor = 1;
401 * Saving scalar parameters for shear strain
403 if (display_act->pida->exist_sstrain) {
404 if (flag_vor == 0) {
405 gpiv_post_print_parameters (fp, display_act->pida->post_par);
406 flag_vor = 1;
411 * Saving scalar parameters for normal strain
413 if (display_act->pida->exist_nstrain) {
414 if (flag_vor == 0) {
415 gpiv_post_print_parameters (fp, display_act->pida->post_par);
416 flag_vor = 1;
420 fclose (fp);
421 move_to_uri (uri_out, fname);
422 g_free (fname);
427 void
428 write_ascii_data (const gchar *fname_out_nosuf,
429 GnomeVFSURI *uri_out)
430 /*------------------------------------------------------------------------------
431 * Store resulting (PIV and derived) data in ASCII format at
432 * different files
435 char *err_msg = NULL;
436 gchar *fname;
437 FILE *fp = NULL;
441 * Saving PIV data
443 if (display_act->pida->exist_piv) {
444 fname = g_strdup_printf ("%s%s",
445 replace_tilde_with_home_dir (fname_out_nosuf),
446 GPIV_EXT_PIV);
448 if ((fp = fopen (fname, "wb")) == NULL) {
449 warning_gpiv ("write_ascii_data: failing fopen %s", fname);
450 return;
453 if (display_act->pida->scaled_piv) {
454 if ((err_msg =
455 gpiv_write_pivdata (fp,
456 display_act->pida->piv_data_scaled,
457 FALSE))
458 != NULL) error_gpiv ("%s", err_msg);
459 } else {
460 if ((err_msg =
461 gpiv_write_pivdata (fp,
462 display_act->pida->piv_data,
463 FALSE))
464 != NULL) error_gpiv ("%s", err_msg);
467 fclose (fp);
468 display_act->pida->saved_piv = TRUE;
469 move_to_uri (uri_out, fname);
470 g_free (fname);
474 * Saving histogram
476 if (display_act->pida->exist_histo) {
477 fname = g_strdup_printf ("%s%s" ,
478 replace_tilde_with_home_dir (fname_out_nosuf),
479 GPIV_EXT_UVHISTO);
480 if (gpiv_par->verbose) g_message ("writing HISTO data to %s", fname);
482 if ((fp = fopen (fname, "wb")) == NULL) {
483 warning_gpiv ("write_ascii_data: failing fopen %s", fname);
484 return;
487 gpiv_print_histo (fp,
488 display_act->pida->klass,
489 FALSE);
491 fclose (fp);
492 display_act->pida->saved_histo = TRUE;
493 move_to_uri (uri_out, fname);
494 g_free (fname);
498 * Saving scalar vorticity data
500 if (display_act->pida->exist_vor_scaled) {
501 write_ascii_scalardata (uri_out, display_act->pida->vor_data_scaled,
502 fname_out_nosuf, GPIV_EXT_VOR);
503 display_act->pida->saved_vor = TRUE;
504 } else if (display_act->pida->exist_vor) {
505 write_ascii_scalardata (uri_out, display_act->pida->vor_data,
506 fname_out_nosuf, GPIV_EXT_VOR);
507 display_act->pida->saved_vor = TRUE;
512 * Saving scalar shear strain data
514 if (display_act->pida->exist_sstrain_scaled) {
515 write_ascii_scalardata (uri_out, display_act->pida->sstrain_data_scaled,
516 fname_out_nosuf, GPIV_EXT_SSTR);
517 display_act->pida->saved_sstrain = TRUE;
518 } else if (display_act->pida->exist_sstrain) {
519 write_ascii_scalardata (uri_out, display_act->pida->sstrain_data,
520 fname_out_nosuf, GPIV_EXT_SSTR);
521 display_act->pida->saved_sstrain = TRUE;
525 * Saving scalar normal strain data
527 if (display_act->pida->exist_nstrain_scaled) {
528 write_ascii_scalardata (uri_out, display_act->pida->nstrain_data_scaled,
529 fname_out_nosuf, GPIV_EXT_NSTR);
530 display_act->pida->saved_nstrain = TRUE;
531 } else if (display_act->pida->exist_nstrain) {
532 write_ascii_scalardata (uri_out, display_act->pida->nstrain_data,
533 fname_out_nosuf, GPIV_EXT_NSTR);
534 display_act->pida->saved_nstrain = TRUE;
542 * Local functions on retrieving data
545 static gint
546 select_action_from_uri (GpivConsole *gpiv,
547 GnomeVFSURI *uri)
548 /* ----------------------------------------------------------------------------
549 * checks filename extension on valid image/data name, on existence of buffer,
550 * loads data
551 * As i/o from libgpiv (which does not have libgnomevfs) will have to be
552 * be used (applying libpng funcionality), the image (data) are copied to a
553 * tmp directory and subsequtially be loaded with libgpiv i/o
554 * functions.
557 GnomeVFSHandle *read_handle, *write_handle;
558 GnomeVFSResult result, result_while;
559 GnomeVFSFileSize bytes_read, bytes_written;
560 guint buffer[BYTES_TO_PROCESS];
561 gchar *output_uri_string;
563 const gchar *tmp_dir = g_get_tmp_dir ();
564 const gchar *user_name = g_get_user_name ();
565 const gchar *local_tmp_dirname = g_strdup_printf ("%s/%s",
566 tmp_dir, user_name);
567 FILE *fp;
569 gchar *dirname = gnome_vfs_uri_extract_dirname (uri);
570 gchar *basename = gnome_vfs_uri_extract_short_name (uri);
571 gchar *command;
574 g_mkdir(local_tmp_dirname, 0777);
575 if (gpiv_par->verbose) {
576 g_message ("select_action_from_uri:: tmp_dir = %s", tmp_dir);
577 g_message ("select_action_from_uri:: user_name = %s", user_name);
578 g_message ("select_action_from_uri:: local_tmp_dirname = %s",
579 local_tmp_dirname);
580 g_message ("select_action_from_uri:: dirname = %s basename = %s",
581 dirname, basename);
584 output_uri_string = g_strdup_printf ("%s/%s/%s" ,tmp_dir,
585 user_name, basename);
586 if (gpiv_par->verbose) {
587 g_message ("select_action_from_uri:: output_uri_string = %s",
588 output_uri_string);
591 if ((result = gnome_vfs_open_uri (&read_handle, uri, GNOME_VFS_OPEN_READ))
592 != GNOME_VFS_OK)
593 gpiv_error ("select_action_from_uri: gnome_vfs_open_uri");
595 if ((result = gnome_vfs_create (&write_handle, output_uri_string,
596 GNOME_VFS_OPEN_WRITE, FALSE, 0777))
597 != GNOME_VFS_OK) gpiv_error ("select_action_from_uri: gnome_vfs_create");
599 /* read data from the input uri */
600 while (result == GNOME_VFS_OK) {
601 result = gnome_vfs_read (read_handle, buffer, BYTES_TO_PROCESS,
602 &bytes_read);
604 if ((result_while = gnome_vfs_seek (write_handle, GNOME_VFS_SEEK_END, 0))
605 != GNOME_VFS_OK)
606 gpiv_error ("select_action_from_uri: gnome_vfs_seek");
608 if ((result_while = gnome_vfs_write (write_handle, buffer, bytes_read,
609 &bytes_written))
610 != GNOME_VFS_OK)
611 gpiv_error ("select_action_from_uri: gnome_vfs_write");
615 * add to uri to fname_last;
616 * Reading image from temporarly storage
617 * put correct name in Image tab
618 * Cleaning up tmp
620 gpiv_var->fname_last = gnome_vfs_uri_to_string (uri, TRUE);
621 gnome_config_push_prefix ("/gpiv/RuntimeVariables/");
622 gnome_config_set_string ("fname_last", gpiv_var->fname_last);
623 gnome_config_pop_prefix ();
624 gnome_config_sync ();
625 push_list_lastfnames (gpiv_var->fname_last);
627 gpiv_var->fname_last__set = TRUE;
628 select_action_from_fname (gpiv, output_uri_string);
629 gpiv_var->fname_last__set = FALSE;
631 display_act->file_uri_name =
632 g_strdup_printf ("%s",
633 strtok (gnome_vfs_uri_to_string (uri, TRUE), "."));
635 update_imgh_entries (gpiv, display_act->img->image->header);
637 command = g_strdup_printf ("rm %s", output_uri_string);
638 if (gpiv_par->verbose) {
639 g_message ("command = %s", command);
641 if (system (command) != 0) {
642 g_warning ("select_action_from_uri: could not exec shell command: %s",
643 command);
647 g_free (output_uri_string);
648 g_free (local_tmp_dirname);
649 g_free (dirname);
650 g_free (basename);
651 g_free (command);
652 return 0;
656 static void
657 select_action_from_fname (GpivConsole *gpiv,
658 gchar *fname_in)
659 /* ----------------------------------------------------------------------------
660 * checks filename suffux on valid image/data name, on existence of buffer,
661 * loads data
664 gchar *err_msg = NULL;
665 gint ibuf, return_val;
666 gchar *suf, /* filename suffix */
667 *dirname, /* directory name */
668 *fname_base, /* Image name without suffix and dirname */
669 *fname_nosuf, /* Image name without suffix */
670 *fname_home;
671 gboolean exist_buf = FALSE;
672 gchar command[2 * GPIV_MAX_CHARS];
673 /* gchar *command; */
676 suf = g_strdup (strrchr (fname_in, '.'));
677 /* add to push_list_lastfnames (fname_base); */
678 if (!gpiv_var->fname_last__set) {
679 gpiv_var->fname_last = g_strdup (fname_in);
680 gnome_config_push_prefix ("/gpiv/RuntimeVariables/");
681 gnome_config_set_string ("fname_last", gpiv_var->fname_last);
682 gnome_config_pop_prefix ();
683 gnome_config_sync ();
686 * Stripping file name
687 * We need them for displaying in the image header tab and on the buffer list
689 fname_home = replace_home_dir_with_tilde (fname_in);
690 dirname = g_strdup (g_path_get_dirname (fname_home));
691 /* (replace_home_dir_with_tilde (fname_in))); */
692 g_free (fname_home);
693 fname_base = g_strdup (g_path_get_basename (fname_in));
694 strtok (fname_base, ".");
695 if (!gpiv_var->fname_last__set) {
696 push_list_lastfnames (fname_base);
698 fname_nosuf = g_strdup (g_strconcat (dirname, G_DIR_SEPARATOR_S,
699 fname_base, NULL));
702 * reading image data from raw, PNG, hdf or Davis(tm) format
703 * Ensure that the tabulator widgets are enabled
705 if (g_str_has_suffix (fname_in, GPIV_EXT_RAW_IMAGE)
706 || g_str_has_suffix (fname_in, GPIV_EXT_PNG_IMAGE)
707 || g_str_has_suffix (fname_in, GPIV_EXT_PNG_IMAGE_UPCASE)
708 || g_str_has_suffix (fname_in, GPIV_EXT_GPIV)
709 || g_str_has_suffix (fname_in, GPIV_EXT_DAVIS)
710 || g_str_has_suffix (fname_in, GPIV_EXT_PGM_IMAGE)
711 || g_str_has_suffix (fname_in, GPIV_EXT_PGM_IMAGE_UPCASE)
712 || g_str_has_suffix (fname_in, ".gif")
713 || g_str_has_suffix (fname_in, ".GIF")
714 || g_str_has_suffix (fname_in, ".tif")
715 || g_str_has_suffix (fname_in, ".TIF")
716 || g_str_has_suffix (fname_in, ".bmp")
717 || g_str_has_suffix (fname_in, ".BMP")
721 exist_buf = FALSE;
722 if (nbufs == 0) {
723 sensitive (gpiv, IMG, TRUE);
724 sensitive (gpiv, EVAL, TRUE);
725 } else {
728 * Check if file basename (and buffer/display) already exists
729 * or if there is a gap in the numbering of the buffers
731 for (ibuf = 0; ibuf < nbufs; ibuf++) {
732 display_act = gtk_clist_get_row_data (GTK_CLIST (gpiv->clist_buf),
733 ibuf);
734 if (ibuf == display_act->id
735 && strcmp (fname_nosuf, display_act->file_uri_name) == 0 ) {
736 if ((display_act->img->image = open_img (fname_in)) == NULL) {
737 return;
740 exist_buf = TRUE;
741 break;
743 } else if (ibuf < display_act->id) {
744 if ((err_msg =
745 load_buffer (gpiv, fname_in, fname_base, fname_nosuf,
746 ibuf, INSERT)) != NULL) {
747 g_warning ("select_action_from_fname: failing load_buffer");
748 return;
750 exist_buf = TRUE;
751 nbufs++;
752 break;
753 } else {
754 /* g_warning ("select_action_from_fname: should not arrive here"); */
760 * All buffers with id between 0 and nbufs already exist.
761 * Create a new buffer and launches a display if file basename doesn't exist
762 * and append to the list
764 if (!exist_buf) {
765 nbufs++;
766 ibuf = nbufs - 1;
767 load_buffer (gpiv, fname_in, fname_base, fname_nosuf,
768 ibuf, APPEND);
771 } else {
772 warning_gpiv (_("select_action_from_fname: non-valid file name"));
775 gtk_clist_set_selection_mode (GTK_CLIST (gpiv->clist_buf),
776 GTK_SELECTION_EXTENDED);
778 #ifdef ENABLE_IMGPROC
779 set_imgproc_filtervar (gpiv, GPIV_IMGFI_SUBACK, nbufs - 1);
780 #endif
782 g_free (suf);
783 g_free (dirname);
784 g_free (fname_base);
785 g_free (fname_nosuf);
790 static GpivImage *
791 open_img (char *fname_in
793 /*-----------------------------------------------------------------------------
794 * Opens an image from png, hdf or raw-data file (<=> open_dac_img)
797 GpivImage *image = NULL;
798 gchar *err_msg = NULL;
801 if ((image = gpiv_fread_image (fname_in)) == NULL) {
802 warning_gpiv ("OPEN_IMG: failing gpiv_fread_image %s", fname_in);
803 return;
806 display_act->img->exist_img = TRUE;
809 * The loaded image has not yet been modified, so there is no need to save
810 * image or header information
812 display_act->img->saved_img = TRUE;
815 return image;
820 static gint
821 open_piv (char *fname_in,
822 PivData *pida
824 /*-----------------------------------------------------------------------------
825 * Opens piv data file and displays
828 char *err_msg = NULL, RCSID_DATA[GPIV_MAX_CHARS];
829 char *ext = g_strdup (strrchr (fname_in, '.'));
830 gint var_scale = 0, scale = 0;
834 * parameter initializing of piv data
836 /* pida->piv_par->autokey = 1; */
837 gpiv_piv_parameters_set (pida->piv_par, FALSE);
840 * Reads hdf format
842 if (strcmp (ext, GPIV_EXT_GPIV) == 0) {
843 if ((pida->piv_data =
844 gpiv_fread_hdf5_pivdata (fname_in, "PIV"))
845 == NULL) {
846 warning_gpiv ("OPEN_PIV: failing piv_fread_hdf5_pivdata %s",
847 fname_in);
848 return -1;
850 pida->exist_piv = TRUE;
851 pida->averaged_piv = FALSE;
855 * Reads raw data format
857 } else if (strcmp (ext, GPIV_EXT_RAW_IMAGE) == 0
858 || strcmp (ext, GPIV_EXT_DAVIS) == 0
860 FILE *fp;
861 gchar *dirname = g_strdup (g_dirname (fname_in)),
862 *fname_base = g_strdup (g_basename (fname_in)),
863 *fname_ext,
864 *fname_piv;
866 /* BUGFIX
867 * check on existance of piv data file
869 strtok (fname_base, ".");
870 fname_ext = g_strdup (g_strconcat (dirname, G_DIR_SEPARATOR_S,
871 fname_base, NULL));
872 fname_piv = g_strdup (g_strconcat (fname_ext, GPIV_EXT_PIV, NULL));
874 if ((fp = fopen (fname_piv, "r")) == NULL) {
875 warning_gpiv ("OPEN_PIV: failing fopen %s", fname_piv);
878 if ((pida->piv_data = gpiv_read_pivdata (fp)) == NULL) {
879 warning_gpiv ("OPEN_PIV: failing gpiv_read_pivdata");
880 return -1;
883 fclose (fp);
884 pida->exist_piv = TRUE;
885 pida->averaged_piv = FALSE;
887 g_free (dirname);
888 g_free (fname_base);
889 g_free (fname_ext);
890 g_free (fname_piv);
893 } else {
894 if (gpiv_par->verbose) g_message ("open_piv: no PIV data");
895 return -1;
899 g_free (ext);
900 return 0;
905 static gchar *
906 load_buffer (GpivConsole *gpiv,
907 gchar *fname_in,
908 gchar *fname_base,
909 gchar *fname_ext,
910 guint ibuf,
911 enum ClistPut clist_put
913 /*-----------------------------------------------------------------------------
914 * create display and its (popup) menu, load image, piv data and puts
915 * buffername into clist
918 gint return_val = 0;
919 gchar *err_msg = NULL;
920 gchar *clist_buf_txt[MAX_BUFS][2], cl_int[3];
921 gboolean local_stretch_window = gpiv_par->display__stretch_window;
922 GpivImage *image = NULL;
926 * We need to know the image dimensions before creating the buffer display
927 * So, first load the image
929 if ((image = gpiv_fread_image (fname_in)) == NULL) {
930 warning_gpiv ("load_buffer: failing gpiv_fread_image %s", fname_in);
931 err_msg = "failing gpiv_fread_image";
932 return err_msg;
936 * Creating buffer and display
938 display[ibuf] = create_display (fname_ext, image, ibuf, gpiv);
939 display_act = display[ibuf];
940 display_act->img->exist_img = TRUE;
941 display_act->img->saved_img = TRUE;
944 * Load eventually existing PIV data
946 open_piv (fname_in, display[ibuf]->pida);
949 * Suppress creating intregs during load_buffer by toggle menu;
950 * after displaying it will result into the correct effect
952 view_toggle_intregs_flag = FALSE;
953 gpiv_par->display__stretch_window = TRUE;
955 display_act->display_popupmenu = create_display_popupmenu (display_act);
956 gtk_signal_connect_object (GTK_OBJECT (display_act->mwin),
957 "button_press_event",
958 GTK_SIGNAL_FUNC (on_my_popup_handler),
959 GTK_OBJECT (display_act->display_popupmenu));
960 gpiv_par->display__stretch_window = local_stretch_window;
961 stretch_window (display_act);
964 * Creating pixbuf image for displaying of image data
966 if ( display_act->gci_bg != NULL) {
967 destroy_background(display_act->gci_bg);
969 display_act->gci_bg = create_background (display_act);
971 create_img (display_act);
972 if (display_act->img->image->header->x_corr
973 && display_act->img->image->frame2
974 && display_act->display_backgrnd == SHOW_BG_IMG1) {
975 /* hide_img2 (display_act); */
976 show_img1 (display_act);
977 } else if (display_act->display_backgrnd == SHOW_BG_IMG2) {
978 /* hide_img1 (display_act); */
979 show_img2 (display_act);
980 } else if (display_act->display_backgrnd == SHOW_BG_DARKBLUE) {
981 gchar *color = "darkblue";
982 /* hide_img1 (display_act); */
983 /* hide_img2 (display_act); */
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);
989 } else {
990 gchar *color = "black";
991 if (display_act->gci_bg != NULL) {
992 gnome_canvas_item_set (GNOME_CANVAS_ITEM (display_act->gci_bg),
993 "fill_color", color,
994 NULL);
1000 * Creating PIV vectors
1002 if (display_act->display_piv) {
1003 create_all_vectors (display_act->pida);
1006 display_act->img->img_mean =
1007 image_mean (display_act->img->image->frame1,
1008 display_act->img->image->header->ncolumns,
1009 display_act->img->image->header->nrows);
1012 if (gpiv_par->display__intregs == 1) {
1013 show_all_intregs (display_act);
1015 view_toggle_intregs_flag = TRUE;
1018 * Add buffer to clist; the column int the console which shows the names of
1019 * images
1021 g_snprintf (cl_int, 3, "%d", ibuf);
1022 clist_buf_txt[ibuf][0] = (gchar *) cl_int;
1023 clist_buf_txt[ibuf][1] = fname_base;
1024 if (clist_put == PREPEND) {
1025 gtk_clist_prepend (GTK_CLIST (gpiv->clist_buf),
1026 clist_buf_txt[ibuf]);
1027 } else if (clist_put == INSERT) {
1028 gtk_clist_insert (GTK_CLIST (gpiv->clist_buf), ibuf,
1029 clist_buf_txt[ibuf]);
1030 } else if (clist_put == APPEND) {
1031 gtk_clist_append (GTK_CLIST (gpiv->clist_buf),
1032 clist_buf_txt[ibuf]);
1033 } else {
1034 error_gpiv ("non-existent CLIST enumerate");
1037 gtk_clist_set_row_data (GTK_CLIST (gpiv->clist_buf), ibuf,
1038 display_act);
1041 gtk_widget_set_size_request (display_act->canvas,
1042 (gint) (display_act->zoom_factor *
1043 display_act->img->image->header->ncolumns),
1044 (gint) (display_act->zoom_factor *
1045 display_act->img->image->header->nrows));
1047 #ifdef ENABLE_IMGPROC
1048 set_imgproc_filtervar (gpiv, GPIV_IMGFI_SUBACK, nbufs - 1);
1049 #endif
1050 return err_msg;
1056 * Local functions on writing
1059 static void
1060 write_from_tmp_to_uri (const gchar *uri_string_tmp,
1061 const gchar *uri_string_out)
1062 /*-----------------------------------------------------------------------------
1065 GnomeVFSHandle *read_handle, *write_handle;
1066 GnomeVFSResult result, result_while;
1067 GnomeVFSFileSize bytes_read, bytes_written;
1068 guint buffer[BYTES_TO_PROCESS];
1069 gchar *command;
1072 if ((result = gnome_vfs_open (&read_handle, uri_string_tmp,
1073 GNOME_VFS_OPEN_READ))
1074 != GNOME_VFS_OK) gpiv_error ("write_from_tmp_to_uri: gnome_vfs_open_uri");
1076 if ((result = gnome_vfs_create (&write_handle, uri_string_out,
1077 GNOME_VFS_OPEN_WRITE, FALSE, 0777))
1078 != GNOME_VFS_OK) gpiv_error ("write_from_tmp_to_uri: gnome_vfs_create");
1080 while (result == GNOME_VFS_OK) {
1081 result = gnome_vfs_read (read_handle, buffer, BYTES_TO_PROCESS,
1082 &bytes_read);
1084 if ((result_while = gnome_vfs_write (write_handle, buffer, bytes_read,
1085 &bytes_written))
1086 != GNOME_VFS_OK) gpiv_error ("write_from_tmp_to_uri: gnome_vfs_write");
1090 command = g_strdup_printf ("rm %s", gnome_vfs_uri_get_path
1091 (gnome_vfs_uri_new (uri_string_tmp)));
1092 if (system (command) != 0) {
1093 warning_gpiv ("write_from_tmp_to_uri: could not exec shell command: %s",
1094 command);
1098 g_free (command);
1099 return;
1103 static void
1104 move_to_uri (GnomeVFSURI *uri_out,
1105 const gchar *fname_out)
1106 /*-----------------------------------------------------------------------------
1107 * Move file to uri if not local
1110 gchar *suffix = g_strdup (strrchr (fname_out, '.'));
1113 if (!gnome_vfs_uri_is_local (uri_out)) {
1114 gchar *uri_string_tmp = gnome_vfs_make_uri_from_input(fname_out);
1115 gchar *uri_string_out = gnome_vfs_make_uri_from_input
1118 (g_strdup_printf ("%s%s" ,
1119 display_act->file_uri_name,
1120 suffix));
1121 write_from_tmp_to_uri (uri_string_tmp, uri_string_out);
1122 g_free (uri_string_tmp);
1123 g_free (uri_string_out);
1126 g_free (suffix);
1130 static void
1131 write_ascii_scalardata (GnomeVFSURI *uri_out,
1132 GpivScalarData *scalar_data,
1133 const char *fname_out_nosuf,
1134 const gchar *suffix)
1135 /*-----------------------------------------------------------------------------
1136 * Saves scalar data in ascii format
1139 gchar *err_msg = NULL;
1140 gchar *fname_out = g_strdup_printf ("%s%s" ,
1141 replace_tilde_with_home_dir
1142 (fname_out_nosuf),
1143 suffix);
1144 FILE *fp = NULL;
1147 if (gpiv_par->verbose) g_message ("writing ascii data to %s", fname_out);
1149 if ((fp = fopen (fname_out, "w")) == NULL) {
1150 warning_gpiv ("write_ascii_data: failing fopen %s", fname_out);
1151 return;
1155 if ((err_msg =
1156 gpiv_write_scdata (fp, scalar_data, FALSE))
1157 != NULL) error_gpiv ("%s", err_msg);
1160 fclose (fp);
1161 move_to_uri (uri_out, fname_out);
1162 g_free (fname_out);