From 818e5f3a486522743546fa6f802eb2e8c09415fc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 12 Mar 2014 23:04:19 +0000 Subject: [PATCH] Allow saving 24 bpp ICO file in wxImage. Create ICO files with 24bpp if the image being saved contains more than 256 colours. Also use smaller values (4 bpp or monochrome) if the image uses fewer colours. See #15918. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76133 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 2 +- src/common/imagbmp.cpp | 32 +++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index b4f72551ef..e3dc342671 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -54,7 +54,7 @@ wxMSW: - Support multiline strings in wxDC::DrawRotatedText() (Artur Wieczorek). - Fix stretchable spacers in vertical toolbars (Artur Wieczorek). - Add wxEnhMetaFile::Detach() (Luca Bacci). -- Add support for saving 256*256 ICOs in PNG format (Artur Wieczorek). +- Add support for saving 256*256 24bpp ICOs in PNG format (Artur Wieczorek). wxOSX/Cocoa: diff --git a/src/common/imagbmp.cpp b/src/common/imagbmp.cpp index 21961bc700..eba80ff440 100644 --- a/src/common/imagbmp.cpp +++ b/src/common/imagbmp.cpp @@ -1274,8 +1274,34 @@ bool wxICOHandler::SaveFile(wxImage *image, mask.SetRGB(i, j, 0, 0, 0 ); } // Set the formats for image and mask - // (Windows never saves with more than 8 colors): - image->SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_8BPP); + + // The format depends on the number of the colours used, so count them, + // but stop at 257 because we have to use 24 bpp anyhow if we have that + // many of them. + const int colours = image->CountColours(257); + int bppFormat; + int bpp; + if ( colours > 256 ) + { + bppFormat = wxBMP_24BPP; + bpp = 24; + } + else if ( colours > 16 ) + { + bppFormat = wxBMP_8BPP; + bpp = 8; + } + else if ( colours > 2 ) + { + bppFormat = wxBMP_4BPP; + bpp = 4; + } + else + { + bppFormat = wxBMP_1BPP; + bpp = 1; + } + image->SetOption(wxIMAGE_OPTION_BMP_FORMAT, bppFormat); // monochome bitmap: mask.SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_1BPP_BW); @@ -1352,7 +1378,7 @@ bool wxICOHandler::SaveFile(wxImage *image, icondirentry.bColorCount = 0; icondirentry.bReserved = 0; icondirentry.wPlanes = wxUINT16_SWAP_ON_BE(1); - icondirentry.wBitCount = wxUINT16_SWAP_ON_BE(wxBMP_8BPP); + icondirentry.wBitCount = wxUINT16_SWAP_ON_BE(bpp); if ( type == 2 /*CUR*/) { int hx = image->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ?