[PATCH 0/1] MR9923: windowscodecs: Correct GIF LZW compression initialization for images with truncated color palette.
From: Jiajin Cui <cuijiajin@uniontech.com> Supersedes https://gitlab.winehq.org/wine/wine/-/merge_requests/9486 Signed-off-by: Dmitry Timoshkov <dmitry@baikal.ru> --- dlls/windowscodecs/gifformat.c | 37 ++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/dlls/windowscodecs/gifformat.c b/dlls/windowscodecs/gifformat.c index 4fde4f81bba..290ae6bf899 100644 --- a/dlls/windowscodecs/gifformat.c +++ b/dlls/windowscodecs/gifformat.c @@ -1415,7 +1415,7 @@ static inline int read_byte(struct input_stream *in, unsigned char *byte) return 0; } -static HRESULT gif_compress(IStream *out_stream, const BYTE *in_data, ULONG in_size) +static HRESULT gif_compress(IStream *out_stream, const BYTE *in_data, ULONG in_size, int color_bits) { struct input_stream in; struct output_stream out; @@ -1429,7 +1429,7 @@ static HRESULT gif_compress(IStream *out_stream, const BYTE *in_data, ULONG in_s out.gif_block.len = 0; out.out = out_stream; - init_code_bits = suffix = 8; + init_code_bits = suffix = max(2, color_bits); if (IStream_Write(out.out, &suffix, sizeof(suffix), NULL) != S_OK) return E_FAIL; @@ -1534,6 +1534,7 @@ static HRESULT WINAPI GifFrameEncode_Commit(IWICBitmapFrameEncode *iface) if (hr == S_OK) { struct image_descriptor imd; + int colors, color_bits = 1; /* Image Descriptor */ imd.left = 0; @@ -1548,23 +1549,33 @@ static HRESULT WINAPI GifFrameEncode_Commit(IWICBitmapFrameEncode *iface) } /* FIXME: interlace flag */ hr = IStream_Write(This->encoder->stream, &imd, sizeof(imd), NULL); - if (hr == S_OK && This->colors) + if (hr == S_OK) { - UINT i; - - /* Local Color Table */ - memset(gif_palette, 0, sizeof(gif_palette)); - for (i = 0; i < This->colors; i++) + if (This->colors) { - gif_palette[i][0] = (This->palette[i] >> 16) & 0xff; - gif_palette[i][1] = (This->palette[i] >> 8) & 0xff; - gif_palette[i][2] = This->palette[i] & 0xff; + UINT i; + + /* Local Color Table */ + memset(gif_palette, 0, sizeof(gif_palette)); + for (i = 0; i < This->colors; i++) + { + gif_palette[i][0] = (This->palette[i] >> 16) & 0xff; + gif_palette[i][1] = (This->palette[i] >> 8) & 0xff; + gif_palette[i][2] = This->palette[i] & 0xff; + } + hr = IStream_Write(This->encoder->stream, gif_palette, sizeof(gif_palette), NULL); + if (hr == S_OK) + colors = This->colors; } - hr = IStream_Write(This->encoder->stream, gif_palette, sizeof(gif_palette), NULL); + else + colors = This->encoder->colors; + if (hr == S_OK) { + while ((1 << color_bits) < colors) color_bits++; + /* Image Data */ - hr = gif_compress(This->encoder->stream, This->image_data, This->width * This->height); + hr = gif_compress(This->encoder->stream, This->image_data, This->width * This->height, color_bits); if (hr == S_OK) This->committed = TRUE; } -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/9923
This merge request was approved by Dmitry Timoshkov. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/9923
This merge request was approved by Esme Povirk. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/9923
participants (3)
-
Dmitry Timoshkov (@dmitry) -
Esme Povirk (@madewokherd) -
Jiajin Cui