2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wincodecs_private.h"
23 IWICComponentFactory IWICComponentFactory_iface
;
27 static inline ComponentFactory
*impl_from_IWICComponentFactory(IWICComponentFactory
*iface
)
29 return CONTAINING_RECORD(iface
, ComponentFactory
, IWICComponentFactory_iface
);
32 static HRESULT WINAPI
ComponentFactory_QueryInterface(IWICComponentFactory
*iface
, REFIID iid
,
35 ComponentFactory
*This
= impl_from_IWICComponentFactory(iface
);
36 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
38 if (!ppv
) return E_INVALIDARG
;
40 if (IsEqualIID(&IID_IUnknown
, iid
) ||
41 IsEqualIID(&IID_IWICImagingFactory
, iid
) ||
42 IsEqualIID(&IID_IWICComponentFactory
, iid
))
44 *ppv
= &This
->IWICComponentFactory_iface
;
52 IUnknown_AddRef((IUnknown
*)*ppv
);
56 static ULONG WINAPI
ComponentFactory_AddRef(IWICComponentFactory
*iface
)
58 ComponentFactory
*This
= impl_from_IWICComponentFactory(iface
);
59 ULONG ref
= InterlockedIncrement(&This
->ref
);
61 TRACE("(%p) refcount=%u\n", iface
, ref
);
66 static ULONG WINAPI
ComponentFactory_Release(IWICComponentFactory
*iface
)
68 ComponentFactory
*This
= impl_from_IWICComponentFactory(iface
);
69 ULONG ref
= InterlockedDecrement(&This
->ref
);
71 TRACE("(%p) refcount=%u\n", iface
, ref
);
74 HeapFree(GetProcessHeap(), 0, This
);
79 static HRESULT WINAPI
ComponentFactory_CreateDecoderFromFilename(
80 IWICComponentFactory
*iface
, LPCWSTR wzFilename
, const GUID
*pguidVendor
,
81 DWORD dwDesiredAccess
, WICDecodeOptions metadataOptions
,
82 IWICBitmapDecoder
**ppIDecoder
)
87 TRACE("(%p,%s,%s,%u,%u,%p)\n", iface
, debugstr_w(wzFilename
),
88 debugstr_guid(pguidVendor
), dwDesiredAccess
, metadataOptions
, ppIDecoder
);
90 hr
= StreamImpl_Create(&stream
);
93 hr
= IWICStream_InitializeFromFilename(stream
, wzFilename
, dwDesiredAccess
);
97 hr
= IWICComponentFactory_CreateDecoderFromStream(iface
, (IStream
*)stream
,
98 pguidVendor
, metadataOptions
, ppIDecoder
);
101 IWICStream_Release(stream
);
107 static HRESULT
find_decoder(IStream
*pIStream
, const GUID
*pguidVendor
,
108 WICDecodeOptions metadataOptions
, IWICBitmapDecoder
**decoder
)
110 IEnumUnknown
*enumdecoders
;
111 IUnknown
*unkdecoderinfo
;
112 IWICBitmapDecoderInfo
*decoderinfo
;
120 res
= CreateComponentEnumerator(WICDecoder
, WICComponentEnumerateDefault
, &enumdecoders
);
121 if (FAILED(res
)) return res
;
125 res
= IEnumUnknown_Next(enumdecoders
, 1, &unkdecoderinfo
, &num_fetched
);
129 res
= IUnknown_QueryInterface(unkdecoderinfo
, &IID_IWICBitmapDecoderInfo
, (void**)&decoderinfo
);
135 res
= IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo
, &vendor
);
136 if (FAILED(res
) || !IsEqualIID(&vendor
, pguidVendor
))
138 IWICBitmapDecoderInfo_Release(decoderinfo
);
139 IUnknown_Release(unkdecoderinfo
);
144 res
= IWICBitmapDecoderInfo_MatchesPattern(decoderinfo
, pIStream
, &matches
);
146 if (SUCCEEDED(res
) && matches
)
148 res
= IWICBitmapDecoderInfo_CreateInstance(decoderinfo
, decoder
);
150 /* FIXME: should use QueryCapability to choose a decoder */
154 res
= IWICBitmapDecoder_Initialize(*decoder
, pIStream
, metadataOptions
);
158 IWICBitmapDecoder_Release(*decoder
);
159 IWICBitmapDecoderInfo_Release(decoderinfo
);
160 IUnknown_Release(unkdecoderinfo
);
167 IWICBitmapDecoderInfo_Release(decoderinfo
);
170 IUnknown_Release(unkdecoderinfo
);
176 IEnumUnknown_Release(enumdecoders
);
178 return WINCODEC_ERR_COMPONENTNOTFOUND
;
181 static HRESULT WINAPI
ComponentFactory_CreateDecoderFromStream(
182 IWICComponentFactory
*iface
, IStream
*pIStream
, const GUID
*pguidVendor
,
183 WICDecodeOptions metadataOptions
, IWICBitmapDecoder
**ppIDecoder
)
186 IWICBitmapDecoder
*decoder
= NULL
;
188 TRACE("(%p,%p,%s,%u,%p)\n", iface
, pIStream
, debugstr_guid(pguidVendor
),
189 metadataOptions
, ppIDecoder
);
192 res
= find_decoder(pIStream
, pguidVendor
, metadataOptions
, &decoder
);
194 res
= find_decoder(pIStream
, NULL
, metadataOptions
, &decoder
);
198 *ppIDecoder
= decoder
;
203 if (WARN_ON(wincodecs
))
209 WARN("failed to load from a stream %#x\n", res
);
212 if (IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
) == S_OK
)
214 if (IStream_Read(pIStream
, data
, 4, &bytesread
) == S_OK
)
215 WARN("first %i bytes of stream=%x %x %x %x\n", bytesread
, data
[0], data
[1], data
[2], data
[3]);
223 static HRESULT WINAPI
ComponentFactory_CreateDecoderFromFileHandle(
224 IWICComponentFactory
*iface
, ULONG_PTR hFile
, const GUID
*pguidVendor
,
225 WICDecodeOptions metadataOptions
, IWICBitmapDecoder
**ppIDecoder
)
230 TRACE("(%p,%lx,%s,%u,%p)\n", iface
, hFile
, debugstr_guid(pguidVendor
),
231 metadataOptions
, ppIDecoder
);
233 hr
= StreamImpl_Create(&stream
);
236 hr
= stream_initialize_from_filehandle(stream
, (HANDLE
)hFile
);
239 hr
= IWICComponentFactory_CreateDecoderFromStream(iface
, (IStream
*)stream
,
240 pguidVendor
, metadataOptions
, ppIDecoder
);
242 IWICStream_Release(stream
);
247 static HRESULT WINAPI
ComponentFactory_CreateComponentInfo(IWICComponentFactory
*iface
,
248 REFCLSID clsidComponent
, IWICComponentInfo
**ppIInfo
)
250 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(clsidComponent
), ppIInfo
);
251 return CreateComponentInfo(clsidComponent
, ppIInfo
);
254 static HRESULT WINAPI
ComponentFactory_CreateDecoder(IWICComponentFactory
*iface
,
255 REFGUID guidContainerFormat
, const GUID
*pguidVendor
,
256 IWICBitmapDecoder
**ppIDecoder
)
258 IEnumUnknown
*enumdecoders
;
259 IUnknown
*unkdecoderinfo
;
260 IWICBitmapDecoderInfo
*decoderinfo
;
261 IWICBitmapDecoder
*decoder
= NULL
, *preferred_decoder
= NULL
;
266 TRACE("(%p,%s,%s,%p)\n", iface
, debugstr_guid(guidContainerFormat
),
267 debugstr_guid(pguidVendor
), ppIDecoder
);
269 if (!guidContainerFormat
|| !ppIDecoder
) return E_INVALIDARG
;
271 res
= CreateComponentEnumerator(WICDecoder
, WICComponentEnumerateDefault
, &enumdecoders
);
272 if (FAILED(res
)) return res
;
274 while (!preferred_decoder
)
276 res
= IEnumUnknown_Next(enumdecoders
, 1, &unkdecoderinfo
, &num_fetched
);
277 if (res
!= S_OK
) break;
279 res
= IUnknown_QueryInterface(unkdecoderinfo
, &IID_IWICBitmapDecoderInfo
, (void **)&decoderinfo
);
284 res
= IWICBitmapDecoderInfo_GetContainerFormat(decoderinfo
, &container_guid
);
285 if (SUCCEEDED(res
) && IsEqualIID(&container_guid
, guidContainerFormat
))
287 IWICBitmapDecoder
*new_decoder
;
289 res
= IWICBitmapDecoderInfo_CreateInstance(decoderinfo
, &new_decoder
);
294 res
= IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo
, &vendor
);
295 if (SUCCEEDED(res
) && IsEqualIID(&vendor
, pguidVendor
))
297 preferred_decoder
= new_decoder
;
302 if (new_decoder
&& !decoder
)
304 decoder
= new_decoder
;
308 if (new_decoder
) IWICBitmapDecoder_Release(new_decoder
);
312 IWICBitmapDecoderInfo_Release(decoderinfo
);
315 IUnknown_Release(unkdecoderinfo
);
318 IEnumUnknown_Release(enumdecoders
);
320 if (preferred_decoder
)
322 *ppIDecoder
= preferred_decoder
;
323 if (decoder
) IWICBitmapDecoder_Release(decoder
);
329 *ppIDecoder
= decoder
;
334 return WINCODEC_ERR_COMPONENTNOTFOUND
;
337 static HRESULT WINAPI
ComponentFactory_CreateEncoder(IWICComponentFactory
*iface
,
338 REFGUID guidContainerFormat
, const GUID
*pguidVendor
,
339 IWICBitmapEncoder
**ppIEncoder
)
342 IEnumUnknown
*enumencoders
;
343 IUnknown
*unkencoderinfo
;
344 IWICBitmapEncoderInfo
*encoderinfo
;
345 IWICBitmapEncoder
*encoder
=NULL
;
348 GUID actual_containerformat
;
350 TRACE("(%p,%s,%s,%p)\n", iface
, debugstr_guid(guidContainerFormat
),
351 debugstr_guid(pguidVendor
), ppIEncoder
);
353 if (pguidVendor
&& !fixme
++)
354 FIXME("ignoring vendor GUID\n");
356 res
= CreateComponentEnumerator(WICEncoder
, WICComponentEnumerateDefault
, &enumencoders
);
357 if (FAILED(res
)) return res
;
361 res
= IEnumUnknown_Next(enumencoders
, 1, &unkencoderinfo
, &num_fetched
);
365 res
= IUnknown_QueryInterface(unkencoderinfo
, &IID_IWICBitmapEncoderInfo
, (void**)&encoderinfo
);
369 res
= IWICBitmapEncoderInfo_GetContainerFormat(encoderinfo
, &actual_containerformat
);
371 if (SUCCEEDED(res
) && IsEqualGUID(guidContainerFormat
, &actual_containerformat
))
373 res
= IWICBitmapEncoderInfo_CreateInstance(encoderinfo
, &encoder
);
378 IWICBitmapEncoderInfo_Release(encoderinfo
);
381 IUnknown_Release(unkencoderinfo
);
387 IEnumUnknown_Release(enumencoders
);
391 *ppIEncoder
= encoder
;
396 WARN("failed to create encoder\n");
398 return WINCODEC_ERR_COMPONENTNOTFOUND
;
402 static HRESULT WINAPI
ComponentFactory_CreatePalette(IWICComponentFactory
*iface
,
403 IWICPalette
**ppIPalette
)
405 TRACE("(%p,%p)\n", iface
, ppIPalette
);
406 return PaletteImpl_Create(ppIPalette
);
409 static HRESULT WINAPI
ComponentFactory_CreateFormatConverter(IWICComponentFactory
*iface
,
410 IWICFormatConverter
**ppIFormatConverter
)
412 return FormatConverter_CreateInstance(&IID_IWICFormatConverter
, (void**)ppIFormatConverter
);
415 static HRESULT WINAPI
ComponentFactory_CreateBitmapScaler(IWICComponentFactory
*iface
,
416 IWICBitmapScaler
**ppIBitmapScaler
)
418 TRACE("(%p,%p)\n", iface
, ppIBitmapScaler
);
420 return BitmapScaler_Create(ppIBitmapScaler
);
423 static HRESULT WINAPI
ComponentFactory_CreateBitmapClipper(IWICComponentFactory
*iface
,
424 IWICBitmapClipper
**ppIBitmapClipper
)
426 TRACE("(%p,%p)\n", iface
, ppIBitmapClipper
);
427 return BitmapClipper_Create(ppIBitmapClipper
);
430 static HRESULT WINAPI
ComponentFactory_CreateBitmapFlipRotator(IWICComponentFactory
*iface
,
431 IWICBitmapFlipRotator
**ppIBitmapFlipRotator
)
433 TRACE("(%p,%p)\n", iface
, ppIBitmapFlipRotator
);
434 return FlipRotator_Create(ppIBitmapFlipRotator
);
437 static HRESULT WINAPI
ComponentFactory_CreateStream(IWICComponentFactory
*iface
,
438 IWICStream
**ppIWICStream
)
440 TRACE("(%p,%p)\n", iface
, ppIWICStream
);
441 return StreamImpl_Create(ppIWICStream
);
444 static HRESULT WINAPI
ComponentFactory_CreateColorContext(IWICComponentFactory
*iface
,
445 IWICColorContext
**ppIColorContext
)
447 TRACE("(%p,%p)\n", iface
, ppIColorContext
);
448 return ColorContext_Create(ppIColorContext
);
451 static HRESULT WINAPI
ComponentFactory_CreateColorTransformer(IWICComponentFactory
*iface
,
452 IWICColorTransform
**ppIColorTransform
)
454 TRACE("(%p,%p)\n", iface
, ppIColorTransform
);
455 return ColorTransform_Create(ppIColorTransform
);
458 static HRESULT WINAPI
ComponentFactory_CreateBitmap(IWICComponentFactory
*iface
,
459 UINT uiWidth
, UINT uiHeight
, REFWICPixelFormatGUID pixelFormat
,
460 WICBitmapCreateCacheOption option
, IWICBitmap
**ppIBitmap
)
462 TRACE("(%p,%u,%u,%s,%u,%p)\n", iface
, uiWidth
, uiHeight
,
463 debugstr_guid(pixelFormat
), option
, ppIBitmap
);
464 return BitmapImpl_Create(uiWidth
, uiHeight
, 0, 0, NULL
, pixelFormat
, option
, ppIBitmap
);
467 static HRESULT WINAPI
ComponentFactory_CreateBitmapFromSource(IWICComponentFactory
*iface
,
468 IWICBitmapSource
*piBitmapSource
, WICBitmapCreateCacheOption option
,
469 IWICBitmap
**ppIBitmap
)
472 IWICBitmapLock
*lock
;
473 IWICPalette
*palette
;
475 WICPixelFormatGUID pixelformat
= {0};
479 IWICComponentInfo
*info
;
480 IWICPixelFormatInfo2
*formatinfo
;
481 WICPixelFormatNumericRepresentation format_type
;
483 TRACE("(%p,%p,%u,%p)\n", iface
, piBitmapSource
, option
, ppIBitmap
);
485 if (!piBitmapSource
|| !ppIBitmap
)
488 hr
= IWICBitmapSource_GetSize(piBitmapSource
, &width
, &height
);
491 hr
= IWICBitmapSource_GetPixelFormat(piBitmapSource
, &pixelformat
);
494 hr
= CreateComponentInfo(&pixelformat
, &info
);
498 hr
= IWICComponentInfo_QueryInterface(info
, &IID_IWICPixelFormatInfo2
, (void**)&formatinfo
);
502 hr
= IWICPixelFormatInfo2_GetNumericRepresentation(formatinfo
, &format_type
);
504 IWICPixelFormatInfo2_Release(formatinfo
);
507 IWICComponentInfo_Release(info
);
511 hr
= BitmapImpl_Create(width
, height
, 0, 0, NULL
, &pixelformat
, option
, &result
);
515 hr
= IWICBitmap_Lock(result
, NULL
, WICBitmapLockWrite
, &lock
);
518 UINT stride
, buffersize
;
524 hr
= IWICBitmapLock_GetStride(lock
, &stride
);
527 hr
= IWICBitmapLock_GetDataPointer(lock
, &buffersize
, &buffer
);
530 hr
= IWICBitmapSource_CopyPixels(piBitmapSource
, &rc
, stride
,
533 IWICBitmapLock_Release(lock
);
536 if (SUCCEEDED(hr
) && (format_type
== WICPixelFormatNumericRepresentationUnspecified
||
537 format_type
== WICPixelFormatNumericRepresentationIndexed
))
539 hr
= PaletteImpl_Create(&palette
);
543 hr
= IWICBitmapSource_CopyPalette(piBitmapSource
, palette
);
546 hr
= IWICBitmap_SetPalette(result
, palette
);
550 IWICPalette_Release(palette
);
556 hr
= IWICBitmapSource_GetResolution(piBitmapSource
, &dpix
, &dpiy
);
559 hr
= IWICBitmap_SetResolution(result
, dpix
, dpiy
);
567 IWICBitmap_Release(result
);
573 static HRESULT WINAPI
ComponentFactory_CreateBitmapFromSourceRect(IWICComponentFactory
*iface
,
574 IWICBitmapSource
*piBitmapSource
, UINT x
, UINT y
, UINT width
, UINT height
,
575 IWICBitmap
**ppIBitmap
)
577 FIXME("(%p,%p,%u,%u,%u,%u,%p): stub\n", iface
, piBitmapSource
, x
, y
, width
,
582 static HRESULT WINAPI
ComponentFactory_CreateBitmapFromMemory(IWICComponentFactory
*iface
,
583 UINT width
, UINT height
, REFWICPixelFormatGUID format
, UINT stride
,
584 UINT size
, BYTE
*buffer
, IWICBitmap
**bitmap
)
588 TRACE("(%p,%u,%u,%s,%u,%u,%p,%p\n", iface
, width
, height
,
589 debugstr_guid(format
), stride
, size
, buffer
, bitmap
);
591 if (!stride
|| !size
|| !buffer
|| !bitmap
) return E_INVALIDARG
;
593 hr
= BitmapImpl_Create(width
, height
, stride
, size
, NULL
, format
, WICBitmapCacheOnLoad
, bitmap
);
596 IWICBitmapLock
*lock
;
598 hr
= IWICBitmap_Lock(*bitmap
, NULL
, WICBitmapLockWrite
, &lock
);
604 IWICBitmapLock_GetDataPointer(lock
, &buffersize
, &data
);
605 memcpy(data
, buffer
, buffersize
);
607 IWICBitmapLock_Release(lock
);
611 IWICBitmap_Release(*bitmap
);
618 static BOOL
get_16bpp_format(HBITMAP hbm
, WICPixelFormatGUID
*format
)
624 hdc
= CreateCompatibleDC(0);
626 memset(&bmh
, 0, sizeof(bmh
));
627 bmh
.bV4Size
= sizeof(bmh
);
630 bmh
.bV4V4Compression
= BI_BITFIELDS
;
631 bmh
.bV4BitCount
= 16;
633 GetDIBits(hdc
, hbm
, 0, 0, NULL
, (BITMAPINFO
*)&bmh
, DIB_RGB_COLORS
);
635 if (bmh
.bV4RedMask
== 0x7c00 &&
636 bmh
.bV4GreenMask
== 0x3e0 &&
637 bmh
.bV4BlueMask
== 0x1f)
639 *format
= GUID_WICPixelFormat16bppBGR555
;
641 else if (bmh
.bV4RedMask
== 0xf800 &&
642 bmh
.bV4GreenMask
== 0x7e0 &&
643 bmh
.bV4BlueMask
== 0x1f)
645 *format
= GUID_WICPixelFormat16bppBGR565
;
649 FIXME("unrecognized bitfields %x,%x,%x\n", bmh
.bV4RedMask
,
650 bmh
.bV4GreenMask
, bmh
.bV4BlueMask
);
658 static HRESULT WINAPI
ComponentFactory_CreateBitmapFromHBITMAP(IWICComponentFactory
*iface
,
659 HBITMAP hbm
, HPALETTE hpal
, WICBitmapAlphaChannelOption option
, IWICBitmap
**bitmap
)
663 WICPixelFormatGUID format
;
664 IWICBitmapLock
*lock
;
665 UINT size
, num_palette_entries
= 0;
666 PALETTEENTRY entry
[256];
668 TRACE("(%p,%p,%p,%u,%p)\n", iface
, hbm
, hpal
, option
, bitmap
);
670 if (!bitmap
) return E_INVALIDARG
;
672 if (GetObjectW(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
673 return WINCODEC_ERR_WIN32ERROR
;
677 num_palette_entries
= GetPaletteEntries(hpal
, 0, 256, entry
);
678 if (!num_palette_entries
)
679 return WINCODEC_ERR_WIN32ERROR
;
682 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
683 switch(bm
.bmBitsPixel
)
686 format
= GUID_WICPixelFormat1bppIndexed
;
689 format
= GUID_WICPixelFormat4bppIndexed
;
692 format
= GUID_WICPixelFormat8bppIndexed
;
695 if (!get_16bpp_format(hbm
, &format
))
699 format
= GUID_WICPixelFormat24bppBGR
;
704 case WICBitmapUseAlpha
:
705 format
= GUID_WICPixelFormat32bppBGRA
;
707 case WICBitmapUsePremultipliedAlpha
:
708 format
= GUID_WICPixelFormat32bppPBGRA
;
710 case WICBitmapIgnoreAlpha
:
711 format
= GUID_WICPixelFormat32bppBGR
;
718 format
= GUID_WICPixelFormat48bppRGB
;
721 FIXME("unsupported %d bpp\n", bm
.bmBitsPixel
);
725 hr
= BitmapImpl_Create(bm
.bmWidth
, bm
.bmHeight
, bm
.bmWidthBytes
, 0, NULL
, &format
, WICBitmapCacheOnLoad
, bitmap
);
726 if (hr
!= S_OK
) return hr
;
728 hr
= IWICBitmap_Lock(*bitmap
, NULL
, WICBitmapLockWrite
, &lock
);
733 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
) + 256 * sizeof(RGBQUAD
)];
734 BITMAPINFO
*bmi
= (BITMAPINFO
*)bmibuf
;
736 IWICBitmapLock_GetDataPointer(lock
, &size
, &buffer
);
738 hdc
= CreateCompatibleDC(0);
740 bmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
741 bmi
->bmiHeader
.biBitCount
= 0;
742 GetDIBits(hdc
, hbm
, 0, 0, NULL
, bmi
, DIB_RGB_COLORS
);
743 bmi
->bmiHeader
.biHeight
= -bm
.bmHeight
;
744 GetDIBits(hdc
, hbm
, 0, bm
.bmHeight
, buffer
, bmi
, DIB_RGB_COLORS
);
747 IWICBitmapLock_Release(lock
);
749 if (num_palette_entries
)
751 IWICPalette
*palette
;
752 WICColor colors
[256];
755 hr
= PaletteImpl_Create(&palette
);
758 for (i
= 0; i
< num_palette_entries
; i
++)
759 colors
[i
] = 0xff000000 | entry
[i
].peRed
<< 16 |
760 entry
[i
].peGreen
<< 8 | entry
[i
].peBlue
;
762 hr
= IWICPalette_InitializeCustom(palette
, colors
, num_palette_entries
);
764 hr
= IWICBitmap_SetPalette(*bitmap
, palette
);
766 IWICPalette_Release(palette
);
773 IWICBitmap_Release(*bitmap
);
780 static HRESULT WINAPI
ComponentFactory_CreateBitmapFromHICON(IWICComponentFactory
*iface
,
781 HICON hicon
, IWICBitmap
**bitmap
)
783 IWICBitmapLock
*lock
;
786 int width
, height
, x
, y
;
795 TRACE("(%p,%p,%p)\n", iface
, hicon
, bitmap
);
797 if (!bitmap
) return E_INVALIDARG
;
799 if (!GetIconInfo(hicon
, &info
))
800 return HRESULT_FROM_WIN32(GetLastError());
802 GetObjectW(info
.hbmColor
? info
.hbmColor
: info
.hbmMask
, sizeof(bm
), &bm
);
805 height
= info
.hbmColor
? abs(bm
.bmHeight
) : abs(bm
.bmHeight
) / 2;
807 size
= stride
* height
;
809 hr
= BitmapImpl_Create(width
, height
, stride
, size
, NULL
,
810 &GUID_WICPixelFormat32bppBGRA
, WICBitmapCacheOnLoad
, bitmap
);
811 if (hr
!= S_OK
) goto failed
;
813 hr
= IWICBitmap_Lock(*bitmap
, NULL
, WICBitmapLockWrite
, &lock
);
816 IWICBitmap_Release(*bitmap
);
819 IWICBitmapLock_GetDataPointer(lock
, &size
, &buffer
);
821 hdc
= CreateCompatibleDC(0);
823 memset(&bi
, 0, sizeof(bi
));
824 bi
.bmiHeader
.biSize
= sizeof(bi
.bmiHeader
);
825 bi
.bmiHeader
.biWidth
= width
;
826 bi
.bmiHeader
.biHeight
= info
.hbmColor
? -height
: -height
* 2;
827 bi
.bmiHeader
.biPlanes
= 1;
828 bi
.bmiHeader
.biBitCount
= 32;
829 bi
.bmiHeader
.biCompression
= BI_RGB
;
835 GetDIBits(hdc
, info
.hbmColor
, 0, height
, buffer
, &bi
, DIB_RGB_COLORS
);
837 if (bm
.bmBitsPixel
== 32)
839 /* If any pixel has a non-zero alpha, ignore hbmMask */
840 bits
= (DWORD
*)buffer
;
841 for (x
= 0; x
< width
&& !has_alpha
; x
++, bits
++)
843 for (y
= 0; y
< height
; y
++)
845 if (*bits
& 0xff000000)
855 GetDIBits(hdc
, info
.hbmMask
, 0, height
, buffer
, &bi
, DIB_RGB_COLORS
);
865 mask
= HeapAlloc(GetProcessHeap(), 0, size
);
868 IWICBitmapLock_Release(lock
);
869 IWICBitmap_Release(*bitmap
);
875 /* read alpha data from the mask */
876 GetDIBits(hdc
, info
.hbmMask
, info
.hbmColor
? 0 : height
, height
, mask
, &bi
, DIB_RGB_COLORS
);
878 for (y
= 0; y
< height
; y
++)
880 rgba
= (DWORD
*)(buffer
+ y
* stride
);
881 bits
= (DWORD
*)(mask
+ y
* stride
);
883 for (x
= 0; x
< width
; x
++, rgba
++, bits
++)
892 HeapFree(GetProcessHeap(), 0, mask
);
896 /* set constant alpha of 255 */
897 for (y
= 0; y
< height
; y
++)
899 rgba
= (DWORD
*)(buffer
+ y
* stride
);
900 for (x
= 0; x
< width
; x
++, rgba
++)
907 IWICBitmapLock_Release(lock
);
911 DeleteObject(info
.hbmColor
);
912 DeleteObject(info
.hbmMask
);
917 static HRESULT WINAPI
ComponentFactory_CreateComponentEnumerator(IWICComponentFactory
*iface
,
918 DWORD componentTypes
, DWORD options
, IEnumUnknown
**ppIEnumUnknown
)
920 TRACE("(%p,%u,%u,%p)\n", iface
, componentTypes
, options
, ppIEnumUnknown
);
921 return CreateComponentEnumerator(componentTypes
, options
, ppIEnumUnknown
);
924 static HRESULT WINAPI
ComponentFactory_CreateFastMetadataEncoderFromDecoder(
925 IWICComponentFactory
*iface
, IWICBitmapDecoder
*pIDecoder
,
926 IWICFastMetadataEncoder
**ppIFastEncoder
)
928 FIXME("(%p,%p,%p): stub\n", iface
, pIDecoder
, ppIFastEncoder
);
932 static HRESULT WINAPI
ComponentFactory_CreateFastMetadataEncoderFromFrameDecode(
933 IWICComponentFactory
*iface
, IWICBitmapFrameDecode
*pIFrameDecoder
,
934 IWICFastMetadataEncoder
**ppIFastEncoder
)
936 FIXME("(%p,%p,%p): stub\n", iface
, pIFrameDecoder
, ppIFastEncoder
);
940 static HRESULT WINAPI
ComponentFactory_CreateQueryWriter(IWICComponentFactory
*iface
,
941 REFGUID guidMetadataFormat
, const GUID
*pguidVendor
,
942 IWICMetadataQueryWriter
**ppIQueryWriter
)
944 FIXME("(%p,%s,%s,%p): stub\n", iface
, debugstr_guid(guidMetadataFormat
),
945 debugstr_guid(pguidVendor
), ppIQueryWriter
);
949 static HRESULT WINAPI
ComponentFactory_CreateQueryWriterFromReader(IWICComponentFactory
*iface
,
950 IWICMetadataQueryReader
*pIQueryReader
, const GUID
*pguidVendor
,
951 IWICMetadataQueryWriter
**ppIQueryWriter
)
953 FIXME("(%p,%p,%s,%p): stub\n", iface
, pIQueryReader
, debugstr_guid(pguidVendor
),
958 static HRESULT WINAPI
ComponentFactory_CreateMetadataReader(IWICComponentFactory
*iface
,
959 REFGUID format
, const GUID
*vendor
, DWORD options
, IStream
*stream
, IWICMetadataReader
**reader
)
961 FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface
, debugstr_guid(format
), debugstr_guid(vendor
),
962 options
, stream
, reader
);
966 static HRESULT WINAPI
ComponentFactory_CreateMetadataReaderFromContainer(IWICComponentFactory
*iface
,
967 REFGUID format
, const GUID
*vendor
, DWORD options
, IStream
*stream
, IWICMetadataReader
**reader
)
970 IEnumUnknown
*enumreaders
;
971 IUnknown
*unkreaderinfo
;
972 IWICMetadataReaderInfo
*readerinfo
;
973 IWICPersistStream
*wicpersiststream
;
979 TRACE("%p,%s,%s,%x,%p,%p\n", iface
, debugstr_guid(format
), debugstr_guid(vendor
),
980 options
, stream
, reader
);
982 if (!format
|| !stream
|| !reader
)
987 hr
= CreateComponentEnumerator(WICMetadataReader
, WICComponentEnumerateDefault
, &enumreaders
);
988 if (FAILED(hr
)) return hr
;
995 hr
= IEnumUnknown_Next(enumreaders
, 1, &unkreaderinfo
, &num_fetched
);
999 hr
= IUnknown_QueryInterface(unkreaderinfo
, &IID_IWICMetadataReaderInfo
, (void**)&readerinfo
);
1005 hr
= IWICMetadataReaderInfo_GetVendorGUID(readerinfo
, &decoder_vendor
);
1007 if (FAILED(hr
) || !IsEqualIID(vendor
, &decoder_vendor
))
1009 IWICMetadataReaderInfo_Release(readerinfo
);
1010 IUnknown_Release(unkreaderinfo
);
1015 hr
= IWICMetadataReaderInfo_MatchesPattern(readerinfo
, format
, stream
, &matches
);
1017 if (SUCCEEDED(hr
) && matches
)
1019 hr
= IStream_Seek(stream
, zero
, STREAM_SEEK_SET
, NULL
);
1022 hr
= IWICMetadataReaderInfo_CreateInstance(readerinfo
, reader
);
1026 hr
= IWICMetadataReader_QueryInterface(*reader
, &IID_IWICPersistStream
, (void**)&wicpersiststream
);
1030 hr
= IWICPersistStream_LoadEx(wicpersiststream
,
1031 stream
, vendor
, options
& WICPersistOptionMask
);
1033 IWICPersistStream_Release(wicpersiststream
);
1038 IWICMetadataReader_Release(*reader
);
1044 IUnknown_Release(readerinfo
);
1047 IUnknown_Release(unkreaderinfo
);
1053 if (!*reader
&& vendor
)
1056 IEnumUnknown_Reset(enumreaders
);
1060 IEnumUnknown_Release(enumreaders
);
1062 if (!*reader
&& !(options
& WICMetadataCreationFailUnknown
))
1064 hr
= IStream_Seek(stream
, zero
, STREAM_SEEK_SET
, NULL
);
1067 hr
= UnknownMetadataReader_CreateInstance(&IID_IWICMetadataReader
, (void**)reader
);
1071 hr
= IWICMetadataReader_QueryInterface(*reader
, &IID_IWICPersistStream
, (void**)&wicpersiststream
);
1075 hr
= IWICPersistStream_LoadEx(wicpersiststream
, stream
, NULL
, options
& WICPersistOptionMask
);
1077 IWICPersistStream_Release(wicpersiststream
);
1082 IWICMetadataReader_Release(*reader
);
1091 return WINCODEC_ERR_COMPONENTNOTFOUND
;
1094 static HRESULT WINAPI
ComponentFactory_CreateMetadataWriter(IWICComponentFactory
*iface
,
1095 REFGUID format
, const GUID
*vendor
, DWORD options
, IWICMetadataWriter
**writer
)
1097 FIXME("%p,%s,%s,%x,%p: stub\n", iface
, debugstr_guid(format
), debugstr_guid(vendor
), options
, writer
);
1101 static HRESULT WINAPI
ComponentFactory_CreateMetadataWriterFromReader(IWICComponentFactory
*iface
,
1102 IWICMetadataReader
*reader
, const GUID
*vendor
, IWICMetadataWriter
**writer
)
1104 FIXME("%p,%p,%s,%p: stub\n", iface
, reader
, debugstr_guid(vendor
), writer
);
1108 static HRESULT WINAPI
ComponentFactory_CreateQueryReaderFromBlockReader(IWICComponentFactory
*iface
,
1109 IWICMetadataBlockReader
*block_reader
, IWICMetadataQueryReader
**query_reader
)
1111 TRACE("%p,%p,%p\n", iface
, block_reader
, query_reader
);
1113 if (!block_reader
|| !query_reader
)
1114 return E_INVALIDARG
;
1116 return MetadataQueryReader_CreateInstance(block_reader
, query_reader
);
1119 static HRESULT WINAPI
ComponentFactory_CreateQueryWriterFromBlockWriter(IWICComponentFactory
*iface
,
1120 IWICMetadataBlockWriter
*block_writer
, IWICMetadataQueryWriter
**query_writer
)
1122 FIXME("%p,%p,%p: stub\n", iface
, block_writer
, query_writer
);
1126 static HRESULT WINAPI
ComponentFactory_CreateEncoderPropertyBag(IWICComponentFactory
*iface
,
1127 PROPBAG2
*options
, UINT count
, IPropertyBag2
**property
)
1129 TRACE("(%p,%p,%u,%p)\n", iface
, options
, count
, property
);
1130 return CreatePropertyBag2(options
, count
, property
);
1133 static const IWICComponentFactoryVtbl ComponentFactory_Vtbl
= {
1134 ComponentFactory_QueryInterface
,
1135 ComponentFactory_AddRef
,
1136 ComponentFactory_Release
,
1137 ComponentFactory_CreateDecoderFromFilename
,
1138 ComponentFactory_CreateDecoderFromStream
,
1139 ComponentFactory_CreateDecoderFromFileHandle
,
1140 ComponentFactory_CreateComponentInfo
,
1141 ComponentFactory_CreateDecoder
,
1142 ComponentFactory_CreateEncoder
,
1143 ComponentFactory_CreatePalette
,
1144 ComponentFactory_CreateFormatConverter
,
1145 ComponentFactory_CreateBitmapScaler
,
1146 ComponentFactory_CreateBitmapClipper
,
1147 ComponentFactory_CreateBitmapFlipRotator
,
1148 ComponentFactory_CreateStream
,
1149 ComponentFactory_CreateColorContext
,
1150 ComponentFactory_CreateColorTransformer
,
1151 ComponentFactory_CreateBitmap
,
1152 ComponentFactory_CreateBitmapFromSource
,
1153 ComponentFactory_CreateBitmapFromSourceRect
,
1154 ComponentFactory_CreateBitmapFromMemory
,
1155 ComponentFactory_CreateBitmapFromHBITMAP
,
1156 ComponentFactory_CreateBitmapFromHICON
,
1157 ComponentFactory_CreateComponentEnumerator
,
1158 ComponentFactory_CreateFastMetadataEncoderFromDecoder
,
1159 ComponentFactory_CreateFastMetadataEncoderFromFrameDecode
,
1160 ComponentFactory_CreateQueryWriter
,
1161 ComponentFactory_CreateQueryWriterFromReader
,
1162 ComponentFactory_CreateMetadataReader
,
1163 ComponentFactory_CreateMetadataReaderFromContainer
,
1164 ComponentFactory_CreateMetadataWriter
,
1165 ComponentFactory_CreateMetadataWriterFromReader
,
1166 ComponentFactory_CreateQueryReaderFromBlockReader
,
1167 ComponentFactory_CreateQueryWriterFromBlockWriter
,
1168 ComponentFactory_CreateEncoderPropertyBag
1171 HRESULT
ComponentFactory_CreateInstance(REFIID iid
, void** ppv
)
1173 ComponentFactory
*This
;
1176 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1180 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(ComponentFactory
));
1181 if (!This
) return E_OUTOFMEMORY
;
1183 This
->IWICComponentFactory_iface
.lpVtbl
= &ComponentFactory_Vtbl
;
1186 ret
= IWICComponentFactory_QueryInterface(&This
->IWICComponentFactory_iface
, iid
, ppv
);
1187 IWICComponentFactory_Release(&This
->IWICComponentFactory_iface
);
1192 HRESULT WINAPI
WICCreateBitmapFromSectionEx(UINT width
, UINT height
,
1193 REFWICPixelFormatGUID format
, HANDLE section
, UINT stride
,
1194 UINT offset
, WICSectionAccessLevel wicaccess
, IWICBitmap
**bitmap
)
1200 TRACE("%u,%u,%s,%p,%u,%#x,%#x,%p\n", width
, height
, debugstr_guid(format
),
1201 section
, stride
, offset
, wicaccess
, bitmap
);
1203 if (!width
|| !height
|| !section
|| !bitmap
) return E_INVALIDARG
;
1207 case WICSectionAccessLevelReadWrite
:
1208 access
= FILE_MAP_READ
| FILE_MAP_WRITE
;
1211 case WICSectionAccessLevelRead
:
1212 access
= FILE_MAP_READ
;
1216 FIXME("unsupported access %#x\n", wicaccess
);
1217 return E_INVALIDARG
;
1220 buffer
= MapViewOfFile(section
, access
, 0, offset
, 0);
1221 if (!buffer
) return HRESULT_FROM_WIN32(GetLastError());
1223 hr
= BitmapImpl_Create(width
, height
, stride
, 0, buffer
, format
, WICBitmapCacheOnLoad
, bitmap
);
1224 if (FAILED(hr
)) UnmapViewOfFile(buffer
);
1228 HRESULT WINAPI
WICCreateBitmapFromSection(UINT width
, UINT height
,
1229 REFWICPixelFormatGUID format
, HANDLE section
,
1230 UINT stride
, UINT offset
, IWICBitmap
**bitmap
)
1232 TRACE("%u,%u,%s,%p,%u,%u,%p\n", width
, height
, debugstr_guid(format
),
1233 section
, stride
, offset
, bitmap
);
1235 return WICCreateBitmapFromSectionEx(width
, height
, format
, section
,
1236 stride
, offset
, WICSectionAccessLevelRead
, bitmap
);