head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.39; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.56; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.03; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.59; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# 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 Library General Public
    00016 // License along with this library; if not, write to the
    00017 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018 // Boston, MA  02111-1307, USA.
    00019 //
    00020 // $Id: screen-dump.cxx,v 1.7 2001/05/15 19:26:24 curt Exp $
    00021
    00022
    00023 #ifdef HAVE_CONFIG_H
    00024 #  include <simgear_config.h>
    00025 #endif
    00026
    00027 #ifdef HAVE_WINDOWS_H
    00028 #  include <windows.h>
    00029 #endif
    00030
    00031 #include <stdio.h>
    00032 #include <stdlib.h>
    00033 #include <limits.h>
    00034
    00035 #include <GL/glut.h>
    00036 #include <simgear/xgl/xgl.h>
    00037
    00038 #include "screen-dump.hxx"
    00039
    00040
    00041 #define RGB3 3                  // 3 bytes of color info per pixel
    00042 #define RGBA 4                  // 4 bytes of color+alpha info
    00043
    00044 bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
    00045 {
    00046     int i, j, k, q;
    00047     unsigned char *ibuffer;
    00048     FILE *fp;
    00049     int pixelSize = mode==GL_RGBA?4:3;
    00050
    00051     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3);
    00052
    00053     if ( (fp = fopen(filename, "wb")) == NULL ) {
    00054         printf("Warning: cannot open %s\n", filename);
    00055         return false;
    00056     }
    00057
    00058     fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
    00059             win_width, win_height, UCHAR_MAX);
    00060     q = 0;
    00061     for (i = 0; i < win_height; i++)
    00062         for (j = 0; j < win_width; j++)
    00063             for (k = 0; k < RGB3; k++)
    00064                 ibuffer[q++] = (unsigned char)
    00065                     *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
    00066     fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp);
    00067     fclose(fp);
    00068     free(ibuffer);
    00069
    00070     printf("wrote file (%d x %d pixels, %d bytes)\n",
    00071            win_width, win_height, RGB3*win_width*win_height);
    00072     return true;
    00073 }
    00074
    00075
    00076 // dump the screen buffer to a ppm file
    00077 bool sg_glDumpWindow(const char *filename, int win_width, int win_height) {
    00078     GLubyte *buffer;
    00079     bool result;
    00080
    00081     buffer = (GLubyte *) malloc(win_width*win_height*RGBA);
    00082
    00083     // read window contents from color buffer with glReadPixels
    00084     glFinish();
    00085     glReadPixels(0, 0, win_width, win_height,
    00086                  GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    00087     result = sg_glWritePPMFile( filename, buffer, win_width, win_height,
    00088                                 GL_RGBA );
    00089     free(buffer);
    00090
    00091     return result;
    00092 }
    00093

@ 1.3 log @Oops, try again. @ text @d102 1 a102 1