mfplat: Implement GetScanline0AndPitch() for d3d11 buffers.
[wine/zf.git] / dlls / windowscodecs / imgfactory.c
blob7e10fc3787513843a8eaef24d0fdcba9170866fe
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=%u\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=%u\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,%u,%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 %#x\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 %i 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,%lx,%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 %x,%x,%x\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 bits = (DWORD *)buffer;
905 for (x = 0; x < width && !has_alpha; x++, bits++)
907 for (y = 0; y < height; y++)
909 if (*bits & 0xff000000)
911 has_alpha = TRUE;
912 break;
918 else
919 GetDIBits(hdc, info.hbmMask, 0, height, buffer, &bi, DIB_RGB_COLORS);
921 if (!has_alpha)
923 DWORD *rgba;
925 if (info.hbmMask)
927 BYTE *mask;
929 mask = HeapAlloc(GetProcessHeap(), 0, size);
930 if (!mask)
932 IWICBitmapLock_Release(lock);
933 IWICBitmap_Release(*bitmap);
934 DeleteDC(hdc);
935 hr = E_OUTOFMEMORY;
936 goto failed;
939 /* read alpha data from the mask */
940 GetDIBits(hdc, info.hbmMask, info.hbmColor ? 0 : height, height, mask, &bi, DIB_RGB_COLORS);
942 for (y = 0; y < height; y++)
944 rgba = (DWORD *)(buffer + y * stride);
945 bits = (DWORD *)(mask + y * stride);
947 for (x = 0; x < width; x++, rgba++, bits++)
949 if (*bits)
950 *rgba = 0;
951 else
952 *rgba |= 0xff000000;
956 HeapFree(GetProcessHeap(), 0, mask);
958 else
960 /* set constant alpha of 255 */
961 for (y = 0; y < height; y++)
963 rgba = (DWORD *)(buffer + y * stride);
964 for (x = 0; x < width; x++, rgba++)
965 *rgba |= 0xff000000;
971 IWICBitmapLock_Release(lock);
972 DeleteDC(hdc);
974 failed:
975 DeleteObject(info.hbmColor);
976 DeleteObject(info.hbmMask);
978 return hr;
981 static HRESULT WINAPI ImagingFactory_CreateComponentEnumerator(IWICImagingFactory2 *iface,
982 DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
984 TRACE("(%p,%u,%u,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
985 return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
988 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromDecoder(
989 IWICImagingFactory2 *iface, IWICBitmapDecoder *pIDecoder,
990 IWICFastMetadataEncoder **ppIFastEncoder)
992 FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
993 return E_NOTIMPL;
996 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromFrameDecode(
997 IWICImagingFactory2 *iface, IWICBitmapFrameDecode *pIFrameDecoder,
998 IWICFastMetadataEncoder **ppIFastEncoder)
1000 FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
1001 return E_NOTIMPL;
1004 static HRESULT WINAPI ImagingFactory_CreateQueryWriter(IWICImagingFactory2 *iface,
1005 REFGUID guidMetadataFormat, const GUID *pguidVendor,
1006 IWICMetadataQueryWriter **ppIQueryWriter)
1008 FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
1009 debugstr_guid(pguidVendor), ppIQueryWriter);
1010 return E_NOTIMPL;
1013 static HRESULT WINAPI ImagingFactory_CreateQueryWriterFromReader(IWICImagingFactory2 *iface,
1014 IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
1015 IWICMetadataQueryWriter **ppIQueryWriter)
1017 FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
1018 ppIQueryWriter);
1019 return E_NOTIMPL;
1022 static HRESULT WINAPI ImagingFactory_CreateImageEncoder(IWICImagingFactory2 *iface, ID2D1Device *device, IWICImageEncoder **encoder)
1024 FIXME("%p,%p,%p stub.\n", iface, device, encoder);
1025 return E_NOTIMPL;
1028 static const IWICImagingFactory2Vtbl ImagingFactory_Vtbl = {
1029 ImagingFactory_QueryInterface,
1030 ImagingFactory_AddRef,
1031 ImagingFactory_Release,
1032 ImagingFactory_CreateDecoderFromFilename,
1033 ImagingFactory_CreateDecoderFromStream,
1034 ImagingFactory_CreateDecoderFromFileHandle,
1035 ImagingFactory_CreateComponentInfo,
1036 ImagingFactory_CreateDecoder,
1037 ImagingFactory_CreateEncoder,
1038 ImagingFactory_CreatePalette,
1039 ImagingFactory_CreateFormatConverter,
1040 ImagingFactory_CreateBitmapScaler,
1041 ImagingFactory_CreateBitmapClipper,
1042 ImagingFactory_CreateBitmapFlipRotator,
1043 ImagingFactory_CreateStream,
1044 ImagingFactory_CreateColorContext,
1045 ImagingFactory_CreateColorTransformer,
1046 ImagingFactory_CreateBitmap,
1047 ImagingFactory_CreateBitmapFromSource,
1048 ImagingFactory_CreateBitmapFromSourceRect,
1049 ImagingFactory_CreateBitmapFromMemory,
1050 ImagingFactory_CreateBitmapFromHBITMAP,
1051 ImagingFactory_CreateBitmapFromHICON,
1052 ImagingFactory_CreateComponentEnumerator,
1053 ImagingFactory_CreateFastMetadataEncoderFromDecoder,
1054 ImagingFactory_CreateFastMetadataEncoderFromFrameDecode,
1055 ImagingFactory_CreateQueryWriter,
1056 ImagingFactory_CreateQueryWriterFromReader,
1057 ImagingFactory_CreateImageEncoder,
1060 static HRESULT WINAPI ComponentFactory_QueryInterface(IWICComponentFactory *iface, REFIID iid, void **ppv)
1062 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1063 return IWICImagingFactory2_QueryInterface(&This->IWICImagingFactory2_iface, iid, ppv);
1066 static ULONG WINAPI ComponentFactory_AddRef(IWICComponentFactory *iface)
1068 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1069 return IWICImagingFactory2_AddRef(&This->IWICImagingFactory2_iface);
1072 static ULONG WINAPI ComponentFactory_Release(IWICComponentFactory *iface)
1074 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1075 return IWICImagingFactory2_Release(&This->IWICImagingFactory2_iface);
1078 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFilename(IWICComponentFactory *iface, LPCWSTR filename,
1079 const GUID *vendor, DWORD desired_access, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1081 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1082 return IWICImagingFactory2_CreateDecoderFromFilename(&This->IWICImagingFactory2_iface, filename, vendor,
1083 desired_access, options, decoder);
1086 static HRESULT WINAPI ComponentFactory_CreateDecoderFromStream(IWICComponentFactory *iface, IStream *stream,
1087 const GUID *vendor, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1089 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1090 return IWICImagingFactory2_CreateDecoderFromStream(&This->IWICImagingFactory2_iface, stream, vendor,
1091 options, decoder);
1094 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFileHandle(IWICComponentFactory *iface, ULONG_PTR hFile,
1095 const GUID *vendor, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1097 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1098 return IWICImagingFactory2_CreateDecoderFromFileHandle(&This->IWICImagingFactory2_iface, hFile, vendor,
1099 options, decoder);
1102 static HRESULT WINAPI ComponentFactory_CreateComponentInfo(IWICComponentFactory *iface, REFCLSID component,
1103 IWICComponentInfo **info)
1105 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1106 return IWICImagingFactory2_CreateComponentInfo(&This->IWICImagingFactory2_iface, component, info);
1109 static HRESULT WINAPI ComponentFactory_CreateDecoder(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1110 IWICBitmapDecoder **decoder)
1112 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1113 return IWICImagingFactory2_CreateDecoder(&This->IWICImagingFactory2_iface, format, vendor, decoder);
1116 static HRESULT WINAPI ComponentFactory_CreateEncoder(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1117 IWICBitmapEncoder **encoder)
1119 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1120 return IWICImagingFactory2_CreateEncoder(&This->IWICImagingFactory2_iface, format, vendor, encoder);
1123 static HRESULT WINAPI ComponentFactory_CreatePalette(IWICComponentFactory *iface, IWICPalette **palette)
1125 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1126 return IWICImagingFactory2_CreatePalette(&This->IWICImagingFactory2_iface, palette);
1129 static HRESULT WINAPI ComponentFactory_CreateFormatConverter(IWICComponentFactory *iface, IWICFormatConverter **converter)
1131 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1132 return IWICImagingFactory2_CreateFormatConverter(&This->IWICImagingFactory2_iface, converter);
1135 static HRESULT WINAPI ComponentFactory_CreateBitmapScaler(IWICComponentFactory *iface, IWICBitmapScaler **scaler)
1137 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1138 return IWICImagingFactory2_CreateBitmapScaler(&This->IWICImagingFactory2_iface, scaler);
1141 static HRESULT WINAPI ComponentFactory_CreateBitmapClipper(IWICComponentFactory *iface, IWICBitmapClipper **clipper)
1143 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1144 return IWICImagingFactory2_CreateBitmapClipper(&This->IWICImagingFactory2_iface, clipper);
1147 static HRESULT WINAPI ComponentFactory_CreateBitmapFlipRotator(IWICComponentFactory *iface, IWICBitmapFlipRotator **fliprotator)
1149 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1150 return IWICImagingFactory2_CreateBitmapFlipRotator(&This->IWICImagingFactory2_iface, fliprotator);
1153 static HRESULT WINAPI ComponentFactory_CreateStream(IWICComponentFactory *iface, IWICStream **stream)
1155 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1156 return IWICImagingFactory2_CreateStream(&This->IWICImagingFactory2_iface, stream);
1159 static HRESULT WINAPI ComponentFactory_CreateColorContext(IWICComponentFactory *iface, IWICColorContext **context)
1161 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1162 return IWICImagingFactory2_CreateColorContext(&This->IWICImagingFactory2_iface, context);
1165 static HRESULT WINAPI ComponentFactory_CreateColorTransformer(IWICComponentFactory *iface, IWICColorTransform **transformer)
1167 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1168 return IWICImagingFactory2_CreateColorTransformer(&This->IWICImagingFactory2_iface, transformer);
1171 static HRESULT WINAPI ComponentFactory_CreateBitmap(IWICComponentFactory *iface, UINT width, UINT height, REFWICPixelFormatGUID pixel_format,
1172 WICBitmapCreateCacheOption option, IWICBitmap **bitmap)
1174 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1175 return IWICImagingFactory2_CreateBitmap(&This->IWICImagingFactory2_iface, width, height, pixel_format, option, bitmap);
1178 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSource(IWICComponentFactory *iface, IWICBitmapSource *source,
1179 WICBitmapCreateCacheOption option, IWICBitmap **bitmap)
1181 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1182 return IWICImagingFactory2_CreateBitmapFromSource(&This->IWICImagingFactory2_iface, source, option, bitmap);
1185 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSourceRect(IWICComponentFactory *iface, IWICBitmapSource *source,
1186 UINT x, UINT y, UINT width, UINT height, IWICBitmap **bitmap)
1188 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1189 return IWICImagingFactory2_CreateBitmapFromSourceRect(&This->IWICImagingFactory2_iface, source, x, y, width, height, bitmap);
1192 static HRESULT WINAPI ComponentFactory_CreateBitmapFromMemory(IWICComponentFactory *iface, UINT width, UINT height,
1193 REFWICPixelFormatGUID format, UINT stride, UINT size, BYTE *buffer, IWICBitmap **bitmap)
1195 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1196 return IWICImagingFactory2_CreateBitmapFromMemory(&This->IWICImagingFactory2_iface, width, height, format, stride,
1197 size, buffer, bitmap);
1200 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHBITMAP(IWICComponentFactory *iface, HBITMAP hbm, HPALETTE hpal,
1201 WICBitmapAlphaChannelOption option, IWICBitmap **bitmap)
1203 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1204 return IWICImagingFactory2_CreateBitmapFromHBITMAP(&This->IWICImagingFactory2_iface, hbm, hpal, option, bitmap);
1207 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHICON(IWICComponentFactory *iface, HICON hicon, IWICBitmap **bitmap)
1209 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1210 return IWICImagingFactory2_CreateBitmapFromHICON(&This->IWICImagingFactory2_iface, hicon, bitmap);
1213 static HRESULT WINAPI ComponentFactory_CreateComponentEnumerator(IWICComponentFactory *iface, DWORD component_types,
1214 DWORD options, IEnumUnknown **enumerator)
1216 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1217 return IWICImagingFactory2_CreateComponentEnumerator(&This->IWICImagingFactory2_iface, component_types,
1218 options, enumerator);
1221 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromDecoder(IWICComponentFactory *iface, IWICBitmapDecoder *decoder,
1222 IWICFastMetadataEncoder **encoder)
1224 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1225 return IWICImagingFactory2_CreateFastMetadataEncoderFromDecoder(&This->IWICImagingFactory2_iface, decoder, encoder);
1228 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromFrameDecode(IWICComponentFactory *iface,
1229 IWICBitmapFrameDecode *frame_decode, IWICFastMetadataEncoder **encoder)
1231 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1232 return IWICImagingFactory2_CreateFastMetadataEncoderFromFrameDecode(&This->IWICImagingFactory2_iface, frame_decode, encoder);
1235 static HRESULT WINAPI ComponentFactory_CreateQueryWriter(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1236 IWICMetadataQueryWriter **writer)
1238 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1239 return IWICImagingFactory2_CreateQueryWriter(&This->IWICImagingFactory2_iface, format, vendor, writer);
1242 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromReader(IWICComponentFactory *iface, IWICMetadataQueryReader *reader,
1243 const GUID *vendor, IWICMetadataQueryWriter **writer)
1245 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1246 return IWICImagingFactory2_CreateQueryWriterFromReader(&This->IWICImagingFactory2_iface, reader, vendor, writer);
1249 static HRESULT WINAPI ComponentFactory_CreateMetadataReader(IWICComponentFactory *iface,
1250 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
1252 FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
1253 options, stream, reader);
1254 return E_NOTIMPL;
1257 static HRESULT WINAPI ComponentFactory_CreateMetadataReaderFromContainer(IWICComponentFactory *iface,
1258 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
1260 HRESULT hr;
1261 IEnumUnknown *enumreaders;
1262 IUnknown *unkreaderinfo;
1263 IWICMetadataReaderInfo *readerinfo;
1264 IWICPersistStream *wicpersiststream;
1265 ULONG num_fetched;
1266 GUID decoder_vendor;
1267 BOOL matches;
1268 LARGE_INTEGER zero;
1270 TRACE("%p,%s,%s,%x,%p,%p\n", iface, debugstr_guid(format), debugstr_guid(vendor),
1271 options, stream, reader);
1273 if (!format || !stream || !reader)
1274 return E_INVALIDARG;
1276 zero.QuadPart = 0;
1278 hr = CreateComponentEnumerator(WICMetadataReader, WICComponentEnumerateDefault, &enumreaders);
1279 if (FAILED(hr)) return hr;
1281 *reader = NULL;
1283 start:
1284 while (!*reader)
1286 hr = IEnumUnknown_Next(enumreaders, 1, &unkreaderinfo, &num_fetched);
1288 if (hr == S_OK)
1290 hr = IUnknown_QueryInterface(unkreaderinfo, &IID_IWICMetadataReaderInfo, (void**)&readerinfo);
1292 if (SUCCEEDED(hr))
1294 if (vendor)
1296 hr = IWICMetadataReaderInfo_GetVendorGUID(readerinfo, &decoder_vendor);
1298 if (FAILED(hr) || !IsEqualIID(vendor, &decoder_vendor))
1300 IWICMetadataReaderInfo_Release(readerinfo);
1301 IUnknown_Release(unkreaderinfo);
1302 continue;
1306 hr = IWICMetadataReaderInfo_MatchesPattern(readerinfo, format, stream, &matches);
1308 if (SUCCEEDED(hr) && matches)
1310 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
1312 if (SUCCEEDED(hr))
1313 hr = IWICMetadataReaderInfo_CreateInstance(readerinfo, reader);
1315 if (SUCCEEDED(hr))
1317 hr = IWICMetadataReader_QueryInterface(*reader, &IID_IWICPersistStream, (void**)&wicpersiststream);
1319 if (SUCCEEDED(hr))
1321 hr = IWICPersistStream_LoadEx(wicpersiststream,
1322 stream, vendor, options & WICPersistOptionMask);
1324 IWICPersistStream_Release(wicpersiststream);
1327 if (FAILED(hr))
1329 IWICMetadataReader_Release(*reader);
1330 *reader = NULL;
1335 IUnknown_Release(readerinfo);
1338 IUnknown_Release(unkreaderinfo);
1340 else
1341 break;
1344 if (!*reader && vendor)
1346 vendor = NULL;
1347 IEnumUnknown_Reset(enumreaders);
1348 goto start;
1351 IEnumUnknown_Release(enumreaders);
1353 if (!*reader && !(options & WICMetadataCreationFailUnknown))
1355 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
1357 if (SUCCEEDED(hr))
1358 hr = UnknownMetadataReader_CreateInstance(&IID_IWICMetadataReader, (void**)reader);
1360 if (SUCCEEDED(hr))
1362 hr = IWICMetadataReader_QueryInterface(*reader, &IID_IWICPersistStream, (void**)&wicpersiststream);
1364 if (SUCCEEDED(hr))
1366 hr = IWICPersistStream_LoadEx(wicpersiststream, stream, NULL, options & WICPersistOptionMask);
1368 IWICPersistStream_Release(wicpersiststream);
1371 if (FAILED(hr))
1373 IWICMetadataReader_Release(*reader);
1374 *reader = NULL;
1379 if (*reader)
1380 return S_OK;
1381 else
1382 return WINCODEC_ERR_COMPONENTNOTFOUND;
1385 static HRESULT WINAPI ComponentFactory_CreateMetadataWriter(IWICComponentFactory *iface,
1386 REFGUID format, const GUID *vendor, DWORD options, IWICMetadataWriter **writer)
1388 FIXME("%p,%s,%s,%x,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor), options, writer);
1389 return E_NOTIMPL;
1392 static HRESULT WINAPI ComponentFactory_CreateMetadataWriterFromReader(IWICComponentFactory *iface,
1393 IWICMetadataReader *reader, const GUID *vendor, IWICMetadataWriter **writer)
1395 FIXME("%p,%p,%s,%p: stub\n", iface, reader, debugstr_guid(vendor), writer);
1396 return E_NOTIMPL;
1399 static HRESULT WINAPI ComponentFactory_CreateQueryReaderFromBlockReader(IWICComponentFactory *iface,
1400 IWICMetadataBlockReader *block_reader, IWICMetadataQueryReader **query_reader)
1402 TRACE("%p,%p,%p\n", iface, block_reader, query_reader);
1404 if (!block_reader || !query_reader)
1405 return E_INVALIDARG;
1407 return MetadataQueryReader_CreateInstance(block_reader, NULL, query_reader);
1410 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromBlockWriter(IWICComponentFactory *iface,
1411 IWICMetadataBlockWriter *block_writer, IWICMetadataQueryWriter **query_writer)
1413 TRACE("%p,%p,%p\n", iface, block_writer, query_writer);
1415 if (!block_writer || !query_writer)
1416 return E_INVALIDARG;
1418 return MetadataQueryWriter_CreateInstance(block_writer, NULL, query_writer);
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);