windowscodecs: Add support for decoding RLE4-encoded BMP files.
This commit is contained in:
parent
07a6b89f87
commit
48d58cef59
|
@ -492,6 +492,127 @@ fail:
|
|||
return hr;
|
||||
}
|
||||
|
||||
static HRESULT BmpFrameDecode_ReadRLE4(BmpFrameDecode* This)
|
||||
{
|
||||
UINT bytesperrow;
|
||||
UINT width, height;
|
||||
BYTE *rledata, *cursor, *rledataend;
|
||||
UINT rlesize, datasize, palettesize;
|
||||
DWORD palette[16];
|
||||
UINT x, y;
|
||||
DWORD *bgrdata;
|
||||
HRESULT hr;
|
||||
LARGE_INTEGER offbits;
|
||||
ULONG bytesread;
|
||||
|
||||
width = This->bih.bV5Width;
|
||||
height = abs(This->bih.bV5Height);
|
||||
bytesperrow = width * 4;
|
||||
datasize = bytesperrow * height;
|
||||
rlesize = This->bih.bV5SizeImage;
|
||||
if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 16)
|
||||
palettesize = 4 * This->bih.bV5ClrUsed;
|
||||
else
|
||||
palettesize = 4 * 16;
|
||||
|
||||
rledata = HeapAlloc(GetProcessHeap(), 0, rlesize);
|
||||
This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
|
||||
if (!This->imagedata || !rledata)
|
||||
{
|
||||
hr = E_OUTOFMEMORY;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* read palette */
|
||||
offbits.QuadPart = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
|
||||
hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
|
||||
if (FAILED(hr)) goto fail;
|
||||
|
||||
hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
|
||||
if (FAILED(hr) || bytesread != palettesize) goto fail;
|
||||
|
||||
/* read RLE data */
|
||||
offbits.QuadPart = This->bfh.bfOffBits;
|
||||
hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
|
||||
if (FAILED(hr)) goto fail;
|
||||
|
||||
hr = IStream_Read(This->stream, rledata, rlesize, &bytesread);
|
||||
if (FAILED(hr) || bytesread != rlesize) goto fail;
|
||||
|
||||
/* decode RLE */
|
||||
bgrdata = (DWORD*)This->imagedata;
|
||||
x = 0;
|
||||
y = 0;
|
||||
rledataend = rledata + rlesize;
|
||||
cursor = rledata;
|
||||
while (cursor < rledataend && y < height)
|
||||
{
|
||||
BYTE length = *cursor++;
|
||||
if (length == 0)
|
||||
{
|
||||
/* escape code */
|
||||
BYTE escape = *cursor++;
|
||||
switch(escape)
|
||||
{
|
||||
case 0: /* end of line */
|
||||
x = 0;
|
||||
y++;
|
||||
break;
|
||||
case 1: /* end of bitmap */
|
||||
goto end;
|
||||
case 2: /* delta */
|
||||
if (cursor < rledataend)
|
||||
{
|
||||
x += *cursor++;
|
||||
y += *cursor++;
|
||||
}
|
||||
break;
|
||||
default: /* absolute mode */
|
||||
length = escape;
|
||||
while (cursor < rledataend && length-- && x < width)
|
||||
{
|
||||
BYTE colors = *cursor++;
|
||||
bgrdata[y*width + x++] = palette[colors>>4];
|
||||
if (length-- && x < width)
|
||||
bgrdata[y*width + x++] = palette[colors&0xf];
|
||||
else
|
||||
break;
|
||||
}
|
||||
if ((cursor - rledata) & 1) cursor++; /* skip pad byte */
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
BYTE colors = *cursor++;
|
||||
DWORD color1 = palette[colors>>4];
|
||||
DWORD color2 = palette[colors&0xf];
|
||||
while (length-- && x < width)
|
||||
{
|
||||
bgrdata[y*width + x++] = color1;
|
||||
if (length-- && x < width)
|
||||
bgrdata[y*width + x++] = color2;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
HeapFree(GetProcessHeap(), 0, rledata);
|
||||
|
||||
This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
|
||||
This->stride = -bytesperrow;
|
||||
|
||||
return S_OK;
|
||||
|
||||
fail:
|
||||
HeapFree(GetProcessHeap(), 0, rledata);
|
||||
HeapFree(GetProcessHeap(), 0, This->imagedata);
|
||||
This->imagedata = NULL;
|
||||
if (SUCCEEDED(hr)) hr = E_FAIL;
|
||||
return hr;
|
||||
}
|
||||
|
||||
static HRESULT BmpFrameDecode_ReadUnsupported(BmpFrameDecode* This)
|
||||
{
|
||||
return E_FAIL;
|
||||
|
@ -626,6 +747,11 @@ static HRESULT BmpDecoder_ReadHeaders(BmpDecoder* This, IStream *stream)
|
|||
This->read_data_func = BmpFrameDecode_ReadRLE8;
|
||||
This->pixelformat = &GUID_WICPixelFormat32bppBGR;
|
||||
break;
|
||||
case BI_RLE4:
|
||||
This->bitsperpixel = 32;
|
||||
This->read_data_func = BmpFrameDecode_ReadRLE4;
|
||||
This->pixelformat = &GUID_WICPixelFormat32bppBGR;
|
||||
break;
|
||||
default:
|
||||
This->bitsperpixel = 0;
|
||||
This->read_data_func = BmpFrameDecode_ReadUnsupported;
|
||||
|
|
|
@ -721,6 +721,172 @@ static void test_decode_rle8(void)
|
|||
IWICBitmapDecoder_Release(decoder);
|
||||
}
|
||||
|
||||
static const char testbmp_rle4[] = {
|
||||
/* BITMAPFILEHEADER */
|
||||
66,77, /* "BM" */
|
||||
142,0,0,0, /* file size */
|
||||
0,0,0,0, /* reserved */
|
||||
78,0,0,0, /* offset to bits */
|
||||
/* BITMAPINFOHEADER */
|
||||
40,0,0,0, /* header size */
|
||||
8,0,0,0, /* width */
|
||||
8,0,0,0, /* height */
|
||||
1,0, /* planes */
|
||||
4,0, /* bit count */
|
||||
2,0,0,0, /* compression = BI_RLE4 */
|
||||
64,0,0,0, /* image size */
|
||||
19,11,0,0, /* X pixels per meter */
|
||||
19,11,0,0, /* Y pixels per meter */
|
||||
6,0,0,0, /* colors used */
|
||||
6,0,0,0, /* colors important */
|
||||
/* color table */
|
||||
0,0,0,0,
|
||||
255,0,0,0,
|
||||
0,0,255,0,
|
||||
255,0,255,0,
|
||||
0,255,0,0,
|
||||
255,255,255,0,
|
||||
/* bits */
|
||||
0,8,68,68,0,0,0,0,0,8,68,68,3,48,0,0,0,8,68,68,3,48,0,0,0,8,68,68,0,0,0,0,0,8,81,81,34,34,0,0,0,8,21,21,34,34,0,0,0,8,81,81,34,34,0,0,0,8,21,21,34,34,0,1
|
||||
};
|
||||
|
||||
static void test_decode_rle4(void)
|
||||
{
|
||||
IWICBitmapDecoder *decoder, *decoder2;
|
||||
IWICBitmapFrameDecode *framedecode;
|
||||
HRESULT hr;
|
||||
HGLOBAL hbmpdata;
|
||||
char *bmpdata;
|
||||
IStream *bmpstream;
|
||||
DWORD capability=0;
|
||||
GUID guidresult;
|
||||
UINT count=0, width=0, height=0;
|
||||
double dpiX, dpiY;
|
||||
DWORD imagedata[64] = {1};
|
||||
const DWORD expected_imagedata[64] = {
|
||||
0x0000ff,0xffffff,0x0000ff,0xffffff,0xff0000,0xff0000,0xff0000,0xff0000,
|
||||
0xffffff,0x0000ff,0xffffff,0x0000ff,0xff0000,0xff0000,0xff0000,0xff0000,
|
||||
0x0000ff,0xffffff,0x0000ff,0xffffff,0xff0000,0xff0000,0xff0000,0xff0000,
|
||||
0xffffff,0x0000ff,0xffffff,0x0000ff,0xff0000,0xff0000,0xff0000,0xff0000,
|
||||
0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x000000,0x000000,0x000000,0x000000,
|
||||
0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x000000,0xff00ff,0xff00ff,0x000000,
|
||||
0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x000000,0xff00ff,0xff00ff,0x000000,
|
||||
0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x000000,0x000000,0x000000,0x000000};
|
||||
WICColor palettedata[6] = {1};
|
||||
const WICColor expected_palettedata[6] = {
|
||||
0xff000000,0xff0000ff,0xffff0000,0xffff00ff,0xff00ff00,0xffffffff};
|
||||
WICRect rc;
|
||||
|
||||
hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
|
||||
&IID_IWICBitmapDecoder, (void**)&decoder);
|
||||
ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
|
||||
if (!SUCCEEDED(hr)) return;
|
||||
|
||||
hbmpdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(testbmp_rle4));
|
||||
ok(hbmpdata != 0, "GlobalAlloc failed\n");
|
||||
if (hbmpdata)
|
||||
{
|
||||
bmpdata = GlobalLock(hbmpdata);
|
||||
memcpy(bmpdata, testbmp_rle4, sizeof(testbmp_rle4));
|
||||
GlobalUnlock(hbmpdata);
|
||||
|
||||
hr = CreateStreamOnHGlobal(hbmpdata, FALSE, &bmpstream);
|
||||
ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
|
||||
ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
|
||||
|
||||
hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
|
||||
ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
|
||||
ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatBmp), "unexpected container format\n");
|
||||
|
||||
hr = IWICBitmapDecoder_GetFrameCount(decoder, &count);
|
||||
ok(SUCCEEDED(hr), "GetFrameCount failed, hr=%x\n", hr);
|
||||
ok(count == 1, "unexpected count %u\n", count);
|
||||
|
||||
hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
|
||||
ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
IWICImagingFactory *factory;
|
||||
IWICPalette *palette;
|
||||
|
||||
hr = IWICBitmapFrameDecode_GetSize(framedecode, &width, &height);
|
||||
ok(SUCCEEDED(hr), "GetSize failed, hr=%x\n", hr);
|
||||
ok(width == 8, "expected width=8, got %u\n", width);
|
||||
ok(height == 8, "expected height=8, got %u\n", height);
|
||||
|
||||
hr = IWICBitmapFrameDecode_GetResolution(framedecode, &dpiX, &dpiY);
|
||||
ok(SUCCEEDED(hr), "GetResolution failed, hr=%x\n", hr);
|
||||
ok(fabs(dpiX - 72.0) < 0.01, "expected dpiX=96.0, got %f\n", dpiX);
|
||||
ok(fabs(dpiY - 72.0) < 0.01, "expected dpiY=96.0, got %f\n", dpiY);
|
||||
|
||||
hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &guidresult);
|
||||
ok(SUCCEEDED(hr), "GetPixelFormat failed, hr=%x\n", hr);
|
||||
ok(IsEqualGUID(&guidresult, &GUID_WICPixelFormat32bppBGR), "unexpected pixel format\n");
|
||||
|
||||
hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
|
||||
&IID_IWICImagingFactory, (void**)&factory);
|
||||
ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
hr = IWICImagingFactory_CreatePalette(factory, &palette);
|
||||
ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
|
||||
ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %x\n", hr);
|
||||
|
||||
hr = IWICBitmapFrameDecode_CopyPalette(framedecode, palette);
|
||||
ok(SUCCEEDED(hr), "CopyPalette failed, hr=%x\n", hr);
|
||||
|
||||
hr = IWICPalette_GetColorCount(palette, &count);
|
||||
ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
|
||||
ok(count == 6, "expected count=6, got %u\n", count);
|
||||
|
||||
hr = IWICPalette_GetColors(palette, 6, palettedata, &count);
|
||||
ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
|
||||
ok(count == 6, "expected count=6, got %u\n", count);
|
||||
ok(!memcmp(palettedata, expected_palettedata, sizeof(palettedata)), "unexpected palette data\n");
|
||||
|
||||
IWICPalette_Release(palette);
|
||||
}
|
||||
|
||||
IWICImagingFactory_Release(factory);
|
||||
}
|
||||
|
||||
rc.X = 0;
|
||||
rc.Y = 0;
|
||||
rc.Width = 8;
|
||||
rc.Height = 8;
|
||||
hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 32, sizeof(imagedata), (BYTE*)imagedata);
|
||||
ok(SUCCEEDED(hr), "CopyPixels failed, hr=%x\n", hr);
|
||||
ok(!memcmp(imagedata, expected_imagedata, sizeof(imagedata)), "unexpected image data\n");
|
||||
|
||||
IWICBitmapFrameDecode_Release(framedecode);
|
||||
}
|
||||
|
||||
hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
|
||||
&IID_IWICBitmapDecoder, (void**)&decoder2);
|
||||
ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
|
||||
ok(hr == S_OK, "QueryCapability failed, hr=%x\n", hr);
|
||||
ok(capability == (WICBitmapDecoderCapabilityCanDecodeAllImages),
|
||||
"unexpected capabilities: %x\n", capability);
|
||||
}
|
||||
|
||||
IStream_Release(bmpstream);
|
||||
}
|
||||
|
||||
GlobalFree(hbmpdata);
|
||||
}
|
||||
|
||||
IWICBitmapDecoder_Release(decoder);
|
||||
}
|
||||
|
||||
START_TEST(bmpformat)
|
||||
{
|
||||
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
|
||||
|
@ -729,6 +895,7 @@ START_TEST(bmpformat)
|
|||
test_decode_1bpp();
|
||||
test_decode_4bpp();
|
||||
test_decode_rle8();
|
||||
test_decode_rle4();
|
||||
|
||||
CoUninitialize();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue