From efe8479af9cf88743a6fe325ed0c81d9aa9d8d1c Mon Sep 17 00:00:00 2001 From: wiredfool Date: Tue, 19 Sep 2017 10:32:11 +0100 Subject: [PATCH] Additional tests -- access/pack/unpack --- Tests/test_mode_i16.py | 40 ++++++++++++++++++++++++++++++++++++++++ _imaging.c | 14 +++++++++----- libImaging/Pack.c | 11 +++++++++++ libImaging/Unpack.c | 13 ++++++++++++- 4 files changed, 72 insertions(+), 6 deletions(-) diff --git a/Tests/test_mode_i16.py b/Tests/test_mode_i16.py index b150126f0..42d5ed83e 100644 --- a/Tests/test_mode_i16.py +++ b/Tests/test_mode_i16.py @@ -1,7 +1,12 @@ from helper import unittest, PillowTestCase, hopper from PIL import Image +import struct +HAS_PYACCESS = False +try: + from PIL import PyAccess +except: pass class TestModeI16(PillowTestCase): @@ -113,5 +118,40 @@ class TestModeI16(PillowTestCase): self.verify(im.convert("I;16S").convert("I")) + def _test_i16s(self, pixels, data, rawmode): + ct = len(pixels) + im = Image.frombytes('I;16S', + (ct,1), + data, + 'raw', + rawmode) + + self.assertEqual(im.tobytes('raw', rawmode), data) + + + def _test_access(im, pixels): + access = im.load() + if HAS_PYACCESS: + py_access = PyAccess.new(im, im.readonly) + for ix, val in enumerate(pixels): + self.assertEqual(access[(ix, 0)], val) + if HAS_PYACCESS: + self.assertEqual(py_access[(ix, 0)], val) + + _test_access(im, pixels) + _test_access(im.convert('I'), pixels) # lossless + _test_access(im.convert('F'), pixels) # lossless + _test_access(im.convert('L'), (0,0,0,0,1,128,255,255)) #lossy + + def test_i16s(self): + # LE, base Rawmode: + pixels = (-2**15, -2**7, -1, 0, 1, 128, 255, 2**15-1) + ct = len(pixels) + data = struct.pack("<%dh"%ct, *pixels) + self._test_i16s(pixels, data, 'I;16S') + data = struct.pack(">%dh"%ct, *pixels) + self._test_i16s(pixels, data, 'I;16BS') + + if __name__ == '__main__': unittest.main() diff --git a/_imaging.c b/_imaging.c index d0777c73a..5bedbdeaa 100644 --- a/_imaging.c +++ b/_imaging.c @@ -440,10 +440,11 @@ static inline PyObject* getpixel(Imaging im, ImagingAccess access, int x, int y) { union { - UINT8 b[4]; - UINT16 h; - INT32 i; - FLOAT32 f; + UINT8 b[4]; + UINT16 H; + INT16 h; + INT32 i; + FLOAT32 f; } pixel; if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { @@ -471,8 +472,11 @@ getpixel(Imaging im, ImagingAccess access, int x, int y) case IMAGING_TYPE_FLOAT32: return PyFloat_FromDouble(pixel.f); case IMAGING_TYPE_SPECIAL: - if (strncmp(im->mode, "I;16", 4) == 0) + if (strncmp(im->mode, "I;16S", 5) == 0) { return PyInt_FromLong(pixel.h); + } else if (strncmp(im->mode, "I;16", 4) == 0) { + return PyInt_FromLong(pixel.H); + } break; } diff --git a/libImaging/Pack.c b/libImaging/Pack.c index f74e18e1b..5b3e883ea 100644 --- a/libImaging/Pack.c +++ b/libImaging/Pack.c @@ -402,6 +402,16 @@ packI16N_I16(UINT8* out, const UINT8* in, int pixels){ } } +static void +packI16S_I16BS(UINT8* out, const UINT8* in, int pixels){ + int i; + UINT8* tmp = (UINT8*) in; + for (i = 0; i < pixels; i++) { + C16S; + out += 2; tmp += 2; + } +} + static void packI32S(UINT8* out, const UINT8* in, int pixels) @@ -615,6 +625,7 @@ static struct { {"I;16B", "I;16B", 16, copy2}, {"I;16L", "I;16L", 16, copy2}, {"I;16S", "I;16S", 16, copy2}, + {"I;16S", "I;16BS", 16, packI16S_I16BS}, {"I;16", "I;16N", 16, packI16N_I16}, // LibTiff native->image endian. {"I;16L", "I;16N", 16, packI16N_I16}, {"I;16B", "I;16N", 16, packI16N_I16B}, diff --git a/libImaging/Unpack.c b/libImaging/Unpack.c index 183193983..e1a3e7845 100644 --- a/libImaging/Unpack.c +++ b/libImaging/Unpack.c @@ -966,6 +966,16 @@ unpackI16N_I16(UINT8* out, const UINT8* in, int pixels){ } } +static void +unpackI16BS_I16S(UINT8* out, const UINT8* in, int pixels){ + int i; + UINT8* tmp = (UINT8*) out; + for (i = 0; i < pixels; i++) { + C16S; + in += 2; tmp += 2; + } +} + static void unpackI12_I16(UINT8* out, const UINT8* in, int pixels){ /* Fillorder 1/MSB -> LittleEndian, for 12bit integer greyscale tiffs. @@ -1406,7 +1416,8 @@ static struct { {"I;16", "I;16", 16, copy2}, {"I;16B", "I;16B", 16, copy2}, {"I;16L", "I;16L", 16, copy2}, - {"I;16S", "I;16S", 16, copy2}, + {"I;16S", "I;16S", 16, copy2}, + {"I;16S", "I;16BS", 16, unpackI16BS_I16S}, {"I;16", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian. {"I;16L", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian.