00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include "ImageIO.h"
00028 #include "SnapshotDisplayDevice.h"
00029 #include "Inform.h"
00030 #include "utilities.h"
00031 #include "config.h"
00032
00033 #if defined(_MSC_VER) || defined(WIN32)
00034 #define DEF_SNAPSHOT_FILENAME "vmdscene.bmp"
00035 #else
00036 #define DEF_SNAPSHOT_FILENAME "vmdscene.tga"
00037 #endif
00038
00039 SnapshotDisplayDevice::SnapshotDisplayDevice(DisplayDevice *d)
00040 : FileRenderer("snapshot", "Snapshot (VMD OpenGL window)", DEF_SNAPSHOT_FILENAME, DEF_VMDIMAGEVIEWER) {
00041 display = d;
00042
00043 const char *envtxt = getenv("VMDIMAGEVIEWER");
00044 if (envtxt) {
00045 delete [] defaultCommandLine;
00046 defaultCommandLine = stringdup(envtxt);
00047 set_exec_string(envtxt);
00048 }
00049 }
00050
00051 int SnapshotDisplayDevice::open_file(const char *filename) {
00052 if ((outfile = fopen(filename, "wb")) == NULL) {
00053 msgErr << "Could not open file " << filename
00054 << " in current directory for writing!" << sendmsg;
00055 return FALSE;
00056 }
00057 my_filename = stringdup(filename);
00058 isOpened = TRUE;
00059 return TRUE;
00060 }
00061
00062 static int checkfileextension(const char * s, const char * extension) {
00063 int sz, extsz;
00064 sz = strlen(s);
00065 extsz = strlen(extension);
00066
00067 if (extsz > sz)
00068 return 0;
00069
00070 if (!strupncmp(s + (sz - extsz), extension, extsz)) {
00071 return 1;
00072 }
00073
00074 return 0;
00075 }
00076
00077
00078
00079
00080 void SnapshotDisplayDevice::close_file(void) {
00081 int xs=0, ys=0;
00082 unsigned char * img;
00083
00084 img = display->readpixels_rgb3u(xs, ys);
00085
00086
00087 if (checkfileextension(my_filename, ".bmp")) {
00088 vmd_writebmp(outfile, img, xs, ys);
00089 #if defined(VMDLIBPNG)
00090 } else if (checkfileextension(my_filename, ".png")) {
00091 vmd_writepng(outfile, img, xs, ys);
00092 #endif
00093 } else if (checkfileextension(my_filename, ".ppm")) {
00094 vmd_writeppm(outfile, img, xs, ys);
00095 } else if (checkfileextension(my_filename, ".rgb")) {
00096 vmd_writergb(outfile, img, xs, ys);
00097 } else if (checkfileextension(my_filename, ".tga")) {
00098 vmd_writetga(outfile, img, xs, ys);
00099 } else {
00100 #if defined(_MSC_VER) || defined(WIN32)
00101 msgErr << "Unrecognized image file extension, writing Windows Bitmap file."
00102 << sendmsg;
00103 vmd_writebmp(outfile, img, xs, ys);
00104 #else
00105 msgErr << "Unrecognized image file extension, writing Targa file."
00106 << sendmsg;
00107 vmd_writetga(outfile, img, xs, ys);
00108 #endif
00109 }
00110
00111 free(img);
00112 fclose(outfile);
00113 outfile = NULL;
00114 delete [] my_filename;
00115 my_filename = NULL;
00116 isOpened = FALSE;
00117 }
00118