From 0c1d97ca80e77a06c85796360d56987b2d51ad0d Mon Sep 17 00:00:00 2001
From: Dmitry Timoshkov <dmitry@baikal.ru>
Date: Fri, 9 Dec 2016 13:02:07 +0800
Subject: [PATCH] windowscodecs: Add some tests for various TIFF color formats.

---
 dlls/windowscodecs/tests/tiffformat.c | 437 ++++++++++++++++++++++++++++++++++
 1 file changed, 437 insertions(+)

diff --git a/dlls/windowscodecs/tests/tiffformat.c b/dlls/windowscodecs/tests/tiffformat.c
index 2f9a0ca..8cdc004 100644
--- a/dlls/windowscodecs/tests/tiffformat.c
+++ b/dlls/windowscodecs/tests/tiffformat.c
@@ -361,6 +361,61 @@ static HRESULT create_decoder(const void *image_data, UINT image_size, IWICBitma
     return hr;
 }
 
+static HRESULT get_pixelformat_info(const GUID *format, UINT *bpp, UINT *channels, BOOL *trasparency)
+{
+    HRESULT hr;
+    IWICComponentInfo *info;
+    IWICPixelFormatInfo2 *formatinfo;
+
+    hr = IWICImagingFactory_CreateComponentInfo(factory, format, &info);
+    ok(hr == S_OK, "CreateComponentInfo(%s) error %#x\n", wine_dbgstr_guid(format), hr);
+    if (hr == S_OK)
+    {
+        hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo2, (void **)&formatinfo);
+        if (hr == S_OK)
+        {
+            hr = IWICPixelFormatInfo2_SupportsTransparency(formatinfo, trasparency);
+            ok(hr == S_OK, "SupportsTransparency error %#x\n", hr);
+            IWICPixelFormatInfo2_Release(formatinfo);
+        }
+        hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo, (void **)&formatinfo);
+        if (hr == S_OK)
+        {
+            hr = IWICPixelFormatInfo2_GetBitsPerPixel(formatinfo, bpp);
+            ok(hr == S_OK, "GetBitsPerPixel error %#x\n", hr);
+            hr = IWICPixelFormatInfo2_GetChannelCount(formatinfo, channels);
+            ok(hr == S_OK, "GetChannelCount error %#x\n", hr);
+            IWICPixelFormatInfo2_Release(formatinfo);
+        }
+        IWICComponentInfo_Release(info);
+    }
+    return hr;
+}
+
+static void dump_tiff(void *buf)
+{
+    UINT count, i;
+    struct tiff_1bpp_data *tiff;
+    struct IFD_entry *tag;
+
+    tiff = buf;
+    count = *(short *)((char *)tiff + tiff->dir_offset);
+    tag = (struct IFD_entry *)((char *)tiff + tiff->dir_offset + sizeof(short));
+
+    for (i = 0; i < count; i++)
+    {
+        printf("tag %u: id %04x, type %04x, count %u, value %d",
+                i, tag[i].id, tag[i].type, tag[i].count, tag[i].value);
+        if (tag[i].id == 0x102 && tag[i].count > 2)
+        {
+            short *bps = (short *)((char *)tiff + tag[i].value);
+            printf(" (%d,%d,%d,%d)\n", bps[0], bps[1], bps[2], bps[3]);
+        }
+        else
+            printf("\n");
+    }
+}
+
 static void test_tiff_1bpp_palette(void)
 {
     HRESULT hr;
@@ -697,6 +752,7 @@ static void test_tiff_24bpp(void)
     hr = create_decoder(&tiff_24bpp_data, sizeof(tiff_24bpp_data), &decoder);
     ok(hr == S_OK, "Failed to load TIFF image data %#x\n", hr);
     if (hr != S_OK) return;
+    ok(decoder != NULL, "Failed to load TIFF image data\n");
 
     hr = IWICBitmapDecoder_GetFrameCount(decoder, &count);
     ok(hr == S_OK, "GetFrameCount error %#x\n", hr);
@@ -743,6 +799,386 @@ static void test_tiff_24bpp(void)
     IWICBitmapDecoder_Release(decoder);
 }
 
+#include "pshpack2.h"
+static const struct tiff_1x1_data
+{
+    USHORT byte_order;
+    USHORT version;
+    ULONG  dir_offset;
+    USHORT number_of_entries;
+    struct IFD_entry entry[12];
+    ULONG next_IFD;
+    struct IFD_rational res;
+    short palette_data[3][256];
+    short bps_data[4];
+    BYTE pixel_data[32];
+} tiff_1x1_data =
+{
+#ifdef WORDS_BIGENDIAN
+    'M' | 'M' << 8,
+#else
+    'I' | 'I' << 8,
+#endif
+    42,
+    FIELD_OFFSET(struct tiff_1x1_data, number_of_entries),
+    12,
+    {
+        { 0xff, IFD_SHORT, 1, 0 }, /* SUBFILETYPE */
+        { 0x100, IFD_LONG, 1, 1 }, /* IMAGEWIDTH */
+        { 0x101, IFD_LONG, 1, 1 }, /* IMAGELENGTH */
+        { 0x102, IFD_SHORT, 3, FIELD_OFFSET(struct tiff_1x1_data, bps_data) }, /* BITSPERSAMPLE */
+        { 0x103, IFD_SHORT, 1, 1 }, /* COMPRESSION: XP doesn't accept IFD_LONG here */
+        { 0x106, IFD_SHORT, 1, 2 }, /* PHOTOMETRIC */
+        { 0x111, IFD_LONG, 1, FIELD_OFFSET(struct tiff_1x1_data, pixel_data) }, /* STRIPOFFSETS */
+        { 0x115, IFD_SHORT, 1, 3 }, /* SAMPLESPERPIXEL */
+        { 0x11a, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_1x1_data, res) },
+        { 0x11b, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_1x1_data, res) },
+        { 0x128, IFD_SHORT, 1, 2 }, /* RESOLUTIONUNIT */
+        { 0x140, IFD_SHORT, 256*3, FIELD_OFFSET(struct tiff_1x1_data, palette_data) } /* COLORMAP */
+    },
+    0,
+    { 96, 1 },
+    { { 0 } },
+    { 8,8,8,0 },
+    { 1,0,2,3,4,5,6,7,8,9,0,1,2,3,4,5 }
+};
+#include "poppack.h"
+
+static UINT width_bytes(UINT width, UINT bpp)
+{
+    return (width * bpp + 7) / 8;
+}
+
+static void test_color_formats(void)
+{
+    struct bitmap_data
+    {
+        UINT bpp;
+        UINT width;
+        UINT height;
+        const WICPixelFormatGUID *format;
+        const BYTE *bits;
+    };
+    static const BYTE bits_1bpsBGR[] = { 0,255,0,255,0,255,255,255,0,0,0,255,255,0,0,0,255,255,255,255,255,0,0,0,0,255,0,255,0,255 };
+    static const struct bitmap_data data_1bpsBGR =
+    {
+        24, 10, 2, &GUID_WICPixelFormat24bppBGR, bits_1bpsBGR
+    };
+    static const BYTE bits_4bpsBGR[] = { 204,85,85,136,187,51,0,85,85,85,0,68,0,102,0,136,0,119,0,153,0 };
+    static const struct bitmap_data data_4bpsBGR =
+    {
+        24, 5, 2, &GUID_WICPixelFormat24bppBGR, bits_4bpsBGR
+    };
+    static const BYTE bits_8bpsBGR[] = { 2,0,1,5,4,3,8,7,6 };
+    static const struct bitmap_data data_8bpsBGR =
+    {
+        24, 3, 1, &GUID_WICPixelFormat24bppBGR, bits_8bpsBGR
+    };
+    static const BYTE bits_48bppRGB[] = { 1,0,2,3,4,5,6,7,8,9,0,1 };
+    static const struct bitmap_data data_48bppRGB =
+    {
+        48, 2, 1, &GUID_WICPixelFormat48bppRGB, bits_48bppRGB
+    };
+    static const BYTE bits_1bpsBGRA[] = { 0,255,0,255,0,255,0,255,0,255,255,0,255,0,0,255,255,0,255,255,0,0,255,0,0,255,0,255,0,255,0,255,0,0,0,0,0,255,0,0 };
+    static const struct bitmap_data data_1bpsBGRA =
+    {
+        32, 5, 2, &GUID_WICPixelFormat32bppBGRA, bits_1bpsBGRA
+    };
+    static const BYTE bits_4bpsBGRA[] = { 204,85,85,51,85,136,187,85,0,68,0,85,0,102,0,119,0,136,0,153,0,0,0,17,0,34,0,51 };
+    static const struct bitmap_data data_4bpsBGRA =
+    {
+        32, 5, 2, &GUID_WICPixelFormat32bppBGRA, bits_4bpsBGRA
+    };
+    static const BYTE bits_8bpsBGRA[] = { 2,0,1,3,6,5,4,7,0,9,8,1,4,3,2,5 };
+    static const struct bitmap_data data_8bpsBGRA =
+    {
+        32, 4, 1, &GUID_WICPixelFormat32bppBGRA, bits_8bpsBGRA
+    };
+    static const BYTE bits_64bppRGBA[] = { 1,0,2,3,4,5,6,7,8,9,0,1,2,3,4,5 };
+    static const struct bitmap_data data_64bppRGBA =
+    {
+        64, 2, 1, &GUID_WICPixelFormat64bppRGBA, bits_64bppRGBA
+    };
+    static const BYTE bits_BlackWhite[] = { 85,195,184,85 };
+    static const struct bitmap_data data_BlackWhite =
+    {
+        1, 30, 1, &GUID_WICPixelFormatBlackWhite, bits_BlackWhite
+    };
+    static const BYTE bits_BlackWhite_xp[] = { 85,195,184,84 };
+    static const struct bitmap_data data_BlackWhite_xp =
+    {
+        1, 30, 1, &GUID_WICPixelFormatBlackWhite, bits_BlackWhite_xp
+    };
+    static const BYTE bits_4bppGray[] = { 85,195,184,85 };
+    static const struct bitmap_data data_4bppGray =
+    {
+        4, 7, 1, &GUID_WICPixelFormat4bppGray, bits_4bppGray
+    };
+    static const BYTE bits_4bppGray_xp[] = { 85,195,184,80 };
+    static const struct bitmap_data data_4bppGray_xp =
+    {
+        4, 7, 1, &GUID_WICPixelFormat4bppGray, bits_4bppGray_xp
+    };
+    static const BYTE bits_8bppGray[] = { 1,0,2,3,4,5,6,7,8,9 };
+    static const struct bitmap_data data_8bppGray =
+    {
+        8, 10, 1, &GUID_WICPixelFormat8bppGray, bits_8bppGray
+    };
+    static const BYTE bits_16bppGray[] = { 1,0,2,3,4,5 };
+    static const struct bitmap_data data_16bppGray =
+    {
+        16, 3, 1, &GUID_WICPixelFormat16bppGray, bits_16bppGray
+    };
+    static const BYTE bits_32bppGrayFloat[] = { 1,0,2,3,4,5,6,7,8,9,0,1 };
+    static const struct bitmap_data data_32bppGrayFloat =
+    {
+        32, 3, 1, &GUID_WICPixelFormat32bppGrayFloat, bits_32bppGrayFloat
+    };
+#if 0 /* FIXME */
+    static const BYTE bits_96bpp3Channels[] = { 0 };
+    static const struct bitmap_data data_96bpp3Channels =
+    {
+        64, 1, 1, &GUID_WICPixelFormat96bpp3Channels, bits_96bpp3Channels
+    };
+#endif
+    static const BYTE bits_128bppRGBAFloat[] = { 1,0,2,3,4,5,6,7,8,9,0,1,2,3,4,5 };
+    static const struct bitmap_data data_128bppRGBAFloat =
+    {
+        128, 1, 1, &GUID_WICPixelFormat128bppRGBAFloat, bits_128bppRGBAFloat
+    };
+    static const BYTE bits_1bppIndexed[] = { 85,195,184,85 };
+    static const struct bitmap_data data_1bppIndexed =
+    {
+        1, 32, 1, &GUID_WICPixelFormat1bppIndexed, bits_1bppIndexed
+    };
+    static const BYTE bits_4bppIndexed[] = { 85,195,184,85 };
+    static const struct bitmap_data data_4bppIndexed =
+    {
+        4, 7, 1, &GUID_WICPixelFormat4bppIndexed, bits_4bppIndexed
+    };
+    static const BYTE bits_4bppIndexed_xp[] = { 85,195,184,80 };
+    static const struct bitmap_data data_4bppIndexed_xp =
+    {
+        4, 7, 1, &GUID_WICPixelFormat4bppIndexed, bits_4bppIndexed_xp
+    };
+    static const BYTE bits_8bppIndexed[] = { 1,0,2,3,4,5,6,7,8,9 };
+    static const struct bitmap_data data_8bppIndexed =
+    {
+        8, 3, 1, &GUID_WICPixelFormat8bppIndexed, bits_8bppIndexed
+    };
+    static const BYTE bits_32bppCMYK[] = { 1,0,2,3,4,5,6,7,8,9,0,1 };
+    static const struct bitmap_data data_32bppCMYK =
+    {
+        32, 3, 1, &GUID_WICPixelFormat32bppCMYK, bits_32bppCMYK
+    };
+    static const BYTE bits_64bppCMYK[] = { 1,0,2,3,4,5,6,7,8,9,0,1,2,3,4,5 };
+    static const struct bitmap_data data_64bppCMYK =
+    {
+        64, 2, 1, &GUID_WICPixelFormat64bppCMYK, bits_64bppCMYK
+    };
+    static const struct
+    {
+        int photometric; /* PhotometricInterpretation */
+        int samples; /* SamplesPerPixel */
+        int bps; /* BitsPerSample */
+        const struct bitmap_data *data;
+        const struct bitmap_data *alt_data;
+    } td[] =
+    {
+        /* 2 - RGB */
+        { 2, 3, 1, &data_1bpsBGR },
+        { 2, 3, 4, &data_4bpsBGR },
+        { 2, 3, 8, &data_8bpsBGR },
+        { 2, 3, 16, &data_48bppRGB },
+        { 2, 3, 24, NULL },
+#if 0 /* FIXME */
+        { 2, 3, 32, &data_96bpp3Channels },
+#endif
+        { 2, 4, 1, &data_1bpsBGRA },
+        { 2, 4, 4, &data_4bpsBGRA },
+        { 2, 4, 8, &data_8bpsBGRA },
+        { 2, 4, 16, &data_64bppRGBA },
+        { 2, 4, 24, NULL },
+        { 2, 4, 32, &data_128bppRGBAFloat },
+        /* 1 - BlackIsZero (Bilevel) */
+        { 1, 1, 1, &data_BlackWhite, &data_BlackWhite_xp },
+        { 1, 1, 4, &data_4bppGray, &data_4bppGray_xp },
+        { 1, 1, 8, &data_8bppGray },
+        { 1, 1, 16, &data_16bppGray },
+        { 1, 1, 24, NULL },
+        { 1, 1, 32, &data_32bppGrayFloat },
+        /* 3 - Palette Color */
+        { 3, 1, 1, &data_1bppIndexed },
+        { 3, 1, 4, &data_4bppIndexed, &data_4bppIndexed_xp },
+        { 3, 1, 8, &data_8bppIndexed },
+#if 0 /* FIXME: for some reason libtiff replaces photometric 3 by 1 for bps > 8 */
+        { 3, 1, 16, &data_8bppIndexed },
+        { 3, 1, 24, &data_8bppIndexed },
+        { 3, 1, 32, &data_8bppIndexed },
+#endif
+        /* 5 - Separated */
+        { 5, 4, 1, NULL },
+        { 5, 4, 4, NULL },
+        { 5, 4, 8, &data_32bppCMYK },
+        { 5, 4, 16, &data_64bppCMYK },
+        { 5, 4, 24, NULL },
+        { 5, 4, 32, NULL },
+    };
+    BYTE buf[sizeof(tiff_1x1_data)];
+    BYTE pixels[256];
+    HRESULT hr;
+    IWICBitmapDecoder *decoder;
+    IWICBitmapFrameDecode *frame;
+    GUID format;
+    UINT count, i, bpp, channels, ret;
+    BOOL trasparency;
+    struct IFD_entry *tag, *tag_photo = NULL, *tag_bps = NULL, *tag_samples = NULL, *tag_colormap = NULL;
+    struct IFD_entry *tag_width = NULL, *tag_height = NULL;
+    short *bps;
+
+    memcpy(buf, &tiff_1x1_data, sizeof(tiff_1x1_data));
+    generate_tiff_palette(buf + FIELD_OFFSET(struct tiff_1x1_data, palette_data), 256);
+
+    count = *(short *)(buf + tiff_1x1_data.dir_offset);
+    tag = (struct IFD_entry *)(buf + tiff_1x1_data.dir_offset + sizeof(short));
+
+    /* verify the TIFF structure */
+    for (i = 0; i < count; i++)
+    {
+        if (tag[i].id == 0x100) /* ImageWidth */
+            tag_width = &tag[i];
+        else if (tag[i].id == 0x101) /* ImageLength */
+            tag_height = &tag[i];
+        else if (tag[i].id == 0x102) /* BitsPerSample */
+            tag_bps = &tag[i];
+        else if (tag[i].id == 0x106) /* PhotometricInterpretation */
+            tag_photo = &tag[i];
+        else if (tag[i].id == 0x115) /* SamplesPerPixel */
+            tag_samples = &tag[i];
+        else if (tag[i].id == 0x140) /* ColorMap */
+            tag_colormap = &tag[i];
+    }
+
+    ok(tag_bps && tag_photo && tag_samples && tag_colormap, "tag 0x102,0x106,0x115 or 0x140 is missing\n");
+    if (!tag_bps || !tag_photo || !tag_samples || !tag_colormap) return;
+
+    ok(tag_bps->type == IFD_SHORT, "tag 0x102 should have type IFD_SHORT\n");
+    bps = (short *)(buf + tag_bps->value);
+    ok(bps[0] == 8 && bps[1] == 8 && bps[2] == 8 && bps[3] == 0,
+       "expected bps 8,8,8,0 got %d,%d,%d,%d\n", bps[0], bps[1], bps[2], bps[3]);
+
+    for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
+    {
+        if (td[i].data)
+        {
+            bpp = td[i].samples * td[i].bps;
+            if (winetest_debug > 1)
+                trace("samples %u, bps %u, bpp %u, width %u => width_bytes %u\n", td[i].samples, td[i].bps, bpp,
+                      td[i].data->width, width_bytes(td[i].data->width, bpp));
+            tag_width->value = td[i].data->width;
+            tag_height->value = td[i].data->height;
+        }
+        else
+        {
+            tag_width->value = 1;
+            tag_height->value = 1;
+        }
+
+        tag_colormap->count = (1 << td[i].bps) * 3;
+
+        if (td[i].bps < 8)
+        {
+            buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data)] = 0x55;
+            buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 1] = 0xc3;
+            buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 2] = 0xb8;
+            buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 3] = 0x55;
+        }
+        else
+        {
+            buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data)] = 1;
+            buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 1] = 0;
+            buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 2] = 2;
+            buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 3] = 3;
+        }
+
+        tag_photo->value = td[i].photometric;
+        tag_bps->count = td[i].samples;
+        tag_samples->value = td[i].samples;
+
+        if (td[i].samples == 1)
+            tag_bps->value = td[i].bps;
+        else if (td[i].samples == 2)
+            tag_bps->value = MAKELONG(td[i].bps, td[i].bps);
+        else if (td[i].samples == 3)
+        {
+            tag_bps->value = (BYTE *)bps - buf;
+            bps[0] = bps[1] = bps[2] = td[i].bps;
+        }
+        else if (td[i].samples == 4)
+        {
+            tag_bps->value = (BYTE *)bps - buf;
+            bps[0] = bps[1] = bps[2] = bps[3] = td[i].bps;
+        }
+        else
+        {
+            ok(0, "%u: unsupported samples count %d\n", i, td[i].samples);
+            continue;
+        }
+
+        hr = create_decoder(buf, sizeof(buf), &decoder);
+        if (!td[i].data)
+        {
+            ok(hr == WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT || hr == WINCODEC_ERR_COMPONENTNOTFOUND /* win8+ */ || WINCODEC_ERR_BADIMAGE /* XP */,
+               "%u: (%d,%d,%d) wrong error %#x\n", i, td[i].photometric, td[i].samples, td[i].bps, hr);
+            if (hr == S_OK)
+            {
+                IWICBitmapDecoder_Release(decoder);
+                dump_tiff(buf);
+            }
+            continue;
+        }
+        else
+            ok(hr == S_OK || broken(hr == WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT || hr == WINCODEC_ERR_BADIMAGE) /* XP */,
+               "%u: failed to load TIFF image data (%d,%d,%d) %#x\n",
+               i, td[i].photometric, td[i].samples, td[i].bps, hr);
+        if (hr != S_OK) continue;
+
+        hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
+        ok(hr == S_OK, "%u: GetFrame error %#x\n", i, hr);
+
+        hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
+        ok(hr == S_OK, "%u: GetPixelFormat error %#x\n", i, hr);
+        ok(IsEqualGUID(&format, td[i].data->format),
+           "%u (%d,%d,%d): expected %s, got %s\n",
+            i, td[i].photometric, td[i].samples, td[i].bps,
+            wine_dbgstr_guid(td[i].data->format), wine_dbgstr_guid(&format));
+
+        trasparency = (td[i].photometric == 2 && td[i].samples == 4); /* for XP */
+        hr = get_pixelformat_info(&format, &bpp, &channels, &trasparency);
+        ok(hr == S_OK, "%u: get_pixelformat_bpp error %#x\n", i, hr);
+        ok(bpp == td[i].data->bpp, "%u: expected %u, got %u\n", i, td[i].data->bpp, bpp);
+        ok(channels == td[i].samples, "%u: expected %u, got %u\n", i, td[i].samples, channels);
+        ok(trasparency == (td[i].photometric == 2 && td[i].samples == 4), "%u: got %u\n", i, trasparency);
+
+        memset(pixels, 0, sizeof(pixels));
+        hr = IWICBitmapFrameDecode_CopyPixels(frame, NULL, width_bytes(td[i].data->width, bpp), sizeof(pixels), pixels);
+        ok(hr == S_OK, "%u: CopyPixels error %#x\n", i, hr);
+        ret = memcmp(pixels, td[i].data->bits, width_bytes(td[i].data->width, bpp));
+        if (ret && td[i].alt_data)
+            ret = memcmp(pixels, td[i].alt_data->bits, width_bytes(td[i].data->width, bpp));
+        ok(ret == 0, "%u: (%d,%d,%d) wrong pixel data\n", i, td[i].photometric, td[i].samples, td[i].bps);
+        if (ret)
+        {
+            UINT j, n = width_bytes(td[i].data->width, bpp);
+            for (j = 0; j < n; j++)
+                printf("%u%s", pixels[j], (j + 1) < n ? "," : "\n");
+        }
+
+        IWICBitmapFrameDecode_Release(frame);
+        IWICBitmapDecoder_Release(decoder);
+    }
+}
+
 START_TEST(tiffformat)
 {
     HRESULT hr;
@@ -754,6 +1190,7 @@ START_TEST(tiffformat)
     ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
     if (FAILED(hr)) return;
 
+    test_color_formats();
     test_tiff_1bpp_palette();
     test_tiff_8bpp_palette();
     test_QueryCapability();
-- 
1.9.1

