00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "Teddy/TeddyConfig.h"
00026 #include "Teddy/Graphics/Device.h"
00027 #include "Teddy/Graphics/View.h"
00028 #include "Teddy/SysSupport/StdSDL.h"
00029 #include "Teddy/SysSupport/StdIO.h"
00030
00031 #ifndef SWIG
00032 #include <cstdlib>
00033 #ifdef HAVE_LIB_PNG
00034 # include <png.h>
00035 # if defined (_MSC_VER)
00036 # pragma comment (lib, "libpng1.lib" )
00037 # endif
00038 #endif
00039 #endif
00040
00041 using namespace Teddy::Maths;
00042 using namespace Teddy::PhysicalComponents;
00043 using namespace Teddy::SysSupport;
00044
00045
00046 namespace Teddy {
00047 namespace Graphics {
00048
00049
00050 #ifdef HAVE_LIB_PNG
00051
00052
00053 FILE *View::fp;
00054
00055
00056 void View::pngUserWriteData( png_structp png_ptr, png_bytep data, png_size_t length ){
00057 fwrite( data, length, 1, fp );
00058 }
00059
00060
00061 void View::pngUserFlushData( png_structp png_ptr ){
00062 fflush( fp );
00063 }
00064
00065
00066 #endif
00067
00068
00069
00070 bool View::pngScreenshot( char *filename ){
00071 #if defined( HAVE_LIB_PNG ) && !defined( USE_TINY_GL )
00072 unsigned char *data = NULL;
00073 png_structp png_ptr = NULL;
00074 png_infop info_ptr = NULL;
00075 png_byte **row_pointers = NULL;
00076 bool written = false;
00077
00078 int width = size[0];
00079 int height = size[1];
00080
00081 fp = fopen( filename, "wb" );
00082 data = (unsigned char*)malloc ( width * height * 3 );
00083 row_pointers = (png_byte**) malloc ( height * sizeof(png_byte*) );
00084 png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
00085 info_ptr = png_create_info_struct ( png_ptr );
00086 if( (fp!=NULL) && (data!=NULL) && (row_pointers!=NULL) && (row_pointers!=NULL) && (png_ptr!=NULL) && (info_ptr!=NULL) ){
00087 for( int i=0; i<height; i++ ){
00088 row_pointers[i] = data + (height - i - 1) * 3 * width;
00089 }
00090 glReadPixels ( 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );
00091 png_set_write_fn( png_ptr, 0, View::pngUserWriteData, View::pngUserFlushData );
00092 png_set_IHDR ( png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT );
00093 png_write_info ( png_ptr, info_ptr );
00094 png_write_image ( png_ptr, row_pointers );
00095 png_write_end ( png_ptr, info_ptr );
00096 written = true;
00097 }
00098 if( png_ptr != NULL ){ png_destroy_write_struct( &png_ptr, &info_ptr ); }
00099 if( row_pointers != NULL ){ free ( row_pointers ); }
00100 if( data != NULL ){ free ( data ); }
00101 if( fp != NULL ){ fclose ( fp ); }
00102 return written;
00103 #else
00104 return false;
00105 #endif
00106 }
00107
00108
00109 #ifdef BMP_SCREENSHOT
00110 #include "Teddy/SysSupport/StdSDL.h"
00111
00112
00113 bool bmpScreenshot( char *filename, int width, int height ){
00114 unsigned char *data;
00115
00116 data = malloc( width * height * 3 );
00117 glReadPixels( 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );
00118
00119
00120
00121 {
00122 SDL_Surface *temp;
00123 int i;
00124
00125 temp = SDL_CreateRGBSurface( SDL_SWSURFACE, width, height, 24,
00126 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
00127 0x000000FF, 0x0000FF00, 0x00FF0000, 0
00128 #else
00129 0x00FF0000, 0x0000FF00, 0x000000FF, 0
00130 #endif
00131 );
00132
00133 if( temp == NULL ){
00134 return false;
00135 }
00136
00137 for (i = 0; i < height; i++){
00138 memcpy(((char *) temp->pixels) + temp->pitch * i, data + 3 * width * (height - i - 1), width * 3);
00139 }
00140
00141 SDL_SaveBMP( temp, filename );
00142 SDL_FreeSurface( temp );
00143 }
00144
00145 free( data );
00146 return 0;
00147 }
00148
00149 #endif
00150
00151
00152 };
00153 };
00154