- Added support for saving TGA files.
- Added wxArtProvider returning higher quality icons from Tango project.
- wxPropertyGrid: Added "HasAlpha" attribute for wxColourProperty.
+- Added support for saving PNG files with palette (troelsk).
GTK:
{
wxPNG_TYPE_COLOUR = 0,
wxPNG_TYPE_GREY = 2,
- wxPNG_TYPE_GREY_RED = 3
+ wxPNG_TYPE_GREY_RED = 3,
+ wxPNG_TYPE_PALETTE = 4
};
class WXDLLIMPEXP_CORE wxPNGHandler: public wxImageHandler
{
wxPNG_TYPE_COLOUR = 0, ///< Colour PNG image.
wxPNG_TYPE_GREY = 2, ///< Greyscale PNG image converted from RGB.
- wxPNG_TYPE_GREY_RED = 3 ///< Greyscale PNG image using red as grey.
+ wxPNG_TYPE_GREY_RED = 3, ///< Greyscale PNG image using red as grey.
+ wxPNG_TYPE_PALETTE = 4 ///< Palette encoding.
};
/**
return false;
}
+// ----------------------------------------------------------------------------
+// SaveFile() helpers
+// ----------------------------------------------------------------------------
+
+static int PaletteFind(const png_color& clr,
+ const png_color *pal, png_uint_16 palCount)
+{
+ for (png_uint_16 i = 0; i < palCount; i++)
+ {
+ if ( (clr.red == pal[i].red)
+ && (clr.green == pal[i].green)
+ && (clr.blue == pal[i].blue))
+ {
+ return i;
+ }
+ }
+
+ return wxNOT_FOUND;
+}
+
// ----------------------------------------------------------------------------
// writing PNGs
// ----------------------------------------------------------------------------
// 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)
+ const bool bHasPngFormatOption
+ = image->HasOption(wxIMAGE_OPTION_PNG_FORMAT);
+
+ int iColorType = bHasPngFormatOption
? 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;
bool bHasAlpha = image->HasAlpha();
bool bHasMask = image->HasMask();
- bool bUseAlpha = bHasAlpha || bHasMask;
+
+#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
+
+ 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
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
+ if (bUsePalette)
+ {
+ const wxPalette& pal = image->GetPalette();
+ const int palCount = pal.GetColoursCount();
+ png_colorp 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;
+ }
+
+ png_uint_16 i;
+ for (i = 0; i < palCount; ++i)
+ {
+ pal.GetRGB(i, &palette[i].red, &palette[i].green, &palette[i].blue);
+ }
+
+ png_uint_16 numPalette = palCount;
+ if (bHasMask)
+ {
+ int index = PaletteFind(mask, palette, numPalette);
+
+ if (index)
+ {
+ if (index == wxNOT_FOUND)
+ {
+ numPalette++;
+ index = palCount;
+ palette[index] = mask;
+ }
+
+ wxSwap(palette[0], palette[index]);
+ }
+
+ png_byte trans = 0;
+ png_set_tRNS(png_ptr, info_ptr, &trans, 1, NULL);
+ }
+
+ png_set_PLTE(png_ptr, info_ptr, palette, numPalette);
+ free (palette);
+ }
+#endif // wxUSE_PALETTE
+
int iElements;
png_color_8 sig_bit;
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++)
{
- unsigned char uchRed = *pColors++;
- unsigned char uchGreen = *pColors++;
- unsigned char uchBlue = *pColors++;
+ png_color clr;
+ clr.red = *pColors++;
+ clr.green = *pColors++;
+ clr.blue = *pColors++;
switch ( iColorType )
{
// fall through
case wxPNG_TYPE_COLOUR:
- *pData++ = uchRed;
+ *pData++ = clr.red;
if ( iBitDepth == 16 )
*pData++ = 0;
- *pData++ = uchGreen;
+ *pData++ = clr.green;
if ( iBitDepth == 16 )
*pData++ = 0;
- *pData++ = uchBlue;
+ *pData++ = clr.blue;
if ( iBitDepth == 16 )
*pData++ = 0;
break;
// where do these coefficients come from? maybe we
// should have image options for them as well?
unsigned uiColor =
- (unsigned) (76.544*(unsigned)uchRed +
- 150.272*(unsigned)uchGreen +
- 36.864*(unsigned)uchBlue);
+ (unsigned) (76.544*(unsigned)clr.red +
+ 150.272*(unsigned)clr.green +
+ 36.864*(unsigned)clr.blue);
*pData++ = (unsigned char)((uiColor >> 8) & 0xFF);
if ( iBitDepth == 16 )
break;
case wxPNG_TYPE_GREY_RED:
- *pData++ = uchRed;
+ *pData++ = clr.red;
if ( iBitDepth == 16 )
*pData++ = 0;
break;
+
+ case wxPNG_TYPE_PALETTE:
+ *pData++ = (unsigned char) PaletteFind(clr,
+ info_ptr->palette, info_ptr->num_palette);
+ break;
}
if ( bUseAlpha )
if ( bHasMask )
{
- if ( (uchRed == uchMaskRed)
- && (uchGreen == uchMaskGreen)
- && (uchBlue == uchMaskBlue) )
+ if ( (clr.red == mask.red)
+ && (clr.green == mask.green)
+ && (clr.blue == mask.blue) )
uchAlpha = 0;
}
#endif // WX_PRECOMP
#include "wx/image.h"
+#include "wx/palette.h"
#include "wx/url.h"
#include "wx/log.h"
#include "wx/mstream.h"
}
+enum
+{
+ wxIMAGE_HAVE_ALPHA = (1 << 0),
+ wxIMAGE_HAVE_PALETTE = (1 << 1)
+};
+
static
-void CompareImage(const wxImageHandler& handler, const wxImage& expected)
+void CompareImage(const wxImageHandler& handler, const wxImage& image,
+ int properties = 0, const wxImage *compareTo = NULL)
{
- bool testAlpha = expected.HasAlpha();
wxBitmapType type = handler.GetType();
+
+ const bool testPalette = (properties & wxIMAGE_HAVE_PALETTE) != 0;
+ /*
+ This is getting messy and should probably be transformed into a table
+ with image format features before it gets hairier.
+ */
+ if ( testPalette
+ && ( !(type == wxBITMAP_TYPE_BMP
+ || type == wxBITMAP_TYPE_GIF
+ || type == wxBITMAP_TYPE_PNG)
+ || type == wxBITMAP_TYPE_XPM) )
+ {
+ return;
+ }
+
+ const bool testAlpha = (properties & wxIMAGE_HAVE_ALPHA) != 0;
if (testAlpha
&& !(type == wxBITMAP_TYPE_PNG || type == wxBITMAP_TYPE_TGA) )
{
}
wxMemoryOutputStream memOut;
- if ( !expected.SaveFile(memOut, type) )
+ if ( !image.SaveFile(memOut, type) )
{
// Unfortunately we can't know if the handler just doesn't support
// saving images, or if it failed to save.
wxImage actual(memIn);
CPPUNIT_ASSERT(actual.IsOk());
- CPPUNIT_ASSERT( actual.GetSize() == expected.GetSize() );
+ const wxImage *expected = compareTo ? compareTo : ℑ
+ CPPUNIT_ASSERT( actual.GetSize() == expected->GetSize() );
+ unsigned bitsPerPixel = testPalette ? 8 : (testAlpha ? 32 : 24);
WX_ASSERT_MESSAGE
(
- ("Compare test '%s' for saving failed", handler.GetExtension()),
+ ("Compare test '%s (%d-bit)' for saving failed",
+ handler.GetExtension(), bitsPerPixel),
- memcmp(actual.GetData(), expected.GetData(),
- expected.GetWidth() * expected.GetHeight() * 3) == 0
+ memcmp(actual.GetData(), expected->GetData(),
+ expected->GetWidth() * expected->GetHeight() * 3) == 0
);
+#if wxUSE_PALETTE
+ CPPUNIT_ASSERT(actual.HasPalette()
+ == (testPalette || type == wxBITMAP_TYPE_XPM));
+#endif
+
+ CPPUNIT_ASSERT( actual.HasAlpha() == testAlpha);
+
if (!testAlpha)
{
return;
}
-
- CPPUNIT_ASSERT( actual.HasAlpha() );
-
WX_ASSERT_MESSAGE
(
("Compare alpha test '%s' for saving failed", handler.GetExtension()),
- memcmp(actual.GetAlpha(), expected.GetAlpha(),
- expected.GetWidth() * expected.GetHeight()) == 0
+ memcmp(actual.GetAlpha(), expected->GetAlpha(),
+ expected->GetWidth() * expected->GetHeight()) == 0
);
}
CPPUNIT_ASSERT( expected24.IsOk() );
CPPUNIT_ASSERT( !expected24.HasAlpha() );
+ unsigned long numColours = expected24.CountColours();
+ wxImage expected8 = expected24.ConvertToGreyscale();
+ numColours = expected8.CountColours();
+
+ unsigned char greys[256];
+ for (size_t i = 0; i < 256; ++i)
+ {
+ greys[i] = i;
+ }
+ wxPalette palette(256, greys, greys, greys);
+ expected8.SetPalette(palette);
+ expected8.SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_8BPP_PALETTE);
+
// Create an image with alpha based on the loaded image
wxImage expected32(expected24);
expected32.SetAlpha();
{
wxImageHandler *handler = (wxImageHandler *) node->GetData();
+#if wxUSE_PALETTE
+ CompareImage(*handler, expected8, wxIMAGE_HAVE_PALETTE);
+#endif
CompareImage(*handler, expected24);
- CompareImage(*handler, expected32);
+ CompareImage(*handler, expected32, wxIMAGE_HAVE_ALPHA);
+ }
+
+
+ expected8.LoadFile("horse.gif");
+ CPPUNIT_ASSERT( expected8.IsOk() );
+ CPPUNIT_ASSERT( expected8.HasPalette() );
+
+ expected8.SetAlpha();
+
+ width = expected8.GetWidth();
+ height = expected8.GetHeight();
+ for (int y = 0; y < height; ++y)
+ {
+ for (int x = 0; x < width; ++x)
+ {
+ expected8.SetAlpha(x, y, (x*y) & wxIMAGE_ALPHA_OPAQUE);
+ }
}
+
+ /*
+ 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
}
#endif //wxUSE_IMAGE