Always define WIDL_C_INLINE_WRAPPERS.
[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     double dpiX, dpiY;
73     BYTE *bits;
74 } IcoFrameDecode;
75
76 static inline IcoDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
77 {
78     return CONTAINING_RECORD(iface, IcoDecoder, IWICBitmapDecoder_iface);
79 }
80
81 static inline IcoFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
82 {
83     return CONTAINING_RECORD(iface, IcoFrameDecode, IWICBitmapFrameDecode_iface);
84 }
85
86 static HRESULT WINAPI IcoFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
87     void **ppv)
88 {
89     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
90     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
91
92     if (!ppv) return E_INVALIDARG;
93
94     if (IsEqualIID(&IID_IUnknown, iid) ||
95         IsEqualIID(&IID_IWICBitmapSource, iid) ||
96         IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
97     {
98         *ppv = &This->IWICBitmapFrameDecode_iface;
99     }
100     else
101     {
102         *ppv = NULL;
103         return E_NOINTERFACE;
104     }
105
106     IUnknown_AddRef((IUnknown*)*ppv);
107     return S_OK;
108 }
109
110 static ULONG WINAPI IcoFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
111 {
112     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
113     ULONG ref = InterlockedIncrement(&This->ref);
114
115     TRACE("(%p) refcount=%u\n", iface, ref);
116
117     return ref;
118 }
119
120 static ULONG WINAPI IcoFrameDecode_Release(IWICBitmapFrameDecode *iface)
121 {
122     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
123     ULONG ref = InterlockedDecrement(&This->ref);
124
125     TRACE("(%p) refcount=%u\n", iface, ref);
126
127     if (ref == 0)
128     {
129         HeapFree(GetProcessHeap(), 0, This->bits);
130         HeapFree(GetProcessHeap(), 0, This);
131     }
132
133     return ref;
134 }
135
136 static HRESULT WINAPI IcoFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
137     UINT *puiWidth, UINT *puiHeight)
138 {
139     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
140
141     *puiWidth = This->width;
142     *puiHeight = This->height;
143
144     TRACE("(%p) -> (%i,%i)\n", iface, *puiWidth, *puiHeight);
145
146     return S_OK;
147 }
148
149 static HRESULT WINAPI IcoFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
150     WICPixelFormatGUID *pPixelFormat)
151 {
152     memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
153     return S_OK;
154 }
155
156 static HRESULT WINAPI IcoFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
157     double *pDpiX, double *pDpiY)
158 {
159     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
160
161     *pDpiX = This->dpiX;
162     *pDpiY = This->dpiY;
163
164     TRACE("(%p) -> (%f,%f)\n", iface, *pDpiX, *pDpiY);
165
166     return S_OK;
167 }
168
169 static HRESULT WINAPI IcoFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
170     IWICPalette *pIPalette)
171 {
172     TRACE("(%p,%p)\n", iface, pIPalette);
173     return WINCODEC_ERR_PALETTEUNAVAILABLE;
174 }
175
176 static HRESULT WINAPI IcoFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
177     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
178 {
179     IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
180     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
181
182     return copy_pixels(32, This->bits, This->width, This->height, This->width * 4,
183         prc, cbStride, cbBufferSize, pbBuffer);
184 }
185
186 static HRESULT WINAPI IcoFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
187     IWICMetadataQueryReader **ppIMetadataQueryReader)
188 {
189     TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
190     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
191 }
192
193 static HRESULT WINAPI IcoFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
194     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
195 {
196     TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
197     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
198 }
199
200 static HRESULT WINAPI IcoFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
201     IWICBitmapSource **ppIThumbnail)
202 {
203     TRACE("(%p,%p)\n", iface, ppIThumbnail);
204     return WINCODEC_ERR_CODECNOTHUMBNAIL;
205 }
206
207 static const IWICBitmapFrameDecodeVtbl IcoFrameDecode_Vtbl = {
208     IcoFrameDecode_QueryInterface,
209     IcoFrameDecode_AddRef,
210     IcoFrameDecode_Release,
211     IcoFrameDecode_GetSize,
212     IcoFrameDecode_GetPixelFormat,
213     IcoFrameDecode_GetResolution,
214     IcoFrameDecode_CopyPalette,
215     IcoFrameDecode_CopyPixels,
216     IcoFrameDecode_GetMetadataQueryReader,
217     IcoFrameDecode_GetColorContexts,
218     IcoFrameDecode_GetThumbnail
219 };
220
221 static inline void pixel_set_trans(DWORD* pixel, BOOL transparent)
222 {
223     if (transparent) *pixel = 0;
224     else *pixel |= 0xff000000;
225 }
226
227 static HRESULT ReadIcoDib(IStream *stream, IcoFrameDecode *result)
228 {
229     HRESULT hr;
230     BmpDecoder *bmp_decoder;
231     IWICBitmapDecoder *decoder;
232     IWICBitmapFrameDecode *framedecode;
233     WICPixelFormatGUID pixelformat;
234     IWICBitmapSource *source;
235     int has_alpha=FALSE; /* if TRUE, alpha data might be in the image data */
236     WICRect rc;
237
238     hr = IcoDibDecoder_CreateInstance(&bmp_decoder);
239     if (SUCCEEDED(hr))
240     {
241         BmpDecoder_GetWICDecoder(bmp_decoder, &decoder);
242         hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
243
244         if (SUCCEEDED(hr))
245             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
246
247         if (SUCCEEDED(hr))
248         {
249             hr = IWICBitmapFrameDecode_GetSize(framedecode, &result->width, &result->height);
250
251             if (SUCCEEDED(hr))
252             {
253                 result->bits = HeapAlloc(GetProcessHeap(), 0, result->width * result->height * 4);
254                 if (!result->bits) hr = E_OUTOFMEMORY;
255             }
256
257             if (SUCCEEDED(hr))
258                 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &pixelformat);
259
260             if (IsEqualGUID(&pixelformat, &GUID_WICPixelFormat32bppBGR) ||
261                 IsEqualGUID(&pixelformat, &GUID_WICPixelFormat32bppBGRA))
262             {
263                 source = (IWICBitmapSource*)framedecode;
264                 IWICBitmapSource_AddRef(source);
265                 has_alpha = TRUE;
266             }
267             else
268             {
269                 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA,
270                     (IWICBitmapSource*)framedecode, &source);
271                 has_alpha = FALSE;
272             }
273
274             if (SUCCEEDED(hr))
275             {
276                 rc.X = 0;
277                 rc.Y = 0;
278                 rc.Width = result->width;
279                 rc.Height = result->height;
280                 hr = IWICBitmapSource_CopyPixels(source, &rc, result->width * 4,
281                     result->width * result->height * 4, result->bits);
282
283                 IWICBitmapSource_Release(source);
284             }
285
286             if (SUCCEEDED(hr))
287                 hr = IWICBitmapFrameDecode_GetResolution(framedecode, &result->dpiX, &result->dpiY);
288
289             IWICBitmapFrameDecode_Release(framedecode);
290         }
291
292         if (SUCCEEDED(hr) && has_alpha)
293         {
294             /* If the alpha channel is fully transparent, we should ignore it. */
295             int nonzero_alpha = 0;
296             int i;
297
298             for (i=0; i<(result->height*result->width); i++)
299             {
300                 if (result->bits[i*4+3] != 0)
301                 {
302                     nonzero_alpha = 1;
303                     break;
304                 }
305             }
306
307             if (!nonzero_alpha)
308             {
309                 for (i=0; i<(result->height*result->width); i++)
310                     result->bits[i*4+3] = 0xff;
311
312                 has_alpha = FALSE;
313             }
314         }
315
316         if (SUCCEEDED(hr) && !has_alpha)
317         {
318             /* set alpha data based on the AND mask */
319             UINT andBytesPerRow = (result->width+31)/32*4;
320             UINT andBytes = andBytesPerRow * result->height;
321             INT andStride;
322             BYTE *tempdata=NULL;
323             BYTE *andRow;
324             BYTE *bitsRow;
325             UINT bitsStride = result->width * 4;
326             UINT x, y;
327             ULONG offset;
328             ULONG bytesread;
329             LARGE_INTEGER seek;
330             int topdown;
331
332             BmpDecoder_FindIconMask(bmp_decoder, &offset, &topdown);
333
334             if (offset)
335             {
336                 seek.QuadPart = offset;
337
338                 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, 0);
339
340                 if (SUCCEEDED(hr))
341                 {
342                     tempdata = HeapAlloc(GetProcessHeap(), 0, andBytes);
343                     if (!tempdata) hr = E_OUTOFMEMORY;
344                 }
345
346                 if (SUCCEEDED(hr))
347                     hr = IStream_Read(stream, tempdata, andBytes, &bytesread);
348
349                 if (SUCCEEDED(hr) && bytesread == andBytes)
350                 {
351                     if (topdown)
352                     {
353                         andStride = andBytesPerRow;
354                         andRow = tempdata;
355                     }
356                     else
357                     {
358                         andStride = -andBytesPerRow;
359                         andRow = tempdata + (result->height-1)*andBytesPerRow;
360                     }
361
362                     bitsRow = result->bits;
363                     for (y=0; y<result->height; y++) {
364                         BYTE *andByte=andRow;
365                         DWORD *bitsPixel=(DWORD*)bitsRow;
366                         for (x=0; x<result->width; x+=8) {
367                             BYTE andVal=*andByte++;
368                             pixel_set_trans(bitsPixel++, andVal>>7&1);
369                             if (x+1 < result->width) pixel_set_trans(bitsPixel++, andVal>>6&1);
370                             if (x+2 < result->width) pixel_set_trans(bitsPixel++, andVal>>5&1);
371                             if (x+3 < result->width) pixel_set_trans(bitsPixel++, andVal>>4&1);
372                             if (x+4 < result->width) pixel_set_trans(bitsPixel++, andVal>>3&1);
373                             if (x+5 < result->width) pixel_set_trans(bitsPixel++, andVal>>2&1);
374                             if (x+6 < result->width) pixel_set_trans(bitsPixel++, andVal>>1&1);
375                             if (x+7 < result->width) pixel_set_trans(bitsPixel++, andVal&1);
376                         }
377                         andRow += andStride;
378                         bitsRow += bitsStride;
379                     }
380                 }
381
382                 HeapFree(GetProcessHeap(), 0, tempdata);
383             }
384         }
385
386         IWICBitmapDecoder_Release(decoder);
387     }
388
389     return hr;
390 }
391
392 static HRESULT ReadIcoPng(IStream *stream, IcoFrameDecode *result)
393 {
394     IWICBitmapDecoder *decoder = NULL;
395     IWICBitmapFrameDecode *sourceFrame = NULL;
396     IWICBitmapSource *sourceBitmap = NULL;
397     WICRect rect;
398     HRESULT hr;
399
400     hr = PngDecoder_CreateInstance(NULL, &IID_IWICBitmapDecoder, (void**)&decoder);
401     if (FAILED(hr))
402         goto end;
403     hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
404     if (FAILED(hr))
405         goto end;
406     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &sourceFrame);
407     if (FAILED(hr))
408         goto end;
409     hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)sourceFrame, &sourceBitmap);
410     if (FAILED(hr))
411         goto end;
412     hr = IWICBitmapFrameDecode_GetSize(sourceFrame, &result->width, &result->height);
413     if (FAILED(hr))
414         goto end;
415     hr = IWICBitmapFrameDecode_GetResolution(sourceFrame, &result->dpiX, &result->dpiY);
416     if (FAILED(hr))
417         goto end;
418     result->bits = HeapAlloc(GetProcessHeap(), 0, 4 * result->width * result->height);
419     if (result->bits == NULL)
420     {
421         hr = E_OUTOFMEMORY;
422         goto end;
423     }
424     rect.X = 0;
425     rect.Y = 0;
426     rect.Width = result->width;
427     rect.Height = result->height;
428     hr = IWICBitmapSource_CopyPixels(sourceBitmap, &rect, 4*result->width,
429                                      4*result->width*result->height, result->bits);
430
431 end:
432     if (decoder != NULL)
433         IWICBitmapDecoder_Release(decoder);
434     if (sourceFrame != NULL)
435         IWICBitmapFrameDecode_Release(sourceFrame);
436     if (sourceBitmap != NULL)
437         IWICBitmapSource_Release(sourceBitmap);
438     return hr;
439 }
440
441 static HRESULT WINAPI IcoDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
442     void **ppv)
443 {
444     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
445     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
446
447     if (!ppv) return E_INVALIDARG;
448
449     if (IsEqualIID(&IID_IUnknown, iid) ||
450         IsEqualIID(&IID_IWICBitmapDecoder, iid))
451     {
452         *ppv = &This->IWICBitmapDecoder_iface;
453     }
454     else
455     {
456         *ppv = NULL;
457         return E_NOINTERFACE;
458     }
459
460     IUnknown_AddRef((IUnknown*)*ppv);
461     return S_OK;
462 }
463
464 static ULONG WINAPI IcoDecoder_AddRef(IWICBitmapDecoder *iface)
465 {
466     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
467     ULONG ref = InterlockedIncrement(&This->ref);
468
469     TRACE("(%p) refcount=%u\n", iface, ref);
470
471     return ref;
472 }
473
474 static ULONG WINAPI IcoDecoder_Release(IWICBitmapDecoder *iface)
475 {
476     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
477     ULONG ref = InterlockedDecrement(&This->ref);
478
479     TRACE("(%p) refcount=%u\n", iface, ref);
480
481     if (ref == 0)
482     {
483         This->lock.DebugInfo->Spare[0] = 0;
484         DeleteCriticalSection(&This->lock);
485         if (This->stream) IStream_Release(This->stream);
486         HeapFree(GetProcessHeap(), 0, This);
487     }
488
489     return ref;
490 }
491
492 static HRESULT WINAPI IcoDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
493     DWORD *pdwCapability)
494 {
495     FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
496     return E_NOTIMPL;
497 }
498
499 static HRESULT WINAPI IcoDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
500     WICDecodeOptions cacheOptions)
501 {
502     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
503     LARGE_INTEGER seek;
504     HRESULT hr;
505     ULONG bytesread;
506     TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
507
508     EnterCriticalSection(&This->lock);
509
510     if (This->initialized)
511     {
512         hr = WINCODEC_ERR_WRONGSTATE;
513         goto end;
514     }
515
516     seek.QuadPart = 0;
517     hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
518     if (FAILED(hr)) goto end;
519
520     hr = IStream_Read(pIStream, &This->header, sizeof(ICONHEADER), &bytesread);
521     if (FAILED(hr)) goto end;
522     if (bytesread != sizeof(ICONHEADER) ||
523         This->header.idReserved != 0 ||
524         This->header.idType != 1)
525     {
526         hr = E_FAIL;
527         goto end;
528     }
529
530     This->initialized = TRUE;
531     This->stream = pIStream;
532     IStream_AddRef(pIStream);
533
534 end:
535
536     LeaveCriticalSection(&This->lock);
537
538     return hr;
539 }
540
541 static HRESULT WINAPI IcoDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
542     GUID *pguidContainerFormat)
543 {
544     memcpy(pguidContainerFormat, &GUID_ContainerFormatIco, sizeof(GUID));
545     return S_OK;
546 }
547
548 static HRESULT WINAPI IcoDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
549     IWICBitmapDecoderInfo **ppIDecoderInfo)
550 {
551     HRESULT hr;
552     IWICComponentInfo *compinfo;
553
554     TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
555
556     hr = CreateComponentInfo(&CLSID_WICIcoDecoder, &compinfo);
557     if (FAILED(hr)) return hr;
558
559     hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
560         (void**)ppIDecoderInfo);
561
562     IWICComponentInfo_Release(compinfo);
563
564     return hr;
565 }
566
567 static HRESULT WINAPI IcoDecoder_CopyPalette(IWICBitmapDecoder *iface,
568     IWICPalette *pIPalette)
569 {
570     TRACE("(%p,%p)\n", iface, pIPalette);
571     return WINCODEC_ERR_PALETTEUNAVAILABLE;
572 }
573
574 static HRESULT WINAPI IcoDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
575     IWICMetadataQueryReader **ppIMetadataQueryReader)
576 {
577     TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
578     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
579 }
580
581 static HRESULT WINAPI IcoDecoder_GetPreview(IWICBitmapDecoder *iface,
582     IWICBitmapSource **ppIBitmapSource)
583 {
584     TRACE("(%p,%p)\n", iface, ppIBitmapSource);
585     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
586 }
587
588 static HRESULT WINAPI IcoDecoder_GetColorContexts(IWICBitmapDecoder *iface,
589     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
590 {
591     TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
592     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
593 }
594
595 static HRESULT WINAPI IcoDecoder_GetThumbnail(IWICBitmapDecoder *iface,
596     IWICBitmapSource **ppIThumbnail)
597 {
598     TRACE("(%p,%p)\n", iface, ppIThumbnail);
599     return WINCODEC_ERR_CODECNOTHUMBNAIL;
600 }
601
602 static HRESULT WINAPI IcoDecoder_GetFrameCount(IWICBitmapDecoder *iface,
603     UINT *pCount)
604 {
605     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
606     TRACE("(%p,%p)\n", iface, pCount);
607
608     if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
609
610     *pCount = This->header.idCount;
611     TRACE("<-- %u\n", *pCount);
612
613     return S_OK;
614 }
615
616 static HRESULT WINAPI IcoDecoder_GetFrame(IWICBitmapDecoder *iface,
617     UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
618 {
619     IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
620     IcoFrameDecode *result=NULL;
621     LARGE_INTEGER seek;
622     ULARGE_INTEGER offset, length;
623     HRESULT hr;
624     ULONG bytesread;
625     ICONDIRENTRY entry;
626     IWICStream *substream=NULL;
627     DWORD magic;
628     TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
629
630     EnterCriticalSection(&This->lock);
631
632     if (!This->initialized)
633     {
634         hr = WINCODEC_ERR_NOTINITIALIZED;
635         goto fail;
636     }
637
638     if (This->header.idCount < index)
639     {
640         hr = E_INVALIDARG;
641         goto fail;
642     }
643
644     result = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoFrameDecode));
645     if (!result)
646     {
647         hr = E_OUTOFMEMORY;
648         goto fail;
649     }
650
651     result->IWICBitmapFrameDecode_iface.lpVtbl = &IcoFrameDecode_Vtbl;
652     result->ref = 1;
653     result->bits = NULL;
654
655     /* read the icon entry */
656     seek.QuadPart = sizeof(ICONHEADER) + sizeof(ICONDIRENTRY) * index;
657     hr = IStream_Seek(This->stream, seek, STREAM_SEEK_SET, 0);
658     if (FAILED(hr)) goto fail;
659
660     hr = IStream_Read(This->stream, &entry, sizeof(ICONDIRENTRY), &bytesread);
661     if (FAILED(hr) || bytesread != sizeof(ICONDIRENTRY)) goto fail;
662
663     /* create a stream object for this icon */
664     hr = StreamImpl_Create(&substream);
665     if (FAILED(hr)) goto fail;
666
667     offset.QuadPart = entry.dwDIBOffset;
668     length.QuadPart = entry.dwDIBSize;
669     hr = IWICStream_InitializeFromIStreamRegion(substream, This->stream, offset, length);
670     if (FAILED(hr)) goto fail;
671
672     /* read the bitmapinfo size or magic number */
673     hr = IWICStream_Read(substream, &magic, sizeof(magic), &bytesread);
674     if (FAILED(hr) || bytesread != sizeof(magic)) goto fail;
675
676     /* forward to the appropriate decoding function based on the magic number */
677     switch (magic)
678     {
679     case sizeof(BITMAPCOREHEADER):
680     case 64: /* sizeof(BITMAPCOREHEADER2) */
681     case sizeof(BITMAPINFOHEADER):
682     case sizeof(BITMAPV4HEADER):
683     case sizeof(BITMAPV5HEADER):
684         hr = ReadIcoDib((IStream*)substream, result);
685         break;
686     case 0x474e5089:
687         hr = ReadIcoPng((IStream*)substream, result);
688         break;
689     default:
690         FIXME("Unrecognized ICO frame magic: %x\n", magic);
691         hr = E_FAIL;
692         break;
693     }
694     if (FAILED(hr)) goto fail;
695
696     *ppIBitmapFrame = &result->IWICBitmapFrameDecode_iface;
697
698     LeaveCriticalSection(&This->lock);
699
700     IWICStream_Release(substream);
701
702     return S_OK;
703
704 fail:
705     LeaveCriticalSection(&This->lock);
706     HeapFree(GetProcessHeap(), 0, result);
707     if (substream) IWICStream_Release(substream);
708     if (SUCCEEDED(hr)) hr = E_FAIL;
709     TRACE("<-- %x\n", hr);
710     return hr;
711 }
712
713 static const IWICBitmapDecoderVtbl IcoDecoder_Vtbl = {
714     IcoDecoder_QueryInterface,
715     IcoDecoder_AddRef,
716     IcoDecoder_Release,
717     IcoDecoder_QueryCapability,
718     IcoDecoder_Initialize,
719     IcoDecoder_GetContainerFormat,
720     IcoDecoder_GetDecoderInfo,
721     IcoDecoder_CopyPalette,
722     IcoDecoder_GetMetadataQueryReader,
723     IcoDecoder_GetPreview,
724     IcoDecoder_GetColorContexts,
725     IcoDecoder_GetThumbnail,
726     IcoDecoder_GetFrameCount,
727     IcoDecoder_GetFrame
728 };
729
730 HRESULT IcoDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
731 {
732     IcoDecoder *This;
733     HRESULT ret;
734
735     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
736
737     *ppv = NULL;
738
739     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
740
741     This = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoDecoder));
742     if (!This) return E_OUTOFMEMORY;
743
744     This->IWICBitmapDecoder_iface.lpVtbl = &IcoDecoder_Vtbl;
745     This->ref = 1;
746     This->stream = NULL;
747     This->initialized = FALSE;
748     InitializeCriticalSection(&This->lock);
749     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IcoDecoder.lock");
750
751     ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
752     IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
753
754     return ret;
755 }