]> git.mxchange.org Git - simgear.git/blob - simgear/screen/screen-dump.cxx
Merge branch 'ehofman/particles' into next
[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 #ifdef WIN32
27 # include <windows.h>
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <limits.h>
33
34 #include <simgear/compiler.h>
35
36 #include <osg/Image>
37 #include <osgDB/WriteFile>
38
39 #include "screen-dump.hxx"
40
41
42 #define RGB3 3                  // 3 bytes of color info per pixel
43 #define RGBA 4                  // 4 bytes of color+alpha info
44
45 bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
46 {
47     int i, j, k, q;
48     unsigned char *ibuffer;
49     FILE *fp;
50     int pixelSize = mode==GL_RGBA?4:3;
51
52     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3);
53
54     if ( (fp = fopen(filename, "wb")) == NULL ) {
55         free(ibuffer);
56         printf("Warning: cannot open %s\n", filename);
57         return false;
58     }
59
60     fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
61             win_width, win_height, UCHAR_MAX);
62     q = 0;
63     for (i = 0; i < win_height; i++)
64         for (j = 0; j < win_width; j++)
65             for (k = 0; k < RGB3; k++)
66                 ibuffer[q++] = (unsigned char)
67                     *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
68     fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp);
69     fclose(fp);
70     free(ibuffer);
71
72     printf("wrote file '%s' (%d x %d pixels, %d bytes)\n",
73            filename, win_width, win_height, RGB3*win_width*win_height);
74     return true;
75 }
76
77
78 // dump the screen buffer to a png file
79 bool sg_glDumpWindow(const char *filename, int win_width, int win_height) {
80   osg::ref_ptr<osg::Image> img(new osg::Image);
81   img->readPixels(0,0, win_width, win_height, GL_RGB, GL_UNSIGNED_BYTE);
82   osgDB::writeImageFile(*img, filename);
83   return true;
84 }
85