]> git.mxchange.org Git - simgear.git/blob - simgear/screen/screen-dump.cxx
d6a62ba05b48e95160c25355e2610408d5316aff
[simgear.git] / simgear / screen / screen-dump.cxx
1 // screen-dump.cxx -- dump a copy of the opengl screen buffer to a file
2 //
3 // Contributed by Richard Kaszeta <bofh@me.umn.edu>, started October 1999.
4 //
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.
9 //
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.
14 //
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.
19 //
20 // $Id$
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #if defined(__CYGWIN__)  /* && !defined(USING_X) */
28 #define WIN32
29 #endif
30
31 #if defined(WIN32)  /* MINGW and MSC predefine WIN32 */
32 # include <windows.h>
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <limits.h>
38
39 #include <GL/gl.h>
40
41 #include "screen-dump.hxx"
42
43
44 #define RGB3 3                  // 3 bytes of color info per pixel
45 #define RGBA 4                  // 4 bytes of color+alpha info
46
47 bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
48 {
49     int i, j, k, q;
50     unsigned char *ibuffer;
51     FILE *fp;
52     int pixelSize = mode==GL_RGBA?4:3;
53
54     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3);
55
56     if ( (fp = fopen(filename, "wb")) == NULL ) {
57         printf("Warning: cannot open %s\n", filename);
58         return false;
59     }
60
61     fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
62             win_width, win_height, UCHAR_MAX);
63     q = 0;
64     for (i = 0; i < win_height; i++)
65         for (j = 0; j < win_width; j++)
66             for (k = 0; k < RGB3; k++)
67                 ibuffer[q++] = (unsigned char)
68                     *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
69     fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp);
70     fclose(fp);
71     free(ibuffer);
72
73     printf("wrote file (%d x %d pixels, %d bytes)\n",
74            win_width, win_height, RGB3*win_width*win_height);
75     return true;
76 }
77
78
79 // dump the screen buffer to a ppm file
80 bool sg_glDumpWindow(const char *filename, int win_width, int win_height) {
81     GLubyte *buffer;
82     bool result;
83
84     buffer = (GLubyte *) malloc(win_width*win_height*RGBA);
85
86     // read window contents from color buffer with glReadPixels
87     glFinish();
88     glReadPixels(0, 0, win_width, win_height, 
89                  GL_RGBA, GL_UNSIGNED_BYTE, buffer);
90     result = sg_glWritePPMFile( filename, buffer, win_width, win_height,
91                                 GL_RGBA );
92     free(buffer);
93
94     return result;
95 }
96