Add wxIMAGE_OPTION_GIF_TRANSPARENCY for GIF image loading

Allow to keep the originally defined transparent pixels colour instead
of replacing it with bright pink (which is still the default behaviour).

Closes #18014.
This commit is contained in:
Hugo Elias
2017-12-24 15:14:00 +01:00
committed by Vadim Zeitlin
parent 1ebc28787c
commit 28bf0e8687
4 changed files with 69 additions and 12 deletions

View File

@@ -130,8 +130,10 @@ bool wxGIFDecoder::ConvertToImage(unsigned int frame, wxImage *image) const
unsigned long i;
int transparent;
// just in case...
image->Destroy();
// Store the original value of the transparency option, before it is reset
// by Create().
const wxString&
transparency = image->GetOption(wxIMAGE_OPTION_GIF_TRANSPARENCY);
// create the image
wxSize sz = GetFrameSize(frame);
@@ -149,24 +151,58 @@ bool wxGIFDecoder::ConvertToImage(unsigned int frame, wxImage *image) const
// set transparent colour mask
if (transparent != -1)
{
for (i = 0; i < GetNcolours(frame); i++)
if ( transparency.empty() ||
transparency == wxIMAGE_OPTION_GIF_TRANSPARENCY_HIGHLIGHT )
{
if ((pal[3 * i + 0] == 255) &&
(pal[3 * i + 1] == 0) &&
(pal[3 * i + 2] == 255))
// By default, we assign bright pink to transparent pixels to make
// them perfectly noticeable if someone accidentally draws the
// image without taking transparency into account. Due to this use
// of pink, we need to change any existing image pixels with this
// colour to use something different.
for (i = 0; i < GetNcolours(frame); i++)
{
pal[3 * i + 2] = 254;
if ((pal[3 * i + 0] == 255) &&
(pal[3 * i + 1] == 0) &&
(pal[3 * i + 2] == 255))
{
pal[3 * i + 2] = 254;
}
}
pal[3 * transparent + 0] = 255;
pal[3 * transparent + 1] = 0;
pal[3 * transparent + 2] = 255;
image->SetMaskColour(255, 0, 255);
}
else if ( transparency == wxIMAGE_OPTION_GIF_TRANSPARENCY_UNCHANGED )
{
// Leave the GIF exactly as as it was, just adjust (in the least
// noticeable way, by just flipping a single bit) non-transparent
// pixels colour,
for (i = 0; i < GetNcolours(frame); i++)
{
if ((pal[3 * i + 0] == pal[3 * transparent + 0]) &&
(pal[3 * i + 1] == pal[3 * transparent + 1]) &&
(pal[3 * i + 2] == pal[3 * transparent + 2]))
{
pal[3 * i + 2] ^= 1;
}
}
pal[3 * transparent + 0] = 255,
pal[3 * transparent + 1] = 0,
pal[3 * transparent + 2] = 255;
image->SetMaskColour(255, 0, 255);
image->SetMaskColour(pal[3 * transparent + 0],
pal[3 * transparent + 1],
pal[3 * transparent + 2]);
}
else
{
wxFAIL_MSG( wxS("Unknown wxIMAGE_OPTION_GIF_TRANSPARENCY value") );
}
}
else
{
image->SetMask(false);
}
#if wxUSE_PALETTE
unsigned char r[256];