diff --git a/Tests/images/16bit.r.tif b/Tests/images/16bit.r.tif new file mode 100644 index 00000000000..0f3996e95eb Binary files /dev/null and b/Tests/images/16bit.r.tif differ diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index d8d5753f6db..a94e4dc337e 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -225,6 +225,15 @@ def test_big_endian(self): assert b[0] == ord(b"\x01") assert b[1] == ord(b"\xe0") + def test_16bit_r(self): + with Image.open("Tests/images/16bit.r.tif") as im: + assert im.getpixel((0, 0)) == 480 + assert im.mode == "I;16" + + b = im.tobytes() + assert b[0] == ord(b"\xe0") + assert b[1] == ord(b"\x01") + def test_16bit_s(self): with Image.open("Tests/images/16bit.s.tif") as im: im.load() diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 3b70512605f..4165fabfcb0 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -175,6 +175,7 @@ (II, 1, (1,), 1, (12,), ()): ("I;16", "I;12"), (II, 1, (1,), 1, (16,), ()): ("I;16", "I;16"), (MM, 1, (1,), 1, (16,), ()): ("I;16B", "I;16B"), + (II, 1, (1,), 2, (16,), ()): ("I;16", "I;16R"), (II, 1, (2,), 1, (16,), ()): ("I", "I;16S"), (MM, 1, (2,), 1, (16,), ()): ("I", "I;16BS"), (II, 0, (3,), 1, (32,), ()): ("F", "F;32F"), diff --git a/src/libImaging/Unpack.c b/src/libImaging/Unpack.c index 4f9838fa8f6..fc7568a2c4b 100644 --- a/src/libImaging/Unpack.c +++ b/src/libImaging/Unpack.c @@ -1124,6 +1124,16 @@ unpackI16N_I16(UINT8 *out, const UINT8 *in, int pixels) { tmp += 2; } } +static void +unpackI16R_I16(UINT8 *out, const UINT8 *in, int pixels) { + int i; + for (i = 0; i < pixels; i++) { + out[0] = BITFLIP[in[0]]; + out[1] = BITFLIP[in[1]]; + in += 2; + out += 2; + } +} static void unpackI12_I16(UINT8 *out, const UINT8 *in, int pixels) { @@ -1731,6 +1741,8 @@ static struct { {"I;16L", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian. {"I;16B", "I;16N", 16, unpackI16N_I16B}, + {"I;16", "I;16R", 16, unpackI16R_I16}, + {"I;16", "I;12", 12, unpackI12_I16}, // 12 bit Tiffs stored in 16bits. {NULL} /* sentinel */