hlink: Convert dll registration to the IRegistrar mechanism.
[wine] / dlls / windowscodecs / icoformat.c
1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "objbase.h"
29 #include "wincodec.h"
30
31 #include "wincodecs_private.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
36
37 #include "pshpack1.h"
38
39 typedef struct {
40     BYTE bWidth;
41     BYTE bHeight;
42     BYTE bColorCount;
43     BYTE bReserved;
44     WORD wPlanes;
45     WORD wBitCount;
46     DWORD dwDIBSize;
47     DWORD dwDIBOffset;
48 } ICONDIRENTRY;
49
50 typedef struct
51 {
52     WORD idReserved;
53     WORD idType;
54     WORD idCount;
55 } ICONHEADER;
56
57 #include "poppack.h"
58
59 typedef struct {
60     IWICBitmapDecoder IWICBitmapDecoder_iface;
61     LONG ref;
62     BOOL initialized;
63     IStream *stream;
64     ICONHEADER header;
65     CRITICAL_SECTION lock; /* must be held when accessing stream */
66 } IcoDecoder;
67
68 typedef struct {
69     IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
70     LONG ref;
71     UINT width, height;
72     BYTE *bits;
73 } IcoFrameDecode;
74
75 static inline IcoDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
76 {
77     return CONTAINING_RECORD(iface, IcoDecoder, IWICBitmapDecoder_iface);
78 }
79
80 static inline IcoFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
81 {
82     return CONTAINING_RECORD(iface, IcoFrameDecode, IWICBitmapFrameDecode_iface);
83 }
84
85 static HRESULT WINAPI IcoFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
86     void **ppv)
87 {
88     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
89     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
90
91     if (!ppv) return E_INVALIDARG;
92
93     if (IsEqualIID(&IID_IUnknown, iid) ||
94         IsEqualIID(&IID_IWICBitmapSource, iid) ||
95         IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
96     {
97         *ppv = This;
98     }
99     else
100     {
101         *ppv = NULL;
102         return E_NOINTERFACE;
103     }
104
105     IUnknown_AddRef((IUnknown*)*ppv);
106     return S_OK;
107 }
108
109 static ULONG WINAPI IcoFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
110 {
111     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
112     ULONG ref = InterlockedIncrement(&This->ref);
113
114     TRACE("(%p) refcount=%u\n", iface, ref);
115
116     return ref;
117 }
118
119 static ULONG WINAPI IcoFrameDecode_Release(IWICBitmapFrameDecode *iface)
120 {
121     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
122     ULONG ref = InterlockedDecrement(&This->ref);
123
124     TRACE("(%p) refcount=%u\n", iface, ref);
125
126     if (ref == 0)
127     {
128         HeapFree(GetProcessHeap(), 0, This->bits);
129         HeapFree(GetProcessHeap(), 0, This);
130     }
131
132     return ref;
133 }
134
135 static HRESULT WINAPI IcoFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
136     UINT *puiWidth, UINT *puiHeight)
137 {
138     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
139
140     *puiWidth = This->width;
141     *puiHeight = This->height;
142
143     TRACE("(%p) -> (%i,%i)\n", iface, *puiWidth, *puiHeight);
144
145     return S_OK;
146 }
147
148 static HRESULT WINAPI IcoFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
149     WICPixelFormatGUID *pPixelFormat)
150 {
151     memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
152     return S_OK;
153 }
154
155 static HRESULT WINAPI IcoFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
156     double *pDpiX, double *pDpiY)
157 {
158     FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
159     return E_NOTIMPL;
160 }
161
162 static HRESULT WINAPI IcoFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
163     IWICPalette *pIPalette)
164 {
165     TRACE("(%p,%p)\n", iface, pIPalette);
166     return WINCODEC_ERR_PALETTEUNAVAILABLE;
167 }
168
169 static HRESULT WINAPI IcoFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
170     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
171 {
172     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
173     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
174
175     return copy_pixels(32, This->bits, This->width, This->height, This->width * 4,
176         prc, cbStride, cbBufferSize, pbBuffer);
177 }
178
179 static HRESULT WINAPI IcoFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
180     IWICMetadataQueryReader **ppIMetadataQueryReader)
181 {
182     TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
183     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
184 }
185
186 static HRESULT WINAPI IcoFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
187     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
188 {
189     TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
190     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
191 }
192
193 static HRESULT WINAPI IcoFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
194     IWICBitmapSource **ppIThumbnail)
195 {
196     TRACE("(%p,%p)\n", iface, ppIThumbnail);
197     return WINCODEC_ERR_CODECNOTHUMBNAIL;
198 }
199
200 static const IWICBitmapFrameDecodeVtbl IcoFrameDecode_Vtbl = {
201     IcoFrameDecode_QueryInterface,
202     IcoFrameDecode_AddRef,
203     IcoFrameDecode_Release,
204     IcoFrameDecode_GetSize,
205     IcoFrameDecode_GetPixelFormat,
206     IcoFrameDecode_GetResolution,
207     IcoFrameDecode_CopyPalette,
208     IcoFrameDecode_CopyPixels,
209     IcoFrameDecode_GetMetadataQueryReader,
210     IcoFrameDecode_GetColorContexts,
211     IcoFrameDecode_GetThumbnail
212 };
213
214 static inline void pixel_set_trans(DWORD* pixel, BOOL transparent)
215 {
216     if (transparent) *pixel = 0;
217     else *pixel |= 0xff000000;
218 }
219
220 static HRESULT ReadIcoDib(IStream *stream, IcoFrameDecode *result)
221 {
222     HRESULT hr;
223     IWICBitmapDecoder *decoder;
224     IWICBitmapFrameDecode *framedecode;
225     WICPixelFormatGUID pixelformat;
226     IWICBitmapSource *source;
227     int has_alpha=FALSE; /* if TRUE, alpha data might be in the image data */
228     WICRect rc;
229
230     hr = IcoDibDecoder_CreateInstance(NULL, &IID_IWICBitmapDecoder, (void**)&decoder);
231     if (SUCCEEDED(hr))
232     {
233         hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
234
235         if (SUCCEEDED(hr))
236             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
237
238         if (SUCCEEDED(hr))
239         {
240             hr = IWICBitmapFrameDecode_GetSize(framedecode, &result->width, &result->height);
241
242             if (SUCCEEDED(hr))
243             {
244                 result->bits = HeapAlloc(GetProcessHeap(), 0, result->width * result->height * 4);
245                 if (!result->bits) hr = E_OUTOFMEMORY;
246             }
247
248             if (SUCCEEDED(hr))
249                 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &pixelformat);
250
251             if (IsEqualGUID(&pixelformat, &GUID_WICPixelFormat32bppBGR) ||
252                 IsEqualGUID(&pixelformat, &GUID_WICPixelFormat32bppBGRA))
253             {
254                 source = (IWICBitmapSource*)framedecode;
255                 IWICBitmapSource_AddRef(source);
256                 has_alpha = TRUE;
257             }
258             else
259             {
260                 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA,
261                     (IWICBitmapSource*)framedecode, &source);
262                 has_alpha = FALSE;
263             }
264
265             if (SUCCEEDED(hr))
266             {
267                 rc.X = 0;
268                 rc.Y = 0;
269                 rc.Width = result->width;
270                 rc.Height = result->height;
271                 hr = IWICBitmapSource_CopyPixels(source, &rc, result->width * 4,
272                     result->width * result->height * 4, result->bits);
273
274                 IWICBitmapSource_Release(source);
275             }
276
277             IWICBitmapFrameDecode_Release(framedecode);
278         }
279
280         if (SUCCEEDED(hr) && !has_alpha)
281         {
282             /* set alpha data based on the AND mask */
283             UINT andBytesPerRow = (result->width+31)/32*4;
284             UINT andBytes = andBytesPerRow * result->height;
285             INT andStride;
286             BYTE *tempdata=NULL;
287             BYTE *andRow;
288             BYTE *bitsRow;
289             UINT bitsStride = result->width * 4;
290             UINT x, y;
291             ULONG offset;
292             ULONG bytesread;
293             LARGE_INTEGER seek;
294             int topdown;
295
296             BmpDecoder_FindIconMask(decoder, &offset, &topdown);
297
298             if (offset)
299             {
300                 seek.QuadPart = offset;
301
302                 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, 0);
303
304                 if (SUCCEEDED(hr))
305                 {
306                     tempdata = HeapAlloc(GetProcessHeap(), 0, andBytes);
307                     if (!tempdata) hr = E_OUTOFMEMORY;
308                 }
309
310                 if (SUCCEEDED(hr))
311                     hr = IStream_Read(stream, tempdata, andBytes, &bytesread);
312
313                 if (SUCCEEDED(hr) && bytesread == andBytes)
314                 {
315                     if (topdown)
316                     {
317                         andStride = andBytesPerRow;
318                         andRow = tempdata;
319                     }
320                     else
321                     {
322                         andStride = -andBytesPerRow;
323                         andRow = tempdata + (result->height-1)*andBytesPerRow;
324                     }
325
326                     bitsRow = result->bits;
327                     for (y=0; y<result->height; y++) {
328                         BYTE *andByte=andRow;
329                         DWORD *bitsPixel=(DWORD*)bitsRow;
330                         for (x=0; x<result->width; x+=8) {
331                             BYTE andVal=*andByte++;
332                             pixel_set_trans(bitsPixel++, andVal>>7&1);
333                             if (x+1 < result->width) pixel_set_trans(bitsPixel++, andVal>>6&1);
334                             if (x+2 < result->width) pixel_set_trans(bitsPixel++, andVal>>5&1);
335                             if (x+3 < result->width) pixel_set_trans(bitsPixel++, andVal>>4&1);
336                             if (x+4 < result->width) pixel_set_trans(bitsPixel++, andVal>>3&1);
337                             if (x+5 < result->width) pixel_set_trans(bitsPixel++, andVal>>2&1);
338                             if (x+6 < result->width) pixel_set_trans(bitsPixel++, andVal>>1&1);
339                             if (x+7 < result->width) pixel_set_trans(bitsPixel++, andVal&1);
340                         }
341                         andRow += andStride;
342                         bitsRow += bitsStride;
343                     }
344                 }
345
346                 HeapFree(GetProcessHeap(), 0, tempdata);
347             }
348         }
349
350         IWICBitmapDecoder_Release(decoder);
351     }
352
353     return hr;
354 }
355
356 static HRESULT ReadIcoPng(IStream *stream, IcoFrameDecode *result)
357 {
358     IWICBitmapDecoder *decoder = NULL;
359     IWICBitmapFrameDecode *sourceFrame = NULL;
360     IWICBitmapSource *sourceBitmap = NULL;
361     WICRect rect;
362     HRESULT hr;
363
364     hr = PngDecoder_CreateInstance(NULL, &IID_IWICBitmapDecoder, (void**)&decoder);
365     if (FAILED(hr))
366         goto end;
367     hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
368     if (FAILED(hr))
369         goto end;
370     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &sourceFrame);
371     if (FAILED(hr))
372         goto end;
373     hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)sourceFrame, &sourceBitmap);
374     if (FAILED(hr))
375         goto end;
376     hr = IWICBitmapFrameDecode_GetSize(sourceFrame, &result->width, &result->height);
377     if (FAILED(hr))
378         goto end;
379     result->bits = HeapAlloc(GetProcessHeap(), 0, 4 * result->width * result->height);
380     if (result->bits == NULL)
381     {
382         hr = E_OUTOFMEMORY;
383         goto end;
384     }
385     rect.X = 0;
386     rect.Y = 0;
387     rect.Width = result->width;
388     rect.Height = result->height;
389     hr = IWICBitmapSource_CopyPixels(sourceBitmap, &rect, 4*result->width,
390                                      4*result->width*result->height, result->bits);
391
392 end:
393     if (decoder != NULL)
394         IWICBitmapDecoder_Release(decoder);
395     if (sourceFrame != NULL)
396         IWICBitmapFrameDecode_Release(sourceFrame);
397     if (sourceBitmap != NULL)
398         IWICBitmapSource_Release(sourceBitmap);
399     return hr;
400 }
401
402 static HRESULT WINAPI IcoDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
403     void **ppv)
404 {
405     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
406     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
407
408     if (!ppv) return E_INVALIDARG;
409
410     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
411     {
412         *ppv = This;
413     }
414     else
415     {
416         *ppv = NULL;
417         return E_NOINTERFACE;
418     }
419
420     IUnknown_AddRef((IUnknown*)*ppv);
421     return S_OK;
422 }
423
424 static ULONG WINAPI IcoDecoder_AddRef(IWICBitmapDecoder *iface)
425 {
426     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
427     ULONG ref = InterlockedIncrement(&This->ref);
428
429     TRACE("(%p) refcount=%u\n", iface, ref);
430
431     return ref;
432 }
433
434 static ULONG WINAPI IcoDecoder_Release(IWICBitmapDecoder *iface)
435 {
436     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
437     ULONG ref = InterlockedDecrement(&This->ref);
438
439     TRACE("(%p) refcount=%u\n", iface, ref);
440
441     if (ref == 0)
442     {
443         This->lock.DebugInfo->Spare[0] = 0;
444         DeleteCriticalSection(&This->lock);
445         if (This->stream) IStream_Release(This->stream);
446         HeapFree(GetProcessHeap(), 0, This);
447     }
448
449     return ref;
450 }
451
452 static HRESULT WINAPI IcoDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
453     DWORD *pdwCapability)
454 {
455     FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
456     return E_NOTIMPL;
457 }
458
459 static HRESULT WINAPI IcoDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
460     WICDecodeOptions cacheOptions)
461 {
462     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
463     LARGE_INTEGER seek;
464     HRESULT hr;
465     ULONG bytesread;
466     TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
467
468     EnterCriticalSection(&This->lock);
469
470     if (This->initialized)
471     {
472         hr = WINCODEC_ERR_WRONGSTATE;
473         goto end;
474     }
475
476     seek.QuadPart = 0;
477     hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
478     if (FAILED(hr)) goto end;
479
480     hr = IStream_Read(pIStream, &This->header, sizeof(ICONHEADER), &bytesread);
481     if (FAILED(hr)) goto end;
482     if (bytesread != sizeof(ICONHEADER) ||
483         This->header.idReserved != 0 ||
484         This->header.idType != 1)
485     {
486         hr = E_FAIL;
487         goto end;
488     }
489
490     This->initialized = TRUE;
491     This->stream = pIStream;
492     IStream_AddRef(pIStream);
493
494 end:
495
496     LeaveCriticalSection(&This->lock);
497
498     return hr;
499 }
500
501 static HRESULT WINAPI IcoDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
502     GUID *pguidContainerFormat)
503 {
504     FIXME("(%p,%p): stub\n", iface, pguidContainerFormat);
505     return E_NOTIMPL;
506 }
507
508 static HRESULT WINAPI IcoDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
509     IWICBitmapDecoderInfo **ppIDecoderInfo)
510 {
511     FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
512     return E_NOTIMPL;
513 }
514
515 static HRESULT WINAPI IcoDecoder_CopyPalette(IWICBitmapDecoder *iface,
516     IWICPalette *pIPalette)
517 {
518     TRACE("(%p,%p)\n", iface, pIPalette);
519     return WINCODEC_ERR_PALETTEUNAVAILABLE;
520 }
521
522 static HRESULT WINAPI IcoDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
523     IWICMetadataQueryReader **ppIMetadataQueryReader)
524 {
525     TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
526     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
527 }
528
529 static HRESULT WINAPI IcoDecoder_GetPreview(IWICBitmapDecoder *iface,
530     IWICBitmapSource **ppIBitmapSource)
531 {
532     TRACE("(%p,%p)\n", iface, ppIBitmapSource);
533     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
534 }
535
536 static HRESULT WINAPI IcoDecoder_GetColorContexts(IWICBitmapDecoder *iface,
537     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
538 {
539     TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
540     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
541 }
542
543 static HRESULT WINAPI IcoDecoder_GetThumbnail(IWICBitmapDecoder *iface,
544     IWICBitmapSource **ppIThumbnail)
545 {
546     TRACE("(%p,%p)\n", iface, ppIThumbnail);
547     return WINCODEC_ERR_CODECNOTHUMBNAIL;
548 }
549
550 static HRESULT WINAPI IcoDecoder_GetFrameCount(IWICBitmapDecoder *iface,
551     UINT *pCount)
552 {
553     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
554     TRACE("(%p,%p)\n", iface, pCount);
555
556     if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
557
558     *pCount = This->header.idCount;
559     TRACE("<-- %u\n", *pCount);
560
561     return S_OK;
562 }
563
564 static HRESULT WINAPI IcoDecoder_GetFrame(IWICBitmapDecoder *iface,
565     UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
566 {
567     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
568     IcoFrameDecode *result=NULL;
569     LARGE_INTEGER seek;
570     ULARGE_INTEGER offset, length;
571     HRESULT hr;
572     ULONG bytesread;
573     ICONDIRENTRY entry;
574     IWICStream *substream=NULL;
575     DWORD magic;
576     TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
577
578     EnterCriticalSection(&This->lock);
579
580     if (!This->initialized)
581     {
582         hr = WINCODEC_ERR_NOTINITIALIZED;
583         goto fail;
584     }
585
586     if (This->header.idCount < index)
587     {
588         hr = E_INVALIDARG;
589         goto fail;
590     }
591
592     result = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoFrameDecode));
593     if (!result)
594     {
595         hr = E_OUTOFMEMORY;
596         goto fail;
597     }
598
599     result->IWICBitmapFrameDecode_iface.lpVtbl = &IcoFrameDecode_Vtbl;
600     result->ref = 1;
601     result->bits = NULL;
602
603     /* read the icon entry */
604     seek.QuadPart = sizeof(ICONHEADER) + sizeof(ICONDIRENTRY) * index;
605     hr = IStream_Seek(This->stream, seek, STREAM_SEEK_SET, 0);
606     if (FAILED(hr)) goto fail;
607
608     hr = IStream_Read(This->stream, &entry, sizeof(ICONDIRENTRY), &bytesread);
609     if (FAILED(hr) || bytesread != sizeof(ICONDIRENTRY)) goto fail;
610
611     /* create a stream object for this icon */
612     hr = StreamImpl_Create(&substream);
613     if (FAILED(hr)) goto fail;
614
615     offset.QuadPart = entry.dwDIBOffset;
616     length.QuadPart = entry.dwDIBSize;
617     hr = IWICStream_InitializeFromIStreamRegion(substream, This->stream, offset, length);
618     if (FAILED(hr)) goto fail;
619
620     /* read the bitmapinfo size or magic number */
621     hr = IWICStream_Read(substream, &magic, sizeof(magic), &bytesread);
622     if (FAILED(hr) || bytesread != sizeof(magic)) goto fail;
623
624     /* forward to the appropriate decoding function based on the magic number */
625     switch (magic)
626     {
627     case sizeof(BITMAPCOREHEADER):
628     case 64: /* sizeof(BITMAPCOREHEADER2) */
629     case sizeof(BITMAPINFOHEADER):
630     case sizeof(BITMAPV4HEADER):
631     case sizeof(BITMAPV5HEADER):
632         hr = ReadIcoDib((IStream*)substream, result);
633         break;
634     case 0x474e5089:
635         hr = ReadIcoPng((IStream*)substream, result);
636         break;
637     default:
638         FIXME("Unrecognized ICO frame magic: %x\n", magic);
639         hr = E_FAIL;
640         break;
641     }
642     if (FAILED(hr)) goto fail;
643
644     *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
645
646     LeaveCriticalSection(&This->lock);
647
648     return S_OK;
649
650 fail:
651     LeaveCriticalSection(&This->lock);
652     HeapFree(GetProcessHeap(), 0, result);
653     if (substream) IStream_Release(substream);
654     if (SUCCEEDED(hr)) hr = E_FAIL;
655     TRACE("<-- %x\n", hr);
656     return hr;
657 }
658
659 static const IWICBitmapDecoderVtbl IcoDecoder_Vtbl = {
660     IcoDecoder_QueryInterface,
661     IcoDecoder_AddRef,
662     IcoDecoder_Release,
663     IcoDecoder_QueryCapability,
664     IcoDecoder_Initialize,
665     IcoDecoder_GetContainerFormat,
666     IcoDecoder_GetDecoderInfo,
667     IcoDecoder_CopyPalette,
668     IcoDecoder_GetMetadataQueryReader,
669     IcoDecoder_GetPreview,
670     IcoDecoder_GetColorContexts,
671     IcoDecoder_GetThumbnail,
672     IcoDecoder_GetFrameCount,
673     IcoDecoder_GetFrame
674 };
675
676 HRESULT IcoDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
677 {
678     IcoDecoder *This;
679     HRESULT ret;
680
681     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
682
683     *ppv = NULL;
684
685     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
686
687     This = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoDecoder));
688     if (!This) return E_OUTOFMEMORY;
689
690     This->IWICBitmapDecoder_iface.lpVtbl = &IcoDecoder_Vtbl;
691     This->ref = 1;
692     This->stream = NULL;
693     This->initialized = FALSE;
694     InitializeCriticalSection(&This->lock);
695     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IcoDecoder.lock");
696
697     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
698     IUnknown_Release((IUnknown*)This);
699
700     return ret;
701 }