/ tests / test_pbm_load.py
test_pbm_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_pbm_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.pbm_binary import iterbits, reverse
 34  from .displayio_shared_bindings import Bitmap_C_Interface, Palette_C_Interface
 35  
 36  
 37  class TestPbmLoad(TestCase):
 38      def test_load_fails_with_no_header_data(self):
 39          file = BytesIO(b"some initial binary data: \x00\x01")
 40          try:
 41              pnm.load(
 42                  file, b"P1", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
 43              )
 44              self.fail("should have failed")
 45          except Exception as caught_exception:
 46              if "Unsupported image format" not in str(caught_exception):
 47                  raise
 48  
 49      def test_load_works_p1_ascii(self):
 50          test_file = os.path.join(
 51              os.path.dirname(__file__),
 52              "..",
 53              "examples",
 54              "images",
 55              "netpbm_p1_mono_ascii.pbm",
 56          )
 57          with open(test_file, "rb") as file:
 58              bitmap, palette = pnm.load(
 59                  file, b"P1", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
 60              )
 61          self.assertTrue(isinstance(bitmap, Bitmap_C_Interface), bitmap)
 62          self.assertEqual(1, bitmap.colors)
 63          self.assertEqual(13, bitmap.width)
 64          self.assertEqual(21, bitmap.height)
 65  
 66          bitmap.validate()
 67          self.assertEqual(0, bitmap[0])  # check first row
 68          self.assertEqual(1, bitmap[11, 1])  # check second row
 69  
 70          self.assertEqual(1, palette.num_colors)
 71          palette.validate()
 72  
 73      def test_load_works_p4_in_mem(self):
 74          file = BytesIO(b"P4\n4 2\n\x55")
 75          bitmap, palette = pnm.load(
 76              file, b"P4", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
 77          )
 78          self.assertEqual(4, bitmap.width)
 79          self.assertEqual(2, bitmap.height)
 80          bitmap.validate()
 81          self.assertEqual("\n   0   1   0   1\n   0   1   0   1\n", str(bitmap))
 82          palette.validate()
 83  
 84      def test_load_works_p4_binary(self):
 85          test_file = os.path.join(
 86              os.path.dirname(__file__),
 87              "..",
 88              "examples",
 89              "images",
 90              "netpbm_p4_mono_binary.pbm",
 91          )
 92          with open(test_file, "rb") as file:
 93              bitmap, palette = pnm.load(
 94                  file, b"P4", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
 95              )
 96          self.assertEqual(1, palette.num_colors)
 97          palette.validate()
 98          self.assertEqual(b"\xff\xff\xff", palette[0])
 99          self.assertTrue(isinstance(bitmap, Bitmap_C_Interface))
100          self.assertEqual(1, bitmap.colors)
101          self.assertEqual(32, bitmap.width)
102          self.assertEqual(15, bitmap.height)
103          bitmap.validate()
104  
105      def test_load_works_p4_binary_high_res(self):
106          test_file = os.path.join(
107              os.path.dirname(__file__),
108              "..",
109              "examples",
110              "images",
111              "netpbm_p4_mono_large.pbm",
112          )
113          with open(test_file, "rb") as file:
114              bitmap, palette = pnm.load(
115                  file, b"P4", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
116              )
117          self.assertTrue(isinstance(bitmap, Bitmap_C_Interface))
118          self.assertEqual(1, bitmap.colors)
119          self.assertEqual(320, bitmap.width)
120          self.assertEqual(240, bitmap.height)
121          bitmap.validate()
122          self.assertEqual(1, palette.num_colors)
123          palette.validate()
124  
125      def test_iterbits(self):
126          k = b"k"
127          bits = []
128          for byte in iterbits(k):
129              bits.append(byte)
130          # self.assertEqual([0,1,1,0,1,0,1,1], bits[::-1])
131          self.assertEqual([0, 1, 1, 0, 1, 0, 1, 1], bits)
132  
133      def test_reverse(self):
134          # 00110100 to 00101100
135          self.assertEqual(reverse(0x34), 0x2C)
136          self.assertEqual(reverse(0xFF), 0xFF)
137          self.assertEqual(reverse(0x00), 0x00)
138          self.assertEqual(reverse(0x0E), 0x70)