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 IWICBitmapDecoder
*decoder
;
224 IWICBitmapFrameDecode
*framedecode
;
225 WICPixelFormatGUID pixelformat
;
226 IWICBitmapSource
*source
;
227 int has_alpha
=FALSE
; /* if TRUE, alpha data might be in the image data */
230 hr
= IcoDibDecoder_CreateInstance(NULL
, &IID_IWICBitmapDecoder
, (void**)&decoder
);
233 hr
= IWICBitmapDecoder_Initialize(decoder
, stream
, WICDecodeMetadataCacheOnLoad
);
236 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &framedecode
);
240 hr
= IWICBitmapFrameDecode_GetSize(framedecode
, &result
->width
, &result
->height
);
244 result
->bits
= HeapAlloc(GetProcessHeap(), 0, result
->width
* result
->height
* 4);
245 if (!result
->bits
) hr
= E_OUTOFMEMORY
;
249 hr
= IWICBitmapFrameDecode_GetPixelFormat(framedecode
, &pixelformat
);
251 if (IsEqualGUID(&pixelformat
, &GUID_WICPixelFormat32bppBGR
) ||
252 IsEqualGUID(&pixelformat
, &GUID_WICPixelFormat32bppBGRA
))
254 source
= (IWICBitmapSource
*)framedecode
;
255 IWICBitmapSource_AddRef(source
);
260 hr
= WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA
,
261 (IWICBitmapSource
*)framedecode
, &source
);
269 rc
.Width
= result
->width
;
270 rc
.Height
= result
->height
;
271 hr
= IWICBitmapSource_CopyPixels(source
, &rc
, result
->width
* 4,
272 result
->width
* result
->height
* 4, result
->bits
);
274 IWICBitmapSource_Release(source
);
277 IWICBitmapFrameDecode_Release(framedecode
);
280 if (SUCCEEDED(hr
) && !has_alpha
)
282 /* set alpha data based on the AND mask */
283 UINT andBytesPerRow
= (result
->width
+31)/32*4;
284 UINT andBytes
= andBytesPerRow
* result
->height
;
289 UINT bitsStride
= result
->width
* 4;
296 BmpDecoder_FindIconMask(decoder
, &offset
, &topdown
);
300 seek
.QuadPart
= offset
;
302 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, 0);
306 tempdata
= HeapAlloc(GetProcessHeap(), 0, andBytes
);
307 if (!tempdata
) hr
= E_OUTOFMEMORY
;
311 hr
= IStream_Read(stream
, tempdata
, andBytes
, &bytesread
);
313 if (SUCCEEDED(hr
) && bytesread
== andBytes
)
317 andStride
= andBytesPerRow
;
322 andStride
= -andBytesPerRow
;
323 andRow
= tempdata
+ (result
->height
-1)*andBytesPerRow
;
326 bitsRow
= result
->bits
;
327 for (y
=0; y
<result
->height
; y
++) {
328 BYTE
*andByte
=andRow
;
329 DWORD
*bitsPixel
=(DWORD
*)bitsRow
;
330 for (x
=0; x
<result
->width
; x
+=8) {
331 BYTE andVal
=*andByte
++;
332 pixel_set_trans(bitsPixel
++, andVal
>>7&1);
333 if (x
+1 < result
->width
) pixel_set_trans(bitsPixel
++, andVal
>>6&1);
334 if (x
+2 < result
->width
) pixel_set_trans(bitsPixel
++, andVal
>>5&1);
335 if (x
+3 < result
->width
) pixel_set_trans(bitsPixel
++, andVal
>>4&1);
336 if (x
+4 < result
->width
) pixel_set_trans(bitsPixel
++, andVal
>>3&1);
337 if (x
+5 < result
->width
) pixel_set_trans(bitsPixel
++, andVal
>>2&1);
338 if (x
+6 < result
->width
) pixel_set_trans(bitsPixel
++, andVal
>>1&1);
339 if (x
+7 < result
->width
) pixel_set_trans(bitsPixel
++, andVal
&1);
342 bitsRow
+= bitsStride
;
346 HeapFree(GetProcessHeap(), 0, tempdata
);
350 IWICBitmapDecoder_Release(decoder
);
356 static HRESULT
ReadIcoPng(IStream
*stream
, IcoFrameDecode
*result
)
358 IWICBitmapDecoder
*decoder
= NULL
;
359 IWICBitmapFrameDecode
*sourceFrame
= NULL
;
360 IWICBitmapSource
*sourceBitmap
= NULL
;
364 hr
= PngDecoder_CreateInstance(NULL
, &IID_IWICBitmapDecoder
, (void**)&decoder
);
367 hr
= IWICBitmapDecoder_Initialize(decoder
, stream
, WICDecodeMetadataCacheOnLoad
);
370 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &sourceFrame
);
373 hr
= WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA
, (IWICBitmapSource
*)sourceFrame
, &sourceBitmap
);
376 hr
= IWICBitmapFrameDecode_GetSize(sourceFrame
, &result
->width
, &result
->height
);
379 result
->bits
= HeapAlloc(GetProcessHeap(), 0, 4 * result
->width
* result
->height
);
380 if (result
->bits
== NULL
)
387 rect
.Width
= result
->width
;
388 rect
.Height
= result
->height
;
389 hr
= IWICBitmapSource_CopyPixels(sourceBitmap
, &rect
, 4*result
->width
,
390 4*result
->width
*result
->height
, result
->bits
);
394 IWICBitmapDecoder_Release(decoder
);
395 if (sourceFrame
!= NULL
)
396 IWICBitmapFrameDecode_Release(sourceFrame
);
397 if (sourceBitmap
!= NULL
)
398 IWICBitmapSource_Release(sourceBitmap
);
402 static HRESULT WINAPI
IcoDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
405 IcoDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
406 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
408 if (!ppv
) return E_INVALIDARG
;
410 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
417 return E_NOINTERFACE
;
420 IUnknown_AddRef((IUnknown
*)*ppv
);
424 static ULONG WINAPI
IcoDecoder_AddRef(IWICBitmapDecoder
*iface
)
426 IcoDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
427 ULONG ref
= InterlockedIncrement(&This
->ref
);
429 TRACE("(%p) refcount=%u\n", iface
, ref
);
434 static ULONG WINAPI
IcoDecoder_Release(IWICBitmapDecoder
*iface
)
436 IcoDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
437 ULONG ref
= InterlockedDecrement(&This
->ref
);
439 TRACE("(%p) refcount=%u\n", iface
, ref
);
443 This
->lock
.DebugInfo
->Spare
[0] = 0;
444 DeleteCriticalSection(&This
->lock
);
445 if (This
->stream
) IStream_Release(This
->stream
);
446 HeapFree(GetProcessHeap(), 0, This
);
452 static HRESULT WINAPI
IcoDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
453 DWORD
*pdwCapability
)
455 FIXME("(%p,%p,%p): stub\n", iface
, pIStream
, pdwCapability
);
459 static HRESULT WINAPI
IcoDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
460 WICDecodeOptions cacheOptions
)
462 IcoDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
466 TRACE("(%p,%p,%x)\n", iface
, pIStream
, cacheOptions
);
468 EnterCriticalSection(&This
->lock
);
470 if (This
->initialized
)
472 hr
= WINCODEC_ERR_WRONGSTATE
;
477 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
478 if (FAILED(hr
)) goto end
;
480 hr
= IStream_Read(pIStream
, &This
->header
, sizeof(ICONHEADER
), &bytesread
);
481 if (FAILED(hr
)) goto end
;
482 if (bytesread
!= sizeof(ICONHEADER
) ||
483 This
->header
.idReserved
!= 0 ||
484 This
->header
.idType
!= 1)
490 This
->initialized
= TRUE
;
491 This
->stream
= pIStream
;
492 IStream_AddRef(pIStream
);
496 LeaveCriticalSection(&This
->lock
);
501 static HRESULT WINAPI
IcoDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
502 GUID
*pguidContainerFormat
)
504 FIXME("(%p,%p): stub\n", iface
, pguidContainerFormat
);
508 static HRESULT WINAPI
IcoDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
509 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
511 FIXME("(%p,%p): stub\n", iface
, ppIDecoderInfo
);
515 static HRESULT WINAPI
IcoDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
516 IWICPalette
*pIPalette
)
518 TRACE("(%p,%p)\n", iface
, pIPalette
);
519 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
522 static HRESULT WINAPI
IcoDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
523 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
525 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
526 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
529 static HRESULT WINAPI
IcoDecoder_GetPreview(IWICBitmapDecoder
*iface
,
530 IWICBitmapSource
**ppIBitmapSource
)
532 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
533 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
536 static HRESULT WINAPI
IcoDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
537 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
539 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
540 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
543 static HRESULT WINAPI
IcoDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
544 IWICBitmapSource
**ppIThumbnail
)
546 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
547 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
550 static HRESULT WINAPI
IcoDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
553 IcoDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
554 TRACE("(%p,%p)\n", iface
, pCount
);
556 if (!This
->initialized
) return WINCODEC_ERR_NOTINITIALIZED
;
558 *pCount
= This
->header
.idCount
;
559 TRACE("<-- %u\n", *pCount
);
564 static HRESULT WINAPI
IcoDecoder_GetFrame(IWICBitmapDecoder
*iface
,
565 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
567 IcoDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
568 IcoFrameDecode
*result
=NULL
;
570 ULARGE_INTEGER offset
, length
;
574 IWICStream
*substream
=NULL
;
576 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
578 EnterCriticalSection(&This
->lock
);
580 if (!This
->initialized
)
582 hr
= WINCODEC_ERR_NOTINITIALIZED
;
586 if (This
->header
.idCount
< index
)
592 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(IcoFrameDecode
));
599 result
->IWICBitmapFrameDecode_iface
.lpVtbl
= &IcoFrameDecode_Vtbl
;
603 /* read the icon entry */
604 seek
.QuadPart
= sizeof(ICONHEADER
) + sizeof(ICONDIRENTRY
) * index
;
605 hr
= IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, 0);
606 if (FAILED(hr
)) goto fail
;
608 hr
= IStream_Read(This
->stream
, &entry
, sizeof(ICONDIRENTRY
), &bytesread
);
609 if (FAILED(hr
) || bytesread
!= sizeof(ICONDIRENTRY
)) goto fail
;
611 /* create a stream object for this icon */
612 hr
= StreamImpl_Create(&substream
);
613 if (FAILED(hr
)) goto fail
;
615 offset
.QuadPart
= entry
.dwDIBOffset
;
616 length
.QuadPart
= entry
.dwDIBSize
;
617 hr
= IWICStream_InitializeFromIStreamRegion(substream
, This
->stream
, offset
, length
);
618 if (FAILED(hr
)) goto fail
;
620 /* read the bitmapinfo size or magic number */
621 hr
= IWICStream_Read(substream
, &magic
, sizeof(magic
), &bytesread
);
622 if (FAILED(hr
) || bytesread
!= sizeof(magic
)) goto fail
;
624 /* forward to the appropriate decoding function based on the magic number */
627 case sizeof(BITMAPCOREHEADER
):
628 case 64: /* sizeof(BITMAPCOREHEADER2) */
629 case sizeof(BITMAPINFOHEADER
):
630 case sizeof(BITMAPV4HEADER
):
631 case sizeof(BITMAPV5HEADER
):
632 hr
= ReadIcoDib((IStream
*)substream
, result
);
635 hr
= ReadIcoPng((IStream
*)substream
, result
);
638 FIXME("Unrecognized ICO frame magic: %x\n", magic
);
642 if (FAILED(hr
)) goto fail
;
644 *ppIBitmapFrame
= (IWICBitmapFrameDecode
*)result
;
646 LeaveCriticalSection(&This
->lock
);
651 LeaveCriticalSection(&This
->lock
);
652 HeapFree(GetProcessHeap(), 0, result
);
653 if (substream
) IStream_Release(substream
);
654 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
655 TRACE("<-- %x\n", hr
);
659 static const IWICBitmapDecoderVtbl IcoDecoder_Vtbl
= {
660 IcoDecoder_QueryInterface
,
663 IcoDecoder_QueryCapability
,
664 IcoDecoder_Initialize
,
665 IcoDecoder_GetContainerFormat
,
666 IcoDecoder_GetDecoderInfo
,
667 IcoDecoder_CopyPalette
,
668 IcoDecoder_GetMetadataQueryReader
,
669 IcoDecoder_GetPreview
,
670 IcoDecoder_GetColorContexts
,
671 IcoDecoder_GetThumbnail
,
672 IcoDecoder_GetFrameCount
,
676 HRESULT
IcoDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
681 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
685 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
687 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(IcoDecoder
));
688 if (!This
) return E_OUTOFMEMORY
;
690 This
->IWICBitmapDecoder_iface
.lpVtbl
= &IcoDecoder_Vtbl
;
693 This
->initialized
= FALSE
;
694 InitializeCriticalSection(&This
->lock
);
695 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": IcoDecoder.lock");
697 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
698 IUnknown_Release((IUnknown
*)This
);