windowscodecs: Factor out common GIF palette copying logic.

This commit is contained in:
Jeff Smith 2023-07-16 15:02:12 -05:00 committed by Alexandre Julliard
parent 70a6ac07bf
commit 8ff77999b5

View file

@ -701,14 +701,49 @@ static HRESULT WINAPI GifFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
return S_OK; return S_OK;
} }
static void copy_palette(ColorMapObject *cm, Extensions *extensions, int count, WICColor *colors)
{
int i;
if (cm)
{
for (i = 0; i < count; i++)
{
colors[i] = 0xff000000 | /* alpha */
cm->Colors[i].Red << 16 |
cm->Colors[i].Green << 8 |
cm->Colors[i].Blue;
}
}
else
{
colors[0] = 0xff000000;
colors[1] = 0xffffffff;
for (i = 2; i < count; i++)
colors[i] = 0xff000000;
}
/* look for the transparent color extension */
for (i = 0; i < extensions->ExtensionBlockCount; i++)
{
ExtensionBlock *eb = extensions->ExtensionBlocks + i;
if (eb->Function == GRAPHICS_EXT_FUNC_CODE &&
eb->ByteCount == 8 && eb->Bytes[3] & 1)
{
int trans = (unsigned char)eb->Bytes[6];
colors[trans] &= 0xffffff; /* set alpha to 0 */
break;
}
}
}
static HRESULT WINAPI GifFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface, static HRESULT WINAPI GifFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
IWICPalette *pIPalette) IWICPalette *pIPalette)
{ {
GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface); GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
WICColor colors[256]; WICColor colors[256];
ColorMapObject *cm = This->frame->ImageDesc.ColorMap; ColorMapObject *cm = This->frame->ImageDesc.ColorMap;
int i, trans;
ExtensionBlock *eb;
TRACE("(%p,%p)\n", iface, pIPalette); TRACE("(%p,%p)\n", iface, pIPalette);
if (!cm) cm = This->parent->gif->SColorMap; if (!cm) cm = This->parent->gif->SColorMap;
@ -719,24 +754,7 @@ static HRESULT WINAPI GifFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
return E_FAIL; return E_FAIL;
} }
for (i = 0; i < cm->ColorCount; i++) { copy_palette(cm, &This->frame->Extensions, cm->ColorCount, colors);
colors[i] = 0xff000000| /* alpha */
cm->Colors[i].Red << 16|
cm->Colors[i].Green << 8|
cm->Colors[i].Blue;
}
/* look for the transparent color extension */
for (i = 0; i < This->frame->Extensions.ExtensionBlockCount; ++i) {
eb = This->frame->Extensions.ExtensionBlocks + i;
if (eb->Function == GRAPHICS_EXT_FUNC_CODE && eb->ByteCount == 8) {
if (eb->Bytes[3] & 1) {
trans = (unsigned char)eb->Bytes[6];
colors[trans] &= 0xffffff; /* set alpha to 0 */
break;
}
}
}
return IWICPalette_InitializeCustom(pIPalette, colors, cm->ColorCount); return IWICPalette_InitializeCustom(pIPalette, colors, cm->ColorCount);
} }
@ -1170,8 +1188,7 @@ static HRESULT WINAPI GifDecoder_CopyPalette(IWICBitmapDecoder *iface, IWICPalet
GifDecoder *This = impl_from_IWICBitmapDecoder(iface); GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
WICColor colors[256]; WICColor colors[256];
ColorMapObject *cm; ColorMapObject *cm;
int i, trans, count; int count;
ExtensionBlock *eb;
TRACE("(%p,%p)\n", iface, palette); TRACE("(%p,%p)\n", iface, palette);
@ -1187,41 +1204,12 @@ static HRESULT WINAPI GifDecoder_CopyPalette(IWICBitmapDecoder *iface, IWICPalet
return E_FAIL; return E_FAIL;
} }
for (i = 0; i < cm->ColorCount; i++)
{
colors[i] = 0xff000000 | /* alpha */
cm->Colors[i].Red << 16 |
cm->Colors[i].Green << 8 |
cm->Colors[i].Blue;
}
count = cm->ColorCount; count = cm->ColorCount;
} }
else else
{
colors[0] = 0xff000000;
colors[1] = 0xffffffff;
for (i = 2; i < 256; i++)
colors[i] = 0xff000000;
count = 256; count = 256;
}
/* look for the transparent color extension */ copy_palette(cm, &This->gif->SavedImages[This->current_frame].Extensions, count, colors);
for (i = 0; i < This->gif->SavedImages[This->current_frame].Extensions.ExtensionBlockCount; i++)
{
eb = This->gif->SavedImages[This->current_frame].Extensions.ExtensionBlocks + i;
if (eb->Function == GRAPHICS_EXT_FUNC_CODE && eb->ByteCount == 8)
{
if (eb->Bytes[3] & 1)
{
trans = (unsigned char)eb->Bytes[6];
colors[trans] &= 0xffffff; /* set alpha to 0 */
break;
}
}
}
return IWICPalette_InitializeCustom(palette, colors, count); return IWICPalette_InitializeCustom(palette, colors, count);
} }