webservices: Write correct envelope headers for WS_ENVELOPE_VERSION_NONE.
[wine.git] / dlls / windowscodecs / imgfactory.c
blobbf634949b441a0f971d40a1c5cb95510a1f1af00
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"
32 #include "wincodecs_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
38 typedef struct {
39 IWICComponentFactory IWICComponentFactory_iface;
40 LONG ref;
41 } ComponentFactory;
43 static inline ComponentFactory *impl_from_IWICComponentFactory(IWICComponentFactory *iface)
45 return CONTAINING_RECORD(iface, ComponentFactory, IWICComponentFactory_iface);
48 static HRESULT WINAPI ComponentFactory_QueryInterface(IWICComponentFactory *iface, REFIID iid,
49 void **ppv)
51 ComponentFactory *This = impl_from_IWICComponentFactory(iface);
52 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
54 if (!ppv) return E_INVALIDARG;
56 if (IsEqualIID(&IID_IUnknown, iid) ||
57 IsEqualIID(&IID_IWICImagingFactory, iid) ||
58 IsEqualIID(&IID_IWICComponentFactory, iid))
60 *ppv = &This->IWICComponentFactory_iface;
62 else
64 *ppv = NULL;
65 return E_NOINTERFACE;
68 IUnknown_AddRef((IUnknown*)*ppv);
69 return S_OK;
72 static ULONG WINAPI ComponentFactory_AddRef(IWICComponentFactory *iface)
74 ComponentFactory *This = impl_from_IWICComponentFactory(iface);
75 ULONG ref = InterlockedIncrement(&This->ref);
77 TRACE("(%p) refcount=%u\n", iface, ref);
79 return ref;
82 static ULONG WINAPI ComponentFactory_Release(IWICComponentFactory *iface)
84 ComponentFactory *This = impl_from_IWICComponentFactory(iface);
85 ULONG ref = InterlockedDecrement(&This->ref);
87 TRACE("(%p) refcount=%u\n", iface, ref);
89 if (ref == 0)
90 HeapFree(GetProcessHeap(), 0, This);
92 return ref;
95 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFilename(
96 IWICComponentFactory *iface, LPCWSTR wzFilename, const GUID *pguidVendor,
97 DWORD dwDesiredAccess, WICDecodeOptions metadataOptions,
98 IWICBitmapDecoder **ppIDecoder)
100 IWICStream *stream;
101 HRESULT hr;
103 TRACE("(%p,%s,%s,%u,%u,%p)\n", iface, debugstr_w(wzFilename),
104 debugstr_guid(pguidVendor), dwDesiredAccess, metadataOptions, ppIDecoder);
106 hr = StreamImpl_Create(&stream);
107 if (SUCCEEDED(hr))
109 hr = IWICStream_InitializeFromFilename(stream, wzFilename, dwDesiredAccess);
111 if (SUCCEEDED(hr))
113 hr = IWICComponentFactory_CreateDecoderFromStream(iface, (IStream*)stream,
114 pguidVendor, metadataOptions, ppIDecoder);
117 IWICStream_Release(stream);
120 return hr;
123 static IWICBitmapDecoder *find_decoder(IStream *pIStream, const GUID *pguidVendor,
124 WICDecodeOptions metadataOptions)
126 IEnumUnknown *enumdecoders;
127 IUnknown *unkdecoderinfo;
128 IWICBitmapDecoderInfo *decoderinfo;
129 IWICBitmapDecoder *decoder = NULL;
130 GUID vendor;
131 HRESULT res;
132 ULONG num_fetched;
133 BOOL matches;
135 res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
136 if (FAILED(res)) return NULL;
138 while (!decoder)
140 res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
142 if (res == S_OK)
144 res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void**)&decoderinfo);
146 if (SUCCEEDED(res))
148 if (pguidVendor)
150 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
151 if (FAILED(res) || !IsEqualIID(&vendor, pguidVendor))
153 IWICBitmapDecoderInfo_Release(decoderinfo);
154 IUnknown_Release(unkdecoderinfo);
155 continue;
159 res = IWICBitmapDecoderInfo_MatchesPattern(decoderinfo, pIStream, &matches);
161 if (SUCCEEDED(res) && matches)
163 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &decoder);
165 /* FIXME: should use QueryCapability to choose a decoder */
167 if (SUCCEEDED(res))
169 res = IWICBitmapDecoder_Initialize(decoder, pIStream, metadataOptions);
171 if (FAILED(res))
173 IWICBitmapDecoder_Release(decoder);
174 decoder = NULL;
179 IWICBitmapDecoderInfo_Release(decoderinfo);
182 IUnknown_Release(unkdecoderinfo);
184 else
185 break;
188 IEnumUnknown_Release(enumdecoders);
190 return decoder;
193 static HRESULT WINAPI ComponentFactory_CreateDecoderFromStream(
194 IWICComponentFactory *iface, IStream *pIStream, const GUID *pguidVendor,
195 WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
197 HRESULT res;
198 IWICBitmapDecoder *decoder = NULL;
200 TRACE("(%p,%p,%s,%u,%p)\n", iface, pIStream, debugstr_guid(pguidVendor),
201 metadataOptions, ppIDecoder);
203 if (pguidVendor)
204 decoder = find_decoder(pIStream, pguidVendor, metadataOptions);
205 if (!decoder)
206 decoder = find_decoder(pIStream, NULL, metadataOptions);
208 if (decoder)
210 *ppIDecoder = decoder;
211 return S_OK;
213 else
215 if (WARN_ON(wincodecs))
217 LARGE_INTEGER seek;
218 BYTE data[4];
219 ULONG bytesread;
221 WARN("failed to load from a stream\n");
223 seek.QuadPart = 0;
224 res = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
225 if (SUCCEEDED(res))
226 res = IStream_Read(pIStream, data, 4, &bytesread);
227 if (SUCCEEDED(res))
228 WARN("first %i bytes of stream=%x %x %x %x\n", bytesread, data[0], data[1], data[2], data[3]);
230 *ppIDecoder = NULL;
231 return WINCODEC_ERR_COMPONENTNOTFOUND;
235 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFileHandle(
236 IWICComponentFactory *iface, ULONG_PTR hFile, const GUID *pguidVendor,
237 WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
239 IWICStream *stream;
240 HRESULT hr;
242 TRACE("(%p,%lx,%s,%u,%p)\n", iface, hFile, debugstr_guid(pguidVendor),
243 metadataOptions, ppIDecoder);
245 hr = StreamImpl_Create(&stream);
246 if (SUCCEEDED(hr))
248 hr = stream_initialize_from_filehandle(stream, (HANDLE)hFile);
249 if (SUCCEEDED(hr))
251 hr = IWICComponentFactory_CreateDecoderFromStream(iface, (IStream*)stream,
252 pguidVendor, metadataOptions, ppIDecoder);
254 IWICStream_Release(stream);
256 return hr;
259 static HRESULT WINAPI ComponentFactory_CreateComponentInfo(IWICComponentFactory *iface,
260 REFCLSID clsidComponent, IWICComponentInfo **ppIInfo)
262 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(clsidComponent), ppIInfo);
263 return CreateComponentInfo(clsidComponent, ppIInfo);
266 static HRESULT WINAPI ComponentFactory_CreateDecoder(IWICComponentFactory *iface,
267 REFGUID guidContainerFormat, const GUID *pguidVendor,
268 IWICBitmapDecoder **ppIDecoder)
270 IEnumUnknown *enumdecoders;
271 IUnknown *unkdecoderinfo;
272 IWICBitmapDecoderInfo *decoderinfo;
273 IWICBitmapDecoder *decoder = NULL, *preferred_decoder = NULL;
274 GUID vendor;
275 HRESULT res;
276 ULONG num_fetched;
278 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
279 debugstr_guid(pguidVendor), ppIDecoder);
281 if (!guidContainerFormat || !ppIDecoder) return E_INVALIDARG;
283 res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
284 if (FAILED(res)) return res;
286 while (!preferred_decoder)
288 res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
289 if (res != S_OK) break;
291 res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void **)&decoderinfo);
292 if (SUCCEEDED(res))
294 GUID container_guid;
296 res = IWICBitmapDecoderInfo_GetContainerFormat(decoderinfo, &container_guid);
297 if (SUCCEEDED(res) && IsEqualIID(&container_guid, guidContainerFormat))
299 IWICBitmapDecoder *new_decoder;
301 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &new_decoder);
302 if (SUCCEEDED(res))
304 if (pguidVendor)
306 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
307 if (SUCCEEDED(res) && IsEqualIID(&vendor, pguidVendor))
309 preferred_decoder = new_decoder;
310 new_decoder = NULL;
314 if (new_decoder && !decoder)
316 decoder = new_decoder;
317 new_decoder = NULL;
320 if (new_decoder) IWICBitmapDecoder_Release(new_decoder);
324 IWICBitmapDecoderInfo_Release(decoderinfo);
327 IUnknown_Release(unkdecoderinfo);
330 IEnumUnknown_Release(enumdecoders);
332 if (preferred_decoder)
334 *ppIDecoder = preferred_decoder;
335 if (decoder) IWICBitmapDecoder_Release(decoder);
336 return S_OK;
339 if (decoder)
341 *ppIDecoder = decoder;
342 return S_OK;
345 *ppIDecoder = NULL;
346 return WINCODEC_ERR_COMPONENTNOTFOUND;
349 static HRESULT WINAPI ComponentFactory_CreateEncoder(IWICComponentFactory *iface,
350 REFGUID guidContainerFormat, const GUID *pguidVendor,
351 IWICBitmapEncoder **ppIEncoder)
353 static int fixme=0;
354 IEnumUnknown *enumencoders;
355 IUnknown *unkencoderinfo;
356 IWICBitmapEncoderInfo *encoderinfo;
357 IWICBitmapEncoder *encoder=NULL;
358 HRESULT res=S_OK;
359 ULONG num_fetched;
360 GUID actual_containerformat;
362 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
363 debugstr_guid(pguidVendor), ppIEncoder);
365 if (pguidVendor && !fixme++)
366 FIXME("ignoring vendor GUID\n");
368 res = CreateComponentEnumerator(WICEncoder, WICComponentEnumerateDefault, &enumencoders);
369 if (FAILED(res)) return res;
371 while (!encoder)
373 res = IEnumUnknown_Next(enumencoders, 1, &unkencoderinfo, &num_fetched);
375 if (res == S_OK)
377 res = IUnknown_QueryInterface(unkencoderinfo, &IID_IWICBitmapEncoderInfo, (void**)&encoderinfo);
379 if (SUCCEEDED(res))
381 res = IWICBitmapEncoderInfo_GetContainerFormat(encoderinfo, &actual_containerformat);
383 if (SUCCEEDED(res) && IsEqualGUID(guidContainerFormat, &actual_containerformat))
385 res = IWICBitmapEncoderInfo_CreateInstance(encoderinfo, &encoder);
386 if (FAILED(res))
387 encoder = NULL;
390 IWICBitmapEncoderInfo_Release(encoderinfo);
393 IUnknown_Release(unkencoderinfo);
395 else
396 break;
399 IEnumUnknown_Release(enumencoders);
401 if (encoder)
403 *ppIEncoder = encoder;
404 return S_OK;
406 else
408 WARN("failed to create encoder\n");
409 *ppIEncoder = NULL;
410 return WINCODEC_ERR_COMPONENTNOTFOUND;
414 static HRESULT WINAPI ComponentFactory_CreatePalette(IWICComponentFactory *iface,
415 IWICPalette **ppIPalette)
417 TRACE("(%p,%p)\n", iface, ppIPalette);
418 return PaletteImpl_Create(ppIPalette);
421 static HRESULT WINAPI ComponentFactory_CreateFormatConverter(IWICComponentFactory *iface,
422 IWICFormatConverter **ppIFormatConverter)
424 return FormatConverter_CreateInstance(&IID_IWICFormatConverter, (void**)ppIFormatConverter);
427 static HRESULT WINAPI ComponentFactory_CreateBitmapScaler(IWICComponentFactory *iface,
428 IWICBitmapScaler **ppIBitmapScaler)
430 TRACE("(%p,%p)\n", iface, ppIBitmapScaler);
432 return BitmapScaler_Create(ppIBitmapScaler);
435 static HRESULT WINAPI ComponentFactory_CreateBitmapClipper(IWICComponentFactory *iface,
436 IWICBitmapClipper **ppIBitmapClipper)
438 TRACE("(%p,%p)\n", iface, ppIBitmapClipper);
439 return BitmapClipper_Create(ppIBitmapClipper);
442 static HRESULT WINAPI ComponentFactory_CreateBitmapFlipRotator(IWICComponentFactory *iface,
443 IWICBitmapFlipRotator **ppIBitmapFlipRotator)
445 TRACE("(%p,%p)\n", iface, ppIBitmapFlipRotator);
446 return FlipRotator_Create(ppIBitmapFlipRotator);
449 static HRESULT WINAPI ComponentFactory_CreateStream(IWICComponentFactory *iface,
450 IWICStream **ppIWICStream)
452 TRACE("(%p,%p)\n", iface, ppIWICStream);
453 return StreamImpl_Create(ppIWICStream);
456 static HRESULT WINAPI ComponentFactory_CreateColorContext(IWICComponentFactory *iface,
457 IWICColorContext **ppIColorContext)
459 TRACE("(%p,%p)\n", iface, ppIColorContext);
460 return ColorContext_Create(ppIColorContext);
463 static HRESULT WINAPI ComponentFactory_CreateColorTransformer(IWICComponentFactory *iface,
464 IWICColorTransform **ppIColorTransform)
466 TRACE("(%p,%p)\n", iface, ppIColorTransform);
467 return ColorTransform_Create(ppIColorTransform);
470 static HRESULT WINAPI ComponentFactory_CreateBitmap(IWICComponentFactory *iface,
471 UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat,
472 WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
474 TRACE("(%p,%u,%u,%s,%u,%p)\n", iface, uiWidth, uiHeight,
475 debugstr_guid(pixelFormat), option, ppIBitmap);
476 return BitmapImpl_Create(uiWidth, uiHeight, 0, 0, NULL, pixelFormat, option, ppIBitmap);
479 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSource(IWICComponentFactory *iface,
480 IWICBitmapSource *piBitmapSource, WICBitmapCreateCacheOption option,
481 IWICBitmap **ppIBitmap)
483 IWICBitmap *result;
484 IWICBitmapLock *lock;
485 IWICPalette *palette;
486 UINT width, height;
487 WICPixelFormatGUID pixelformat = {0};
488 HRESULT hr;
489 WICRect rc;
490 double dpix, dpiy;
491 IWICComponentInfo *info;
492 IWICPixelFormatInfo2 *formatinfo;
493 WICPixelFormatNumericRepresentation format_type;
495 TRACE("(%p,%p,%u,%p)\n", iface, piBitmapSource, option, ppIBitmap);
497 if (!piBitmapSource || !ppIBitmap)
498 return E_INVALIDARG;
500 hr = IWICBitmapSource_GetSize(piBitmapSource, &width, &height);
502 if (SUCCEEDED(hr))
503 hr = IWICBitmapSource_GetPixelFormat(piBitmapSource, &pixelformat);
505 if (SUCCEEDED(hr))
506 hr = CreateComponentInfo(&pixelformat, &info);
508 if (SUCCEEDED(hr))
510 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo2, (void**)&formatinfo);
512 if (SUCCEEDED(hr))
514 hr = IWICPixelFormatInfo2_GetNumericRepresentation(formatinfo, &format_type);
516 IWICPixelFormatInfo2_Release(formatinfo);
519 IWICComponentInfo_Release(info);
522 if (SUCCEEDED(hr))
523 hr = BitmapImpl_Create(width, height, 0, 0, NULL, &pixelformat, option, &result);
525 if (SUCCEEDED(hr))
527 hr = IWICBitmap_Lock(result, NULL, WICBitmapLockWrite, &lock);
528 if (SUCCEEDED(hr))
530 UINT stride, buffersize;
531 BYTE *buffer;
532 rc.X = rc.Y = 0;
533 rc.Width = width;
534 rc.Height = height;
536 hr = IWICBitmapLock_GetStride(lock, &stride);
538 if (SUCCEEDED(hr))
539 hr = IWICBitmapLock_GetDataPointer(lock, &buffersize, &buffer);
541 if (SUCCEEDED(hr))
542 hr = IWICBitmapSource_CopyPixels(piBitmapSource, &rc, stride,
543 buffersize, buffer);
545 IWICBitmapLock_Release(lock);
548 if (SUCCEEDED(hr) && (format_type == WICPixelFormatNumericRepresentationUnspecified ||
549 format_type == WICPixelFormatNumericRepresentationIndexed))
551 hr = PaletteImpl_Create(&palette);
553 if (SUCCEEDED(hr))
555 hr = IWICBitmapSource_CopyPalette(piBitmapSource, palette);
557 if (SUCCEEDED(hr))
558 hr = IWICBitmap_SetPalette(result, palette);
559 else
560 hr = S_OK;
562 IWICPalette_Release(palette);
566 if (SUCCEEDED(hr))
568 hr = IWICBitmapSource_GetResolution(piBitmapSource, &dpix, &dpiy);
570 if (SUCCEEDED(hr))
571 hr = IWICBitmap_SetResolution(result, dpix, dpiy);
572 else
573 hr = S_OK;
576 if (SUCCEEDED(hr))
577 *ppIBitmap = result;
578 else
579 IWICBitmap_Release(result);
582 return hr;
585 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSourceRect(IWICComponentFactory *iface,
586 IWICBitmapSource *piBitmapSource, UINT x, UINT y, UINT width, UINT height,
587 IWICBitmap **ppIBitmap)
589 FIXME("(%p,%p,%u,%u,%u,%u,%p): stub\n", iface, piBitmapSource, x, y, width,
590 height, ppIBitmap);
591 return E_NOTIMPL;
594 static HRESULT WINAPI ComponentFactory_CreateBitmapFromMemory(IWICComponentFactory *iface,
595 UINT width, UINT height, REFWICPixelFormatGUID format, UINT stride,
596 UINT size, BYTE *buffer, IWICBitmap **bitmap)
598 TRACE("(%p,%u,%u,%s,%u,%u,%p,%p\n", iface, width, height,
599 debugstr_guid(format), stride, size, buffer, bitmap);
601 if (!stride || !size || !buffer || !bitmap) return E_INVALIDARG;
603 return BitmapImpl_Create(width, height, stride, size, buffer, format, WICBitmapCacheOnLoad, bitmap);
606 static BOOL get_16bpp_format(HBITMAP hbm, WICPixelFormatGUID *format)
608 BOOL ret = TRUE;
609 BITMAPV4HEADER bmh;
610 HDC hdc;
612 hdc = CreateCompatibleDC(0);
614 memset(&bmh, 0, sizeof(bmh));
615 bmh.bV4Size = sizeof(bmh);
616 bmh.bV4Width = 1;
617 bmh.bV4Height = 1;
618 bmh.bV4V4Compression = BI_BITFIELDS;
619 bmh.bV4BitCount = 16;
621 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO *)&bmh, DIB_RGB_COLORS);
623 if (bmh.bV4RedMask == 0x7c00 &&
624 bmh.bV4GreenMask == 0x3e0 &&
625 bmh.bV4BlueMask == 0x1f)
627 *format = GUID_WICPixelFormat16bppBGR555;
629 else if (bmh.bV4RedMask == 0xf800 &&
630 bmh.bV4GreenMask == 0x7e0 &&
631 bmh.bV4BlueMask == 0x1f)
633 *format = GUID_WICPixelFormat16bppBGR565;
635 else
637 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
638 bmh.bV4GreenMask, bmh.bV4BlueMask);
639 ret = FALSE;
642 DeleteDC(hdc);
643 return ret;
646 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHBITMAP(IWICComponentFactory *iface,
647 HBITMAP hbm, HPALETTE hpal, WICBitmapAlphaChannelOption option, IWICBitmap **bitmap)
649 BITMAP bm;
650 HRESULT hr;
651 WICPixelFormatGUID format;
652 IWICBitmapLock *lock;
653 UINT size, num_palette_entries = 0;
654 PALETTEENTRY entry[256];
656 TRACE("(%p,%p,%p,%u,%p)\n", iface, hbm, hpal, option, bitmap);
658 if (!bitmap) return E_INVALIDARG;
660 if (GetObjectW(hbm, sizeof(bm), &bm) != sizeof(bm))
661 return WINCODEC_ERR_WIN32ERROR;
663 if (hpal)
665 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
666 if (!num_palette_entries)
667 return WINCODEC_ERR_WIN32ERROR;
670 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
671 switch(bm.bmBitsPixel)
673 case 1:
674 format = GUID_WICPixelFormat1bppIndexed;
675 break;
676 case 4:
677 format = GUID_WICPixelFormat4bppIndexed;
678 break;
679 case 8:
680 format = GUID_WICPixelFormat8bppIndexed;
681 break;
682 case 16:
683 if (!get_16bpp_format(hbm, &format))
684 return E_INVALIDARG;
685 break;
686 case 24:
687 format = GUID_WICPixelFormat24bppBGR;
688 break;
689 case 32:
690 switch (option)
692 case WICBitmapUseAlpha:
693 format = GUID_WICPixelFormat32bppBGRA;
694 break;
695 case WICBitmapUsePremultipliedAlpha:
696 format = GUID_WICPixelFormat32bppPBGRA;
697 break;
698 case WICBitmapIgnoreAlpha:
699 format = GUID_WICPixelFormat32bppBGR;
700 break;
701 default:
702 return E_INVALIDARG;
704 break;
705 case 48:
706 format = GUID_WICPixelFormat48bppRGB;
707 break;
708 default:
709 FIXME("unsupported %d bpp\n", bm.bmBitsPixel);
710 return E_INVALIDARG;
713 hr = BitmapImpl_Create(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, 0, NULL, &format, WICBitmapCacheOnLoad, bitmap);
714 if (hr != S_OK) return hr;
716 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
717 if (hr == S_OK)
719 BYTE *buffer;
720 HDC hdc;
721 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
722 BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
724 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
726 hdc = CreateCompatibleDC(0);
728 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
729 bmi->bmiHeader.biBitCount = 0;
730 GetDIBits(hdc, hbm, 0, 0, NULL, bmi, DIB_RGB_COLORS);
731 bmi->bmiHeader.biHeight = -bm.bmHeight;
732 GetDIBits(hdc, hbm, 0, bm.bmHeight, buffer, bmi, DIB_RGB_COLORS);
734 DeleteDC(hdc);
735 IWICBitmapLock_Release(lock);
737 if (num_palette_entries)
739 IWICPalette *palette;
740 WICColor colors[256];
741 UINT i;
743 hr = PaletteImpl_Create(&palette);
744 if (hr == S_OK)
746 for (i = 0; i < num_palette_entries; i++)
747 colors[i] = 0xff000000 | entry[i].peRed << 16 |
748 entry[i].peGreen << 8 | entry[i].peBlue;
750 hr = IWICPalette_InitializeCustom(palette, colors, num_palette_entries);
751 if (hr == S_OK)
752 hr = IWICBitmap_SetPalette(*bitmap, palette);
754 IWICPalette_Release(palette);
759 if (hr != S_OK)
761 IWICBitmap_Release(*bitmap);
762 *bitmap = NULL;
765 return hr;
768 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHICON(IWICComponentFactory *iface,
769 HICON hicon, IWICBitmap **bitmap)
771 IWICBitmapLock *lock;
772 ICONINFO info;
773 BITMAP bm;
774 int width, height, x, y;
775 UINT stride, size;
776 BYTE *buffer;
777 DWORD *bits;
778 BITMAPINFO bi;
779 HDC hdc;
780 BOOL has_alpha;
781 HRESULT hr;
783 TRACE("(%p,%p,%p)\n", iface, hicon, bitmap);
785 if (!bitmap) return E_INVALIDARG;
787 if (!GetIconInfo(hicon, &info))
788 return HRESULT_FROM_WIN32(GetLastError());
790 GetObjectW(info.hbmColor ? info.hbmColor : info.hbmMask, sizeof(bm), &bm);
792 width = bm.bmWidth;
793 height = info.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
794 stride = width * 4;
795 size = stride * height;
797 hr = BitmapImpl_Create(width, height, stride, size, NULL,
798 &GUID_WICPixelFormat32bppBGRA, WICBitmapCacheOnLoad, bitmap);
799 if (hr != S_OK) goto failed;
801 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
802 if (hr != S_OK)
804 IWICBitmap_Release(*bitmap);
805 goto failed;
807 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
809 hdc = CreateCompatibleDC(0);
811 memset(&bi, 0, sizeof(bi));
812 bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
813 bi.bmiHeader.biWidth = width;
814 bi.bmiHeader.biHeight = info.hbmColor ? -height: -height * 2;
815 bi.bmiHeader.biPlanes = 1;
816 bi.bmiHeader.biBitCount = 32;
817 bi.bmiHeader.biCompression = BI_RGB;
819 has_alpha = FALSE;
821 if (info.hbmColor)
823 GetDIBits(hdc, info.hbmColor, 0, height, buffer, &bi, DIB_RGB_COLORS);
825 if (bm.bmBitsPixel == 32)
827 /* If any pixel has a non-zero alpha, ignore hbmMask */
828 bits = (DWORD *)buffer;
829 for (x = 0; x < width && !has_alpha; x++, bits++)
831 for (y = 0; y < height; y++)
833 if (*bits & 0xff000000)
835 has_alpha = TRUE;
836 break;
842 else
843 GetDIBits(hdc, info.hbmMask, 0, height, buffer, &bi, DIB_RGB_COLORS);
845 if (!has_alpha)
847 DWORD *rgba;
849 if (info.hbmMask)
851 BYTE *mask;
853 mask = HeapAlloc(GetProcessHeap(), 0, size);
854 if (!mask)
856 IWICBitmapLock_Release(lock);
857 IWICBitmap_Release(*bitmap);
858 DeleteDC(hdc);
859 hr = E_OUTOFMEMORY;
860 goto failed;
863 /* read alpha data from the mask */
864 GetDIBits(hdc, info.hbmMask, info.hbmColor ? 0 : height, height, mask, &bi, DIB_RGB_COLORS);
866 for (y = 0; y < height; y++)
868 rgba = (DWORD *)(buffer + y * stride);
869 bits = (DWORD *)(mask + y * stride);
871 for (x = 0; x < width; x++, rgba++, bits++)
873 if (*bits)
874 *rgba = 0;
875 else
876 *rgba |= 0xff000000;
880 HeapFree(GetProcessHeap(), 0, mask);
882 else
884 /* set constant alpha of 255 */
885 for (y = 0; y < height; y++)
887 rgba = (DWORD *)(buffer + y * stride);
888 for (x = 0; x < width; x++, rgba++)
889 *rgba |= 0xff000000;
895 IWICBitmapLock_Release(lock);
896 DeleteDC(hdc);
898 failed:
899 DeleteObject(info.hbmColor);
900 DeleteObject(info.hbmMask);
902 return hr;
905 static HRESULT WINAPI ComponentFactory_CreateComponentEnumerator(IWICComponentFactory *iface,
906 DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
908 TRACE("(%p,%u,%u,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
909 return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
912 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromDecoder(
913 IWICComponentFactory *iface, IWICBitmapDecoder *pIDecoder,
914 IWICFastMetadataEncoder **ppIFastEncoder)
916 FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
917 return E_NOTIMPL;
920 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromFrameDecode(
921 IWICComponentFactory *iface, IWICBitmapFrameDecode *pIFrameDecoder,
922 IWICFastMetadataEncoder **ppIFastEncoder)
924 FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
925 return E_NOTIMPL;
928 static HRESULT WINAPI ComponentFactory_CreateQueryWriter(IWICComponentFactory *iface,
929 REFGUID guidMetadataFormat, const GUID *pguidVendor,
930 IWICMetadataQueryWriter **ppIQueryWriter)
932 FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
933 debugstr_guid(pguidVendor), ppIQueryWriter);
934 return E_NOTIMPL;
937 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromReader(IWICComponentFactory *iface,
938 IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
939 IWICMetadataQueryWriter **ppIQueryWriter)
941 FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
942 ppIQueryWriter);
943 return E_NOTIMPL;
946 static HRESULT WINAPI ComponentFactory_CreateMetadataReader(IWICComponentFactory *iface,
947 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
949 FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
950 options, stream, reader);
951 return E_NOTIMPL;
954 static HRESULT WINAPI ComponentFactory_CreateMetadataReaderFromContainer(IWICComponentFactory *iface,
955 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
957 HRESULT hr;
958 IEnumUnknown *enumreaders;
959 IUnknown *unkreaderinfo;
960 IWICMetadataReaderInfo *readerinfo;
961 IWICPersistStream *wicpersiststream;
962 ULONG num_fetched;
963 GUID decoder_vendor;
964 BOOL matches;
965 LARGE_INTEGER zero;
967 TRACE("%p,%s,%s,%x,%p,%p\n", iface, debugstr_guid(format), debugstr_guid(vendor),
968 options, stream, reader);
970 if (!format || !stream || !reader)
971 return E_INVALIDARG;
973 zero.QuadPart = 0;
975 hr = CreateComponentEnumerator(WICMetadataReader, WICComponentEnumerateDefault, &enumreaders);
976 if (FAILED(hr)) return hr;
978 *reader = NULL;
980 start:
981 while (!*reader)
983 hr = IEnumUnknown_Next(enumreaders, 1, &unkreaderinfo, &num_fetched);
985 if (hr == S_OK)
987 hr = IUnknown_QueryInterface(unkreaderinfo, &IID_IWICMetadataReaderInfo, (void**)&readerinfo);
989 if (SUCCEEDED(hr))
991 if (vendor)
993 hr = IWICMetadataReaderInfo_GetVendorGUID(readerinfo, &decoder_vendor);
995 if (FAILED(hr) || !IsEqualIID(vendor, &decoder_vendor))
997 IWICMetadataReaderInfo_Release(readerinfo);
998 IUnknown_Release(unkreaderinfo);
999 continue;
1003 hr = IWICMetadataReaderInfo_MatchesPattern(readerinfo, format, stream, &matches);
1005 if (SUCCEEDED(hr) && matches)
1007 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
1009 if (SUCCEEDED(hr))
1010 hr = IWICMetadataReaderInfo_CreateInstance(readerinfo, reader);
1012 if (SUCCEEDED(hr))
1014 hr = IWICMetadataReader_QueryInterface(*reader, &IID_IWICPersistStream, (void**)&wicpersiststream);
1016 if (SUCCEEDED(hr))
1018 hr = IWICPersistStream_LoadEx(wicpersiststream,
1019 stream, vendor, options & WICPersistOptionMask);
1021 IWICPersistStream_Release(wicpersiststream);
1024 if (FAILED(hr))
1026 IWICMetadataReader_Release(*reader);
1027 *reader = NULL;
1032 IUnknown_Release(readerinfo);
1035 IUnknown_Release(unkreaderinfo);
1037 else
1038 break;
1041 if (!*reader && vendor)
1043 vendor = NULL;
1044 IEnumUnknown_Reset(enumreaders);
1045 goto start;
1048 IEnumUnknown_Release(enumreaders);
1050 if (!*reader && !(options & WICMetadataCreationFailUnknown))
1052 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
1054 if (SUCCEEDED(hr))
1055 hr = UnknownMetadataReader_CreateInstance(&IID_IWICMetadataReader, (void**)reader);
1057 if (SUCCEEDED(hr))
1059 hr = IWICMetadataReader_QueryInterface(*reader, &IID_IWICPersistStream, (void**)&wicpersiststream);
1061 if (SUCCEEDED(hr))
1063 hr = IWICPersistStream_LoadEx(wicpersiststream, stream, NULL, options & WICPersistOptionMask);
1065 IWICPersistStream_Release(wicpersiststream);
1068 if (FAILED(hr))
1070 IWICMetadataReader_Release(*reader);
1071 *reader = NULL;
1076 if (*reader)
1077 return S_OK;
1078 else
1079 return WINCODEC_ERR_COMPONENTNOTFOUND;
1082 static HRESULT WINAPI ComponentFactory_CreateMetadataWriter(IWICComponentFactory *iface,
1083 REFGUID format, const GUID *vendor, DWORD options, IWICMetadataWriter **writer)
1085 FIXME("%p,%s,%s,%x,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor), options, writer);
1086 return E_NOTIMPL;
1089 static HRESULT WINAPI ComponentFactory_CreateMetadataWriterFromReader(IWICComponentFactory *iface,
1090 IWICMetadataReader *reader, const GUID *vendor, IWICMetadataWriter **writer)
1092 FIXME("%p,%p,%s,%p: stub\n", iface, reader, debugstr_guid(vendor), writer);
1093 return E_NOTIMPL;
1096 static HRESULT WINAPI ComponentFactory_CreateQueryReaderFromBlockReader(IWICComponentFactory *iface,
1097 IWICMetadataBlockReader *block_reader, IWICMetadataQueryReader **query_reader)
1099 TRACE("%p,%p,%p\n", iface, block_reader, query_reader);
1101 if (!block_reader || !query_reader)
1102 return E_INVALIDARG;
1104 return MetadataQueryReader_CreateInstance(block_reader, query_reader);
1107 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromBlockWriter(IWICComponentFactory *iface,
1108 IWICMetadataBlockWriter *block_writer, IWICMetadataQueryWriter **query_writer)
1110 FIXME("%p,%p,%p: stub\n", iface, block_writer, query_writer);
1111 return E_NOTIMPL;
1114 static HRESULT WINAPI ComponentFactory_CreateEncoderPropertyBag(IWICComponentFactory *iface,
1115 PROPBAG2 *options, UINT count, IPropertyBag2 **property)
1117 TRACE("(%p,%p,%u,%p)\n", iface, options, count, property);
1118 return CreatePropertyBag2(options, count, property);
1121 static const IWICComponentFactoryVtbl ComponentFactory_Vtbl = {
1122 ComponentFactory_QueryInterface,
1123 ComponentFactory_AddRef,
1124 ComponentFactory_Release,
1125 ComponentFactory_CreateDecoderFromFilename,
1126 ComponentFactory_CreateDecoderFromStream,
1127 ComponentFactory_CreateDecoderFromFileHandle,
1128 ComponentFactory_CreateComponentInfo,
1129 ComponentFactory_CreateDecoder,
1130 ComponentFactory_CreateEncoder,
1131 ComponentFactory_CreatePalette,
1132 ComponentFactory_CreateFormatConverter,
1133 ComponentFactory_CreateBitmapScaler,
1134 ComponentFactory_CreateBitmapClipper,
1135 ComponentFactory_CreateBitmapFlipRotator,
1136 ComponentFactory_CreateStream,
1137 ComponentFactory_CreateColorContext,
1138 ComponentFactory_CreateColorTransformer,
1139 ComponentFactory_CreateBitmap,
1140 ComponentFactory_CreateBitmapFromSource,
1141 ComponentFactory_CreateBitmapFromSourceRect,
1142 ComponentFactory_CreateBitmapFromMemory,
1143 ComponentFactory_CreateBitmapFromHBITMAP,
1144 ComponentFactory_CreateBitmapFromHICON,
1145 ComponentFactory_CreateComponentEnumerator,
1146 ComponentFactory_CreateFastMetadataEncoderFromDecoder,
1147 ComponentFactory_CreateFastMetadataEncoderFromFrameDecode,
1148 ComponentFactory_CreateQueryWriter,
1149 ComponentFactory_CreateQueryWriterFromReader,
1150 ComponentFactory_CreateMetadataReader,
1151 ComponentFactory_CreateMetadataReaderFromContainer,
1152 ComponentFactory_CreateMetadataWriter,
1153 ComponentFactory_CreateMetadataWriterFromReader,
1154 ComponentFactory_CreateQueryReaderFromBlockReader,
1155 ComponentFactory_CreateQueryWriterFromBlockWriter,
1156 ComponentFactory_CreateEncoderPropertyBag
1159 HRESULT ComponentFactory_CreateInstance(REFIID iid, void** ppv)
1161 ComponentFactory *This;
1162 HRESULT ret;
1164 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1166 *ppv = NULL;
1168 This = HeapAlloc(GetProcessHeap(), 0, sizeof(ComponentFactory));
1169 if (!This) return E_OUTOFMEMORY;
1171 This->IWICComponentFactory_iface.lpVtbl = &ComponentFactory_Vtbl;
1172 This->ref = 1;
1174 ret = IWICComponentFactory_QueryInterface(&This->IWICComponentFactory_iface, iid, ppv);
1175 IWICComponentFactory_Release(&This->IWICComponentFactory_iface);
1177 return ret;