Sync with trunk r63743.
[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(&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 BOOL get_16bpp_format(HBITMAP hbm, WICPixelFormatGUID *format)
589 {
590 BOOL ret = TRUE;
591 BITMAPV4HEADER bmh;
592 HDC hdc;
593
594 hdc = CreateCompatibleDC(0);
595
596 memset(&bmh, 0, sizeof(bmh));
597 bmh.bV4Size = sizeof(bmh);
598 bmh.bV4Width = 1;
599 bmh.bV4Height = 1;
600 bmh.bV4V4Compression = BI_BITFIELDS;
601 bmh.bV4BitCount = 16;
602
603 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO *)&bmh, DIB_RGB_COLORS);
604
605 if (bmh.bV4RedMask == 0x7c00 &&
606 bmh.bV4GreenMask == 0x3e0 &&
607 bmh.bV4BlueMask == 0x1f)
608 {
609 *format = GUID_WICPixelFormat16bppBGR555;
610 }
611 else if (bmh.bV4RedMask == 0xf800 &&
612 bmh.bV4GreenMask == 0x7e0 &&
613 bmh.bV4BlueMask == 0x1f)
614 {
615 *format = GUID_WICPixelFormat16bppBGR565;
616 }
617 else
618 {
619 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
620 bmh.bV4GreenMask, bmh.bV4BlueMask);
621 ret = FALSE;
622 }
623
624 DeleteDC(hdc);
625 return ret;
626 }
627
628 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHBITMAP(IWICComponentFactory *iface,
629 HBITMAP hbm, HPALETTE hpal, WICBitmapAlphaChannelOption option, IWICBitmap **bitmap)
630 {
631 BITMAP bm;
632 HRESULT hr;
633 WICPixelFormatGUID format;
634 IWICBitmapLock *lock;
635 UINT size, num_palette_entries = 0;
636 PALETTEENTRY entry[256];
637
638 TRACE("(%p,%p,%p,%u,%p)\n", iface, hbm, hpal, option, bitmap);
639
640 if (!bitmap) return E_INVALIDARG;
641
642 if (GetObjectW(hbm, sizeof(bm), &bm) != sizeof(bm))
643 return WINCODEC_ERR_WIN32ERROR;
644
645 if (hpal)
646 {
647 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
648 if (!num_palette_entries)
649 return WINCODEC_ERR_WIN32ERROR;
650 }
651
652 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
653 switch(bm.bmBitsPixel)
654 {
655 case 1:
656 format = GUID_WICPixelFormat1bppIndexed;
657 break;
658 case 4:
659 format = GUID_WICPixelFormat4bppIndexed;
660 break;
661 case 8:
662 format = GUID_WICPixelFormat8bppIndexed;
663 break;
664 case 16:
665 if (!get_16bpp_format(hbm, &format))
666 return E_INVALIDARG;
667 break;
668 case 24:
669 format = GUID_WICPixelFormat24bppBGR;
670 break;
671 case 32:
672 switch (option)
673 {
674 case WICBitmapUseAlpha:
675 format = GUID_WICPixelFormat32bppBGRA;
676 break;
677 case WICBitmapUsePremultipliedAlpha:
678 format = GUID_WICPixelFormat32bppPBGRA;
679 break;
680 case WICBitmapIgnoreAlpha:
681 format = GUID_WICPixelFormat32bppBGR;
682 break;
683 default:
684 return E_INVALIDARG;
685 }
686 break;
687 case 48:
688 format = GUID_WICPixelFormat48bppRGB;
689 break;
690 default:
691 FIXME("unsupported %d bpp\n", bm.bmBitsPixel);
692 return E_INVALIDARG;
693 }
694
695 hr = BitmapImpl_Create(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, 0, NULL, &format, option, bitmap);
696 if (hr != S_OK) return hr;
697
698 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
699 if (hr == S_OK)
700 {
701 BYTE *buffer;
702 HDC hdc;
703 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors) + 256 * sizeof(RGBQUAD)];
704 BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
705
706 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
707
708 hdc = CreateCompatibleDC(0);
709
710 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
711 bmi->bmiHeader.biBitCount = 0;
712 GetDIBits(hdc, hbm, 0, 0, NULL, bmi, DIB_RGB_COLORS);
713 bmi->bmiHeader.biHeight = -bm.bmHeight;
714 GetDIBits(hdc, hbm, 0, bm.bmHeight, buffer, bmi, DIB_RGB_COLORS);
715
716 DeleteDC(hdc);
717 IWICBitmapLock_Release(lock);
718
719 if (num_palette_entries)
720 {
721 IWICPalette *palette;
722 WICColor colors[256];
723 UINT i;
724
725 hr = PaletteImpl_Create(&palette);
726 if (hr == S_OK)
727 {
728 for (i = 0; i < num_palette_entries; i++)
729 colors[i] = 0xff000000 | entry[i].peRed << 16 |
730 entry[i].peGreen << 8 | entry[i].peBlue;
731
732 hr = IWICPalette_InitializeCustom(palette, colors, num_palette_entries);
733 if (hr == S_OK)
734 hr = IWICBitmap_SetPalette(*bitmap, palette);
735
736 IWICPalette_Release(palette);
737 }
738 }
739 }
740
741 if (hr != S_OK)
742 {
743 IWICBitmap_Release(*bitmap);
744 *bitmap = NULL;
745 }
746
747 return hr;
748 }
749
750 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHICON(IWICComponentFactory *iface,
751 HICON hicon, IWICBitmap **bitmap)
752 {
753 IWICBitmapLock *lock;
754 ICONINFO info;
755 BITMAP bm;
756 int width, height, x, y;
757 UINT stride, size;
758 BYTE *buffer;
759 DWORD *bits;
760 BITMAPINFO bi;
761 HDC hdc;
762 BOOL has_alpha;
763 HRESULT hr;
764
765 TRACE("(%p,%p,%p)\n", iface, hicon, bitmap);
766
767 if (!bitmap) return E_INVALIDARG;
768
769 if (!GetIconInfo(hicon, &info))
770 return HRESULT_FROM_WIN32(GetLastError());
771
772 GetObjectW(info.hbmColor ? info.hbmColor : info.hbmMask, sizeof(bm), &bm);
773
774 width = bm.bmWidth;
775 height = info.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
776 stride = width * 4;
777 size = stride * height;
778
779 hr = BitmapImpl_Create(width, height, stride, size, NULL,
780 &GUID_WICPixelFormat32bppBGRA, WICBitmapCacheOnLoad, bitmap);
781 if (hr != S_OK) goto failed;
782
783 hr = IWICBitmap_Lock(*bitmap, NULL, WICBitmapLockWrite, &lock);
784 if (hr != S_OK)
785 {
786 IWICBitmap_Release(*bitmap);
787 goto failed;
788 }
789 IWICBitmapLock_GetDataPointer(lock, &size, &buffer);
790
791 hdc = CreateCompatibleDC(0);
792
793 memset(&bi, 0, sizeof(bi));
794 bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
795 bi.bmiHeader.biWidth = width;
796 bi.bmiHeader.biHeight = info.hbmColor ? -height: -height * 2;
797 bi.bmiHeader.biPlanes = 1;
798 bi.bmiHeader.biBitCount = 32;
799 bi.bmiHeader.biCompression = BI_RGB;
800
801 has_alpha = FALSE;
802
803 if (info.hbmColor)
804 {
805 GetDIBits(hdc, info.hbmColor, 0, height, buffer, &bi, DIB_RGB_COLORS);
806
807 if (bm.bmBitsPixel == 32)
808 {
809 /* If any pixel has a non-zero alpha, ignore hbmMask */
810 bits = (DWORD *)buffer;
811 for (x = 0; x < width && !has_alpha; x++, bits++)
812 {
813 for (y = 0; y < height; y++)
814 {
815 if (*bits & 0xff000000)
816 {
817 has_alpha = TRUE;
818 break;
819 }
820 }
821 }
822 }
823 }
824 else
825 GetDIBits(hdc, info.hbmMask, 0, height, buffer, &bi, DIB_RGB_COLORS);
826
827 if (!has_alpha)
828 {
829 DWORD *rgba;
830
831 if (info.hbmMask)
832 {
833 BYTE *mask;
834
835 mask = HeapAlloc(GetProcessHeap(), 0, size);
836 if (!mask)
837 {
838 IWICBitmapLock_Release(lock);
839 IWICBitmap_Release(*bitmap);
840 DeleteDC(hdc);
841 hr = E_OUTOFMEMORY;
842 goto failed;
843 }
844
845 /* read alpha data from the mask */
846 GetDIBits(hdc, info.hbmMask, info.hbmColor ? 0 : height, height, mask, &bi, DIB_RGB_COLORS);
847
848 for (y = 0; y < height; y++)
849 {
850 rgba = (DWORD *)(buffer + y * stride);
851 bits = (DWORD *)(mask + y * stride);
852
853 for (x = 0; x < width; x++, rgba++, bits++)
854 {
855 if (*bits)
856 *rgba = 0;
857 else
858 *rgba |= 0xff000000;
859 }
860 }
861
862 HeapFree(GetProcessHeap(), 0, mask);
863 }
864 else
865 {
866 /* set constant alpha of 255 */
867 for (y = 0; y < height; y++)
868 {
869 rgba = (DWORD *)(buffer + y * stride);
870 for (x = 0; x < width; x++, rgba++)
871 *rgba |= 0xff000000;
872 }
873 }
874
875 }
876
877 IWICBitmapLock_Release(lock);
878 DeleteDC(hdc);
879
880 failed:
881 DeleteObject(info.hbmColor);
882 DeleteObject(info.hbmMask);
883
884 return hr;
885 }
886
887 static HRESULT WINAPI ComponentFactory_CreateComponentEnumerator(IWICComponentFactory *iface,
888 DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
889 {
890 TRACE("(%p,%u,%u,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
891 return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
892 }
893
894 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromDecoder(
895 IWICComponentFactory *iface, IWICBitmapDecoder *pIDecoder,
896 IWICFastMetadataEncoder **ppIFastEncoder)
897 {
898 FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
899 return E_NOTIMPL;
900 }
901
902 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromFrameDecode(
903 IWICComponentFactory *iface, IWICBitmapFrameDecode *pIFrameDecoder,
904 IWICFastMetadataEncoder **ppIFastEncoder)
905 {
906 FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
907 return E_NOTIMPL;
908 }
909
910 static HRESULT WINAPI ComponentFactory_CreateQueryWriter(IWICComponentFactory *iface,
911 REFGUID guidMetadataFormat, const GUID *pguidVendor,
912 IWICMetadataQueryWriter **ppIQueryWriter)
913 {
914 FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
915 debugstr_guid(pguidVendor), ppIQueryWriter);
916 return E_NOTIMPL;
917 }
918
919 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromReader(IWICComponentFactory *iface,
920 IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
921 IWICMetadataQueryWriter **ppIQueryWriter)
922 {
923 FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
924 ppIQueryWriter);
925 return E_NOTIMPL;
926 }
927
928 static HRESULT WINAPI ComponentFactory_CreateMetadataReader(IWICComponentFactory *iface,
929 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
930 {
931 FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
932 options, stream, reader);
933 return E_NOTIMPL;
934 }
935
936 static HRESULT WINAPI ComponentFactory_CreateMetadataReaderFromContainer(IWICComponentFactory *iface,
937 REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
938 {
939 FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
940 options, stream, reader);
941 return E_NOTIMPL;
942 }
943
944 static HRESULT WINAPI ComponentFactory_CreateMetadataWriter(IWICComponentFactory *iface,
945 REFGUID format, const GUID *vendor, DWORD options, IWICMetadataWriter **writer)
946 {
947 FIXME("%p,%s,%s,%x,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor), options, writer);
948 return E_NOTIMPL;
949 }
950
951 static HRESULT WINAPI ComponentFactory_CreateMetadataWriterFromReader(IWICComponentFactory *iface,
952 IWICMetadataReader *reader, const GUID *vendor, IWICMetadataWriter **writer)
953 {
954 FIXME("%p,%p,%s,%p: stub\n", iface, reader, debugstr_guid(vendor), writer);
955 return E_NOTIMPL;
956 }
957
958 static HRESULT WINAPI ComponentFactory_CreateQueryReaderFromBlockReader(IWICComponentFactory *iface,
959 IWICMetadataBlockReader *block_reader, IWICMetadataQueryReader **query_reader)
960 {
961 FIXME("%p,%p,%p: stub\n", iface, block_reader, query_reader);
962 return E_NOTIMPL;
963 }
964
965 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromBlockWriter(IWICComponentFactory *iface,
966 IWICMetadataBlockWriter *block_writer, IWICMetadataQueryWriter **query_writer)
967 {
968 FIXME("%p,%p,%p: stub\n", iface, block_writer, query_writer);
969 return E_NOTIMPL;
970 }
971
972 static HRESULT WINAPI ComponentFactory_CreateEncoderPropertyBag(IWICComponentFactory *iface,
973 PROPBAG2 *options, UINT count, IPropertyBag2 **property)
974 {
975 TRACE("(%p,%p,%u,%p)\n", iface, options, count, property);
976 return CreatePropertyBag2(options, count, property);
977 }
978
979 static const IWICComponentFactoryVtbl ComponentFactory_Vtbl = {
980 ComponentFactory_QueryInterface,
981 ComponentFactory_AddRef,
982 ComponentFactory_Release,
983 ComponentFactory_CreateDecoderFromFilename,
984 ComponentFactory_CreateDecoderFromStream,
985 ComponentFactory_CreateDecoderFromFileHandle,
986 ComponentFactory_CreateComponentInfo,
987 ComponentFactory_CreateDecoder,
988 ComponentFactory_CreateEncoder,
989 ComponentFactory_CreatePalette,
990 ComponentFactory_CreateFormatConverter,
991 ComponentFactory_CreateBitmapScaler,
992 ComponentFactory_CreateBitmapClipper,
993 ComponentFactory_CreateBitmapFlipRotator,
994 ComponentFactory_CreateStream,
995 ComponentFactory_CreateColorContext,
996 ComponentFactory_CreateColorTransformer,
997 ComponentFactory_CreateBitmap,
998 ComponentFactory_CreateBitmapFromSource,
999 ComponentFactory_CreateBitmapFromSourceRect,
1000 ComponentFactory_CreateBitmapFromMemory,
1001 ComponentFactory_CreateBitmapFromHBITMAP,
1002 ComponentFactory_CreateBitmapFromHICON,
1003 ComponentFactory_CreateComponentEnumerator,
1004 ComponentFactory_CreateFastMetadataEncoderFromDecoder,
1005 ComponentFactory_CreateFastMetadataEncoderFromFrameDecode,
1006 ComponentFactory_CreateQueryWriter,
1007 ComponentFactory_CreateQueryWriterFromReader,
1008 ComponentFactory_CreateMetadataReader,
1009 ComponentFactory_CreateMetadataReaderFromContainer,
1010 ComponentFactory_CreateMetadataWriter,
1011 ComponentFactory_CreateMetadataWriterFromReader,
1012 ComponentFactory_CreateQueryReaderFromBlockReader,
1013 ComponentFactory_CreateQueryWriterFromBlockWriter,
1014 ComponentFactory_CreateEncoderPropertyBag
1015 };
1016
1017 HRESULT ComponentFactory_CreateInstance(REFIID iid, void** ppv)
1018 {
1019 ComponentFactory *This;
1020 HRESULT ret;
1021
1022 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1023
1024 *ppv = NULL;
1025
1026 This = HeapAlloc(GetProcessHeap(), 0, sizeof(ComponentFactory));
1027 if (!This) return E_OUTOFMEMORY;
1028
1029 This->IWICComponentFactory_iface.lpVtbl = &ComponentFactory_Vtbl;
1030 This->ref = 1;
1031
1032 ret = IWICComponentFactory_QueryInterface(&This->IWICComponentFactory_iface, iid, ppv);
1033 IWICComponentFactory_Release(&This->IWICComponentFactory_iface);
1034
1035 return ret;
1036 }