From 964980631537fea41619e05d09f1d14cf663755c Mon Sep 17 00:00:00 2001
From: Dmitry Timoshkov <dmitry@baikal.ru>
Date: Mon, 7 Mar 2016 15:38:19 +0800
Subject: windowscodecs: Add support for 32bppGrayFloat format.

For bug 36517.
---
 dlls/windowscodecs/converter.c       | 45 ++++++++++++++++++++++++++++++++++
 dlls/windowscodecs/regsvr.c          |  1 +
 dlls/windowscodecs/tests/converter.c | 47 ++++++++++++++++++++++++++++++++++++
 include/wincodec.idl                 |  1 +
 4 files changed, 94 insertions(+)

diff --git a/dlls/windowscodecs/converter.c b/dlls/windowscodecs/converter.c
index 11f04d9..79b0a4b 100644
--- a/dlls/windowscodecs/converter.c
+++ b/dlls/windowscodecs/converter.c
@@ -50,6 +50,7 @@ enum pixelformat {
     format_16bppBGRA5551,
     format_24bppBGR,
     format_24bppRGB,
+    format_32bppGrayFloat,
     format_32bppBGR,
     format_32bppBGRA,
     format_32bppPBGRA,
@@ -994,6 +995,49 @@ static HRESULT copypixels_to_24bppRGB(struct FormatConverter *This, const WICRec
     }
 }
 
+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 const struct pixelformatinfo supported_formats[] = {
     {format_1bppIndexed, &GUID_WICPixelFormat1bppIndexed, NULL},
     {format_2bppIndexed, &GUID_WICPixelFormat2bppIndexed, NULL},
@@ -1009,6 +1053,7 @@ static const struct pixelformatinfo supported_formats[] = {
     {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_32bppBGRA, &GUID_WICPixelFormat32bppBGRA, copypixels_to_32bppBGRA},
     {format_32bppPBGRA, &GUID_WICPixelFormat32bppPBGRA, copypixels_to_32bppPBGRA},
diff --git a/dlls/windowscodecs/regsvr.c b/dlls/windowscodecs/regsvr.c
index ccb78b8..08aeb06 100644
--- a/dlls/windowscodecs/regsvr.c
+++ b/dlls/windowscodecs/regsvr.c
@@ -1454,6 +1454,7 @@ static GUID const * const converter_formats[] = {
     &GUID_WICPixelFormat32bppBGR,
     &GUID_WICPixelFormat32bppBGRA,
     &GUID_WICPixelFormat32bppPBGRA,
+    &GUID_WICPixelFormat32bppGrayFloat,
     &GUID_WICPixelFormat48bppRGB,
     &GUID_WICPixelFormat64bppRGBA,
     &GUID_WICPixelFormat32bppCMYK,
diff --git a/dlls/windowscodecs/tests/converter.c b/dlls/windowscodecs/tests/converter.c
index 00ea483..6a5fc4f 100644
--- a/dlls/windowscodecs/tests/converter.c
+++ b/dlls/windowscodecs/tests/converter.c
@@ -43,6 +43,11 @@ typedef struct BitmapTestSrc {
     const bitmap_data *data;
 } BitmapTestSrc;
 
+static BOOL near_equal(float a, float b)
+{
+    return fabsf(a - b) < 0.001;
+}
+
 static inline BitmapTestSrc *impl_from_IWICBitmapSource(IWICBitmapSource *iface)
 {
     return CONTAINING_RECORD(iface, BitmapTestSrc, IWICBitmapSource_iface);
@@ -196,6 +201,11 @@ static void DeleteTestBitmap(BitmapTestSrc *This)
     HeapFree(GetProcessHeap(), 0, This);
 }
 
+/* XP and 2003 use linear color conversion, later versions use sRGB gamma */
+static const float bits_32bppGrayFloat_xp[] = {
+    0.114000f,0.587000f,0.299000f,0.000000f,
+    0.886000f,0.413000f,0.701000f,1.000000f};
+
 static void compare_bitmap_data(const struct bitmap_data *expect, IWICBitmapSource *source, const char *name)
 {
     BYTE *converted_bits;
@@ -245,6 +255,20 @@ static void compare_bitmap_data(const struct bitmap_data *expect, IWICBitmapSour
             }
         ok(equal, "unexpected pixel data (%s)\n", name);
     }
+    else if (IsEqualGUID(expect->format, &GUID_WICPixelFormat32bppGrayFloat))
+    {
+        BOOL equal=TRUE;
+        UINT i;
+        const float *a=(const float*)expect->bits, *b=(const float*)converted_bits;
+        for (i=0; i<(buffersize/4); i++)
+            if (!near_equal(a[i], b[i]) && !near_equal(bits_32bppGrayFloat_xp[i], b[i]))
+            {
+                equal = FALSE;
+                break;
+            }
+
+        ok(equal, "unexpected pixel data (%s)\n", name);
+    }
     else
         ok(memcmp(expect->bits, converted_bits, buffersize) == 0, "unexpected pixel data (%s)\n", name);
 
@@ -265,6 +289,20 @@ static void compare_bitmap_data(const struct bitmap_data *expect, IWICBitmapSour
             }
         ok(equal, "unexpected pixel data with rc=NULL (%s)\n", name);
     }
+    else if (IsEqualGUID(expect->format, &GUID_WICPixelFormat32bppGrayFloat))
+    {
+        BOOL equal=TRUE;
+        UINT i;
+        const float *a=(const float*)expect->bits, *b=(const float*)converted_bits;
+        for (i=0; i<(buffersize/4); i++)
+            if (!near_equal(a[i], b[i]) && !near_equal(bits_32bppGrayFloat_xp[i], b[i]))
+            {
+                equal = FALSE;
+                break;
+            }
+
+        ok(equal, "unexpected pixel data (%s)\n", name);
+    }
     else
         ok(memcmp(expect->bits, converted_bits, buffersize) == 0, "unexpected pixel data with rc=NULL (%s)\n", name);
 
@@ -295,6 +333,12 @@ static const BYTE bits_32bppBGRA[] = {
 static const struct bitmap_data testdata_32bppBGRA = {
     &GUID_WICPixelFormat32bppBGRA, 32, bits_32bppBGRA, 4, 2, 96.0, 96.0};
 
+static const float bits_32bppGrayFloat[] = {
+    0.072200f,0.715200f,0.212600f,0.000000f,
+    0.927800f,0.284800f,0.787400f,1.000000f};
+static const struct bitmap_data testdata_32bppGrayFloat = {
+    &GUID_WICPixelFormat32bppGrayFloat, 32, (const BYTE *)bits_32bppGrayFloat, 4, 2, 96.0, 96.0};
+
 static void test_conversion(const struct bitmap_data *src, const struct bitmap_data *dst, const char *name, BOOL todo)
 {
     BitmapTestSrc *src_obj;
@@ -734,6 +778,9 @@ START_TEST(converter)
     test_conversion(&testdata_32bppBGR, &testdata_24bppRGB, "32bppBGR -> 24bppRGB", FALSE);
     test_conversion(&testdata_24bppRGB, &testdata_32bppBGR, "24bppRGB -> 32bppBGR", FALSE);
 
+    test_conversion(&testdata_24bppRGB, &testdata_32bppGrayFloat, "24bppRGB -> 32bppGrayFloat", FALSE);
+    test_conversion(&testdata_32bppBGR, &testdata_32bppGrayFloat, "32bppBGR -> 32bppGrayFloat", FALSE);
+
     test_invalid_conversion();
     test_default_converter();
 
diff --git a/include/wincodec.idl b/include/wincodec.idl
index 639d925..83daba8 100644
--- a/include/wincodec.idl
+++ b/include/wincodec.idl
@@ -196,6 +196,7 @@ cpp_quote("DEFINE_GUID(GUID_WICPixelFormat32bppPBGRA, 0x6fddc324,0x4e03,0x4bfe,0
 cpp_quote("DEFINE_GUID(GUID_WICPixelFormat32bppRGB, 0xd98c6b95,0x3efe,0x47d6,0xbb,0x25,0xeb,0x17,0x48,0xab,0x0c,0xf1);")
 cpp_quote("DEFINE_GUID(GUID_WICPixelFormat32bppRGBA, 0xf5c7ad2d,0x6a8d,0x43dd,0xa7,0xa8,0xa2,0x99,0x35,0x26,0x1a,0xe9);")
 cpp_quote("DEFINE_GUID(GUID_WICPixelFormat32bppPRGBA, 0x3cc4a650,0xa527,0x4d37,0xa9,0x16,0x31,0x42,0xc7,0xeb,0xed,0xba);")
+cpp_quote("DEFINE_GUID(GUID_WICPixelFormat32bppGrayFloat, 0x6fddc324,0x4e03,0x4bfe,0xb1,0x85,0x3d,0x77,0x76,0x8d,0xc9,0x11);")
 
 cpp_quote("DEFINE_GUID(GUID_WICPixelFormat48bppRGB, 0x6fddc324,0x4e03,0x4bfe,0xb1,0x85,0x3d,0x77,0x76,0x8d,0xc9,0x15);")
 cpp_quote("DEFINE_GUID(GUID_WICPixelFormat64bppRGBA, 0x6fddc324,0x4e03,0x4bfe,0xb1,0x85,0x3d,0x77,0x76,0x8d,0xc9,0x16);")
-- 
2.7.1

