]> git.mxchange.org Git - simgear.git/blob - simgear/screen/screen-dump.cxx
Nick WARNE: add file name to screenshot info line
[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 General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19 // $Id$
20
21
22 #ifdef HAVE_CONFIG_H
23 #  include <simgear_config.h>
24 #endif
25
26 #if defined(__CYGWIN__)  /* && !defined(USING_X) */
27 #define WIN32
28 #endif
29
30 #if defined(WIN32)  /* MINGW and MSC predefine WIN32 */
31 # include <windows.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <limits.h>
37
38 #include <simgear/compiler.h>
39
40 #include SG_GL_H
41
42 #include "screen-dump.hxx"
43
44
45 #define RGB3 3                  // 3 bytes of color info per pixel
46 #define RGBA 4                  // 4 bytes of color+alpha info
47
48 bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
49 {
50     int i, j, k, q;
51     unsigned char *ibuffer;
52     FILE *fp;
53     int pixelSize = mode==GL_RGBA?4:3;
54
55     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3);
56
57     if ( (fp = fopen(filename, "wb")) == NULL ) {
58         printf("Warning: cannot open %s\n", filename);
59         return false;
60     }
61
62     fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
63             win_width, win_height, UCHAR_MAX);
64     q = 0;
65     for (i = 0; i < win_height; i++)
66         for (j = 0; j < win_width; j++)
67             for (k = 0; k < RGB3; k++)
68                 ibuffer[q++] = (unsigned char)
69                     *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
70     fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp);
71     fclose(fp);
72     free(ibuffer);
73
74     printf("wrote file '%s' (%d x %d pixels, %d bytes)\n",
75            filename, win_width, win_height, RGB3*win_width*win_height);
76     return true;
77 }
78
79
80 // dump the screen buffer to a ppm file
81 bool sg_glDumpWindow(const char *filename, int win_width, int win_height) {
82     GLubyte *buffer;
83     bool result;
84
85     buffer = (GLubyte *) malloc(win_width*win_height*RGBA);
86
87     // read window contents from color buffer with glReadPixels
88     glFinish();
89     glReadPixels(0, 0, win_width, win_height, 
90                  GL_RGBA, GL_UNSIGNED_BYTE, buffer);
91     result = sg_glWritePPMFile( filename, buffer, win_width, win_height,
92                                 GL_RGBA );
93     free(buffer);
94
95     return result;
96 }
97