+ 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;
+ }
+
+ 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);
+ palette = NULL;
+
+ // Let palette point to libpng's copy of the palette.
+ (void) png_get_PLTE(png_ptr, info_ptr, &palette, &numPalette);
+ }
+#endif // wxUSE_PALETTE
+
+ int iElements;