Packing tests, first part. Fix RGBX packers

This commit is contained in:
Alexander 2017-08-24 00:50:17 +03:00
parent 2439c9b2db
commit 8cb9cba1e0
2 changed files with 104 additions and 32 deletions

View File

@ -9,10 +9,6 @@ X = 255
class TestLibPack(PillowTestCase): class TestLibPack(PillowTestCase):
def pack(self):
pass # not yet
def test_pack(self): def test_pack(self):
def pack(mode, rawmode, in_data=(1, 2, 3, 4)): def pack(mode, rawmode, in_data=(1, 2, 3, 4)):
@ -28,40 +24,116 @@ class TestLibPack(PillowTestCase):
order = 1 if Image._ENDIAN == '<' else -1 order = 1 if Image._ENDIAN == '<' else -1
self.assertEqual(pack("1", "1"), [128])
self.assertEqual(pack("1", "1;I"), [0])
self.assertEqual(pack("1", "1;R"), [1])
self.assertEqual(pack("1", "1;IR"), [0])
self.assertEqual(pack("L", "L"), [1])
self.assertEqual(pack("I", "I"), [1, 0, 0, 0][::order]) self.assertEqual(pack("I", "I"), [1, 0, 0, 0][::order])
self.assertEqual(pack("F", "F"), [0, 0, 128, 63][::order]) self.assertEqual(pack("F", "F"), [0, 0, 128, 63][::order])
self.assertEqual(pack("LA", "LA"), [1, 2])
self.assertEqual(pack("RGB", "RGB"), [1, 2, 3])
self.assertEqual(pack("RGB", "RGB;L"), [1, 2, 3])
self.assertEqual(pack("RGB", "BGR"), [3, 2, 1])
self.assertEqual(pack("RGB", "RGBX"), [1, 2, 3, 255]) # 255?
self.assertEqual(pack("RGB", "BGRX"), [3, 2, 1, 0])
self.assertEqual(pack("RGB", "XRGB"), [0, 1, 2, 3])
self.assertEqual(pack("RGB", "XBGR"), [0, 3, 2, 1])
self.assertEqual(pack("RGBX", "RGBX"), [1, 2, 3, 4]) # 4->255?
self.assertEqual(pack("RGBA", "RGBA"), [1, 2, 3, 4])
self.assertEqual(pack("RGBA", "BGRa"), [0, 0, 0, 4])
self.assertEqual(pack("RGBA", "BGRa", in_data=(20, 30, 40, 50)), [8, 6, 4, 50])
self.assertEqual(pack("RGBa", "RGBa"), [1, 2, 3, 4])
self.assertEqual(pack("RGBa", "BGRa"), [3, 2, 1, 4])
self.assertEqual(pack("RGBa", "aBGR"), [4, 3, 2, 1])
self.assertEqual(pack("CMYK", "CMYK"), [1, 2, 3, 4]) self.assertEqual(pack("CMYK", "CMYK"), [1, 2, 3, 4])
self.assertEqual(pack("YCbCr", "YCbCr"), [1, 2, 3]) self.assertEqual(pack("YCbCr", "YCbCr"), [1, 2, 3])
def assert_pack(self, mode, rawmode, data, *pixels):
"""
data - eather raw bytes with data or just number of bytes in rawmode.
"""
im = Image.new(mode, (len(pixels), 1))
for x, pixel in enumerate(pixels):
im.putpixel((x, 0), pixel)
if isinstance(data, (int)):
data_len = data * len(pixels)
data = bytes(bytearray(range(1, data_len + 1)))
self.assertEqual(data, im.tobytes("raw", rawmode))
def test_1(self):
self.assert_pack("1", "1", b'\x01', 0,0,0,0,0,0,0,X)
self.assert_pack("1", "1;I", b'\x01', X,X,X,X,X,X,X,0)
self.assert_pack("1", "1;R", b'\x01', X,0,0,0,0,0,0,0)
self.assert_pack("1", "1;IR", b'\x01', 0,X,X,X,X,X,X,X)
self.assert_pack("1", "1", b'\xaa', X,0,X,0,X,0,X,0)
self.assert_pack("1", "1;I", b'\xaa', 0,X,0,X,0,X,0,X)
self.assert_pack("1", "1;R", b'\xaa', 0,X,0,X,0,X,0,X)
self.assert_pack("1", "1;IR", b'\xaa', X,0,X,0,X,0,X,0)
self.assert_pack("1", "L", b'\xff\x00\x00\xff\x00\x00', X,0,0,X,0,0)
def test_L(self):
self.assert_pack("L", "L", 1, 1,2,3,4)
def test_LA(self):
self.assert_pack("LA", "LA", 2, (1,2), (3,4), (5,6))
self.assert_pack("LA", "LA;L", 2, (1,4), (2,5), (3,6))
def test_P(self):
self.assert_pack("P", "P;1", b'\xe4', 1, 1, 1, 0, 0, 255, 0, 0)
self.assert_pack("P", "P;2", b'\xe4', 3, 2, 1, 0)
self.assert_pack("P", "P;4", b'\x02\xef', 0, 2, 14, 15)
self.assert_pack("P", "P", 1, 1, 2, 3, 4)
def test_PA(self):
self.assert_pack("PA", "PA", 2, (1,2), (3,4), (5,6))
self.assert_pack("PA", "PA;L", 2, (1,4), (2,5), (3,6))
def test_RGB(self):
self.assert_pack("RGB", "RGB", 3, (1,2,3), (4,5,6), (7,8,9))
self.assert_pack("RGB", "RGBX",
b'\x01\x02\x03\xff\x05\x06\x07\xff', (1,2,3), (5,6,7))
self.assert_pack("RGB", "XRGB",
b'\x00\x02\x03\x04\x00\x06\x07\x08', (2,3,4), (6,7,8))
self.assert_pack("RGB", "BGR", 3, (3,2,1), (6,5,4), (9,8,7))
self.assert_pack("RGB", "BGRX",
b'\x01\x02\x03\x00\x05\x06\x07\x00', (3,2,1), (7,6,5))
self.assert_pack("RGB", "XBGR",
b'\x00\x02\x03\x04\x00\x06\x07\x08', (4,3,2), (8,7,6))
self.assert_pack("RGB", "RGB;L", 3, (1,4,7), (2,5,8), (3,6,9))
self.assert_pack("RGB", "R", 1, (1,9,9), (2,9,9), (3,9,9))
self.assert_pack("RGB", "G", 1, (9,1,9), (9,2,9), (9,3,9))
self.assert_pack("RGB", "B", 1, (9,9,1), (9,9,2), (9,9,3))
def test_RGBA(self):
self.assert_pack("RGBA", "RGBA", 4,
(1,2,3,4), (5,6,7,8), (9,10,11,12))
self.assert_pack("RGBA", "RGBA;L", 4,
(1,4,7,10), (2,5,8,11), (3,6,9,12))
self.assert_pack("RGBA", "RGB", 3, (1,2,3,14), (4,5,6,15), (7,8,9,16))
self.assert_pack("RGBA", "BGR", 3, (3,2,1,14), (6,5,4,15), (9,8,7,16))
self.assert_pack("RGBA", "BGRA", 4,
(3,2,1,4), (7,6,5,8), (11,10,9,12))
self.assert_pack("RGBA", "ABGR", 4,
(4,3,2,1), (8,7,6,5), (12,11,10,9))
self.assert_pack("RGBA", "BGRa", 4,
(191,127,63,4), (223,191,159,8), (233,212,191,12))
self.assert_pack("RGBA", "R", 1, (1,0,8,9), (2,0,8,9), (3,0,8,0))
self.assert_pack("RGBA", "G", 1, (6,1,8,9), (6,2,8,9), (6,3,8,9))
self.assert_pack("RGBA", "B", 1, (6,7,1,9), (6,7,2,0), (6,7,3,9))
self.assert_pack("RGBA", "A", 1, (6,7,0,1), (6,7,0,2), (0,7,0,3))
def test_RGBa(self):
self.assert_pack("RGBa", "RGBa", 4,
(1,2,3,4), (5,6,7,8), (9,10,11,12))
self.assert_pack("RGBa", "BGRa", 4,
(3,2,1,4), (7,6,5,8), (11,10,9,12))
self.assert_pack("RGBa", "aBGR", 4,
(4,3,2,1), (8,7,6,5), (12,11,10,9))
def test_RGBX(self):
self.assert_pack("RGBX", "RGBX", 4, (1,2,3,4), (5,6,7,8), (9,10,11,12))
self.assert_pack("RGBX", "RGBX;L", 4, (1,4,7,10), (2,5,8,11), (3,6,9,12))
self.assert_pack("RGBX", "RGB", 3, (1,2,3,X), (4,5,6,X), (7,8,9,X))
self.assert_pack("RGBX", "BGR", 3, (3,2,1,X), (6,5,4,X), (9,8,7,X))
self.assert_pack("RGBX", "BGRX",
b'\x01\x02\x03\x00\x05\x06\x07\x00\t\n\x0b\x00',
(3,2,1,X), (7,6,5,X), (11,10,9,X))
self.assert_pack("RGBX", "XBGR",
b'\x00\x02\x03\x04\x00\x06\x07\x08\x00\n\x0b\x0c',
(4,3,2,X), (8,7,6,X), (12,11,10,X))
self.assert_pack("RGBX", "R", 1, (1,0,8,9), (2,0,8,9), (3,0,8,0))
self.assert_pack("RGBX", "G", 1, (6,1,8,9), (6,2,8,9), (6,3,8,9))
self.assert_pack("RGBX", "B", 1, (6,7,1,9), (6,7,2,0), (6,7,3,9))
self.assert_pack("RGBX", "X", 1, (6,7,0,1), (6,7,0,2), (0,7,0,3))
class TestLibUnpack(PillowTestCase): class TestLibUnpack(PillowTestCase):
def assert_unpack(self, mode, rawmode, data, *pixels): def assert_unpack(self, mode, rawmode, data, *pixels):

View File

@ -559,8 +559,8 @@ static struct {
/* true colour w. padding */ /* true colour w. padding */
{"RGBX", "RGBX", 32, copy4}, {"RGBX", "RGBX", 32, copy4},
{"RGBX", "RGBX;L", 32, packRGBXL}, {"RGBX", "RGBX;L", 32, packRGBXL},
{"RGBX", "RGB", 32, ImagingPackRGB}, {"RGBX", "RGB", 24, ImagingPackRGB},
{"RGBX", "BGR", 32, ImagingPackBGR}, {"RGBX", "BGR", 24, ImagingPackBGR},
{"RGBX", "BGRX", 32, ImagingPackBGRX}, {"RGBX", "BGRX", 32, ImagingPackBGRX},
{"RGBX", "XBGR", 32, ImagingPackXBGR}, {"RGBX", "XBGR", 32, ImagingPackXBGR},
{"RGBX", "R", 8, band0}, {"RGBX", "R", 8, band0},