]> git.mxchange.org Git - simgear.git/blob - simgear/screen/screen-dump.cxx
Patch from Melchior Franz:
[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 #ifdef HAVE_WINDOWS_H
28 #  include <windows.h>                     
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <limits.h>
34
35 #include <GL/glut.h>
36 #include <simgear/xgl/xgl.h>
37
38 #include "screen-dump.hxx"
39
40
41 #define RGB3 3                  // 3 bytes of color info per pixel
42 #define RGBA 4                  // 4 bytes of color+alpha info
43
44 bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
45 {
46     int i, j, k, q;
47     unsigned char *ibuffer;
48     FILE *fp;
49     int pixelSize = mode==GL_RGBA?4:3;
50
51     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3);
52
53     if ( (fp = fopen(filename, "wb")) == NULL ) {
54         printf("Warning: cannot open %s\n", filename);
55         return false;
56     }
57
58     fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
59             win_width, win_height, UCHAR_MAX);
60     q = 0;
61     for (i = 0; i < win_height; i++)
62         for (j = 0; j < win_width; j++)
63             for (k = 0; k < RGB3; k++)
64                 ibuffer[q++] = (unsigned char)
65                     *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
66     fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp);
67     fclose(fp);
68     free(ibuffer);
69
70     printf("wrote file (%d x %d pixels, %d bytes)\n",
71            win_width, win_height, RGB3*win_width*win_height);
72     return true;
73 }
74
75
76 // dump the screen buffer to a ppm file
77 bool sg_glDumpWindow(const char *filename, int win_width, int win_height) {
78     GLubyte *buffer;
79     bool result;
80
81     buffer = (GLubyte *) malloc(win_width*win_height*RGBA);
82
83     // read window contents from color buffer with glReadPixels
84     glFinish();
85     glReadPixels(0, 0, win_width, win_height, 
86                  GL_RGBA, GL_UNSIGNED_BYTE, buffer);
87     result = sg_glWritePPMFile( filename, buffer, win_width, win_height,
88                                 GL_RGBA );
89     free(buffer);
90
91     return result;
92 }
93