2 * Copyright 2009 Vincent Povirk
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wincodecs_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
36 struct FormatConverter
;
59 typedef HRESULT (*copyfunc
)(struct FormatConverter
*This
, const WICRect
*prc
,
60 UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
, enum pixelformat source_format
);
62 struct pixelformatinfo
{
63 enum pixelformat format
;
64 const WICPixelFormatGUID
*guid
;
65 copyfunc copy_function
;
68 typedef struct FormatConverter
{
69 IWICFormatConverter IWICFormatConverter_iface
;
71 IWICBitmapSource
*source
;
72 const struct pixelformatinfo
*dst_format
, *src_format
;
73 WICBitmapDitherType dither
;
74 double alpha_threshold
;
75 WICBitmapPaletteType palette_type
;
76 CRITICAL_SECTION lock
; /* must be held when initialized */
79 static inline FormatConverter
*impl_from_IWICFormatConverter(IWICFormatConverter
*iface
)
81 return CONTAINING_RECORD(iface
, FormatConverter
, IWICFormatConverter_iface
);
84 static void make_grayscale_palette(WICColor
*colors
, UINT num_colors
)
87 for (i
=0; i
<num_colors
; i
++)
89 v
= i
* 255 / (num_colors
-1);
90 colors
[i
] = 0xff000000 | v
<<16 | v
<<8 | v
;
94 static HRESULT
copypixels_to_32bppBGRA(struct FormatConverter
*This
, const WICRect
*prc
,
95 UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
, enum pixelformat source_format
)
97 switch (source_format
)
99 case format_1bppIndexed
:
100 case format_BlackWhite
:
106 UINT srcstride
, srcdatasize
;
112 IWICPalette
*palette
;
115 if (source_format
== format_1bppIndexed
)
117 res
= PaletteImpl_Create(&palette
);
118 if (FAILED(res
)) return res
;
120 res
= IWICBitmapSource_CopyPalette(This
->source
, palette
);
122 res
= IWICPalette_GetColors(palette
, 2, colors
, &actualcolors
);
124 IWICPalette_Release(palette
);
126 if (FAILED(res
)) return res
;
130 colors
[0] = 0xff000000;
131 colors
[1] = 0xffffffff;
134 srcstride
= (prc
->Width
+7)/8;
135 srcdatasize
= srcstride
* prc
->Height
;
137 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
138 if (!srcdata
) return E_OUTOFMEMORY
;
140 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
146 for (y
=0; y
<prc
->Height
; y
++) {
147 srcbyte
=(const BYTE
*)srcrow
;
148 dstpixel
=(DWORD
*)dstrow
;
149 for (x
=0; x
<prc
->Width
; x
+=8) {
152 *dstpixel
++ = colors
[srcval
>>7&1];
153 if (x
+1 < prc
->Width
) *dstpixel
++ = colors
[srcval
>>6&1];
154 if (x
+2 < prc
->Width
) *dstpixel
++ = colors
[srcval
>>5&1];
155 if (x
+3 < prc
->Width
) *dstpixel
++ = colors
[srcval
>>4&1];
156 if (x
+4 < prc
->Width
) *dstpixel
++ = colors
[srcval
>>3&1];
157 if (x
+5 < prc
->Width
) *dstpixel
++ = colors
[srcval
>>2&1];
158 if (x
+6 < prc
->Width
) *dstpixel
++ = colors
[srcval
>>1&1];
159 if (x
+7 < prc
->Width
) *dstpixel
++ = colors
[srcval
&1];
166 HeapFree(GetProcessHeap(), 0, srcdata
);
171 case format_2bppIndexed
:
172 case format_2bppGray
:
178 UINT srcstride
, srcdatasize
;
184 IWICPalette
*palette
;
187 if (source_format
== format_2bppIndexed
)
189 res
= PaletteImpl_Create(&palette
);
190 if (FAILED(res
)) return res
;
192 res
= IWICBitmapSource_CopyPalette(This
->source
, palette
);
194 res
= IWICPalette_GetColors(palette
, 4, colors
, &actualcolors
);
196 IWICPalette_Release(palette
);
198 if (FAILED(res
)) return res
;
201 make_grayscale_palette(colors
, 4);
203 srcstride
= (prc
->Width
+3)/4;
204 srcdatasize
= srcstride
* prc
->Height
;
206 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
207 if (!srcdata
) return E_OUTOFMEMORY
;
209 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
215 for (y
=0; y
<prc
->Height
; y
++) {
216 srcbyte
=(const BYTE
*)srcrow
;
217 dstpixel
=(DWORD
*)dstrow
;
218 for (x
=0; x
<prc
->Width
; x
+=4) {
221 *dstpixel
++ = colors
[srcval
>>6];
222 if (x
+1 < prc
->Width
) *dstpixel
++ = colors
[srcval
>>4&0x3];
223 if (x
+2 < prc
->Width
) *dstpixel
++ = colors
[srcval
>>2&0x3];
224 if (x
+1 < prc
->Width
) *dstpixel
++ = colors
[srcval
&0x3];
231 HeapFree(GetProcessHeap(), 0, srcdata
);
236 case format_4bppIndexed
:
237 case format_4bppGray
:
243 UINT srcstride
, srcdatasize
;
249 IWICPalette
*palette
;
252 if (source_format
== format_4bppIndexed
)
254 res
= PaletteImpl_Create(&palette
);
255 if (FAILED(res
)) return res
;
257 res
= IWICBitmapSource_CopyPalette(This
->source
, palette
);
259 res
= IWICPalette_GetColors(palette
, 16, colors
, &actualcolors
);
261 IWICPalette_Release(palette
);
263 if (FAILED(res
)) return res
;
266 make_grayscale_palette(colors
, 16);
268 srcstride
= (prc
->Width
+1)/2;
269 srcdatasize
= srcstride
* prc
->Height
;
271 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
272 if (!srcdata
) return E_OUTOFMEMORY
;
274 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
280 for (y
=0; y
<prc
->Height
; y
++) {
281 srcbyte
=(const BYTE
*)srcrow
;
282 dstpixel
=(DWORD
*)dstrow
;
283 for (x
=0; x
<prc
->Width
; x
+=2) {
286 *dstpixel
++ = colors
[srcval
>>4];
287 if (x
+1 < prc
->Width
) *dstpixel
++ = colors
[srcval
&0xf];
294 HeapFree(GetProcessHeap(), 0, srcdata
);
299 case format_8bppGray
:
305 UINT srcstride
, srcdatasize
;
311 srcstride
= prc
->Width
;
312 srcdatasize
= srcstride
* prc
->Height
;
314 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
315 if (!srcdata
) return E_OUTOFMEMORY
;
317 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
323 for (y
=0; y
<prc
->Height
; y
++) {
324 srcbyte
=(const BYTE
*)srcrow
;
325 dstpixel
=(DWORD
*)dstrow
;
326 for (x
=0; x
<prc
->Width
; x
++)
328 *dstpixel
++ = 0xff000000|(*srcbyte
<<16)|(*srcbyte
<<8)|*srcbyte
;
336 HeapFree(GetProcessHeap(), 0, srcdata
);
341 case format_8bppIndexed
:
347 UINT srcstride
, srcdatasize
;
352 WICColor colors
[256];
353 IWICPalette
*palette
;
356 res
= PaletteImpl_Create(&palette
);
357 if (FAILED(res
)) return res
;
359 res
= IWICBitmapSource_CopyPalette(This
->source
, palette
);
361 res
= IWICPalette_GetColors(palette
, 256, colors
, &actualcolors
);
363 IWICPalette_Release(palette
);
365 if (FAILED(res
)) return res
;
367 srcstride
= prc
->Width
;
368 srcdatasize
= srcstride
* prc
->Height
;
370 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
371 if (!srcdata
) return E_OUTOFMEMORY
;
373 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
379 for (y
=0; y
<prc
->Height
; y
++) {
380 srcbyte
=(const BYTE
*)srcrow
;
381 dstpixel
=(DWORD
*)dstrow
;
382 for (x
=0; x
<prc
->Width
; x
++)
383 *dstpixel
++ = colors
[*srcbyte
++];
389 HeapFree(GetProcessHeap(), 0, srcdata
);
394 case format_16bppGray
:
400 UINT srcstride
, srcdatasize
;
406 srcstride
= prc
->Width
* 2;
407 srcdatasize
= srcstride
* prc
->Height
;
409 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
410 if (!srcdata
) return E_OUTOFMEMORY
;
412 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
418 for (y
=0; y
<prc
->Height
; y
++) {
419 srcbyte
=(const BYTE
*)srcrow
;
420 dstpixel
=(DWORD
*)dstrow
;
421 for (x
=0; x
<prc
->Width
; x
++)
423 *dstpixel
++ = 0xff000000|(*srcbyte
<<16)|(*srcbyte
<<8)|*srcbyte
;
431 HeapFree(GetProcessHeap(), 0, srcdata
);
436 case format_16bppBGR555
:
442 UINT srcstride
, srcdatasize
;
444 const WORD
*srcpixel
;
448 srcstride
= 2 * prc
->Width
;
449 srcdatasize
= srcstride
* prc
->Height
;
451 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
452 if (!srcdata
) return E_OUTOFMEMORY
;
454 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
460 for (y
=0; y
<prc
->Height
; y
++) {
461 srcpixel
=(const WORD
*)srcrow
;
462 dstpixel
=(DWORD
*)dstrow
;
463 for (x
=0; x
<prc
->Width
; x
++) {
466 *dstpixel
++=0xff000000 | /* constant 255 alpha */
467 ((srcval
<< 9) & 0xf80000) | /* r */
468 ((srcval
<< 4) & 0x070000) | /* r - 3 bits */
469 ((srcval
<< 6) & 0x00f800) | /* g */
470 ((srcval
<< 1) & 0x000700) | /* g - 3 bits */
471 ((srcval
<< 3) & 0x0000f8) | /* b */
472 ((srcval
>> 2) & 0x000007); /* b - 3 bits */
479 HeapFree(GetProcessHeap(), 0, srcdata
);
484 case format_16bppBGR565
:
490 UINT srcstride
, srcdatasize
;
492 const WORD
*srcpixel
;
496 srcstride
= 2 * prc
->Width
;
497 srcdatasize
= srcstride
* prc
->Height
;
499 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
500 if (!srcdata
) return E_OUTOFMEMORY
;
502 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
508 for (y
=0; y
<prc
->Height
; y
++) {
509 srcpixel
=(const WORD
*)srcrow
;
510 dstpixel
=(DWORD
*)dstrow
;
511 for (x
=0; x
<prc
->Width
; x
++) {
514 *dstpixel
++=0xff000000 | /* constant 255 alpha */
515 ((srcval
<< 8) & 0xf80000) | /* r */
516 ((srcval
<< 3) & 0x070000) | /* r - 3 bits */
517 ((srcval
<< 5) & 0x00fc00) | /* g */
518 ((srcval
>> 1) & 0x000300) | /* g - 2 bits */
519 ((srcval
<< 3) & 0x0000f8) | /* b */
520 ((srcval
>> 2) & 0x000007); /* b - 3 bits */
527 HeapFree(GetProcessHeap(), 0, srcdata
);
532 case format_16bppBGRA5551
:
538 UINT srcstride
, srcdatasize
;
540 const WORD
*srcpixel
;
544 srcstride
= 2 * prc
->Width
;
545 srcdatasize
= srcstride
* prc
->Height
;
547 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
548 if (!srcdata
) return E_OUTOFMEMORY
;
550 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
556 for (y
=0; y
<prc
->Height
; y
++) {
557 srcpixel
=(const WORD
*)srcrow
;
558 dstpixel
=(DWORD
*)dstrow
;
559 for (x
=0; x
<prc
->Width
; x
++) {
562 *dstpixel
++=((srcval
& 0x8000) ? 0xff000000 : 0) | /* alpha */
563 ((srcval
<< 9) & 0xf80000) | /* r */
564 ((srcval
<< 4) & 0x070000) | /* r - 3 bits */
565 ((srcval
<< 6) & 0x00f800) | /* g */
566 ((srcval
<< 1) & 0x000700) | /* g - 3 bits */
567 ((srcval
<< 3) & 0x0000f8) | /* b */
568 ((srcval
>> 2) & 0x000007); /* b - 3 bits */
575 HeapFree(GetProcessHeap(), 0, srcdata
);
580 case format_24bppBGR
:
586 UINT srcstride
, srcdatasize
;
588 const BYTE
*srcpixel
;
592 srcstride
= 3 * prc
->Width
;
593 srcdatasize
= srcstride
* prc
->Height
;
595 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
596 if (!srcdata
) return E_OUTOFMEMORY
;
598 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
604 for (y
=0; y
<prc
->Height
; y
++) {
607 for (x
=0; x
<prc
->Width
; x
++) {
608 *dstpixel
++=*srcpixel
++; /* blue */
609 *dstpixel
++=*srcpixel
++; /* green */
610 *dstpixel
++=*srcpixel
++; /* red */
611 *dstpixel
++=255; /* alpha */
618 HeapFree(GetProcessHeap(), 0, srcdata
);
623 case format_32bppBGR
:
629 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
630 if (FAILED(res
)) return res
;
632 /* set all alpha values to 255 */
633 for (y
=0; y
<prc
->Height
; y
++)
634 for (x
=0; x
<prc
->Width
; x
++)
635 pbBuffer
[cbStride
*y
+4*x
+3] = 0xff;
638 case format_32bppBGRA
:
640 return IWICBitmapSource_CopyPixels(This
->source
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
642 case format_48bppRGB
:
648 UINT srcstride
, srcdatasize
;
650 const BYTE
*srcpixel
;
654 srcstride
= 6 * prc
->Width
;
655 srcdatasize
= srcstride
* prc
->Height
;
657 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
658 if (!srcdata
) return E_OUTOFMEMORY
;
660 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
666 for (y
=0; y
<prc
->Height
; y
++) {
668 dstpixel
=(DWORD
*)dstrow
;
669 for (x
=0; x
<prc
->Width
; x
++) {
670 BYTE red
, green
, blue
;
671 red
= *srcpixel
++; srcpixel
++;
672 green
= *srcpixel
++; srcpixel
++;
673 blue
= *srcpixel
++; srcpixel
++;
674 *dstpixel
++=0xff000000|red
<<16|green
<<8|blue
;
681 HeapFree(GetProcessHeap(), 0, srcdata
);
686 case format_64bppRGBA
:
692 UINT srcstride
, srcdatasize
;
694 const BYTE
*srcpixel
;
698 srcstride
= 8 * prc
->Width
;
699 srcdatasize
= srcstride
* prc
->Height
;
701 srcdata
= HeapAlloc(GetProcessHeap(), 0, srcdatasize
);
702 if (!srcdata
) return E_OUTOFMEMORY
;
704 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, srcstride
, srcdatasize
, srcdata
);
710 for (y
=0; y
<prc
->Height
; y
++) {
712 dstpixel
=(DWORD
*)dstrow
;
713 for (x
=0; x
<prc
->Width
; x
++) {
714 BYTE red
, green
, blue
, alpha
;
715 red
= *srcpixel
++; srcpixel
++;
716 green
= *srcpixel
++; srcpixel
++;
717 blue
= *srcpixel
++; srcpixel
++;
718 alpha
= *srcpixel
++; srcpixel
++;
719 *dstpixel
++=alpha
<<24|red
<<16|green
<<8|blue
;
726 HeapFree(GetProcessHeap(), 0, srcdata
);
731 case format_32bppCMYK
:
737 res
= IWICBitmapSource_CopyPixels(This
->source
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
738 if (FAILED(res
)) return res
;
740 for (y
=0; y
<prc
->Height
; y
++)
741 for (x
=0; x
<prc
->Width
; x
++)
743 BYTE
*pixel
= pbBuffer
+cbStride
*y
+4*x
;
744 BYTE c
=pixel
[0], m
=pixel
[1], y
=pixel
[2], k
=pixel
[3];
745 pixel
[0] = (255-y
)*(255-k
)/255; /* blue */
746 pixel
[1] = (255-m
)*(255-k
)/255; /* green */
747 pixel
[2] = (255-c
)*(255-k
)/255; /* red */
748 pixel
[3] = 255; /* alpha */
753 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
757 static HRESULT
copypixels_to_32bppBGR(struct FormatConverter
*This
, const WICRect
*prc
,
758 UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
, enum pixelformat source_format
)
760 switch (source_format
)
762 case format_32bppBGR
:
763 case format_32bppBGRA
:
765 return IWICBitmapSource_CopyPixels(This
->source
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
768 return copypixels_to_32bppBGRA(This
, prc
, cbStride
, cbBufferSize
, pbBuffer
, source_format
);
772 static const struct pixelformatinfo supported_formats
[] = {
773 {format_1bppIndexed
, &GUID_WICPixelFormat1bppIndexed
, NULL
},
774 {format_2bppIndexed
, &GUID_WICPixelFormat2bppIndexed
, NULL
},
775 {format_4bppIndexed
, &GUID_WICPixelFormat4bppIndexed
, NULL
},
776 {format_8bppIndexed
, &GUID_WICPixelFormat8bppIndexed
, NULL
},
777 {format_BlackWhite
, &GUID_WICPixelFormatBlackWhite
, NULL
},
778 {format_2bppGray
, &GUID_WICPixelFormat2bppGray
, NULL
},
779 {format_4bppGray
, &GUID_WICPixelFormat4bppGray
, NULL
},
780 {format_8bppGray
, &GUID_WICPixelFormat8bppGray
, NULL
},
781 {format_16bppGray
, &GUID_WICPixelFormat16bppGray
, NULL
},
782 {format_16bppBGR555
, &GUID_WICPixelFormat16bppBGR555
, NULL
},
783 {format_16bppBGR565
, &GUID_WICPixelFormat16bppBGR565
, NULL
},
784 {format_16bppBGRA5551
, &GUID_WICPixelFormat16bppBGRA5551
, NULL
},
785 {format_24bppBGR
, &GUID_WICPixelFormat24bppBGR
, NULL
},
786 {format_32bppBGR
, &GUID_WICPixelFormat32bppBGR
, copypixels_to_32bppBGR
},
787 {format_32bppBGRA
, &GUID_WICPixelFormat32bppBGRA
, copypixels_to_32bppBGRA
},
788 {format_48bppRGB
, &GUID_WICPixelFormat48bppRGB
, NULL
},
789 {format_64bppRGBA
, &GUID_WICPixelFormat64bppRGBA
, NULL
},
790 {format_32bppCMYK
, &GUID_WICPixelFormat32bppCMYK
, NULL
},
794 static const struct pixelformatinfo
*get_formatinfo(const WICPixelFormatGUID
*format
)
798 for (i
=0; supported_formats
[i
].guid
; i
++)
799 if (IsEqualGUID(supported_formats
[i
].guid
, format
)) return &supported_formats
[i
];
804 static HRESULT WINAPI
FormatConverter_QueryInterface(IWICFormatConverter
*iface
, REFIID iid
,
807 FormatConverter
*This
= impl_from_IWICFormatConverter(iface
);
808 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
810 if (!ppv
) return E_INVALIDARG
;
812 if (IsEqualIID(&IID_IUnknown
, iid
) ||
813 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
814 IsEqualIID(&IID_IWICFormatConverter
, iid
))
821 return E_NOINTERFACE
;
824 IUnknown_AddRef((IUnknown
*)*ppv
);
828 static ULONG WINAPI
FormatConverter_AddRef(IWICFormatConverter
*iface
)
830 FormatConverter
*This
= impl_from_IWICFormatConverter(iface
);
831 ULONG ref
= InterlockedIncrement(&This
->ref
);
833 TRACE("(%p) refcount=%u\n", iface
, ref
);
838 static ULONG WINAPI
FormatConverter_Release(IWICFormatConverter
*iface
)
840 FormatConverter
*This
= impl_from_IWICFormatConverter(iface
);
841 ULONG ref
= InterlockedDecrement(&This
->ref
);
843 TRACE("(%p) refcount=%u\n", iface
, ref
);
847 This
->lock
.DebugInfo
->Spare
[0] = 0;
848 DeleteCriticalSection(&This
->lock
);
849 if (This
->source
) IWICBitmapSource_Release(This
->source
);
850 HeapFree(GetProcessHeap(), 0, This
);
856 static HRESULT WINAPI
FormatConverter_GetSize(IWICFormatConverter
*iface
,
857 UINT
*puiWidth
, UINT
*puiHeight
)
859 FormatConverter
*This
= impl_from_IWICFormatConverter(iface
);
861 TRACE("(%p,%p,%p)\n", iface
, puiWidth
, puiHeight
);
864 return IWICBitmapSource_GetSize(This
->source
, puiWidth
, puiHeight
);
866 return WINCODEC_ERR_NOTINITIALIZED
;
869 static HRESULT WINAPI
FormatConverter_GetPixelFormat(IWICFormatConverter
*iface
,
870 WICPixelFormatGUID
*pPixelFormat
)
872 FormatConverter
*This
= impl_from_IWICFormatConverter(iface
);
874 TRACE("(%p,%p): stub\n", iface
, pPixelFormat
);
877 memcpy(pPixelFormat
, This
->dst_format
->guid
, sizeof(GUID
));
879 return WINCODEC_ERR_NOTINITIALIZED
;
884 static HRESULT WINAPI
FormatConverter_GetResolution(IWICFormatConverter
*iface
,
885 double *pDpiX
, double *pDpiY
)
887 FormatConverter
*This
= impl_from_IWICFormatConverter(iface
);
889 TRACE("(%p,%p,%p): stub\n", iface
, pDpiX
, pDpiY
);
892 return IWICBitmapSource_GetResolution(This
->source
, pDpiX
, pDpiY
);
894 return WINCODEC_ERR_NOTINITIALIZED
;
897 static HRESULT WINAPI
FormatConverter_CopyPalette(IWICFormatConverter
*iface
,
898 IWICPalette
*pIPalette
)
900 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
904 static HRESULT WINAPI
FormatConverter_CopyPixels(IWICFormatConverter
*iface
,
905 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
907 FormatConverter
*This
= impl_from_IWICFormatConverter(iface
);
910 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
917 hr
= IWICBitmapSource_GetSize(This
->source
, &width
, &height
);
918 if (FAILED(hr
)) return hr
;
926 return This
->dst_format
->copy_function(This
, prc
, cbStride
, cbBufferSize
,
927 pbBuffer
, This
->src_format
->format
);
930 return WINCODEC_ERR_NOTINITIALIZED
;
933 static HRESULT WINAPI
FormatConverter_Initialize(IWICFormatConverter
*iface
,
934 IWICBitmapSource
*pISource
, REFWICPixelFormatGUID dstFormat
, WICBitmapDitherType dither
,
935 IWICPalette
*pIPalette
, double alphaThresholdPercent
, WICBitmapPaletteType paletteTranslate
)
937 FormatConverter
*This
= impl_from_IWICFormatConverter(iface
);
938 const struct pixelformatinfo
*srcinfo
, *dstinfo
;
943 TRACE("(%p,%p,%s,%u,%p,%0.1f,%u)\n", iface
, pISource
, debugstr_guid(dstFormat
),
944 dither
, pIPalette
, alphaThresholdPercent
, paletteTranslate
);
946 if (pIPalette
&& !fixme
++) FIXME("ignoring palette\n");
948 EnterCriticalSection(&This
->lock
);
952 res
= WINCODEC_ERR_WRONGSTATE
;
956 res
= IWICBitmapSource_GetPixelFormat(pISource
, &srcFormat
);
957 if (FAILED(res
)) goto end
;
959 srcinfo
= get_formatinfo(&srcFormat
);
962 res
= WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT
;
966 dstinfo
= get_formatinfo(dstFormat
);
969 res
= WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT
;
973 if (dstinfo
->copy_function
)
975 IWICBitmapSource_AddRef(pISource
);
976 This
->src_format
= srcinfo
;
977 This
->dst_format
= dstinfo
;
978 This
->dither
= dither
;
979 This
->alpha_threshold
= alphaThresholdPercent
;
980 This
->palette_type
= paletteTranslate
;
981 This
->source
= pISource
;
984 res
= WINCODEC_ERR_UNSUPPORTEDOPERATION
;
988 LeaveCriticalSection(&This
->lock
);
993 static HRESULT WINAPI
FormatConverter_CanConvert(IWICFormatConverter
*iface
,
994 REFWICPixelFormatGUID srcPixelFormat
, REFWICPixelFormatGUID dstPixelFormat
,
997 FormatConverter
*This
= impl_from_IWICFormatConverter(iface
);
998 const struct pixelformatinfo
*srcinfo
, *dstinfo
;
1000 TRACE("(%p,%s,%s,%p)\n", iface
, debugstr_guid(srcPixelFormat
),
1001 debugstr_guid(dstPixelFormat
), pfCanConvert
);
1003 srcinfo
= get_formatinfo(srcPixelFormat
);
1004 if (!srcinfo
) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT
;
1006 dstinfo
= get_formatinfo(dstPixelFormat
);
1007 if (!dstinfo
) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT
;
1009 if (dstinfo
->copy_function
&&
1010 SUCCEEDED(dstinfo
->copy_function(This
, NULL
, 0, 0, NULL
, dstinfo
->format
)))
1011 *pfCanConvert
= TRUE
;
1013 *pfCanConvert
= FALSE
;
1018 static const IWICFormatConverterVtbl FormatConverter_Vtbl
= {
1019 FormatConverter_QueryInterface
,
1020 FormatConverter_AddRef
,
1021 FormatConverter_Release
,
1022 FormatConverter_GetSize
,
1023 FormatConverter_GetPixelFormat
,
1024 FormatConverter_GetResolution
,
1025 FormatConverter_CopyPalette
,
1026 FormatConverter_CopyPixels
,
1027 FormatConverter_Initialize
,
1028 FormatConverter_CanConvert
1031 HRESULT
FormatConverter_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1033 FormatConverter
*This
;
1036 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
1040 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
1042 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(FormatConverter
));
1043 if (!This
) return E_OUTOFMEMORY
;
1045 This
->IWICFormatConverter_iface
.lpVtbl
= &FormatConverter_Vtbl
;
1047 This
->source
= NULL
;
1048 InitializeCriticalSection(&This
->lock
);
1049 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FormatConverter.lock");
1051 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
1052 IUnknown_Release((IUnknown
*)This
);