[ADVAPI32_APITEST][NTDLL_APITEST][WINDOWSCODECS] Comment out some unused functions...
[reactos.git] / dll / win32 / windowscodecs / converter.c
index b1c0952..3fca5b3 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright 2009 Vincent Povirk
+ * Copyright 2016 Dmitry Timoshkov
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include "config.h"
-
-#include <stdarg.h>
-
-#define COBJMACROS
-
-#include "windef.h"
-#include "winbase.h"
-#include "objbase.h"
-#include "wincodec.h"
-
 #include "wincodecs_private.h"
 
-#include "wine/debug.h"
-
-WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
+#include <math.h>
 
 struct FormatConverter;
 
@@ -47,11 +35,19 @@ enum pixelformat {
     format_16bppGray,
     format_16bppBGR555,
     format_16bppBGR565,
+    format_16bppBGRA5551,
     format_24bppBGR,
+    format_24bppRGB,
+    format_32bppGrayFloat,
     format_32bppBGR,
+    format_32bppRGB,
     format_32bppBGRA,
+    format_32bppRGBA,
+    format_32bppPBGRA,
+    format_32bppPRGBA,
     format_48bppRGB,
     format_64bppRGBA,
+    format_32bppCMYK,
 };
 
 typedef HRESULT (*copyfunc)(struct FormatConverter *This, const WICRect *prc,
@@ -64,24 +60,70 @@ struct pixelformatinfo {
 };
 
 typedef struct FormatConverter {
-    const IWICFormatConverterVtbl *lpVtbl;
+    IWICFormatConverter IWICFormatConverter_iface;
     LONG ref;
     IWICBitmapSource *source;
     const struct pixelformatinfo *dst_format, *src_format;
     WICBitmapDitherType dither;
     double alpha_threshold;
-    WICBitmapPaletteType palette_type;
+    IWICPalette *palette;
     CRITICAL_SECTION lock; /* must be held when initialized */
 } FormatConverter;
 
-static void make_grayscale_palette(WICColor *colors, UINT num_colors)
+/* https://www.w3.org/Graphics/Color/srgb */
+#ifndef __REACTOS__
+static inline float from_sRGB_component(float f)
 {
-    int i, v;
-    for (i=0; i<num_colors; i++)
-    {
-        v = i * 255 / (num_colors-1);
-        colors[i] = 0xff000000 | v<<16 | v<<8 | v;
-    }
+    if (f <= 0.04045f) return f / 12.92f;
+    return powf((f + 0.055f) / 1.055f, 2.4f);
+}
+#endif
+
+static inline float to_sRGB_component(float f)
+{
+    if (f <= 0.0031308f) return 12.92f * f;
+    return 1.055f * powf(f, 1.0f/2.4f) - 0.055f;
+}
+
+#if 0 /* FIXME: enable once needed */
+static void from_sRGB(BYTE *bgr)
+{
+    float r, g, b;
+
+    r = bgr[2] / 255.0f;
+    g = bgr[1] / 255.0f;
+    b = bgr[0] / 255.0f;
+
+    r = from_sRGB_component(r);
+    g = from_sRGB_component(g);
+    b = from_sRGB_component(b);
+
+    bgr[2] = (BYTE)(r * 255.0f);
+    bgr[1] = (BYTE)(g * 255.0f);
+    bgr[0] = (BYTE)(b * 255.0f);
+}
+
+static void to_sRGB(BYTE *bgr)
+{
+    float r, g, b;
+
+    r = bgr[2] / 255.0f;
+    g = bgr[1] / 255.0f;
+    b = bgr[0] / 255.0f;
+
+    r = to_sRGB_component(r);
+    g = to_sRGB_component(g);
+    b = to_sRGB_component(b);
+
+    bgr[2] = (BYTE)(r * 255.0f);
+    bgr[1] = (BYTE)(g * 255.0f);
+    bgr[0] = (BYTE)(b * 255.0f);
+}
+#endif
+
+static inline FormatConverter *impl_from_IWICFormatConverter(IWICFormatConverter *iface)
+{
+    return CONTAINING_RECORD(iface, FormatConverter, IWICFormatConverter_iface);
 }
 
 static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRect *prc,
@@ -94,7 +136,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -105,24 +147,19 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
             IWICPalette *palette;
             UINT actualcolors;
 
-            if (source_format == format_1bppIndexed)
-            {
-                res = PaletteImpl_Create(&palette);
-                if (FAILED(res)) return res;
+            res = PaletteImpl_Create(&palette);
+            if (FAILED(res)) return res;
 
+            if (source_format == format_1bppIndexed)
                 res = IWICBitmapSource_CopyPalette(This->source, palette);
-                if (SUCCEEDED(res))
-                    res = IWICPalette_GetColors(palette, 2, colors, &actualcolors);
+            else
+                res = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedBW, FALSE);
 
-                IWICPalette_Release(palette);
+            if (SUCCEEDED(res))
+                res = IWICPalette_GetColors(palette, 2, colors, &actualcolors);
 
-                if (FAILED(res)) return res;
-            }
-            else
-            {
-                colors[0] = 0xff000000;
-                colors[1] = 0xffffffff;
-            }
+            IWICPalette_Release(palette);
+            if (FAILED(res)) return res;
 
             srcstride = (prc->Width+7)/8;
             srcdatasize = srcstride * prc->Height;
@@ -137,7 +174,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
                 srcrow = srcdata;
                 dstrow = pbBuffer;
                 for (y=0; y<prc->Height; y++) {
-                    srcbyte=(const BYTE*)srcrow;
+                    srcbyte = srcrow;
                     dstpixel=(DWORD*)dstrow;
                     for (x=0; x<prc->Width; x+=8) {
                         BYTE srcval;
@@ -166,7 +203,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -177,21 +214,19 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
             IWICPalette *palette;
             UINT actualcolors;
 
-            if (source_format == format_2bppIndexed)
-            {
-                res = PaletteImpl_Create(&palette);
-                if (FAILED(res)) return res;
+            res = PaletteImpl_Create(&palette);
+            if (FAILED(res)) return res;
 
+            if (source_format == format_2bppIndexed)
                 res = IWICBitmapSource_CopyPalette(This->source, palette);
-                if (SUCCEEDED(res))
-                    res = IWICPalette_GetColors(palette, 4, colors, &actualcolors);
+            else
+                res = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray4, FALSE);
 
-                IWICPalette_Release(palette);
+            if (SUCCEEDED(res))
+                res = IWICPalette_GetColors(palette, 4, colors, &actualcolors);
 
-                if (FAILED(res)) return res;
-            }
-            else
-                make_grayscale_palette(colors, 4);
+            IWICPalette_Release(palette);
+            if (FAILED(res)) return res;
 
             srcstride = (prc->Width+3)/4;
             srcdatasize = srcstride * prc->Height;
@@ -206,7 +241,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
                 srcrow = srcdata;
                 dstrow = pbBuffer;
                 for (y=0; y<prc->Height; y++) {
-                    srcbyte=(const BYTE*)srcrow;
+                    srcbyte = srcrow;
                     dstpixel=(DWORD*)dstrow;
                     for (x=0; x<prc->Width; x+=4) {
                         BYTE srcval;
@@ -214,7 +249,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
                         *dstpixel++ = colors[srcval>>6];
                         if (x+1 < prc->Width) *dstpixel++ = colors[srcval>>4&0x3];
                         if (x+2 < prc->Width) *dstpixel++ = colors[srcval>>2&0x3];
-                        if (x+1 < prc->Width) *dstpixel++ = colors[srcval&0x3];
+                        if (x+3 < prc->Width) *dstpixel++ = colors[srcval&0x3];
                     }
                     srcrow += srcstride;
                     dstrow += cbStride;
@@ -231,7 +266,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -242,21 +277,19 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
             IWICPalette *palette;
             UINT actualcolors;
 
-            if (source_format == format_4bppIndexed)
-            {
-                res = PaletteImpl_Create(&palette);
-                if (FAILED(res)) return res;
+            res = PaletteImpl_Create(&palette);
+            if (FAILED(res)) return res;
 
+            if (source_format == format_4bppIndexed)
                 res = IWICBitmapSource_CopyPalette(This->source, palette);
-                if (SUCCEEDED(res))
-                    res = IWICPalette_GetColors(palette, 16, colors, &actualcolors);
+            else
+                res = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray16, FALSE);
 
-                IWICPalette_Release(palette);
+            if (SUCCEEDED(res))
+                res = IWICPalette_GetColors(palette, 16, colors, &actualcolors);
 
-                if (FAILED(res)) return res;
-            }
-            else
-                make_grayscale_palette(colors, 16);
+            IWICPalette_Release(palette);
+            if (FAILED(res)) return res;
 
             srcstride = (prc->Width+1)/2;
             srcdatasize = srcstride * prc->Height;
@@ -271,7 +304,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
                 srcrow = srcdata;
                 dstrow = pbBuffer;
                 for (y=0; y<prc->Height; y++) {
-                    srcbyte=(const BYTE*)srcrow;
+                    srcbyte = srcrow;
                     dstpixel=(DWORD*)dstrow;
                     for (x=0; x<prc->Width; x+=2) {
                         BYTE srcval;
@@ -293,7 +326,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -314,7 +347,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
                 srcrow = srcdata;
                 dstrow = pbBuffer;
                 for (y=0; y<prc->Height; y++) {
-                    srcbyte=(const BYTE*)srcrow;
+                    srcbyte = srcrow;
                     dstpixel=(DWORD*)dstrow;
                     for (x=0; x<prc->Width; x++)
                     {
@@ -335,7 +368,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -370,7 +403,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
                 srcrow = srcdata;
                 dstrow = pbBuffer;
                 for (y=0; y<prc->Height; y++) {
-                    srcbyte=(const BYTE*)srcrow;
+                    srcbyte = srcrow;
                     dstpixel=(DWORD*)dstrow;
                     for (x=0; x<prc->Width; x++)
                         *dstpixel++ = colors[*srcbyte++];
@@ -388,7 +421,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -409,7 +442,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
                 srcrow = srcdata;
                 dstrow = pbBuffer;
                 for (y=0; y<prc->Height; y++) {
-                    srcbyte=(const BYTE*)srcrow;
+                    srcbyte = srcrow;
                     dstpixel=(DWORD*)dstrow;
                     for (x=0; x<prc->Width; x++)
                     {
@@ -430,7 +463,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -478,7 +511,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -522,11 +555,59 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
             return res;
         }
         return S_OK;
+    case format_16bppBGRA5551:
+        if (prc)
+        {
+            HRESULT res;
+            INT x, y;
+            BYTE *srcdata;
+            UINT srcstride, srcdatasize;
+            const BYTE *srcrow;
+            const WORD *srcpixel;
+            BYTE *dstrow;
+            DWORD *dstpixel;
+
+            srcstride = 2 * prc->Width;
+            srcdatasize = srcstride * prc->Height;
+
+            srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
+            if (!srcdata) return E_OUTOFMEMORY;
+
+            res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
+
+            if (SUCCEEDED(res))
+            {
+                srcrow = srcdata;
+                dstrow = pbBuffer;
+                for (y=0; y<prc->Height; y++) {
+                    srcpixel=(const WORD*)srcrow;
+                    dstpixel=(DWORD*)dstrow;
+                    for (x=0; x<prc->Width; x++) {
+                        WORD srcval;
+                        srcval=*srcpixel++;
+                        *dstpixel++=((srcval & 0x8000) ? 0xff000000 : 0) | /* alpha */
+                                    ((srcval << 9) & 0xf80000) | /* r */
+                                    ((srcval << 4) & 0x070000) | /* r - 3 bits */
+                                    ((srcval << 6) & 0x00f800) | /* g */
+                                    ((srcval << 1) & 0x000700) | /* g - 3 bits */
+                                    ((srcval << 3) & 0x0000f8) | /* b */
+                                    ((srcval >> 2) & 0x000007);  /* b - 3 bits */
+                    }
+                    srcrow += srcstride;
+                    dstrow += cbStride;
+                }
+            }
+
+            HeapFree(GetProcessHeap(), 0, srcdata);
+
+            return res;
+        }
+        return S_OK;
     case format_24bppBGR:
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -565,11 +646,59 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
             return res;
         }
         return S_OK;
+    case format_24bppRGB:
+        if (prc)
+        {
+            HRESULT res;
+            INT x, y;
+            BYTE *srcdata;
+            UINT srcstride, srcdatasize;
+            const BYTE *srcrow;
+            const BYTE *srcpixel;
+            BYTE *dstrow;
+            BYTE *dstpixel;
+            BYTE tmppixel[3];
+
+            srcstride = 3 * prc->Width;
+            srcdatasize = srcstride * prc->Height;
+
+            srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
+            if (!srcdata) return E_OUTOFMEMORY;
+
+            res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
+
+            if (SUCCEEDED(res))
+            {
+                srcrow = srcdata;
+                dstrow = pbBuffer;
+                for (y=0; y<prc->Height; y++) {
+                    srcpixel=srcrow;
+                    dstpixel=dstrow;
+                    for (x=0; x<prc->Width; x++) {
+                        tmppixel[0]=*srcpixel++; /* red */
+                        tmppixel[1]=*srcpixel++; /* green */
+                        tmppixel[2]=*srcpixel++; /* blue */
+
+                        *dstpixel++=tmppixel[2]; /* blue */
+                        *dstpixel++=tmppixel[1]; /* green */
+                        *dstpixel++=tmppixel[0]; /* red */
+                        *dstpixel++=255; /* alpha */
+                    }
+                    srcrow += srcstride;
+                    dstrow += cbStride;
+                }
+            }
+
+            HeapFree(GetProcessHeap(), 0, srcdata);
+
+            return res;
+        }
+        return S_OK;
     case format_32bppBGR:
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
 
             res = IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
             if (FAILED(res)) return res;
@@ -584,11 +713,33 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
         if (prc)
             return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
         return S_OK;
+    case format_32bppPBGRA:
+        if (prc)
+        {
+            HRESULT res;
+            INT x, y;
+
+            res = IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+            if (FAILED(res)) return res;
+
+            for (y=0; y<prc->Height; y++)
+                for (x=0; x<prc->Width; x++)
+                {
+                    BYTE alpha = pbBuffer[cbStride*y+4*x+3];
+                    if (alpha != 0 && alpha != 255)
+                    {
+                        pbBuffer[cbStride*y+4*x] = pbBuffer[cbStride*y+4*x] * 255 / alpha;
+                        pbBuffer[cbStride*y+4*x+1] = pbBuffer[cbStride*y+4*x+1] * 255 / alpha;
+                        pbBuffer[cbStride*y+4*x+2] = pbBuffer[cbStride*y+4*x+2] * 255 / alpha;
+                    }
+                }
+        }
+        return S_OK;
     case format_48bppRGB:
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -632,7 +783,7 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
         if (prc)
         {
             HRESULT res;
-            UINT x, y;
+            INT x, y;
             BYTE *srcdata;
             UINT srcstride, srcdatasize;
             const BYTE *srcrow;
@@ -673,11 +824,53 @@ static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRe
             return res;
         }
         return S_OK;
+    case format_32bppCMYK:
+        if (prc)
+        {
+            HRESULT res;
+            UINT x, y;
+
+            res = IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+            if (FAILED(res)) return res;
+
+            for (y=0; y<prc->Height; y++)
+                for (x=0; x<prc->Width; x++)
+                {
+                    BYTE *pixel = pbBuffer+cbStride*y+4*x;
+                    BYTE c=pixel[0], m=pixel[1], y=pixel[2], k=pixel[3];
+                    pixel[0] = (255-y)*(255-k)/255; /* blue */
+                    pixel[1] = (255-m)*(255-k)/255; /* green */
+                    pixel[2] = (255-c)*(255-k)/255; /* red */
+                    pixel[3] = 255; /* alpha */
+                }
+        }
+        return S_OK;
     default:
         return WINCODEC_ERR_UNSUPPORTEDOPERATION;
     }
 }
 
+static HRESULT copypixels_to_32bppRGBA(struct FormatConverter *This, const WICRect *prc,
+    UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
+{
+    HRESULT hr;
+
+    switch (source_format)
+    {
+    case format_32bppRGB:
+    case format_32bppRGBA:
+    case format_32bppPRGBA:
+        if (prc)
+            return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+        return S_OK;
+    default:
+        hr = copypixels_to_32bppBGRA(This, prc, cbStride, cbBufferSize, pbBuffer, source_format);
+        if (SUCCEEDED(hr) && prc)
+              reverse_bgr8(4, pbBuffer, prc->Width, prc->Height, cbStride);
+        return hr;
+    }
+}
+
 static HRESULT copypixels_to_32bppBGR(struct FormatConverter *This, const WICRect *prc,
     UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
 {
@@ -685,6 +878,7 @@ static HRESULT copypixels_to_32bppBGR(struct FormatConverter *This, const WICRec
     {
     case format_32bppBGR:
     case format_32bppBGRA:
+    case format_32bppPBGRA:
         if (prc)
             return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
         return S_OK;
@@ -693,54 +887,582 @@ static HRESULT copypixels_to_32bppBGR(struct FormatConverter *This, const WICRec
     }
 }
 
-static const struct pixelformatinfo supported_formats[] = {
-    {format_1bppIndexed, &GUID_WICPixelFormat1bppIndexed, NULL},
-    {format_2bppIndexed, &GUID_WICPixelFormat2bppIndexed, NULL},
-    {format_4bppIndexed, &GUID_WICPixelFormat4bppIndexed, NULL},
-    {format_8bppIndexed, &GUID_WICPixelFormat8bppIndexed, NULL},
-    {format_BlackWhite, &GUID_WICPixelFormatBlackWhite, NULL},
-    {format_2bppGray, &GUID_WICPixelFormat2bppGray, NULL},
-    {format_4bppGray, &GUID_WICPixelFormat4bppGray, NULL},
-    {format_8bppGray, &GUID_WICPixelFormat8bppGray, NULL},
-    {format_16bppGray, &GUID_WICPixelFormat16bppGray, NULL},
-    {format_16bppBGR555, &GUID_WICPixelFormat16bppBGR555, NULL},
-    {format_16bppBGR565, &GUID_WICPixelFormat16bppBGR565, NULL},
-    {format_24bppBGR, &GUID_WICPixelFormat24bppBGR, NULL},
-    {format_32bppBGR, &GUID_WICPixelFormat32bppBGR, copypixels_to_32bppBGR},
-    {format_32bppBGRA, &GUID_WICPixelFormat32bppBGRA, copypixels_to_32bppBGRA},
-    {format_48bppRGB, &GUID_WICPixelFormat48bppRGB, NULL},
-    {format_64bppRGBA, &GUID_WICPixelFormat64bppRGBA, NULL},
-    {0}
-};
+static HRESULT copypixels_to_32bppRGB(struct FormatConverter *This, const WICRect *prc,
+    UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
+{
+    switch (source_format)
+    {
+    case format_32bppRGB:
+    case format_32bppRGBA:
+    case format_32bppPRGBA:
+        if (prc)
+            return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+        return S_OK;
+    default:
+        return copypixels_to_32bppRGBA(This, prc, cbStride, cbBufferSize, pbBuffer, source_format);
+    }
+}
 
-static const struct pixelformatinfo *get_formatinfo(const WICPixelFormatGUID *format)
+static HRESULT copypixels_to_32bppPBGRA(struct FormatConverter *This, const WICRect *prc,
+    UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
 {
-    UINT i;
+    HRESULT hr;
 
-    for (i=0; supported_formats[i].guid; i++)
-        if (IsEqualGUID(supported_formats[i].guid, format)) return &supported_formats[i];
+    switch (source_format)
+    {
+    case format_32bppPBGRA:
+        if (prc)
+            return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+        return S_OK;
+    default:
+        hr = copypixels_to_32bppBGRA(This, prc, cbStride, cbBufferSize, pbBuffer, source_format);
+        if (SUCCEEDED(hr) && prc)
+        {
+            INT x, y;
 
-    return NULL;
+            for (y=0; y<prc->Height; y++)
+                for (x=0; x<prc->Width; x++)
+                {
+                    BYTE alpha = pbBuffer[cbStride*y+4*x+3];
+                    if (alpha != 255)
+                    {
+                        pbBuffer[cbStride*y+4*x] = pbBuffer[cbStride*y+4*x] * alpha / 255;
+                        pbBuffer[cbStride*y+4*x+1] = pbBuffer[cbStride*y+4*x+1] * alpha / 255;
+                        pbBuffer[cbStride*y+4*x+2] = pbBuffer[cbStride*y+4*x+2] * alpha / 255;
+                    }
+                }
+        }
+        return hr;
+    }
 }
 
-static HRESULT WINAPI FormatConverter_QueryInterface(IWICFormatConverter *iface, REFIID iid,
-    void **ppv)
+static HRESULT copypixels_to_32bppPRGBA(struct FormatConverter *This, const WICRect *prc,
+    UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
 {
-    FormatConverter *This = (FormatConverter*)iface;
-    TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
-
-    if (!ppv) return E_INVALIDARG;
+    HRESULT hr;
 
-    if (IsEqualIID(&IID_IUnknown, iid) ||
-        IsEqualIID(&IID_IWICBitmapSource, iid) ||
-        IsEqualIID(&IID_IWICFormatConverter, iid))
-    {
-        *ppv = This;
-    }
-    else
+    switch (source_format)
     {
-        *ppv = NULL;
-        return E_NOINTERFACE;
+    case format_32bppPRGBA:
+        if (prc)
+            return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+        return S_OK;
+    default:
+        hr = copypixels_to_32bppRGBA(This, prc, cbStride, cbBufferSize, pbBuffer, source_format);
+        if (SUCCEEDED(hr) && prc)
+        {
+            INT x, y;
+
+            for (y=0; y<prc->Height; y++)
+                for (x=0; x<prc->Width; x++)
+                {
+                    BYTE alpha = pbBuffer[cbStride*y+4*x+3];
+                    if (alpha != 255)
+                    {
+                        pbBuffer[cbStride*y+4*x] = pbBuffer[cbStride*y+4*x] * alpha / 255;
+                        pbBuffer[cbStride*y+4*x+1] = pbBuffer[cbStride*y+4*x+1] * alpha / 255;
+                        pbBuffer[cbStride*y+4*x+2] = pbBuffer[cbStride*y+4*x+2] * alpha / 255;
+                    }
+                }
+        }
+        return hr;
+    }
+}
+
+static HRESULT copypixels_to_24bppBGR(struct FormatConverter *This, const WICRect *prc,
+    UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
+{
+    HRESULT hr;
+
+    switch (source_format)
+    {
+    case format_24bppBGR:
+    case format_24bppRGB:
+        if (prc)
+        {
+            hr = IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+            if (SUCCEEDED(hr) && source_format == format_24bppRGB)
+              reverse_bgr8(3, pbBuffer, prc->Width, prc->Height, cbStride);
+            return hr;
+        }
+        return S_OK;
+    case format_32bppBGR:
+    case format_32bppBGRA:
+    case format_32bppPBGRA:
+        if (prc)
+        {
+            HRESULT res;
+            INT x, y;
+            BYTE *srcdata;
+            UINT srcstride, srcdatasize;
+            const BYTE *srcrow;
+            const BYTE *srcpixel;
+            BYTE *dstrow;
+            BYTE *dstpixel;
+
+            srcstride = 4 * prc->Width;
+            srcdatasize = srcstride * prc->Height;
+
+            srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
+            if (!srcdata) return E_OUTOFMEMORY;
+
+            res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
+
+            if (SUCCEEDED(res))
+            {
+                srcrow = srcdata;
+                dstrow = pbBuffer;
+                for (y=0; y<prc->Height; y++) {
+                    srcpixel=srcrow;
+                    dstpixel=dstrow;
+                    for (x=0; x<prc->Width; x++) {
+                        *dstpixel++=*srcpixel++; /* blue */
+                        *dstpixel++=*srcpixel++; /* green */
+                        *dstpixel++=*srcpixel++; /* red */
+                        srcpixel++; /* alpha */
+                    }
+                    srcrow += srcstride;
+                    dstrow += cbStride;
+                }
+            }
+
+            HeapFree(GetProcessHeap(), 0, srcdata);
+
+            return res;
+        }
+        return S_OK;
+
+    case format_32bppGrayFloat:
+        if (prc)
+        {
+            BYTE *srcdata;
+            UINT srcstride, srcdatasize;
+
+            srcstride = 4 * prc->Width;
+            srcdatasize = srcstride * prc->Height;
+
+            srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
+            if (!srcdata) return E_OUTOFMEMORY;
+
+            hr = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
+
+            if (SUCCEEDED(hr))
+            {
+                INT x, y;
+                BYTE *src = srcdata, *dst = pbBuffer;
+
+                for (y = 0; y < prc->Height; y++)
+                {
+                    float *gray_float = (float *)src;
+                    BYTE *bgr = dst;
+
+                    for (x = 0; x < prc->Width; x++)
+                    {
+                        BYTE gray = (BYTE)floorf(to_sRGB_component(gray_float[x]) * 255.0f + 0.51f);
+                        *bgr++ = gray;
+                        *bgr++ = gray;
+                        *bgr++ = gray;
+                    }
+                    src += srcstride;
+                    dst += cbStride;
+                }
+            }
+
+            HeapFree(GetProcessHeap(), 0, srcdata);
+
+            return hr;
+        }
+        return S_OK;
+
+    case format_32bppCMYK:
+        if (prc)
+        {
+            BYTE *srcdata;
+            UINT srcstride, srcdatasize;
+
+            srcstride = 4 * prc->Width;
+            srcdatasize = srcstride * prc->Height;
+
+            srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
+            if (!srcdata) return E_OUTOFMEMORY;
+
+            hr = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
+            if (SUCCEEDED(hr))
+            {
+                INT x, y;
+                BYTE *src = srcdata, *dst = pbBuffer;
+
+                for (y = 0; y < prc->Height; y++)
+                {
+                    BYTE *cmyk = src;
+                    BYTE *bgr = dst;
+
+                    for (x = 0; x < prc->Width; x++)
+                    {
+                        BYTE c = cmyk[0], m = cmyk[1], y = cmyk[2], k = cmyk[3];
+                        bgr[0] = (255 - y) * (255 - k) / 255; /* B */
+                        bgr[1] = (255 - m) * (255 - k) / 255; /* G */
+                        bgr[2] = (255 - c) * (255 - k) / 255; /* R */
+                        cmyk += 4;
+                        bgr += 3;
+                    }
+                    src += srcstride;
+                    dst += cbStride;
+                }
+            }
+
+            HeapFree(GetProcessHeap(), 0, srcdata);
+            return hr;
+        }
+        return S_OK;
+
+    default:
+        FIXME("Unimplemented conversion path!\n");
+        return WINCODEC_ERR_UNSUPPORTEDOPERATION;
+    }
+}
+
+static HRESULT copypixels_to_24bppRGB(struct FormatConverter *This, const WICRect *prc,
+    UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
+{
+    HRESULT hr;
+
+    switch (source_format)
+    {
+    case format_24bppBGR:
+    case format_24bppRGB:
+        if (prc)
+        {
+            hr = IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+            if (SUCCEEDED(hr) && source_format == format_24bppBGR)
+              reverse_bgr8(3, pbBuffer, prc->Width, prc->Height, cbStride);
+            return hr;
+        }
+        return S_OK;
+    case format_32bppBGR:
+    case format_32bppBGRA:
+    case format_32bppPBGRA:
+        if (prc)
+        {
+            HRESULT res;
+            INT x, y;
+            BYTE *srcdata;
+            UINT srcstride, srcdatasize;
+            const BYTE *srcrow;
+            const BYTE *srcpixel;
+            BYTE *dstrow;
+            BYTE *dstpixel;
+            BYTE tmppixel[3];
+
+            srcstride = 4 * prc->Width;
+            srcdatasize = srcstride * prc->Height;
+
+            srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
+            if (!srcdata) return E_OUTOFMEMORY;
+
+            res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
+
+            if (SUCCEEDED(res))
+            {
+                srcrow = srcdata;
+                dstrow = pbBuffer;
+                for (y=0; y<prc->Height; y++) {
+                    srcpixel=srcrow;
+                    dstpixel=dstrow;
+                    for (x=0; x<prc->Width; x++) {
+                        tmppixel[0]=*srcpixel++; /* blue */
+                        tmppixel[1]=*srcpixel++; /* green */
+                        tmppixel[2]=*srcpixel++; /* red */
+                        srcpixel++; /* alpha */
+
+                        *dstpixel++=tmppixel[2]; /* red */
+                        *dstpixel++=tmppixel[1]; /* green */
+                        *dstpixel++=tmppixel[0]; /* blue */
+                    }
+                    srcrow += srcstride;
+                    dstrow += cbStride;
+                }
+            }
+
+            HeapFree(GetProcessHeap(), 0, srcdata);
+
+            return res;
+        }
+        return S_OK;
+    default:
+        FIXME("Unimplemented conversion path!\n");
+        return WINCODEC_ERR_UNSUPPORTEDOPERATION;
+    }
+}
+
+static HRESULT copypixels_to_32bppGrayFloat(struct FormatConverter *This, const WICRect *prc,
+    UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
+{
+    HRESULT hr;
+
+    switch (source_format)
+    {
+    case format_32bppBGR:
+    case format_32bppBGRA:
+    case format_32bppPBGRA:
+    case format_32bppGrayFloat:
+        if (prc)
+        {
+            hr = IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+            break;
+        }
+        return S_OK;
+
+    default:
+        hr = copypixels_to_32bppBGRA(This, prc, cbStride, cbBufferSize, pbBuffer, source_format);
+        break;
+    }
+
+    if (SUCCEEDED(hr) && prc && source_format != format_32bppGrayFloat)
+    {
+        INT x, y;
+        BYTE *p = pbBuffer;
+
+        for (y = 0; y < prc->Height; y++)
+        {
+            BYTE *bgr = p;
+            for (x = 0; x < prc->Width; x++)
+            {
+                float gray = (bgr[2] * 0.2126f + bgr[1] * 0.7152f + bgr[0] * 0.0722f) / 255.0f;
+                *(float *)bgr = gray;
+                bgr += 4;
+            }
+            p += cbStride;
+        }
+    }
+    return hr;
+}
+
+static HRESULT copypixels_to_8bppGray(struct FormatConverter *This, const WICRect *prc,
+    UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
+{
+    HRESULT hr;
+    BYTE *srcdata;
+    UINT srcstride, srcdatasize;
+
+    if (source_format == format_8bppGray)
+    {
+        if (prc)
+            return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+
+        return S_OK;
+    }
+
+    if (source_format == format_32bppGrayFloat)
+    {
+        hr = S_OK;
+
+        if (prc)
+        {
+            srcstride = 4 * prc->Width;
+            srcdatasize = srcstride * prc->Height;
+
+            srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
+            if (!srcdata) return E_OUTOFMEMORY;
+
+            hr = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
+            if (SUCCEEDED(hr))
+            {
+                INT x, y;
+                BYTE *src = srcdata, *dst = pbBuffer;
+
+                for (y=0; y < prc->Height; y++)
+                {
+                    float *srcpixel = (float*)src;
+                    BYTE *dstpixel = dst;
+
+                    for (x=0; x < prc->Width; x++)
+                        *dstpixel++ = (BYTE)floorf(to_sRGB_component(*srcpixel++) * 255.0f + 0.51f);
+
+                    src += srcstride;
+                    dst += cbStride;
+                }
+            }
+
+            HeapFree(GetProcessHeap(), 0, srcdata);
+        }
+
+        return hr;
+    }
+
+    srcstride = 3 * prc->Width;
+    srcdatasize = srcstride * prc->Height;
+
+    srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
+    if (!srcdata) return E_OUTOFMEMORY;
+
+    hr = copypixels_to_24bppBGR(This, prc, srcstride, srcdatasize, srcdata, source_format);
+    if (SUCCEEDED(hr) && prc)
+    {
+        INT x, y;
+        BYTE *src = srcdata, *dst = pbBuffer;
+
+        for (y = 0; y < prc->Height; y++)
+        {
+            BYTE *bgr = src;
+
+            for (x = 0; x < prc->Width; x++)
+            {
+                float gray = (bgr[2] * 0.2126f + bgr[1] * 0.7152f + bgr[0] * 0.0722f) / 255.0f;
+
+                gray = to_sRGB_component(gray) * 255.0f;
+                dst[x] = (BYTE)floorf(gray + 0.51f);
+                bgr += 3;
+            }
+            src += srcstride;
+            dst += cbStride;
+        }
+    }
+
+    HeapFree(GetProcessHeap(), 0, srcdata);
+    return hr;
+}
+
+static UINT rgb_to_palette_index(BYTE r, BYTE g, BYTE b, WICColor *colors, UINT count)
+{
+    UINT best_diff, best_index, i;
+
+    best_diff = ~0;
+    best_index = 0;
+
+    for (i = 0; i < count; i++)
+    {
+        BYTE pal_r, pal_g, pal_b;
+        DWORD diff_r, diff_g, diff_b, diff;
+
+        pal_r = colors[i] >> 16;
+        pal_g = colors[i] >> 8;
+        pal_b = colors[i];
+
+        diff_r = r - pal_r;
+        diff_g = g - pal_g;
+        diff_b = b - pal_b;
+
+        diff = diff_r * diff_r + diff_g * diff_g + diff_b * diff_b;
+        if (diff == 0) return i;
+
+        if (diff < best_diff)
+        {
+            best_diff = diff;
+            best_index = i;
+        }
+    }
+
+    return best_index;
+}
+
+static HRESULT copypixels_to_8bppIndexed(struct FormatConverter *This, const WICRect *prc,
+    UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
+{
+    HRESULT hr;
+    BYTE *srcdata;
+    WICColor colors[256];
+    UINT srcstride, srcdatasize, count;
+
+    if (source_format == format_8bppIndexed)
+    {
+        if (prc)
+            return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
+
+        return S_OK;
+    }
+
+    if (!This->palette) return WINCODEC_ERR_WRONGSTATE;
+
+    hr = IWICPalette_GetColors(This->palette, 256, colors, &count);
+    if (hr != S_OK) return hr;
+
+    srcstride = 3 * prc->Width;
+    srcdatasize = srcstride * prc->Height;
+
+    srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
+    if (!srcdata) return E_OUTOFMEMORY;
+
+    hr = copypixels_to_24bppBGR(This, prc, srcstride, srcdatasize, srcdata, source_format);
+    if (SUCCEEDED(hr) && prc)
+    {
+        INT x, y;
+        BYTE *src = srcdata, *dst = pbBuffer;
+
+        for (y = 0; y < prc->Height; y++)
+        {
+            BYTE *bgr = src;
+
+            for (x = 0; x < prc->Width; x++)
+            {
+                dst[x] = rgb_to_palette_index(bgr[2], bgr[1], bgr[0], colors, count);
+                bgr += 3;
+            }
+            src += srcstride;
+            dst += cbStride;
+        }
+    }
+
+    HeapFree(GetProcessHeap(), 0, srcdata);
+    return hr;
+}
+
+static const struct pixelformatinfo supported_formats[] = {
+    {format_1bppIndexed, &GUID_WICPixelFormat1bppIndexed, NULL},
+    {format_2bppIndexed, &GUID_WICPixelFormat2bppIndexed, NULL},
+    {format_4bppIndexed, &GUID_WICPixelFormat4bppIndexed, NULL},
+    {format_8bppIndexed, &GUID_WICPixelFormat8bppIndexed, copypixels_to_8bppIndexed},
+    {format_BlackWhite, &GUID_WICPixelFormatBlackWhite, NULL},
+    {format_2bppGray, &GUID_WICPixelFormat2bppGray, NULL},
+    {format_4bppGray, &GUID_WICPixelFormat4bppGray, NULL},
+    {format_8bppGray, &GUID_WICPixelFormat8bppGray, copypixels_to_8bppGray},
+    {format_16bppGray, &GUID_WICPixelFormat16bppGray, NULL},
+    {format_16bppBGR555, &GUID_WICPixelFormat16bppBGR555, NULL},
+    {format_16bppBGR565, &GUID_WICPixelFormat16bppBGR565, NULL},
+    {format_16bppBGRA5551, &GUID_WICPixelFormat16bppBGRA5551, NULL},
+    {format_24bppBGR, &GUID_WICPixelFormat24bppBGR, copypixels_to_24bppBGR},
+    {format_24bppRGB, &GUID_WICPixelFormat24bppRGB, copypixels_to_24bppRGB},
+    {format_32bppGrayFloat, &GUID_WICPixelFormat32bppGrayFloat, copypixels_to_32bppGrayFloat},
+    {format_32bppBGR, &GUID_WICPixelFormat32bppBGR, copypixels_to_32bppBGR},
+    {format_32bppRGB, &GUID_WICPixelFormat32bppRGB, copypixels_to_32bppRGB},
+    {format_32bppBGRA, &GUID_WICPixelFormat32bppBGRA, copypixels_to_32bppBGRA},
+    {format_32bppRGBA, &GUID_WICPixelFormat32bppRGBA, copypixels_to_32bppRGBA},
+    {format_32bppPBGRA, &GUID_WICPixelFormat32bppPBGRA, copypixels_to_32bppPBGRA},
+    {format_32bppPRGBA, &GUID_WICPixelFormat32bppPRGBA, copypixels_to_32bppPRGBA},
+    {format_48bppRGB, &GUID_WICPixelFormat48bppRGB, NULL},
+    {format_64bppRGBA, &GUID_WICPixelFormat64bppRGBA, NULL},
+    {format_32bppCMYK, &GUID_WICPixelFormat32bppCMYK, NULL},
+    {0}
+};
+
+static const struct pixelformatinfo *get_formatinfo(const WICPixelFormatGUID *format)
+{
+    UINT i;
+
+    for (i=0; supported_formats[i].guid; i++)
+        if (IsEqualGUID(supported_formats[i].guid, format)) return &supported_formats[i];
+
+    return NULL;
+}
+
+static HRESULT WINAPI FormatConverter_QueryInterface(IWICFormatConverter *iface, REFIID iid,
+    void **ppv)
+{
+    FormatConverter *This = impl_from_IWICFormatConverter(iface);
+    TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
+
+    if (!ppv) return E_INVALIDARG;
+
+    if (IsEqualIID(&IID_IUnknown, iid) ||
+        IsEqualIID(&IID_IWICBitmapSource, iid) ||
+        IsEqualIID(&IID_IWICFormatConverter, iid))
+    {
+        *ppv = &This->IWICFormatConverter_iface;
+    }
+    else
+    {
+        *ppv = NULL;
+        return E_NOINTERFACE;
     }
 
     IUnknown_AddRef((IUnknown*)*ppv);
@@ -749,7 +1471,7 @@ static HRESULT WINAPI FormatConverter_QueryInterface(IWICFormatConverter *iface,
 
 static ULONG WINAPI FormatConverter_AddRef(IWICFormatConverter *iface)
 {
-    FormatConverter *This = (FormatConverter*)iface;
+    FormatConverter *This = impl_from_IWICFormatConverter(iface);
     ULONG ref = InterlockedIncrement(&This->ref);
 
     TRACE("(%p) refcount=%u\n", iface, ref);
@@ -759,7 +1481,7 @@ static ULONG WINAPI FormatConverter_AddRef(IWICFormatConverter *iface)
 
 static ULONG WINAPI FormatConverter_Release(IWICFormatConverter *iface)
 {
-    FormatConverter *This = (FormatConverter*)iface;
+    FormatConverter *This = impl_from_IWICFormatConverter(iface);
     ULONG ref = InterlockedDecrement(&This->ref);
 
     TRACE("(%p) refcount=%u\n", iface, ref);
@@ -769,6 +1491,7 @@ static ULONG WINAPI FormatConverter_Release(IWICFormatConverter *iface)
         This->lock.DebugInfo->Spare[0] = 0;
         DeleteCriticalSection(&This->lock);
         if (This->source) IWICBitmapSource_Release(This->source);
+        if (This->palette) IWICPalette_Release(This->palette);
         HeapFree(GetProcessHeap(), 0, This);
     }
 
@@ -778,7 +1501,7 @@ static ULONG WINAPI FormatConverter_Release(IWICFormatConverter *iface)
 static HRESULT WINAPI FormatConverter_GetSize(IWICFormatConverter *iface,
     UINT *puiWidth, UINT *puiHeight)
 {
-    FormatConverter *This = (FormatConverter*)iface;
+    FormatConverter *This = impl_from_IWICFormatConverter(iface);
 
     TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
 
@@ -791,9 +1514,9 @@ static HRESULT WINAPI FormatConverter_GetSize(IWICFormatConverter *iface,
 static HRESULT WINAPI FormatConverter_GetPixelFormat(IWICFormatConverter *iface,
     WICPixelFormatGUID *pPixelFormat)
 {
-    FormatConverter *This = (FormatConverter*)iface;
+    FormatConverter *This = impl_from_IWICFormatConverter(iface);
 
-    TRACE("(%p,%p): stub\n", iface, pPixelFormat);
+    TRACE("(%p,%p)\n", iface, pPixelFormat);
 
     if (This->source)
         memcpy(pPixelFormat, This->dst_format->guid, sizeof(GUID));
@@ -806,9 +1529,9 @@ static HRESULT WINAPI FormatConverter_GetPixelFormat(IWICFormatConverter *iface,
 static HRESULT WINAPI FormatConverter_GetResolution(IWICFormatConverter *iface,
     double *pDpiX, double *pDpiY)
 {
-    FormatConverter *This = (FormatConverter*)iface;
+    FormatConverter *This = impl_from_IWICFormatConverter(iface);
 
-    TRACE("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
+    TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
 
     if (This->source)
         return IWICBitmapSource_GetResolution(This->source, pDpiX, pDpiY);
@@ -817,39 +1540,108 @@ static HRESULT WINAPI FormatConverter_GetResolution(IWICFormatConverter *iface,
 }
 
 static HRESULT WINAPI FormatConverter_CopyPalette(IWICFormatConverter *iface,
-    IWICPalette *pIPalette)
+    IWICPalette *palette)
 {
-    FIXME("(%p,%p): stub\n", iface, pIPalette);
-    return E_NOTIMPL;
+    FormatConverter *This = impl_from_IWICFormatConverter(iface);
+
+    TRACE("(%p,%p)\n", iface, palette);
+
+    if (!palette) return E_INVALIDARG;
+    if (!This->source) return WINCODEC_ERR_WRONGSTATE;
+
+    if (!This->palette)
+    {
+        HRESULT hr;
+        UINT bpp;
+
+        hr = get_pixelformat_bpp(This->dst_format->guid, &bpp);
+        if (hr != S_OK) return hr;
+        if (bpp <= 8) return WINCODEC_ERR_WRONGSTATE;
+        return IWICBitmapSource_CopyPalette(This->source, palette);
+    }
+
+    return IWICPalette_InitializeFromPalette(palette, This->palette);
 }
 
 static HRESULT WINAPI FormatConverter_CopyPixels(IWICFormatConverter *iface,
     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
 {
-    FormatConverter *This = (FormatConverter*)iface;
+    FormatConverter *This = impl_from_IWICFormatConverter(iface);
+    WICRect rc;
+    HRESULT hr;
     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
 
     if (This->source)
+    {
+        if (!prc)
+        {
+            UINT width, height;
+            hr = IWICBitmapSource_GetSize(This->source, &width, &height);
+            if (FAILED(hr)) return hr;
+            rc.X = 0;
+            rc.Y = 0;
+            rc.Width = width;
+            rc.Height = height;
+            prc = &rc;
+        }
+
         return This->dst_format->copy_function(This, prc, cbStride, cbBufferSize,
             pbBuffer, This->src_format->format);
+    }
     else
-        return WINCODEC_ERR_NOTINITIALIZED;
+        return WINCODEC_ERR_WRONGSTATE;
 }
 
 static HRESULT WINAPI FormatConverter_Initialize(IWICFormatConverter *iface,
-    IWICBitmapSource *pISource, REFWICPixelFormatGUID dstFormat, WICBitmapDitherType dither,
-    IWICPalette *pIPalette, double alphaThresholdPercent, WICBitmapPaletteType paletteTranslate)
+    IWICBitmapSource *source, REFWICPixelFormatGUID dstFormat, WICBitmapDitherType dither,
+    IWICPalette *palette, double alpha_threshold, WICBitmapPaletteType palette_type)
 {
-    FormatConverter *This = (FormatConverter*)iface;
+    FormatConverter *This = impl_from_IWICFormatConverter(iface);
     const struct pixelformatinfo *srcinfo, *dstinfo;
-    static INT fixme=0;
     GUID srcFormat;
-    HRESULT res=S_OK;
+    HRESULT res;
+
+    TRACE("(%p,%p,%s,%u,%p,%0.3f,%u)\n", iface, source, debugstr_guid(dstFormat),
+        dither, palette, alpha_threshold, palette_type);
 
-    TRACE("(%p,%p,%s,%u,%p,%0.1f,%u)\n", iface, pISource, debugstr_guid(dstFormat),
-        dither, pIPalette, alphaThresholdPercent, paletteTranslate);
+    if (!palette)
+    {
+        UINT bpp;
+        res = get_pixelformat_bpp(dstFormat, &bpp);
+        if (res != S_OK) return res;
 
-    if (pIPalette && !fixme++) FIXME("ignoring palette\n");
+        res = PaletteImpl_Create(&palette);
+        if (res != S_OK) return res;
+
+        switch (palette_type)
+        {
+        case WICBitmapPaletteTypeCustom:
+            IWICPalette_Release(palette);
+            palette = NULL;
+            if (bpp <= 8) return E_INVALIDARG;
+            break;
+
+        case WICBitmapPaletteTypeMedianCut:
+        {
+            if (bpp <= 8)
+                res = IWICPalette_InitializeFromBitmap(palette, source, 1 << bpp, FALSE);
+            break;
+        }
+
+        default:
+            if (bpp <= 8)
+                res = IWICPalette_InitializePredefined(palette, palette_type, FALSE);
+            break;
+        }
+
+        if (res != S_OK)
+        {
+            IWICPalette_Release(palette);
+            return res;
+        }
+    }
+    else
+        IWICPalette_AddRef(palette);
 
     EnterCriticalSection(&This->lock);
 
@@ -859,13 +1651,14 @@ static HRESULT WINAPI FormatConverter_Initialize(IWICFormatConverter *iface,
         goto end;
     }
 
-    res = IWICBitmapSource_GetPixelFormat(pISource, &srcFormat);
+    res = IWICBitmapSource_GetPixelFormat(source, &srcFormat);
     if (FAILED(res)) goto end;
 
     srcinfo = get_formatinfo(&srcFormat);
     if (!srcinfo)
     {
         res = WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
+        FIXME("Unsupported source format %s\n", debugstr_guid(&srcFormat));
         goto end;
     }
 
@@ -873,26 +1666,33 @@ static HRESULT WINAPI FormatConverter_Initialize(IWICFormatConverter *iface,
     if (!dstinfo)
     {
         res = WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
+        FIXME("Unsupported destination format %s\n", debugstr_guid(dstFormat));
         goto end;
     }
 
     if (dstinfo->copy_function)
     {
-        IWICBitmapSource_AddRef(pISource);
+        IWICBitmapSource_AddRef(source);
         This->src_format = srcinfo;
         This->dst_format = dstinfo;
         This->dither = dither;
-        This->alpha_threshold = alphaThresholdPercent;
-        This->palette_type = paletteTranslate;
-        This->source = pISource;
+        This->alpha_threshold = alpha_threshold;
+        This->palette = palette;
+        This->source = source;
     }
     else
+    {
+        FIXME("Unsupported conversion %s -> %s\n", debugstr_guid(&srcFormat), debugstr_guid(dstFormat));
         res = WINCODEC_ERR_UNSUPPORTEDOPERATION;
+    }
 
 end:
 
     LeaveCriticalSection(&This->lock);
 
+    if (res != S_OK && palette)
+        IWICPalette_Release(palette);
+
     return res;
 }
 
@@ -900,23 +1700,34 @@ static HRESULT WINAPI FormatConverter_CanConvert(IWICFormatConverter *iface,
     REFWICPixelFormatGUID srcPixelFormat, REFWICPixelFormatGUID dstPixelFormat,
     BOOL *pfCanConvert)
 {
-    FormatConverter *This = (FormatConverter*)iface;
+    FormatConverter *This = impl_from_IWICFormatConverter(iface);
     const struct pixelformatinfo *srcinfo, *dstinfo;
 
     TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(srcPixelFormat),
         debugstr_guid(dstPixelFormat), pfCanConvert);
 
     srcinfo = get_formatinfo(srcPixelFormat);
-    if (!srcinfo) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
+    if (!srcinfo)
+    {
+        FIXME("Unsupported source format %s\n", debugstr_guid(srcPixelFormat));
+        return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
+    }
 
     dstinfo = get_formatinfo(dstPixelFormat);
-    if (!dstinfo) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
+    if (!dstinfo)
+    {
+        FIXME("Unsupported destination format %s\n", debugstr_guid(dstPixelFormat));
+        return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
+    }
 
     if (dstinfo->copy_function &&
         SUCCEEDED(dstinfo->copy_function(This, NULL, 0, 0, NULL, dstinfo->format)))
         *pfCanConvert = TRUE;
     else
+    {
+        FIXME("Unsupported conversion %s -> %s\n", debugstr_guid(srcPixelFormat), debugstr_guid(dstPixelFormat));
         *pfCanConvert = FALSE;
+    }
 
     return S_OK;
 }
@@ -934,28 +1745,27 @@ static const IWICFormatConverterVtbl FormatConverter_Vtbl = {
     FormatConverter_CanConvert
 };
 
-HRESULT FormatConverter_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
+HRESULT FormatConverter_CreateInstance(REFIID iid, void** ppv)
 {
     FormatConverter *This;
     HRESULT ret;
 
-    TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
+    TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
 
     *ppv = NULL;
 
-    if (pUnkOuter) return CLASS_E_NOAGGREGATION;
-
     This = HeapAlloc(GetProcessHeap(), 0, sizeof(FormatConverter));
     if (!This) return E_OUTOFMEMORY;
 
-    This->lpVtbl = &FormatConverter_Vtbl;
+    This->IWICFormatConverter_iface.lpVtbl = &FormatConverter_Vtbl;
     This->ref = 1;
     This->source = NULL;
+    This->palette = NULL;
     InitializeCriticalSection(&This->lock);
     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FormatConverter.lock");
 
-    ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
-    IUnknown_Release((IUnknown*)This);
+    ret = IWICFormatConverter_QueryInterface(&This->IWICFormatConverter_iface, iid, ppv);
+    IWICFormatConverter_Release(&This->IWICFormatConverter_iface);
 
     return ret;
 }