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