Various background processes must be launched detached from current console (if any).
[wine] / dlls / windowscodecs / imgfactory.c
1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
3  * Copyright 2012 Dmitry Timoshkov
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "config.h"
21
22 #include <stdarg.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "objbase.h"
30 #include "shellapi.h"
31 #include "wincodec.h"
32 #include "wincodecsdk.h"
33
34 #include "wincodecs_private.h"
35
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
39
40 typedef struct {
41     IWICComponentFactory IWICComponentFactory_iface;
42     LONG ref;
43 } ComponentFactory;
44
45 static inline ComponentFactory *impl_from_IWICComponentFactory(IWICComponentFactory *iface)
46 {
47     return CONTAINING_RECORD(iface, ComponentFactory, IWICComponentFactory_iface);
48 }
49
50 static HRESULT WINAPI ComponentFactory_QueryInterface(IWICComponentFactory *iface, REFIID iid,
51     void **ppv)
52 {
53     ComponentFactory *This = impl_from_IWICComponentFactory(iface);
54     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
55
56     if (!ppv) return E_INVALIDARG;
57
58     if (IsEqualIID(&IID_IUnknown, iid) ||
59         IsEqualIID(&IID_IWICImagingFactory, iid) ||
60         IsEqualIID(&IID_IWICComponentFactory, iid))
61     {
62         *ppv = &This->IWICComponentFactory_iface;
63     }
64     else
65     {
66         *ppv = NULL;
67         return E_NOINTERFACE;
68     }
69
70     IUnknown_AddRef((IUnknown*)*ppv);
71     return S_OK;
72 }
73
74 static ULONG WINAPI ComponentFactory_AddRef(IWICComponentFactory *iface)
75 {
76     ComponentFactory *This = impl_from_IWICComponentFactory(iface);
77     ULONG ref = InterlockedIncrement(&This->ref);
78
79     TRACE("(%p) refcount=%u\n", iface, ref);
80
81     return ref;
82 }
83
84 static ULONG WINAPI ComponentFactory_Release(IWICComponentFactory *iface)
85 {
86     ComponentFactory *This = impl_from_IWICComponentFactory(iface);
87     ULONG ref = InterlockedDecrement(&This->ref);
88
89     TRACE("(%p) refcount=%u\n", iface, ref);
90
91     if (ref == 0)
92         HeapFree(GetProcessHeap(), 0, This);
93
94     return ref;
95 }
96
97 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFilename(
98     IWICComponentFactory *iface, LPCWSTR wzFilename, const GUID *pguidVendor,
99     DWORD dwDesiredAccess, WICDecodeOptions metadataOptions,
100     IWICBitmapDecoder **ppIDecoder)
101 {
102     IWICStream *stream;
103     HRESULT hr;
104
105     TRACE("(%p,%s,%s,%u,%u,%p)\n", iface, debugstr_w(wzFilename),
106         debugstr_guid(pguidVendor), dwDesiredAccess, metadataOptions, ppIDecoder);
107
108     hr = StreamImpl_Create(&stream);
109     if (SUCCEEDED(hr))
110     {
111         hr = IWICStream_InitializeFromFilename(stream, wzFilename, dwDesiredAccess);
112
113         if (SUCCEEDED(hr))
114         {
115             hr = IWICComponentFactory_CreateDecoderFromStream(iface, (IStream*)stream,
116                 pguidVendor, metadataOptions, ppIDecoder);
117         }
118
119         IWICStream_Release(stream);
120     }
121
122     return hr;
123 }
124
125 static IWICBitmapDecoder *find_decoder(IStream *pIStream, const GUID *pguidVendor,
126                                        WICDecodeOptions metadataOptions)
127 {
128     IEnumUnknown *enumdecoders;
129     IUnknown *unkdecoderinfo;
130     IWICBitmapDecoderInfo *decoderinfo;
131     IWICBitmapDecoder *decoder = NULL;
132     GUID vendor;
133     HRESULT res;
134     ULONG num_fetched;
135     BOOL matches;
136
137     res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
138     if (FAILED(res)) return NULL;
139
140     while (!decoder)
141     {
142         res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
143
144         if (res == S_OK)
145         {
146             res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void**)&decoderinfo);
147
148             if (SUCCEEDED(res))
149             {
150                 if (pguidVendor)
151                 {
152                     res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
153                     if (FAILED(res) || !IsEqualIID(&vendor, pguidVendor))
154                     {
155                         IWICBitmapDecoderInfo_Release(decoderinfo);
156                         IUnknown_Release(unkdecoderinfo);
157                         continue;
158                     }
159                 }
160
161                 res = IWICBitmapDecoderInfo_MatchesPattern(decoderinfo, pIStream, &matches);
162
163                 if (SUCCEEDED(res) && matches)
164                 {
165                     res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &decoder);
166
167                     /* FIXME: should use QueryCapability to choose a decoder */
168
169                     if (SUCCEEDED(res))
170                     {
171                         res = IWICBitmapDecoder_Initialize(decoder, pIStream, metadataOptions);
172
173                         if (FAILED(res))
174                         {
175                             IWICBitmapDecoder_Release(decoder);
176                             decoder = NULL;
177                         }
178                     }
179                 }
180
181                 IWICBitmapDecoderInfo_Release(decoderinfo);
182             }
183
184             IUnknown_Release(unkdecoderinfo);
185         }
186         else
187             break;
188     }
189
190     IEnumUnknown_Release(enumdecoders);
191
192     return decoder;
193 }
194
195 static HRESULT WINAPI ComponentFactory_CreateDecoderFromStream(
196     IWICComponentFactory *iface, IStream *pIStream, const GUID *pguidVendor,
197     WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
198 {
199     HRESULT res;
200     IWICBitmapDecoder *decoder = NULL;
201
202     TRACE("(%p,%p,%s,%u,%p)\n", iface, pIStream, debugstr_guid(pguidVendor),
203         metadataOptions, ppIDecoder);
204
205     if (pguidVendor)
206         decoder = find_decoder(pIStream, pguidVendor, metadataOptions);
207     if (!decoder)
208         decoder = find_decoder(pIStream, NULL, metadataOptions);
209
210     if (decoder)
211     {
212         *ppIDecoder = decoder;
213         return S_OK;
214     }
215     else
216     {
217         if (WARN_ON(wincodecs))
218         {
219             LARGE_INTEGER seek;
220             BYTE data[4];
221             ULONG bytesread;
222
223             WARN("failed to load from a stream\n");
224
225             seek.QuadPart = 0;
226             res = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
227             if (SUCCEEDED(res))
228                 res = IStream_Read(pIStream, data, 4, &bytesread);
229             if (SUCCEEDED(res))
230                 WARN("first %i bytes of stream=%x %x %x %x\n", bytesread, data[0], data[1], data[2], data[3]);
231         }
232         *ppIDecoder = NULL;
233         return WINCODEC_ERR_COMPONENTNOTFOUND;
234     }
235 }
236
237 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFileHandle(
238     IWICComponentFactory *iface, ULONG_PTR hFile, const GUID *pguidVendor,
239     WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
240 {
241     FIXME("(%p,%lx,%s,%u,%p): stub\n", iface, hFile, debugstr_guid(pguidVendor),
242         metadataOptions, ppIDecoder);
243     return E_NOTIMPL;
244 }
245
246 static HRESULT WINAPI ComponentFactory_CreateComponentInfo(IWICComponentFactory *iface,
247     REFCLSID clsidComponent, IWICComponentInfo **ppIInfo)
248 {
249     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(clsidComponent), ppIInfo);
250     return CreateComponentInfo(clsidComponent, ppIInfo);
251 }
252
253 static HRESULT WINAPI ComponentFactory_CreateDecoder(IWICComponentFactory *iface,
254     REFGUID guidContainerFormat, const GUID *pguidVendor,
255     IWICBitmapDecoder **ppIDecoder)
256 {
257     IEnumUnknown *enumdecoders;
258     IUnknown *unkdecoderinfo;
259     IWICBitmapDecoderInfo *decoderinfo;
260     IWICBitmapDecoder *decoder = NULL, *preferred_decoder = NULL;
261     GUID vendor;
262     HRESULT res;
263     ULONG num_fetched;
264
265     TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
266         debugstr_guid(pguidVendor), ppIDecoder);
267
268     if (!guidContainerFormat || !ppIDecoder) return E_INVALIDARG;
269
270     res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
271     if (FAILED(res)) return res;
272
273     while (!preferred_decoder)
274     {
275         res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
276         if (res != S_OK) break;
277
278         res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void **)&decoderinfo);
279         if (SUCCEEDED(res))
280         {
281             GUID container_guid;
282
283             res = IWICBitmapDecoderInfo_GetContainerFormat(decoderinfo, &container_guid);
284             if (SUCCEEDED(res) && IsEqualIID(&container_guid, guidContainerFormat))
285             {
286                 IWICBitmapDecoder *new_decoder;
287
288                 res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &new_decoder);
289                 if (SUCCEEDED(res))
290                 {
291                     if (pguidVendor)
292                     {
293                         res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
294                         if (SUCCEEDED(res) && IsEqualIID(&vendor, pguidVendor))
295                         {
296                             preferred_decoder = new_decoder;
297                             new_decoder = NULL;
298                         }
299                     }
300
301                     if (new_decoder && !decoder)
302                     {
303                         decoder = new_decoder;
304                         new_decoder = NULL;
305                     }
306
307                     if (new_decoder) IWICBitmapDecoder_Release(new_decoder);
308                 }
309             }
310
311             IWICBitmapDecoderInfo_Release(decoderinfo);
312         }
313
314         IUnknown_Release(unkdecoderinfo);
315     }
316
317     IEnumUnknown_Release(enumdecoders);
318
319     if (preferred_decoder)
320     {
321         *ppIDecoder = preferred_decoder;
322         if (decoder) IWICBitmapDecoder_Release(decoder);
323         return S_OK;
324     }
325
326     if (decoder)
327     {
328         *ppIDecoder = decoder;
329         return S_OK;
330     }
331
332     *ppIDecoder = NULL;
333     return WINCODEC_ERR_COMPONENTNOTFOUND;
334 }
335
336 static HRESULT WINAPI ComponentFactory_CreateEncoder(IWICComponentFactory *iface,
337     REFGUID guidContainerFormat, const GUID *pguidVendor,
338     IWICBitmapEncoder **ppIEncoder)
339 {
340     static int fixme=0;
341     IEnumUnknown *enumencoders;
342     IUnknown *unkencoderinfo;
343     IWICBitmapEncoderInfo *encoderinfo;
344     IWICBitmapEncoder *encoder=NULL;
345     HRESULT res=S_OK;
346     ULONG num_fetched;
347     GUID actual_containerformat;
348
349     TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(guidContainerFormat),
350         debugstr_guid(pguidVendor), ppIEncoder);
351
352     if (pguidVendor && !fixme++)
353         FIXME("ignoring vendor GUID\n");
354
355     res = CreateComponentEnumerator(WICEncoder, WICComponentEnumerateDefault, &enumencoders);
356     if (FAILED(res)) return res;
357
358     while (!encoder)
359     {
360         res = IEnumUnknown_Next(enumencoders, 1, &unkencoderinfo, &num_fetched);
361
362         if (res == S_OK)
363         {
364             res = IUnknown_QueryInterface(unkencoderinfo, &IID_IWICBitmapEncoderInfo, (void**)&encoderinfo);
365
366             if (SUCCEEDED(res))
367             {
368                 res = IWICBitmapEncoderInfo_GetContainerFormat(encoderinfo, &actual_containerformat);
369
370                 if (SUCCEEDED(res) && IsEqualGUID(guidContainerFormat, &actual_containerformat))
371                 {
372                     res = IWICBitmapEncoderInfo_CreateInstance(encoderinfo, &encoder);
373                     if (FAILED(res))
374                         encoder = NULL;
375                 }
376
377                 IWICBitmapEncoderInfo_Release(encoderinfo);
378             }
379
380             IUnknown_Release(unkencoderinfo);
381         }
382         else
383             break;
384     }
385
386     IEnumUnknown_Release(enumencoders);
387
388     if (encoder)
389     {
390         *ppIEncoder = encoder;
391         return S_OK;
392     }
393     else
394     {
395         WARN("failed to create encoder\n");
396         *ppIEncoder = NULL;
397         return WINCODEC_ERR_COMPONENTNOTFOUND;
398     }
399 }
400
401 static HRESULT WINAPI ComponentFactory_CreatePalette(IWICComponentFactory *iface,
402     IWICPalette **ppIPalette)
403 {
404     TRACE("(%p,%p)\n", iface, ppIPalette);
405     return PaletteImpl_Create(ppIPalette);
406 }
407
408 static HRESULT WINAPI ComponentFactory_CreateFormatConverter(IWICComponentFactory *iface,
409     IWICFormatConverter **ppIFormatConverter)
410 {
411     return FormatConverter_CreateInstance(NULL, &IID_IWICFormatConverter, (void**)ppIFormatConverter);
412 }
413
414 static HRESULT WINAPI ComponentFactory_CreateBitmapScaler(IWICComponentFactory *iface,
415     IWICBitmapScaler **ppIBitmapScaler)
416 {
417     TRACE("(%p,%p)\n", iface, ppIBitmapScaler);
418
419     return BitmapScaler_Create(ppIBitmapScaler);
420 }
421
422 static HRESULT WINAPI ComponentFactory_CreateBitmapClipper(IWICComponentFactory *iface,
423     IWICBitmapClipper **ppIBitmapClipper)
424 {
425     FIXME("(%p,%p): stub\n", iface, ppIBitmapClipper);
426     return E_NOTIMPL;
427 }
428
429 static HRESULT WINAPI ComponentFactory_CreateBitmapFlipRotator(IWICComponentFactory *iface,
430     IWICBitmapFlipRotator **ppIBitmapFlipRotator)
431 {
432     TRACE("(%p,%p)\n", iface, ppIBitmapFlipRotator);
433     return FlipRotator_Create(ppIBitmapFlipRotator);
434 }
435
436 static HRESULT WINAPI ComponentFactory_CreateStream(IWICComponentFactory *iface,
437     IWICStream **ppIWICStream)
438 {
439     TRACE("(%p,%p)\n", iface, ppIWICStream);
440     return StreamImpl_Create(ppIWICStream);
441 }
442
443 static HRESULT WINAPI ComponentFactory_CreateColorContext(IWICComponentFactory *iface,
444     IWICColorContext **ppIColorContext)
445 {
446     TRACE("(%p,%p)\n", iface, ppIColorContext);
447     return ColorContext_Create(ppIColorContext);
448 }
449
450 static HRESULT WINAPI ComponentFactory_CreateColorTransformer(IWICComponentFactory *iface,
451     IWICColorTransform **ppIColorTransform)
452 {
453     FIXME("(%p,%p): stub\n", iface, ppIColorTransform);
454     return E_NOTIMPL;
455 }
456
457 static HRESULT WINAPI ComponentFactory_CreateBitmap(IWICComponentFactory *iface,
458     UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat,
459     WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
460 {
461     TRACE("(%p,%u,%u,%s,%u,%p)\n", iface, uiWidth, uiHeight,
462         debugstr_guid(pixelFormat), option, ppIBitmap);
463     return BitmapImpl_Create(uiWidth, uiHeight, 0, 0, NULL, pixelFormat, option, ppIBitmap);
464 }
465
466 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSource(IWICComponentFactory *iface,
467     IWICBitmapSource *piBitmapSource, WICBitmapCreateCacheOption option,
468     IWICBitmap **ppIBitmap)
469 {
470     IWICBitmap *result;
471     IWICBitmapLock *lock;
472     IWICPalette *palette;
473     UINT width, height;
474     WICPixelFormatGUID pixelformat = {0};
475     HRESULT hr;
476     WICRect rc;
477     double dpix, dpiy;
478     IWICComponentInfo *info;
479     IWICPixelFormatInfo2 *formatinfo;
480     WICPixelFormatNumericRepresentation format_type;
481
482     TRACE("(%p,%p,%u,%p)\n", iface, piBitmapSource, option, ppIBitmap);
483
484     if (!piBitmapSource || !ppIBitmap)
485         return E_INVALIDARG;
486
487     hr = IWICBitmapSource_GetSize(piBitmapSource, &width, &height);
488
489     if (SUCCEEDED(hr))
490         hr = IWICBitmapSource_GetPixelFormat(piBitmapSource, &pixelformat);
491
492     if (SUCCEEDED(hr))
493         hr = CreateComponentInfo(&pixelformat, &info);
494
495     if (SUCCEEDED(hr))
496     {
497         hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo2, (void**)&formatinfo);
498
499         if (SUCCEEDED(hr))
500         {
501             hr = IWICPixelFormatInfo2_GetNumericRepresentation(formatinfo, &format_type);
502
503             IWICPixelFormatInfo2_Release(formatinfo);
504         }
505
506         IWICComponentInfo_Release(info);
507     }
508
509     if (SUCCEEDED(hr))
510         hr = BitmapImpl_Create(width, height, 0, 0, NULL, &pixelformat, option, &result);
511
512     if (SUCCEEDED(hr))
513     {
514         hr = IWICBitmap_Lock(result, NULL, WICBitmapLockWrite, &lock);
515         if (SUCCEEDED(hr))
516         {
517             UINT stride, buffersize;
518             BYTE *buffer;
519             rc.X = rc.Y = 0;
520             rc.Width = width;
521             rc.Height = height;
522
523             hr = IWICBitmapLock_GetStride(lock, &stride);
524
525             if (SUCCEEDED(hr))
526                 hr = IWICBitmapLock_GetDataPointer(lock, &buffersize, &buffer);
527
528             if (SUCCEEDED(hr))
529                 hr = IWICBitmapSource_CopyPixels(piBitmapSource, &rc, stride,
530                     buffersize, buffer);
531
532             IWICBitmapLock_Release(lock);
533         }
534
535         if (SUCCEEDED(hr))
536             hr = PaletteImpl_Create(&palette);
537
538         if (SUCCEEDED(hr) && (format_type == WICPixelFormatNumericRepresentationUnspecified ||
539                               format_type == WICPixelFormatNumericRepresentationIndexed))
540         {
541             hr = IWICBitmapSource_CopyPalette(piBitmapSource, palette);
542
543             if (SUCCEEDED(hr))
544                 hr = IWICBitmap_SetPalette(result, palette);
545             else
546                 hr = S_OK;
547
548             IWICPalette_Release(palette);
549         }
550
551         if (SUCCEEDED(hr))
552         {
553             hr = IWICBitmapSource_GetResolution(piBitmapSource, &dpix, &dpiy);
554
555             if (SUCCEEDED(hr))
556                 hr = IWICBitmap_SetResolution(result, dpix, dpiy);
557             else
558                 hr = S_OK;
559         }
560
561         if (SUCCEEDED(hr))
562             *ppIBitmap = result;
563         else
564             IWICBitmap_Release(result);
565     }
566
567     return hr;
568 }
569
570 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSourceRect(IWICComponentFactory *iface,
571     IWICBitmapSource *piBitmapSource, UINT x, UINT y, UINT width, UINT height,
572     IWICBitmap **ppIBitmap)
573 {
574     FIXME("(%p,%p,%u,%u,%u,%u,%p): stub\n", iface, piBitmapSource, x, y, width,
575         height, ppIBitmap);
576     return E_NOTIMPL;
577 }
578
579 static HRESULT WINAPI ComponentFactory_CreateBitmapFromMemory(IWICComponentFactory *iface,
580     UINT width, UINT height, REFWICPixelFormatGUID format, UINT stride,
581     UINT size, BYTE *buffer, IWICBitmap **bitmap)
582 {
583     TRACE("(%p,%u,%u,%s,%u,%u,%p,%p\n", iface, width, height,
584         debugstr_guid(format), stride, size, buffer, bitmap);
585
586     if (!stride || !size || !buffer || !bitmap) return E_INVALIDARG;
587
588     return BitmapImpl_Create(width, height, stride, size, buffer, format, WICBitmapCacheOnLoad, bitmap);
589 }
590
591 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHBITMAP(IWICComponentFactory *iface,
592     HBITMAP hBitmap, HPALETTE hPalette, WICBitmapAlphaChannelOption options,
593     IWICBitmap **ppIBitmap)
594 {
595     FIXME("(%p,%p,%p,%u,%p): stub\n", iface, hBitmap, hPalette, options, ppIBitmap);
596     return E_NOTIMPL;
597 }
598
599 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHICON(IWICComponentFactory *iface,
600     HICON hicon, IWICBitmap **bitmap)
601 {
602     IWICBitmapLock *lock;
603     ICONINFO info;
604     BITMAP bm;
605     int width, height, x, y;
606     UINT stride, size;
607     BYTE *buffer;
608     DWORD *bits;
609     BITMAPINFO bi;
610     HDC hdc;
611     BOOL has_alpha;
612     HRESULT hr;
613
614     TRACE("(%p,%p,%p)\n", iface, hicon, bitmap);
615
616     if (!bitmap) return E_INVALIDARG;
617
618     if (!GetIconInfo(hicon, &info))
619         return HRESULT_FROM_WIN32(GetLastError());
620
621     GetObjectW(info.hbmColor ? info.hbmColor : info.hbmMask, sizeof(bm), &bm);
622
623     width = bm.bmWidth;
624     height = info.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
625     stride = width * 4;
626     size = stride * height;
627
628     hr = BitmapImpl_Create(width, height, stride, size, NULL,
629                            &GUID_WICPixelFormat32bppBGRA, WICBitmapCacheOnLoad, bitmap);
630     if (hr != S_OK) goto failed;
631
632     hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
633     if (hr != S_OK)
634     {
635         IWICBitmap_Release(*bitmap);
636         goto failed;
637     }
638     IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
639
640     hdc = CreateCompatibleDC(0);
641
642     memset(&bi, 0, sizeof(bi));
643     bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
644     bi.bmiHeader.biWidth = width;
645     bi.bmiHeader.biHeight = info.hbmColor ? -height: -height * 2;
646     bi.bmiHeader.biPlanes = 1;
647     bi.bmiHeader.biBitCount = 32;
648     bi.bmiHeader.biCompression = BI_RGB;
649
650     has_alpha = FALSE;
651
652     if (info.hbmColor)
653     {
654         GetDIBits(hdc, info.hbmColor, 0, height, buffer, &bi, DIB_RGB_COLORS);
655
656         if (bm.bmBitsPixel == 32)
657         {
658             /* If any pixel has a non-zero alpha, ignore hbmMask */
659             bits = (DWORD *)buffer;
660             for (x = 0; x < width && !has_alpha; x++, bits++)
661             {
662                 for (y = 0; y < height; y++)
663                 {
664                     if (*bits & 0xff000000)
665                     {
666                         has_alpha = TRUE;
667                         break;
668                     }
669                 }
670             }
671         }
672     }
673     else
674         GetDIBits(hdc, info.hbmMask, 0, height, buffer, &bi, DIB_RGB_COLORS);
675
676     if (!has_alpha)
677     {
678         DWORD *rgba;
679
680         if (info.hbmMask)
681         {
682             BYTE *mask;
683
684             mask = HeapAlloc(GetProcessHeap(), 0, size);
685             if (!mask)
686             {
687                 IWICBitmapLock_Release(lock);
688                 IWICBitmap_Release(*bitmap);
689                 DeleteDC(hdc);
690                 hr = E_OUTOFMEMORY;
691                 goto failed;
692             }
693
694             /* read alpha data from the mask */
695             GetDIBits(hdc, info.hbmMask, info.hbmColor ? 0 : height, height, mask, &bi, DIB_RGB_COLORS);
696
697             for (y = 0; y < height; y++)
698             {
699                 rgba = (DWORD *)(buffer + y * stride);
700                 bits = (DWORD *)(mask + y * stride);
701
702                 for (x = 0; x < width; x++, rgba++, bits++)
703                 {
704                     if (*bits)
705                         *rgba = 0;
706                     else
707                         *rgba |= 0xff000000;
708                 }
709             }
710
711             HeapFree(GetProcessHeap(), 0, mask);
712         }
713         else
714         {
715             /* set constant alpha of 255 */
716             for (y = 0; y < height; y++)
717             {
718                 rgba = (DWORD *)(buffer + y * stride);
719                 for (x = 0; x < width; x++, rgba++)
720                     *rgba |= 0xff000000;
721             }
722         }
723
724     }
725
726     IWICBitmapLock_Release(lock);
727     DeleteDC(hdc);
728
729 failed:
730     DeleteObject(info.hbmColor);
731     DeleteObject(info.hbmMask);
732
733     return hr;
734 }
735
736 static HRESULT WINAPI ComponentFactory_CreateComponentEnumerator(IWICComponentFactory *iface,
737     DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
738 {
739     TRACE("(%p,%u,%u,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
740     return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
741 }
742
743 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromDecoder(
744     IWICComponentFactory *iface, IWICBitmapDecoder *pIDecoder,
745     IWICFastMetadataEncoder **ppIFastEncoder)
746 {
747     FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
748     return E_NOTIMPL;
749 }
750
751 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromFrameDecode(
752     IWICComponentFactory *iface, IWICBitmapFrameDecode *pIFrameDecoder,
753     IWICFastMetadataEncoder **ppIFastEncoder)
754 {
755     FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
756     return E_NOTIMPL;
757 }
758
759 static HRESULT WINAPI ComponentFactory_CreateQueryWriter(IWICComponentFactory *iface,
760     REFGUID guidMetadataFormat, const GUID *pguidVendor,
761     IWICMetadataQueryWriter **ppIQueryWriter)
762 {
763     FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
764         debugstr_guid(pguidVendor), ppIQueryWriter);
765     return E_NOTIMPL;
766 }
767
768 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromReader(IWICComponentFactory *iface,
769     IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
770     IWICMetadataQueryWriter **ppIQueryWriter)
771 {
772     FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
773         ppIQueryWriter);
774     return E_NOTIMPL;
775 }
776
777 static HRESULT WINAPI ComponentFactory_CreateMetadataReader(IWICComponentFactory *iface,
778         REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
779 {
780     FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
781         options, stream, reader);
782     return E_NOTIMPL;
783 }
784
785 static HRESULT WINAPI ComponentFactory_CreateMetadataReaderFromContainer(IWICComponentFactory *iface,
786         REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
787 {
788     FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
789         options, stream, reader);
790     return E_NOTIMPL;
791 }
792
793 static HRESULT WINAPI ComponentFactory_CreateMetadataWriter(IWICComponentFactory *iface,
794         REFGUID format, const GUID *vendor, DWORD options, IWICMetadataWriter **writer)
795 {
796     FIXME("%p,%s,%s,%x,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor), options, writer);
797     return E_NOTIMPL;
798 }
799
800 static HRESULT WINAPI ComponentFactory_CreateMetadataWriterFromReader(IWICComponentFactory *iface,
801         IWICMetadataReader *reader, const GUID *vendor, IWICMetadataWriter **writer)
802 {
803     FIXME("%p,%p,%s,%p: stub\n", iface, reader, debugstr_guid(vendor), writer);
804     return E_NOTIMPL;
805 }
806
807 static HRESULT WINAPI ComponentFactory_CreateQueryReaderFromBlockReader(IWICComponentFactory *iface,
808         IWICMetadataBlockReader *block_reader, IWICMetadataQueryReader **query_reader)
809 {
810     FIXME("%p,%p,%p: stub\n", iface, block_reader, query_reader);
811     return E_NOTIMPL;
812 }
813
814 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromBlockWriter(IWICComponentFactory *iface,
815         IWICMetadataBlockWriter *block_writer, IWICMetadataQueryWriter **query_writer)
816 {
817     FIXME("%p,%p,%p: stub\n", iface, block_writer, query_writer);
818     return E_NOTIMPL;
819 }
820
821 static HRESULT WINAPI ComponentFactory_CreateEncoderPropertyBag(IWICComponentFactory *iface,
822         PROPBAG2 *options, UINT count, IPropertyBag2 **property)
823 {
824     FIXME("%p,%p,%u,%p: stub\n", iface, options, count, property);
825     return E_NOTIMPL;
826 }
827
828 static const IWICComponentFactoryVtbl ComponentFactory_Vtbl = {
829     ComponentFactory_QueryInterface,
830     ComponentFactory_AddRef,
831     ComponentFactory_Release,
832     ComponentFactory_CreateDecoderFromFilename,
833     ComponentFactory_CreateDecoderFromStream,
834     ComponentFactory_CreateDecoderFromFileHandle,
835     ComponentFactory_CreateComponentInfo,
836     ComponentFactory_CreateDecoder,
837     ComponentFactory_CreateEncoder,
838     ComponentFactory_CreatePalette,
839     ComponentFactory_CreateFormatConverter,
840     ComponentFactory_CreateBitmapScaler,
841     ComponentFactory_CreateBitmapClipper,
842     ComponentFactory_CreateBitmapFlipRotator,
843     ComponentFactory_CreateStream,
844     ComponentFactory_CreateColorContext,
845     ComponentFactory_CreateColorTransformer,
846     ComponentFactory_CreateBitmap,
847     ComponentFactory_CreateBitmapFromSource,
848     ComponentFactory_CreateBitmapFromSourceRect,
849     ComponentFactory_CreateBitmapFromMemory,
850     ComponentFactory_CreateBitmapFromHBITMAP,
851     ComponentFactory_CreateBitmapFromHICON,
852     ComponentFactory_CreateComponentEnumerator,
853     ComponentFactory_CreateFastMetadataEncoderFromDecoder,
854     ComponentFactory_CreateFastMetadataEncoderFromFrameDecode,
855     ComponentFactory_CreateQueryWriter,
856     ComponentFactory_CreateQueryWriterFromReader,
857     ComponentFactory_CreateMetadataReader,
858     ComponentFactory_CreateMetadataReaderFromContainer,
859     ComponentFactory_CreateMetadataWriter,
860     ComponentFactory_CreateMetadataWriterFromReader,
861     ComponentFactory_CreateQueryReaderFromBlockReader,
862     ComponentFactory_CreateQueryWriterFromBlockWriter,
863     ComponentFactory_CreateEncoderPropertyBag
864 };
865
866 HRESULT ComponentFactory_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
867 {
868     ComponentFactory *This;
869     HRESULT ret;
870
871     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
872
873     *ppv = NULL;
874
875     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
876
877     This = HeapAlloc(GetProcessHeap(), 0, sizeof(ComponentFactory));
878     if (!This) return E_OUTOFMEMORY;
879
880     This->IWICComponentFactory_iface.lpVtbl = &ComponentFactory_Vtbl;
881     This->ref = 1;
882
883     ret = IWICComponentFactory_QueryInterface(&This->IWICComponentFactory_iface, iid, ppv);
884     IWICComponentFactory_Release(&This->IWICComponentFactory_iface);
885
886     return ret;
887 }