test_ppm_load.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2019 Matt Land 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 """ 23 `adafruit_imageload.tests.test_ppm_load` 24 ==================================================== 25 26 * Author(s): Matt Land 27 28 """ 29 import os 30 from io import BytesIO 31 from unittest import TestCase 32 from adafruit_imageload import pnm 33 from adafruit_imageload.pnm.ppm_ascii import read_three_colors 34 from .displayio_shared_bindings import Bitmap_C_Interface, Palette_C_Interface 35 36 37 class TestPpmLoad(TestCase): 38 def test_load_works_p3_ascii(self): 39 test_file = os.path.join( 40 os.path.dirname(__file__), 41 "..", 42 "examples", 43 "images", 44 "netpbm_p3_rgb_ascii.ppm", 45 ) 46 with open(test_file, "rb") as file: 47 bitmap, palette = pnm.load( 48 file, b"P3", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface 49 ) # type: Bitmap_C_Interface, Palette_C_Interface 50 51 self.assertTrue(isinstance(palette, Palette_C_Interface)) 52 self.assertEqual(6, palette.num_colors) 53 palette.validate() 54 # self.fail(str(palette)) 55 self.assertTrue(isinstance(bitmap, Bitmap_C_Interface), bitmap) 56 self.assertEqual(6, bitmap.colors) 57 self.assertEqual(16, bitmap.width) 58 self.assertEqual(16, bitmap.height) 59 bitmap.validate() 60 61 def test_load_works_p6_binary(self): 62 test_file = os.path.join( 63 os.path.dirname(__file__), 64 "..", 65 "examples", 66 "images", 67 "netpbm_p6_binary.ppm", 68 ) 69 with open(test_file, "rb") as file: 70 bitmap, palette = pnm.load( 71 file, b"P6", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface 72 ) # type: Bitmap_C_Interface, Palette_C_Interface 73 self.assertEqual(6, palette.num_colors) 74 palette.validate() 75 self.assertTrue(isinstance(bitmap, Bitmap_C_Interface), bitmap) 76 self.assertEqual(6, bitmap.colors) 77 self.assertEqual(16, bitmap.width) 78 self.assertEqual(16, bitmap.height) 79 bitmap.validate() 80 81 def test_load_three_colors_tail(self): 82 buffer = BytesIO(b"211 222 233") 83 for i in read_three_colors(buffer): 84 self.assertEqual(b"\xd3\xde\xe9", i) 85 86 def test_load_three_colors_middle(self): 87 buffer = BytesIO(b"0 128 255 45 55 25") 88 for i in iter(read_three_colors(buffer)): 89 self.assertEqual(b"\x00\x80\xff", i) 90 break