include: Be consistent in naming regarding MSF's block.
[wine.git] / dlls / windowscodecs / imgfactory.c
blob07f5b53fefd88c7c681c67a6f92429c190cb169b
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 <assert.h>
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "objbase.h"
29 #include "shellapi.h"
31 #include "wincodecs_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
37 typedef struct {
38 IWICImagingFactory2 IWICImagingFactory2_iface;
39 IWICComponentFactory IWICComponentFactory_iface;
40 LONG ref;
41 } ImagingFactory;
43 static inline ImagingFactory *impl_from_IWICComponentFactory(IWICComponentFactory *iface)
45 return CONTAINING_RECORD(iface, ImagingFactory, IWICComponentFactory_iface);
48 static inline ImagingFactory *impl_from_IWICImagingFactory2(IWICImagingFactory2 *iface)
50 return CONTAINING_RECORD(iface, ImagingFactory, IWICImagingFactory2_iface);
53 static HRESULT WINAPI ImagingFactory_QueryInterface(IWICImagingFactory2 *iface, REFIID iid,
54 void **ppv)
56 ImagingFactory *This = impl_from_IWICImagingFactory2(iface);
57 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
59 if (!ppv) return E_INVALIDARG;
61 if (IsEqualIID(&IID_IUnknown, iid) ||
62 IsEqualIID(&IID_IWICImagingFactory, iid) ||
63 IsEqualIID(&IID_IWICComponentFactory, iid))
65 *ppv = &This->IWICComponentFactory_iface;
67 else if (IsEqualIID(&IID_IWICImagingFactory2, iid))
69 *ppv = &This->IWICImagingFactory2_iface;
71 else
73 *ppv = NULL;
74 return E_NOINTERFACE;
77 IUnknown_AddRef((IUnknown*)*ppv);
78 return S_OK;
81 static ULONG WINAPI ImagingFactory_AddRef(IWICImagingFactory2 *iface)
83 ImagingFactory *This = impl_from_IWICImagingFactory2(iface);
84 ULONG ref = InterlockedIncrement(&This->ref);
86 TRACE("(%p) refcount=%lu\n", iface, ref);
88 return ref;
91 static ULONG WINAPI ImagingFactory_Release(IWICImagingFactory2 *iface)
93 ImagingFactory *This = impl_from_IWICImagingFactory2(iface);
94 ULONG ref = InterlockedDecrement(&This->ref);
96 TRACE("(%p) refcount=%lu\n", iface, ref);
98 if (ref == 0)
99 HeapFree(GetProcessHeap(), 0, This);
101 return ref;
104 static HRESULT WINAPI ImagingFactory_CreateDecoderFromFilename(
105 IWICImagingFactory2 *iface, LPCWSTR wzFilename, const GUID *pguidVendor,
106 DWORD dwDesiredAccess, WICDecodeOptions metadataOptions,
107 IWICBitmapDecoder **ppIDecoder)
109 IWICStream *stream;
110 HRESULT hr;
112 TRACE("(%p,%s,%s,%lu,%u,%p)\n", iface, debugstr_w(wzFilename),
113 debugstr_guid(pguidVendor), dwDesiredAccess, metadataOptions, ppIDecoder);
115 hr = StreamImpl_Create(&stream);
116 if (SUCCEEDED(hr))
118 hr = IWICStream_InitializeFromFilename(stream, wzFilename, dwDesiredAccess);
120 if (SUCCEEDED(hr))
122 hr = IWICImagingFactory2_CreateDecoderFromStream(iface, (IStream*)stream,
123 pguidVendor, metadataOptions, ppIDecoder);
126 IWICStream_Release(stream);
129 return hr;
132 static HRESULT find_decoder(IStream *pIStream, const GUID *pguidVendor,
133 WICDecodeOptions metadataOptions, IWICBitmapDecoder **decoder)
135 IEnumUnknown *enumdecoders = NULL;
136 IUnknown *unkdecoderinfo = NULL;
137 GUID vendor;
138 HRESULT res, res_wine;
139 ULONG num_fetched;
140 BOOL matches, found;
142 *decoder = NULL;
144 res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
145 if (FAILED(res)) return res;
147 found = FALSE;
148 while (IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched) == S_OK)
150 IWICBitmapDecoderInfo *decoderinfo = NULL;
151 IWICWineDecoder *wine_decoder = NULL;
153 res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void**)&decoderinfo);
154 if (FAILED(res)) goto next;
156 if (pguidVendor)
158 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
159 if (FAILED(res) || !IsEqualIID(&vendor, pguidVendor)) goto next;
162 res = IWICBitmapDecoderInfo_MatchesPattern(decoderinfo, pIStream, &matches);
163 if (FAILED(res) || !matches) goto next;
165 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, decoder);
166 if (FAILED(res)) goto next;
168 /* FIXME: should use QueryCapability to choose a decoder */
170 found = TRUE;
171 res = IWICBitmapDecoder_Initialize(*decoder, pIStream, metadataOptions);
172 if (FAILED(res))
174 res_wine = IWICBitmapDecoder_QueryInterface(*decoder, &IID_IWICWineDecoder, (void **)&wine_decoder);
175 if (FAILED(res_wine))
177 IWICBitmapDecoder_Release(*decoder);
178 *decoder = NULL;
179 goto next;
182 res_wine = IWICWineDecoder_Initialize(wine_decoder, pIStream, metadataOptions);
183 if (FAILED(res_wine))
185 IWICBitmapDecoder_Release(*decoder);
186 *decoder = NULL;
187 goto next;
190 res = res_wine;
193 next:
194 if (wine_decoder) IWICWineDecoder_Release(wine_decoder);
195 if (decoderinfo) IWICBitmapDecoderInfo_Release(decoderinfo);
196 IUnknown_Release(unkdecoderinfo);
197 if (found) break;
200 IEnumUnknown_Release(enumdecoders);
201 if (!found) res = WINCODEC_ERR_COMPONENTNOTFOUND;
202 return res;
205 static HRESULT WINAPI ImagingFactory_CreateDecoderFromStream(
206 IWICImagingFactory2 *iface, IStream *pIStream, const GUID *pguidVendor,
207 WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
209 HRESULT res;
210 IWICBitmapDecoder *decoder = NULL;
212 TRACE("(%p,%p,%s,%u,%p)\n", iface, pIStream, debugstr_guid(pguidVendor),
213 metadataOptions, ppIDecoder);
215 if (pguidVendor)
216 res = find_decoder(pIStream, pguidVendor, metadataOptions, &decoder);
217 if (!decoder)
218 res = find_decoder(pIStream, NULL, metadataOptions, &decoder);
220 if (decoder)
222 *ppIDecoder = decoder;
223 return S_OK;
225 else
227 if (WARN_ON(wincodecs))
229 LARGE_INTEGER seek;
230 BYTE data[4];
231 ULONG bytesread;
233 WARN("failed to load from a stream %#lx\n", res);
235 seek.QuadPart = 0;
236 if (IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL) == S_OK)
238 if (IStream_Read(pIStream, data, 4, &bytesread) == S_OK)
239 WARN("first %li bytes of stream=%x %x %x %x\n", bytesread, data[0], data[1], data[2], data[3]);
242 *ppIDecoder = NULL;
243 return res;
247 static HRESULT WINAPI ImagingFactory_CreateDecoderFromFileHandle(
248 IWICImagingFactory2 *iface, ULONG_PTR hFile, const GUID *pguidVendor,
249 WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
251 IWICStream *stream;
252 HRESULT hr;
254 TRACE("(%p,%Ix,%s,%u,%p)\n", iface, hFile, debugstr_guid(pguidVendor),
255 metadataOptions, ppIDecoder);
257 hr = StreamImpl_Create(&stream);
258 if (SUCCEEDED(hr))
260 hr = stream_initialize_from_filehandle(stream, (HANDLE)hFile);
261 if (SUCCEEDED(hr))
263 hr = IWICImagingFactory2_CreateDecoderFromStream(iface, (IStream*)stream,
264 pguidVendor, metadataOptions, ppIDecoder);
266 IWICStream_Release(stream);
268 return hr;
271 static HRESULT WINAPI ImagingFactory_CreateComponentInfo(IWICImagingFactory2 *iface,
272 REFCLSID clsidComponent, IWICComponentInfo **ppIInfo)
274 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(clsidComponent), ppIInfo);
275 return CreateComponentInfo(clsidComponent, ppIInfo);
278 static HRESULT WINAPI ImagingFactory_CreateDecoder(IWICImagingFactory2 *iface,
279 REFGUID guidContainerFormat, const GUID *pguidVendor,
280 IWICBitmapDecoder **ppIDecoder)
282 IEnumUnknown *enumdecoders;
283 IUnknown *unkdecoderinfo;
284 IWICBitmapDecoderInfo *decoderinfo;
285 IWICBitmapDecoder *decoder = NULL, *preferred_decoder = NULL;
286 GUID vendor;
287 HRESULT res;
288 ULONG num_fetched;
290 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
291 debugstr_guid(pguidVendor), ppIDecoder);
293 if (!guidContainerFormat || !ppIDecoder) return E_INVALIDARG;
295 res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
296 if (FAILED(res)) return res;
298 while (!preferred_decoder)
300 res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
301 if (res != S_OK) break;
303 res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void **)&decoderinfo);
304 if (SUCCEEDED(res))
306 GUID container_guid;
308 res = IWICBitmapDecoderInfo_GetContainerFormat(decoderinfo, &container_guid);
309 if (SUCCEEDED(res) && IsEqualIID(&container_guid, guidContainerFormat))
311 IWICBitmapDecoder *new_decoder;
313 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &new_decoder);
314 if (SUCCEEDED(res))
316 if (pguidVendor)
318 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
319 if (SUCCEEDED(res) && IsEqualIID(&vendor, pguidVendor))
321 preferred_decoder = new_decoder;
322 new_decoder = NULL;
326 if (new_decoder && !decoder)
328 decoder = new_decoder;
329 new_decoder = NULL;
332 if (new_decoder) IWICBitmapDecoder_Release(new_decoder);
336 IWICBitmapDecoderInfo_Release(decoderinfo);
339 IUnknown_Release(unkdecoderinfo);
342 IEnumUnknown_Release(enumdecoders);
344 if (preferred_decoder)
346 *ppIDecoder = preferred_decoder;
347 if (decoder) IWICBitmapDecoder_Release(decoder);
348 return S_OK;
351 if (decoder)
353 *ppIDecoder = decoder;
354 return S_OK;
357 *ppIDecoder = NULL;
358 return WINCODEC_ERR_COMPONENTNOTFOUND;
361 static HRESULT WINAPI ImagingFactory_CreateEncoder(IWICImagingFactory2 *iface,
362 REFGUID guidContainerFormat, const GUID *pguidVendor,
363 IWICBitmapEncoder **ppIEncoder)
365 static int fixme=0;
366 IEnumUnknown *enumencoders;
367 IUnknown *unkencoderinfo;
368 IWICBitmapEncoderInfo *encoderinfo;
369 IWICBitmapEncoder *encoder=NULL;
370 HRESULT res=S_OK;
371 ULONG num_fetched;
372 GUID actual_containerformat;
374 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
375 debugstr_guid(pguidVendor), ppIEncoder);
377 if (pguidVendor && !fixme++)
378 FIXME("ignoring vendor GUID\n");
380 res = CreateComponentEnumerator(WICEncoder, WICComponentEnumerateDefault, &enumencoders);
381 if (FAILED(res)) return res;
383 while (!encoder)
385 res = IEnumUnknown_Next(enumencoders, 1, &unkencoderinfo, &num_fetched);
387 if (res == S_OK)
389 res = IUnknown_QueryInterface(unkencoderinfo, &IID_IWICBitmapEncoderInfo, (void**)&encoderinfo);
391 if (SUCCEEDED(res))
393 res = IWICBitmapEncoderInfo_GetContainerFormat(encoderinfo, &actual_containerformat);
395 if (SUCCEEDED(res) && IsEqualGUID(guidContainerFormat, &actual_containerformat))
397 res = IWICBitmapEncoderInfo_CreateInstance(encoderinfo, &encoder);
398 if (FAILED(res))
399 encoder = NULL;
402 IWICBitmapEncoderInfo_Release(encoderinfo);
405 IUnknown_Release(unkencoderinfo);
407 else
408 break;
411 IEnumUnknown_Release(enumencoders);
413 if (encoder)
415 *ppIEncoder = encoder;
416 return S_OK;
418 else
420 WARN("failed to create encoder\n");
421 *ppIEncoder = NULL;
422 return WINCODEC_ERR_COMPONENTNOTFOUND;
426 static HRESULT WINAPI ImagingFactory_CreatePalette(IWICImagingFactory2 *iface,
427 IWICPalette **ppIPalette)
429 TRACE("(%p,%p)\n", iface, ppIPalette);
430 return PaletteImpl_Create(ppIPalette);
433 static HRESULT WINAPI ImagingFactory_CreateFormatConverter(IWICImagingFactory2 *iface,
434 IWICFormatConverter **ppIFormatConverter)
436 return FormatConverter_CreateInstance(&IID_IWICFormatConverter, (void**)ppIFormatConverter);
439 static HRESULT WINAPI ImagingFactory_CreateBitmapScaler(IWICImagingFactory2 *iface,
440 IWICBitmapScaler **ppIBitmapScaler)
442 TRACE("(%p,%p)\n", iface, ppIBitmapScaler);
444 return BitmapScaler_Create(ppIBitmapScaler);
447 static HRESULT WINAPI ImagingFactory_CreateBitmapClipper(IWICImagingFactory2 *iface,
448 IWICBitmapClipper **ppIBitmapClipper)
450 TRACE("(%p,%p)\n", iface, ppIBitmapClipper);
451 return BitmapClipper_Create(ppIBitmapClipper);
454 static HRESULT WINAPI ImagingFactory_CreateBitmapFlipRotator(IWICImagingFactory2 *iface,
455 IWICBitmapFlipRotator **ppIBitmapFlipRotator)
457 TRACE("(%p,%p)\n", iface, ppIBitmapFlipRotator);
458 return FlipRotator_Create(ppIBitmapFlipRotator);
461 static HRESULT WINAPI ImagingFactory_CreateStream(IWICImagingFactory2 *iface,
462 IWICStream **ppIWICStream)
464 TRACE("(%p,%p)\n", iface, ppIWICStream);
465 return StreamImpl_Create(ppIWICStream);
468 static HRESULT WINAPI ImagingFactory_CreateColorContext(IWICImagingFactory2 *iface,
469 IWICColorContext **ppIColorContext)
471 TRACE("(%p,%p)\n", iface, ppIColorContext);
472 return ColorContext_Create(ppIColorContext);
475 static HRESULT WINAPI ImagingFactory_CreateColorTransformer(IWICImagingFactory2 *iface,
476 IWICColorTransform **ppIColorTransform)
478 TRACE("(%p,%p)\n", iface, ppIColorTransform);
479 return ColorTransform_Create(ppIColorTransform);
482 static HRESULT WINAPI ImagingFactory_CreateBitmap(IWICImagingFactory2 *iface,
483 UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat,
484 WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
486 TRACE("(%p,%u,%u,%s,%u,%p)\n", iface, uiWidth, uiHeight,
487 debugstr_guid(pixelFormat), option, ppIBitmap);
488 return BitmapImpl_Create(uiWidth, uiHeight, 0, 0, NULL, 0, pixelFormat, option, ppIBitmap);
491 static HRESULT create_bitmap_from_source_rect(IWICBitmapSource *piBitmapSource, const WICRect *rect,
492 WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
494 IWICBitmap *result;
495 IWICBitmapLock *lock;
496 IWICPalette *palette;
497 UINT width, height;
498 WICPixelFormatGUID pixelformat = {0};
499 HRESULT hr;
500 WICRect rc;
501 double dpix, dpiy;
502 IWICComponentInfo *info;
503 IWICPixelFormatInfo2 *formatinfo;
504 WICPixelFormatNumericRepresentation format_type;
506 assert(!rect || option == WICBitmapCacheOnLoad);
508 if (!piBitmapSource || !ppIBitmap)
509 return E_INVALIDARG;
511 if (option == WICBitmapNoCache && SUCCEEDED(IWICBitmapSource_QueryInterface(piBitmapSource,
512 &IID_IWICBitmap, (void **)&result)))
514 *ppIBitmap = result;
515 return S_OK;
518 hr = IWICBitmapSource_GetSize(piBitmapSource, &width, &height);
520 if (SUCCEEDED(hr) && rect)
522 if (rect->X >= width || rect->Y >= height || rect->Width == 0 || rect->Height == 0)
523 return E_INVALIDARG;
525 width = min(width - rect->X, rect->Width);
526 height = min(height - rect->Y, rect->Height);
529 if (SUCCEEDED(hr))
530 hr = IWICBitmapSource_GetPixelFormat(piBitmapSource, &pixelformat);
532 if (SUCCEEDED(hr))
533 hr = CreateComponentInfo(&pixelformat, &info);
535 if (SUCCEEDED(hr))
537 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo2, (void**)&formatinfo);
539 if (SUCCEEDED(hr))
541 hr = IWICPixelFormatInfo2_GetNumericRepresentation(formatinfo, &format_type);
543 IWICPixelFormatInfo2_Release(formatinfo);
546 IWICComponentInfo_Release(info);
549 if (SUCCEEDED(hr))
550 hr = BitmapImpl_Create(width, height, 0, 0, NULL, 0, &pixelformat, option, &result);
552 if (SUCCEEDED(hr))
554 hr = IWICBitmap_Lock(result, NULL, WICBitmapLockWrite, &lock);
555 if (SUCCEEDED(hr))
557 UINT stride, buffersize;
558 BYTE *buffer;
560 if (rect)
562 rc.X = rect->X;
563 rc.Y = rect->Y;
565 else
566 rc.X = rc.Y = 0;
567 rc.Width = width;
568 rc.Height = height;
570 hr = IWICBitmapLock_GetStride(lock, &stride);
572 if (SUCCEEDED(hr))
573 hr = IWICBitmapLock_GetDataPointer(lock, &buffersize, &buffer);
575 if (SUCCEEDED(hr))
576 hr = IWICBitmapSource_CopyPixels(piBitmapSource, &rc, stride,
577 buffersize, buffer);
579 IWICBitmapLock_Release(lock);
582 if (SUCCEEDED(hr) && (format_type == WICPixelFormatNumericRepresentationUnspecified ||
583 format_type == WICPixelFormatNumericRepresentationIndexed))
585 hr = PaletteImpl_Create(&palette);
587 if (SUCCEEDED(hr))
589 hr = IWICBitmapSource_CopyPalette(piBitmapSource, palette);
591 if (SUCCEEDED(hr))
592 hr = IWICBitmap_SetPalette(result, palette);
593 else
594 hr = S_OK;
596 IWICPalette_Release(palette);
600 if (SUCCEEDED(hr))
602 hr = IWICBitmapSource_GetResolution(piBitmapSource, &dpix, &dpiy);
604 if (SUCCEEDED(hr))
605 hr = IWICBitmap_SetResolution(result, dpix, dpiy);
606 else
607 hr = S_OK;
610 if (SUCCEEDED(hr))
611 *ppIBitmap = result;
612 else
613 IWICBitmap_Release(result);
616 return hr;
619 static HRESULT WINAPI ImagingFactory_CreateBitmapFromSource(IWICImagingFactory2 *iface,
620 IWICBitmapSource *piBitmapSource, WICBitmapCreateCacheOption option,
621 IWICBitmap **ppIBitmap)
623 TRACE("(%p,%p,%u,%p)\n", iface, piBitmapSource, option, ppIBitmap);
625 return create_bitmap_from_source_rect(piBitmapSource, NULL, option, ppIBitmap);
628 static HRESULT WINAPI ImagingFactory_CreateBitmapFromSourceRect(IWICImagingFactory2 *iface,
629 IWICBitmapSource *piBitmapSource, UINT x, UINT y, UINT width, UINT height,
630 IWICBitmap **ppIBitmap)
632 WICRect rect;
634 TRACE("(%p,%p,%u,%u,%u,%u,%p)\n", iface, piBitmapSource, x, y, width,
635 height, ppIBitmap);
637 rect.X = x;
638 rect.Y = y;
639 rect.Width = width;
640 rect.Height = height;
642 return create_bitmap_from_source_rect(piBitmapSource, &rect, WICBitmapCacheOnLoad, ppIBitmap);
645 static HRESULT WINAPI ImagingFactory_CreateBitmapFromMemory(IWICImagingFactory2 *iface,
646 UINT width, UINT height, REFWICPixelFormatGUID format, UINT stride,
647 UINT size, BYTE *buffer, IWICBitmap **bitmap)
649 HRESULT hr;
651 TRACE("(%p,%u,%u,%s,%u,%u,%p,%p\n", iface, width, height,
652 debugstr_guid(format), stride, size, buffer, bitmap);
654 if (!stride || !size || !buffer || !bitmap) return E_INVALIDARG;
656 hr = BitmapImpl_Create(width, height, stride, size, NULL, 0, format, WICBitmapCacheOnLoad, bitmap);
657 if (SUCCEEDED(hr))
659 IWICBitmapLock *lock;
661 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
662 if (SUCCEEDED(hr))
664 UINT buffersize;
665 BYTE *data;
667 IWICBitmapLock_GetDataPointer(lock, &buffersize, &data);
668 memcpy(data, buffer, buffersize);
670 IWICBitmapLock_Release(lock);
672 else
674 IWICBitmap_Release(*bitmap);
675 *bitmap = NULL;
678 return hr;
681 static BOOL get_16bpp_format(HBITMAP hbm, WICPixelFormatGUID *format)
683 BOOL ret = TRUE;
684 BITMAPV4HEADER bmh;
685 HDC hdc;
687 hdc = CreateCompatibleDC(0);
689 memset(&bmh, 0, sizeof(bmh));
690 bmh.bV4Size = sizeof(bmh);
691 bmh.bV4Width = 1;
692 bmh.bV4Height = 1;
693 bmh.bV4V4Compression = BI_BITFIELDS;
694 bmh.bV4BitCount = 16;
696 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO *)&bmh, DIB_RGB_COLORS);
698 if (bmh.bV4RedMask == 0x7c00 &&
699 bmh.bV4GreenMask == 0x3e0 &&
700 bmh.bV4BlueMask == 0x1f)
702 *format = GUID_WICPixelFormat16bppBGR555;
704 else if (bmh.bV4RedMask == 0xf800 &&
705 bmh.bV4GreenMask == 0x7e0 &&
706 bmh.bV4BlueMask == 0x1f)
708 *format = GUID_WICPixelFormat16bppBGR565;
710 else
712 FIXME("unrecognized bitfields %lx,%lx,%lx\n", bmh.bV4RedMask,
713 bmh.bV4GreenMask, bmh.bV4BlueMask);
714 ret = FALSE;
717 DeleteDC(hdc);
718 return ret;
721 static HRESULT WINAPI ImagingFactory_CreateBitmapFromHBITMAP(IWICImagingFactory2 *iface,
722 HBITMAP hbm, HPALETTE hpal, WICBitmapAlphaChannelOption option, IWICBitmap **bitmap)
724 BITMAP bm;
725 HRESULT hr;
726 WICPixelFormatGUID format;
727 IWICBitmapLock *lock;
728 UINT size, num_palette_entries = 0;
729 PALETTEENTRY entry[256];
731 TRACE("(%p,%p,%p,%u,%p)\n", iface, hbm, hpal, option, bitmap);
733 if (!bitmap) return E_INVALIDARG;
735 if (GetObjectW(hbm, sizeof(bm), &bm) != sizeof(bm))
736 return WINCODEC_ERR_WIN32ERROR;
738 if (hpal)
740 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
741 if (!num_palette_entries)
742 return WINCODEC_ERR_WIN32ERROR;
745 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
746 switch(bm.bmBitsPixel)
748 case 1:
749 format = GUID_WICPixelFormat1bppIndexed;
750 break;
751 case 4:
752 format = GUID_WICPixelFormat4bppIndexed;
753 break;
754 case 8:
755 format = GUID_WICPixelFormat8bppIndexed;
756 break;
757 case 16:
758 if (!get_16bpp_format(hbm, &format))
759 return E_INVALIDARG;
760 break;
761 case 24:
762 format = GUID_WICPixelFormat24bppBGR;
763 break;
764 case 32:
765 switch (option)
767 case WICBitmapUseAlpha:
768 format = GUID_WICPixelFormat32bppBGRA;
769 break;
770 case WICBitmapUsePremultipliedAlpha:
771 format = GUID_WICPixelFormat32bppPBGRA;
772 break;
773 case WICBitmapIgnoreAlpha:
774 format = GUID_WICPixelFormat32bppBGR;
775 break;
776 default:
777 return E_INVALIDARG;
779 break;
780 case 48:
781 format = GUID_WICPixelFormat48bppRGB;
782 break;
783 default:
784 FIXME("unsupported %d bpp\n", bm.bmBitsPixel);
785 return E_INVALIDARG;
788 hr = BitmapImpl_Create(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, 0, NULL, 0, &format,
789 WICBitmapCacheOnLoad, bitmap);
790 if (hr != S_OK) return hr;
792 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
793 if (hr == S_OK)
795 BYTE *buffer;
796 HDC hdc;
797 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
798 BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
800 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
802 hdc = CreateCompatibleDC(0);
804 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
805 bmi->bmiHeader.biBitCount = 0;
806 GetDIBits(hdc, hbm, 0, 0, NULL, bmi, DIB_RGB_COLORS);
807 bmi->bmiHeader.biHeight = -bm.bmHeight;
808 GetDIBits(hdc, hbm, 0, bm.bmHeight, buffer, bmi, DIB_RGB_COLORS);
810 DeleteDC(hdc);
811 IWICBitmapLock_Release(lock);
813 if (num_palette_entries)
815 IWICPalette *palette;
816 WICColor colors[256];
817 UINT i;
819 hr = PaletteImpl_Create(&palette);
820 if (hr == S_OK)
822 for (i = 0; i < num_palette_entries; i++)
823 colors[i] = 0xff000000 | entry[i].peRed << 16 |
824 entry[i].peGreen << 8 | entry[i].peBlue;
826 hr = IWICPalette_InitializeCustom(palette, colors, num_palette_entries);
827 if (hr == S_OK)
828 hr = IWICBitmap_SetPalette(*bitmap, palette);
830 IWICPalette_Release(palette);
835 if (hr != S_OK)
837 IWICBitmap_Release(*bitmap);
838 *bitmap = NULL;
841 return hr;
844 static HRESULT WINAPI ImagingFactory_CreateBitmapFromHICON(IWICImagingFactory2 *iface,
845 HICON hicon, IWICBitmap **bitmap)
847 IWICBitmapLock *lock;
848 ICONINFO info;
849 BITMAP bm;
850 int width, height, x, y;
851 UINT stride, size;
852 BYTE *buffer;
853 DWORD *bits;
854 BITMAPINFO bi;
855 HDC hdc;
856 BOOL has_alpha;
857 HRESULT hr;
859 TRACE("(%p,%p,%p)\n", iface, hicon, bitmap);
861 if (!bitmap) return E_INVALIDARG;
863 if (!GetIconInfo(hicon, &info))
864 return HRESULT_FROM_WIN32(GetLastError());
866 GetObjectW(info.hbmColor ? info.hbmColor : info.hbmMask, sizeof(bm), &bm);
868 width = bm.bmWidth;
869 height = info.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
870 stride = width * 4;
871 size = stride * height;
873 hr = BitmapImpl_Create(width, height, stride, size, NULL, 0,
874 &GUID_WICPixelFormat32bppBGRA, WICBitmapCacheOnLoad, bitmap);
875 if (hr != S_OK) goto failed;
877 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
878 if (hr != S_OK)
880 IWICBitmap_Release(*bitmap);
881 goto failed;
883 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
885 hdc = CreateCompatibleDC(0);
887 memset(&bi, 0, sizeof(bi));
888 bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
889 bi.bmiHeader.biWidth = width;
890 bi.bmiHeader.biHeight = info.hbmColor ? -height: -height * 2;
891 bi.bmiHeader.biPlanes = 1;
892 bi.bmiHeader.biBitCount = 32;
893 bi.bmiHeader.biCompression = BI_RGB;
895 has_alpha = FALSE;
897 if (info.hbmColor)
899 GetDIBits(hdc, info.hbmColor, 0, height, buffer, &bi, DIB_RGB_COLORS);
901 if (bm.bmBitsPixel == 32)
903 /* If any pixel has a non-zero alpha, ignore hbmMask */
904 DWORD *ptr = (DWORD *)buffer;
905 DWORD *end = ptr + width * height;
906 while (ptr != end)
908 if (*ptr++ & 0xff000000)
910 has_alpha = TRUE;
911 break;
916 else
917 GetDIBits(hdc, info.hbmMask, 0, height, buffer, &bi, DIB_RGB_COLORS);
919 if (!has_alpha)
921 DWORD *rgba;
923 if (info.hbmMask)
925 BYTE *mask;
927 mask = HeapAlloc(GetProcessHeap(), 0, size);
928 if (!mask)
930 IWICBitmapLock_Release(lock);
931 IWICBitmap_Release(*bitmap);
932 DeleteDC(hdc);
933 hr = E_OUTOFMEMORY;
934 goto failed;
937 /* read alpha data from the mask */
938 GetDIBits(hdc, info.hbmMask, info.hbmColor ? 0 : height, height, mask, &bi, DIB_RGB_COLORS);
940 for (y = 0; y < height; y++)
942 rgba = (DWORD *)(buffer + y * stride);
943 bits = (DWORD *)(mask + y * stride);
945 for (x = 0; x < width; x++, rgba++, bits++)
947 if (*bits)
948 *rgba = 0;
949 else
950 *rgba |= 0xff000000;
954 HeapFree(GetProcessHeap(), 0, mask);
956 else
958 /* set constant alpha of 255 */
959 for (y = 0; y < height; y++)
961 rgba = (DWORD *)(buffer + y * stride);
962 for (x = 0; x < width; x++, rgba++)
963 *rgba |= 0xff000000;
969 IWICBitmapLock_Release(lock);
970 DeleteDC(hdc);
972 failed:
973 DeleteObject(info.hbmColor);
974 DeleteObject(info.hbmMask);
976 return hr;
979 static HRESULT WINAPI ImagingFactory_CreateComponentEnumerator(IWICImagingFactory2 *iface,
980 DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
982 TRACE("(%p,%lu,%lu,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
983 return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
986 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromDecoder(
987 IWICImagingFactory2 *iface, IWICBitmapDecoder *pIDecoder,
988 IWICFastMetadataEncoder **ppIFastEncoder)
990 FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
991 return E_NOTIMPL;
994 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromFrameDecode(
995 IWICImagingFactory2 *iface, IWICBitmapFrameDecode *pIFrameDecoder,
996 IWICFastMetadataEncoder **ppIFastEncoder)
998 FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
999 return E_NOTIMPL;
1002 static HRESULT WINAPI ImagingFactory_CreateQueryWriter(IWICImagingFactory2 *iface,
1003 REFGUID guidMetadataFormat, const GUID *pguidVendor,
1004 IWICMetadataQueryWriter **ppIQueryWriter)
1006 FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
1007 debugstr_guid(pguidVendor), ppIQueryWriter);
1008 return E_NOTIMPL;
1011 static HRESULT WINAPI ImagingFactory_CreateQueryWriterFromReader(IWICImagingFactory2 *iface,
1012 IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
1013 IWICMetadataQueryWriter **ppIQueryWriter)
1015 FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
1016 ppIQueryWriter);
1017 return E_NOTIMPL;
1020 static HRESULT WINAPI ImagingFactory_CreateImageEncoder(IWICImagingFactory2 *iface, ID2D1Device *device, IWICImageEncoder **encoder)
1022 FIXME("%p,%p,%p stub.\n", iface, device, encoder);
1023 return E_NOTIMPL;
1026 static const IWICImagingFactory2Vtbl ImagingFactory_Vtbl = {
1027 ImagingFactory_QueryInterface,
1028 ImagingFactory_AddRef,
1029 ImagingFactory_Release,
1030 ImagingFactory_CreateDecoderFromFilename,
1031 ImagingFactory_CreateDecoderFromStream,
1032 ImagingFactory_CreateDecoderFromFileHandle,
1033 ImagingFactory_CreateComponentInfo,
1034 ImagingFactory_CreateDecoder,
1035 ImagingFactory_CreateEncoder,
1036 ImagingFactory_CreatePalette,
1037 ImagingFactory_CreateFormatConverter,
1038 ImagingFactory_CreateBitmapScaler,
1039 ImagingFactory_CreateBitmapClipper,
1040 ImagingFactory_CreateBitmapFlipRotator,
1041 ImagingFactory_CreateStream,
1042 ImagingFactory_CreateColorContext,
1043 ImagingFactory_CreateColorTransformer,
1044 ImagingFactory_CreateBitmap,
1045 ImagingFactory_CreateBitmapFromSource,
1046 ImagingFactory_CreateBitmapFromSourceRect,
1047 ImagingFactory_CreateBitmapFromMemory,
1048 ImagingFactory_CreateBitmapFromHBITMAP,
1049 ImagingFactory_CreateBitmapFromHICON,
1050 ImagingFactory_CreateComponentEnumerator,
1051 ImagingFactory_CreateFastMetadataEncoderFromDecoder,
1052 ImagingFactory_CreateFastMetadataEncoderFromFrameDecode,
1053 ImagingFactory_CreateQueryWriter,
1054 ImagingFactory_CreateQueryWriterFromReader,
1055 ImagingFactory_CreateImageEncoder,
1058 static HRESULT WINAPI ComponentFactory_QueryInterface(IWICComponentFactory *iface, REFIID iid, void **ppv)
1060 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1061 return IWICImagingFactory2_QueryInterface(&This->IWICImagingFactory2_iface, iid, ppv);
1064 static ULONG WINAPI ComponentFactory_AddRef(IWICComponentFactory *iface)
1066 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1067 return IWICImagingFactory2_AddRef(&This->IWICImagingFactory2_iface);
1070 static ULONG WINAPI ComponentFactory_Release(IWICComponentFactory *iface)
1072 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1073 return IWICImagingFactory2_Release(&This->IWICImagingFactory2_iface);
1076 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFilename(IWICComponentFactory *iface, LPCWSTR filename,
1077 const GUID *vendor, DWORD desired_access, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1079 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1080 return IWICImagingFactory2_CreateDecoderFromFilename(&This->IWICImagingFactory2_iface, filename, vendor,
1081 desired_access, options, decoder);
1084 static HRESULT WINAPI ComponentFactory_CreateDecoderFromStream(IWICComponentFactory *iface, IStream *stream,
1085 const GUID *vendor, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1087 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1088 return IWICImagingFactory2_CreateDecoderFromStream(&This->IWICImagingFactory2_iface, stream, vendor,
1089 options, decoder);
1092 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFileHandle(IWICComponentFactory *iface, ULONG_PTR hFile,
1093 const GUID *vendor, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1095 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1096 return IWICImagingFactory2_CreateDecoderFromFileHandle(&This->IWICImagingFactory2_iface, hFile, vendor,
1097 options, decoder);
1100 static HRESULT WINAPI ComponentFactory_CreateComponentInfo(IWICComponentFactory *iface, REFCLSID component,
1101 IWICComponentInfo **info)
1103 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1104 return IWICImagingFactory2_CreateComponentInfo(&This->IWICImagingFactory2_iface, component, info);
1107 static HRESULT WINAPI ComponentFactory_CreateDecoder(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1108 IWICBitmapDecoder **decoder)
1110 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1111 return IWICImagingFactory2_CreateDecoder(&This->IWICImagingFactory2_iface, format, vendor, decoder);
1114 static HRESULT WINAPI ComponentFactory_CreateEncoder(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1115 IWICBitmapEncoder **encoder)
1117 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1118 return IWICImagingFactory2_CreateEncoder(&This->IWICImagingFactory2_iface, format, vendor, encoder);
1121 static HRESULT WINAPI ComponentFactory_CreatePalette(IWICComponentFactory *iface, IWICPalette **palette)
1123 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1124 return IWICImagingFactory2_CreatePalette(&This->IWICImagingFactory2_iface, palette);
1127 static HRESULT WINAPI ComponentFactory_CreateFormatConverter(IWICComponentFactory *iface, IWICFormatConverter **converter)
1129 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1130 return IWICImagingFactory2_CreateFormatConverter(&This->IWICImagingFactory2_iface, converter);
1133 static HRESULT WINAPI ComponentFactory_CreateBitmapScaler(IWICComponentFactory *iface, IWICBitmapScaler **scaler)
1135 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1136 return IWICImagingFactory2_CreateBitmapScaler(&This->IWICImagingFactory2_iface, scaler);
1139 static HRESULT WINAPI ComponentFactory_CreateBitmapClipper(IWICComponentFactory *iface, IWICBitmapClipper **clipper)
1141 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1142 return IWICImagingFactory2_CreateBitmapClipper(&This->IWICImagingFactory2_iface, clipper);
1145 static HRESULT WINAPI ComponentFactory_CreateBitmapFlipRotator(IWICComponentFactory *iface, IWICBitmapFlipRotator **fliprotator)
1147 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1148 return IWICImagingFactory2_CreateBitmapFlipRotator(&This->IWICImagingFactory2_iface, fliprotator);
1151 static HRESULT WINAPI ComponentFactory_CreateStream(IWICComponentFactory *iface, IWICStream **stream)
1153 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1154 return IWICImagingFactory2_CreateStream(&This->IWICImagingFactory2_iface, stream);
1157 static HRESULT WINAPI ComponentFactory_CreateColorContext(IWICComponentFactory *iface, IWICColorContext **context)
1159 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1160 return IWICImagingFactory2_CreateColorContext(&This->IWICImagingFactory2_iface, context);
1163 static HRESULT WINAPI ComponentFactory_CreateColorTransformer(IWICComponentFactory *iface, IWICColorTransform **transformer)
1165 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1166 return IWICImagingFactory2_CreateColorTransformer(&This->IWICImagingFactory2_iface, transformer);
1169 static HRESULT WINAPI ComponentFactory_CreateBitmap(IWICComponentFactory *iface, UINT width, UINT height, REFWICPixelFormatGUID pixel_format,
1170 WICBitmapCreateCacheOption option, IWICBitmap **bitmap)
1172 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1173 return IWICImagingFactory2_CreateBitmap(&This->IWICImagingFactory2_iface, width, height, pixel_format, option, bitmap);
1176 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSource(IWICComponentFactory *iface, IWICBitmapSource *source,
1177 WICBitmapCreateCacheOption option, IWICBitmap **bitmap)
1179 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1180 return IWICImagingFactory2_CreateBitmapFromSource(&This->IWICImagingFactory2_iface, source, option, bitmap);
1183 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSourceRect(IWICComponentFactory *iface, IWICBitmapSource *source,
1184 UINT x, UINT y, UINT width, UINT height, IWICBitmap **bitmap)
1186 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1187 return IWICImagingFactory2_CreateBitmapFromSourceRect(&This->IWICImagingFactory2_iface, source, x, y, width, height, bitmap);
1190 static HRESULT WINAPI ComponentFactory_CreateBitmapFromMemory(IWICComponentFactory *iface, UINT width, UINT height,
1191 REFWICPixelFormatGUID format, UINT stride, UINT size, BYTE *buffer, IWICBitmap **bitmap)
1193 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1194 return IWICImagingFactory2_CreateBitmapFromMemory(&This->IWICImagingFactory2_iface, width, height, format, stride,
1195 size, buffer, bitmap);
1198 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHBITMAP(IWICComponentFactory *iface, HBITMAP hbm, HPALETTE hpal,
1199 WICBitmapAlphaChannelOption option, IWICBitmap **bitmap)
1201 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1202 return IWICImagingFactory2_CreateBitmapFromHBITMAP(&This->IWICImagingFactory2_iface, hbm, hpal, option, bitmap);
1205 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHICON(IWICComponentFactory *iface, HICON hicon, IWICBitmap **bitmap)
1207 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1208 return IWICImagingFactory2_CreateBitmapFromHICON(&This->IWICImagingFactory2_iface, hicon, bitmap);
1211 static HRESULT WINAPI ComponentFactory_CreateComponentEnumerator(IWICComponentFactory *iface, DWORD component_types,
1212 DWORD options, IEnumUnknown **enumerator)
1214 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1215 return IWICImagingFactory2_CreateComponentEnumerator(&This->IWICImagingFactory2_iface, component_types,
1216 options, enumerator);
1219 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromDecoder(IWICComponentFactory *iface, IWICBitmapDecoder *decoder,
1220 IWICFastMetadataEncoder **encoder)
1222 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1223 return IWICImagingFactory2_CreateFastMetadataEncoderFromDecoder(&This->IWICImagingFactory2_iface, decoder, encoder);
1226 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromFrameDecode(IWICComponentFactory *iface,
1227 IWICBitmapFrameDecode *frame_decode, IWICFastMetadataEncoder **encoder)
1229 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1230 return IWICImagingFactory2_CreateFastMetadataEncoderFromFrameDecode(&This->IWICImagingFactory2_iface, frame_decode, encoder);
1233 static HRESULT WINAPI ComponentFactory_CreateQueryWriter(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1234 IWICMetadataQueryWriter **writer)
1236 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1237 return IWICImagingFactory2_CreateQueryWriter(&This->IWICImagingFactory2_iface, format, vendor, writer);
1240 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromReader(IWICComponentFactory *iface, IWICMetadataQueryReader *reader,
1241 const GUID *vendor, IWICMetadataQueryWriter **writer)
1243 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1244 return IWICImagingFactory2_CreateQueryWriterFromReader(&This->IWICImagingFactory2_iface, reader, vendor, writer);
1247 static HRESULT WINAPI ComponentFactory_CreateMetadataReader(IWICComponentFactory *iface,
1248 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
1250 FIXME("%p,%s,%s,%lx,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
1251 options, stream, reader);
1252 return E_NOTIMPL;
1255 static HRESULT WINAPI ComponentFactory_CreateMetadataReaderFromContainer(IWICComponentFactory *iface,
1256 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
1258 HRESULT hr;
1259 IEnumUnknown *enumreaders;
1260 IUnknown *unkreaderinfo;
1261 IWICMetadataReaderInfo *readerinfo;
1262 IWICPersistStream *wicpersiststream;
1263 ULONG num_fetched;
1264 GUID decoder_vendor;
1265 BOOL matches;
1266 LARGE_INTEGER zero;
1268 TRACE("%p,%s,%s,%lx,%p,%p\n", iface, debugstr_guid(format), debugstr_guid(vendor),
1269 options, stream, reader);
1271 if (!format || !stream || !reader)
1272 return E_INVALIDARG;
1274 zero.QuadPart = 0;
1276 hr = CreateComponentEnumerator(WICMetadataReader, WICComponentEnumerateDefault, &enumreaders);
1277 if (FAILED(hr)) return hr;
1279 *reader = NULL;
1281 start:
1282 while (!*reader)
1284 hr = IEnumUnknown_Next(enumreaders, 1, &unkreaderinfo, &num_fetched);
1286 if (hr == S_OK)
1288 hr = IUnknown_QueryInterface(unkreaderinfo, &IID_IWICMetadataReaderInfo, (void**)&readerinfo);
1290 if (SUCCEEDED(hr))
1292 if (vendor)
1294 hr = IWICMetadataReaderInfo_GetVendorGUID(readerinfo, &decoder_vendor);
1296 if (FAILED(hr) || !IsEqualIID(vendor, &decoder_vendor))
1298 IWICMetadataReaderInfo_Release(readerinfo);
1299 IUnknown_Release(unkreaderinfo);
1300 continue;
1304 hr = IWICMetadataReaderInfo_MatchesPattern(readerinfo, format, stream, &matches);
1306 if (SUCCEEDED(hr) && matches)
1308 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
1310 if (SUCCEEDED(hr))
1311 hr = IWICMetadataReaderInfo_CreateInstance(readerinfo, reader);
1313 if (SUCCEEDED(hr))
1315 hr = IWICMetadataReader_QueryInterface(*reader, &IID_IWICPersistStream, (void**)&wicpersiststream);
1317 if (SUCCEEDED(hr))
1319 hr = IWICPersistStream_LoadEx(wicpersiststream,
1320 stream, vendor, options & WICPersistOptionMask);
1322 IWICPersistStream_Release(wicpersiststream);
1325 if (FAILED(hr))
1327 IWICMetadataReader_Release(*reader);
1328 *reader = NULL;
1333 IUnknown_Release(readerinfo);
1336 IUnknown_Release(unkreaderinfo);
1338 else
1339 break;
1342 if (!*reader && vendor)
1344 vendor = NULL;
1345 IEnumUnknown_Reset(enumreaders);
1346 goto start;
1349 IEnumUnknown_Release(enumreaders);
1351 if (!*reader && !(options & WICMetadataCreationFailUnknown))
1353 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
1355 if (SUCCEEDED(hr))
1356 hr = UnknownMetadataReader_CreateInstance(&IID_IWICMetadataReader, (void**)reader);
1358 if (SUCCEEDED(hr))
1360 hr = IWICMetadataReader_QueryInterface(*reader, &IID_IWICPersistStream, (void**)&wicpersiststream);
1362 if (SUCCEEDED(hr))
1364 hr = IWICPersistStream_LoadEx(wicpersiststream, stream, NULL, options & WICPersistOptionMask);
1366 IWICPersistStream_Release(wicpersiststream);
1369 if (FAILED(hr))
1371 IWICMetadataReader_Release(*reader);
1372 *reader = NULL;
1377 if (*reader)
1378 return S_OK;
1379 else
1380 return WINCODEC_ERR_COMPONENTNOTFOUND;
1383 static HRESULT WINAPI ComponentFactory_CreateMetadataWriter(IWICComponentFactory *iface,
1384 REFGUID format, const GUID *vendor, DWORD options, IWICMetadataWriter **writer)
1386 FIXME("%p,%s,%s,%lx,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor), options, writer);
1387 return E_NOTIMPL;
1390 static HRESULT WINAPI ComponentFactory_CreateMetadataWriterFromReader(IWICComponentFactory *iface,
1391 IWICMetadataReader *reader, const GUID *vendor, IWICMetadataWriter **writer)
1393 FIXME("%p,%p,%s,%p: stub\n", iface, reader, debugstr_guid(vendor), writer);
1394 return E_NOTIMPL;
1397 static HRESULT WINAPI ComponentFactory_CreateQueryReaderFromBlockReader(IWICComponentFactory *iface,
1398 IWICMetadataBlockReader *block_reader, IWICMetadataQueryReader **query_reader)
1400 TRACE("%p,%p,%p\n", iface, block_reader, query_reader);
1402 if (!block_reader || !query_reader)
1403 return E_INVALIDARG;
1405 return MetadataQueryReader_CreateInstance(block_reader, NULL, query_reader);
1408 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromBlockWriter(IWICComponentFactory *iface,
1409 IWICMetadataBlockWriter *block_writer, IWICMetadataQueryWriter **query_writer)
1411 TRACE("%p,%p,%p\n", iface, block_writer, query_writer);
1413 if (!block_writer || !query_writer)
1414 return E_INVALIDARG;
1416 return MetadataQueryWriter_CreateInstance(block_writer, NULL, query_writer);
1419 static HRESULT WINAPI ComponentFactory_CreateEncoderPropertyBag(IWICComponentFactory *iface,
1420 PROPBAG2 *options, UINT count, IPropertyBag2 **property)
1422 TRACE("(%p,%p,%u,%p)\n", iface, options, count, property);
1423 return CreatePropertyBag2(options, count, property);
1426 static const IWICComponentFactoryVtbl ComponentFactory_Vtbl = {
1427 ComponentFactory_QueryInterface,
1428 ComponentFactory_AddRef,
1429 ComponentFactory_Release,
1430 ComponentFactory_CreateDecoderFromFilename,
1431 ComponentFactory_CreateDecoderFromStream,
1432 ComponentFactory_CreateDecoderFromFileHandle,
1433 ComponentFactory_CreateComponentInfo,
1434 ComponentFactory_CreateDecoder,
1435 ComponentFactory_CreateEncoder,
1436 ComponentFactory_CreatePalette,
1437 ComponentFactory_CreateFormatConverter,
1438 ComponentFactory_CreateBitmapScaler,
1439 ComponentFactory_CreateBitmapClipper,
1440 ComponentFactory_CreateBitmapFlipRotator,
1441 ComponentFactory_CreateStream,
1442 ComponentFactory_CreateColorContext,
1443 ComponentFactory_CreateColorTransformer,
1444 ComponentFactory_CreateBitmap,
1445 ComponentFactory_CreateBitmapFromSource,
1446 ComponentFactory_CreateBitmapFromSourceRect,
1447 ComponentFactory_CreateBitmapFromMemory,
1448 ComponentFactory_CreateBitmapFromHBITMAP,
1449 ComponentFactory_CreateBitmapFromHICON,
1450 ComponentFactory_CreateComponentEnumerator,
1451 ComponentFactory_CreateFastMetadataEncoderFromDecoder,
1452 ComponentFactory_CreateFastMetadataEncoderFromFrameDecode,
1453 ComponentFactory_CreateQueryWriter,
1454 ComponentFactory_CreateQueryWriterFromReader,
1455 ComponentFactory_CreateMetadataReader,
1456 ComponentFactory_CreateMetadataReaderFromContainer,
1457 ComponentFactory_CreateMetadataWriter,
1458 ComponentFactory_CreateMetadataWriterFromReader,
1459 ComponentFactory_CreateQueryReaderFromBlockReader,
1460 ComponentFactory_CreateQueryWriterFromBlockWriter,
1461 ComponentFactory_CreateEncoderPropertyBag
1464 HRESULT ImagingFactory_CreateInstance(REFIID iid, void** ppv)
1466 ImagingFactory *This;
1467 HRESULT ret;
1469 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1471 *ppv = NULL;
1473 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1474 if (!This) return E_OUTOFMEMORY;
1476 This->IWICImagingFactory2_iface.lpVtbl = &ImagingFactory_Vtbl;
1477 This->IWICComponentFactory_iface.lpVtbl = &ComponentFactory_Vtbl;
1478 This->ref = 1;
1480 ret = IWICImagingFactory2_QueryInterface(&This->IWICImagingFactory2_iface, iid, ppv);
1481 IWICImagingFactory2_Release(&This->IWICImagingFactory2_iface);
1483 return ret;
1486 HRESULT WINAPI WICCreateBitmapFromSectionEx(UINT width, UINT height,
1487 REFWICPixelFormatGUID format, HANDLE section, UINT stride,
1488 UINT offset, WICSectionAccessLevel wicaccess, IWICBitmap **bitmap)
1490 SYSTEM_INFO sysinfo;
1491 UINT bpp, access, size, view_offset, view_size;
1492 void *view;
1493 HRESULT hr;
1495 TRACE("%u,%u,%s,%p,%u,%u,%#x,%p\n", width, height, debugstr_guid(format),
1496 section, stride, offset, wicaccess, bitmap);
1498 if (!width || !height || !section || !bitmap) return E_INVALIDARG;
1500 hr = get_pixelformat_bpp(format, &bpp);
1501 if (FAILED(hr)) return hr;
1503 switch (wicaccess)
1505 case WICSectionAccessLevelReadWrite:
1506 access = FILE_MAP_READ | FILE_MAP_WRITE;
1507 break;
1509 case WICSectionAccessLevelRead:
1510 access = FILE_MAP_READ;
1511 break;
1513 default:
1514 FIXME("unsupported access %#x\n", wicaccess);
1515 return E_INVALIDARG;
1518 if (!stride) stride = (((bpp * width) + 31) / 32) * 4;
1519 size = stride * height;
1520 if (size / height != stride) return E_INVALIDARG;
1522 GetSystemInfo(&sysinfo);
1523 view_offset = offset - (offset % sysinfo.dwAllocationGranularity);
1524 view_size = size + (offset - view_offset);
1526 view = MapViewOfFile(section, access, 0, view_offset, view_size);
1527 if (!view) return HRESULT_FROM_WIN32(GetLastError());
1529 offset -= view_offset;
1530 hr = BitmapImpl_Create(width, height, stride, 0, view, offset, format, WICBitmapCacheOnLoad, bitmap);
1531 if (FAILED(hr)) UnmapViewOfFile(view);
1532 return hr;
1535 HRESULT WINAPI WICCreateBitmapFromSection(UINT width, UINT height,
1536 REFWICPixelFormatGUID format, HANDLE section,
1537 UINT stride, UINT offset, IWICBitmap **bitmap)
1539 TRACE("%u,%u,%s,%p,%u,%u,%p\n", width, height, debugstr_guid(format),
1540 section, stride, offset, bitmap);
1542 return WICCreateBitmapFromSectionEx(width, height, format, section,
1543 stride, offset, WICSectionAccessLevelRead, bitmap);