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 IWICBitmapDecoder IWICBitmapDecoder_iface;
65 CRITICAL_SECTION lock; /* must be held when accessing stream */
69 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
75 static inline IcoDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
77 return CONTAINING_RECORD(iface, IcoDecoder, IWICBitmapDecoder_iface);
80 static inline IcoFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
82 return CONTAINING_RECORD(iface, IcoFrameDecode, IWICBitmapFrameDecode_iface);
85 static HRESULT WINAPI IcoFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
88 IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
89 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
91 if (!ppv) return E_INVALIDARG;
93 if (IsEqualIID(&IID_IUnknown, iid) ||
94 IsEqualIID(&IID_IWICBitmapSource, iid) ||
95 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
102 return E_NOINTERFACE;
105 IUnknown_AddRef((IUnknown*)*ppv);
109 static ULONG WINAPI IcoFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
111 IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
112 ULONG ref = InterlockedIncrement(&This->ref);
114 TRACE("(%p) refcount=%u\n", iface, ref);
119 static ULONG WINAPI IcoFrameDecode_Release(IWICBitmapFrameDecode *iface)
121 IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
122 ULONG ref = InterlockedDecrement(&This->ref);
124 TRACE("(%p) refcount=%u\n", iface, ref);
128 HeapFree(GetProcessHeap(), 0, This->bits);
129 HeapFree(GetProcessHeap(), 0, This);
135 static HRESULT WINAPI IcoFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
136 UINT *puiWidth, UINT *puiHeight)
138 IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
140 *puiWidth = This->width;
141 *puiHeight = This->height;
143 TRACE("(%p) -> (%i,%i)\n", iface, *puiWidth, *puiHeight);
148 static HRESULT WINAPI IcoFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
149 WICPixelFormatGUID *pPixelFormat)
151 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
155 static HRESULT WINAPI IcoFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
156 double *pDpiX, double *pDpiY)
158 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
162 static HRESULT WINAPI IcoFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
163 IWICPalette *pIPalette)
165 TRACE("(%p,%p)\n", iface, pIPalette);
166 return WINCODEC_ERR_PALETTEUNAVAILABLE;
169 static HRESULT WINAPI IcoFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
170 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
172 IcoFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
173 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
175 return copy_pixels(32, This->bits, This->width, This->height, This->width * 4,
176 prc, cbStride, cbBufferSize, pbBuffer);
179 static HRESULT WINAPI IcoFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
180 IWICMetadataQueryReader **ppIMetadataQueryReader)
182 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
183 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
186 static HRESULT WINAPI IcoFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
187 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
189 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
190 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
193 static HRESULT WINAPI IcoFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
194 IWICBitmapSource **ppIThumbnail)
196 TRACE("(%p,%p)\n", iface, ppIThumbnail);
197 return WINCODEC_ERR_CODECNOTHUMBNAIL;
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
214 static inline void pixel_set_trans(DWORD* pixel, BOOL transparent)
216 if (transparent) *pixel = 0;
217 else *pixel |= 0xff000000;
220 static HRESULT ReadIcoDib(IStream *stream, IcoFrameDecode *result)
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 */
231 hr = IcoDibDecoder_CreateInstance(&bmp_decoder);
234 BmpDecoder_GetWICDecoder(bmp_decoder, &decoder);
235 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
238 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
242 hr = IWICBitmapFrameDecode_GetSize(framedecode, &result->width, &result->height);
246 result->bits = HeapAlloc(GetProcessHeap(), 0, result->width * result->height * 4);
247 if (!result->bits) hr = E_OUTOFMEMORY;
251 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &pixelformat);
253 if (IsEqualGUID(&pixelformat, &GUID_WICPixelFormat32bppBGR) ||
254 IsEqualGUID(&pixelformat, &GUID_WICPixelFormat32bppBGRA))
256 source = (IWICBitmapSource*)framedecode;
257 IWICBitmapSource_AddRef(source);
262 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA,
263 (IWICBitmapSource*)framedecode, &source);
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);
276 IWICBitmapSource_Release(source);
279 IWICBitmapFrameDecode_Release(framedecode);
282 if (SUCCEEDED(hr) && has_alpha)
284 /* If the alpha channel is fully transparent, we should ignore it. */
285 int nonzero_alpha = 0;
288 for (i=0; i<(result->height*result->width); i++)
290 if (result->bits[i*4+3] != 0)
299 for (i=0; i<(result->height*result->width); i++)
300 result->bits[i*4+3] = 0xff;
306 if (SUCCEEDED(hr) && !has_alpha)
308 /* set alpha data based on the AND mask */
309 UINT andBytesPerRow = (result->width+31)/32*4;
310 UINT andBytes = andBytesPerRow * result->height;
315 UINT bitsStride = result->width * 4;
322 BmpDecoder_FindIconMask(bmp_decoder, &offset, &topdown);
326 seek.QuadPart = offset;
328 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, 0);
332 tempdata = HeapAlloc(GetProcessHeap(), 0, andBytes);
333 if (!tempdata) hr = E_OUTOFMEMORY;
337 hr = IStream_Read(stream, tempdata, andBytes, &bytesread);
339 if (SUCCEEDED(hr) && bytesread == andBytes)
343 andStride = andBytesPerRow;
348 andStride = -andBytesPerRow;
349 andRow = tempdata + (result->height-1)*andBytesPerRow;
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);
368 bitsRow += bitsStride;
372 HeapFree(GetProcessHeap(), 0, tempdata);
376 IWICBitmapDecoder_Release(decoder);
382 static HRESULT ReadIcoPng(IStream *stream, IcoFrameDecode *result)
384 IWICBitmapDecoder *decoder = NULL;
385 IWICBitmapFrameDecode *sourceFrame = NULL;
386 IWICBitmapSource *sourceBitmap = NULL;
390 hr = PngDecoder_CreateInstance(NULL, &IID_IWICBitmapDecoder, (void**)&decoder);
393 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
396 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &sourceFrame);
399 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)sourceFrame, &sourceBitmap);
402 hr = IWICBitmapFrameDecode_GetSize(sourceFrame, &result->width, &result->height);
405 result->bits = HeapAlloc(GetProcessHeap(), 0, 4 * result->width * result->height);
406 if (result->bits == NULL)
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);
420 IWICBitmapDecoder_Release(decoder);
421 if (sourceFrame != NULL)
422 IWICBitmapFrameDecode_Release(sourceFrame);
423 if (sourceBitmap != NULL)
424 IWICBitmapSource_Release(sourceBitmap);
428 static HRESULT WINAPI IcoDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
431 IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
432 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
434 if (!ppv) return E_INVALIDARG;
436 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
443 return E_NOINTERFACE;
446 IUnknown_AddRef((IUnknown*)*ppv);
450 static ULONG WINAPI IcoDecoder_AddRef(IWICBitmapDecoder *iface)
452 IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
453 ULONG ref = InterlockedIncrement(&This->ref);
455 TRACE("(%p) refcount=%u\n", iface, ref);
460 static ULONG WINAPI IcoDecoder_Release(IWICBitmapDecoder *iface)
462 IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
463 ULONG ref = InterlockedDecrement(&This->ref);
465 TRACE("(%p) refcount=%u\n", iface, ref);
469 This->lock.DebugInfo->Spare[0] = 0;
470 DeleteCriticalSection(&This->lock);
471 if (This->stream) IStream_Release(This->stream);
472 HeapFree(GetProcessHeap(), 0, This);
478 static HRESULT WINAPI IcoDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
479 DWORD *pdwCapability)
481 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
485 static HRESULT WINAPI IcoDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
486 WICDecodeOptions cacheOptions)
488 IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
492 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
494 EnterCriticalSection(&This->lock);
496 if (This->initialized)
498 hr = WINCODEC_ERR_WRONGSTATE;
503 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
504 if (FAILED(hr)) goto end;
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)
516 This->initialized = TRUE;
517 This->stream = pIStream;
518 IStream_AddRef(pIStream);
522 LeaveCriticalSection(&This->lock);
527 static HRESULT WINAPI IcoDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
528 GUID *pguidContainerFormat)
530 FIXME("(%p,%p): stub\n", iface, pguidContainerFormat);
534 static HRESULT WINAPI IcoDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
535 IWICBitmapDecoderInfo **ppIDecoderInfo)
537 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
541 static HRESULT WINAPI IcoDecoder_CopyPalette(IWICBitmapDecoder *iface,
542 IWICPalette *pIPalette)
544 TRACE("(%p,%p)\n", iface, pIPalette);
545 return WINCODEC_ERR_PALETTEUNAVAILABLE;
548 static HRESULT WINAPI IcoDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
549 IWICMetadataQueryReader **ppIMetadataQueryReader)
551 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
552 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
555 static HRESULT WINAPI IcoDecoder_GetPreview(IWICBitmapDecoder *iface,
556 IWICBitmapSource **ppIBitmapSource)
558 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
559 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
562 static HRESULT WINAPI IcoDecoder_GetColorContexts(IWICBitmapDecoder *iface,
563 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
565 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
566 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
569 static HRESULT WINAPI IcoDecoder_GetThumbnail(IWICBitmapDecoder *iface,
570 IWICBitmapSource **ppIThumbnail)
572 TRACE("(%p,%p)\n", iface, ppIThumbnail);
573 return WINCODEC_ERR_CODECNOTHUMBNAIL;
576 static HRESULT WINAPI IcoDecoder_GetFrameCount(IWICBitmapDecoder *iface,
579 IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
580 TRACE("(%p,%p)\n", iface, pCount);
582 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
584 *pCount = This->header.idCount;
585 TRACE("<-- %u\n", *pCount);
590 static HRESULT WINAPI IcoDecoder_GetFrame(IWICBitmapDecoder *iface,
591 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
593 IcoDecoder *This = impl_from_IWICBitmapDecoder(iface);
594 IcoFrameDecode *result=NULL;
596 ULARGE_INTEGER offset, length;
600 IWICStream *substream=NULL;
602 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
604 EnterCriticalSection(&This->lock);
606 if (!This->initialized)
608 hr = WINCODEC_ERR_NOTINITIALIZED;
612 if (This->header.idCount < index)
618 result = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoFrameDecode));
625 result->IWICBitmapFrameDecode_iface.lpVtbl = &IcoFrameDecode_Vtbl;
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;
634 hr = IStream_Read(This->stream, &entry, sizeof(ICONDIRENTRY), &bytesread);
635 if (FAILED(hr) || bytesread != sizeof(ICONDIRENTRY)) goto fail;
637 /* create a stream object for this icon */
638 hr = StreamImpl_Create(&substream);
639 if (FAILED(hr)) goto fail;
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;
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;
650 /* forward to the appropriate decoding function based on the magic number */
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);
661 hr = ReadIcoPng((IStream*)substream, result);
664 FIXME("Unrecognized ICO frame magic: %x\n", magic);
668 if (FAILED(hr)) goto fail;
670 *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
672 LeaveCriticalSection(&This->lock);
674 IStream_Release(substream);
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);
687 static const IWICBitmapDecoderVtbl IcoDecoder_Vtbl = {
688 IcoDecoder_QueryInterface,
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,
704 HRESULT IcoDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
709 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
713 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
715 This = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoDecoder));
716 if (!This) return E_OUTOFMEMORY;
718 This->IWICBitmapDecoder_iface.lpVtbl = &IcoDecoder_Vtbl;
721 This->initialized = FALSE;
722 InitializeCriticalSection(&This->lock);
723 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IcoDecoder.lock");
725 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
726 IUnknown_Release((IUnknown*)This);