Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

SnapshotDisplayDevice.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2011 The Board of Trustees of the           
00004  *cr                        University of Illinois                       
00005  *cr                         All Rights Reserved                        
00006  *cr                                                                   
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: SnapshotDisplayDevice.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.44 $       $Date: 2011/03/02 18:30:07 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *   Dump the screen shot to a file by calling the machine specific external
00019  * program with the right options.  (Actually, someday the routines might
00020  * be put here as well; your preference.)
00021  *
00022  ***************************************************************************/
00023 
00024 // This turned out to be mroe painless than I expected
00025 // Basically, I already know the screen parameters since I'm a DisplayDevice,
00026 // and I got the info from "display".  It is just a matter of constructing
00027 // the correct string.
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include "ImageIO.h"
00032 #include "SnapshotDisplayDevice.h"
00033 #include "Inform.h"
00034 #include "utilities.h"
00035 #include "config.h"
00036 
00037 #if defined(_MSC_VER) || defined(WIN32)
00038 #define DEF_SNAPSHOT_FILENAME "vmdscene.bmp"
00039 #else
00040 #define DEF_SNAPSHOT_FILENAME "vmdscene.tga"
00041 #endif
00042 
00043 SnapshotDisplayDevice::SnapshotDisplayDevice(DisplayDevice *d) 
00044 : FileRenderer("snapshot", "Snapshot (VMD OpenGL window)", DEF_SNAPSHOT_FILENAME, DEF_VMDIMAGEVIEWER) {
00045   display = d; 
00046   // override default command line if environment variable set
00047   const char *envtxt = getenv("VMDIMAGEVIEWER");
00048   if (envtxt) {
00049     delete [] defaultCommandLine;
00050     defaultCommandLine = stringdup(envtxt);
00051     set_exec_string(envtxt); // change current exec command as well
00052   }
00053 }
00054 
00055 int SnapshotDisplayDevice::open_file(const char *filename) {
00056   if ((outfile = fopen(filename, "wb")) == NULL) {
00057     msgErr << "Could not open file " << filename
00058            << " in current directory for writing!" << sendmsg;
00059     return FALSE;
00060   }
00061   my_filename = stringdup(filename);
00062   isOpened = TRUE;
00063   return TRUE;
00064 }
00065 
00066 static int checkfileextension(const char * s, const char * extension) {
00067   int sz, extsz; 
00068   sz = strlen(s);
00069   extsz = strlen(extension); 
00070 
00071   if (extsz > sz)
00072     return 0;
00073 
00074   if (!strupncmp(s + (sz - extsz), extension, extsz)) {
00075     return 1;
00076   }
00077 
00078   return 0;
00079 }
00080 
00081 
00082 // construct the exec string, then system() it
00083 // pretty easy, eh?
00084 void SnapshotDisplayDevice::close_file(void) {
00085   int xs=0, ys=0;
00086   unsigned char * img;
00087 
00088   img = display->readpixels(xs, ys);
00089 
00090   // write the image to a file on disk
00091   if (checkfileextension(my_filename, ".bmp")) {
00092     vmd_writebmp(outfile, img, xs, ys);
00093 #if defined(VMDPNG)
00094   } else if (checkfileextension(my_filename, ".png")) {
00095     vmd_writepng(outfile, img, xs, ys);
00096 #endif
00097   } else if (checkfileextension(my_filename, ".ppm")) {
00098     vmd_writeppm(outfile, img, xs, ys);
00099   } else if (checkfileextension(my_filename, ".rgb")) {
00100     vmd_writergb(outfile, img, xs, ys);
00101   } else if (checkfileextension(my_filename, ".tga")) {
00102     vmd_writetga(outfile, img, xs, ys);
00103   } else {
00104 #if defined(_MSC_VER) || defined(WIN32)
00105     msgErr << "Unrecognized image file extension, writing Windows Bitmap file." 
00106            << sendmsg;
00107     vmd_writebmp(outfile, img, xs, ys);
00108 #else
00109     msgErr << "Unrecognized image file extension, writing Targa file." 
00110            << sendmsg;
00111     vmd_writetga(outfile, img, xs, ys);
00112 #endif
00113   }
00114 
00115   free(img); // free img memory block
00116   fclose(outfile);
00117   outfile = NULL;
00118   delete [] my_filename;
00119   my_filename = NULL;
00120   isOpened = FALSE;
00121 }
00122 

Generated on Sat May 26 01:48:27 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002