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
31 #include "wincodecs_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
60 const IWICBitmapDecoderVtbl *lpVtbl;
65 CRITICAL_SECTION lock; /* must be held when accessing stream */
69 const IWICBitmapFrameDecodeVtbl *lpVtbl;
76 static HRESULT WINAPI IcoFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
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))
96 IUnknown_AddRef((IUnknown*)*ppv);
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);
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);
119 IUnknown_Release((IUnknown*)This->parent);
120 HeapFree(GetProcessHeap(), 0, This->bits);
121 HeapFree(GetProcessHeap(), 0, This);
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);
140 static HRESULT WINAPI IcoFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
141 WICPixelFormatGUID *pPixelFormat)
143 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
147 static HRESULT WINAPI IcoFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
148 double *pDpiX, double *pDpiY)
150 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
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;
175 BYTE *tempdata = NULL;
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 (bih.biBitCount <= 8)
194 /* read the palette */
195 colorcount = bih.biClrUsed ? bih.biClrUsed : 1 << bih.biBitCount;
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 (bih.biBitCount)
209 UINT xorBytesPerRow = (width+31)/32*4;
210 UINT xorBytes = xorBytesPerRow * height;
216 tempdata = HeapAlloc(GetProcessHeap(), 0, xorBytes);
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;
237 bits = HeapAlloc(GetProcessHeap(), 0, bitsSize);
239 /* palette-map the 1-bit data */
241 for (y=0; y<height; y++) {
242 BYTE *xorByte=xorRow;
243 DWORD *bitsPixel=(DWORD*)bitsRow;
244 for (x=0; x<width; x+=8) {
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];
257 bitsRow += bitsStride;
260 HeapFree(GetProcessHeap(), 0, tempdata);
265 UINT xorBytesPerRow = (width+7)/8*4;
266 UINT xorBytes = xorBytesPerRow * height;
272 tempdata = HeapAlloc(GetProcessHeap(), 0, xorBytes);
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;
293 bits = HeapAlloc(GetProcessHeap(), 0, bitsSize);
295 /* palette-map the 4-bit data */
297 for (y=0; y<height; y++) {
298 BYTE *xorByte=xorRow;
299 DWORD *bitsPixel=(DWORD*)bitsRow;
300 for (x=0; x<width; x+=2) {
303 *bitsPixel++ = colors[xorVal>>4];
304 if (x+1 < width) *bitsPixel++ = colors[xorVal&0xf];
307 bitsRow += bitsStride;
310 HeapFree(GetProcessHeap(), 0, tempdata);
315 UINT xorBytesPerRow = (width+3)/4*4;
316 UINT xorBytes = xorBytesPerRow * height;
322 tempdata = HeapAlloc(GetProcessHeap(), 0, xorBytes);
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;
343 bits = HeapAlloc(GetProcessHeap(), 0, bitsSize);
345 /* palette-map the 8-bit data */
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++];
353 bitsRow += bitsStride;
356 HeapFree(GetProcessHeap(), 0, tempdata);
361 UINT xorBytesPerRow = (width*3+3)/4*4;
362 UINT xorBytes = xorBytesPerRow * height;
368 tempdata = HeapAlloc(GetProcessHeap(), 0, xorBytes);
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;
389 bits = HeapAlloc(GetProcessHeap(), 0, bitsSize);
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 */
404 bitsRow += bitsStride;
407 HeapFree(GetProcessHeap(), 0, tempdata);
412 UINT xorBytesPerRow = width*4;
413 UINT xorBytes = xorBytesPerRow * height;
415 bits = HeapAlloc(GetProcessHeap(), 0, xorBytes);
422 if (bih.biHeight > 0) /* bottom-up DIB */
424 /* read the rows backwards so we get a top-down DIB */
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;
443 FIXME("unsupported bitcount: %u\n", bih.biBitCount);
447 if (bih.biBitCount < 32)
449 /* set alpha data based on the AND mask */
450 UINT andBytesPerRow = (width+31)/32*4;
451 UINT andBytes = andBytesPerRow * height;
457 tempdata = HeapAlloc(GetProcessHeap(), 0, andBytes);
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;
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);
494 bitsRow += bitsStride;
497 HeapFree(GetProcessHeap(), 0, tempdata);
505 HeapFree(GetProcessHeap(), 0, tempdata);
506 HeapFree(GetProcessHeap(), 0, bits);
507 if (SUCCEEDED(hr)) hr = E_FAIL;
508 TRACE("<-- %x\n", hr);
512 static HRESULT WINAPI IcoFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
513 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
515 IcoFrameDecode *This = (IcoFrameDecode*)iface;
517 UINT width, height, stride;
518 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
520 EnterCriticalSection(&This->parent->lock);
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;
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,
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))
586 return E_NOINTERFACE;
589 IUnknown_AddRef((IUnknown*)*ppv);
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);
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);
612 This->lock.DebugInfo->Spare[0] = 0;
613 DeleteCriticalSection(&This->lock);
614 if (This->stream) IStream_Release(This->stream);
615 HeapFree(GetProcessHeap(), 0, This);
621 static HRESULT WINAPI IcoDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
622 DWORD *pdwCapability)
624 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
628 static HRESULT WINAPI IcoDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
629 WICDecodeOptions cacheOptions)
631 IcoDecoder *This = (IcoDecoder*)iface;
635 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
637 EnterCriticalSection(&This->lock);
639 if (This->initialized)
641 hr = WINCODEC_ERR_WRONGSTATE;
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)
659 This->initialized = TRUE;
660 This->stream = pIStream;
661 IStream_AddRef(pIStream);
665 LeaveCriticalSection(&This->lock);
670 static HRESULT WINAPI IcoDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
671 GUID *pguidContainerFormat)
673 FIXME("(%p,%p): stub\n", iface, pguidContainerFormat);
677 static HRESULT WINAPI IcoDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
678 IWICBitmapDecoderInfo **ppIDecoderInfo)
680 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
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,
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);
733 static HRESULT WINAPI IcoDecoder_GetFrame(IWICBitmapDecoder *iface,
734 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
736 IcoDecoder *This = (IcoDecoder*)iface;
737 IcoFrameDecode *result=NULL;
741 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
743 EnterCriticalSection(&This->lock);
745 if (!This->initialized)
747 hr = WINCODEC_ERR_NOTINITIALIZED;
751 if (This->header.idCount < index)
757 result = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoFrameDecode));
764 result->lpVtbl = &IcoFrameDecode_Vtbl;
766 result->parent = This;
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);
786 LeaveCriticalSection(&This->lock);
787 HeapFree(GetProcessHeap(), 0, result);
788 if (SUCCEEDED(hr)) hr = E_FAIL;
789 TRACE("<-- %x\n", hr);
793 static const IWICBitmapDecoderVtbl IcoDecoder_Vtbl = {
794 IcoDecoder_QueryInterface,
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,
810 HRESULT IcoDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
815 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
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;
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);