+ png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (!png_ptr)
+ {
+ return FALSE;
+ }
+
+ png_infop info_ptr = png_create_info_struct(png_ptr);
+ if (info_ptr == NULL)
+ {
+ png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
+ return FALSE;
+ }
+
+ if (setjmp(png_ptr->jmpbuf))
+ {
+ png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
+ return FALSE;
+ }
+
+ png_set_write_fn( png_ptr, &stream, _PNG_stream_writer, NULL);
+
+ png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8,
+ PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
+
+ png_color_8 sig_bit;
+ sig_bit.red = 8;
+ sig_bit.green = 8;
+ sig_bit.blue = 8;
+ sig_bit.alpha = 8;
+ png_set_sBIT( png_ptr, info_ptr, &sig_bit );
+ png_write_info( png_ptr, info_ptr );
+ png_set_shift( png_ptr, &sig_bit );
+ png_set_packing( png_ptr );
+
+ unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
+ if (!data)
+ {
+ png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
+ return FALSE;
+ }
+
+ for (int y = 0; y < image->GetHeight(); y++)
+ {
+ unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
+ for (int x = 0; x < image->GetWidth(); x++)
+ {
+ data[(x << 2) + 0] = *ptr++;
+ data[(x << 2) + 1] = *ptr++;
+ data[(x << 2) + 2] = *ptr++;
+ if ((data[(x << 2) + 0] == image->GetMaskRed()) &&
+ (data[(x << 2) + 1] == image->GetMaskGreen()) &&
+ (data[(x << 2) + 2] == image->GetMaskBlue()))
+ {
+ data[(x << 2) + 3] = 0;
+ }
+ else
+ {
+ data[(x << 2) + 3] = 255;
+ }
+ }
+ png_bytep row_ptr = data;
+ png_write_rows( png_ptr, &row_ptr, 1 );
+ }
+
+ free(data);
+ png_write_end( png_ptr, info_ptr );
+ png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );