From ebdb47e693d266ccd80874321d6a705b9fc94bfe Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 20 Mar 2022 13:34:48 +1100 Subject: [PATCH] Support reading I;16R TIFF images --- Tests/images/16bit.r.tif | Bin 0 -> 8502 bytes Tests/test_file_tiff.py | 9 +++++++++ src/PIL/TiffImagePlugin.py | 1 + src/libImaging/Unpack.c | 12 ++++++++++++ 4 files changed, 22 insertions(+) create mode 100644 Tests/images/16bit.r.tif diff --git a/Tests/images/16bit.r.tif b/Tests/images/16bit.r.tif new file mode 100644 index 0000000000000000000000000000000000000000..0f3996e95eb284db22889658455e5fa15363ddb8 GIT binary patch literal 8502 zcmYk>4@_Uzbq8>M_DqYru;R*0xC;%9f(n`4;VM+1mi=gCchHv3sX}F|K_XSya9buk z8Of|=mqhl4t9l}xnQYZ)zjmg>YS>5@CVLAK?ZSrFGLb1rMk^KF%w(#d;m%BE%WwOf zS5FF0zxVFD_uO;7=X=im^Db>|^(F6oq@?5>>G#s_q~A)t>2x}hrqfI5wX~Mbq=&r! zEPdVkg|sJqD|M%jrYF;9Qd7F`?L%pI>PeTF=PE7HU471BCvJ*k%FKKd%yU8MaIt0&V81gFvu(%sZRS0S~q zy2;)5p_4rhn)CI~->}z+M}Go2IoiNd6A~B3hFq*LgL}LJR*!Fx>;$ zHm-Y9DOOs^kLo#znI&8_rYabVID9X^f0SM&cVFFe>>O0COV7}<$&0r1d6rIR?R_kq zNiUFW!A67MB&1hl&RKf9yp6GOAEsew!qYiXdqQp$<-i&wH9VhyRoWIOfAL41(5+qii}Bm&zd{cF@!_9Y_ zmc?eA{gZ4SmM2HmpHJVzP))jt_y1<$Nl|~n{faDW(4$;bE@mDsz_cnK-jX3htbW%x zI8OH_R7KCPz&0)KX0sfqW@|y#UDo#)q(|Y7y1jlaB(`}U+3+SWJ6S&^+8<7TA=dA~ z!V$hS$od|cGs>TFvbWgmC4Z6q1y-ved^TN^I~{C{@nVks`*?T=TTAInWNKKNOCO_a z+gp*p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/test_file_tiff.py b/Tests/test_file_tiff.py index d8d5753f6..a94e4dc33 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -225,6 +225,15 @@ class TestFileTiff: 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 3b7051260..4165fabfc 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -175,6 +175,7 @@ OPEN_INFO = { (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 4f9838fa8..fc7568a2c 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 */