+
+ /*
+ The image contains 256 indexed colours and needs another palette entry
+ for storing the transparency index. This results in wanting 257 palette
+ entries but that amount is not supported by PNG, as such this image
+ should not contain a palette (but still have alpha) and be stored as a
+ true colour image instead.
+ */
+ CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
+ expected8, wxIMAGE_HAVE_ALPHA);
+
+#if wxUSE_PALETTE
+ /*
+ Now do the same test again but remove one (random) palette entry. This
+ should result in saving the PNG with a palette.
+ */
+ unsigned char red[256], green[256], blue[256];
+ const wxPalette& pal = expected8.GetPalette();
+ const int paletteCount = pal.GetColoursCount();
+ for (int i = 0; i < paletteCount; ++i)
+ {
+ expected8.GetPalette().GetRGB(i, &red[i], &green[i], &blue[i]);
+ }
+ wxPalette newPal(paletteCount - 1, red, green, blue);
+ expected8.Replace(
+ red[paletteCount-1], green[paletteCount-1], blue[paletteCount-1],
+ red[paletteCount-2], green[paletteCount-2], blue[paletteCount-2]);
+
+ expected8.SetPalette(newPal);
+ /*
+ Explicitly make known we want a palettised PNG. If we don't then this
+ particular image gets saved as a true colour image because there's an
+ alpha channel present and the PNG saver prefers to keep the alpha over
+ saving as a palettised image that has alpha converted to a mask.
+ */
+ expected8.SetOption(wxIMAGE_OPTION_PNG_FORMAT, wxPNG_TYPE_PALETTE);
+
+ wxImage ref8 = expected8;
+
+ /*
+ Convert the alpha channel to a mask like the PNG saver does. Also convert
+ the colour used for transparency from 1,0,0 to 2,0,0. The latter gets
+ done by the PNG loader in search of an unused colour to use for
+ transparency (this should be fixed).
+ */
+ ref8.ConvertAlphaToMask();
+ ref8.Replace(1, 0, 0, 2, 0, 0);
+
+ CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
+ expected8, wxIMAGE_HAVE_PALETTE, &ref8);
+#endif