windowscodecs: find_decoder() should return an error it received from the decoder.
[wine.git] / dlls / windowscodecs / imgfactory.c
blobfb235fbbe168cacdcc2b3a3c2411b7a1928eaa82
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
22 #include <assert.h>
23 #include <stdarg.h>
25 #define COBJMACROS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "objbase.h"
31 #include "shellapi.h"
33 #include "wincodecs_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
39 typedef struct {
40 IWICImagingFactory2 IWICImagingFactory2_iface;
41 IWICComponentFactory IWICComponentFactory_iface;
42 LONG ref;
43 } ImagingFactory;
45 static inline ImagingFactory *impl_from_IWICComponentFactory(IWICComponentFactory *iface)
47 return CONTAINING_RECORD(iface, ImagingFactory, IWICComponentFactory_iface);
50 static inline ImagingFactory *impl_from_IWICImagingFactory2(IWICImagingFactory2 *iface)
52 return CONTAINING_RECORD(iface, ImagingFactory, IWICImagingFactory2_iface);
55 static HRESULT WINAPI ImagingFactory_QueryInterface(IWICImagingFactory2 *iface, REFIID iid,
56 void **ppv)
58 ImagingFactory *This = impl_from_IWICImagingFactory2(iface);
59 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
61 if (!ppv) return E_INVALIDARG;
63 if (IsEqualIID(&IID_IUnknown, iid) ||
64 IsEqualIID(&IID_IWICImagingFactory, iid) ||
65 IsEqualIID(&IID_IWICComponentFactory, iid))
67 *ppv = &This->IWICComponentFactory_iface;
69 else if (IsEqualIID(&IID_IWICImagingFactory2, iid))
71 *ppv = &This->IWICImagingFactory2_iface;
73 else
75 *ppv = NULL;
76 return E_NOINTERFACE;
79 IUnknown_AddRef((IUnknown*)*ppv);
80 return S_OK;
83 static ULONG WINAPI ImagingFactory_AddRef(IWICImagingFactory2 *iface)
85 ImagingFactory *This = impl_from_IWICImagingFactory2(iface);
86 ULONG ref = InterlockedIncrement(&This->ref);
88 TRACE("(%p) refcount=%u\n", iface, ref);
90 return ref;
93 static ULONG WINAPI ImagingFactory_Release(IWICImagingFactory2 *iface)
95 ImagingFactory *This = impl_from_IWICImagingFactory2(iface);
96 ULONG ref = InterlockedDecrement(&This->ref);
98 TRACE("(%p) refcount=%u\n", iface, ref);
100 if (ref == 0)
101 HeapFree(GetProcessHeap(), 0, This);
103 return ref;
106 static HRESULT WINAPI ImagingFactory_CreateDecoderFromFilename(
107 IWICImagingFactory2 *iface, LPCWSTR wzFilename, const GUID *pguidVendor,
108 DWORD dwDesiredAccess, WICDecodeOptions metadataOptions,
109 IWICBitmapDecoder **ppIDecoder)
111 IWICStream *stream;
112 HRESULT hr;
114 TRACE("(%p,%s,%s,%u,%u,%p)\n", iface, debugstr_w(wzFilename),
115 debugstr_guid(pguidVendor), dwDesiredAccess, metadataOptions, ppIDecoder);
117 hr = StreamImpl_Create(&stream);
118 if (SUCCEEDED(hr))
120 hr = IWICStream_InitializeFromFilename(stream, wzFilename, dwDesiredAccess);
122 if (SUCCEEDED(hr))
124 hr = IWICImagingFactory2_CreateDecoderFromStream(iface, (IStream*)stream,
125 pguidVendor, metadataOptions, ppIDecoder);
128 IWICStream_Release(stream);
131 return hr;
134 static HRESULT find_decoder(IStream *pIStream, const GUID *pguidVendor,
135 WICDecodeOptions metadataOptions, IWICBitmapDecoder **decoder)
137 IEnumUnknown *enumdecoders;
138 IUnknown *unkdecoderinfo;
139 IWICBitmapDecoderInfo *decoderinfo;
140 GUID vendor;
141 HRESULT res;
142 ULONG num_fetched;
143 BOOL matches;
145 *decoder = NULL;
147 res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
148 if (FAILED(res)) return res;
150 while (!*decoder)
152 res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
154 if (res == S_OK)
156 res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void**)&decoderinfo);
158 if (SUCCEEDED(res))
160 if (pguidVendor)
162 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
163 if (FAILED(res) || !IsEqualIID(&vendor, pguidVendor))
165 IWICBitmapDecoderInfo_Release(decoderinfo);
166 IUnknown_Release(unkdecoderinfo);
167 continue;
171 res = IWICBitmapDecoderInfo_MatchesPattern(decoderinfo, pIStream, &matches);
173 if (SUCCEEDED(res) && matches)
175 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, decoder);
177 /* FIXME: should use QueryCapability to choose a decoder */
179 if (SUCCEEDED(res))
181 res = IWICBitmapDecoder_Initialize(*decoder, pIStream, metadataOptions);
183 if (FAILED(res))
185 IWICBitmapDecoder_Release(*decoder);
186 IWICBitmapDecoderInfo_Release(decoderinfo);
187 IUnknown_Release(unkdecoderinfo);
188 *decoder = NULL;
189 return res;
194 IWICBitmapDecoderInfo_Release(decoderinfo);
197 IUnknown_Release(unkdecoderinfo);
199 else
200 break;
203 IEnumUnknown_Release(enumdecoders);
205 return WINCODEC_ERR_COMPONENTNOTFOUND;
208 static HRESULT WINAPI ImagingFactory_CreateDecoderFromStream(
209 IWICImagingFactory2 *iface, IStream *pIStream, const GUID *pguidVendor,
210 WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
212 HRESULT res;
213 IWICBitmapDecoder *decoder = NULL;
215 TRACE("(%p,%p,%s,%u,%p)\n", iface, pIStream, debugstr_guid(pguidVendor),
216 metadataOptions, ppIDecoder);
218 if (pguidVendor)
219 res = find_decoder(pIStream, pguidVendor, metadataOptions, &decoder);
220 if (!decoder)
221 res = find_decoder(pIStream, NULL, metadataOptions, &decoder);
223 if (decoder)
225 *ppIDecoder = decoder;
226 return S_OK;
228 else
230 if (WARN_ON(wincodecs))
232 LARGE_INTEGER seek;
233 BYTE data[4];
234 ULONG bytesread;
236 WARN("failed to load from a stream %#x\n", res);
238 seek.QuadPart = 0;
239 if (IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL) == S_OK)
241 if (IStream_Read(pIStream, data, 4, &bytesread) == S_OK)
242 WARN("first %i bytes of stream=%x %x %x %x\n", bytesread, data[0], data[1], data[2], data[3]);
245 *ppIDecoder = NULL;
246 return res;
250 static HRESULT WINAPI ImagingFactory_CreateDecoderFromFileHandle(
251 IWICImagingFactory2 *iface, ULONG_PTR hFile, const GUID *pguidVendor,
252 WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
254 IWICStream *stream;
255 HRESULT hr;
257 TRACE("(%p,%lx,%s,%u,%p)\n", iface, hFile, debugstr_guid(pguidVendor),
258 metadataOptions, ppIDecoder);
260 hr = StreamImpl_Create(&stream);
261 if (SUCCEEDED(hr))
263 hr = stream_initialize_from_filehandle(stream, (HANDLE)hFile);
264 if (SUCCEEDED(hr))
266 hr = IWICImagingFactory2_CreateDecoderFromStream(iface, (IStream*)stream,
267 pguidVendor, metadataOptions, ppIDecoder);
269 IWICStream_Release(stream);
271 return hr;
274 static HRESULT WINAPI ImagingFactory_CreateComponentInfo(IWICImagingFactory2 *iface,
275 REFCLSID clsidComponent, IWICComponentInfo **ppIInfo)
277 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(clsidComponent), ppIInfo);
278 return CreateComponentInfo(clsidComponent, ppIInfo);
281 static HRESULT WINAPI ImagingFactory_CreateDecoder(IWICImagingFactory2 *iface,
282 REFGUID guidContainerFormat, const GUID *pguidVendor,
283 IWICBitmapDecoder **ppIDecoder)
285 IEnumUnknown *enumdecoders;
286 IUnknown *unkdecoderinfo;
287 IWICBitmapDecoderInfo *decoderinfo;
288 IWICBitmapDecoder *decoder = NULL, *preferred_decoder = NULL;
289 GUID vendor;
290 HRESULT res;
291 ULONG num_fetched;
293 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
294 debugstr_guid(pguidVendor), ppIDecoder);
296 if (!guidContainerFormat || !ppIDecoder) return E_INVALIDARG;
298 res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
299 if (FAILED(res)) return res;
301 while (!preferred_decoder)
303 res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
304 if (res != S_OK) break;
306 res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void **)&decoderinfo);
307 if (SUCCEEDED(res))
309 GUID container_guid;
311 res = IWICBitmapDecoderInfo_GetContainerFormat(decoderinfo, &container_guid);
312 if (SUCCEEDED(res) && IsEqualIID(&container_guid, guidContainerFormat))
314 IWICBitmapDecoder *new_decoder;
316 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &new_decoder);
317 if (SUCCEEDED(res))
319 if (pguidVendor)
321 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
322 if (SUCCEEDED(res) && IsEqualIID(&vendor, pguidVendor))
324 preferred_decoder = new_decoder;
325 new_decoder = NULL;
329 if (new_decoder && !decoder)
331 decoder = new_decoder;
332 new_decoder = NULL;
335 if (new_decoder) IWICBitmapDecoder_Release(new_decoder);
339 IWICBitmapDecoderInfo_Release(decoderinfo);
342 IUnknown_Release(unkdecoderinfo);
345 IEnumUnknown_Release(enumdecoders);
347 if (preferred_decoder)
349 *ppIDecoder = preferred_decoder;
350 if (decoder) IWICBitmapDecoder_Release(decoder);
351 return S_OK;
354 if (decoder)
356 *ppIDecoder = decoder;
357 return S_OK;
360 *ppIDecoder = NULL;
361 return WINCODEC_ERR_COMPONENTNOTFOUND;
364 static HRESULT WINAPI ImagingFactory_CreateEncoder(IWICImagingFactory2 *iface,
365 REFGUID guidContainerFormat, const GUID *pguidVendor,
366 IWICBitmapEncoder **ppIEncoder)
368 static int fixme=0;
369 IEnumUnknown *enumencoders;
370 IUnknown *unkencoderinfo;
371 IWICBitmapEncoderInfo *encoderinfo;
372 IWICBitmapEncoder *encoder=NULL;
373 HRESULT res=S_OK;
374 ULONG num_fetched;
375 GUID actual_containerformat;
377 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
378 debugstr_guid(pguidVendor), ppIEncoder);
380 if (pguidVendor && !fixme++)
381 FIXME("ignoring vendor GUID\n");
383 res = CreateComponentEnumerator(WICEncoder, WICComponentEnumerateDefault, &enumencoders);
384 if (FAILED(res)) return res;
386 while (!encoder)
388 res = IEnumUnknown_Next(enumencoders, 1, &unkencoderinfo, &num_fetched);
390 if (res == S_OK)
392 res = IUnknown_QueryInterface(unkencoderinfo, &IID_IWICBitmapEncoderInfo, (void**)&encoderinfo);
394 if (SUCCEEDED(res))
396 res = IWICBitmapEncoderInfo_GetContainerFormat(encoderinfo, &actual_containerformat);
398 if (SUCCEEDED(res) && IsEqualGUID(guidContainerFormat, &actual_containerformat))
400 res = IWICBitmapEncoderInfo_CreateInstance(encoderinfo, &encoder);
401 if (FAILED(res))
402 encoder = NULL;
405 IWICBitmapEncoderInfo_Release(encoderinfo);
408 IUnknown_Release(unkencoderinfo);
410 else
411 break;
414 IEnumUnknown_Release(enumencoders);
416 if (encoder)
418 *ppIEncoder = encoder;
419 return S_OK;
421 else
423 WARN("failed to create encoder\n");
424 *ppIEncoder = NULL;
425 return WINCODEC_ERR_COMPONENTNOTFOUND;
429 static HRESULT WINAPI ImagingFactory_CreatePalette(IWICImagingFactory2 *iface,
430 IWICPalette **ppIPalette)
432 TRACE("(%p,%p)\n", iface, ppIPalette);
433 return PaletteImpl_Create(ppIPalette);
436 static HRESULT WINAPI ImagingFactory_CreateFormatConverter(IWICImagingFactory2 *iface,
437 IWICFormatConverter **ppIFormatConverter)
439 return FormatConverter_CreateInstance(&IID_IWICFormatConverter, (void**)ppIFormatConverter);
442 static HRESULT WINAPI ImagingFactory_CreateBitmapScaler(IWICImagingFactory2 *iface,
443 IWICBitmapScaler **ppIBitmapScaler)
445 TRACE("(%p,%p)\n", iface, ppIBitmapScaler);
447 return BitmapScaler_Create(ppIBitmapScaler);
450 static HRESULT WINAPI ImagingFactory_CreateBitmapClipper(IWICImagingFactory2 *iface,
451 IWICBitmapClipper **ppIBitmapClipper)
453 TRACE("(%p,%p)\n", iface, ppIBitmapClipper);
454 return BitmapClipper_Create(ppIBitmapClipper);
457 static HRESULT WINAPI ImagingFactory_CreateBitmapFlipRotator(IWICImagingFactory2 *iface,
458 IWICBitmapFlipRotator **ppIBitmapFlipRotator)
460 TRACE("(%p,%p)\n", iface, ppIBitmapFlipRotator);
461 return FlipRotator_Create(ppIBitmapFlipRotator);
464 static HRESULT WINAPI ImagingFactory_CreateStream(IWICImagingFactory2 *iface,
465 IWICStream **ppIWICStream)
467 TRACE("(%p,%p)\n", iface, ppIWICStream);
468 return StreamImpl_Create(ppIWICStream);
471 static HRESULT WINAPI ImagingFactory_CreateColorContext(IWICImagingFactory2 *iface,
472 IWICColorContext **ppIColorContext)
474 TRACE("(%p,%p)\n", iface, ppIColorContext);
475 return ColorContext_Create(ppIColorContext);
478 static HRESULT WINAPI ImagingFactory_CreateColorTransformer(IWICImagingFactory2 *iface,
479 IWICColorTransform **ppIColorTransform)
481 TRACE("(%p,%p)\n", iface, ppIColorTransform);
482 return ColorTransform_Create(ppIColorTransform);
485 static HRESULT WINAPI ImagingFactory_CreateBitmap(IWICImagingFactory2 *iface,
486 UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat,
487 WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
489 TRACE("(%p,%u,%u,%s,%u,%p)\n", iface, uiWidth, uiHeight,
490 debugstr_guid(pixelFormat), option, ppIBitmap);
491 return BitmapImpl_Create(uiWidth, uiHeight, 0, 0, NULL, 0, pixelFormat, option, ppIBitmap);
494 static HRESULT create_bitmap_from_source_rect(IWICBitmapSource *piBitmapSource, const WICRect *rect,
495 WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
497 IWICBitmap *result;
498 IWICBitmapLock *lock;
499 IWICPalette *palette;
500 UINT width, height;
501 WICPixelFormatGUID pixelformat = {0};
502 HRESULT hr;
503 WICRect rc;
504 double dpix, dpiy;
505 IWICComponentInfo *info;
506 IWICPixelFormatInfo2 *formatinfo;
507 WICPixelFormatNumericRepresentation format_type;
509 assert(!rect || (rect && option == WICBitmapCacheOnLoad));
511 if (!piBitmapSource || !ppIBitmap)
512 return E_INVALIDARG;
514 if (option == WICBitmapNoCache && SUCCEEDED(IWICBitmapSource_QueryInterface(piBitmapSource,
515 &IID_IWICBitmap, (void **)&result)))
517 *ppIBitmap = result;
518 return S_OK;
521 hr = IWICBitmapSource_GetSize(piBitmapSource, &width, &height);
523 if (SUCCEEDED(hr) && rect)
525 if (rect->X >= width || rect->Y >= height || rect->Width == 0 || rect->Height == 0)
526 return E_INVALIDARG;
528 width = min(width - rect->X, rect->Width);
529 height = min(height - rect->Y, rect->Height);
532 if (SUCCEEDED(hr))
533 hr = IWICBitmapSource_GetPixelFormat(piBitmapSource, &pixelformat);
535 if (SUCCEEDED(hr))
536 hr = CreateComponentInfo(&pixelformat, &info);
538 if (SUCCEEDED(hr))
540 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo2, (void**)&formatinfo);
542 if (SUCCEEDED(hr))
544 hr = IWICPixelFormatInfo2_GetNumericRepresentation(formatinfo, &format_type);
546 IWICPixelFormatInfo2_Release(formatinfo);
549 IWICComponentInfo_Release(info);
552 if (SUCCEEDED(hr))
553 hr = BitmapImpl_Create(width, height, 0, 0, NULL, 0, &pixelformat, option, &result);
555 if (SUCCEEDED(hr))
557 hr = IWICBitmap_Lock(result, NULL, WICBitmapLockWrite, &lock);
558 if (SUCCEEDED(hr))
560 UINT stride, buffersize;
561 BYTE *buffer;
563 if (rect)
565 rc.X = rect->X;
566 rc.Y = rect->Y;
568 else
569 rc.X = rc.Y = 0;
570 rc.Width = width;
571 rc.Height = height;
573 hr = IWICBitmapLock_GetStride(lock, &stride);
575 if (SUCCEEDED(hr))
576 hr = IWICBitmapLock_GetDataPointer(lock, &buffersize, &buffer);
578 if (SUCCEEDED(hr))
579 hr = IWICBitmapSource_CopyPixels(piBitmapSource, &rc, stride,
580 buffersize, buffer);
582 IWICBitmapLock_Release(lock);
585 if (SUCCEEDED(hr) && (format_type == WICPixelFormatNumericRepresentationUnspecified ||
586 format_type == WICPixelFormatNumericRepresentationIndexed))
588 hr = PaletteImpl_Create(&palette);
590 if (SUCCEEDED(hr))
592 hr = IWICBitmapSource_CopyPalette(piBitmapSource, palette);
594 if (SUCCEEDED(hr))
595 hr = IWICBitmap_SetPalette(result, palette);
596 else
597 hr = S_OK;
599 IWICPalette_Release(palette);
603 if (SUCCEEDED(hr))
605 hr = IWICBitmapSource_GetResolution(piBitmapSource, &dpix, &dpiy);
607 if (SUCCEEDED(hr))
608 hr = IWICBitmap_SetResolution(result, dpix, dpiy);
609 else
610 hr = S_OK;
613 if (SUCCEEDED(hr))
614 *ppIBitmap = result;
615 else
616 IWICBitmap_Release(result);
619 return hr;
622 static HRESULT WINAPI ImagingFactory_CreateBitmapFromSource(IWICImagingFactory2 *iface,
623 IWICBitmapSource *piBitmapSource, WICBitmapCreateCacheOption option,
624 IWICBitmap **ppIBitmap)
626 TRACE("(%p,%p,%u,%p)\n", iface, piBitmapSource, option, ppIBitmap);
628 return create_bitmap_from_source_rect(piBitmapSource, NULL, option, ppIBitmap);
631 static HRESULT WINAPI ImagingFactory_CreateBitmapFromSourceRect(IWICImagingFactory2 *iface,
632 IWICBitmapSource *piBitmapSource, UINT x, UINT y, UINT width, UINT height,
633 IWICBitmap **ppIBitmap)
635 WICRect rect;
637 TRACE("(%p,%p,%u,%u,%u,%u,%p)\n", iface, piBitmapSource, x, y, width,
638 height, ppIBitmap);
640 rect.X = x;
641 rect.Y = y;
642 rect.Width = width;
643 rect.Height = height;
645 return create_bitmap_from_source_rect(piBitmapSource, &rect, WICBitmapCacheOnLoad, ppIBitmap);
648 static HRESULT WINAPI ImagingFactory_CreateBitmapFromMemory(IWICImagingFactory2 *iface,
649 UINT width, UINT height, REFWICPixelFormatGUID format, UINT stride,
650 UINT size, BYTE *buffer, IWICBitmap **bitmap)
652 HRESULT hr;
654 TRACE("(%p,%u,%u,%s,%u,%u,%p,%p\n", iface, width, height,
655 debugstr_guid(format), stride, size, buffer, bitmap);
657 if (!stride || !size || !buffer || !bitmap) return E_INVALIDARG;
659 hr = BitmapImpl_Create(width, height, stride, size, NULL, 0, format, WICBitmapCacheOnLoad, bitmap);
660 if (SUCCEEDED(hr))
662 IWICBitmapLock *lock;
664 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
665 if (SUCCEEDED(hr))
667 UINT buffersize;
668 BYTE *data;
670 IWICBitmapLock_GetDataPointer(lock, &buffersize, &data);
671 memcpy(data, buffer, buffersize);
673 IWICBitmapLock_Release(lock);
675 else
677 IWICBitmap_Release(*bitmap);
678 *bitmap = NULL;
681 return hr;
684 static BOOL get_16bpp_format(HBITMAP hbm, WICPixelFormatGUID *format)
686 BOOL ret = TRUE;
687 BITMAPV4HEADER bmh;
688 HDC hdc;
690 hdc = CreateCompatibleDC(0);
692 memset(&bmh, 0, sizeof(bmh));
693 bmh.bV4Size = sizeof(bmh);
694 bmh.bV4Width = 1;
695 bmh.bV4Height = 1;
696 bmh.bV4V4Compression = BI_BITFIELDS;
697 bmh.bV4BitCount = 16;
699 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO *)&bmh, DIB_RGB_COLORS);
701 if (bmh.bV4RedMask == 0x7c00 &&
702 bmh.bV4GreenMask == 0x3e0 &&
703 bmh.bV4BlueMask == 0x1f)
705 *format = GUID_WICPixelFormat16bppBGR555;
707 else if (bmh.bV4RedMask == 0xf800 &&
708 bmh.bV4GreenMask == 0x7e0 &&
709 bmh.bV4BlueMask == 0x1f)
711 *format = GUID_WICPixelFormat16bppBGR565;
713 else
715 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
716 bmh.bV4GreenMask, bmh.bV4BlueMask);
717 ret = FALSE;
720 DeleteDC(hdc);
721 return ret;
724 static HRESULT WINAPI ImagingFactory_CreateBitmapFromHBITMAP(IWICImagingFactory2 *iface,
725 HBITMAP hbm, HPALETTE hpal, WICBitmapAlphaChannelOption option, IWICBitmap **bitmap)
727 BITMAP bm;
728 HRESULT hr;
729 WICPixelFormatGUID format;
730 IWICBitmapLock *lock;
731 UINT size, num_palette_entries = 0;
732 PALETTEENTRY entry[256];
734 TRACE("(%p,%p,%p,%u,%p)\n", iface, hbm, hpal, option, bitmap);
736 if (!bitmap) return E_INVALIDARG;
738 if (GetObjectW(hbm, sizeof(bm), &bm) != sizeof(bm))
739 return WINCODEC_ERR_WIN32ERROR;
741 if (hpal)
743 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
744 if (!num_palette_entries)
745 return WINCODEC_ERR_WIN32ERROR;
748 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
749 switch(bm.bmBitsPixel)
751 case 1:
752 format = GUID_WICPixelFormat1bppIndexed;
753 break;
754 case 4:
755 format = GUID_WICPixelFormat4bppIndexed;
756 break;
757 case 8:
758 format = GUID_WICPixelFormat8bppIndexed;
759 break;
760 case 16:
761 if (!get_16bpp_format(hbm, &format))
762 return E_INVALIDARG;
763 break;
764 case 24:
765 format = GUID_WICPixelFormat24bppBGR;
766 break;
767 case 32:
768 switch (option)
770 case WICBitmapUseAlpha:
771 format = GUID_WICPixelFormat32bppBGRA;
772 break;
773 case WICBitmapUsePremultipliedAlpha:
774 format = GUID_WICPixelFormat32bppPBGRA;
775 break;
776 case WICBitmapIgnoreAlpha:
777 format = GUID_WICPixelFormat32bppBGR;
778 break;
779 default:
780 return E_INVALIDARG;
782 break;
783 case 48:
784 format = GUID_WICPixelFormat48bppRGB;
785 break;
786 default:
787 FIXME("unsupported %d bpp\n", bm.bmBitsPixel);
788 return E_INVALIDARG;
791 hr = BitmapImpl_Create(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, 0, NULL, 0, &format,
792 WICBitmapCacheOnLoad, bitmap);
793 if (hr != S_OK) return hr;
795 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
796 if (hr == S_OK)
798 BYTE *buffer;
799 HDC hdc;
800 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
801 BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
803 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
805 hdc = CreateCompatibleDC(0);
807 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
808 bmi->bmiHeader.biBitCount = 0;
809 GetDIBits(hdc, hbm, 0, 0, NULL, bmi, DIB_RGB_COLORS);
810 bmi->bmiHeader.biHeight = -bm.bmHeight;
811 GetDIBits(hdc, hbm, 0, bm.bmHeight, buffer, bmi, DIB_RGB_COLORS);
813 DeleteDC(hdc);
814 IWICBitmapLock_Release(lock);
816 if (num_palette_entries)
818 IWICPalette *palette;
819 WICColor colors[256];
820 UINT i;
822 hr = PaletteImpl_Create(&palette);
823 if (hr == S_OK)
825 for (i = 0; i < num_palette_entries; i++)
826 colors[i] = 0xff000000 | entry[i].peRed << 16 |
827 entry[i].peGreen << 8 | entry[i].peBlue;
829 hr = IWICPalette_InitializeCustom(palette, colors, num_palette_entries);
830 if (hr == S_OK)
831 hr = IWICBitmap_SetPalette(*bitmap, palette);
833 IWICPalette_Release(palette);
838 if (hr != S_OK)
840 IWICBitmap_Release(*bitmap);
841 *bitmap = NULL;
844 return hr;
847 static HRESULT WINAPI ImagingFactory_CreateBitmapFromHICON(IWICImagingFactory2 *iface,
848 HICON hicon, IWICBitmap **bitmap)
850 IWICBitmapLock *lock;
851 ICONINFO info;
852 BITMAP bm;
853 int width, height, x, y;
854 UINT stride, size;
855 BYTE *buffer;
856 DWORD *bits;
857 BITMAPINFO bi;
858 HDC hdc;
859 BOOL has_alpha;
860 HRESULT hr;
862 TRACE("(%p,%p,%p)\n", iface, hicon, bitmap);
864 if (!bitmap) return E_INVALIDARG;
866 if (!GetIconInfo(hicon, &info))
867 return HRESULT_FROM_WIN32(GetLastError());
869 GetObjectW(info.hbmColor ? info.hbmColor : info.hbmMask, sizeof(bm), &bm);
871 width = bm.bmWidth;
872 height = info.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
873 stride = width * 4;
874 size = stride * height;
876 hr = BitmapImpl_Create(width, height, stride, size, NULL, 0,
877 &GUID_WICPixelFormat32bppBGRA, WICBitmapCacheOnLoad, bitmap);
878 if (hr != S_OK) goto failed;
880 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
881 if (hr != S_OK)
883 IWICBitmap_Release(*bitmap);
884 goto failed;
886 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
888 hdc = CreateCompatibleDC(0);
890 memset(&bi, 0, sizeof(bi));
891 bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
892 bi.bmiHeader.biWidth = width;
893 bi.bmiHeader.biHeight = info.hbmColor ? -height: -height * 2;
894 bi.bmiHeader.biPlanes = 1;
895 bi.bmiHeader.biBitCount = 32;
896 bi.bmiHeader.biCompression = BI_RGB;
898 has_alpha = FALSE;
900 if (info.hbmColor)
902 GetDIBits(hdc, info.hbmColor, 0, height, buffer, &bi, DIB_RGB_COLORS);
904 if (bm.bmBitsPixel == 32)
906 /* If any pixel has a non-zero alpha, ignore hbmMask */
907 bits = (DWORD *)buffer;
908 for (x = 0; x < width && !has_alpha; x++, bits++)
910 for (y = 0; y < height; y++)
912 if (*bits & 0xff000000)
914 has_alpha = TRUE;
915 break;
921 else
922 GetDIBits(hdc, info.hbmMask, 0, height, buffer, &bi, DIB_RGB_COLORS);
924 if (!has_alpha)
926 DWORD *rgba;
928 if (info.hbmMask)
930 BYTE *mask;
932 mask = HeapAlloc(GetProcessHeap(), 0, size);
933 if (!mask)
935 IWICBitmapLock_Release(lock);
936 IWICBitmap_Release(*bitmap);
937 DeleteDC(hdc);
938 hr = E_OUTOFMEMORY;
939 goto failed;
942 /* read alpha data from the mask */
943 GetDIBits(hdc, info.hbmMask, info.hbmColor ? 0 : height, height, mask, &bi, DIB_RGB_COLORS);
945 for (y = 0; y < height; y++)
947 rgba = (DWORD *)(buffer + y * stride);
948 bits = (DWORD *)(mask + y * stride);
950 for (x = 0; x < width; x++, rgba++, bits++)
952 if (*bits)
953 *rgba = 0;
954 else
955 *rgba |= 0xff000000;
959 HeapFree(GetProcessHeap(), 0, mask);
961 else
963 /* set constant alpha of 255 */
964 for (y = 0; y < height; y++)
966 rgba = (DWORD *)(buffer + y * stride);
967 for (x = 0; x < width; x++, rgba++)
968 *rgba |= 0xff000000;
974 IWICBitmapLock_Release(lock);
975 DeleteDC(hdc);
977 failed:
978 DeleteObject(info.hbmColor);
979 DeleteObject(info.hbmMask);
981 return hr;
984 static HRESULT WINAPI ImagingFactory_CreateComponentEnumerator(IWICImagingFactory2 *iface,
985 DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
987 TRACE("(%p,%u,%u,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
988 return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
991 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromDecoder(
992 IWICImagingFactory2 *iface, IWICBitmapDecoder *pIDecoder,
993 IWICFastMetadataEncoder **ppIFastEncoder)
995 FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
996 return E_NOTIMPL;
999 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromFrameDecode(
1000 IWICImagingFactory2 *iface, IWICBitmapFrameDecode *pIFrameDecoder,
1001 IWICFastMetadataEncoder **ppIFastEncoder)
1003 FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
1004 return E_NOTIMPL;
1007 static HRESULT WINAPI ImagingFactory_CreateQueryWriter(IWICImagingFactory2 *iface,
1008 REFGUID guidMetadataFormat, const GUID *pguidVendor,
1009 IWICMetadataQueryWriter **ppIQueryWriter)
1011 FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
1012 debugstr_guid(pguidVendor), ppIQueryWriter);
1013 return E_NOTIMPL;
1016 static HRESULT WINAPI ImagingFactory_CreateQueryWriterFromReader(IWICImagingFactory2 *iface,
1017 IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
1018 IWICMetadataQueryWriter **ppIQueryWriter)
1020 FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
1021 ppIQueryWriter);
1022 return E_NOTIMPL;
1025 static HRESULT WINAPI ImagingFactory_CreateImageEncoder(IWICImagingFactory2 *iface, ID2D1Device *device, IWICImageEncoder **encoder)
1027 FIXME("%p,%p,%p stub.\n", iface, device, encoder);
1028 return E_NOTIMPL;
1031 static const IWICImagingFactory2Vtbl ImagingFactory_Vtbl = {
1032 ImagingFactory_QueryInterface,
1033 ImagingFactory_AddRef,
1034 ImagingFactory_Release,
1035 ImagingFactory_CreateDecoderFromFilename,
1036 ImagingFactory_CreateDecoderFromStream,
1037 ImagingFactory_CreateDecoderFromFileHandle,
1038 ImagingFactory_CreateComponentInfo,
1039 ImagingFactory_CreateDecoder,
1040 ImagingFactory_CreateEncoder,
1041 ImagingFactory_CreatePalette,
1042 ImagingFactory_CreateFormatConverter,
1043 ImagingFactory_CreateBitmapScaler,
1044 ImagingFactory_CreateBitmapClipper,
1045 ImagingFactory_CreateBitmapFlipRotator,
1046 ImagingFactory_CreateStream,
1047 ImagingFactory_CreateColorContext,
1048 ImagingFactory_CreateColorTransformer,
1049 ImagingFactory_CreateBitmap,
1050 ImagingFactory_CreateBitmapFromSource,
1051 ImagingFactory_CreateBitmapFromSourceRect,
1052 ImagingFactory_CreateBitmapFromMemory,
1053 ImagingFactory_CreateBitmapFromHBITMAP,
1054 ImagingFactory_CreateBitmapFromHICON,
1055 ImagingFactory_CreateComponentEnumerator,
1056 ImagingFactory_CreateFastMetadataEncoderFromDecoder,
1057 ImagingFactory_CreateFastMetadataEncoderFromFrameDecode,
1058 ImagingFactory_CreateQueryWriter,
1059 ImagingFactory_CreateQueryWriterFromReader,
1060 ImagingFactory_CreateImageEncoder,
1063 static HRESULT WINAPI ComponentFactory_QueryInterface(IWICComponentFactory *iface, REFIID iid, void **ppv)
1065 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1066 return IWICImagingFactory2_QueryInterface(&This->IWICImagingFactory2_iface, iid, ppv);
1069 static ULONG WINAPI ComponentFactory_AddRef(IWICComponentFactory *iface)
1071 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1072 return IWICImagingFactory2_AddRef(&This->IWICImagingFactory2_iface);
1075 static ULONG WINAPI ComponentFactory_Release(IWICComponentFactory *iface)
1077 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1078 return IWICImagingFactory2_Release(&This->IWICImagingFactory2_iface);
1081 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFilename(IWICComponentFactory *iface, LPCWSTR filename,
1082 const GUID *vendor, DWORD desired_access, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1084 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1085 return IWICImagingFactory2_CreateDecoderFromFilename(&This->IWICImagingFactory2_iface, filename, vendor,
1086 desired_access, options, decoder);
1089 static HRESULT WINAPI ComponentFactory_CreateDecoderFromStream(IWICComponentFactory *iface, IStream *stream,
1090 const GUID *vendor, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1092 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1093 return IWICImagingFactory2_CreateDecoderFromStream(&This->IWICImagingFactory2_iface, stream, vendor,
1094 options, decoder);
1097 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFileHandle(IWICComponentFactory *iface, ULONG_PTR hFile,
1098 const GUID *vendor, WICDecodeOptions options, IWICBitmapDecoder **decoder)
1100 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1101 return IWICImagingFactory2_CreateDecoderFromFileHandle(&This->IWICImagingFactory2_iface, hFile, vendor,
1102 options, decoder);
1105 static HRESULT WINAPI ComponentFactory_CreateComponentInfo(IWICComponentFactory *iface, REFCLSID component,
1106 IWICComponentInfo **info)
1108 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1109 return IWICImagingFactory2_CreateComponentInfo(&This->IWICImagingFactory2_iface, component, info);
1112 static HRESULT WINAPI ComponentFactory_CreateDecoder(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1113 IWICBitmapDecoder **decoder)
1115 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1116 return IWICImagingFactory2_CreateDecoder(&This->IWICImagingFactory2_iface, format, vendor, decoder);
1119 static HRESULT WINAPI ComponentFactory_CreateEncoder(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1120 IWICBitmapEncoder **encoder)
1122 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1123 return IWICImagingFactory2_CreateEncoder(&This->IWICImagingFactory2_iface, format, vendor, encoder);
1126 static HRESULT WINAPI ComponentFactory_CreatePalette(IWICComponentFactory *iface, IWICPalette **palette)
1128 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1129 return IWICImagingFactory2_CreatePalette(&This->IWICImagingFactory2_iface, palette);
1132 static HRESULT WINAPI ComponentFactory_CreateFormatConverter(IWICComponentFactory *iface, IWICFormatConverter **converter)
1134 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1135 return IWICImagingFactory2_CreateFormatConverter(&This->IWICImagingFactory2_iface, converter);
1138 static HRESULT WINAPI ComponentFactory_CreateBitmapScaler(IWICComponentFactory *iface, IWICBitmapScaler **scaler)
1140 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1141 return IWICImagingFactory2_CreateBitmapScaler(&This->IWICImagingFactory2_iface, scaler);
1144 static HRESULT WINAPI ComponentFactory_CreateBitmapClipper(IWICComponentFactory *iface, IWICBitmapClipper **clipper)
1146 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1147 return IWICImagingFactory2_CreateBitmapClipper(&This->IWICImagingFactory2_iface, clipper);
1150 static HRESULT WINAPI ComponentFactory_CreateBitmapFlipRotator(IWICComponentFactory *iface, IWICBitmapFlipRotator **fliprotator)
1152 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1153 return IWICImagingFactory2_CreateBitmapFlipRotator(&This->IWICImagingFactory2_iface, fliprotator);
1156 static HRESULT WINAPI ComponentFactory_CreateStream(IWICComponentFactory *iface, IWICStream **stream)
1158 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1159 return IWICImagingFactory2_CreateStream(&This->IWICImagingFactory2_iface, stream);
1162 static HRESULT WINAPI ComponentFactory_CreateColorContext(IWICComponentFactory *iface, IWICColorContext **context)
1164 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1165 return IWICImagingFactory2_CreateColorContext(&This->IWICImagingFactory2_iface, context);
1168 static HRESULT WINAPI ComponentFactory_CreateColorTransformer(IWICComponentFactory *iface, IWICColorTransform **transformer)
1170 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1171 return IWICImagingFactory2_CreateColorTransformer(&This->IWICImagingFactory2_iface, transformer);
1174 static HRESULT WINAPI ComponentFactory_CreateBitmap(IWICComponentFactory *iface, UINT width, UINT height, REFWICPixelFormatGUID pixel_format,
1175 WICBitmapCreateCacheOption option, IWICBitmap **bitmap)
1177 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1178 return IWICImagingFactory2_CreateBitmap(&This->IWICImagingFactory2_iface, width, height, pixel_format, option, bitmap);
1181 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSource(IWICComponentFactory *iface, IWICBitmapSource *source,
1182 WICBitmapCreateCacheOption option, IWICBitmap **bitmap)
1184 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1185 return IWICImagingFactory2_CreateBitmapFromSource(&This->IWICImagingFactory2_iface, source, option, bitmap);
1188 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSourceRect(IWICComponentFactory *iface, IWICBitmapSource *source,
1189 UINT x, UINT y, UINT width, UINT height, IWICBitmap **bitmap)
1191 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1192 return IWICImagingFactory2_CreateBitmapFromSourceRect(&This->IWICImagingFactory2_iface, source, x, y, width, height, bitmap);
1195 static HRESULT WINAPI ComponentFactory_CreateBitmapFromMemory(IWICComponentFactory *iface, UINT width, UINT height,
1196 REFWICPixelFormatGUID format, UINT stride, UINT size, BYTE *buffer, IWICBitmap **bitmap)
1198 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1199 return IWICImagingFactory2_CreateBitmapFromMemory(&This->IWICImagingFactory2_iface, width, height, format, stride,
1200 size, buffer, bitmap);
1203 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHBITMAP(IWICComponentFactory *iface, HBITMAP hbm, HPALETTE hpal,
1204 WICBitmapAlphaChannelOption option, IWICBitmap **bitmap)
1206 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1207 return IWICImagingFactory2_CreateBitmapFromHBITMAP(&This->IWICImagingFactory2_iface, hbm, hpal, option, bitmap);
1210 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHICON(IWICComponentFactory *iface, HICON hicon, IWICBitmap **bitmap)
1212 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1213 return IWICImagingFactory2_CreateBitmapFromHICON(&This->IWICImagingFactory2_iface, hicon, bitmap);
1216 static HRESULT WINAPI ComponentFactory_CreateComponentEnumerator(IWICComponentFactory *iface, DWORD component_types,
1217 DWORD options, IEnumUnknown **enumerator)
1219 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1220 return IWICImagingFactory2_CreateComponentEnumerator(&This->IWICImagingFactory2_iface, component_types,
1221 options, enumerator);
1224 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromDecoder(IWICComponentFactory *iface, IWICBitmapDecoder *decoder,
1225 IWICFastMetadataEncoder **encoder)
1227 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1228 return IWICImagingFactory2_CreateFastMetadataEncoderFromDecoder(&This->IWICImagingFactory2_iface, decoder, encoder);
1231 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromFrameDecode(IWICComponentFactory *iface,
1232 IWICBitmapFrameDecode *frame_decode, IWICFastMetadataEncoder **encoder)
1234 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1235 return IWICImagingFactory2_CreateFastMetadataEncoderFromFrameDecode(&This->IWICImagingFactory2_iface, frame_decode, encoder);
1238 static HRESULT WINAPI ComponentFactory_CreateQueryWriter(IWICComponentFactory *iface, REFGUID format, const GUID *vendor,
1239 IWICMetadataQueryWriter **writer)
1241 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1242 return IWICImagingFactory2_CreateQueryWriter(&This->IWICImagingFactory2_iface, format, vendor, writer);
1245 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromReader(IWICComponentFactory *iface, IWICMetadataQueryReader *reader,
1246 const GUID *vendor, IWICMetadataQueryWriter **writer)
1248 ImagingFactory *This = impl_from_IWICComponentFactory(iface);
1249 return IWICImagingFactory2_CreateQueryWriterFromReader(&This->IWICImagingFactory2_iface, reader, vendor, writer);
1252 static HRESULT WINAPI ComponentFactory_CreateMetadataReader(IWICComponentFactory *iface,
1253 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
1255 FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
1256 options, stream, reader);
1257 return E_NOTIMPL;
1260 static HRESULT WINAPI ComponentFactory_CreateMetadataReaderFromContainer(IWICComponentFactory *iface,
1261 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
1263 HRESULT hr;
1264 IEnumUnknown *enumreaders;
1265 IUnknown *unkreaderinfo;
1266 IWICMetadataReaderInfo *readerinfo;
1267 IWICPersistStream *wicpersiststream;
1268 ULONG num_fetched;
1269 GUID decoder_vendor;
1270 BOOL matches;
1271 LARGE_INTEGER zero;
1273 TRACE("%p,%s,%s,%x,%p,%p\n", iface, debugstr_guid(format), debugstr_guid(vendor),
1274 options, stream, reader);
1276 if (!format || !stream || !reader)
1277 return E_INVALIDARG;
1279 zero.QuadPart = 0;
1281 hr = CreateComponentEnumerator(WICMetadataReader, WICComponentEnumerateDefault, &enumreaders);
1282 if (FAILED(hr)) return hr;
1284 *reader = NULL;
1286 start:
1287 while (!*reader)
1289 hr = IEnumUnknown_Next(enumreaders, 1, &unkreaderinfo, &num_fetched);
1291 if (hr == S_OK)
1293 hr = IUnknown_QueryInterface(unkreaderinfo, &IID_IWICMetadataReaderInfo, (void**)&readerinfo);
1295 if (SUCCEEDED(hr))
1297 if (vendor)
1299 hr = IWICMetadataReaderInfo_GetVendorGUID(readerinfo, &decoder_vendor);
1301 if (FAILED(hr) || !IsEqualIID(vendor, &decoder_vendor))
1303 IWICMetadataReaderInfo_Release(readerinfo);
1304 IUnknown_Release(unkreaderinfo);
1305 continue;
1309 hr = IWICMetadataReaderInfo_MatchesPattern(readerinfo, format, stream, &matches);
1311 if (SUCCEEDED(hr) && matches)
1313 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
1315 if (SUCCEEDED(hr))
1316 hr = IWICMetadataReaderInfo_CreateInstance(readerinfo, reader);
1318 if (SUCCEEDED(hr))
1320 hr = IWICMetadataReader_QueryInterface(*reader, &IID_IWICPersistStream, (void**)&wicpersiststream);
1322 if (SUCCEEDED(hr))
1324 hr = IWICPersistStream_LoadEx(wicpersiststream,
1325 stream, vendor, options & WICPersistOptionMask);
1327 IWICPersistStream_Release(wicpersiststream);
1330 if (FAILED(hr))
1332 IWICMetadataReader_Release(*reader);
1333 *reader = NULL;
1338 IUnknown_Release(readerinfo);
1341 IUnknown_Release(unkreaderinfo);
1343 else
1344 break;
1347 if (!*reader && vendor)
1349 vendor = NULL;
1350 IEnumUnknown_Reset(enumreaders);
1351 goto start;
1354 IEnumUnknown_Release(enumreaders);
1356 if (!*reader && !(options & WICMetadataCreationFailUnknown))
1358 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
1360 if (SUCCEEDED(hr))
1361 hr = UnknownMetadataReader_CreateInstance(&IID_IWICMetadataReader, (void**)reader);
1363 if (SUCCEEDED(hr))
1365 hr = IWICMetadataReader_QueryInterface(*reader, &IID_IWICPersistStream, (void**)&wicpersiststream);
1367 if (SUCCEEDED(hr))
1369 hr = IWICPersistStream_LoadEx(wicpersiststream, stream, NULL, options & WICPersistOptionMask);
1371 IWICPersistStream_Release(wicpersiststream);
1374 if (FAILED(hr))
1376 IWICMetadataReader_Release(*reader);
1377 *reader = NULL;
1382 if (*reader)
1383 return S_OK;
1384 else
1385 return WINCODEC_ERR_COMPONENTNOTFOUND;
1388 static HRESULT WINAPI ComponentFactory_CreateMetadataWriter(IWICComponentFactory *iface,
1389 REFGUID format, const GUID *vendor, DWORD options, IWICMetadataWriter **writer)
1391 FIXME("%p,%s,%s,%x,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor), options, writer);
1392 return E_NOTIMPL;
1395 static HRESULT WINAPI ComponentFactory_CreateMetadataWriterFromReader(IWICComponentFactory *iface,
1396 IWICMetadataReader *reader, const GUID *vendor, IWICMetadataWriter **writer)
1398 FIXME("%p,%p,%s,%p: stub\n", iface, reader, debugstr_guid(vendor), writer);
1399 return E_NOTIMPL;
1402 static HRESULT WINAPI ComponentFactory_CreateQueryReaderFromBlockReader(IWICComponentFactory *iface,
1403 IWICMetadataBlockReader *block_reader, IWICMetadataQueryReader **query_reader)
1405 TRACE("%p,%p,%p\n", iface, block_reader, query_reader);
1407 if (!block_reader || !query_reader)
1408 return E_INVALIDARG;
1410 return MetadataQueryReader_CreateInstance(block_reader, NULL, query_reader);
1413 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromBlockWriter(IWICComponentFactory *iface,
1414 IWICMetadataBlockWriter *block_writer, IWICMetadataQueryWriter **query_writer)
1416 FIXME("%p,%p,%p: stub\n", iface, block_writer, query_writer);
1417 return E_NOTIMPL;
1420 static HRESULT WINAPI ComponentFactory_CreateEncoderPropertyBag(IWICComponentFactory *iface,
1421 PROPBAG2 *options, UINT count, IPropertyBag2 **property)
1423 TRACE("(%p,%p,%u,%p)\n", iface, options, count, property);
1424 return CreatePropertyBag2(options, count, property);
1427 static const IWICComponentFactoryVtbl ComponentFactory_Vtbl = {
1428 ComponentFactory_QueryInterface,
1429 ComponentFactory_AddRef,
1430 ComponentFactory_Release,
1431 ComponentFactory_CreateDecoderFromFilename,
1432 ComponentFactory_CreateDecoderFromStream,
1433 ComponentFactory_CreateDecoderFromFileHandle,
1434 ComponentFactory_CreateComponentInfo,
1435 ComponentFactory_CreateDecoder,
1436 ComponentFactory_CreateEncoder,
1437 ComponentFactory_CreatePalette,
1438 ComponentFactory_CreateFormatConverter,
1439 ComponentFactory_CreateBitmapScaler,
1440 ComponentFactory_CreateBitmapClipper,
1441 ComponentFactory_CreateBitmapFlipRotator,
1442 ComponentFactory_CreateStream,
1443 ComponentFactory_CreateColorContext,
1444 ComponentFactory_CreateColorTransformer,
1445 ComponentFactory_CreateBitmap,
1446 ComponentFactory_CreateBitmapFromSource,
1447 ComponentFactory_CreateBitmapFromSourceRect,
1448 ComponentFactory_CreateBitmapFromMemory,
1449 ComponentFactory_CreateBitmapFromHBITMAP,
1450 ComponentFactory_CreateBitmapFromHICON,
1451 ComponentFactory_CreateComponentEnumerator,
1452 ComponentFactory_CreateFastMetadataEncoderFromDecoder,
1453 ComponentFactory_CreateFastMetadataEncoderFromFrameDecode,
1454 ComponentFactory_CreateQueryWriter,
1455 ComponentFactory_CreateQueryWriterFromReader,
1456 ComponentFactory_CreateMetadataReader,
1457 ComponentFactory_CreateMetadataReaderFromContainer,
1458 ComponentFactory_CreateMetadataWriter,
1459 ComponentFactory_CreateMetadataWriterFromReader,
1460 ComponentFactory_CreateQueryReaderFromBlockReader,
1461 ComponentFactory_CreateQueryWriterFromBlockWriter,
1462 ComponentFactory_CreateEncoderPropertyBag
1465 HRESULT ImagingFactory_CreateInstance(REFIID iid, void** ppv)
1467 ImagingFactory *This;
1468 HRESULT ret;
1470 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1472 *ppv = NULL;
1474 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1475 if (!This) return E_OUTOFMEMORY;
1477 This->IWICImagingFactory2_iface.lpVtbl = &ImagingFactory_Vtbl;
1478 This->IWICComponentFactory_iface.lpVtbl = &ComponentFactory_Vtbl;
1479 This->ref = 1;
1481 ret = IWICImagingFactory2_QueryInterface(&This->IWICImagingFactory2_iface, iid, ppv);
1482 IWICImagingFactory2_Release(&This->IWICImagingFactory2_iface);
1484 return ret;
1487 HRESULT WINAPI WICCreateBitmapFromSectionEx(UINT width, UINT height,
1488 REFWICPixelFormatGUID format, HANDLE section, UINT stride,
1489 UINT offset, WICSectionAccessLevel wicaccess, IWICBitmap **bitmap)
1491 SYSTEM_INFO sysinfo;
1492 UINT bpp, access, size, view_offset, view_size;
1493 void *view;
1494 HRESULT hr;
1496 TRACE("%u,%u,%s,%p,%u,%u,%#x,%p\n", width, height, debugstr_guid(format),
1497 section, stride, offset, wicaccess, bitmap);
1499 if (!width || !height || !section || !bitmap) return E_INVALIDARG;
1501 hr = get_pixelformat_bpp(format, &bpp);
1502 if (FAILED(hr)) return hr;
1504 switch (wicaccess)
1506 case WICSectionAccessLevelReadWrite:
1507 access = FILE_MAP_READ | FILE_MAP_WRITE;
1508 break;
1510 case WICSectionAccessLevelRead:
1511 access = FILE_MAP_READ;
1512 break;
1514 default:
1515 FIXME("unsupported access %#x\n", wicaccess);
1516 return E_INVALIDARG;
1519 if (!stride) stride = (((bpp * width) + 31) / 32) * 4;
1520 size = stride * height;
1521 if (size / height != stride) return E_INVALIDARG;
1523 GetSystemInfo(&sysinfo);
1524 view_offset = offset - (offset % sysinfo.dwAllocationGranularity);
1525 view_size = size + (offset - view_offset);
1527 view = MapViewOfFile(section, access, 0, view_offset, view_size);
1528 if (!view) return HRESULT_FROM_WIN32(GetLastError());
1530 offset -= view_offset;
1531 hr = BitmapImpl_Create(width, height, stride, 0, view, offset, format, WICBitmapCacheOnLoad, bitmap);
1532 if (FAILED(hr)) UnmapViewOfFile(view);
1533 return hr;
1536 HRESULT WINAPI WICCreateBitmapFromSection(UINT width, UINT height,
1537 REFWICPixelFormatGUID format, HANDLE section,
1538 UINT stride, UINT offset, IWICBitmap **bitmap)
1540 TRACE("%u,%u,%s,%p,%u,%u,%p\n", width, height, debugstr_guid(format),
1541 section, stride, offset, bitmap);
1543 return WICCreateBitmapFromSectionEx(width, height, format, section,
1544 stride, offset, WICSectionAccessLevelRead, bitmap);