Capture.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
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         } // namespace gl
00112 
00113     } // namespace helper
00114 
00115 } // namespace sofa
00116 

Generated on Sun Nov 23 02:29:29 2008 for Sofa by  doxygen 1.5.7.1