Actual source code: dsave.c

      1: #include <petsc/private/drawimpl.h>

      3: PETSC_EXTERN PetscErrorCode PetscDrawImageSave(const char[],const char[],unsigned char[][3],unsigned int,unsigned int,const unsigned char[]);
      4: PETSC_EXTERN PetscErrorCode PetscDrawMovieSave(const char[],PetscInt,const char[],PetscInt,const char[]);
      5: PETSC_EXTERN PetscErrorCode PetscDrawImageCheckFormat(const char *[]);
      6: PETSC_EXTERN PetscErrorCode PetscDrawMovieCheckFormat(const char *[]);

      8: #if defined(PETSC_HAVE_SAWS)
      9: static PetscErrorCode PetscDrawSave_SAWs(PetscDraw);
     10: #endif

     12: /*@C
     13:    PetscDrawSetSave - Saves images produced in a PetscDraw into a file

     15:    Collective on PetscDraw

     17:    Input Parameter:
     18: +  draw      - the graphics context
     19: -  filename  - name of the file, if .ext then uses name of draw object plus .ext using .ext to determine the image type

     21:    Options Database Command:
     22: +  -draw_save <filename>  - filename could be name.ext or .ext (where .ext determines the type of graphics file to save, for example .png)
     23: .  -draw_save_final_image [optional filename] - saves the final image displayed in a window
     24: -  -draw_save_single_file - saves each new image in the same file, normally each new image is saved in a new file with filename/filename_%d.ext

     26:    Level: intermediate

     28:    Notes:
     29:     You should call this BEFORE creating your image and calling PetscDrawSave().
     30:    The supported image types are .png, .gif, .jpg, and .ppm (PETSc chooses the default in that order).
     31:    Support for .png images requires configure --with-libpng.
     32:    Support for .gif images requires configure --with-giflib.
     33:    Support for .jpg images requires configure --with-libjpeg.
     34:    Support for .ppm images is built-in. The PPM format has no compression (640x480 pixels ~ 900 KiB).

     36: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawSetSaveFinalImage()
     37: @*/
     38: PetscErrorCode  PetscDrawSetSave(PetscDraw draw,const char filename[])
     39: {
     40:   const char     *savename = NULL;
     41:   const char     *imageext = NULL;
     42:   char           buf[PETSC_MAX_PATH_LEN];

     49:   /* determine save filename and image extension */
     50:   if (filename && filename[0]) {
     51:     PetscStrchr(filename,'.',(char **)&imageext);
     52:     if (!imageext) savename = filename;
     53:     else if (imageext != filename) {
     54:       size_t l1 = 0,l2 = 0;
     55:       PetscStrlen(filename,&l1);
     56:       PetscStrlen(imageext,&l2);
     57:       PetscStrncpy(buf,filename,l1-l2+1);
     58:       savename = buf;
     59:     }
     60:   }

     62:   if (!savename) {PetscObjectGetName((PetscObject)draw,&savename);}
     63:   PetscDrawImageCheckFormat(&imageext);

     65:   draw->savefilecount = 0;
     66:   PetscFree(draw->savefilename);
     67:   PetscFree(draw->saveimageext);
     68:   PetscStrallocpy(savename,&draw->savefilename);
     69:   PetscStrallocpy(imageext,&draw->saveimageext);

     71:   if (draw->savesinglefile) {
     72:     PetscInfo2(NULL,"Will save image to file %s%s\n",draw->savefilename,draw->saveimageext);
     73:   } else {
     74:     PetscInfo3(NULL,"Will save images to file %s/%s_%%d%s\n",draw->savefilename,draw->savefilename,draw->saveimageext);
     75:   }
     76:   return(0);
     77: }

     79: /*@C
     80:    PetscDrawSetSaveMovie - Saves a movie produced from a PetscDraw into a file

     82:    Collective on PetscDraw

     84:    Input Parameter:
     85: +  draw      - the graphics context
     86: -  movieext  - optional extension defining the movie format

     88:    Options Database Command:
     89: .  -draw_save_movie <.ext> - saves a movie with extension .ext

     91:    Level: intermediate

     93:    Notes:
     94:     You should call this AFTER calling PetscDrawSetSave() and BEFORE creating your image with PetscDrawSave().
     95:    The ffmpeg utility must be in your path to make the movie.

     97: .seealso: PetscDrawSetSave(), PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
     98: @*/
     99: PetscErrorCode  PetscDrawSetSaveMovie(PetscDraw draw,const char movieext[])
    100: {

    108:   if (!draw->savefilename) {PetscDrawSetSave(draw,"");}
    109:   PetscDrawMovieCheckFormat(&movieext);
    110:   PetscStrallocpy(movieext,&draw->savemovieext);
    111:   draw->savesinglefile = PETSC_FALSE; /* otherwise we cannot generage movies */

    113:   PetscInfo2(NULL,"Will save movie to file %s%s\n",draw->savefilename,draw->savemovieext);
    114:   return(0);
    115: }

    117: /*@C
    118:    PetscDrawSetSaveFinalImage - Saves the final image produced in a PetscDraw into a file

    120:    Collective on PetscDraw

    122:    Input Parameter:
    123: +  draw      - the graphics context
    124: -  filename  - name of the file, if NULL or empty uses name set with PetscDrawSetSave() or name of draw object

    126:    Options Database Command:
    127: .  -draw_save_final_image  <filename> - filename could be name.ext or .ext (where .ext determines the type of graphics file to save, for example .png)

    129:    Level: intermediate

    131:    Notes:
    132:     You should call this BEFORE creating your image and calling PetscDrawSave().
    133:    The supported image types are .png, .gif, and .ppm (PETSc chooses the default in that order).
    134:    Support for .png images requires configure --with-libpng.
    135:    Support for .gif images requires configure --with-giflib.
    136:    Support for .jpg images requires configure --with-libjpeg.
    137:    Support for .ppm images is built-in. The PPM format has no compression (640x480 pixels ~ 900 KiB).

    139: .seealso: PetscDrawSetSave(), PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
    140: @*/
    141: PetscErrorCode  PetscDrawSetSaveFinalImage(PetscDraw draw,const char filename[])
    142: {
    143:   char           buf[PETSC_MAX_PATH_LEN];

    148:   if (!filename || !filename[0]) {
    149:     if (!draw->savefilename) {
    150:       PetscObjectGetName((PetscObject)draw,&filename);
    151:     } else {
    152:       PetscSNPrintf(buf,sizeof(buf),"%s%s",draw->savefilename,draw->saveimageext);
    153:       filename = buf;
    154:     }
    155:   }
    156:   PetscFree(draw->savefinalfilename);
    157:   PetscStrallocpy(filename,&draw->savefinalfilename);
    158:   return(0);
    159: }

    161: /*@
    162:    PetscDrawSave - Saves a drawn image

    164:    Collective on PetscDraw

    166:    Input Parameters:
    167: .  draw - the drawing context

    169:    Level: advanced

    171:    Notes:
    172:     this is not normally called by the user.

    174: .seealso: PetscDrawSetSave()

    176: @*/
    177: PetscErrorCode  PetscDrawSave(PetscDraw draw)
    178: {
    179:   PetscInt       saveindex;
    180:   char           basename[PETSC_MAX_PATH_LEN];
    181:   unsigned char  palette[256][3];
    182:   unsigned int   w,h;
    183:   unsigned char  *pixels = NULL;
    184:   PetscMPIInt    rank;

    189:   if (!draw->ops->save && !draw->ops->getimage) return(0);
    190:   if (draw->ops->save) {(*draw->ops->save)(draw); goto finally;}
    191:   if (!draw->savefilename || !draw->saveimageext) return(0);
    192:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);

    194:   saveindex = draw->savefilecount++;

    196:   if (!rank && !saveindex) {
    197:     char path[PETSC_MAX_PATH_LEN];
    198:     if (draw->savesinglefile) {
    199:       PetscSNPrintf(path,sizeof(path),"%s%s",draw->savefilename,draw->saveimageext);
    200:       (void)remove(path);
    201:     } else {
    202:       PetscSNPrintf(path,sizeof(path),"%s",draw->savefilename);
    203:       PetscRMTree(path);
    204:       PetscMkdir(path);
    205:     }
    206:     if (draw->savemovieext) {
    207:       PetscSNPrintf(path,sizeof(path),"%s%s",draw->savefilename,draw->savemovieext);
    208:       (void)remove(path);
    209:     }
    210:   }
    211:   if (draw->savesinglefile) {
    212:     PetscSNPrintf(basename,sizeof(basename),"%s",draw->savefilename);
    213:   } else {
    214:     char *basefilename;

    216:     PetscStrrchr(draw->savefilename, '/', (char **) &basefilename);
    217:     if (basefilename != draw->savefilename) {
    218:       PetscSNPrintf(basename,sizeof(basename),"%s_%d",draw->savefilename,(int)saveindex);
    219:     } else {
    220:       PetscSNPrintf(basename,sizeof(basename),"%s/%s_%d",draw->savefilename,draw->savefilename,(int)saveindex);
    221:     }
    222:   }

    224:   /* this call is collective, only the first process gets the image data */
    225:   (*draw->ops->getimage)(draw,palette,&w,&h,&pixels);
    226:   /* only the first process handles the saving business */
    227:   if (!rank) {PetscDrawImageSave(basename,draw->saveimageext,palette,w,h,pixels);}
    228:   PetscFree(pixels);
    229:   MPI_Barrier(PetscObjectComm((PetscObject)draw));

    231: finally:
    232: #if defined(PETSC_HAVE_SAWS)
    233:   PetscDrawSave_SAWs(draw);
    234: #endif
    235:   return(0);
    236: }

    238: /*@
    239:    PetscDrawSaveMovie - Saves a movie from previously saved images

    241:    Collective on PetscDraw

    243:    Input Parameters:
    244: .  draw - the drawing context

    246:    Level: advanced

    248:    Notes:
    249:     this is not normally called by the user.
    250:    The ffmpeg utility must be in your path to make the movie.

    252: .seealso: PetscDrawSetSave(), PetscDrawSetSaveMovie()

    254: @*/
    255: PetscErrorCode PetscDrawSaveMovie(PetscDraw draw)
    256: {
    257:   PetscMPIInt    rank;

    262:   if (!draw->ops->save && !draw->ops->getimage) return(0);
    263:   if (!draw->savefilename || !draw->savemovieext || draw->savesinglefile) return(0);
    264:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);
    265:   {
    266:     const char *fname = draw->savefilename;
    267:     const char *imext = draw->saveimageext;
    268:     const char *mvext = draw->savemovieext;
    269:     if (!rank) {PetscDrawMovieSave(fname,draw->savefilecount,imext,draw->savemoviefps,mvext);}
    270:     MPI_Barrier(PetscObjectComm((PetscObject)draw));
    271:   }
    272:   return(0);
    273: }

    276: #if defined(PETSC_HAVE_SAWS)
    277: #include <petscviewersaws.h>
    278: /*
    279:   The PetscImageList object and functions are used to maintain a list of file images
    280:   that can be displayed by the SAWs webserver.
    281: */
    282: typedef struct _P_PetscImageList *PetscImageList;
    283: struct _P_PetscImageList {
    284:   PetscImageList next;
    285:   char           *filename;
    286:   char           *ext;
    287:   PetscInt       count;
    288: } ;

    290: static PetscImageList SAWs_images = NULL;

    292: static PetscErrorCode PetscImageListDestroy(void)
    293: {
    295:   PetscImageList image = SAWs_images;

    298:   while (image) {
    299:     PetscImageList next = image->next;
    300:     PetscFree(image->filename);
    301:     PetscFree(image->ext);
    302:     PetscFree(image);
    303:     image = next;
    304:   }
    305:   return(0);
    306: }

    308: static PetscErrorCode PetscImageListAdd(const char filename[],const char ext[],PetscInt count)
    309: {
    310:   PetscErrorCode  ierr;
    311:   PetscImageList  image,oimage = SAWs_images;
    312:   PetscBool       flg;

    315:   if (oimage) {
    316:     PetscStrcmp(filename,oimage->filename,&flg);
    317:     if (flg) {
    318:       oimage->count = count;
    319:       return(0);
    320:     }
    321:     while (oimage->next) {
    322:       oimage = oimage->next;
    323:       PetscStrcmp(filename,oimage->filename,&flg);
    324:       if (flg) {
    325:         oimage->count = count;
    326:         return(0);
    327:       }
    328:     }
    329:     PetscNew(&image);
    330:     oimage->next = image;
    331:   } else {
    332:     PetscRegisterFinalize(PetscImageListDestroy);
    333:     PetscNew(&image);
    334:     SAWs_images = image;
    335:   }
    336:   PetscStrallocpy(filename,&image->filename);
    337:   PetscStrallocpy(ext,&image->ext);
    338:   image->count = count;
    339:   return(0);
    340: }

    342: static PetscErrorCode PetscDrawSave_SAWs(PetscDraw draw)
    343: {
    344:   PetscImageList image;
    345:   char           body[4096];
    346:   size_t         len = 0;

    350:   if (!draw->savefilename || !draw->saveimageext) return(0);
    351:   PetscImageListAdd(draw->savefilename,draw->saveimageext,draw->savefilecount-1);
    352:   image = SAWs_images;
    353:   while (image) {
    354:     const char *name = image->filename;
    355:     const char *ext  = image->ext;
    356:     if (draw->savesinglefile) {
    357:       PetscSNPrintf(body+len,4086-len,"<img src=\"%s%s\" alt=\"None\">",name,ext);
    358:     } else {
    359:       PetscSNPrintf(body+len,4086-len,"<img src=\"%s/%s_%d%s\" alt=\"None\">",name,name,image->count,ext);
    360:     }
    361:     PetscStrlen(body,&len);
    362:     image = image->next;
    363:   }
    364:   PetscStrlcat(body,"<br>\n",sizeof(body));
    365:   if (draw->savefilecount > 0) PetscStackCallSAWs(SAWs_Pop_Body,("index.html",1));
    366:   PetscStackCallSAWs(SAWs_Push_Body,("index.html",1,body));
    367:   return(0);
    368: }

    370: #endif