2009-08-20 16:22:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Vincent Povirk for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#define COBJMACROS
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2009-08-20 18:58:49 +02:00
|
|
|
#include "wingdi.h"
|
2009-08-20 16:22:16 +02:00
|
|
|
#include "objbase.h"
|
|
|
|
#include "wincodec.h"
|
|
|
|
|
|
|
|
#include "wincodecs_private.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
|
|
|
|
|
|
|
|
#include "pshpack1.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
BYTE bWidth;
|
|
|
|
BYTE bHeight;
|
|
|
|
BYTE bColorCount;
|
|
|
|
BYTE bReserved;
|
|
|
|
WORD wPlanes;
|
|
|
|
WORD wBitCount;
|
|
|
|
DWORD dwDIBSize;
|
|
|
|
DWORD dwDIBOffset;
|
|
|
|
} ICONDIRENTRY;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
WORD idReserved;
|
|
|
|
WORD idType;
|
|
|
|
WORD idCount;
|
|
|
|
} ICONHEADER;
|
|
|
|
|
|
|
|
#include "poppack.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2010-11-29 00:58:53 +01:00
|
|
|
IWICBitmapDecoder IWICBitmapDecoder_iface;
|
2009-08-20 16:22:16 +02:00
|
|
|
LONG ref;
|
2009-08-20 16:50:06 +02:00
|
|
|
BOOL initialized;
|
|
|
|
IStream *stream;
|
|
|
|
ICONHEADER header;
|
2010-04-09 21:49:06 +02:00
|
|
|
CRITICAL_SECTION lock; /* must be held when accessing stream */
|
2009-08-20 16:22:16 +02:00
|
|
|
} IcoDecoder;
|
|
|
|
|
2009-08-20 17:06:01 +02:00
|
|
|
typedef struct {
|
2010-11-29 00:58:53 +01:00
|
|
|
IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
|
2009-08-20 17:06:01 +02:00
|
|
|
LONG ref;
|
2010-09-16 20:41:43 +02:00
|
|
|
UINT width, height;
|
2011-07-27 23:29:53 +02:00
|
|
|
double dpiX, dpiY;
|
2009-08-20 18:58:49 +02:00
|
|
|
BYTE *bits;
|
2009-08-20 17:06:01 +02:00
|
|
|
} IcoFrameDecode;
|
|
|
|
|
2010-11-29 00:58:53 +01:00
|
|
|
static inline IcoDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(iface, IcoDecoder, IWICBitmapDecoder_iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline IcoFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(iface, IcoFrameDecode, IWICBitmapFrameDecode_iface);
|
|
|
|
}
|
|
|
|
|
2009-08-20 17:06:01 +02:00
|
|
|
static HRESULT WINAPI IcoFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
|
|
|
|
void **ppv)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
|
2009-08-20 17:06:01 +02:00
|
|
|
TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
|
|
|
|
|
|
|
|
if (!ppv) return E_INVALIDARG;
|
|
|
|
|
|
|
|
if (IsEqualIID(&IID_IUnknown, iid) ||
|
|
|
|
IsEqualIID(&IID_IWICBitmapSource, iid) ||
|
|
|
|
IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
|
|
|
|
{
|
2012-07-25 04:25:35 +02:00
|
|
|
*ppv = &This->IWICBitmapFrameDecode_iface;
|
2009-08-20 17:06:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ppv = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
IUnknown_AddRef((IUnknown*)*ppv);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI IcoFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
|
2009-08-20 17:06:01 +02:00
|
|
|
ULONG ref = InterlockedIncrement(&This->ref);
|
|
|
|
|
|
|
|
TRACE("(%p) refcount=%u\n", iface, ref);
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI IcoFrameDecode_Release(IWICBitmapFrameDecode *iface)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
|
2009-08-20 17:06:01 +02:00
|
|
|
ULONG ref = InterlockedDecrement(&This->ref);
|
|
|
|
|
|
|
|
TRACE("(%p) refcount=%u\n", iface, ref);
|
|
|
|
|
|
|
|
if (ref == 0)
|
|
|
|
{
|
2009-08-20 18:58:49 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, This->bits);
|
2009-08-20 17:06:01 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
|
|
|
|
UINT *puiWidth, UINT *puiHeight)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
|
2009-08-20 17:23:00 +02:00
|
|
|
|
2010-09-16 20:41:43 +02:00
|
|
|
*puiWidth = This->width;
|
|
|
|
*puiHeight = This->height;
|
2009-08-20 17:23:00 +02:00
|
|
|
|
|
|
|
TRACE("(%p) -> (%i,%i)\n", iface, *puiWidth, *puiHeight);
|
|
|
|
|
|
|
|
return S_OK;
|
2009-08-20 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
|
|
|
|
WICPixelFormatGUID *pPixelFormat)
|
|
|
|
{
|
|
|
|
memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
|
|
|
|
double *pDpiX, double *pDpiY)
|
|
|
|
{
|
2011-07-27 23:29:53 +02:00
|
|
|
IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
|
|
|
|
|
|
|
|
*pDpiX = This->dpiX;
|
|
|
|
*pDpiY = This->dpiY;
|
|
|
|
|
|
|
|
TRACE("(%p) -> (%f,%f)\n", iface, *pDpiX, *pDpiY);
|
|
|
|
|
|
|
|
return S_OK;
|
2009-08-20 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
|
|
|
|
IWICPalette *pIPalette)
|
|
|
|
{
|
|
|
|
TRACE("(%p,%p)\n", iface, pIPalette);
|
|
|
|
return WINCODEC_ERR_PALETTEUNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
|
|
|
|
const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
|
2009-08-20 18:58:49 +02:00
|
|
|
TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
|
|
|
|
|
2010-09-16 20:41:43 +02:00
|
|
|
return copy_pixels(32, This->bits, This->width, This->height, This->width * 4,
|
2009-08-20 18:58:49 +02:00
|
|
|
prc, cbStride, cbBufferSize, pbBuffer);
|
2009-08-20 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
|
|
|
|
IWICMetadataQueryReader **ppIMetadataQueryReader)
|
|
|
|
{
|
|
|
|
TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
|
|
|
|
return WINCODEC_ERR_UNSUPPORTEDOPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
|
|
|
|
UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
|
|
|
|
{
|
|
|
|
TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
|
|
|
|
return WINCODEC_ERR_UNSUPPORTEDOPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
|
|
|
|
IWICBitmapSource **ppIThumbnail)
|
|
|
|
{
|
2012-11-15 14:10:31 +01:00
|
|
|
FIXME("(%p,%p)\n", iface, ppIThumbnail);
|
|
|
|
return E_NOTIMPL;
|
2009-08-20 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const IWICBitmapFrameDecodeVtbl IcoFrameDecode_Vtbl = {
|
|
|
|
IcoFrameDecode_QueryInterface,
|
|
|
|
IcoFrameDecode_AddRef,
|
|
|
|
IcoFrameDecode_Release,
|
|
|
|
IcoFrameDecode_GetSize,
|
|
|
|
IcoFrameDecode_GetPixelFormat,
|
|
|
|
IcoFrameDecode_GetResolution,
|
|
|
|
IcoFrameDecode_CopyPalette,
|
|
|
|
IcoFrameDecode_CopyPixels,
|
|
|
|
IcoFrameDecode_GetMetadataQueryReader,
|
|
|
|
IcoFrameDecode_GetColorContexts,
|
|
|
|
IcoFrameDecode_GetThumbnail
|
|
|
|
};
|
|
|
|
|
2010-09-16 20:41:43 +02:00
|
|
|
static inline void pixel_set_trans(DWORD* pixel, BOOL transparent)
|
|
|
|
{
|
|
|
|
if (transparent) *pixel = 0;
|
|
|
|
else *pixel |= 0xff000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT ReadIcoDib(IStream *stream, IcoFrameDecode *result)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
2011-01-10 22:36:14 +01:00
|
|
|
BmpDecoder *bmp_decoder;
|
2010-09-16 20:41:43 +02:00
|
|
|
IWICBitmapDecoder *decoder;
|
|
|
|
IWICBitmapFrameDecode *framedecode;
|
|
|
|
WICPixelFormatGUID pixelformat;
|
|
|
|
IWICBitmapSource *source;
|
|
|
|
int has_alpha=FALSE; /* if TRUE, alpha data might be in the image data */
|
|
|
|
WICRect rc;
|
|
|
|
|
2011-01-10 22:36:14 +01:00
|
|
|
hr = IcoDibDecoder_CreateInstance(&bmp_decoder);
|
2010-09-16 20:41:43 +02:00
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2011-01-10 22:36:14 +01:00
|
|
|
BmpDecoder_GetWICDecoder(bmp_decoder, &decoder);
|
2010-09-16 20:41:43 +02:00
|
|
|
hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
hr = IWICBitmapFrameDecode_GetSize(framedecode, &result->width, &result->height);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
result->bits = HeapAlloc(GetProcessHeap(), 0, result->width * result->height * 4);
|
|
|
|
if (!result->bits) hr = E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &pixelformat);
|
|
|
|
|
|
|
|
if (IsEqualGUID(&pixelformat, &GUID_WICPixelFormat32bppBGR) ||
|
|
|
|
IsEqualGUID(&pixelformat, &GUID_WICPixelFormat32bppBGRA))
|
|
|
|
{
|
|
|
|
source = (IWICBitmapSource*)framedecode;
|
|
|
|
IWICBitmapSource_AddRef(source);
|
|
|
|
has_alpha = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA,
|
|
|
|
(IWICBitmapSource*)framedecode, &source);
|
|
|
|
has_alpha = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
rc.X = 0;
|
|
|
|
rc.Y = 0;
|
|
|
|
rc.Width = result->width;
|
|
|
|
rc.Height = result->height;
|
|
|
|
hr = IWICBitmapSource_CopyPixels(source, &rc, result->width * 4,
|
|
|
|
result->width * result->height * 4, result->bits);
|
|
|
|
|
|
|
|
IWICBitmapSource_Release(source);
|
|
|
|
}
|
|
|
|
|
2011-07-27 23:29:53 +02:00
|
|
|
if (SUCCEEDED(hr))
|
2011-07-29 18:07:37 +02:00
|
|
|
hr = IWICBitmapFrameDecode_GetResolution(framedecode, &result->dpiX, &result->dpiY);
|
2011-07-27 23:29:53 +02:00
|
|
|
|
2010-09-16 20:41:43 +02:00
|
|
|
IWICBitmapFrameDecode_Release(framedecode);
|
|
|
|
}
|
|
|
|
|
2011-01-10 21:36:19 +01:00
|
|
|
if (SUCCEEDED(hr) && has_alpha)
|
|
|
|
{
|
|
|
|
/* If the alpha channel is fully transparent, we should ignore it. */
|
|
|
|
int nonzero_alpha = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<(result->height*result->width); i++)
|
|
|
|
{
|
|
|
|
if (result->bits[i*4+3] != 0)
|
|
|
|
{
|
|
|
|
nonzero_alpha = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nonzero_alpha)
|
|
|
|
{
|
|
|
|
for (i=0; i<(result->height*result->width); i++)
|
|
|
|
result->bits[i*4+3] = 0xff;
|
|
|
|
|
|
|
|
has_alpha = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-16 20:41:43 +02:00
|
|
|
if (SUCCEEDED(hr) && !has_alpha)
|
|
|
|
{
|
|
|
|
/* set alpha data based on the AND mask */
|
|
|
|
UINT andBytesPerRow = (result->width+31)/32*4;
|
|
|
|
UINT andBytes = andBytesPerRow * result->height;
|
|
|
|
INT andStride;
|
|
|
|
BYTE *tempdata=NULL;
|
|
|
|
BYTE *andRow;
|
|
|
|
BYTE *bitsRow;
|
|
|
|
UINT bitsStride = result->width * 4;
|
|
|
|
UINT x, y;
|
|
|
|
ULONG offset;
|
|
|
|
ULONG bytesread;
|
|
|
|
LARGE_INTEGER seek;
|
|
|
|
int topdown;
|
|
|
|
|
2011-01-10 22:36:14 +01:00
|
|
|
BmpDecoder_FindIconMask(bmp_decoder, &offset, &topdown);
|
2010-09-16 20:41:43 +02:00
|
|
|
|
|
|
|
if (offset)
|
|
|
|
{
|
|
|
|
seek.QuadPart = offset;
|
|
|
|
|
|
|
|
hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, 0);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
tempdata = HeapAlloc(GetProcessHeap(), 0, andBytes);
|
|
|
|
if (!tempdata) hr = E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
hr = IStream_Read(stream, tempdata, andBytes, &bytesread);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr) && bytesread == andBytes)
|
|
|
|
{
|
|
|
|
if (topdown)
|
|
|
|
{
|
|
|
|
andStride = andBytesPerRow;
|
|
|
|
andRow = tempdata;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
andStride = -andBytesPerRow;
|
|
|
|
andRow = tempdata + (result->height-1)*andBytesPerRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
bitsRow = result->bits;
|
|
|
|
for (y=0; y<result->height; y++) {
|
|
|
|
BYTE *andByte=andRow;
|
|
|
|
DWORD *bitsPixel=(DWORD*)bitsRow;
|
|
|
|
for (x=0; x<result->width; x+=8) {
|
|
|
|
BYTE andVal=*andByte++;
|
|
|
|
pixel_set_trans(bitsPixel++, andVal>>7&1);
|
|
|
|
if (x+1 < result->width) pixel_set_trans(bitsPixel++, andVal>>6&1);
|
|
|
|
if (x+2 < result->width) pixel_set_trans(bitsPixel++, andVal>>5&1);
|
|
|
|
if (x+3 < result->width) pixel_set_trans(bitsPixel++, andVal>>4&1);
|
|
|
|
if (x+4 < result->width) pixel_set_trans(bitsPixel++, andVal>>3&1);
|
|
|
|
if (x+5 < result->width) pixel_set_trans(bitsPixel++, andVal>>2&1);
|
|
|
|
if (x+6 < result->width) pixel_set_trans(bitsPixel++, andVal>>1&1);
|
|
|
|
if (x+7 < result->width) pixel_set_trans(bitsPixel++, andVal&1);
|
|
|
|
}
|
|
|
|
andRow += andStride;
|
|
|
|
bitsRow += bitsStride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, tempdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IWICBitmapDecoder_Release(decoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2010-09-29 21:24:08 +02:00
|
|
|
static HRESULT ReadIcoPng(IStream *stream, IcoFrameDecode *result)
|
|
|
|
{
|
|
|
|
IWICBitmapDecoder *decoder = NULL;
|
|
|
|
IWICBitmapFrameDecode *sourceFrame = NULL;
|
|
|
|
IWICBitmapSource *sourceBitmap = NULL;
|
|
|
|
WICRect rect;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
hr = PngDecoder_CreateInstance(NULL, &IID_IWICBitmapDecoder, (void**)&decoder);
|
|
|
|
if (FAILED(hr))
|
|
|
|
goto end;
|
|
|
|
hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
|
|
|
|
if (FAILED(hr))
|
|
|
|
goto end;
|
|
|
|
hr = IWICBitmapDecoder_GetFrame(decoder, 0, &sourceFrame);
|
|
|
|
if (FAILED(hr))
|
|
|
|
goto end;
|
|
|
|
hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)sourceFrame, &sourceBitmap);
|
|
|
|
if (FAILED(hr))
|
|
|
|
goto end;
|
|
|
|
hr = IWICBitmapFrameDecode_GetSize(sourceFrame, &result->width, &result->height);
|
2011-07-27 23:29:53 +02:00
|
|
|
if (FAILED(hr))
|
|
|
|
goto end;
|
|
|
|
hr = IWICBitmapFrameDecode_GetResolution(sourceFrame, &result->dpiX, &result->dpiY);
|
2010-09-29 21:24:08 +02:00
|
|
|
if (FAILED(hr))
|
|
|
|
goto end;
|
|
|
|
result->bits = HeapAlloc(GetProcessHeap(), 0, 4 * result->width * result->height);
|
|
|
|
if (result->bits == NULL)
|
|
|
|
{
|
|
|
|
hr = E_OUTOFMEMORY;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
rect.X = 0;
|
|
|
|
rect.Y = 0;
|
|
|
|
rect.Width = result->width;
|
|
|
|
rect.Height = result->height;
|
|
|
|
hr = IWICBitmapSource_CopyPixels(sourceBitmap, &rect, 4*result->width,
|
|
|
|
4*result->width*result->height, result->bits);
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (decoder != NULL)
|
|
|
|
IWICBitmapDecoder_Release(decoder);
|
|
|
|
if (sourceFrame != NULL)
|
|
|
|
IWICBitmapFrameDecode_Release(sourceFrame);
|
|
|
|
if (sourceBitmap != NULL)
|
|
|
|
IWICBitmapSource_Release(sourceBitmap);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2009-08-20 16:22:16 +02:00
|
|
|
static HRESULT WINAPI IcoDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
|
|
|
|
void **ppv)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
|
2009-08-20 16:22:16 +02:00
|
|
|
TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
|
|
|
|
|
|
|
|
if (!ppv) return E_INVALIDARG;
|
|
|
|
|
2012-07-25 04:25:35 +02:00
|
|
|
if (IsEqualIID(&IID_IUnknown, iid) ||
|
|
|
|
IsEqualIID(&IID_IWICBitmapDecoder, iid))
|
2009-08-20 16:22:16 +02:00
|
|
|
{
|
2012-07-25 04:25:35 +02:00
|
|
|
*ppv = &This->IWICBitmapDecoder_iface;
|
2009-08-20 16:22:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ppv = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
IUnknown_AddRef((IUnknown*)*ppv);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI IcoDecoder_AddRef(IWICBitmapDecoder *iface)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
|
2009-08-20 16:22:16 +02:00
|
|
|
ULONG ref = InterlockedIncrement(&This->ref);
|
|
|
|
|
|
|
|
TRACE("(%p) refcount=%u\n", iface, ref);
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI IcoDecoder_Release(IWICBitmapDecoder *iface)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
|
2009-08-20 16:22:16 +02:00
|
|
|
ULONG ref = InterlockedDecrement(&This->ref);
|
|
|
|
|
|
|
|
TRACE("(%p) refcount=%u\n", iface, ref);
|
|
|
|
|
|
|
|
if (ref == 0)
|
|
|
|
{
|
2010-04-09 21:49:06 +02:00
|
|
|
This->lock.DebugInfo->Spare[0] = 0;
|
|
|
|
DeleteCriticalSection(&This->lock);
|
2009-08-20 16:50:06 +02:00
|
|
|
if (This->stream) IStream_Release(This->stream);
|
2009-08-20 16:22:16 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2012-11-23 04:48:49 +01:00
|
|
|
static HRESULT WINAPI IcoDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
|
|
|
|
DWORD *capability)
|
2009-08-20 16:22:16 +02:00
|
|
|
{
|
2012-11-23 04:48:49 +01:00
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
TRACE("(%p,%p,%p)\n", iface, stream, capability);
|
|
|
|
|
|
|
|
if (!stream || !capability) return E_INVALIDARG;
|
|
|
|
|
|
|
|
hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
|
|
|
|
if (hr != S_OK) return hr;
|
|
|
|
|
|
|
|
*capability = WICBitmapDecoderCapabilityCanDecodeAllImages;
|
|
|
|
return S_OK;
|
2009-08-20 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
|
|
|
|
WICDecodeOptions cacheOptions)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
|
2009-08-20 16:50:06 +02:00
|
|
|
LARGE_INTEGER seek;
|
|
|
|
HRESULT hr;
|
|
|
|
ULONG bytesread;
|
|
|
|
TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
|
|
|
|
|
2010-04-09 21:49:06 +02:00
|
|
|
EnterCriticalSection(&This->lock);
|
|
|
|
|
|
|
|
if (This->initialized)
|
|
|
|
{
|
|
|
|
hr = WINCODEC_ERR_WRONGSTATE;
|
|
|
|
goto end;
|
|
|
|
}
|
2009-08-20 16:50:06 +02:00
|
|
|
|
|
|
|
seek.QuadPart = 0;
|
|
|
|
hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
|
2010-04-09 21:49:06 +02:00
|
|
|
if (FAILED(hr)) goto end;
|
2009-08-20 16:50:06 +02:00
|
|
|
|
|
|
|
hr = IStream_Read(pIStream, &This->header, sizeof(ICONHEADER), &bytesread);
|
2010-04-09 21:49:06 +02:00
|
|
|
if (FAILED(hr)) goto end;
|
2009-08-20 16:50:06 +02:00
|
|
|
if (bytesread != sizeof(ICONHEADER) ||
|
|
|
|
This->header.idReserved != 0 ||
|
2010-04-09 21:49:06 +02:00
|
|
|
This->header.idType != 1)
|
|
|
|
{
|
|
|
|
hr = E_FAIL;
|
|
|
|
goto end;
|
|
|
|
}
|
2009-08-20 16:50:06 +02:00
|
|
|
|
|
|
|
This->initialized = TRUE;
|
|
|
|
This->stream = pIStream;
|
|
|
|
IStream_AddRef(pIStream);
|
|
|
|
|
2010-04-09 21:49:06 +02:00
|
|
|
end:
|
|
|
|
|
|
|
|
LeaveCriticalSection(&This->lock);
|
|
|
|
|
|
|
|
return hr;
|
2009-08-20 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
|
|
|
|
GUID *pguidContainerFormat)
|
|
|
|
{
|
2011-07-27 22:54:07 +02:00
|
|
|
memcpy(pguidContainerFormat, &GUID_ContainerFormatIco, sizeof(GUID));
|
|
|
|
return S_OK;
|
2009-08-20 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
|
|
|
|
IWICBitmapDecoderInfo **ppIDecoderInfo)
|
|
|
|
{
|
2011-07-27 23:03:09 +02:00
|
|
|
HRESULT hr;
|
|
|
|
IWICComponentInfo *compinfo;
|
|
|
|
|
|
|
|
TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
|
|
|
|
|
|
|
|
hr = CreateComponentInfo(&CLSID_WICIcoDecoder, &compinfo);
|
|
|
|
if (FAILED(hr)) return hr;
|
|
|
|
|
|
|
|
hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
|
|
|
|
(void**)ppIDecoderInfo);
|
|
|
|
|
|
|
|
IWICComponentInfo_Release(compinfo);
|
|
|
|
|
|
|
|
return hr;
|
2009-08-20 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoDecoder_CopyPalette(IWICBitmapDecoder *iface,
|
|
|
|
IWICPalette *pIPalette)
|
|
|
|
{
|
|
|
|
TRACE("(%p,%p)\n", iface, pIPalette);
|
|
|
|
return WINCODEC_ERR_PALETTEUNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
|
|
|
|
IWICMetadataQueryReader **ppIMetadataQueryReader)
|
|
|
|
{
|
|
|
|
TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
|
|
|
|
return WINCODEC_ERR_UNSUPPORTEDOPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoDecoder_GetPreview(IWICBitmapDecoder *iface,
|
|
|
|
IWICBitmapSource **ppIBitmapSource)
|
|
|
|
{
|
|
|
|
TRACE("(%p,%p)\n", iface, ppIBitmapSource);
|
|
|
|
return WINCODEC_ERR_UNSUPPORTEDOPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoDecoder_GetColorContexts(IWICBitmapDecoder *iface,
|
|
|
|
UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
|
|
|
|
{
|
|
|
|
TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
|
|
|
|
return WINCODEC_ERR_UNSUPPORTEDOPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoDecoder_GetThumbnail(IWICBitmapDecoder *iface,
|
|
|
|
IWICBitmapSource **ppIThumbnail)
|
|
|
|
{
|
|
|
|
TRACE("(%p,%p)\n", iface, ppIThumbnail);
|
|
|
|
return WINCODEC_ERR_CODECNOTHUMBNAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoDecoder_GetFrameCount(IWICBitmapDecoder *iface,
|
|
|
|
UINT *pCount)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
|
2009-08-20 17:06:01 +02:00
|
|
|
|
2012-11-23 04:48:28 +01:00
|
|
|
if (!pCount) return E_INVALIDARG;
|
2009-08-20 17:06:01 +02:00
|
|
|
|
2012-11-23 04:48:28 +01:00
|
|
|
EnterCriticalSection(&This->lock);
|
|
|
|
*pCount = This->initialized ? This->header.idCount : 0;
|
|
|
|
LeaveCriticalSection(&This->lock);
|
|
|
|
|
|
|
|
TRACE("(%p) <-- %d\n", iface, *pCount);
|
2009-08-20 17:06:01 +02:00
|
|
|
|
|
|
|
return S_OK;
|
2009-08-20 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IcoDecoder_GetFrame(IWICBitmapDecoder *iface,
|
|
|
|
UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
|
|
|
|
{
|
2010-11-29 00:58:53 +01:00
|
|
|
IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
|
2010-04-09 21:49:06 +02:00
|
|
|
IcoFrameDecode *result=NULL;
|
2009-08-20 17:06:01 +02:00
|
|
|
LARGE_INTEGER seek;
|
2010-09-16 20:41:43 +02:00
|
|
|
ULARGE_INTEGER offset, length;
|
2009-08-20 17:06:01 +02:00
|
|
|
HRESULT hr;
|
|
|
|
ULONG bytesread;
|
2010-09-16 20:41:43 +02:00
|
|
|
ICONDIRENTRY entry;
|
|
|
|
IWICStream *substream=NULL;
|
|
|
|
DWORD magic;
|
2009-08-20 17:06:01 +02:00
|
|
|
TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
|
|
|
|
|
2010-04-09 21:49:06 +02:00
|
|
|
EnterCriticalSection(&This->lock);
|
|
|
|
|
|
|
|
if (!This->initialized)
|
|
|
|
{
|
2012-11-23 08:41:33 +01:00
|
|
|
hr = WINCODEC_ERR_FRAMEMISSING;
|
2010-04-09 21:49:06 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2009-08-20 17:06:01 +02:00
|
|
|
|
2010-04-09 21:49:06 +02:00
|
|
|
if (This->header.idCount < index)
|
|
|
|
{
|
|
|
|
hr = E_INVALIDARG;
|
|
|
|
goto fail;
|
|
|
|
}
|
2009-08-20 17:06:01 +02:00
|
|
|
|
|
|
|
result = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoFrameDecode));
|
2010-04-09 21:49:06 +02:00
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
hr = E_OUTOFMEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
2009-08-20 17:06:01 +02:00
|
|
|
|
2010-11-29 00:58:53 +01:00
|
|
|
result->IWICBitmapFrameDecode_iface.lpVtbl = &IcoFrameDecode_Vtbl;
|
2009-08-20 17:06:01 +02:00
|
|
|
result->ref = 1;
|
2009-08-20 18:58:49 +02:00
|
|
|
result->bits = NULL;
|
2009-08-20 17:06:01 +02:00
|
|
|
|
|
|
|
/* read the icon entry */
|
|
|
|
seek.QuadPart = sizeof(ICONHEADER) + sizeof(ICONDIRENTRY) * index;
|
|
|
|
hr = IStream_Seek(This->stream, seek, STREAM_SEEK_SET, 0);
|
|
|
|
if (FAILED(hr)) goto fail;
|
|
|
|
|
2010-09-16 20:41:43 +02:00
|
|
|
hr = IStream_Read(This->stream, &entry, sizeof(ICONDIRENTRY), &bytesread);
|
2009-08-20 17:06:01 +02:00
|
|
|
if (FAILED(hr) || bytesread != sizeof(ICONDIRENTRY)) goto fail;
|
|
|
|
|
2010-09-16 20:41:43 +02:00
|
|
|
/* create a stream object for this icon */
|
|
|
|
hr = StreamImpl_Create(&substream);
|
|
|
|
if (FAILED(hr)) goto fail;
|
|
|
|
|
|
|
|
offset.QuadPart = entry.dwDIBOffset;
|
|
|
|
length.QuadPart = entry.dwDIBSize;
|
|
|
|
hr = IWICStream_InitializeFromIStreamRegion(substream, This->stream, offset, length);
|
|
|
|
if (FAILED(hr)) goto fail;
|
|
|
|
|
|
|
|
/* read the bitmapinfo size or magic number */
|
|
|
|
hr = IWICStream_Read(substream, &magic, sizeof(magic), &bytesread);
|
|
|
|
if (FAILED(hr) || bytesread != sizeof(magic)) goto fail;
|
|
|
|
|
|
|
|
/* forward to the appropriate decoding function based on the magic number */
|
|
|
|
switch (magic)
|
|
|
|
{
|
|
|
|
case sizeof(BITMAPCOREHEADER):
|
|
|
|
case 64: /* sizeof(BITMAPCOREHEADER2) */
|
|
|
|
case sizeof(BITMAPINFOHEADER):
|
|
|
|
case sizeof(BITMAPV4HEADER):
|
|
|
|
case sizeof(BITMAPV5HEADER):
|
|
|
|
hr = ReadIcoDib((IStream*)substream, result);
|
|
|
|
break;
|
|
|
|
case 0x474e5089:
|
2010-09-29 21:24:08 +02:00
|
|
|
hr = ReadIcoPng((IStream*)substream, result);
|
2010-09-16 20:41:43 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME("Unrecognized ICO frame magic: %x\n", magic);
|
|
|
|
hr = E_FAIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (FAILED(hr)) goto fail;
|
2009-08-20 17:06:01 +02:00
|
|
|
|
2012-07-25 04:25:35 +02:00
|
|
|
*ppIBitmapFrame = &result->IWICBitmapFrameDecode_iface;
|
2009-08-20 17:06:01 +02:00
|
|
|
|
2010-04-09 21:49:06 +02:00
|
|
|
LeaveCriticalSection(&This->lock);
|
|
|
|
|
2012-07-27 11:08:19 +02:00
|
|
|
IWICStream_Release(substream);
|
2011-02-05 20:01:20 +01:00
|
|
|
|
2009-08-20 17:06:01 +02:00
|
|
|
return S_OK;
|
|
|
|
|
|
|
|
fail:
|
2010-04-09 21:49:06 +02:00
|
|
|
LeaveCriticalSection(&This->lock);
|
2009-08-20 17:06:01 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, result);
|
2012-07-27 11:08:19 +02:00
|
|
|
if (substream) IWICStream_Release(substream);
|
2009-08-20 17:06:01 +02:00
|
|
|
if (SUCCEEDED(hr)) hr = E_FAIL;
|
|
|
|
TRACE("<-- %x\n", hr);
|
|
|
|
return hr;
|
2009-08-20 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const IWICBitmapDecoderVtbl IcoDecoder_Vtbl = {
|
|
|
|
IcoDecoder_QueryInterface,
|
|
|
|
IcoDecoder_AddRef,
|
|
|
|
IcoDecoder_Release,
|
|
|
|
IcoDecoder_QueryCapability,
|
|
|
|
IcoDecoder_Initialize,
|
|
|
|
IcoDecoder_GetContainerFormat,
|
|
|
|
IcoDecoder_GetDecoderInfo,
|
|
|
|
IcoDecoder_CopyPalette,
|
|
|
|
IcoDecoder_GetMetadataQueryReader,
|
|
|
|
IcoDecoder_GetPreview,
|
|
|
|
IcoDecoder_GetColorContexts,
|
|
|
|
IcoDecoder_GetThumbnail,
|
|
|
|
IcoDecoder_GetFrameCount,
|
|
|
|
IcoDecoder_GetFrame
|
|
|
|
};
|
|
|
|
|
|
|
|
HRESULT IcoDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
|
|
|
|
{
|
|
|
|
IcoDecoder *This;
|
|
|
|
HRESULT ret;
|
|
|
|
|
|
|
|
TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
|
|
|
|
|
|
|
|
*ppv = NULL;
|
|
|
|
|
|
|
|
if (pUnkOuter) return CLASS_E_NOAGGREGATION;
|
|
|
|
|
|
|
|
This = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoDecoder));
|
|
|
|
if (!This) return E_OUTOFMEMORY;
|
|
|
|
|
2010-11-29 00:58:53 +01:00
|
|
|
This->IWICBitmapDecoder_iface.lpVtbl = &IcoDecoder_Vtbl;
|
2009-08-20 16:22:16 +02:00
|
|
|
This->ref = 1;
|
2009-08-20 16:50:06 +02:00
|
|
|
This->stream = NULL;
|
|
|
|
This->initialized = FALSE;
|
2010-04-09 21:49:06 +02:00
|
|
|
InitializeCriticalSection(&This->lock);
|
|
|
|
This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IcoDecoder.lock");
|
2009-08-20 16:22:16 +02:00
|
|
|
|
2012-07-25 04:25:35 +02:00
|
|
|
ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
|
|
|
|
IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
|
2009-08-20 16:22:16 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|