+ png_set_write_fn( png_ptr, &wxinfo, wx_PNG_stream_writer, NULL);
+
+ const bool bHasPngFormatOption
+ = image->HasOption(wxIMAGE_OPTION_PNG_FORMAT);
+
+ int iColorType = bHasPngFormatOption
+ ? image->GetOptionInt(wxIMAGE_OPTION_PNG_FORMAT)
+ : wxPNG_TYPE_COLOUR;
+
+ bool bHasAlpha = image->HasAlpha();
+ bool bHasMask = image->HasMask();
+
+#if wxUSE_PALETTE
+ /*
+ Only save as an indexed image if the number of palette entries does not
+ exceed libpng's limit (256).
+ We assume here that we will need an extra palette entry if there's an
+ alpha or mask, regardless of whether a possibly needed conversion from
+ alpha to a mask fails (unlikely), or whether the mask colour already
+ can be found in the palette (more likely). In the latter case an extra
+ palette entry would not be required later on and the image could actually
+ be saved as a palettised PNG (instead now it will be saved as true colour).
+ A little bit of precision is lost, but at the benefit of a lot more
+ simplified code.
+ */
+ bool bUsePalette =
+ (!bHasPngFormatOption || iColorType == wxPNG_TYPE_PALETTE)
+ && image->HasPalette()
+ && image->GetPalette().GetColoursCount()
+ + ((bHasAlpha || bHasMask) ? 1 : 0) <= PNG_MAX_PALETTE_LENGTH;
+
+ wxImage temp_image(*image);
+ if (bUsePalette && image->HasAlpha() && !bHasMask)
+ {
+ /*
+ Only convert alpha to mask if saving as a palettised image was
+ explicitly requested. We don't want to lose alpha's precision
+ by converting to a mask just to be able to save palettised.
+ */
+ if (iColorType == wxPNG_TYPE_PALETTE
+ && temp_image.ConvertAlphaToMask())
+ {
+ image = &temp_image;
+ bHasMask = true;
+ bHasAlpha = image->HasAlpha();
+ }
+ else
+ {
+ bUsePalette = false;
+ iColorType = wxPNG_TYPE_COLOUR;
+ }
+ }
+#else
+ bool bUsePalette = false;
+#endif // wxUSE_PALETTE
+
+ /*
+ If saving palettised was requested but it was decided we can't use a
+ palette then reset the colour type to RGB.
+ */
+ if (!bUsePalette && iColorType == wxPNG_TYPE_PALETTE)
+ {
+ iColorType = wxPNG_TYPE_COLOUR;
+ }
+
+ bool bUseAlpha = !bUsePalette && (bHasAlpha || bHasMask);
+
+ png_color mask;
+ if (bHasMask)
+ {
+ mask.red = image->GetMaskRed();
+ mask.green = image->GetMaskGreen();
+ mask.blue = image->GetMaskBlue();
+ }
+
+
+ int iPngColorType;
+
+#if wxUSE_PALETTE
+ if (bUsePalette)
+ {
+ iPngColorType = PNG_COLOR_TYPE_PALETTE;
+ iColorType = wxPNG_TYPE_PALETTE;
+ }
+ else
+#endif // wxUSE_PALETTE
+ 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) );
+
+ int iBitDepth = !bUsePalette && image->HasOption(wxIMAGE_OPTION_PNG_BITDEPTH)
+ ? image->GetOptionInt(wxIMAGE_OPTION_PNG_BITDEPTH)
+ : 8;
+
+ png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(),
+ iBitDepth, iPngColorType,
+ PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
+ PNG_FILTER_TYPE_BASE);
+
+#if wxUSE_PALETTE
+ png_colorp palette = NULL;
+ int numPalette = 0;
+
+ if (bUsePalette)
+ {
+ const wxPalette& pal = image->GetPalette();
+ const int palCount = pal.GetColoursCount();
+ palette = (png_colorp) malloc(
+ (palCount + 1 /*headroom for trans */) * sizeof(png_color));
+
+ if (!palette)
+ {
+ png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
+ if (verbose)
+ {
+ wxLogError(_("Couldn't save PNG image."));
+ }
+ return false;
+ }
+
+ for (int i = 0; i < palCount; ++i)
+ {
+ pal.GetRGB(i, &palette[i].red, &palette[i].green, &palette[i].blue);
+ }
+
+ numPalette = palCount;
+ if (bHasMask)
+ {
+ int index = PaletteFind(mask, palette, numPalette);
+
+ if (index)
+ {
+ if (index == wxNOT_FOUND)
+ {
+ numPalette++;
+ index = palCount;
+ palette[index] = mask;
+ }