1 // screen-dump.cxx -- dump a copy of the opengl screen buffer to a file
3 // Contributed by Richard Kaszeta <bofh@me.umn.edu>, started October 1999.
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the
17 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 // Boston, MA 02111-1307, USA.
24 # include <simgear_config.h>
37 #include "screen-dump.hxx"
40 #define RGB3 3 // 3 bytes of color info per pixel
41 #define RGBA 4 // 4 bytes of color+alpha info
43 bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
46 unsigned char *ibuffer;
48 int pixelSize = mode==GL_RGBA?4:3;
50 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3);
52 if ( (fp = fopen(filename, "wb")) == NULL ) {
53 printf("Warning: cannot open %s\n", filename);
57 fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
58 win_width, win_height, UCHAR_MAX);
60 for (i = 0; i < win_height; i++)
61 for (j = 0; j < win_width; j++)
62 for (k = 0; k < RGB3; k++)
63 ibuffer[q++] = (unsigned char)
64 *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
65 fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp);
69 printf("wrote file (%d x %d pixels, %d bytes)\n",
70 win_width, win_height, RGB3*win_width*win_height);
75 // dump the screen buffer to a ppm file
76 bool sg_glDumpWindow(const char *filename, int win_width, int win_height) {
80 buffer = (GLubyte *) malloc(win_width*win_height*RGBA);
82 // read window contents from color buffer with glReadPixels
84 glReadPixels(0, 0, win_width, win_height,
85 GL_RGBA, GL_UNSIGNED_BYTE, buffer);
86 result = sg_glWritePPMFile( filename, buffer, win_width, win_height,