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