Capture.cpp
Go to the documentation of this file.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 #include <sofa/helper/gl/Capture.h>
00028 #include <sofa/helper/io/ImageBMP.h>
00029 #ifdef SOFA_HAVE_PNG
00030 #include <sofa/helper/io/ImagePNG.h>
00031 #endif
00032
00033 #include <cstdio>
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036
00037 namespace sofa
00038 {
00039
00040 namespace helper
00041 {
00042
00043 namespace gl
00044 {
00045
00046 Capture::Capture()
00047 : prefix("capture"), counter(-1)
00048 {
00049 }
00050
00051 bool Capture::saveScreen(const std::string& filename, int compression_level)
00052 {
00053 #ifdef SOFA_HAVE_PNG
00054 io::ImagePNG img;
00055 #else
00056 io::ImageBMP img;
00057 #endif
00058 GLint viewport[4];
00059 glGetIntegerv(GL_VIEWPORT,viewport);
00060 img.init(viewport[2],viewport[3],24);
00061 glReadBuffer(GL_FRONT);
00062 glPixelStorei(GL_PACK_ALIGNMENT, 1);
00063 glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, img.getData());
00064
00065 if (!img.save(filename, compression_level)) return false;
00066 std::cout << "Saved "<<img.getWidth()<<"x"<<img.getHeight()<<" screen image to "<<filename<<std::endl;
00067 glReadBuffer(GL_BACK);
00068 return true;
00069 }
00070
00071 std::string Capture::findFilename()
00072 {
00073 std::string filename;
00074 char buf[32];
00075 int c;
00076 c = 0;
00077 struct stat st;
00078 do
00079 {
00080 ++c;
00081 sprintf(buf, "%04d",c);
00082 filename = prefix;
00083 filename += buf;
00084 #ifdef SOFA_HAVE_PNG
00085 filename += ".png";
00086 #else
00087 filename += ".bmp";
00088 #endif
00089 }
00090 while (stat(filename.c_str(),&st)==0);
00091 counter = c+1;
00092
00093 sprintf(buf, "%04d",c);
00094 filename = prefix;
00095 filename += buf;
00096 #ifdef SOFA_HAVE_PNG
00097 filename += ".png";
00098 #else
00099 filename += ".bmp";
00100 #endif
00101
00102 return filename;
00103 }
00104
00105
00106 bool Capture::saveScreen(int compression_level)
00107 {
00108 return saveScreen(findFilename(), compression_level);
00109 }
00110
00111 }
00112
00113 }
00114
00115 }
00116