screen-dump.cxx

00001 // screen-dump.cxx -- dump a copy of the opengl screen buffer to a file
00002 //
00003 // Contributed by Richard Kaszeta <bofh@me.umn.edu>, started October 1999.
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Library General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Library General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 //
00019 // $Id: screen-dump_8cxx-source.html,v 1.15 2007-12-17 15:37:10 curt Exp $
00020 
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #  include <simgear_config.h>
00024 #endif
00025 
00026 #if defined(__CYGWIN__)  /* && !defined(USING_X) */
00027 #define WIN32
00028 #endif
00029 
00030 #if defined(WIN32)  /* MINGW and MSC predefine WIN32 */
00031 # include <windows.h>
00032 #endif
00033 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <limits.h>
00037 
00038 #include <simgear/compiler.h>
00039 
00040 #include SG_GL_H
00041 
00042 #include "screen-dump.hxx"
00043 
00044 
00045 #define RGB3 3                  // 3 bytes of color info per pixel
00046 #define RGBA 4                  // 4 bytes of color+alpha info
00047 
00048 bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
00049 {
00050     int i, j, k, q;
00051     unsigned char *ibuffer;
00052     FILE *fp;
00053     int pixelSize = mode==GL_RGBA?4:3;
00054 
00055     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3);
00056 
00057     if ( (fp = fopen(filename, "wb")) == NULL ) {
00058         printf("Warning: cannot open %s\n", filename);
00059         return false;
00060     }
00061 
00062     fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
00063             win_width, win_height, UCHAR_MAX);
00064     q = 0;
00065     for (i = 0; i < win_height; i++)
00066         for (j = 0; j < win_width; j++)
00067             for (k = 0; k < RGB3; k++)
00068                 ibuffer[q++] = (unsigned char)
00069                     *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
00070     fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp);
00071     fclose(fp);
00072     free(ibuffer);
00073 
00074     printf("wrote file '%s' (%d x %d pixels, %d bytes)\n",
00075            filename, win_width, win_height, RGB3*win_width*win_height);
00076     return true;
00077 }
00078 
00079 
00080 // dump the screen buffer to a ppm file
00081 bool sg_glDumpWindow(const char *filename, int win_width, int win_height) {
00082     GLubyte *buffer;
00083     bool result;
00084 
00085     buffer = (GLubyte *) malloc(win_width*win_height*RGBA);
00086 
00087     // read window contents from color buffer with glReadPixels
00088     glFinish();
00089     glReadPixels(0, 0, win_width, win_height, 
00090                  GL_RGBA, GL_UNSIGNED_BYTE, buffer);
00091     result = sg_glWritePPMFile( filename, buffer, win_width, win_height,
00092                                 GL_RGBA );
00093     free(buffer);
00094 
00095     return result;
00096 }
00097 

Generated on Mon Dec 17 09:30:55 2007 for SimGear by  doxygen 1.5.1