Assorted spelling fixes.
[wine.git] / dlls / windowscodecs / imgfactory.c
blobe7095ddd1a076bd319e0b7e96c478de0d8a85fd6
1 /*
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 "config.h"
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "objbase.h"
30 #include "shellapi.h"
31 #include "wincodec.h"
32 #include "wincodecsdk.h"
34 #include "wincodecs_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
40 typedef struct {
41 IWICComponentFactory IWICComponentFactory_iface;
42 LONG ref;
43 } ComponentFactory;
45 static inline ComponentFactory *impl_from_IWICComponentFactory(IWICComponentFactory *iface)
47 return CONTAINING_RECORD(iface, ComponentFactory, IWICComponentFactory_iface);
50 static HRESULT WINAPI ComponentFactory_QueryInterface(IWICComponentFactory *iface, REFIID iid,
51 void **ppv)
53 ComponentFactory *This = impl_from_IWICComponentFactory(iface);
54 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
56 if (!ppv) return E_INVALIDARG;
58 if (IsEqualIID(&IID_IUnknown, iid) ||
59 IsEqualIID(&IID_IWICImagingFactory, iid) ||
60 IsEqualIID(&IID_IWICComponentFactory, iid))
62 *ppv = &This->IWICComponentFactory_iface;
64 else
66 *ppv = NULL;
67 return E_NOINTERFACE;
70 IUnknown_AddRef((IUnknown*)*ppv);
71 return S_OK;
74 static ULONG WINAPI ComponentFactory_AddRef(IWICComponentFactory *iface)
76 ComponentFactory *This = impl_from_IWICComponentFactory(iface);
77 ULONG ref = InterlockedIncrement(&This->ref);
79 TRACE("(%p) refcount=%u\n", iface, ref);
81 return ref;
84 static ULONG WINAPI ComponentFactory_Release(IWICComponentFactory *iface)
86 ComponentFactory *This = impl_from_IWICComponentFactory(iface);
87 ULONG ref = InterlockedDecrement(&This->ref);
89 TRACE("(%p) refcount=%u\n", iface, ref);
91 if (ref == 0)
92 HeapFree(GetProcessHeap(), 0, This);
94 return ref;
97 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFilename(
98 IWICComponentFactory *iface, LPCWSTR wzFilename, const GUID *pguidVendor,
99 DWORD dwDesiredAccess, WICDecodeOptions metadataOptions,
100 IWICBitmapDecoder **ppIDecoder)
102 IWICStream *stream;
103 HRESULT hr;
105 TRACE("(%p,%s,%s,%u,%u,%p)\n", iface, debugstr_w(wzFilename),
106 debugstr_guid(pguidVendor), dwDesiredAccess, metadataOptions, ppIDecoder);
108 hr = StreamImpl_Create(&stream);
109 if (SUCCEEDED(hr))
111 hr = IWICStream_InitializeFromFilename(stream, wzFilename, dwDesiredAccess);
113 if (SUCCEEDED(hr))
115 hr = IWICComponentFactory_CreateDecoderFromStream(iface, (IStream*)stream,
116 pguidVendor, metadataOptions, ppIDecoder);
119 IWICStream_Release(stream);
122 return hr;
125 static IWICBitmapDecoder *find_decoder(IStream *pIStream, const GUID *pguidVendor,
126 WICDecodeOptions metadataOptions)
128 IEnumUnknown *enumdecoders;
129 IUnknown *unkdecoderinfo;
130 IWICBitmapDecoderInfo *decoderinfo;
131 IWICBitmapDecoder *decoder = NULL;
132 GUID vendor;
133 HRESULT res;
134 ULONG num_fetched;
135 BOOL matches;
137 res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
138 if (FAILED(res)) return NULL;
140 while (!decoder)
142 res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
144 if (res == S_OK)
146 res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void**)&decoderinfo);
148 if (SUCCEEDED(res))
150 if (pguidVendor)
152 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
153 if (FAILED(res) || !IsEqualIID(&vendor, pguidVendor))
155 IWICBitmapDecoderInfo_Release(decoderinfo);
156 IUnknown_Release(unkdecoderinfo);
157 continue;
161 res = IWICBitmapDecoderInfo_MatchesPattern(decoderinfo, pIStream, &matches);
163 if (SUCCEEDED(res) && matches)
165 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &decoder);
167 /* FIXME: should use QueryCapability to choose a decoder */
169 if (SUCCEEDED(res))
171 res = IWICBitmapDecoder_Initialize(decoder, pIStream, metadataOptions);
173 if (FAILED(res))
175 IWICBitmapDecoder_Release(decoder);
176 decoder = NULL;
181 IWICBitmapDecoderInfo_Release(decoderinfo);
184 IUnknown_Release(unkdecoderinfo);
186 else
187 break;
190 IEnumUnknown_Release(enumdecoders);
192 return decoder;
195 static HRESULT WINAPI ComponentFactory_CreateDecoderFromStream(
196 IWICComponentFactory *iface, IStream *pIStream, const GUID *pguidVendor,
197 WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
199 HRESULT res;
200 IWICBitmapDecoder *decoder = NULL;
202 TRACE("(%p,%p,%s,%u,%p)\n", iface, pIStream, debugstr_guid(pguidVendor),
203 metadataOptions, ppIDecoder);
205 if (pguidVendor)
206 decoder = find_decoder(pIStream, pguidVendor, metadataOptions);
207 if (!decoder)
208 decoder = find_decoder(pIStream, NULL, metadataOptions);
210 if (decoder)
212 *ppIDecoder = decoder;
213 return S_OK;
215 else
217 if (WARN_ON(wincodecs))
219 LARGE_INTEGER seek;
220 BYTE data[4];
221 ULONG bytesread;
223 WARN("failed to load from a stream\n");
225 seek.QuadPart = 0;
226 res = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
227 if (SUCCEEDED(res))
228 res = IStream_Read(pIStream, data, 4, &bytesread);
229 if (SUCCEEDED(res))
230 WARN("first %i bytes of stream=%x %x %x %x\n", bytesread, data[0], data[1], data[2], data[3]);
232 *ppIDecoder = NULL;
233 return WINCODEC_ERR_COMPONENTNOTFOUND;
237 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFileHandle(
238 IWICComponentFactory *iface, ULONG_PTR hFile, const GUID *pguidVendor,
239 WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
241 IWICStream *stream;
242 HRESULT hr;
244 TRACE("(%p,%lx,%s,%u,%p)\n", iface, hFile, debugstr_guid(pguidVendor),
245 metadataOptions, ppIDecoder);
247 hr = StreamImpl_Create(&stream);
248 if (SUCCEEDED(hr))
250 hr = stream_initialize_from_filehandle(stream, (HANDLE)hFile);
251 if (SUCCEEDED(hr))
253 hr = IWICComponentFactory_CreateDecoderFromStream(iface, (IStream*)stream,
254 pguidVendor, metadataOptions, ppIDecoder);
256 IWICStream_Release(stream);
258 return hr;
261 static HRESULT WINAPI ComponentFactory_CreateComponentInfo(IWICComponentFactory *iface,
262 REFCLSID clsidComponent, IWICComponentInfo **ppIInfo)
264 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(clsidComponent), ppIInfo);
265 return CreateComponentInfo(clsidComponent, ppIInfo);
268 static HRESULT WINAPI ComponentFactory_CreateDecoder(IWICComponentFactory *iface,
269 REFGUID guidContainerFormat, const GUID *pguidVendor,
270 IWICBitmapDecoder **ppIDecoder)
272 IEnumUnknown *enumdecoders;
273 IUnknown *unkdecoderinfo;
274 IWICBitmapDecoderInfo *decoderinfo;
275 IWICBitmapDecoder *decoder = NULL, *preferred_decoder = NULL;
276 GUID vendor;
277 HRESULT res;
278 ULONG num_fetched;
280 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
281 debugstr_guid(pguidVendor), ppIDecoder);
283 if (!guidContainerFormat || !ppIDecoder) return E_INVALIDARG;
285 res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
286 if (FAILED(res)) return res;
288 while (!preferred_decoder)
290 res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
291 if (res != S_OK) break;
293 res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void **)&decoderinfo);
294 if (SUCCEEDED(res))
296 GUID container_guid;
298 res = IWICBitmapDecoderInfo_GetContainerFormat(decoderinfo, &container_guid);
299 if (SUCCEEDED(res) && IsEqualIID(&container_guid, guidContainerFormat))
301 IWICBitmapDecoder *new_decoder;
303 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &new_decoder);
304 if (SUCCEEDED(res))
306 if (pguidVendor)
308 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
309 if (SUCCEEDED(res) && IsEqualIID(&vendor, pguidVendor))
311 preferred_decoder = new_decoder;
312 new_decoder = NULL;
316 if (new_decoder && !decoder)
318 decoder = new_decoder;
319 new_decoder = NULL;
322 if (new_decoder) IWICBitmapDecoder_Release(new_decoder);
326 IWICBitmapDecoderInfo_Release(decoderinfo);
329 IUnknown_Release(unkdecoderinfo);
332 IEnumUnknown_Release(enumdecoders);
334 if (preferred_decoder)
336 *ppIDecoder = preferred_decoder;
337 if (decoder) IWICBitmapDecoder_Release(decoder);
338 return S_OK;
341 if (decoder)
343 *ppIDecoder = decoder;
344 return S_OK;
347 *ppIDecoder = NULL;
348 return WINCODEC_ERR_COMPONENTNOTFOUND;
351 static HRESULT WINAPI ComponentFactory_CreateEncoder(IWICComponentFactory *iface,
352 REFGUID guidContainerFormat, const GUID *pguidVendor,
353 IWICBitmapEncoder **ppIEncoder)
355 static int fixme=0;
356 IEnumUnknown *enumencoders;
357 IUnknown *unkencoderinfo;
358 IWICBitmapEncoderInfo *encoderinfo;
359 IWICBitmapEncoder *encoder=NULL;
360 HRESULT res=S_OK;
361 ULONG num_fetched;
362 GUID actual_containerformat;
364 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
365 debugstr_guid(pguidVendor), ppIEncoder);
367 if (pguidVendor && !fixme++)
368 FIXME("ignoring vendor GUID\n");
370 res = CreateComponentEnumerator(WICEncoder, WICComponentEnumerateDefault, &enumencoders);
371 if (FAILED(res)) return res;
373 while (!encoder)
375 res = IEnumUnknown_Next(enumencoders, 1, &unkencoderinfo, &num_fetched);
377 if (res == S_OK)
379 res = IUnknown_QueryInterface(unkencoderinfo, &IID_IWICBitmapEncoderInfo, (void**)&encoderinfo);
381 if (SUCCEEDED(res))
383 res = IWICBitmapEncoderInfo_GetContainerFormat(encoderinfo, &actual_containerformat);
385 if (SUCCEEDED(res) && IsEqualGUID(guidContainerFormat, &actual_containerformat))
387 res = IWICBitmapEncoderInfo_CreateInstance(encoderinfo, &encoder);
388 if (FAILED(res))
389 encoder = NULL;
392 IWICBitmapEncoderInfo_Release(encoderinfo);
395 IUnknown_Release(unkencoderinfo);
397 else
398 break;
401 IEnumUnknown_Release(enumencoders);
403 if (encoder)
405 *ppIEncoder = encoder;
406 return S_OK;
408 else
410 WARN("failed to create encoder\n");
411 *ppIEncoder = NULL;
412 return WINCODEC_ERR_COMPONENTNOTFOUND;
416 static HRESULT WINAPI ComponentFactory_CreatePalette(IWICComponentFactory *iface,
417 IWICPalette **ppIPalette)
419 TRACE("(%p,%p)\n", iface, ppIPalette);
420 return PaletteImpl_Create(ppIPalette);
423 static HRESULT WINAPI ComponentFactory_CreateFormatConverter(IWICComponentFactory *iface,
424 IWICFormatConverter **ppIFormatConverter)
426 return FormatConverter_CreateInstance(&IID_IWICFormatConverter, (void**)ppIFormatConverter);
429 static HRESULT WINAPI ComponentFactory_CreateBitmapScaler(IWICComponentFactory *iface,
430 IWICBitmapScaler **ppIBitmapScaler)
432 TRACE("(%p,%p)\n", iface, ppIBitmapScaler);
434 return BitmapScaler_Create(ppIBitmapScaler);
437 static HRESULT WINAPI ComponentFactory_CreateBitmapClipper(IWICComponentFactory *iface,
438 IWICBitmapClipper **ppIBitmapClipper)
440 TRACE("(%p,%p)\n", iface, ppIBitmapClipper);
441 return BitmapClipper_Create(ppIBitmapClipper);
444 static HRESULT WINAPI ComponentFactory_CreateBitmapFlipRotator(IWICComponentFactory *iface,
445 IWICBitmapFlipRotator **ppIBitmapFlipRotator)
447 TRACE("(%p,%p)\n", iface, ppIBitmapFlipRotator);
448 return FlipRotator_Create(ppIBitmapFlipRotator);
451 static HRESULT WINAPI ComponentFactory_CreateStream(IWICComponentFactory *iface,
452 IWICStream **ppIWICStream)
454 TRACE("(%p,%p)\n", iface, ppIWICStream);
455 return StreamImpl_Create(ppIWICStream);
458 static HRESULT WINAPI ComponentFactory_CreateColorContext(IWICComponentFactory *iface,
459 IWICColorContext **ppIColorContext)
461 TRACE("(%p,%p)\n", iface, ppIColorContext);
462 return ColorContext_Create(ppIColorContext);
465 static HRESULT WINAPI ComponentFactory_CreateColorTransformer(IWICComponentFactory *iface,
466 IWICColorTransform **ppIColorTransform)
468 TRACE("(%p,%p)\n", iface, ppIColorTransform);
469 return ColorTransform_Create(ppIColorTransform);
472 static HRESULT WINAPI ComponentFactory_CreateBitmap(IWICComponentFactory *iface,
473 UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat,
474 WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
476 TRACE("(%p,%u,%u,%s,%u,%p)\n", iface, uiWidth, uiHeight,
477 debugstr_guid(pixelFormat), option, ppIBitmap);
478 return BitmapImpl_Create(uiWidth, uiHeight, 0, 0, NULL, pixelFormat, option, ppIBitmap);
481 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSource(IWICComponentFactory *iface,
482 IWICBitmapSource *piBitmapSource, WICBitmapCreateCacheOption option,
483 IWICBitmap **ppIBitmap)
485 IWICBitmap *result;
486 IWICBitmapLock *lock;
487 IWICPalette *palette;
488 UINT width, height;
489 WICPixelFormatGUID pixelformat = {0};
490 HRESULT hr;
491 WICRect rc;
492 double dpix, dpiy;
493 IWICComponentInfo *info;
494 IWICPixelFormatInfo2 *formatinfo;
495 WICPixelFormatNumericRepresentation format_type;
497 TRACE("(%p,%p,%u,%p)\n", iface, piBitmapSource, option, ppIBitmap);
499 if (!piBitmapSource || !ppIBitmap)
500 return E_INVALIDARG;
502 hr = IWICBitmapSource_GetSize(piBitmapSource, &width, &height);
504 if (SUCCEEDED(hr))
505 hr = IWICBitmapSource_GetPixelFormat(piBitmapSource, &pixelformat);
507 if (SUCCEEDED(hr))
508 hr = CreateComponentInfo(&pixelformat, &info);
510 if (SUCCEEDED(hr))
512 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo2, (void**)&formatinfo);
514 if (SUCCEEDED(hr))
516 hr = IWICPixelFormatInfo2_GetNumericRepresentation(formatinfo, &format_type);
518 IWICPixelFormatInfo2_Release(formatinfo);
521 IWICComponentInfo_Release(info);
524 if (SUCCEEDED(hr))
525 hr = BitmapImpl_Create(width, height, 0, 0, NULL, &pixelformat, option, &result);
527 if (SUCCEEDED(hr))
529 hr = IWICBitmap_Lock(result, NULL, WICBitmapLockWrite, &lock);
530 if (SUCCEEDED(hr))
532 UINT stride, buffersize;
533 BYTE *buffer;
534 rc.X = rc.Y = 0;
535 rc.Width = width;
536 rc.Height = height;
538 hr = IWICBitmapLock_GetStride(lock, &stride);
540 if (SUCCEEDED(hr))
541 hr = IWICBitmapLock_GetDataPointer(lock, &buffersize, &buffer);
543 if (SUCCEEDED(hr))
544 hr = IWICBitmapSource_CopyPixels(piBitmapSource, &rc, stride,
545 buffersize, buffer);
547 IWICBitmapLock_Release(lock);
550 if (SUCCEEDED(hr) && (format_type == WICPixelFormatNumericRepresentationUnspecified ||
551 format_type == WICPixelFormatNumericRepresentationIndexed))
553 hr = PaletteImpl_Create(&palette);
555 if (SUCCEEDED(hr))
557 hr = IWICBitmapSource_CopyPalette(piBitmapSource, palette);
559 if (SUCCEEDED(hr))
560 hr = IWICBitmap_SetPalette(result, palette);
561 else
562 hr = S_OK;
564 IWICPalette_Release(palette);
568 if (SUCCEEDED(hr))
570 hr = IWICBitmapSource_GetResolution(piBitmapSource, &dpix, &dpiy);
572 if (SUCCEEDED(hr))
573 hr = IWICBitmap_SetResolution(result, dpix, dpiy);
574 else
575 hr = S_OK;
578 if (SUCCEEDED(hr))
579 *ppIBitmap = result;
580 else
581 IWICBitmap_Release(result);
584 return hr;
587 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSourceRect(IWICComponentFactory *iface,
588 IWICBitmapSource *piBitmapSource, UINT x, UINT y, UINT width, UINT height,
589 IWICBitmap **ppIBitmap)
591 FIXME("(%p,%p,%u,%u,%u,%u,%p): stub\n", iface, piBitmapSource, x, y, width,
592 height, ppIBitmap);
593 return E_NOTIMPL;
596 static HRESULT WINAPI ComponentFactory_CreateBitmapFromMemory(IWICComponentFactory *iface,
597 UINT width, UINT height, REFWICPixelFormatGUID format, UINT stride,
598 UINT size, BYTE *buffer, IWICBitmap **bitmap)
600 TRACE("(%p,%u,%u,%s,%u,%u,%p,%p\n", iface, width, height,
601 debugstr_guid(format), stride, size, buffer, bitmap);
603 if (!stride || !size || !buffer || !bitmap) return E_INVALIDARG;
605 return BitmapImpl_Create(width, height, stride, size, buffer, format, WICBitmapCacheOnLoad, bitmap);
608 static BOOL get_16bpp_format(HBITMAP hbm, WICPixelFormatGUID *format)
610 BOOL ret = TRUE;
611 BITMAPV4HEADER bmh;
612 HDC hdc;
614 hdc = CreateCompatibleDC(0);
616 memset(&bmh, 0, sizeof(bmh));
617 bmh.bV4Size = sizeof(bmh);
618 bmh.bV4Width = 1;
619 bmh.bV4Height = 1;
620 bmh.bV4V4Compression = BI_BITFIELDS;
621 bmh.bV4BitCount = 16;
623 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO *)&bmh, DIB_RGB_COLORS);
625 if (bmh.bV4RedMask == 0x7c00 &&
626 bmh.bV4GreenMask == 0x3e0 &&
627 bmh.bV4BlueMask == 0x1f)
629 *format = GUID_WICPixelFormat16bppBGR555;
631 else if (bmh.bV4RedMask == 0xf800 &&
632 bmh.bV4GreenMask == 0x7e0 &&
633 bmh.bV4BlueMask == 0x1f)
635 *format = GUID_WICPixelFormat16bppBGR565;
637 else
639 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
640 bmh.bV4GreenMask, bmh.bV4BlueMask);
641 ret = FALSE;
644 DeleteDC(hdc);
645 return ret;
648 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHBITMAP(IWICComponentFactory *iface,
649 HBITMAP hbm, HPALETTE hpal, WICBitmapAlphaChannelOption option, IWICBitmap **bitmap)
651 BITMAP bm;
652 HRESULT hr;
653 WICPixelFormatGUID format;
654 IWICBitmapLock *lock;
655 UINT size, num_palette_entries = 0;
656 PALETTEENTRY entry[256];
658 TRACE("(%p,%p,%p,%u,%p)\n", iface, hbm, hpal, option, bitmap);
660 if (!bitmap) return E_INVALIDARG;
662 if (GetObjectW(hbm, sizeof(bm), &bm) != sizeof(bm))
663 return WINCODEC_ERR_WIN32ERROR;
665 if (hpal)
667 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
668 if (!num_palette_entries)
669 return WINCODEC_ERR_WIN32ERROR;
672 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
673 switch(bm.bmBitsPixel)
675 case 1:
676 format = GUID_WICPixelFormat1bppIndexed;
677 break;
678 case 4:
679 format = GUID_WICPixelFormat4bppIndexed;
680 break;
681 case 8:
682 format = GUID_WICPixelFormat8bppIndexed;
683 break;
684 case 16:
685 if (!get_16bpp_format(hbm, &format))
686 return E_INVALIDARG;
687 break;
688 case 24:
689 format = GUID_WICPixelFormat24bppBGR;
690 break;
691 case 32:
692 switch (option)
694 case WICBitmapUseAlpha:
695 format = GUID_WICPixelFormat32bppBGRA;
696 break;
697 case WICBitmapUsePremultipliedAlpha:
698 format = GUID_WICPixelFormat32bppPBGRA;
699 break;
700 case WICBitmapIgnoreAlpha:
701 format = GUID_WICPixelFormat32bppBGR;
702 break;
703 default:
704 return E_INVALIDARG;
706 break;
707 case 48:
708 format = GUID_WICPixelFormat48bppRGB;
709 break;
710 default:
711 FIXME("unsupported %d bpp\n", bm.bmBitsPixel);
712 return E_INVALIDARG;
715 hr = BitmapImpl_Create(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, 0, NULL, &format, option, bitmap);
716 if (hr != S_OK) return hr;
718 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
719 if (hr == S_OK)
721 BYTE *buffer;
722 HDC hdc;
723 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
724 BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
726 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
728 hdc = CreateCompatibleDC(0);
730 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
731 bmi->bmiHeader.biBitCount = 0;
732 GetDIBits(hdc, hbm, 0, 0, NULL, bmi, DIB_RGB_COLORS);
733 bmi->bmiHeader.biHeight = -bm.bmHeight;
734 GetDIBits(hdc, hbm, 0, bm.bmHeight, buffer, bmi, DIB_RGB_COLORS);
736 DeleteDC(hdc);
737 IWICBitmapLock_Release(lock);
739 if (num_palette_entries)
741 IWICPalette *palette;
742 WICColor colors[256];
743 UINT i;
745 hr = PaletteImpl_Create(&palette);
746 if (hr == S_OK)
748 for (i = 0; i < num_palette_entries; i++)
749 colors[i] = 0xff000000 | entry[i].peRed << 16 |
750 entry[i].peGreen << 8 | entry[i].peBlue;
752 hr = IWICPalette_InitializeCustom(palette, colors, num_palette_entries);
753 if (hr == S_OK)
754 hr = IWICBitmap_SetPalette(*bitmap, palette);
756 IWICPalette_Release(palette);
761 if (hr != S_OK)
763 IWICBitmap_Release(*bitmap);
764 *bitmap = NULL;
767 return hr;
770 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHICON(IWICComponentFactory *iface,
771 HICON hicon, IWICBitmap **bitmap)
773 IWICBitmapLock *lock;
774 ICONINFO info;
775 BITMAP bm;
776 int width, height, x, y;
777 UINT stride, size;
778 BYTE *buffer;
779 DWORD *bits;
780 BITMAPINFO bi;
781 HDC hdc;
782 BOOL has_alpha;
783 HRESULT hr;
785 TRACE("(%p,%p,%p)\n", iface, hicon, bitmap);
787 if (!bitmap) return E_INVALIDARG;
789 if (!GetIconInfo(hicon, &info))
790 return HRESULT_FROM_WIN32(GetLastError());
792 GetObjectW(info.hbmColor ? info.hbmColor : info.hbmMask, sizeof(bm), &bm);
794 width = bm.bmWidth;
795 height = info.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
796 stride = width * 4;
797 size = stride * height;
799 hr = BitmapImpl_Create(width, height, stride, size, NULL,
800 &GUID_WICPixelFormat32bppBGRA, WICBitmapCacheOnLoad, bitmap);
801 if (hr != S_OK) goto failed;
803 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
804 if (hr != S_OK)
806 IWICBitmap_Release(*bitmap);
807 goto failed;
809 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
811 hdc = CreateCompatibleDC(0);
813 memset(&bi, 0, sizeof(bi));
814 bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
815 bi.bmiHeader.biWidth = width;
816 bi.bmiHeader.biHeight = info.hbmColor ? -height: -height * 2;
817 bi.bmiHeader.biPlanes = 1;
818 bi.bmiHeader.biBitCount = 32;
819 bi.bmiHeader.biCompression = BI_RGB;
821 has_alpha = FALSE;
823 if (info.hbmColor)
825 GetDIBits(hdc, info.hbmColor, 0, height, buffer, &bi, DIB_RGB_COLORS);
827 if (bm.bmBitsPixel == 32)
829 /* If any pixel has a non-zero alpha, ignore hbmMask */
830 bits = (DWORD *)buffer;
831 for (x = 0; x < width && !has_alpha; x++, bits++)
833 for (y = 0; y < height; y++)
835 if (*bits & 0xff000000)
837 has_alpha = TRUE;
838 break;
844 else
845 GetDIBits(hdc, info.hbmMask, 0, height, buffer, &bi, DIB_RGB_COLORS);
847 if (!has_alpha)
849 DWORD *rgba;
851 if (info.hbmMask)
853 BYTE *mask;
855 mask = HeapAlloc(GetProcessHeap(), 0, size);
856 if (!mask)
858 IWICBitmapLock_Release(lock);
859 IWICBitmap_Release(*bitmap);
860 DeleteDC(hdc);
861 hr = E_OUTOFMEMORY;
862 goto failed;
865 /* read alpha data from the mask */
866 GetDIBits(hdc, info.hbmMask, info.hbmColor ? 0 : height, height, mask, &bi, DIB_RGB_COLORS);
868 for (y = 0; y < height; y++)
870 rgba = (DWORD *)(buffer + y * stride);
871 bits = (DWORD *)(mask + y * stride);
873 for (x = 0; x < width; x++, rgba++, bits++)
875 if (*bits)
876 *rgba = 0;
877 else
878 *rgba |= 0xff000000;
882 HeapFree(GetProcessHeap(), 0, mask);
884 else
886 /* set constant alpha of 255 */
887 for (y = 0; y < height; y++)
889 rgba = (DWORD *)(buffer + y * stride);
890 for (x = 0; x < width; x++, rgba++)
891 *rgba |= 0xff000000;
897 IWICBitmapLock_Release(lock);
898 DeleteDC(hdc);
900 failed:
901 DeleteObject(info.hbmColor);
902 DeleteObject(info.hbmMask);
904 return hr;
907 static HRESULT WINAPI ComponentFactory_CreateComponentEnumerator(IWICComponentFactory *iface,
908 DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
910 TRACE("(%p,%u,%u,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
911 return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
914 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromDecoder(
915 IWICComponentFactory *iface, IWICBitmapDecoder *pIDecoder,
916 IWICFastMetadataEncoder **ppIFastEncoder)
918 FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
919 return E_NOTIMPL;
922 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromFrameDecode(
923 IWICComponentFactory *iface, IWICBitmapFrameDecode *pIFrameDecoder,
924 IWICFastMetadataEncoder **ppIFastEncoder)
926 FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
927 return E_NOTIMPL;
930 static HRESULT WINAPI ComponentFactory_CreateQueryWriter(IWICComponentFactory *iface,
931 REFGUID guidMetadataFormat, const GUID *pguidVendor,
932 IWICMetadataQueryWriter **ppIQueryWriter)
934 FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
935 debugstr_guid(pguidVendor), ppIQueryWriter);
936 return E_NOTIMPL;
939 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromReader(IWICComponentFactory *iface,
940 IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
941 IWICMetadataQueryWriter **ppIQueryWriter)
943 FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
944 ppIQueryWriter);
945 return E_NOTIMPL;
948 static HRESULT WINAPI ComponentFactory_CreateMetadataReader(IWICComponentFactory *iface,
949 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
951 FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
952 options, stream, reader);
953 return E_NOTIMPL;
956 static HRESULT WINAPI ComponentFactory_CreateMetadataReaderFromContainer(IWICComponentFactory *iface,
957 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
959 FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
960 options, stream, reader);
961 return E_NOTIMPL;
964 static HRESULT WINAPI ComponentFactory_CreateMetadataWriter(IWICComponentFactory *iface,
965 REFGUID format, const GUID *vendor, DWORD options, IWICMetadataWriter **writer)
967 FIXME("%p,%s,%s,%x,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor), options, writer);
968 return E_NOTIMPL;
971 static HRESULT WINAPI ComponentFactory_CreateMetadataWriterFromReader(IWICComponentFactory *iface,
972 IWICMetadataReader *reader, const GUID *vendor, IWICMetadataWriter **writer)
974 FIXME("%p,%p,%s,%p: stub\n", iface, reader, debugstr_guid(vendor), writer);
975 return E_NOTIMPL;
978 static HRESULT WINAPI ComponentFactory_CreateQueryReaderFromBlockReader(IWICComponentFactory *iface,
979 IWICMetadataBlockReader *block_reader, IWICMetadataQueryReader **query_reader)
981 FIXME("%p,%p,%p: stub\n", iface, block_reader, query_reader);
982 return E_NOTIMPL;
985 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromBlockWriter(IWICComponentFactory *iface,
986 IWICMetadataBlockWriter *block_writer, IWICMetadataQueryWriter **query_writer)
988 FIXME("%p,%p,%p: stub\n", iface, block_writer, query_writer);
989 return E_NOTIMPL;
992 static HRESULT WINAPI ComponentFactory_CreateEncoderPropertyBag(IWICComponentFactory *iface,
993 PROPBAG2 *options, UINT count, IPropertyBag2 **property)
995 TRACE("(%p,%p,%u,%p)\n", iface, options, count, property);
996 return CreatePropertyBag2(options, count, property);
999 static const IWICComponentFactoryVtbl ComponentFactory_Vtbl = {
1000 ComponentFactory_QueryInterface,
1001 ComponentFactory_AddRef,
1002 ComponentFactory_Release,
1003 ComponentFactory_CreateDecoderFromFilename,
1004 ComponentFactory_CreateDecoderFromStream,
1005 ComponentFactory_CreateDecoderFromFileHandle,
1006 ComponentFactory_CreateComponentInfo,
1007 ComponentFactory_CreateDecoder,
1008 ComponentFactory_CreateEncoder,
1009 ComponentFactory_CreatePalette,
1010 ComponentFactory_CreateFormatConverter,
1011 ComponentFactory_CreateBitmapScaler,
1012 ComponentFactory_CreateBitmapClipper,
1013 ComponentFactory_CreateBitmapFlipRotator,
1014 ComponentFactory_CreateStream,
1015 ComponentFactory_CreateColorContext,
1016 ComponentFactory_CreateColorTransformer,
1017 ComponentFactory_CreateBitmap,
1018 ComponentFactory_CreateBitmapFromSource,
1019 ComponentFactory_CreateBitmapFromSourceRect,
1020 ComponentFactory_CreateBitmapFromMemory,
1021 ComponentFactory_CreateBitmapFromHBITMAP,
1022 ComponentFactory_CreateBitmapFromHICON,
1023 ComponentFactory_CreateComponentEnumerator,
1024 ComponentFactory_CreateFastMetadataEncoderFromDecoder,
1025 ComponentFactory_CreateFastMetadataEncoderFromFrameDecode,
1026 ComponentFactory_CreateQueryWriter,
1027 ComponentFactory_CreateQueryWriterFromReader,
1028 ComponentFactory_CreateMetadataReader,
1029 ComponentFactory_CreateMetadataReaderFromContainer,
1030 ComponentFactory_CreateMetadataWriter,
1031 ComponentFactory_CreateMetadataWriterFromReader,
1032 ComponentFactory_CreateQueryReaderFromBlockReader,
1033 ComponentFactory_CreateQueryWriterFromBlockWriter,
1034 ComponentFactory_CreateEncoderPropertyBag
1037 HRESULT ComponentFactory_CreateInstance(REFIID iid, void** ppv)
1039 ComponentFactory *This;
1040 HRESULT ret;
1042 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1044 *ppv = NULL;
1046 This = HeapAlloc(GetProcessHeap(), 0, sizeof(ComponentFactory));
1047 if (!This) return E_OUTOFMEMORY;
1049 This->IWICComponentFactory_iface.lpVtbl = &ComponentFactory_Vtbl;
1050 This->ref = 1;
1052 ret = IWICComponentFactory_QueryInterface(&This->IWICComponentFactory_iface, iid, ppv);
1053 IWICComponentFactory_Release(&This->IWICComponentFactory_iface);
1055 return ret;