msvcrt: Added ___mb_cur_max_l_func implementation.
[wine] / dlls / windowscodecs / converter.c
1 /*
2  * Copyright 2009 Vincent Povirk
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
28 #include "wincodec.h"
29
30 #include "wincodecs_private.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
35
36 struct FormatConverter;
37
38 enum pixelformat {
39     format_1bppIndexed,
40     format_2bppIndexed,
41     format_4bppIndexed,
42     format_8bppIndexed,
43     format_BlackWhite,
44     format_2bppGray,
45     format_4bppGray,
46     format_8bppGray,
47     format_16bppGray,
48     format_16bppBGR555,
49     format_16bppBGR565,
50     format_24bppBGR,
51     format_32bppBGR,
52     format_32bppBGRA,
53     format_48bppRGB,
54     format_64bppRGBA,
55 };
56
57 typedef HRESULT (*copyfunc)(struct FormatConverter *This, const WICRect *prc,
58     UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format);
59
60 struct pixelformatinfo {
61     enum pixelformat format;
62     const WICPixelFormatGUID *guid;
63     copyfunc copy_function;
64 };
65
66 typedef struct FormatConverter {
67     const IWICFormatConverterVtbl *lpVtbl;
68     LONG ref;
69     IWICBitmapSource *source;
70     const struct pixelformatinfo *dst_format, *src_format;
71     WICBitmapDitherType dither;
72     double alpha_threshold;
73     WICBitmapPaletteType palette_type;
74     CRITICAL_SECTION lock; /* must be held when initialized */
75 } FormatConverter;
76
77 static void make_grayscale_palette(WICColor *colors, UINT num_colors)
78 {
79     int i, v;
80     for (i=0; i<num_colors; i++)
81     {
82         v = i * 255 / (num_colors-1);
83         colors[i] = 0xff000000 | v<<16 | v<<8 | v;
84     }
85 }
86
87 static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRect *prc,
88     UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
89 {
90     switch (source_format)
91     {
92     case format_1bppIndexed:
93     case format_BlackWhite:
94         if (prc)
95         {
96             HRESULT res;
97             UINT x, y;
98             BYTE *srcdata;
99             UINT srcstride, srcdatasize;
100             const BYTE *srcrow;
101             const BYTE *srcbyte;
102             BYTE *dstrow;
103             DWORD *dstpixel;
104             WICColor colors[2];
105             IWICPalette *palette;
106             UINT actualcolors;
107
108             if (source_format == format_1bppIndexed)
109             {
110                 res = PaletteImpl_Create(&palette);
111                 if (FAILED(res)) return res;
112
113                 res = IWICBitmapSource_CopyPalette(This->source, palette);
114                 if (SUCCEEDED(res))
115                     res = IWICPalette_GetColors(palette, 2, colors, &actualcolors);
116
117                 IWICPalette_Release(palette);
118
119                 if (FAILED(res)) return res;
120             }
121             else
122             {
123                 colors[0] = 0xff000000;
124                 colors[1] = 0xffffffff;
125             }
126
127             srcstride = (prc->Width+7)/8;
128             srcdatasize = srcstride * prc->Height;
129
130             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
131             if (!srcdata) return E_OUTOFMEMORY;
132
133             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
134
135             if (SUCCEEDED(res))
136             {
137                 srcrow = srcdata;
138                 dstrow = pbBuffer;
139                 for (y=0; y<prc->Height; y++) {
140                     srcbyte=(const BYTE*)srcrow;
141                     dstpixel=(DWORD*)dstrow;
142                     for (x=0; x<prc->Width; x+=8) {
143                         BYTE srcval;
144                         srcval=*srcbyte++;
145                         *dstpixel++ = colors[srcval>>7&1];
146                         if (x+1 < prc->Width) *dstpixel++ = colors[srcval>>6&1];
147                         if (x+2 < prc->Width) *dstpixel++ = colors[srcval>>5&1];
148                         if (x+3 < prc->Width) *dstpixel++ = colors[srcval>>4&1];
149                         if (x+4 < prc->Width) *dstpixel++ = colors[srcval>>3&1];
150                         if (x+5 < prc->Width) *dstpixel++ = colors[srcval>>2&1];
151                         if (x+6 < prc->Width) *dstpixel++ = colors[srcval>>1&1];
152                         if (x+7 < prc->Width) *dstpixel++ = colors[srcval&1];
153                     }
154                     srcrow += srcstride;
155                     dstrow += cbStride;
156                 }
157             }
158
159             HeapFree(GetProcessHeap(), 0, srcdata);
160
161             return res;
162         }
163         return S_OK;
164     case format_2bppIndexed:
165     case format_2bppGray:
166         if (prc)
167         {
168             HRESULT res;
169             UINT x, y;
170             BYTE *srcdata;
171             UINT srcstride, srcdatasize;
172             const BYTE *srcrow;
173             const BYTE *srcbyte;
174             BYTE *dstrow;
175             DWORD *dstpixel;
176             WICColor colors[4];
177             IWICPalette *palette;
178             UINT actualcolors;
179
180             if (source_format == format_2bppIndexed)
181             {
182                 res = PaletteImpl_Create(&palette);
183                 if (FAILED(res)) return res;
184
185                 res = IWICBitmapSource_CopyPalette(This->source, palette);
186                 if (SUCCEEDED(res))
187                     res = IWICPalette_GetColors(palette, 4, colors, &actualcolors);
188
189                 IWICPalette_Release(palette);
190
191                 if (FAILED(res)) return res;
192             }
193             else
194                 make_grayscale_palette(colors, 4);
195
196             srcstride = (prc->Width+3)/4;
197             srcdatasize = srcstride * prc->Height;
198
199             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
200             if (!srcdata) return E_OUTOFMEMORY;
201
202             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
203
204             if (SUCCEEDED(res))
205             {
206                 srcrow = srcdata;
207                 dstrow = pbBuffer;
208                 for (y=0; y<prc->Height; y++) {
209                     srcbyte=(const BYTE*)srcrow;
210                     dstpixel=(DWORD*)dstrow;
211                     for (x=0; x<prc->Width; x+=4) {
212                         BYTE srcval;
213                         srcval=*srcbyte++;
214                         *dstpixel++ = colors[srcval>>6];
215                         if (x+1 < prc->Width) *dstpixel++ = colors[srcval>>4&0x3];
216                         if (x+2 < prc->Width) *dstpixel++ = colors[srcval>>2&0x3];
217                         if (x+1 < prc->Width) *dstpixel++ = colors[srcval&0x3];
218                     }
219                     srcrow += srcstride;
220                     dstrow += cbStride;
221                 }
222             }
223
224             HeapFree(GetProcessHeap(), 0, srcdata);
225
226             return res;
227         }
228         return S_OK;
229     case format_4bppIndexed:
230     case format_4bppGray:
231         if (prc)
232         {
233             HRESULT res;
234             UINT x, y;
235             BYTE *srcdata;
236             UINT srcstride, srcdatasize;
237             const BYTE *srcrow;
238             const BYTE *srcbyte;
239             BYTE *dstrow;
240             DWORD *dstpixel;
241             WICColor colors[16];
242             IWICPalette *palette;
243             UINT actualcolors;
244
245             if (source_format == format_4bppIndexed)
246             {
247                 res = PaletteImpl_Create(&palette);
248                 if (FAILED(res)) return res;
249
250                 res = IWICBitmapSource_CopyPalette(This->source, palette);
251                 if (SUCCEEDED(res))
252                     res = IWICPalette_GetColors(palette, 16, colors, &actualcolors);
253
254                 IWICPalette_Release(palette);
255
256                 if (FAILED(res)) return res;
257             }
258             else
259                 make_grayscale_palette(colors, 16);
260
261             srcstride = (prc->Width+1)/2;
262             srcdatasize = srcstride * prc->Height;
263
264             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
265             if (!srcdata) return E_OUTOFMEMORY;
266
267             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
268
269             if (SUCCEEDED(res))
270             {
271                 srcrow = srcdata;
272                 dstrow = pbBuffer;
273                 for (y=0; y<prc->Height; y++) {
274                     srcbyte=(const BYTE*)srcrow;
275                     dstpixel=(DWORD*)dstrow;
276                     for (x=0; x<prc->Width; x+=2) {
277                         BYTE srcval;
278                         srcval=*srcbyte++;
279                         *dstpixel++ = colors[srcval>>4];
280                         if (x+1 < prc->Width) *dstpixel++ = colors[srcval&0xf];
281                     }
282                     srcrow += srcstride;
283                     dstrow += cbStride;
284                 }
285             }
286
287             HeapFree(GetProcessHeap(), 0, srcdata);
288
289             return res;
290         }
291         return S_OK;
292     case format_8bppGray:
293         if (prc)
294         {
295             HRESULT res;
296             UINT x, y;
297             BYTE *srcdata;
298             UINT srcstride, srcdatasize;
299             const BYTE *srcrow;
300             const BYTE *srcbyte;
301             BYTE *dstrow;
302             DWORD *dstpixel;
303
304             srcstride = prc->Width;
305             srcdatasize = srcstride * prc->Height;
306
307             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
308             if (!srcdata) return E_OUTOFMEMORY;
309
310             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
311
312             if (SUCCEEDED(res))
313             {
314                 srcrow = srcdata;
315                 dstrow = pbBuffer;
316                 for (y=0; y<prc->Height; y++) {
317                     srcbyte=(const BYTE*)srcrow;
318                     dstpixel=(DWORD*)dstrow;
319                     for (x=0; x<prc->Width; x++)
320                     {
321                         *dstpixel++ = 0xff000000|(*srcbyte<<16)|(*srcbyte<<8)|*srcbyte;
322                         srcbyte++;
323                     }
324                     srcrow += srcstride;
325                     dstrow += cbStride;
326                 }
327             }
328
329             HeapFree(GetProcessHeap(), 0, srcdata);
330
331             return res;
332         }
333         return S_OK;
334     case format_8bppIndexed:
335         if (prc)
336         {
337             HRESULT res;
338             UINT x, y;
339             BYTE *srcdata;
340             UINT srcstride, srcdatasize;
341             const BYTE *srcrow;
342             const BYTE *srcbyte;
343             BYTE *dstrow;
344             DWORD *dstpixel;
345             WICColor colors[256];
346             IWICPalette *palette;
347             UINT actualcolors;
348
349             res = PaletteImpl_Create(&palette);
350             if (FAILED(res)) return res;
351
352             res = IWICBitmapSource_CopyPalette(This->source, palette);
353             if (SUCCEEDED(res))
354                 res = IWICPalette_GetColors(palette, 256, colors, &actualcolors);
355
356             IWICPalette_Release(palette);
357
358             if (FAILED(res)) return res;
359
360             srcstride = prc->Width;
361             srcdatasize = srcstride * prc->Height;
362
363             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
364             if (!srcdata) return E_OUTOFMEMORY;
365
366             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
367
368             if (SUCCEEDED(res))
369             {
370                 srcrow = srcdata;
371                 dstrow = pbBuffer;
372                 for (y=0; y<prc->Height; y++) {
373                     srcbyte=(const BYTE*)srcrow;
374                     dstpixel=(DWORD*)dstrow;
375                     for (x=0; x<prc->Width; x++)
376                         *dstpixel++ = colors[*srcbyte++];
377                     srcrow += srcstride;
378                     dstrow += cbStride;
379                 }
380             }
381
382             HeapFree(GetProcessHeap(), 0, srcdata);
383
384             return res;
385         }
386         return S_OK;
387     case format_16bppGray:
388         if (prc)
389         {
390             HRESULT res;
391             UINT x, y;
392             BYTE *srcdata;
393             UINT srcstride, srcdatasize;
394             const BYTE *srcrow;
395             const BYTE *srcbyte;
396             BYTE *dstrow;
397             DWORD *dstpixel;
398
399             srcstride = prc->Width * 2;
400             srcdatasize = srcstride * prc->Height;
401
402             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
403             if (!srcdata) return E_OUTOFMEMORY;
404
405             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
406
407             if (SUCCEEDED(res))
408             {
409                 srcrow = srcdata;
410                 dstrow = pbBuffer;
411                 for (y=0; y<prc->Height; y++) {
412                     srcbyte=(const BYTE*)srcrow;
413                     dstpixel=(DWORD*)dstrow;
414                     for (x=0; x<prc->Width; x++)
415                     {
416                         *dstpixel++ = 0xff000000|(*srcbyte<<16)|(*srcbyte<<8)|*srcbyte;
417                         srcbyte+=2;
418                     }
419                     srcrow += srcstride;
420                     dstrow += cbStride;
421                 }
422             }
423
424             HeapFree(GetProcessHeap(), 0, srcdata);
425
426             return res;
427         }
428         return S_OK;
429     case format_16bppBGR555:
430         if (prc)
431         {
432             HRESULT res;
433             UINT x, y;
434             BYTE *srcdata;
435             UINT srcstride, srcdatasize;
436             const BYTE *srcrow;
437             const WORD *srcpixel;
438             BYTE *dstrow;
439             DWORD *dstpixel;
440
441             srcstride = 2 * prc->Width;
442             srcdatasize = srcstride * prc->Height;
443
444             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
445             if (!srcdata) return E_OUTOFMEMORY;
446
447             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
448
449             if (SUCCEEDED(res))
450             {
451                 srcrow = srcdata;
452                 dstrow = pbBuffer;
453                 for (y=0; y<prc->Height; y++) {
454                     srcpixel=(const WORD*)srcrow;
455                     dstpixel=(DWORD*)dstrow;
456                     for (x=0; x<prc->Width; x++) {
457                         WORD srcval;
458                         srcval=*srcpixel++;
459                         *dstpixel++=0xff000000 | /* constant 255 alpha */
460                                     ((srcval << 9) & 0xf80000) | /* r */
461                                     ((srcval << 4) & 0x070000) | /* r - 3 bits */
462                                     ((srcval << 6) & 0x00f800) | /* g */
463                                     ((srcval << 1) & 0x000700) | /* g - 3 bits */
464                                     ((srcval << 3) & 0x0000f8) | /* b */
465                                     ((srcval >> 2) & 0x000007);  /* b - 3 bits */
466                     }
467                     srcrow += srcstride;
468                     dstrow += cbStride;
469                 }
470             }
471
472             HeapFree(GetProcessHeap(), 0, srcdata);
473
474             return res;
475         }
476         return S_OK;
477     case format_16bppBGR565:
478         if (prc)
479         {
480             HRESULT res;
481             UINT x, y;
482             BYTE *srcdata;
483             UINT srcstride, srcdatasize;
484             const BYTE *srcrow;
485             const WORD *srcpixel;
486             BYTE *dstrow;
487             DWORD *dstpixel;
488
489             srcstride = 2 * prc->Width;
490             srcdatasize = srcstride * prc->Height;
491
492             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
493             if (!srcdata) return E_OUTOFMEMORY;
494
495             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
496
497             if (SUCCEEDED(res))
498             {
499                 srcrow = srcdata;
500                 dstrow = pbBuffer;
501                 for (y=0; y<prc->Height; y++) {
502                     srcpixel=(const WORD*)srcrow;
503                     dstpixel=(DWORD*)dstrow;
504                     for (x=0; x<prc->Width; x++) {
505                         WORD srcval;
506                         srcval=*srcpixel++;
507                         *dstpixel++=0xff000000 | /* constant 255 alpha */
508                                     ((srcval << 8) & 0xf80000) | /* r */
509                                     ((srcval << 3) & 0x070000) | /* r - 3 bits */
510                                     ((srcval << 5) & 0x00fc00) | /* g */
511                                     ((srcval >> 1) & 0x000300) | /* g - 2 bits */
512                                     ((srcval << 3) & 0x0000f8) | /* b */
513                                     ((srcval >> 2) & 0x000007);  /* b - 3 bits */
514                     }
515                     srcrow += srcstride;
516                     dstrow += cbStride;
517                 }
518             }
519
520             HeapFree(GetProcessHeap(), 0, srcdata);
521
522             return res;
523         }
524         return S_OK;
525     case format_24bppBGR:
526         if (prc)
527         {
528             HRESULT res;
529             UINT x, y;
530             BYTE *srcdata;
531             UINT srcstride, srcdatasize;
532             const BYTE *srcrow;
533             const BYTE *srcpixel;
534             BYTE *dstrow;
535             BYTE *dstpixel;
536
537             srcstride = 3 * prc->Width;
538             srcdatasize = srcstride * prc->Height;
539
540             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
541             if (!srcdata) return E_OUTOFMEMORY;
542
543             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
544
545             if (SUCCEEDED(res))
546             {
547                 srcrow = srcdata;
548                 dstrow = pbBuffer;
549                 for (y=0; y<prc->Height; y++) {
550                     srcpixel=srcrow;
551                     dstpixel=dstrow;
552                     for (x=0; x<prc->Width; x++) {
553                         *dstpixel++=*srcpixel++; /* blue */
554                         *dstpixel++=*srcpixel++; /* green */
555                         *dstpixel++=*srcpixel++; /* red */
556                         *dstpixel++=255; /* alpha */
557                     }
558                     srcrow += srcstride;
559                     dstrow += cbStride;
560                 }
561             }
562
563             HeapFree(GetProcessHeap(), 0, srcdata);
564
565             return res;
566         }
567         return S_OK;
568     case format_32bppBGR:
569         if (prc)
570         {
571             HRESULT res;
572             UINT x, y;
573
574             res = IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
575             if (FAILED(res)) return res;
576
577             /* set all alpha values to 255 */
578             for (y=0; y<prc->Height; y++)
579                 for (x=0; x<prc->Width; x++)
580                     pbBuffer[cbStride*y+4*x+3] = 0xff;
581         }
582         return S_OK;
583     case format_32bppBGRA:
584         if (prc)
585             return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
586         return S_OK;
587     case format_48bppRGB:
588         if (prc)
589         {
590             HRESULT res;
591             UINT x, y;
592             BYTE *srcdata;
593             UINT srcstride, srcdatasize;
594             const BYTE *srcrow;
595             const BYTE *srcpixel;
596             BYTE *dstrow;
597             DWORD *dstpixel;
598
599             srcstride = 6 * prc->Width;
600             srcdatasize = srcstride * prc->Height;
601
602             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
603             if (!srcdata) return E_OUTOFMEMORY;
604
605             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
606
607             if (SUCCEEDED(res))
608             {
609                 srcrow = srcdata;
610                 dstrow = pbBuffer;
611                 for (y=0; y<prc->Height; y++) {
612                     srcpixel=srcrow;
613                     dstpixel=(DWORD*)dstrow;
614                     for (x=0; x<prc->Width; x++) {
615                         BYTE red, green, blue;
616                         red = *srcpixel++; srcpixel++;
617                         green = *srcpixel++; srcpixel++;
618                         blue = *srcpixel++; srcpixel++;
619                         *dstpixel++=0xff000000|red<<16|green<<8|blue;
620                     }
621                     srcrow += srcstride;
622                     dstrow += cbStride;
623                 }
624             }
625
626             HeapFree(GetProcessHeap(), 0, srcdata);
627
628             return res;
629         }
630         return S_OK;
631     case format_64bppRGBA:
632         if (prc)
633         {
634             HRESULT res;
635             UINT x, y;
636             BYTE *srcdata;
637             UINT srcstride, srcdatasize;
638             const BYTE *srcrow;
639             const BYTE *srcpixel;
640             BYTE *dstrow;
641             DWORD *dstpixel;
642
643             srcstride = 8 * prc->Width;
644             srcdatasize = srcstride * prc->Height;
645
646             srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
647             if (!srcdata) return E_OUTOFMEMORY;
648
649             res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
650
651             if (SUCCEEDED(res))
652             {
653                 srcrow = srcdata;
654                 dstrow = pbBuffer;
655                 for (y=0; y<prc->Height; y++) {
656                     srcpixel=srcrow;
657                     dstpixel=(DWORD*)dstrow;
658                     for (x=0; x<prc->Width; x++) {
659                         BYTE red, green, blue, alpha;
660                         red = *srcpixel++; srcpixel++;
661                         green = *srcpixel++; srcpixel++;
662                         blue = *srcpixel++; srcpixel++;
663                         alpha = *srcpixel++; srcpixel++;
664                         *dstpixel++=alpha<<24|red<<16|green<<8|blue;
665                     }
666                     srcrow += srcstride;
667                     dstrow += cbStride;
668                 }
669             }
670
671             HeapFree(GetProcessHeap(), 0, srcdata);
672
673             return res;
674         }
675         return S_OK;
676     default:
677         return WINCODEC_ERR_UNSUPPORTEDOPERATION;
678     }
679 }
680
681 static HRESULT copypixels_to_32bppBGR(struct FormatConverter *This, const WICRect *prc,
682     UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
683 {
684     switch (source_format)
685     {
686     case format_32bppBGR:
687     case format_32bppBGRA:
688         if (prc)
689             return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
690         return S_OK;
691     default:
692         return copypixels_to_32bppBGRA(This, prc, cbStride, cbBufferSize, pbBuffer, source_format);
693     }
694 }
695
696 static const struct pixelformatinfo supported_formats[] = {
697     {format_1bppIndexed, &GUID_WICPixelFormat1bppIndexed, NULL},
698     {format_2bppIndexed, &GUID_WICPixelFormat2bppIndexed, NULL},
699     {format_4bppIndexed, &GUID_WICPixelFormat4bppIndexed, NULL},
700     {format_8bppIndexed, &GUID_WICPixelFormat8bppIndexed, NULL},
701     {format_BlackWhite, &GUID_WICPixelFormatBlackWhite, NULL},
702     {format_2bppGray, &GUID_WICPixelFormat2bppGray, NULL},
703     {format_4bppGray, &GUID_WICPixelFormat4bppGray, NULL},
704     {format_8bppGray, &GUID_WICPixelFormat8bppGray, NULL},
705     {format_16bppGray, &GUID_WICPixelFormat16bppGray, NULL},
706     {format_16bppBGR555, &GUID_WICPixelFormat16bppBGR555, NULL},
707     {format_16bppBGR565, &GUID_WICPixelFormat16bppBGR565, NULL},
708     {format_24bppBGR, &GUID_WICPixelFormat24bppBGR, NULL},
709     {format_32bppBGR, &GUID_WICPixelFormat32bppBGR, copypixels_to_32bppBGR},
710     {format_32bppBGRA, &GUID_WICPixelFormat32bppBGRA, copypixels_to_32bppBGRA},
711     {format_48bppRGB, &GUID_WICPixelFormat48bppRGB, NULL},
712     {format_64bppRGBA, &GUID_WICPixelFormat64bppRGBA, NULL},
713     {0}
714 };
715
716 static const struct pixelformatinfo *get_formatinfo(const WICPixelFormatGUID *format)
717 {
718     UINT i;
719
720     for (i=0; supported_formats[i].guid; i++)
721         if (IsEqualGUID(supported_formats[i].guid, format)) return &supported_formats[i];
722
723     return NULL;
724 }
725
726 static HRESULT WINAPI FormatConverter_QueryInterface(IWICFormatConverter *iface, REFIID iid,
727     void **ppv)
728 {
729     FormatConverter *This = (FormatConverter*)iface;
730     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
731
732     if (!ppv) return E_INVALIDARG;
733
734     if (IsEqualIID(&IID_IUnknown, iid) ||
735         IsEqualIID(&IID_IWICBitmapSource, iid) ||
736         IsEqualIID(&IID_IWICFormatConverter, iid))
737     {
738         *ppv = This;
739     }
740     else
741     {
742         *ppv = NULL;
743         return E_NOINTERFACE;
744     }
745
746     IUnknown_AddRef((IUnknown*)*ppv);
747     return S_OK;
748 }
749
750 static ULONG WINAPI FormatConverter_AddRef(IWICFormatConverter *iface)
751 {
752     FormatConverter *This = (FormatConverter*)iface;
753     ULONG ref = InterlockedIncrement(&This->ref);
754
755     TRACE("(%p) refcount=%u\n", iface, ref);
756
757     return ref;
758 }
759
760 static ULONG WINAPI FormatConverter_Release(IWICFormatConverter *iface)
761 {
762     FormatConverter *This = (FormatConverter*)iface;
763     ULONG ref = InterlockedDecrement(&This->ref);
764
765     TRACE("(%p) refcount=%u\n", iface, ref);
766
767     if (ref == 0)
768     {
769         This->lock.DebugInfo->Spare[0] = 0;
770         DeleteCriticalSection(&This->lock);
771         if (This->source) IWICBitmapSource_Release(This->source);
772         HeapFree(GetProcessHeap(), 0, This);
773     }
774
775     return ref;
776 }
777
778 static HRESULT WINAPI FormatConverter_GetSize(IWICFormatConverter *iface,
779     UINT *puiWidth, UINT *puiHeight)
780 {
781     FormatConverter *This = (FormatConverter*)iface;
782
783     TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
784
785     if (This->source)
786         return IWICBitmapSource_GetSize(This->source, puiWidth, puiHeight);
787     else
788         return WINCODEC_ERR_NOTINITIALIZED;
789 }
790
791 static HRESULT WINAPI FormatConverter_GetPixelFormat(IWICFormatConverter *iface,
792     WICPixelFormatGUID *pPixelFormat)
793 {
794     FormatConverter *This = (FormatConverter*)iface;
795
796     TRACE("(%p,%p): stub\n", iface, pPixelFormat);
797
798     if (This->source)
799         memcpy(pPixelFormat, This->dst_format->guid, sizeof(GUID));
800     else
801         return WINCODEC_ERR_NOTINITIALIZED;
802
803     return S_OK;
804 }
805
806 static HRESULT WINAPI FormatConverter_GetResolution(IWICFormatConverter *iface,
807     double *pDpiX, double *pDpiY)
808 {
809     FormatConverter *This = (FormatConverter*)iface;
810
811     TRACE("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
812
813     if (This->source)
814         return IWICBitmapSource_GetResolution(This->source, pDpiX, pDpiY);
815     else
816         return WINCODEC_ERR_NOTINITIALIZED;
817 }
818
819 static HRESULT WINAPI FormatConverter_CopyPalette(IWICFormatConverter *iface,
820     IWICPalette *pIPalette)
821 {
822     FIXME("(%p,%p): stub\n", iface, pIPalette);
823     return E_NOTIMPL;
824 }
825
826 static HRESULT WINAPI FormatConverter_CopyPixels(IWICFormatConverter *iface,
827     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
828 {
829     FormatConverter *This = (FormatConverter*)iface;
830     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
831
832     if (This->source)
833         return This->dst_format->copy_function(This, prc, cbStride, cbBufferSize,
834             pbBuffer, This->src_format->format);
835     else
836         return WINCODEC_ERR_NOTINITIALIZED;
837 }
838
839 static HRESULT WINAPI FormatConverter_Initialize(IWICFormatConverter *iface,
840     IWICBitmapSource *pISource, REFWICPixelFormatGUID dstFormat, WICBitmapDitherType dither,
841     IWICPalette *pIPalette, double alphaThresholdPercent, WICBitmapPaletteType paletteTranslate)
842 {
843     FormatConverter *This = (FormatConverter*)iface;
844     const struct pixelformatinfo *srcinfo, *dstinfo;
845     static INT fixme=0;
846     GUID srcFormat;
847     HRESULT res=S_OK;
848
849     TRACE("(%p,%p,%s,%u,%p,%0.1f,%u)\n", iface, pISource, debugstr_guid(dstFormat),
850         dither, pIPalette, alphaThresholdPercent, paletteTranslate);
851
852     if (pIPalette && !fixme++) FIXME("ignoring palette\n");
853
854     EnterCriticalSection(&This->lock);
855
856     if (This->source)
857     {
858         res = WINCODEC_ERR_WRONGSTATE;
859         goto end;
860     }
861
862     res = IWICBitmapSource_GetPixelFormat(pISource, &srcFormat);
863     if (FAILED(res)) goto end;
864
865     srcinfo = get_formatinfo(&srcFormat);
866     if (!srcinfo)
867     {
868         res = WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
869         goto end;
870     }
871
872     dstinfo = get_formatinfo(dstFormat);
873     if (!dstinfo)
874     {
875         res = WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
876         goto end;
877     }
878
879     if (dstinfo->copy_function)
880     {
881         IWICBitmapSource_AddRef(pISource);
882         This->src_format = srcinfo;
883         This->dst_format = dstinfo;
884         This->dither = dither;
885         This->alpha_threshold = alphaThresholdPercent;
886         This->palette_type = paletteTranslate;
887         This->source = pISource;
888     }
889     else
890         res = WINCODEC_ERR_UNSUPPORTEDOPERATION;
891
892 end:
893
894     LeaveCriticalSection(&This->lock);
895
896     return res;
897 }
898
899 static HRESULT WINAPI FormatConverter_CanConvert(IWICFormatConverter *iface,
900     REFWICPixelFormatGUID srcPixelFormat, REFWICPixelFormatGUID dstPixelFormat,
901     BOOL *pfCanConvert)
902 {
903     FormatConverter *This = (FormatConverter*)iface;
904     const struct pixelformatinfo *srcinfo, *dstinfo;
905
906     TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(srcPixelFormat),
907         debugstr_guid(dstPixelFormat), pfCanConvert);
908
909     srcinfo = get_formatinfo(srcPixelFormat);
910     if (!srcinfo) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
911
912     dstinfo = get_formatinfo(dstPixelFormat);
913     if (!dstinfo) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
914
915     if (dstinfo->copy_function &&
916         SUCCEEDED(dstinfo->copy_function(This, NULL, 0, 0, NULL, dstinfo->format)))
917         *pfCanConvert = TRUE;
918     else
919         *pfCanConvert = FALSE;
920
921     return S_OK;
922 }
923
924 static const IWICFormatConverterVtbl FormatConverter_Vtbl = {
925     FormatConverter_QueryInterface,
926     FormatConverter_AddRef,
927     FormatConverter_Release,
928     FormatConverter_GetSize,
929     FormatConverter_GetPixelFormat,
930     FormatConverter_GetResolution,
931     FormatConverter_CopyPalette,
932     FormatConverter_CopyPixels,
933     FormatConverter_Initialize,
934     FormatConverter_CanConvert
935 };
936
937 HRESULT FormatConverter_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
938 {
939     FormatConverter *This;
940     HRESULT ret;
941
942     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
943
944     *ppv = NULL;
945
946     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
947
948     This = HeapAlloc(GetProcessHeap(), 0, sizeof(FormatConverter));
949     if (!This) return E_OUTOFMEMORY;
950
951     This->lpVtbl = &FormatConverter_Vtbl;
952     This->ref = 1;
953     This->source = NULL;
954     InitializeCriticalSection(&This->lock);
955     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FormatConverter.lock");
956
957     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
958     IUnknown_Release((IUnknown*)This);
959
960     return ret;
961 }