From accc66fb5e4213c8a20edcf7a6a82f5eeb1a4515 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 5 Sep 2018 17:45:03 +0300 Subject: [PATCH] add support for RGBaXX and RGBaX raw tiff modes --- Tests/test_lib_pack.py | 10 +++++++-- src/PIL/TiffImagePlugin.py | 4 ++++ src/libImaging/Unpack.c | 44 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/Tests/test_lib_pack.py b/Tests/test_lib_pack.py index 921af3bea..d4e50b0ab 100644 --- a/Tests/test_lib_pack.py +++ b/Tests/test_lib_pack.py @@ -280,8 +280,14 @@ class TestLibUnpack(PillowTestCase): self.assert_unpack("RGBA", "RGBa", 4, (63, 127, 191, 4), (159, 191, 223, 8), (191, 212, 233, 12)) self.assert_unpack("RGBA", "RGBa", - b'\x01\x02\x03\x00\x10\x20\x30\xff', - (0, 0, 0, 0), (16, 32, 48, 255)) + b'\x01\x02\x03\x00\x10\x20\x30\x7f\x10\x20\x30\xff', + (0, 0, 0, 0), (32, 64, 96, 127), (16, 32, 48, 255)) + self.assert_unpack("RGBA", "RGBaX", + b'\x01\x02\x03\x00-\x10\x20\x30\x7f-\x10\x20\x30\xff-', + (0, 0, 0, 0), (32, 64, 96, 127), (16, 32, 48, 255)) + self.assert_unpack("RGBA", "RGBaXX", + b'\x01\x02\x03\x00==\x10\x20\x30\x7f!!\x10\x20\x30\xff??', + (0, 0, 0, 0), (32, 64, 96, 127), (16, 32, 48, 255)) self.assert_unpack("RGBA", "RGBa;16L", 8, (63, 127, 191, 8), (159, 191, 223, 16), (191, 212, 233, 24)) self.assert_unpack("RGBA", "RGBa;16L", diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 17b133c82..e8ea365e8 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -208,6 +208,10 @@ OPEN_INFO = { (MM, 2, (1,), 1, (8, 8, 8, 8, 8, 8), (0, 0, 0)): ("RGBX", "RGBXXX"), (II, 2, (1,), 1, (8, 8, 8, 8), (1,)): ("RGBA", "RGBa"), (MM, 2, (1,), 1, (8, 8, 8, 8), (1,)): ("RGBA", "RGBa"), + (II, 2, (1,), 1, (8, 8, 8, 8, 8), (1, 0)): ("RGBA", "RGBaX"), + (MM, 2, (1,), 1, (8, 8, 8, 8, 8), (1, 0)): ("RGBA", "RGBaX"), + (II, 2, (1,), 1, (8, 8, 8, 8, 8, 8), (1, 0, 0)): ("RGBA", "RGBaXX"), + (MM, 2, (1,), 1, (8, 8, 8, 8, 8, 8), (1, 0, 0)): ("RGBA", "RGBaXX"), (II, 2, (1,), 1, (8, 8, 8, 8), (2,)): ("RGBA", "RGBA"), (MM, 2, (1,), 1, (8, 8, 8, 8), (2,)): ("RGBA", "RGBA"), (II, 2, (1,), 1, (8, 8, 8, 8, 8), (2, 0)): ("RGBA", "RGBAX"), diff --git a/src/libImaging/Unpack.c b/src/libImaging/Unpack.c index 04e05dcb3..7bb8064ee 100644 --- a/src/libImaging/Unpack.c +++ b/src/libImaging/Unpack.c @@ -797,6 +797,48 @@ unpackRGBa(UINT8* _out, const UINT8* in, int pixels) } } +static void +unpackRGBaskip1(UINT8* _out, const UINT8* in, int pixels) +{ + int i; + UINT32* out = (UINT32*) _out; + /* premultiplied RGBA */ + for (i = 0; i < pixels; i++) { + int a = in[3]; + if ( ! a) { + out[i] = 0; + } else if (a == 255) { + out[i] = MAKE_UINT32(in[0], in[1], in[2], a); + } else { + out[i] = MAKE_UINT32(CLIP8(in[0] * 255 / a), + CLIP8(in[1] * 255 / a), + CLIP8(in[2] * 255 / a), a); + } + in += 5; + } +} + +static void +unpackRGBaskip2(UINT8* _out, const UINT8* in, int pixels) +{ + int i; + UINT32* out = (UINT32*) _out; + /* premultiplied RGBA */ + for (i = 0; i < pixels; i++) { + int a = in[3]; + if ( ! a) { + out[i] = 0; + } else if (a == 255) { + out[i] = MAKE_UINT32(in[0], in[1], in[2], a); + } else { + out[i] = MAKE_UINT32(CLIP8(in[0] * 255 / a), + CLIP8(in[1] * 255 / a), + CLIP8(in[2] * 255 / a), a); + } + in += 6; + } +} + static void unpackBGRa(UINT8* _out, const UINT8* in, int pixels) { @@ -1304,6 +1346,8 @@ static struct { {"RGBA", "RGBAX", 40, copy4skip1}, {"RGBA", "RGBAXX", 48, copy4skip2}, {"RGBA", "RGBa", 32, unpackRGBa}, + {"RGBA", "RGBaX", 40, unpackRGBaskip1}, + {"RGBA", "RGBaXX", 48, unpackRGBaskip2}, {"RGBA", "RGBa;16L", 64, unpackRGBa16L}, {"RGBA", "RGBa;16B", 64, unpackRGBa16B}, {"RGBA", "BGRa", 32, unpackBGRa},