00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00047 const char *envtxt = getenv("VMDIMAGEVIEWER");
00048 if (envtxt) {
00049 delete [] defaultCommandLine;
00050 defaultCommandLine = stringdup(envtxt);
00051 set_exec_string(envtxt);
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
00083
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
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);
00116 fclose(outfile);
00117 outfile = NULL;
00118 delete [] my_filename;
00119 my_filename = NULL;
00120 isOpened = FALSE;
00121 }
00122