+
+#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();
+ }
+