kernel32/tests: Add a test to check some fields in fake dlls.
[wine.git] / dlls / windowscodecs / imgfactory.c
blobe7c61cff7011034a2a17d3ea6fbffdd38f271a03
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 <assert.h>
23 #include <stdarg.h>
25 #define COBJMACROS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "objbase.h"
31 #include "shellapi.h"
33 #include "wincodecs_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
39 typedef struct {
40 IWICImagingFactory2 IWICImagingFactory2_iface;
41 IWICComponentFactory IWICComponentFactory_iface;
42 LONG ref;
43 } ImagingFactory;
45 static inline ImagingFactory *impl_from_IWICComponentFactory(IWICComponentFactory *iface)
47 return CONTAINING_RECORD(iface, ImagingFactory, IWICComponentFactory_iface);
50 static inline ImagingFactory *impl_from_IWICImagingFactory2(IWICImagingFactory2 *iface)
52 return CONTAINING_RECORD(iface, ImagingFactory, IWICImagingFactory2_iface);
55 static HRESULT WINAPI ImagingFactory_QueryInterface(IWICImagingFactory2 *iface, REFIID iid,
56 void **ppv)
58 ImagingFactory *This = impl_from_IWICImagingFactory2(iface);
59 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
61 if (!ppv) return E_INVALIDARG;
63 if (IsEqualIID(&IID_IUnknown, iid) ||
64 IsEqualIID(&IID_IWICImagingFactory, iid) ||
65 IsEqualIID(&IID_IWICComponentFactory, iid))
67 *ppv = &This->IWICComponentFactory_iface;
69 else if (IsEqualIID(&IID_IWICImagingFactory2, iid))
71 *ppv = &This->IWICImagingFactory2_iface;
73 else
75 *ppv = NULL;
76 return E_NOINTERFACE;
79 IUnknown_AddRef((IUnknown*)*ppv);
80 return S_OK;
83 static ULONG WINAPI ImagingFactory_AddRef(IWICImagingFactory2 *iface)
85 ImagingFactory *This = impl_from_IWICImagingFactory2(iface);
86 ULONG ref = InterlockedIncrement(&This->ref);
88 TRACE("(%p) refcount=%u\n", iface, ref);
90 return ref;
93 static ULONG WINAPI ImagingFactory_Release(IWICImagingFactory2 *iface)
95 ImagingFactory *This = impl_from_IWICImagingFactory2(iface);
96 ULONG ref = InterlockedDecrement(&This->ref);
98 TRACE("(%p) refcount=%u\n", iface, ref);
100 if (ref == 0)
101 HeapFree(GetProcessHeap(), 0, This);
103 return ref;
106 static HRESULT WINAPI ImagingFactory_CreateDecoderFromFilename(
107 IWICImagingFactory2 *iface, LPCWSTR wzFilename, const GUID *pguidVendor,
108 DWORD dwDesiredAccess, WICDecodeOptions metadataOptions,
109 IWICBitmapDecoder **ppIDecoder)
111 IWICStream *stream;
112 HRESULT hr;
114 TRACE("(%p,%s,%s,%u,%u,%p)\n", iface, debugstr_w(wzFilename),
115 debugstr_guid(pguidVendor), dwDesiredAccess, metadataOptions, ppIDecoder);
117 hr = StreamImpl_Create(&stream);
118 if (SUCCEEDED(hr))
120 hr = IWICStream_InitializeFromFilename(stream, wzFilename, dwDesiredAccess);
122 if (SUCCEEDED(hr))
124 hr = IWICImagingFactory2_CreateDecoderFromStream(iface, (IStream*)stream,
125 pguidVendor, metadataOptions, ppIDecoder);
128 IWICStream_Release(stream);
131 return hr;
134 static HRESULT find_decoder(IStream *pIStream, const GUID *pguidVendor,
135 WICDecodeOptions metadataOptions, IWICBitmapDecoder **decoder)
137 IEnumUnknown *enumdecoders;
138 IUnknown *unkdecoderinfo;
139 IWICBitmapDecoderInfo *decoderinfo;
140 GUID vendor;
141 HRESULT res;
142 ULONG num_fetched;
143 BOOL matches;
145 *decoder = NULL;
147 res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
148 if (FAILED(res)) return res;
150 while (!*decoder)
152 res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
154 if (res == S_OK)
156 res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void**)&decoderinfo);
158 if (SUCCEEDED(res))
160 if (pguidVendor)
162 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
163 if (FAILED(res) || !IsEqualIID(&vendor, pguidVendor))
165 IWICBitmapDecoderInfo_Release(decoderinfo);
166 IUnknown_Release(unkdecoderinfo);
167 continue;
171 res = IWICBitmapDecoderInfo_MatchesPattern(decoderinfo, pIStream, &matches);
173 if (SUCCEEDED(res) && matches)
175 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, decoder);
177 /* FIXME: should use QueryCapability to choose a decoder */
179 if (SUCCEEDED(res))
181 res = IWICBitmapDecoder_Initialize(*decoder, pIStream, metadataOptions);
183 if (FAILED(res))
185 IWICBitmapDecoder_Release(*decoder);
186 IWICBitmapDecoderInfo_Release(decoderinfo);
187 IUnknown_Release(unkdecoderinfo);
188 IEnumUnknown_Release(enumdecoders);
189 *decoder = NULL;
190 return res;
195 IWICBitmapDecoderInfo_Release(decoderinfo);
198 IUnknown_Release(unkdecoderinfo);
200 else
201 break;
204 IEnumUnknown_Release(enumdecoders);
206 return WINCODEC_ERR_COMPONENTNOTFOUND;
209 static HRESULT WINAPI ImagingFactory_CreateDecoderFromStream(
210 IWICImagingFactory2 *iface, IStream *pIStream, const GUID *pguidVendor,
211 WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
213 HRESULT res;
214 IWICBitmapDecoder *decoder = NULL;
216 TRACE("(%p,%p,%s,%u,%p)\n", iface, pIStream, debugstr_guid(pguidVendor),
217 metadataOptions, ppIDecoder);
219 if (pguidVendor)
220 res = find_decoder(pIStream, pguidVendor, metadataOptions, &decoder);
221 if (!decoder)
222 res = find_decoder(pIStream, NULL, metadataOptions, &decoder);
224 if (decoder)
226 *ppIDecoder = decoder;
227 return S_OK;
229 else
231 if (WARN_ON(wincodecs))
233 LARGE_INTEGER seek;
234 BYTE data[4];
235 ULONG bytesread;
237 WARN("failed to load from a stream %#x\n", res);
239 seek.QuadPart = 0;
240 if (IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL) == S_OK)
242 if (IStream_Read(pIStream, data, 4, &bytesread) == S_OK)
243 WARN("first %i bytes of stream=%x %x %x %x\n", bytesread, data[0], data[1], data[2], data[3]);
246 *ppIDecoder = NULL;
247 return res;
251 static HRESULT WINAPI ImagingFactory_CreateDecoderFromFileHandle(
252 IWICImagingFactory2 *iface, ULONG_PTR hFile, const GUID *pguidVendor,
253 WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
255 IWICStream *stream;
256 HRESULT hr;
258 TRACE("(%p,%lx,%s,%u,%p)\n", iface, hFile, debugstr_guid(pguidVendor),
259 metadataOptions, ppIDecoder);
261 hr = StreamImpl_Create(&stream);
262 if (SUCCEEDED(hr))
264 hr = stream_initialize_from_filehandle(stream, (HANDLE)hFile);
265 if (SUCCEEDED(hr))
267 hr = IWICImagingFactory2_CreateDecoderFromStream(iface, (IStream*)stream,
268 pguidVendor, metadataOptions, ppIDecoder);
270 IWICStream_Release(stream);
272 return hr;
275 static HRESULT WINAPI ImagingFactory_CreateComponentInfo(IWICImagingFactory2 *iface,
276 REFCLSID clsidComponent, IWICComponentInfo **ppIInfo)
278 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(clsidComponent), ppIInfo);
279 return CreateComponentInfo(clsidComponent, ppIInfo);
282 static HRESULT WINAPI ImagingFactory_CreateDecoder(IWICImagingFactory2 *iface,
283 REFGUID guidContainerFormat, const GUID *pguidVendor,
284 IWICBitmapDecoder **ppIDecoder)
286 IEnumUnknown *enumdecoders;
287 IUnknown *unkdecoderinfo;
288 IWICBitmapDecoderInfo *decoderinfo;
289 IWICBitmapDecoder *decoder = NULL, *preferred_decoder = NULL;
290 GUID vendor;
291 HRESULT res;
292 ULONG num_fetched;
294 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
295 debugstr_guid(pguidVendor), ppIDecoder);
297 if (!guidContainerFormat || !ppIDecoder) return E_INVALIDARG;
299 res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
300 if (FAILED(res)) return res;
302 while (!preferred_decoder)
304 res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
305 if (res != S_OK) break;
307 res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void **)&decoderinfo);
308 if (SUCCEEDED(res))
310 GUID container_guid;
312 res = IWICBitmapDecoderInfo_GetContainerFormat(decoderinfo, &container_guid);
313 if (SUCCEEDED(res) && IsEqualIID(&container_guid, guidContainerFormat))
315 IWICBitmapDecoder *new_decoder;
317 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &new_decoder);
318 if (SUCCEEDED(res))
320 if (pguidVendor)
322 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
323 if (SUCCEEDED(res) && IsEqualIID(&vendor, pguidVendor))
325 preferred_decoder = new_decoder;
326 new_decoder = NULL;
330 if (new_decoder && !decoder)
332 decoder = new_decoder;
333 new_decoder = NULL;
336 if (new_decoder) IWICBitmapDecoder_Release(new_decoder);
340 IWICBitmapDecoderInfo_Release(decoderinfo);
343 IUnknown_Release(unkdecoderinfo);
346 IEnumUnknown_Release(enumdecoders);
348 if (preferred_decoder)
350 *ppIDecoder = preferred_decoder;
351 if (decoder) IWICBitmapDecoder_Release(decoder);
352 return S_OK;
355 if (decoder)
357 *ppIDecoder = decoder;
358 return S_OK;
361 *ppIDecoder = NULL;
362 return WINCODEC_ERR_COMPONENTNOTFOUND;
365 static HRESULT WINAPI ImagingFactory_CreateEncoder(IWICImagingFactory2 *iface,
366 REFGUID guidContainerFormat, const GUID *pguidVendor,
367 IWICBitmapEncoder **ppIEncoder)
369 static int fixme=0;
370 IEnumUnknown *enumencoders;
371 IUnknown *unkencoderinfo;
372 IWICBitmapEncoderInfo *encoderinfo;
373 IWICBitmapEncoder *encoder=NULL;
374 HRESULT res=S_OK;
375 ULONG num_fetched;
376 GUID actual_containerformat;
378 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
379 debugstr_guid(pguidVendor), ppIEncoder);
381 if (pguidVendor && !fixme++)
382 FIXME("ignoring vendor GUID\n");
384 res = CreateComponentEnumerator(WICEncoder, WICComponentEnumerateDefault, &enumencoders);
385 if (FAILED(res)) return res;
387 while (!encoder)
389 res = IEnumUnknown_Next(enumencoders, 1, &unkencoderinfo, &num_fetched);
391 if (res == S_OK)
393 res = IUnknown_QueryInterface(unkencoderinfo, &IID_IWICBitmapEncoderInfo, (void**)&encoderinfo);
395 if (SUCCEEDED(res))
397 res = IWICBitmapEncoderInfo_GetContainerFormat(encoderinfo, &actual_containerformat);
399 if (SUCCEEDED(res) && IsEqualGUID(guidContainerFormat, &actual_containerformat))
401 res = IWICBitmapEncoderInfo_CreateInstance(encoderinfo, &encoder);
402 if (FAILED(res))
403 encoder = NULL;
406 IWICBitmapEncoderInfo_Release(encoderinfo);
409 IUnknown_Release(unkencoderinfo);
411 else
412 break;
415 IEnumUnknown_Release(enumencoders);
417 if (encoder)
419 *ppIEncoder = encoder;
420 return S_OK;
422 else
424 WARN("failed to create encoder\n");
425 *ppIEncoder = NULL;
426 return WINCODEC_ERR_COMPONENTNOTFOUND;
430 static HRESULT WINAPI ImagingFactory_CreatePalette(IWICImagingFactory2 *iface,
431 IWICPalette **ppIPalette)
433 TRACE("(%p,%p)\n", iface, ppIPalette);
434 return PaletteImpl_Create(ppIPalette);
437 static HRESULT WINAPI ImagingFactory_CreateFormatConverter(IWICImagingFactory2 *iface,
438 IWICFormatConverter **ppIFormatConverter)
440 return FormatConverter_CreateInstance(&IID_IWICFormatConverter, (void**)ppIFormatConverter);
443 static HRESULT WINAPI ImagingFactory_CreateBitmapScaler(IWICImagingFactory2 *iface,
444 IWICBitmapScaler **ppIBitmapScaler)
446 TRACE("(%p,%p)\n", iface, ppIBitmapScaler);
448 return BitmapScaler_Create(ppIBitmapScaler);
451 static HRESULT WINAPI ImagingFactory_CreateBitmapClipper(IWICImagingFactory2 *iface,
452 IWICBitmapClipper **ppIBitmapClipper)
454 TRACE("(%p,%p)\n", iface, ppIBitmapClipper);
455 return BitmapClipper_Create(ppIBitmapClipper);
458 static HRESULT WINAPI ImagingFactory_CreateBitmapFlipRotator(IWICImagingFactory2 *iface,
459 IWICBitmapFlipRotator **ppIBitmapFlipRotator)
461 TRACE("(%p,%p)\n", iface, ppIBitmapFlipRotator);
462 return FlipRotator_Create(ppIBitmapFlipRotator);
465 static HRESULT WINAPI ImagingFactory_CreateStream(IWICImagingFactory2 *iface,
466 IWICStream **ppIWICStream)
468 TRACE("(%p,%p)\n", iface, ppIWICStream);
469 return StreamImpl_Create(ppIWICStream);
472 static HRESULT WINAPI ImagingFactory_CreateColorContext(IWICImagingFactory2 *iface,
473 IWICColorContext **ppIColorContext)
475 TRACE("(%p,%p)\n", iface, ppIColorContext);
476 return ColorContext_Create(ppIColorContext);
479 static HRESULT WINAPI ImagingFactory_CreateColorTransformer(IWICImagingFactory2 *iface,
480 IWICColorTransform **ppIColorTransform)
482 TRACE("(%p,%p)\n", iface, ppIColorTransform);
483 return ColorTransform_Create(ppIColorTransform);
486 static HRESULT WINAPI ImagingFactory_CreateBitmap(IWICImagingFactory2 *iface,
487 UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat,
488 WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
490 TRACE("(%p,%u,%u,%s,%u,%p)\n", iface, uiWidth, uiHeight,
491 debugstr_guid(pixelFormat), option, ppIBitmap);
492 return BitmapImpl_Create(uiWidth, uiHeight, 0, 0, NULL, 0, pixelFormat, option, ppIBitmap);
495 static HRESULT create_bitmap_from_source_rect(IWICBitmapSource *piBitmapSource, const WICRect *rect,
496 WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
498 IWICBitmap *result;
499 IWICBitmapLock *lock;
500 IWICPalette *palette;
501 UINT width, height;
502 WICPixelFormatGUID pixelformat = {0};
503 HRESULT hr;
504 WICRect rc;
505 double dpix, dpiy;
506 IWICComponentInfo *info;
507 IWICPixelFormatInfo2 *formatinfo;
508 WICPixelFormatNumericRepresentation format_type;
510 assert(!rect || option == WICBitmapCacheOnLoad);
512 if (!piBitmapSource || !ppIBitmap)
513 return E_INVALIDARG;
515 if (option == WICBitmapNoCache && SUCCEEDED(IWICBitmapSource_QueryInterface(piBitmapSource,
516 &IID_IWICBitmap, (void **)&result)))
518 *ppIBitmap = result;
519 return S_OK;
522 hr = IWICBitmapSource_GetSize(piBitmapSource, &width, &height);
524 if (SUCCEEDED(hr) && rect)
526 if (rect->X >= width || rect->Y >= height || rect->Width == 0 || rect->Height == 0)
527 return E_INVALIDARG;
529 width = min(width - rect->X, rect->Width);
530 height = min(height - rect->Y, rect->Height);
533 if (SUCCEEDED(hr))
534 hr = IWICBitmapSource_GetPixelFormat(piBitmapSource, &pixelformat);
536 if (SUCCEEDED(hr))
537 hr = CreateComponentInfo(&pixelformat, &info);
539 if (SUCCEEDED(hr))
541 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo2, (void**)&formatinfo);
543 if (SUCCEEDED(hr))
545 hr = IWICPixelFormatInfo2_GetNumericRepresentation(formatinfo, &format_type);
547 IWICPixelFormatInfo2_Release(formatinfo);
550 IWICComponentInfo_Release(info);
553 if (SUCCEEDED(hr))
554 hr = BitmapImpl_Create(width, height, 0, 0, NULL, 0, &pixelformat, option, &result);
556 if (SUCCEEDED(hr))
558 hr = IWICBitmap_Lock(result, NULL, WICBitmapLockWrite, &lock);
559 if (SUCCEEDED(hr))
561 UINT stride, buffersize;
562 BYTE *buffer;
564 if (rect)
566 rc.X = rect->X;
567 rc.Y = rect->Y;
569 else
570 rc.X = rc.Y = 0;
571 rc.Width = width;
572 rc.Height = height;
574 hr = IWICBitmapLock_GetStride(lock, &stride);
576 if (SUCCEEDED(hr))
577 hr = IWICBitmapLock_GetDataPointer(lock, &buffersize, &buffer);
579 if (SUCCEEDED(hr))
580 hr = IWICBitmapSource_CopyPixels(piBitmapSource, &rc, stride,
581 buffersize, buffer);
583 IWICBitmapLock_Release(lock);
586 if (SUCCEEDED(hr) && (format_type == WICPixelFormatNumericRepresentationUnspecified ||
587 format_type == WICPixelFormatNumericRepresentationIndexed))
589 hr = PaletteImpl_Create(&palette);
591 if (SUCCEEDED(hr))
593 hr = IWICBitmapSource_CopyPalette(piBitmapSource, palette);
595 if (SUCCEEDED(hr))
596 hr = IWICBitmap_SetPalette(result, palette);
597 else
598 hr = S_OK;
600 IWICPalette_Release(palette);
604 if (SUCCEEDED(hr))
606 hr = IWICBitmapSource_GetResolution(piBitmapSource, &dpix, &dpiy);
608 if (SUCCEEDED(hr))
609 hr = IWICBitmap_SetResolution(result, dpix, dpiy);
610 else
611 hr = S_OK;
614 if (SUCCEEDED(hr))
615 *ppIBitmap = result;
616 else
617 IWICBitmap_Release(result);
620 return hr;
623 static HRESULT WINAPI ImagingFactory_CreateBitmapFromSource(IWICImagingFactory2 *iface,
624 IWICBitmapSource *piBitmapSource, WICBitmapCreateCacheOption option,
625 IWICBitmap **ppIBitmap)
627 TRACE("(%p,%p,%u,%p)\n", iface, piBitmapSource, option, ppIBitmap);
629 return create_bitmap_from_source_rect(piBitmapSource, NULL, option, ppIBitmap);
632 static HRESULT WINAPI ImagingFactory_CreateBitmapFromSourceRect(IWICImagingFactory2 *iface,
633 IWICBitmapSource *piBitmapSource, UINT x, UINT y, UINT width, UINT height,
634 IWICBitmap **ppIBitmap)
636 WICRect rect;
638 TRACE("(%p,%p,%u,%u,%u,%u,%p)\n", iface, piBitmapSource, x, y, width,
639 height, ppIBitmap);
641 rect.X = x;
642 rect.Y = y;
643 rect.Width = width;
644 rect.Height = height;
646 return create_bitmap_from_source_rect(piBitmapSource, &rect, WICBitmapCacheOnLoad, ppIBitmap);
649 static HRESULT WINAPI ImagingFactory_CreateBitmapFromMemory(IWICImagingFactory2 *iface,
650 UINT width, UINT height, REFWICPixelFormatGUID format, UINT stride,
651 UINT size, BYTE *buffer, IWICBitmap **bitmap)
653 HRESULT hr;
655 TRACE("(%p,%u,%u,%s,%u,%u,%p,%p\n", iface, width, height,
656 debugstr_guid(format), stride, size, buffer, bitmap);
658 if (!stride || !size || !buffer || !bitmap) return E_INVALIDARG;
660 hr = BitmapImpl_Create(width, height, stride, size, NULL, 0, format, WICBitmapCacheOnLoad, bitmap);
661 if (SUCCEEDED(hr))
663 IWICBitmapLock *lock;
665 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
666 if (SUCCEEDED(hr))
668 UINT buffersize;
669 BYTE *data;
671 IWICBitmapLock_GetDataPointer(lock, &buffersize, &data);
672 memcpy(data, buffer, buffersize);
674 IWICBitmapLock_Release(lock);
676 else
678 IWICBitmap_Release(*bitmap);
679 *bitmap = NULL;
682 return hr;
685 static BOOL get_16bpp_format(HBITMAP hbm, WICPixelFormatGUID *format)
687 BOOL ret = TRUE;
688 BITMAPV4HEADER bmh;
689 HDC hdc;
691 hdc = CreateCompatibleDC(0);
693 memset(&bmh, 0, sizeof(bmh));
694 bmh.bV4Size = sizeof(bmh);
695 bmh.bV4Width = 1;
696 bmh.bV4Height = 1;
697 bmh.bV4V4Compression = BI_BITFIELDS;
698 bmh.bV4BitCount = 16;
700 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO *)&bmh, DIB_RGB_COLORS);
702 if (bmh.bV4RedMask == 0x7c00 &&
703 bmh.bV4GreenMask == 0x3e0 &&
704 bmh.bV4BlueMask == 0x1f)
706 *format = GUID_WICPixelFormat16bppBGR555;
708 else if (bmh.bV4RedMask == 0xf800 &&
709 bmh.bV4GreenMask == 0x7e0 &&
710 bmh.bV4BlueMask == 0x1f)
712 *format = GUID_WICPixelFormat16bppBGR565;
714 else
716 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
717 bmh.bV4GreenMask, bmh.bV4BlueMask);
718 ret = FALSE;
721 DeleteDC(hdc);
722 return ret;
725 static HRESULT WINAPI ImagingFactory_CreateBitmapFromHBITMAP(IWICImagingFactory2 *iface,
726 HBITMAP hbm, HPALETTE hpal, WICBitmapAlphaChannelOption option, IWICBitmap **bitmap)
728 BITMAP bm;
729 HRESULT hr;
730 WICPixelFormatGUID format;
731 IWICBitmapLock *lock;
732 UINT size, num_palette_entries = 0;
733 PALETTEENTRY entry[256];
735 TRACE("(%p,%p,%p,%u,%p)\n", iface, hbm, hpal, option, bitmap);
737 if (!bitmap) return E_INVALIDARG;
739 if (GetObjectW(hbm, sizeof(bm), &bm) != sizeof(bm))
740 return WINCODEC_ERR_WIN32ERROR;
742 if (hpal)
744 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
745 if (!num_palette_entries)
746 return WINCODEC_ERR_WIN32ERROR;
749 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
750 switch(bm.bmBitsPixel)
752 case 1:
753 format = GUID_WICPixelFormat1bppIndexed;
754 break;
755 case 4:
756 format = GUID_WICPixelFormat4bppIndexed;
757 break;
758 case 8:
759 format = GUID_WICPixelFormat8bppIndexed;
760 break;
761 case 16:
762 if (!get_16bpp_format(hbm, &format))
763 return E_INVALIDARG;
764 break;
765 case 24:
766 format = GUID_WICPixelFormat24bppBGR;
767 break;
768 case 32:
769 switch (option)
771 case WICBitmapUseAlpha:
772 format = GUID_WICPixelFormat32bppBGRA;
773 break;
774 case WICBitmapUsePremultipliedAlpha:
775 format = GUID_WICPixelFormat32bppPBGRA;
776 break;
777 case WICBitmapIgnoreAlpha:
778 format = GUID_WICPixelFormat32bppBGR;
779 break;
780 default:
781 return E_INVALIDARG;
783 break;
784 case 48:
785 format = GUID_WICPixelFormat48bppRGB;
786 break;
787 default:
788 FIXME("unsupported %d bpp\n", bm.bmBitsPixel);
789 return E_INVALIDARG;
792 hr = BitmapImpl_Create(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, 0, NULL, 0, &format,
793 WICBitmapCacheOnLoad, bitmap);
794 if (hr != S_OK) return hr;
796 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
797 if (hr == S_OK)
799 BYTE *buffer;
800 HDC hdc;
801 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
802 BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
804 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
806 hdc = CreateCompatibleDC(0);
808 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
809 bmi->bmiHeader.biBitCount = 0;
810 GetDIBits(hdc, hbm, 0, 0, NULL, bmi, DIB_RGB_COLORS);
811 bmi->bmiHeader.biHeight = -bm.bmHeight;
812 GetDIBits(hdc, hbm, 0, bm.bmHeight, buffer, bmi, DIB_RGB_COLORS);
814 DeleteDC(hdc);
815 IWICBitmapLock_Release(lock);
817 if (num_palette_entries)
819 IWICPalette *palette;
820 WICColor colors[256];
821 UINT i;
823 hr = PaletteImpl_Create(&palette);
824 if (hr == S_OK)
826 for (i = 0; i < num_palette_entries; i++)
827 colors[i] = 0xff000000 | entry[i].peRed << 16 |
828 entry[i].peGreen << 8 | entry[i].peBlue;
830 hr = IWICPalette_InitializeCustom(palette, colors, num_palette_entries);
831 if (hr == S_OK)
832 hr = IWICBitmap_SetPalette(*bitmap, palette);
834 IWICPalette_Release(palette);
839 if (hr != S_OK)
841 IWICBitmap_Release(*bitmap);
842 *bitmap = NULL;
845 return hr;
848 static HRESULT WINAPI ImagingFactory_CreateBitmapFromHICON(IWICImagingFactory2 *iface,
849 HICON hicon, IWICBitmap **bitmap)
851 IWICBitmapLock *lock;
852 ICONINFO info;
853 BITMAP bm;
854 int width, height, x, y;
855 UINT stride, size;
856 BYTE *buffer;
857 DWORD *bits;
858 BITMAPINFO bi;
859 HDC hdc;
860 BOOL has_alpha;
861 HRESULT hr;
863 TRACE("(%p,%p,%p)\n", iface, hicon, bitmap);
865 if (!bitmap) return E_INVALIDARG;
867 if (!GetIconInfo(hicon, &info))
868 return HRESULT_FROM_WIN32(GetLastError());
870 GetObjectW(info.hbmColor ? info.hbmColor : info.hbmMask, sizeof(bm), &bm);
872 width = bm.bmWidth;
873 height = info.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
874 stride = width * 4;
875 size = stride * height;
877 hr = BitmapImpl_Create(width, height, stride, size, NULL, 0,
878 &GUID_WICPixelFormat32bppBGRA, WICBitmapCacheOnLoad, bitmap);
879 if (hr != S_OK) goto failed;
881 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
882 if (hr != S_OK)
884 IWICBitmap_Release(*bitmap);
885 goto failed;
887 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
889 hdc = CreateCompatibleDC(0);
891 memset(&bi, 0, sizeof(bi));
892 bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
893 bi.bmiHeader.biWidth = width;
894 bi.bmiHeader.biHeight = info.hbmColor ? -height: -height * 2;
895 bi.bmiHeader.biPlanes = 1;
896 bi.bmiHeader.biBitCount = 32;
897 bi.bmiHeader.biCompression = BI_RGB;
899 has_alpha = FALSE;
901 if (info.hbmColor)
903 GetDIBits(hdc, info.hbmColor, 0, height, buffer, &bi, DIB_RGB_COLORS);
905 if (bm.bmBitsPixel == 32)
907 /* If any pixel has a non-zero alpha, ignore hbmMask */
908 bits = (DWORD *)buffer;
909 for (x = 0; x < width && !has_alpha; x++, bits++)
911 for (y = 0; y < height; y++)
913 if (*bits & 0xff000000)
915 has_alpha = TRUE;
916 break;
922 else
923 GetDIBits(hdc, info.hbmMask, 0, height, buffer, &bi, DIB_RGB_COLORS);
925 if (!has_alpha)
927 DWORD *rgba;
929 if (info.hbmMask)
931 BYTE *mask;
933 mask = HeapAlloc(GetProcessHeap(), 0, size);
934 if (!mask)
936 IWICBitmapLock_Release(lock);
937 IWICBitmap_Release(*bitmap);
938 DeleteDC(hdc);
939 hr = E_OUTOFMEMORY;
940 goto failed;
943 /* read alpha data from the mask */
944 GetDIBits(hdc, info.hbmMask, info.hbmColor ? 0 : height, height, mask, &bi, DIB_RGB_COLORS);
946 for (y = 0; y < height; y++)
948 rgba = (DWORD *)(buffer + y * stride);
949 bits = (DWORD *)(mask + y * stride);
951 for (x = 0; x < width; x++, rgba++, bits++)
953 if (*bits)
954 *rgba = 0;
955 else
956 *rgba |= 0xff000000;
960 HeapFree(GetProcessHeap(), 0, mask);
962 else
964 /* set constant alpha of 255 */
965 for (y = 0; y < height; y++)
967 rgba = (DWORD *)(buffer + y * stride);
968 for (x = 0; x < width; x++, rgba++)
969 *rgba |= 0xff000000;
975 IWICBitmapLock_Release(lock);
976 DeleteDC(hdc);
978 failed:
979 DeleteObject(info.hbmColor);
980 DeleteObject(info.hbmMask);
982 return hr;
985 static HRESULT WINAPI ImagingFactory_CreateComponentEnumerator(IWICImagingFactory2 *iface,
986 DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
988 TRACE("(%p,%u,%u,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
989 return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
992 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromDecoder(
993 IWICImagingFactory2 *iface, IWICBitmapDecoder *pIDecoder,
994 IWICFastMetadataEncoder **ppIFastEncoder)
996 FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
997 return E_NOTIMPL;
1000 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromFrameDecode(
1001 IWICImagingFactory2 *iface, IWICBitmapFrameDecode *pIFrameDecoder,
1002 IWICFastMetadataEncoder **ppIFastEncoder)
1004 FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
1005 return E_NOTIMPL;
1008 static HRESULT WINAPI ImagingFactory_CreateQueryWriter(IWICImagingFactory2 *iface,
1009 REFGUID guidMetadataFormat, const GUID *pguidVendor,
1010 IWICMetadataQueryWriter **ppIQueryWriter)
1012 FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
1013 debugstr_guid(pguidVendor), ppIQueryWriter);
1014 return E_NOTIMPL;
1017 static HRESULT WINAPI ImagingFactory_CreateQueryWriterFromReader(IWICImagingFactory2 *iface,
1018 IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
1019 IWICMetadataQueryWriter **ppIQueryWriter)
1021 FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
1022 ppIQueryWriter);
1023 return E_NOTIMPL;
1026 static HRESULT WINAPI ImagingFactory_CreateImageEncoder(IWICImagingFactory2 *iface, ID2D1Device *device, IWICImageEncoder **encoder)
1028 FIXME("%p,%p,%p stub.\n", iface, device, encoder);
1029 return E_NOTIMPL;
1032 static const IWICImagingFactory2Vtbl ImagingFactory_Vtbl = {
1033 ImagingFactory_QueryInterface,
1034 ImagingFactory_AddRef,
1035 ImagingFactory_Release,
1036 ImagingFactory_CreateDecoderFromFilename,
1037 ImagingFactory_CreateDecoderFromStream,
1038 ImagingFactory_CreateDecoderFromFileHandle,
1039 ImagingFactory_CreateComponentInfo,
1040 ImagingFactory_CreateDecoder,
1041 ImagingFactory_CreateEncoder,
1042 ImagingFactory_CreatePalette,
1043 ImagingFactory_CreateFormatConverter,
1044 ImagingFactory_CreateBitmapScaler,
1045 ImagingFactory_CreateBitmapClipper,
1046 ImagingFactory_CreateBitmapFlipRotator,
1047 ImagingFactory_CreateStream,
1048 ImagingFactory_CreateColorContext,
1049 ImagingFactory_CreateColorTransformer,
1050 ImagingFactory_CreateBitmap,
1051 ImagingFactory_CreateBitmapFromSource,
1052 ImagingFactory_CreateBitmapFromSourceRect,
1053 ImagingFactory_CreateBitmapFromMemory,
1054 ImagingFactory_CreateBitmapFromHBITMAP,
1055 ImagingFactory_CreateBitmapFromHICON,
1056 ImagingFactory_CreateComponentEnumerator,
1057 ImagingFactory_CreateFastMetadataEncoderFromDecoder,
1058 ImagingFactory_CreateFastMetadataEncoderFromFrameDecode,
1059 ImagingFactory_CreateQueryWriter,
1060 ImagingFactory_CreateQueryWriterFromReader,
1061 ImagingFactory_CreateImageEncoder,
1064 static HRESULT WINAPI ComponentFactory_QueryInterface(IWICComponentFactory *iface, REFIID iid, void **ppv)
1066 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1067 return IWICImagingFactory2_QueryInterface(&This->IWICImagingFactory2_iface, iid, ppv);
1070 static ULONG WINAPI ComponentFactory_AddRef(IWICComponentFactory *iface)
1072 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1073 return IWICImagingFactory2_AddRef(&This->IWICImagingFactory2_iface);
1076 static ULONG WINAPI ComponentFactory_Release(IWICComponentFactory *iface)
1078 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1079 return IWICImagingFactory2_Release(&This->IWICImagingFactory2_iface);
1082 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFilename(IWICComponentFactory *iface, LPCWSTR filename,
1083 const GUID *vendor, DWORD desired_access, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1085 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1086 return IWICImagingFactory2_CreateDecoderFromFilename(&This->IWICImagingFactory2_iface, filename, vendor,
1087 desired_access, options, decoder);
1090 static HRESULT WINAPI ComponentFactory_CreateDecoderFromStream(IWICComponentFactory *iface, IStream *stream,
1091 const GUID *vendor, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1093 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1094 return IWICImagingFactory2_CreateDecoderFromStream(&This->IWICImagingFactory2_iface, stream, vendor,
1095 options, decoder);
1098 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFileHandle(IWICComponentFactory *iface, ULONG_PTR hFile,
1099 const GUID *vendor, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1101 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1102 return IWICImagingFactory2_CreateDecoderFromFileHandle(&This->IWICImagingFactory2_iface, hFile, vendor,
1103 options, decoder);
1106 static HRESULT WINAPI ComponentFactory_CreateComponentInfo(IWICComponentFactory *iface, REFCLSID component,
1107 IWICComponentInfo **info)
1109 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1110 return IWICImagingFactory2_CreateComponentInfo(&This->IWICImagingFactory2_iface, component, info);
1113 static HRESULT WINAPI ComponentFactory_CreateDecoder(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1114 IWICBitmapDecoder **decoder)
1116 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1117 return IWICImagingFactory2_CreateDecoder(&This->IWICImagingFactory2_iface, format, vendor, decoder);
1120 static HRESULT WINAPI ComponentFactory_CreateEncoder(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1121 IWICBitmapEncoder **encoder)
1123 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1124 return IWICImagingFactory2_CreateEncoder(&This->IWICImagingFactory2_iface, format, vendor, encoder);
1127 static HRESULT WINAPI ComponentFactory_CreatePalette(IWICComponentFactory *iface, IWICPalette **palette)
1129 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1130 return IWICImagingFactory2_CreatePalette(&This->IWICImagingFactory2_iface, palette);
1133 static HRESULT WINAPI ComponentFactory_CreateFormatConverter(IWICComponentFactory *iface, IWICFormatConverter **converter)
1135 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1136 return IWICImagingFactory2_CreateFormatConverter(&This->IWICImagingFactory2_iface, converter);
1139 static HRESULT WINAPI ComponentFactory_CreateBitmapScaler(IWICComponentFactory *iface, IWICBitmapScaler **scaler)
1141 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1142 return IWICImagingFactory2_CreateBitmapScaler(&This->IWICImagingFactory2_iface, scaler);
1145 static HRESULT WINAPI ComponentFactory_CreateBitmapClipper(IWICComponentFactory *iface, IWICBitmapClipper **clipper)
1147 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1148 return IWICImagingFactory2_CreateBitmapClipper(&This->IWICImagingFactory2_iface, clipper);
1151 static HRESULT WINAPI ComponentFactory_CreateBitmapFlipRotator(IWICComponentFactory *iface, IWICBitmapFlipRotator **fliprotator)
1153 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1154 return IWICImagingFactory2_CreateBitmapFlipRotator(&This->IWICImagingFactory2_iface, fliprotator);
1157 static HRESULT WINAPI ComponentFactory_CreateStream(IWICComponentFactory *iface, IWICStream **stream)
1159 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1160 return IWICImagingFactory2_CreateStream(&This->IWICImagingFactory2_iface, stream);
1163 static HRESULT WINAPI ComponentFactory_CreateColorContext(IWICComponentFactory *iface, IWICColorContext **context)
1165 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1166 return IWICImagingFactory2_CreateColorContext(&This->IWICImagingFactory2_iface, context);
1169 static HRESULT WINAPI ComponentFactory_CreateColorTransformer(IWICComponentFactory *iface, IWICColorTransform **transformer)
1171 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1172 return IWICImagingFactory2_CreateColorTransformer(&This->IWICImagingFactory2_iface, transformer);
1175 static HRESULT WINAPI ComponentFactory_CreateBitmap(IWICComponentFactory *iface, UINT width, UINT height, REFWICPixelFormatGUID pixel_format,
1176 WICBitmapCreateCacheOption option, IWICBitmap **bitmap)
1178 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1179 return IWICImagingFactory2_CreateBitmap(&This->IWICImagingFactory2_iface, width, height, pixel_format, option, bitmap);
1182 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSource(IWICComponentFactory *iface, IWICBitmapSource *source,
1183 WICBitmapCreateCacheOption option, IWICBitmap **bitmap)
1185 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1186 return IWICImagingFactory2_CreateBitmapFromSource(&This->IWICImagingFactory2_iface, source, option, bitmap);
1189 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSourceRect(IWICComponentFactory *iface, IWICBitmapSource *source,
1190 UINT x, UINT y, UINT width, UINT height, IWICBitmap **bitmap)
1192 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1193 return IWICImagingFactory2_CreateBitmapFromSourceRect(&This->IWICImagingFactory2_iface, source, x, y, width, height, bitmap);
1196 static HRESULT WINAPI ComponentFactory_CreateBitmapFromMemory(IWICComponentFactory *iface, UINT width, UINT height,
1197 REFWICPixelFormatGUID format, UINT stride, UINT size, BYTE *buffer, IWICBitmap **bitmap)
1199 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1200 return IWICImagingFactory2_CreateBitmapFromMemory(&This->IWICImagingFactory2_iface, width, height, format, stride,
1201 size, buffer, bitmap);
1204 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHBITMAP(IWICComponentFactory *iface, HBITMAP hbm, HPALETTE hpal,
1205 WICBitmapAlphaChannelOption option, IWICBitmap **bitmap)
1207 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1208 return IWICImagingFactory2_CreateBitmapFromHBITMAP(&This->IWICImagingFactory2_iface, hbm, hpal, option, bitmap);
1211 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHICON(IWICComponentFactory *iface, HICON hicon, IWICBitmap **bitmap)
1213 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1214 return IWICImagingFactory2_CreateBitmapFromHICON(&This->IWICImagingFactory2_iface, hicon, bitmap);
1217 static HRESULT WINAPI ComponentFactory_CreateComponentEnumerator(IWICComponentFactory *iface, DWORD component_types,
1218 DWORD options, IEnumUnknown **enumerator)
1220 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1221 return IWICImagingFactory2_CreateComponentEnumerator(&This->IWICImagingFactory2_iface, component_types,
1222 options, enumerator);
1225 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromDecoder(IWICComponentFactory *iface, IWICBitmapDecoder *decoder,
1226 IWICFastMetadataEncoder **encoder)
1228 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1229 return IWICImagingFactory2_CreateFastMetadataEncoderFromDecoder(&This->IWICImagingFactory2_iface, decoder, encoder);
1232 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromFrameDecode(IWICComponentFactory *iface,
1233 IWICBitmapFrameDecode *frame_decode, IWICFastMetadataEncoder **encoder)
1235 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1236 return IWICImagingFactory2_CreateFastMetadataEncoderFromFrameDecode(&This->IWICImagingFactory2_iface, frame_decode, encoder);
1239 static HRESULT WINAPI ComponentFactory_CreateQueryWriter(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1240 IWICMetadataQueryWriter **writer)
1242 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1243 return IWICImagingFactory2_CreateQueryWriter(&This->IWICImagingFactory2_iface, format, vendor, writer);
1246 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromReader(IWICComponentFactory *iface, IWICMetadataQueryReader *reader,
1247 const GUID *vendor, IWICMetadataQueryWriter **writer)
1249 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1250 return IWICImagingFactory2_CreateQueryWriterFromReader(&This->IWICImagingFactory2_iface, reader, vendor, writer);
1253 static HRESULT WINAPI ComponentFactory_CreateMetadataReader(IWICComponentFactory *iface,
1254 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
1256 FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
1257 options, stream, reader);
1258 return E_NOTIMPL;
1261 static HRESULT WINAPI ComponentFactory_CreateMetadataReaderFromContainer(IWICComponentFactory *iface,
1262 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
1264 HRESULT hr;
1265 IEnumUnknown *enumreaders;
1266 IUnknown *unkreaderinfo;
1267 IWICMetadataReaderInfo *readerinfo;
1268 IWICPersistStream *wicpersiststream;
1269 ULONG num_fetched;
1270 GUID decoder_vendor;
1271 BOOL matches;
1272 LARGE_INTEGER zero;
1274 TRACE("%p,%s,%s,%x,%p,%p\n", iface, debugstr_guid(format), debugstr_guid(vendor),
1275 options, stream, reader);
1277 if (!format || !stream || !reader)
1278 return E_INVALIDARG;
1280 zero.QuadPart = 0;
1282 hr = CreateComponentEnumerator(WICMetadataReader, WICComponentEnumerateDefault, &enumreaders);
1283 if (FAILED(hr)) return hr;
1285 *reader = NULL;
1287 start:
1288 while (!*reader)
1290 hr = IEnumUnknown_Next(enumreaders, 1, &unkreaderinfo, &num_fetched);
1292 if (hr == S_OK)
1294 hr = IUnknown_QueryInterface(unkreaderinfo, &IID_IWICMetadataReaderInfo, (void**)&readerinfo);
1296 if (SUCCEEDED(hr))
1298 if (vendor)
1300 hr = IWICMetadataReaderInfo_GetVendorGUID(readerinfo, &decoder_vendor);
1302 if (FAILED(hr) || !IsEqualIID(vendor, &decoder_vendor))
1304 IWICMetadataReaderInfo_Release(readerinfo);
1305 IUnknown_Release(unkreaderinfo);
1306 continue;
1310 hr = IWICMetadataReaderInfo_MatchesPattern(readerinfo, format, stream, &matches);
1312 if (SUCCEEDED(hr) && matches)
1314 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
1316 if (SUCCEEDED(hr))
1317 hr = IWICMetadataReaderInfo_CreateInstance(readerinfo, reader);
1319 if (SUCCEEDED(hr))
1321 hr = IWICMetadataReader_QueryInterface(*reader, &IID_IWICPersistStream, (void**)&wicpersiststream);
1323 if (SUCCEEDED(hr))
1325 hr = IWICPersistStream_LoadEx(wicpersiststream,
1326 stream, vendor, options & WICPersistOptionMask);
1328 IWICPersistStream_Release(wicpersiststream);
1331 if (FAILED(hr))
1333 IWICMetadataReader_Release(*reader);
1334 *reader = NULL;
1339 IUnknown_Release(readerinfo);
1342 IUnknown_Release(unkreaderinfo);
1344 else
1345 break;
1348 if (!*reader && vendor)
1350 vendor = NULL;
1351 IEnumUnknown_Reset(enumreaders);
1352 goto start;
1355 IEnumUnknown_Release(enumreaders);
1357 if (!*reader && !(options & WICMetadataCreationFailUnknown))
1359 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
1361 if (SUCCEEDED(hr))
1362 hr = UnknownMetadataReader_CreateInstance(&IID_IWICMetadataReader, (void**)reader);
1364 if (SUCCEEDED(hr))
1366 hr = IWICMetadataReader_QueryInterface(*reader, &IID_IWICPersistStream, (void**)&wicpersiststream);
1368 if (SUCCEEDED(hr))
1370 hr = IWICPersistStream_LoadEx(wicpersiststream, stream, NULL, options & WICPersistOptionMask);
1372 IWICPersistStream_Release(wicpersiststream);
1375 if (FAILED(hr))
1377 IWICMetadataReader_Release(*reader);
1378 *reader = NULL;
1383 if (*reader)
1384 return S_OK;
1385 else
1386 return WINCODEC_ERR_COMPONENTNOTFOUND;
1389 static HRESULT WINAPI ComponentFactory_CreateMetadataWriter(IWICComponentFactory *iface,
1390 REFGUID format, const GUID *vendor, DWORD options, IWICMetadataWriter **writer)
1392 FIXME("%p,%s,%s,%x,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor), options, writer);
1393 return E_NOTIMPL;
1396 static HRESULT WINAPI ComponentFactory_CreateMetadataWriterFromReader(IWICComponentFactory *iface,
1397 IWICMetadataReader *reader, const GUID *vendor, IWICMetadataWriter **writer)
1399 FIXME("%p,%p,%s,%p: stub\n", iface, reader, debugstr_guid(vendor), writer);
1400 return E_NOTIMPL;
1403 static HRESULT WINAPI ComponentFactory_CreateQueryReaderFromBlockReader(IWICComponentFactory *iface,
1404 IWICMetadataBlockReader *block_reader, IWICMetadataQueryReader **query_reader)
1406 TRACE("%p,%p,%p\n", iface, block_reader, query_reader);
1408 if (!block_reader || !query_reader)
1409 return E_INVALIDARG;
1411 return MetadataQueryReader_CreateInstance(block_reader, NULL, query_reader);
1414 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromBlockWriter(IWICComponentFactory *iface,
1415 IWICMetadataBlockWriter *block_writer, IWICMetadataQueryWriter **query_writer)
1417 FIXME("%p,%p,%p: stub\n", iface, block_writer, query_writer);
1418 return E_NOTIMPL;
1421 static HRESULT WINAPI ComponentFactory_CreateEncoderPropertyBag(IWICComponentFactory *iface,
1422 PROPBAG2 *options, UINT count, IPropertyBag2 **property)
1424 TRACE("(%p,%p,%u,%p)\n", iface, options, count, property);
1425 return CreatePropertyBag2(options, count, property);
1428 static const IWICComponentFactoryVtbl ComponentFactory_Vtbl = {
1429 ComponentFactory_QueryInterface,
1430 ComponentFactory_AddRef,
1431 ComponentFactory_Release,
1432 ComponentFactory_CreateDecoderFromFilename,
1433 ComponentFactory_CreateDecoderFromStream,
1434 ComponentFactory_CreateDecoderFromFileHandle,
1435 ComponentFactory_CreateComponentInfo,
1436 ComponentFactory_CreateDecoder,
1437 ComponentFactory_CreateEncoder,
1438 ComponentFactory_CreatePalette,
1439 ComponentFactory_CreateFormatConverter,
1440 ComponentFactory_CreateBitmapScaler,
1441 ComponentFactory_CreateBitmapClipper,
1442 ComponentFactory_CreateBitmapFlipRotator,
1443 ComponentFactory_CreateStream,
1444 ComponentFactory_CreateColorContext,
1445 ComponentFactory_CreateColorTransformer,
1446 ComponentFactory_CreateBitmap,
1447 ComponentFactory_CreateBitmapFromSource,
1448 ComponentFactory_CreateBitmapFromSourceRect,
1449 ComponentFactory_CreateBitmapFromMemory,
1450 ComponentFactory_CreateBitmapFromHBITMAP,
1451 ComponentFactory_CreateBitmapFromHICON,
1452 ComponentFactory_CreateComponentEnumerator,
1453 ComponentFactory_CreateFastMetadataEncoderFromDecoder,
1454 ComponentFactory_CreateFastMetadataEncoderFromFrameDecode,
1455 ComponentFactory_CreateQueryWriter,
1456 ComponentFactory_CreateQueryWriterFromReader,
1457 ComponentFactory_CreateMetadataReader,
1458 ComponentFactory_CreateMetadataReaderFromContainer,
1459 ComponentFactory_CreateMetadataWriter,
1460 ComponentFactory_CreateMetadataWriterFromReader,
1461 ComponentFactory_CreateQueryReaderFromBlockReader,
1462 ComponentFactory_CreateQueryWriterFromBlockWriter,
1463 ComponentFactory_CreateEncoderPropertyBag
1466 HRESULT ImagingFactory_CreateInstance(REFIID iid, void** ppv)
1468 ImagingFactory *This;
1469 HRESULT ret;
1471 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1473 *ppv = NULL;
1475 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1476 if (!This) return E_OUTOFMEMORY;
1478 This->IWICImagingFactory2_iface.lpVtbl = &ImagingFactory_Vtbl;
1479 This->IWICComponentFactory_iface.lpVtbl = &ComponentFactory_Vtbl;
1480 This->ref = 1;
1482 ret = IWICImagingFactory2_QueryInterface(&This->IWICImagingFactory2_iface, iid, ppv);
1483 IWICImagingFactory2_Release(&This->IWICImagingFactory2_iface);
1485 return ret;
1488 HRESULT WINAPI WICCreateBitmapFromSectionEx(UINT width, UINT height,
1489 REFWICPixelFormatGUID format, HANDLE section, UINT stride,
1490 UINT offset, WICSectionAccessLevel wicaccess, IWICBitmap **bitmap)
1492 SYSTEM_INFO sysinfo;
1493 UINT bpp, access, size, view_offset, view_size;
1494 void *view;
1495 HRESULT hr;
1497 TRACE("%u,%u,%s,%p,%u,%u,%#x,%p\n", width, height, debugstr_guid(format),
1498 section, stride, offset, wicaccess, bitmap);
1500 if (!width || !height || !section || !bitmap) return E_INVALIDARG;
1502 hr = get_pixelformat_bpp(format, &bpp);
1503 if (FAILED(hr)) return hr;
1505 switch (wicaccess)
1507 case WICSectionAccessLevelReadWrite:
1508 access = FILE_MAP_READ | FILE_MAP_WRITE;
1509 break;
1511 case WICSectionAccessLevelRead:
1512 access = FILE_MAP_READ;
1513 break;
1515 default:
1516 FIXME("unsupported access %#x\n", wicaccess);
1517 return E_INVALIDARG;
1520 if (!stride) stride = (((bpp * width) + 31) / 32) * 4;
1521 size = stride * height;
1522 if (size / height != stride) return E_INVALIDARG;
1524 GetSystemInfo(&sysinfo);
1525 view_offset = offset - (offset % sysinfo.dwAllocationGranularity);
1526 view_size = size + (offset - view_offset);
1528 view = MapViewOfFile(section, access, 0, view_offset, view_size);
1529 if (!view) return HRESULT_FROM_WIN32(GetLastError());
1531 offset -= view_offset;
1532 hr = BitmapImpl_Create(width, height, stride, 0, view, offset, format, WICBitmapCacheOnLoad, bitmap);
1533 if (FAILED(hr)) UnmapViewOfFile(view);
1534 return hr;
1537 HRESULT WINAPI WICCreateBitmapFromSection(UINT width, UINT height,
1538 REFWICPixelFormatGUID format, HANDLE section,
1539 UINT stride, UINT offset, IWICBitmap **bitmap)
1541 TRACE("%u,%u,%s,%p,%u,%u,%p\n", width, height, debugstr_guid(format),
1542 section, stride, offset, bitmap);
1544 return WICCreateBitmapFromSectionEx(width, height, format, section,
1545 stride, offset, WICSectionAccessLevelRead, bitmap);