6
#include <core/stdtypes.h>
8
void writePNG( const std::string& file, const _uint8* data, unsigned len )
12
//calculate a reasonable size
13
unsigned width = (unsigned)sqrt( len );
14
unsigned height = len / width + 1;
16
im = gdImageCreate( width, height );
17
for( unsigned i=0; i < 256; i++ )
18
assert( gdImageColorAllocate( im, i ,i ,i ) == i );
20
//fill it in with data
21
for( unsigned y = 0; y < height; y++ )
22
for( unsigned x = 0; x < width; x++ )
24
unsigned at = y * width + x;
26
break; //we're done now
28
gdImageSetPixel( im, x, y, data[at] );
31
FILE* pngout = fopen( file.c_str(), "wb");
32
gdImagePngEx(im, pngout, 9);
38
_uint8* loadPNG( const std::string& file )
40
FILE* pngin = fopen( file.c_str(), "rb" );
42
gdImagePtr im = gdImageCreateFromPng( pngin );
44
unsigned width = gdImageSX(im);
45
unsigned height = gdImageSY(im);
46
_uint8* data = new _uint8[ width * height ];
47
for( unsigned y = 0; y < height; y++ )
48
for( unsigned x = 0; x < width; x++ )
50
unsigned at = y * width + x;
51
data[at] = gdImageGetPixel( im, x, y );