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 #ifdef WIN32
00027 # include <windows.h>
00028 #endif
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <limits.h>
00033
00034 #include <simgear/compiler.h>
00035
00036 #include <osg/Image>
00037 #include <osgDB/WriteFile>
00038
00039 #include "screen-dump.hxx"
00040
00041
00042 #define RGB3 3 // 3 bytes of color info per pixel
00043 #define RGBA 4 // 4 bytes of color+alpha info
00044
00045 bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
00046 {
00047 int i, j, k, q;
00048 unsigned char *ibuffer;
00049 FILE *fp;
00050 int pixelSize = mode==GL_RGBA?4:3;
00051
00052 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3);
00053
00054 if ( (fp = fopen(filename, "wb")) == NULL ) {
00055 free(ibuffer);
00056 printf("Warning: cannot open %s\n", filename);
00057 return false;
00058 }
00059
00060 fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
00061 win_width, win_height, UCHAR_MAX);
00062 q = 0;
00063 for (i = 0; i < win_height; i++)
00064 for (j = 0; j < win_width; j++)
00065 for (k = 0; k < RGB3; k++)
00066 ibuffer[q++] = (unsigned char)
00067 *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
00068 fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp);
00069 fclose(fp);
00070 free(ibuffer);
00071
00072 printf("wrote file '%s' (%d x %d pixels, %d bytes)\n",
00073 filename, win_width, win_height, RGB3*win_width*win_height);
00074 return true;
00075 }
00076
00077
00078
00079 bool sg_glDumpWindow(const char *filename, int win_width, int win_height) {
00080 osg::ref_ptr<osg::Image> img(new osg::Image);
00081 img->readPixels(0,0, win_width, win_height, GL_RGB, GL_UNSIGNED_BYTE);
00082 osgDB::writeImageFile(*img, filename);
00083 return true;
00084 }
00085