- 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++)
+ if (verbose)
+ wxLogError(_("Couldn't save PNG image."));
+ 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 );
+ if (verbose)
+ wxLogError(_("Couldn't save PNG image."));
+ return false;
+ }
+
+ if (setjmp(wxinfo.jmpbuf))
+ {
+ png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
+ if (verbose)
+ wxLogError(_("Couldn't save PNG image."));
+ return false;
+ }
+
+ // NB: please see the comment near wxPNGInfoStruct declaration for
+ // explanation why this line is mandatory
+ png_set_write_fn( png_ptr, &wxinfo, wx_PNG_stream_writer, NULL);
+
+ const int iColorType = image->HasOption(wxIMAGE_OPTION_PNG_FORMAT)
+ ? image->GetOptionInt(wxIMAGE_OPTION_PNG_FORMAT)
+ : wxPNG_TYPE_COLOUR;
+ const int iBitDepth = image->HasOption(wxIMAGE_OPTION_PNG_BITDEPTH)
+ ? image->GetOptionInt(wxIMAGE_OPTION_PNG_BITDEPTH)
+ : 8;
+
+ wxASSERT_MSG( iBitDepth == 8 || iBitDepth == 16,
+ _T("PNG bit depth must be 8 or 16") );
+
+ bool bHasAlpha = image->HasAlpha();
+ bool bHasMask = image->HasMask();
+ bool bUseAlpha = bHasAlpha || bHasMask;
+
+ int iPngColorType;
+ if ( iColorType==wxPNG_TYPE_COLOUR )
+ {
+ iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_RGB_ALPHA
+ : PNG_COLOR_TYPE_RGB;
+ }
+ else
+ {
+ iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_GRAY_ALPHA
+ : PNG_COLOR_TYPE_GRAY;
+ }
+
+ if (image->HasOption(wxIMAGE_OPTION_PNG_FILTER))
+ png_set_filter( png_ptr, PNG_FILTER_TYPE_BASE, image->GetOptionInt(wxIMAGE_OPTION_PNG_FILTER) );
+
+ if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL))
+ png_set_compression_level( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL) );
+
+ if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL))
+ png_set_compression_mem_level( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL) );
+
+ if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY))
+ png_set_compression_strategy( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY) );
+
+ if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE))
+ png_set_compression_buffer_size( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE) );
+
+ png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(),
+ iBitDepth, iPngColorType,
+ PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
+ PNG_FILTER_TYPE_BASE);
+
+ int iElements;
+ png_color_8 sig_bit;
+
+ if ( iPngColorType & PNG_COLOR_MASK_COLOR )
+ {
+ sig_bit.red =
+ sig_bit.green =
+ sig_bit.blue = (png_byte)iBitDepth;
+ iElements = 3;
+ }
+ else // grey
+ {
+ sig_bit.gray = (png_byte)iBitDepth;
+ iElements = 1;
+ }
+
+ if ( iPngColorType & PNG_COLOR_MASK_ALPHA )
+ {
+ sig_bit.alpha = (png_byte)iBitDepth;
+ iElements++;
+ }
+
+ if ( iBitDepth == 16 )
+ iElements *= 2;
+
+ // save the image resolution if we have it
+ int resX, resY;
+ switch ( GetResolutionFromOptions(*image, &resX, &resY) )
+ {
+ case wxIMAGE_RESOLUTION_INCHES:
+ {
+ const double INCHES_IN_METER = 10000.0 / 254;
+ resX = int(resX * INCHES_IN_METER);
+ resY = int(resY * INCHES_IN_METER);
+ }
+ break;
+
+ case wxIMAGE_RESOLUTION_CM:
+ resX *= 100;
+ resY *= 100;
+ break;
+
+ case wxIMAGE_RESOLUTION_NONE:
+ break;
+
+ default:
+ wxFAIL_MSG( _T("unsupported image resolution units") );
+ }
+
+ if ( resX && resY )
+ png_set_pHYs( png_ptr, info_ptr, resX, resY, PNG_RESOLUTION_METER );
+
+ 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() * iElements );
+ if ( !data )
+ {
+ png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
+ return false;
+ }
+
+ unsigned char *
+ pAlpha = (unsigned char *)(bHasAlpha ? image->GetAlpha() : NULL);
+ int iHeight = image->GetHeight();
+ int iWidth = image->GetWidth();
+
+ unsigned char uchMaskRed = 0, uchMaskGreen = 0, uchMaskBlue = 0;
+
+ if ( bHasMask )
+ {
+ uchMaskRed = image->GetMaskRed();
+ uchMaskGreen = image->GetMaskGreen();
+ uchMaskBlue = image->GetMaskBlue();
+ }
+
+ unsigned char *pColors = image->GetData();
+
+ for (int y = 0; y != iHeight; ++y)
+ {
+ unsigned char *pData = data;
+ for (int x = 0; x != iWidth; x++)