00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef HAVE_CONFIG_H
00023 # include <simgear_config.h>
00024 #endif
00025
00026 #if defined(__CYGWIN__)
00027 #define WIN32
00028 #endif
00029
00030 #if defined(WIN32)
00031 # include <windows.h>
00032 #endif
00033
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <limits.h>
00037
00038 #include <simgear/compiler.h>
00039
00040 #include SG_GL_H
00041
00042 #include "screen-dump.hxx"
00043
00044
00045 #define RGB3 3 // 3 bytes of color info per pixel
00046 #define RGBA 4 // 4 bytes of color+alpha info
00047
00048 bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
00049 {
00050 int i, j, k, q;
00051 unsigned char *ibuffer;
00052 FILE *fp;
00053 int pixelSize = mode==GL_RGBA?4:3;
00054
00055 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3);
00056
00057 if ( (fp = fopen(filename, "wb")) == NULL ) {
00058 printf("Warning: cannot open %s\n", filename);
00059 return false;
00060 }
00061
00062 fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
00063 win_width, win_height, UCHAR_MAX);
00064 q = 0;
00065 for (i = 0; i < win_height; i++)
00066 for (j = 0; j < win_width; j++)
00067 for (k = 0; k < RGB3; k++)
00068 ibuffer[q++] = (unsigned char)
00069 *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
00070 fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp);
00071 fclose(fp);
00072 free(ibuffer);
00073
00074 printf("wrote file '%s' (%d x %d pixels, %d bytes)\n",
00075 filename, win_width, win_height, RGB3*win_width*win_height);
00076 return true;
00077 }
00078
00079
00080
00081 bool sg_glDumpWindow(const char *filename, int win_width, int win_height) {
00082 GLubyte *buffer;
00083 bool result;
00084
00085 buffer = (GLubyte *) malloc(win_width*win_height*RGBA);
00086
00087
00088 glFinish();
00089 glReadPixels(0, 0, win_width, win_height,
00090 GL_RGBA, GL_UNSIGNED_BYTE, buffer);
00091 result = sg_glWritePPMFile( filename, buffer, win_width, win_height,
00092 GL_RGBA );
00093 free(buffer);
00094
00095 return result;
00096 }
00097