xref: /petsc/src/sys/classes/draw/interface/dsave.c (revision 71ed57bffcea9baf3f8f4292918aeee927f15d62)
1 #include <petsc/private/drawimpl.h>  /*I "petscdraw.h" I*/
2 
3 #undef __FUNCT__
4 #define __FUNCT__ "PetscDrawSetSave"
5 /*@C
6    PetscDrawSetSave - Saves images produced in a PetscDraw into a file as a Gif file using AfterImage
7 
8    Collective on PetscDraw
9 
10    Input Parameter:
11 +  draw      - the graphics context
12 .  filename  - name of the file, if .ext then uses name of draw object plus .ext using .ext to determine the image type, if NULL use .Gif image type
13 -  movie - produce a movie of all the images
14 
15    Options Database Command:
16 +  -draw_save  <filename> - filename could be name.ext or .ext (where .ext determines the type of graphics file to save, for example .Gif)
17 .  -draw_save_movie
18 .  -draw_save_final_image [optional filename] - (X windows only) saves the final image displayed in a window
19 -  -draw_save_single_file - saves each new image in the same file, normally each new image is saved in a new file with filename_%d
20 
21    Level: intermediate
22 
23    Concepts: X windows^graphics
24 
25    Notes: You should call this BEFORE creating your image and calling PetscDrawFlush().
26 
27    The ffmpeg utility must be in your path to make the movie.
28 
29    It is recommended that PETSc be configured with the option --with-afterimage to save the images. Otherwise, PETSc will write uncompressed, binary PPM files.
30    The .ext formats that are supported depend on what formats AfterImage was configured with; on the Apple Mac both .Gif and .Jpeg are supported.
31    If X windows generates an error message about X_CreateWindow() failing then Afterimage was installed without X windows.
32    Reinstall Afterimage using the following configure flags:
33    ./configure flags --x-includes=/pathtoXincludes --x-libraries=/pathtoXlibraries
34    For example under Mac OS X Mountain Lion --x-includes=/opt/X11/include -x-libraries=/opt/X11/lib
35 
36 .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawSetSaveFinalImage()
37 @*/
38 PetscErrorCode  PetscDrawSetSave(PetscDraw draw,const char *filename,PetscBool movie)
39 {
40   const char     *name = NULL;
41   const char     *ext = NULL;
42   char           buf[PETSC_MAX_PATH_LEN];
43   PetscErrorCode ierr;
44 
45   PetscFunctionBegin;
46   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
47   if (filename) PetscValidCharPointer(filename,2);
48 
49   /* determine filename and extension */
50   if (filename && filename[0]) {
51     ierr = PetscStrchr(filename,'.',(char **)&ext);CHKERRQ(ierr);
52     if (!ext) name = filename;
53     else if (ext != filename) {
54       size_t l1 = 0,l2 = 0;
55       ierr = PetscStrlen(filename,&l1);CHKERRQ(ierr);
56       ierr = PetscStrlen(ext,&l2);CHKERRQ(ierr);
57       ierr = PetscMemzero(buf,sizeof(buf));CHKERRQ(ierr);
58       ierr = PetscStrncpy(buf,filename,l1-l2+1);CHKERRQ(ierr);
59       name = buf;
60     }
61   }
62   if (!name) {ierr = PetscObjectGetName((PetscObject)draw,&name);CHKERRQ(ierr);}
63 #if defined(PETSC_HAVE_AFTERIMAGE)
64   if (!ext) ext = ".Gif";
65 #else
66   if (!ext) ext = ".ppm";
67   else {
68     PetscBool match;
69     ierr = PetscStrcasecmp(ext,".ppm",&match);CHKERRQ(ierr);
70     if (!match) SETERRQ1(PetscObjectComm((PetscObject)draw),PETSC_ERR_SUP,"Image extension %s not supported, use .ppm",ext);
71   }
72 #endif
73 
74   ierr = PetscFree(draw->savefilename);CHKERRQ(ierr);
75   ierr = PetscFree(draw->savefilenameext);CHKERRQ(ierr);
76   ierr = PetscStrallocpy(name,&draw->savefilename);CHKERRQ(ierr);
77   ierr = PetscStrallocpy(ext,&draw->savefilenameext);CHKERRQ(ierr);
78   draw->savefilemovie = movie;
79   if (draw->ops->setsave) {
80     ierr = (*draw->ops->setsave)(draw,draw->savefilename);CHKERRQ(ierr);
81   }
82 
83   if (draw->savesinglefile) {
84     ierr = PetscInfo2(NULL,"Will save image to file %s%s\n",draw->savefilename,draw->savefilenameext);CHKERRQ(ierr);
85   } else {
86     ierr = PetscInfo3(NULL,"Will save images to file %s/%s_*.%s\n",draw->savefilename,draw->savefilename,draw->savefilenameext);CHKERRQ(ierr);
87   }
88   if (draw->savefilemovie) {
89     ierr = PetscInfo1(NULL,"Will save movie to file %s.m4v\n",draw->savefilename);CHKERRQ(ierr);
90   }
91   PetscFunctionReturn(0);
92 }
93 
94 #undef __FUNCT__
95 #define __FUNCT__ "PetscDrawSetSaveFinalImage"
96 /*@C
97    PetscDrawSetSaveFinalImage - Saves the finale image produced in a PetscDraw into a file as a Gif file using AfterImage
98 
99    Collective on PetscDraw
100 
101    Input Parameter:
102 +  draw      - the graphics context
103 -  filename  - name of the file, if NULL uses name of draw object
104 
105    Options Database Command:
106 .  -draw_save_final_image  <filename>
107 
108    Level: intermediate
109 
110    Concepts: X windows^graphics
111 
112    Notes: You should call this BEFORE creating your image and calling PetscDrawFlush().
113 
114    It is recommended that PETSc be configured with the option --with-afterimage to save the images.
115    If X windows generates an error message about X_CreateWindow() failing then Afterimage was installed without X windows.
116    Reinstall Afterimage using the following configure flags:
117    ./configure flags --x-includes=/pathtoXincludes --x-libraries=/pathtoXlibraries
118    For example under Mac OS X Mountain Lion: --x-includes=/opt/X11/include -x-libraries=/opt/X11/lib
119 
120 .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawSetSave()
121 @*/
122 PetscErrorCode  PetscDrawSetSaveFinalImage(PetscDraw draw,const char *filename)
123 {
124   PetscErrorCode ierr;
125 
126   PetscFunctionBegin;
127   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
128 
129   ierr = PetscFree(draw->savefinalfilename);CHKERRQ(ierr);
130   if (filename && filename[0]) {
131     ierr = PetscStrallocpy(filename,&draw->savefinalfilename);CHKERRQ(ierr);
132   } else {
133     const char *name;
134     ierr = PetscObjectGetName((PetscObject)draw,&name);CHKERRQ(ierr);
135     ierr = PetscStrallocpy(name,&draw->savefinalfilename);CHKERRQ(ierr);
136   }
137   PetscFunctionReturn(0);
138 }
139 
140 #undef __FUNCT__
141 #define __FUNCT__ "PetscDrawSave"
142 /*@
143    PetscDrawSave - Saves a drawn image
144 
145    Collective on PetscDraw
146 
147    Input Parameters:
148 .  draw - the drawing context
149 
150    Level: advanced
151 
152    Notes: this is not normally called by the user, it is called by PetscDrawFlush() to save a sequence of images.
153 
154 .seealso: PetscDrawSetSave()
155 
156 @*/
157 PetscErrorCode  PetscDrawSave(PetscDraw draw)
158 {
159   PetscErrorCode ierr;
160 
161   PetscFunctionBegin;
162   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
163   if (draw->ops->save) {
164     ierr = (*draw->ops->save)(draw);CHKERRQ(ierr);
165   }
166   PetscFunctionReturn(0);
167 }
168