|
| 1 | +import io |
| 2 | +import unittest |
| 3 | + |
| 4 | +from tivars.types.picture import * |
| 5 | + |
| 6 | +try: |
| 7 | + from PIL import Image |
| 8 | + from tivars.PIL import * |
| 9 | + |
| 10 | +except ImportError: |
| 11 | + raise unittest.SkipTest("PIL not installed") |
| 12 | + |
| 13 | + |
| 14 | +try: |
| 15 | + import numpy as np |
| 16 | + |
| 17 | +except ImportError: |
| 18 | + raise unittest.SkipTest("NumPy not installed") |
| 19 | + |
| 20 | + |
| 21 | +class PILTests(unittest.TestCase): |
| 22 | + def test_8xi(self): |
| 23 | + ti_img = TIMonoPicture() |
| 24 | + ti_img.open("tests/data/var/BartSimpson.8xi") |
| 25 | + |
| 26 | + arr = np.asarray(ti_img.array(), dtype=np.uint8) |
| 27 | + img = Image.open("tests/data/var/BartSimpson.8xi") |
| 28 | + |
| 29 | + self.assertEqual((np.asarray(Image.fromarray(arr, mode=ti_img.pil_mode)) == |
| 30 | + np.asarray(img)).all(), True) |
| 31 | + |
| 32 | + img.save(buf := io.BytesIO(), "8xi") |
| 33 | + buf.seek(0) |
| 34 | + |
| 35 | + self.assertEqual(buf.read()[72:-2], ti_img.calc_data) |
| 36 | + |
| 37 | + def test_8ci(self): |
| 38 | + def the_palette_is_stinky(byte: int) -> bytes: |
| 39 | + high, low = byte // 16, byte % 16 |
| 40 | + |
| 41 | + high *= high != 11 |
| 42 | + low *= low != 11 |
| 43 | + |
| 44 | + return bytes([16 * high + low]) |
| 45 | + |
| 46 | + ti_img = TIPicture() |
| 47 | + ti_img.open("tests/data/var/Pic1.8ci") |
| 48 | + |
| 49 | + arr = np.asarray(ti_img.array(), dtype=np.uint8) |
| 50 | + img = Image.open("tests/data/var/Pic1.8ci") |
| 51 | + |
| 52 | + self.assertEqual((np.asarray(Image.fromarray(arr, mode=ti_img.pil_mode)) == |
| 53 | + np.asarray(img)).all(), True) |
| 54 | + |
| 55 | + img.save(buf := io.BytesIO(), "8ci") |
| 56 | + buf.seek(0) |
| 57 | + |
| 58 | + trans = b"".join(map(the_palette_is_stinky, range(256))) |
| 59 | + self.assertEqual(buf.read()[72:-2].translate(trans), ti_img.calc_data.translate(trans)) |
| 60 | + |
| 61 | + def test_8ca(self): |
| 62 | + ti_img = TIImage() |
| 63 | + ti_img.open("tests/data/var/Image1.8ca") |
| 64 | + |
| 65 | + arr = np.asarray(ti_img.array(), dtype=np.uint8) |
| 66 | + img = Image.open("tests/data/var/Image1.8ca") |
| 67 | + |
| 68 | + self.assertEqual((np.asarray(Image.fromarray(arr, mode=ti_img.pil_mode)) == |
| 69 | + np.asarray(img)).all(), True) |
0 commit comments