mmdevapi: Fix small capture bugs.
[wine/multimedia.git] / dlls / windowscodecs / icoformat.c
blobe1ea7c6c8d63567c592d99bb9790ce06d04f388c
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
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.
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.
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
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "objbase.h"
29 #include "wincodec.h"
31 #include "wincodecs_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
37 #include "pshpack1.h"
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;
50 typedef struct
52 WORD idReserved;
53 WORD idType;
54 WORD idCount;
55 } ICONHEADER;
57 #include "poppack.h"
59 typedef struct {
60 const IWICBitmapDecoderVtbl *lpVtbl;
61 LONG ref;
62 BOOL initialized;
63 IStream *stream;
64 ICONHEADER header;
65 CRITICAL_SECTION lock; /* must be held when accessing stream */
66 } IcoDecoder;
68 typedef struct {
69 const IWICBitmapFrameDecodeVtbl *lpVtbl;
70 LONG ref;
71 ICONDIRENTRY entry;
72 IcoDecoder *parent;
73 BYTE *bits;
74 } IcoFrameDecode;
76 static HRESULT WINAPI IcoFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
77 void **ppv)
79 IcoFrameDecode *This = (IcoFrameDecode*)iface;
80 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
82 if (!ppv) return E_INVALIDARG;
84 if (IsEqualIID(&IID_IUnknown, iid) ||
85 IsEqualIID(&IID_IWICBitmapSource, iid) ||
86 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
88 *ppv = This;
90 else
92 *ppv = NULL;
93 return E_NOINTERFACE;
96 IUnknown_AddRef((IUnknown*)*ppv);
97 return S_OK;
100 static ULONG WINAPI IcoFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
102 IcoFrameDecode *This = (IcoFrameDecode*)iface;
103 ULONG ref = InterlockedIncrement(&This->ref);
105 TRACE("(%p) refcount=%u\n", iface, ref);
107 return ref;
110 static ULONG WINAPI IcoFrameDecode_Release(IWICBitmapFrameDecode *iface)
112 IcoFrameDecode *This = (IcoFrameDecode*)iface;
113 ULONG ref = InterlockedDecrement(&This->ref);
115 TRACE("(%p) refcount=%u\n", iface, ref);
117 if (ref == 0)
119 IUnknown_Release((IUnknown*)This->parent);
120 HeapFree(GetProcessHeap(), 0, This->bits);
121 HeapFree(GetProcessHeap(), 0, This);
124 return ref;
127 static HRESULT WINAPI IcoFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
128 UINT *puiWidth, UINT *puiHeight)
130 IcoFrameDecode *This = (IcoFrameDecode*)iface;
132 *puiWidth = This->entry.bWidth ? This->entry.bWidth : 256;
133 *puiHeight = This->entry.bHeight ? This->entry.bHeight : 256;
135 TRACE("(%p) -> (%i,%i)\n", iface, *puiWidth, *puiHeight);
137 return S_OK;
140 static HRESULT WINAPI IcoFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
141 WICPixelFormatGUID *pPixelFormat)
143 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
144 return S_OK;
147 static HRESULT WINAPI IcoFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
148 double *pDpiX, double *pDpiY)
150 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
151 return E_NOTIMPL;
154 static HRESULT WINAPI IcoFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
155 IWICPalette *pIPalette)
157 TRACE("(%p,%p)\n", iface, pIPalette);
158 return WINCODEC_ERR_PALETTEUNAVAILABLE;
161 static inline void pixel_set_trans(DWORD* pixel, BOOL transparent)
163 if (transparent) *pixel = 0;
164 else *pixel |= 0xff000000;
167 static HRESULT IcoFrameDecode_ReadPixels(IcoFrameDecode *This)
169 BITMAPINFOHEADER bih;
170 DWORD colors[256];
171 UINT colorcount=0;
172 LARGE_INTEGER seek;
173 ULONG bytesread;
174 HRESULT hr;
175 BYTE *tempdata = NULL;
176 BYTE *bits = NULL;
177 UINT bitsStride;
178 UINT bitsSize;
179 UINT width, height;
181 width = This->entry.bWidth ? This->entry.bWidth : 256;
182 height = This->entry.bHeight ? This->entry.bHeight : 256;
184 /* read the BITMAPINFOHEADER */
185 seek.QuadPart = This->entry.dwDIBOffset;
186 hr = IStream_Seek(This->parent->stream, seek, STREAM_SEEK_SET, NULL);
187 if (FAILED(hr)) goto fail;
189 hr = IStream_Read(This->parent->stream, &bih, sizeof(BITMAPINFOHEADER), &bytesread);
190 if (FAILED(hr) || bytesread != sizeof(BITMAPINFOHEADER)) goto fail;
192 if (This->entry.wBitCount <= 8)
194 /* read the palette */
195 colorcount = This->entry.bColorCount ? This->entry.bColorCount : 256;
197 hr = IStream_Read(This->parent->stream, colors, sizeof(RGBQUAD)*colorcount, &bytesread);
198 if (FAILED(hr) || bytesread != sizeof(RGBQUAD)*colorcount) goto fail;
201 bitsStride = width * 4;
202 bitsSize = bitsStride * height;
204 /* read the XOR data */
205 switch (This->entry.wBitCount)
207 case 1:
209 UINT xorBytesPerRow = (width+31)/32*4;
210 UINT xorBytes = xorBytesPerRow * height;
211 INT xorStride;
212 BYTE *xorRow;
213 BYTE *bitsRow;
214 UINT x, y;
216 tempdata = HeapAlloc(GetProcessHeap(), 0, xorBytes);
217 if (!tempdata)
219 hr = E_OUTOFMEMORY;
220 goto fail;
223 hr = IStream_Read(This->parent->stream, tempdata, xorBytes, &bytesread);
224 if (FAILED(hr) || bytesread != xorBytes) goto fail;
226 if (bih.biHeight > 0) /* bottom-up DIB */
228 xorStride = -xorBytesPerRow;
229 xorRow = tempdata + (height-1)*xorBytesPerRow;
231 else /* top-down DIB */
233 xorStride = xorBytesPerRow;
234 xorRow = tempdata;
237 bits = HeapAlloc(GetProcessHeap(), 0, bitsSize);
239 /* palette-map the 1-bit data */
240 bitsRow = bits;
241 for (y=0; y<height; y++) {
242 BYTE *xorByte=xorRow;
243 DWORD *bitsPixel=(DWORD*)bitsRow;
244 for (x=0; x<width; x+=8) {
245 BYTE xorVal;
246 xorVal=*xorByte++;
247 *bitsPixel++ = colors[xorVal>>7];
248 if (x+1 < width) *bitsPixel++ = colors[xorVal>>6&1];
249 if (x+2 < width) *bitsPixel++ = colors[xorVal>>5&1];
250 if (x+3 < width) *bitsPixel++ = colors[xorVal>>4&1];
251 if (x+4 < width) *bitsPixel++ = colors[xorVal>>3&1];
252 if (x+5 < width) *bitsPixel++ = colors[xorVal>>2&1];
253 if (x+6 < width) *bitsPixel++ = colors[xorVal>>1&1];
254 if (x+7 < width) *bitsPixel++ = colors[xorVal&1];
256 xorRow += xorStride;
257 bitsRow += bitsStride;
260 HeapFree(GetProcessHeap(), 0, tempdata);
261 break;
263 case 4:
265 UINT xorBytesPerRow = (width+7)/8*4;
266 UINT xorBytes = xorBytesPerRow * height;
267 INT xorStride;
268 BYTE *xorRow;
269 BYTE *bitsRow;
270 UINT x, y;
272 tempdata = HeapAlloc(GetProcessHeap(), 0, xorBytes);
273 if (!tempdata)
275 hr = E_OUTOFMEMORY;
276 goto fail;
279 hr = IStream_Read(This->parent->stream, tempdata, xorBytes, &bytesread);
280 if (FAILED(hr) || bytesread != xorBytes) goto fail;
282 if (bih.biHeight > 0) /* bottom-up DIB */
284 xorStride = -xorBytesPerRow;
285 xorRow = tempdata + (height-1)*xorBytesPerRow;
287 else /* top-down DIB */
289 xorStride = xorBytesPerRow;
290 xorRow = tempdata;
293 bits = HeapAlloc(GetProcessHeap(), 0, bitsSize);
295 /* palette-map the 4-bit data */
296 bitsRow = bits;
297 for (y=0; y<height; y++) {
298 BYTE *xorByte=xorRow;
299 DWORD *bitsPixel=(DWORD*)bitsRow;
300 for (x=0; x<width; x+=2) {
301 BYTE xorVal;
302 xorVal=*xorByte++;
303 *bitsPixel++ = colors[xorVal>>4];
304 if (x+1 < width) *bitsPixel++ = colors[xorVal&0xf];
306 xorRow += xorStride;
307 bitsRow += bitsStride;
310 HeapFree(GetProcessHeap(), 0, tempdata);
311 break;
313 case 8:
315 UINT xorBytesPerRow = (width+3)/4*4;
316 UINT xorBytes = xorBytesPerRow * height;
317 INT xorStride;
318 BYTE *xorRow;
319 BYTE *bitsRow;
320 UINT x, y;
322 tempdata = HeapAlloc(GetProcessHeap(), 0, xorBytes);
323 if (!tempdata)
325 hr = E_OUTOFMEMORY;
326 goto fail;
329 hr = IStream_Read(This->parent->stream, tempdata, xorBytes, &bytesread);
330 if (FAILED(hr) || bytesread != xorBytes) goto fail;
332 if (bih.biHeight > 0) /* bottom-up DIB */
334 xorStride = -xorBytesPerRow;
335 xorRow = tempdata + (height-1)*xorBytesPerRow;
337 else /* top-down DIB */
339 xorStride = xorBytesPerRow;
340 xorRow = tempdata;
343 bits = HeapAlloc(GetProcessHeap(), 0, bitsSize);
345 /* palette-map the 8-bit data */
346 bitsRow = bits;
347 for (y=0; y<height; y++) {
348 BYTE *xorByte=xorRow;
349 DWORD *bitsPixel=(DWORD*)bitsRow;
350 for (x=0; x<width; x++)
351 *bitsPixel++ = colors[*xorByte++];
352 xorRow += xorStride;
353 bitsRow += bitsStride;
356 HeapFree(GetProcessHeap(), 0, tempdata);
357 break;
359 case 24:
361 UINT xorBytesPerRow = (width*3+3)/4*4;
362 UINT xorBytes = xorBytesPerRow * height;
363 INT xorStride;
364 BYTE *xorRow;
365 BYTE *bitsRow;
366 UINT x, y;
368 tempdata = HeapAlloc(GetProcessHeap(), 0, xorBytes);
369 if (!tempdata)
371 hr = E_OUTOFMEMORY;
372 goto fail;
375 hr = IStream_Read(This->parent->stream, tempdata, xorBytes, &bytesread);
376 if (FAILED(hr) || bytesread != xorBytes) goto fail;
378 if (bih.biHeight > 0) /* bottom-up DIB */
380 xorStride = -xorBytesPerRow;
381 xorRow = tempdata + (height-1)*xorBytesPerRow;
383 else /* top-down DIB */
385 xorStride = xorBytesPerRow;
386 xorRow = tempdata;
389 bits = HeapAlloc(GetProcessHeap(), 0, bitsSize);
391 /* copy BGR->BGRA */
392 bitsRow = bits;
393 for (y=0; y<height; y++) {
394 BYTE *xorByte=xorRow;
395 BYTE *bitsByte=bitsRow;
396 for (x=0; x<width; x++)
398 *bitsByte++ = *xorByte++; /* blue */
399 *bitsByte++ = *xorByte++; /* green */
400 *bitsByte++ = *xorByte++; /* red */
401 bitsByte++; /* alpha */
403 xorRow += xorStride;
404 bitsRow += bitsStride;
407 HeapFree(GetProcessHeap(), 0, tempdata);
408 break;
410 case 32:
412 UINT xorBytesPerRow = width*4;
413 UINT xorBytes = xorBytesPerRow * height;
415 bits = HeapAlloc(GetProcessHeap(), 0, xorBytes);
416 if (!bits)
418 hr = E_OUTOFMEMORY;
419 goto fail;
422 if (bih.biHeight > 0) /* bottom-up DIB */
424 /* read the rows backwards so we get a top-down DIB */
425 UINT i;
426 BYTE *xorRow = bits + xorBytesPerRow * (height-1);
428 for (i=0; i<height; i++)
430 hr = IStream_Read(This->parent->stream, xorRow, xorBytesPerRow, &bytesread);
431 if (FAILED(hr) || bytesread != xorBytesPerRow) goto fail;
432 xorRow -= xorBytesPerRow;
435 else /* top-down DIB */
437 hr = IStream_Read(This->parent->stream, bits, xorBytes, &bytesread);
438 if (FAILED(hr) || bytesread != xorBytes) goto fail;
440 break;
442 default:
443 FIXME("unsupported bitcount: %u\n", This->entry.wBitCount);
444 goto fail;
447 if (This->entry.wBitCount < 32)
449 /* set alpha data based on the AND mask */
450 UINT andBytesPerRow = (width+31)/32*4;
451 UINT andBytes = andBytesPerRow * height;
452 INT andStride;
453 BYTE *andRow;
454 BYTE *bitsRow;
455 UINT x, y;
457 tempdata = HeapAlloc(GetProcessHeap(), 0, andBytes);
458 if (!tempdata)
460 hr = E_OUTOFMEMORY;
461 goto fail;
464 hr = IStream_Read(This->parent->stream, tempdata, andBytes, &bytesread);
465 if (FAILED(hr) || bytesread != andBytes) goto fail;
467 if (bih.biHeight > 0) /* bottom-up DIB */
469 andStride = -andBytesPerRow;
470 andRow = tempdata + (height-1)*andBytesPerRow;
472 else /* top-down DIB */
474 andStride = andBytesPerRow;
475 andRow = tempdata;
478 bitsRow = bits;
479 for (y=0; y<height; y++) {
480 BYTE *andByte=andRow;
481 DWORD *bitsPixel=(DWORD*)bitsRow;
482 for (x=0; x<width; x+=8) {
483 BYTE andVal=*andByte++;
484 pixel_set_trans(bitsPixel++, andVal>>7&1);
485 if (x+1 < width) pixel_set_trans(bitsPixel++, andVal>>6&1);
486 if (x+2 < width) pixel_set_trans(bitsPixel++, andVal>>5&1);
487 if (x+3 < width) pixel_set_trans(bitsPixel++, andVal>>4&1);
488 if (x+4 < width) pixel_set_trans(bitsPixel++, andVal>>3&1);
489 if (x+5 < width) pixel_set_trans(bitsPixel++, andVal>>2&1);
490 if (x+6 < width) pixel_set_trans(bitsPixel++, andVal>>1&1);
491 if (x+7 < width) pixel_set_trans(bitsPixel++, andVal&1);
493 andRow += andStride;
494 bitsRow += bitsStride;
497 HeapFree(GetProcessHeap(), 0, tempdata);
500 This->bits = bits;
502 return S_OK;
504 fail:
505 HeapFree(GetProcessHeap(), 0, tempdata);
506 HeapFree(GetProcessHeap(), 0, bits);
507 if (SUCCEEDED(hr)) hr = E_FAIL;
508 TRACE("<-- %x\n", hr);
509 return hr;
512 static HRESULT WINAPI IcoFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
513 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
515 IcoFrameDecode *This = (IcoFrameDecode*)iface;
516 HRESULT hr=S_OK;
517 UINT width, height, stride;
518 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
520 EnterCriticalSection(&This->parent->lock);
521 if (!This->bits)
523 hr = IcoFrameDecode_ReadPixels(This);
525 LeaveCriticalSection(&This->parent->lock);
526 if (FAILED(hr)) return hr;
528 width = This->entry.bWidth ? This->entry.bWidth : 256;
529 height = This->entry.bHeight ? This->entry.bHeight : 256;
530 stride = width * 4;
532 return copy_pixels(32, This->bits, width, height, stride,
533 prc, cbStride, cbBufferSize, pbBuffer);
536 static HRESULT WINAPI IcoFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
537 IWICMetadataQueryReader **ppIMetadataQueryReader)
539 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
540 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
543 static HRESULT WINAPI IcoFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
544 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
546 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
547 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
550 static HRESULT WINAPI IcoFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
551 IWICBitmapSource **ppIThumbnail)
553 TRACE("(%p,%p)\n", iface, ppIThumbnail);
554 return WINCODEC_ERR_CODECNOTHUMBNAIL;
557 static const IWICBitmapFrameDecodeVtbl IcoFrameDecode_Vtbl = {
558 IcoFrameDecode_QueryInterface,
559 IcoFrameDecode_AddRef,
560 IcoFrameDecode_Release,
561 IcoFrameDecode_GetSize,
562 IcoFrameDecode_GetPixelFormat,
563 IcoFrameDecode_GetResolution,
564 IcoFrameDecode_CopyPalette,
565 IcoFrameDecode_CopyPixels,
566 IcoFrameDecode_GetMetadataQueryReader,
567 IcoFrameDecode_GetColorContexts,
568 IcoFrameDecode_GetThumbnail
571 static HRESULT WINAPI IcoDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
572 void **ppv)
574 IcoDecoder *This = (IcoDecoder*)iface;
575 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
577 if (!ppv) return E_INVALIDARG;
579 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
581 *ppv = This;
583 else
585 *ppv = NULL;
586 return E_NOINTERFACE;
589 IUnknown_AddRef((IUnknown*)*ppv);
590 return S_OK;
593 static ULONG WINAPI IcoDecoder_AddRef(IWICBitmapDecoder *iface)
595 IcoDecoder *This = (IcoDecoder*)iface;
596 ULONG ref = InterlockedIncrement(&This->ref);
598 TRACE("(%p) refcount=%u\n", iface, ref);
600 return ref;
603 static ULONG WINAPI IcoDecoder_Release(IWICBitmapDecoder *iface)
605 IcoDecoder *This = (IcoDecoder*)iface;
606 ULONG ref = InterlockedDecrement(&This->ref);
608 TRACE("(%p) refcount=%u\n", iface, ref);
610 if (ref == 0)
612 This->lock.DebugInfo->Spare[0] = 0;
613 DeleteCriticalSection(&This->lock);
614 if (This->stream) IStream_Release(This->stream);
615 HeapFree(GetProcessHeap(), 0, This);
618 return ref;
621 static HRESULT WINAPI IcoDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
622 DWORD *pdwCapability)
624 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
625 return E_NOTIMPL;
628 static HRESULT WINAPI IcoDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
629 WICDecodeOptions cacheOptions)
631 IcoDecoder *This = (IcoDecoder*)iface;
632 LARGE_INTEGER seek;
633 HRESULT hr;
634 ULONG bytesread;
635 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
637 EnterCriticalSection(&This->lock);
639 if (This->initialized)
641 hr = WINCODEC_ERR_WRONGSTATE;
642 goto end;
645 seek.QuadPart = 0;
646 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
647 if (FAILED(hr)) goto end;
649 hr = IStream_Read(pIStream, &This->header, sizeof(ICONHEADER), &bytesread);
650 if (FAILED(hr)) goto end;
651 if (bytesread != sizeof(ICONHEADER) ||
652 This->header.idReserved != 0 ||
653 This->header.idType != 1)
655 hr = E_FAIL;
656 goto end;
659 This->initialized = TRUE;
660 This->stream = pIStream;
661 IStream_AddRef(pIStream);
663 end:
665 LeaveCriticalSection(&This->lock);
667 return hr;
670 static HRESULT WINAPI IcoDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
671 GUID *pguidContainerFormat)
673 FIXME("(%p,%p): stub\n", iface, pguidContainerFormat);
674 return E_NOTIMPL;
677 static HRESULT WINAPI IcoDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
678 IWICBitmapDecoderInfo **ppIDecoderInfo)
680 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
681 return E_NOTIMPL;
684 static HRESULT WINAPI IcoDecoder_CopyPalette(IWICBitmapDecoder *iface,
685 IWICPalette *pIPalette)
687 TRACE("(%p,%p)\n", iface, pIPalette);
688 return WINCODEC_ERR_PALETTEUNAVAILABLE;
691 static HRESULT WINAPI IcoDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
692 IWICMetadataQueryReader **ppIMetadataQueryReader)
694 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
695 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
698 static HRESULT WINAPI IcoDecoder_GetPreview(IWICBitmapDecoder *iface,
699 IWICBitmapSource **ppIBitmapSource)
701 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
702 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
705 static HRESULT WINAPI IcoDecoder_GetColorContexts(IWICBitmapDecoder *iface,
706 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
708 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
709 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
712 static HRESULT WINAPI IcoDecoder_GetThumbnail(IWICBitmapDecoder *iface,
713 IWICBitmapSource **ppIThumbnail)
715 TRACE("(%p,%p)\n", iface, ppIThumbnail);
716 return WINCODEC_ERR_CODECNOTHUMBNAIL;
719 static HRESULT WINAPI IcoDecoder_GetFrameCount(IWICBitmapDecoder *iface,
720 UINT *pCount)
722 IcoDecoder *This = (IcoDecoder*)iface;
723 TRACE("(%p,%p)\n", iface, pCount);
725 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
727 *pCount = This->header.idCount;
728 TRACE("<-- %u\n", *pCount);
730 return S_OK;
733 static HRESULT WINAPI IcoDecoder_GetFrame(IWICBitmapDecoder *iface,
734 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
736 IcoDecoder *This = (IcoDecoder*)iface;
737 IcoFrameDecode *result=NULL;
738 LARGE_INTEGER seek;
739 HRESULT hr;
740 ULONG bytesread;
741 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
743 EnterCriticalSection(&This->lock);
745 if (!This->initialized)
747 hr = WINCODEC_ERR_NOTINITIALIZED;
748 goto fail;
751 if (This->header.idCount < index)
753 hr = E_INVALIDARG;
754 goto fail;
757 result = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoFrameDecode));
758 if (!result)
760 hr = E_OUTOFMEMORY;
761 goto fail;
764 result->lpVtbl = &IcoFrameDecode_Vtbl;
765 result->ref = 1;
766 result->parent = This;
767 result->bits = NULL;
769 /* read the icon entry */
770 seek.QuadPart = sizeof(ICONHEADER) + sizeof(ICONDIRENTRY) * index;
771 hr = IStream_Seek(This->stream, seek, STREAM_SEEK_SET, 0);
772 if (FAILED(hr)) goto fail;
774 hr = IStream_Read(This->stream, &result->entry, sizeof(ICONDIRENTRY), &bytesread);
775 if (FAILED(hr) || bytesread != sizeof(ICONDIRENTRY)) goto fail;
777 IWICBitmapDecoder_AddRef(iface);
779 *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
781 LeaveCriticalSection(&This->lock);
783 return S_OK;
785 fail:
786 LeaveCriticalSection(&This->lock);
787 HeapFree(GetProcessHeap(), 0, result);
788 if (SUCCEEDED(hr)) hr = E_FAIL;
789 TRACE("<-- %x\n", hr);
790 return hr;
793 static const IWICBitmapDecoderVtbl IcoDecoder_Vtbl = {
794 IcoDecoder_QueryInterface,
795 IcoDecoder_AddRef,
796 IcoDecoder_Release,
797 IcoDecoder_QueryCapability,
798 IcoDecoder_Initialize,
799 IcoDecoder_GetContainerFormat,
800 IcoDecoder_GetDecoderInfo,
801 IcoDecoder_CopyPalette,
802 IcoDecoder_GetMetadataQueryReader,
803 IcoDecoder_GetPreview,
804 IcoDecoder_GetColorContexts,
805 IcoDecoder_GetThumbnail,
806 IcoDecoder_GetFrameCount,
807 IcoDecoder_GetFrame
810 HRESULT IcoDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
812 IcoDecoder *This;
813 HRESULT ret;
815 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
817 *ppv = NULL;
819 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
821 This = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoDecoder));
822 if (!This) return E_OUTOFMEMORY;
824 This->lpVtbl = &IcoDecoder_Vtbl;
825 This->ref = 1;
826 This->stream = NULL;
827 This->initialized = FALSE;
828 InitializeCriticalSection(&This->lock);
829 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IcoDecoder.lock");
831 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
832 IUnknown_Release((IUnknown*)This);
834 return ret;