GLSnapshot.cpp

00001 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
00002 /* 
00003  * Copyright (C) 2008 Micha Hersch, EPFL
00004  * RobotCub Consortium, European Commission FP6 Project IST-004370
00005  * email:   micha.hersch@robotcub.org
00006  * website: www.robotcub.org
00007  * Permission is granted to copy, distribute, and/or modify this program
00008  * under the terms of the GNU General Public License, version 2 or any
00009  * later version published by the Free Software Foundation.
00010  *
00011  * A copy of the license can be found at
00012  * http://www.robotcub.org/icub/license/gpl.txt
00013  *
00014  * This program is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00017  * Public License for more details
00018  */
00019 #include "GLSnapshot.h"
00020 //#include "gl2ps.h"
00021 
00022 
00023 
00024 GLSnapshot::GLSnapshot(){
00025   m_File    = NULL;
00026   m_Mode    = GLSnap_NONE;
00027   m_Width   = 100;
00028   m_Height  = 100;
00029 }
00030 
00031 GLSnapshot::~GLSnapshot(){
00032   if(m_File!=NULL)
00033     Finish();
00034 }
00035 
00036 void  GLSnapshot::SetViewport(int width, int height){
00037   m_Width   = width;
00038   m_Height  = height;
00039 }
00040 void  GLSnapshot::SetFilename(const char *filename){
00041   strcpy(m_Filename,filename);
00042 }
00043 void  GLSnapshot::SetMode(GLSnapshotMode mode){
00044   m_Mode = mode;
00045 }
00046 
00047 int GLSnapshot::Begin(){
00048   if(m_File!=NULL)
00049     Finish();
00050 
00051   if(m_Mode==GLSnap_NONE)
00052     return TRUE;
00053 
00054   m_File = fopen(m_Filename, "wb");
00055   
00056   if(m_File==NULL)
00057     return FALSE;
00058 
00059   switch(m_Mode){
00060   case GLSnap_NONE:
00061     break;
00062 //   case GLSnap_SVG:
00063 //   case GLSnap_PDF:
00064   case GLSnap_EPS: {
00065 
00066 
00067     GLint viewport[4];
00068     viewport[0] = 0; viewport[1] = 0; viewport[2] = 800; viewport[3] = 600;
00069 
00070     int options   = GL2PS_USE_CURRENT_VIEWPORT;// | GL2PS_OCCLUSION_CULL;
00071     int buffsize  = 1024*1024;
00072     int nbColors  = 0;
00073     int sort      = GL2PS_SIMPLE_SORT;//GL2PS_SIMPLE_SORT;//GL2PS_BSP_SORT; //GL2PS_NO_SORT,
00074 
00075     int ret;
00076     ret = gl2psBeginPage(m_Filename,
00077                    "GLSnapshot",
00078                    NULL, 
00079                    GL2PS_EPS, 
00080                    sort, 
00081                    options,
00082                    GL_RGBA,
00083                    0,
00084                    NULL,
00085                    nbColors, nbColors, nbColors,
00086                    buffsize, 
00087                    m_File, m_Filename);
00088   }
00089     break;
00090   case GLSnap_RGB:
00091     break;
00092   }
00093   return TRUE;
00094 }
00095 
00096 // Fonctions annexes pour la sauvegarde
00097 #define PUT_BYTE(outFile,val)  { unsigned char buf[1];    \
00098                                  buf[0] = (char)(val);            \
00099                                  fwrite(buf,1,1,outFile); }
00100 
00101 #define PUT_SHORT(outFile,val) { unsigned char buf[2];    \
00102                                  buf[0] = (char)((val) >> 8);       \
00103                                  buf[1] = (char)((val) >> 0);       \
00104                                  fwrite(buf,2,1,outFile); }
00105 
00106 #define PUT_LONG(outFile,val)  { unsigned char buf[4];    \
00107                                  buf[0] = (char)((val) >> 24);      \
00108                                  buf[1] = (char)((val) >> 16);      \
00109                                  buf[2] = (char)((val) >> 8);       \
00110                                  buf[3] = (char)((val) >> 0);       \
00111                                  fwrite(buf,4,1,outFile); } 
00112 
00113 int GLSnapshot::Finish(){
00114   if(m_File!=NULL){
00115     switch(m_Mode){
00116     case GLSnap_NONE:
00117       break;
00118 //     case GLSnap_SVG:
00119 //     case GLSnap_PDF:
00120     case GLSnap_EPS:
00121       int ret;
00122       ret = gl2psEndPage();
00123       break;
00124     case GLSnap_RGB: {      
00125       GLubyte * imDataR=NULL;
00126       GLubyte * imDataG=NULL;
00127       GLubyte * imDataB=NULL;
00128 
00129       char iname[80];
00130       int  i,x,y,z;
00131 
00132       imDataR = (GLubyte*) malloc(m_Width*m_Height*sizeof(GLubyte));
00133       imDataG = (GLubyte*) malloc(m_Width*m_Height*sizeof(GLubyte));
00134       imDataB = (GLubyte*) malloc(m_Width*m_Height*sizeof(GLubyte));
00135 
00136       glReadPixels(0,0,m_Width,m_Height, GL_RED,   GL_UNSIGNED_BYTE, imDataR);
00137       glReadPixels(0,0,m_Width,m_Height, GL_GREEN, GL_UNSIGNED_BYTE, imDataG);
00138       glReadPixels(0,0,m_Width,m_Height, GL_BLUE,  GL_UNSIGNED_BYTE, imDataB);
00139   
00140 
00141       PUT_SHORT(m_File,(short)474);       /* MAGIC                       */
00142       PUT_BYTE (m_File,(char) 0);         /* STORAGE is VERBATIM         */
00143       PUT_BYTE (m_File,(char) 1);         /* BPC is 1                    */
00144       PUT_SHORT(m_File,(short)3);         /* DIMENSION is 2              */
00145       PUT_SHORT(m_File,(short)m_Width);         /* XSIZE                       */
00146       PUT_SHORT(m_File,(short)m_Height);         /* YSIZE                       */
00147       PUT_SHORT(m_File,(short)3);         /* ZSIZE                       */
00148       PUT_LONG (m_File,(long) 0);         /* PIXMIN is 0                 */
00149       PUT_LONG (m_File,(long) 255);       /* PIXMAX is 255               */
00150       for(i=0; i<4; i++)       /* DUMMY 4 bytes               */
00151         PUT_BYTE(m_File,(char) 0);
00152   
00153       strcpy(iname,"GLSnapshot");
00154       fwrite(iname,80,1,m_File);   /* IMAGENAME                   */
00155       PUT_LONG(m_File,(long) 0);          /* COLORMAP is 0               */
00156       for(i=0; i<(404+12); i++)
00157         PUT_BYTE(m_File,(char) 0);        /* DUMMY 404 bytes             */
00158   
00159       for(z=0;z<3;z++){
00160         i=0;
00161         for(y=0;y<m_Height;y++){
00162           for(x=0;x<m_Width;x++){       
00163                   switch(z){
00164                   case 0:
00165                     PUT_BYTE(m_File,(char)imDataR[i]);
00166                       break;
00167                   case 1:
00168                     PUT_BYTE(m_File,(char)imDataG[i]);
00169                     break;
00170                   case 2:
00171                     PUT_BYTE(m_File,(char)imDataB[i]);
00172                     break;
00173                   }
00174                   i++;
00175           }
00176         }
00177       }
00178    
00179       free(imDataR);
00180       free(imDataG);
00181       free(imDataB);
00182     }
00183       break;
00184     }
00185     fclose(m_File);
00186     m_File = NULL;
00187   }
00188   return TRUE;
00189 }
00190 
00191 
00192 
 All Data Structures Functions Variables

Generated on Wed Sep 22 16:51:26 2010 for Body_Schema_Learning by  doxygen 1.6.1