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