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