+ 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;
+ }