kernel32: Moved device-related stubs from psapi to kernel32.
[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     BmpDecoder *bmp_decoder;
224     IWICBitmapDecoder *decoder;
225     IWICBitmapFrameDecode *framedecode;
226     WICPixelFormatGUID pixelformat;
227     IWICBitmapSource *source;
228     int has_alpha=FALSE; /* if TRUE, alpha data might be in the image data */
229     WICRect rc;
230
231     hr = IcoDibDecoder_CreateInstance(&bmp_decoder);
232     if (SUCCEEDED(hr))
233     {
234         BmpDecoder_GetWICDecoder(bmp_decoder, &decoder);
235         hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
236
237         if (SUCCEEDED(hr))
238             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
239
240         if (SUCCEEDED(hr))
241         {
242             hr = IWICBitmapFrameDecode_GetSize(framedecode, &result->width, &result->height);
243
244             if (SUCCEEDED(hr))
245             {
246                 result->bits = HeapAlloc(GetProcessHeap(), 0, result->width * result->height * 4);
247                 if (!result->bits) hr = E_OUTOFMEMORY;
248             }
249
250             if (SUCCEEDED(hr))
251                 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &pixelformat);
252
253             if (IsEqualGUID(&pixelformat, &GUID_WICPixelFormat32bppBGR) ||
254                 IsEqualGUID(&pixelformat, &GUID_WICPixelFormat32bppBGRA))
255             {
256                 source = (IWICBitmapSource*)framedecode;
257                 IWICBitmapSource_AddRef(source);
258                 has_alpha = TRUE;
259             }
260             else
261             {
262                 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA,
263                     (IWICBitmapSource*)framedecode, &source);
264                 has_alpha = FALSE;
265             }
266
267             if (SUCCEEDED(hr))
268             {
269                 rc.X = 0;
270                 rc.Y = 0;
271                 rc.Width = result->width;
272                 rc.Height = result->height;
273                 hr = IWICBitmapSource_CopyPixels(source, &rc, result->width * 4,
274                     result->width * result->height * 4, result->bits);
275
276                 IWICBitmapSource_Release(source);
277             }
278
279             IWICBitmapFrameDecode_Release(framedecode);
280         }
281
282         if (SUCCEEDED(hr) && has_alpha)
283         {
284             /* If the alpha channel is fully transparent, we should ignore it. */
285             int nonzero_alpha = 0;
286             int i;
287
288             for (i=0; i<(result->height*result->width); i++)
289             {
290                 if (result->bits[i*4+3] != 0)
291                 {
292                     nonzero_alpha = 1;
293                     break;
294                 }
295             }
296
297             if (!nonzero_alpha)
298             {
299                 for (i=0; i<(result->height*result->width); i++)
300                     result->bits[i*4+3] = 0xff;
301
302                 has_alpha = FALSE;
303             }
304         }
305
306         if (SUCCEEDED(hr) && !has_alpha)
307         {
308             /* set alpha data based on the AND mask */
309             UINT andBytesPerRow = (result->width+31)/32*4;
310             UINT andBytes = andBytesPerRow * result->height;
311             INT andStride;
312             BYTE *tempdata=NULL;
313             BYTE *andRow;
314             BYTE *bitsRow;
315             UINT bitsStride = result->width * 4;
316             UINT x, y;
317             ULONG offset;
318             ULONG bytesread;
319             LARGE_INTEGER seek;
320             int topdown;
321
322             BmpDecoder_FindIconMask(bmp_decoder, &offset, &topdown);
323
324             if (offset)
325             {
326                 seek.QuadPart = offset;
327
328                 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, 0);
329
330                 if (SUCCEEDED(hr))
331                 {
332                     tempdata = HeapAlloc(GetProcessHeap(), 0, andBytes);
333                     if (!tempdata) hr = E_OUTOFMEMORY;
334                 }
335
336                 if (SUCCEEDED(hr))
337                     hr = IStream_Read(stream, tempdata, andBytes, &bytesread);
338
339                 if (SUCCEEDED(hr) && bytesread == andBytes)
340                 {
341                     if (topdown)
342                     {
343                         andStride = andBytesPerRow;
344                         andRow = tempdata;
345                     }
346                     else
347                     {
348                         andStride = -andBytesPerRow;
349                         andRow = tempdata + (result->height-1)*andBytesPerRow;
350                     }
351
352                     bitsRow = result->bits;
353                     for (y=0; y<result->height; y++) {
354                         BYTE *andByte=andRow;
355                         DWORD *bitsPixel=(DWORD*)bitsRow;
356                         for (x=0; x<result->width; x+=8) {
357                             BYTE andVal=*andByte++;
358                             pixel_set_trans(bitsPixel++, andVal>>7&1);
359                             if (x+1 < result->width) pixel_set_trans(bitsPixel++, andVal>>6&1);
360                             if (x+2 < result->width) pixel_set_trans(bitsPixel++, andVal>>5&1);
361                             if (x+3 < result->width) pixel_set_trans(bitsPixel++, andVal>>4&1);
362                             if (x+4 < result->width) pixel_set_trans(bitsPixel++, andVal>>3&1);
363                             if (x+5 < result->width) pixel_set_trans(bitsPixel++, andVal>>2&1);
364                             if (x+6 < result->width) pixel_set_trans(bitsPixel++, andVal>>1&1);
365                             if (x+7 < result->width) pixel_set_trans(bitsPixel++, andVal&1);
366                         }
367                         andRow += andStride;
368                         bitsRow += bitsStride;
369                     }
370                 }
371
372                 HeapFree(GetProcessHeap(), 0, tempdata);
373             }
374         }
375
376         IWICBitmapDecoder_Release(decoder);
377     }
378
379     return hr;
380 }
381
382 static HRESULT ReadIcoPng(IStream *stream, IcoFrameDecode *result)
383 {
384     IWICBitmapDecoder *decoder = NULL;
385     IWICBitmapFrameDecode *sourceFrame = NULL;
386     IWICBitmapSource *sourceBitmap = NULL;
387     WICRect rect;
388     HRESULT hr;
389
390     hr = PngDecoder_CreateInstance(NULL, &IID_IWICBitmapDecoder, (void**)&decoder);
391     if (FAILED(hr))
392         goto end;
393     hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
394     if (FAILED(hr))
395         goto end;
396     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &sourceFrame);
397     if (FAILED(hr))
398         goto end;
399     hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)sourceFrame, &sourceBitmap);
400     if (FAILED(hr))
401         goto end;
402     hr = IWICBitmapFrameDecode_GetSize(sourceFrame, &result->width, &result->height);
403     if (FAILED(hr))
404         goto end;
405     result->bits = HeapAlloc(GetProcessHeap(), 0, 4 * result->width * result->height);
406     if (result->bits == NULL)
407     {
408         hr = E_OUTOFMEMORY;
409         goto end;
410     }
411     rect.X = 0;
412     rect.Y = 0;
413     rect.Width = result->width;
414     rect.Height = result->height;
415     hr = IWICBitmapSource_CopyPixels(sourceBitmap, &rect, 4*result->width,
416                                      4*result->width*result->height, result->bits);
417
418 end:
419     if (decoder != NULL)
420         IWICBitmapDecoder_Release(decoder);
421     if (sourceFrame != NULL)
422         IWICBitmapFrameDecode_Release(sourceFrame);
423     if (sourceBitmap != NULL)
424         IWICBitmapSource_Release(sourceBitmap);
425     return hr;
426 }
427
428 static HRESULT WINAPI IcoDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
429     void **ppv)
430 {
431     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
432     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
433
434     if (!ppv) return E_INVALIDARG;
435
436     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
437     {
438         *ppv = This;
439     }
440     else
441     {
442         *ppv = NULL;
443         return E_NOINTERFACE;
444     }
445
446     IUnknown_AddRef((IUnknown*)*ppv);
447     return S_OK;
448 }
449
450 static ULONG WINAPI IcoDecoder_AddRef(IWICBitmapDecoder *iface)
451 {
452     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
453     ULONG ref = InterlockedIncrement(&This->ref);
454
455     TRACE("(%p) refcount=%u\n", iface, ref);
456
457     return ref;
458 }
459
460 static ULONG WINAPI IcoDecoder_Release(IWICBitmapDecoder *iface)
461 {
462     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
463     ULONG ref = InterlockedDecrement(&This->ref);
464
465     TRACE("(%p) refcount=%u\n", iface, ref);
466
467     if (ref == 0)
468     {
469         This->lock.DebugInfo->Spare[0] = 0;
470         DeleteCriticalSection(&This->lock);
471         if (This->stream) IStream_Release(This->stream);
472         HeapFree(GetProcessHeap(), 0, This);
473     }
474
475     return ref;
476 }
477
478 static HRESULT WINAPI IcoDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
479     DWORD *pdwCapability)
480 {
481     FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
482     return E_NOTIMPL;
483 }
484
485 static HRESULT WINAPI IcoDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
486     WICDecodeOptions cacheOptions)
487 {
488     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
489     LARGE_INTEGER seek;
490     HRESULT hr;
491     ULONG bytesread;
492     TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
493
494     EnterCriticalSection(&This->lock);
495
496     if (This->initialized)
497     {
498         hr = WINCODEC_ERR_WRONGSTATE;
499         goto end;
500     }
501
502     seek.QuadPart = 0;
503     hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
504     if (FAILED(hr)) goto end;
505
506     hr = IStream_Read(pIStream, &This->header, sizeof(ICONHEADER), &bytesread);
507     if (FAILED(hr)) goto end;
508     if (bytesread != sizeof(ICONHEADER) ||
509         This->header.idReserved != 0 ||
510         This->header.idType != 1)
511     {
512         hr = E_FAIL;
513         goto end;
514     }
515
516     This->initialized = TRUE;
517     This->stream = pIStream;
518     IStream_AddRef(pIStream);
519
520 end:
521
522     LeaveCriticalSection(&This->lock);
523
524     return hr;
525 }
526
527 static HRESULT WINAPI IcoDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
528     GUID *pguidContainerFormat)
529 {
530     FIXME("(%p,%p): stub\n", iface, pguidContainerFormat);
531     return E_NOTIMPL;
532 }
533
534 static HRESULT WINAPI IcoDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
535     IWICBitmapDecoderInfo **ppIDecoderInfo)
536 {
537     FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
538     return E_NOTIMPL;
539 }
540
541 static HRESULT WINAPI IcoDecoder_CopyPalette(IWICBitmapDecoder *iface,
542     IWICPalette *pIPalette)
543 {
544     TRACE("(%p,%p)\n", iface, pIPalette);
545     return WINCODEC_ERR_PALETTEUNAVAILABLE;
546 }
547
548 static HRESULT WINAPI IcoDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
549     IWICMetadataQueryReader **ppIMetadataQueryReader)
550 {
551     TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
552     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
553 }
554
555 static HRESULT WINAPI IcoDecoder_GetPreview(IWICBitmapDecoder *iface,
556     IWICBitmapSource **ppIBitmapSource)
557 {
558     TRACE("(%p,%p)\n", iface, ppIBitmapSource);
559     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
560 }
561
562 static HRESULT WINAPI IcoDecoder_GetColorContexts(IWICBitmapDecoder *iface,
563     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
564 {
565     TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
566     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
567 }
568
569 static HRESULT WINAPI IcoDecoder_GetThumbnail(IWICBitmapDecoder *iface,
570     IWICBitmapSource **ppIThumbnail)
571 {
572     TRACE("(%p,%p)\n", iface, ppIThumbnail);
573     return WINCODEC_ERR_CODECNOTHUMBNAIL;
574 }
575
576 static HRESULT WINAPI IcoDecoder_GetFrameCount(IWICBitmapDecoder *iface,
577     UINT *pCount)
578 {
579     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
580     TRACE("(%p,%p)\n", iface, pCount);
581
582     if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
583
584     *pCount = This->header.idCount;
585     TRACE("<-- %u\n", *pCount);
586
587     return S_OK;
588 }
589
590 static HRESULT WINAPI IcoDecoder_GetFrame(IWICBitmapDecoder *iface,
591     UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
592 {
593     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
594     IcoFrameDecode *result=NULL;
595     LARGE_INTEGER seek;
596     ULARGE_INTEGER offset, length;
597     HRESULT hr;
598     ULONG bytesread;
599     ICONDIRENTRY entry;
600     IWICStream *substream=NULL;
601     DWORD magic;
602     TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
603
604     EnterCriticalSection(&This->lock);
605
606     if (!This->initialized)
607     {
608         hr = WINCODEC_ERR_NOTINITIALIZED;
609         goto fail;
610     }
611
612     if (This->header.idCount < index)
613     {
614         hr = E_INVALIDARG;
615         goto fail;
616     }
617
618     result = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoFrameDecode));
619     if (!result)
620     {
621         hr = E_OUTOFMEMORY;
622         goto fail;
623     }
624
625     result->IWICBitmapFrameDecode_iface.lpVtbl = &IcoFrameDecode_Vtbl;
626     result->ref = 1;
627     result->bits = NULL;
628
629     /* read the icon entry */
630     seek.QuadPart = sizeof(ICONHEADER) + sizeof(ICONDIRENTRY) * index;
631     hr = IStream_Seek(This->stream, seek, STREAM_SEEK_SET, 0);
632     if (FAILED(hr)) goto fail;
633
634     hr = IStream_Read(This->stream, &entry, sizeof(ICONDIRENTRY), &bytesread);
635     if (FAILED(hr) || bytesread != sizeof(ICONDIRENTRY)) goto fail;
636
637     /* create a stream object for this icon */
638     hr = StreamImpl_Create(&substream);
639     if (FAILED(hr)) goto fail;
640
641     offset.QuadPart = entry.dwDIBOffset;
642     length.QuadPart = entry.dwDIBSize;
643     hr = IWICStream_InitializeFromIStreamRegion(substream, This->stream, offset, length);
644     if (FAILED(hr)) goto fail;
645
646     /* read the bitmapinfo size or magic number */
647     hr = IWICStream_Read(substream, &magic, sizeof(magic), &bytesread);
648     if (FAILED(hr) || bytesread != sizeof(magic)) goto fail;
649
650     /* forward to the appropriate decoding function based on the magic number */
651     switch (magic)
652     {
653     case sizeof(BITMAPCOREHEADER):
654     case 64: /* sizeof(BITMAPCOREHEADER2) */
655     case sizeof(BITMAPINFOHEADER):
656     case sizeof(BITMAPV4HEADER):
657     case sizeof(BITMAPV5HEADER):
658         hr = ReadIcoDib((IStream*)substream, result);
659         break;
660     case 0x474e5089:
661         hr = ReadIcoPng((IStream*)substream, result);
662         break;
663     default:
664         FIXME("Unrecognized ICO frame magic: %x\n", magic);
665         hr = E_FAIL;
666         break;
667     }
668     if (FAILED(hr)) goto fail;
669
670     *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
671
672     LeaveCriticalSection(&This->lock);
673
674     IStream_Release(substream);
675
676     return S_OK;
677
678 fail:
679     LeaveCriticalSection(&This->lock);
680     HeapFree(GetProcessHeap(), 0, result);
681     if (substream) IStream_Release(substream);
682     if (SUCCEEDED(hr)) hr = E_FAIL;
683     TRACE("<-- %x\n", hr);
684     return hr;
685 }
686
687 static const IWICBitmapDecoderVtbl IcoDecoder_Vtbl = {
688     IcoDecoder_QueryInterface,
689     IcoDecoder_AddRef,
690     IcoDecoder_Release,
691     IcoDecoder_QueryCapability,
692     IcoDecoder_Initialize,
693     IcoDecoder_GetContainerFormat,
694     IcoDecoder_GetDecoderInfo,
695     IcoDecoder_CopyPalette,
696     IcoDecoder_GetMetadataQueryReader,
697     IcoDecoder_GetPreview,
698     IcoDecoder_GetColorContexts,
699     IcoDecoder_GetThumbnail,
700     IcoDecoder_GetFrameCount,
701     IcoDecoder_GetFrame
702 };
703
704 HRESULT IcoDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
705 {
706     IcoDecoder *This;
707     HRESULT ret;
708
709     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
710
711     *ppv = NULL;
712
713     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
714
715     This = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoDecoder));
716     if (!This) return E_OUTOFMEMORY;
717
718     This->IWICBitmapDecoder_iface.lpVtbl = &IcoDecoder_Vtbl;
719     This->ref = 1;
720     This->stream = NULL;
721     This->initialized = FALSE;
722     InitializeCriticalSection(&This->lock);
723     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IcoDecoder.lock");
724
725     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
726     IUnknown_Release((IUnknown*)This);
727
728     return ret;
729 }