/ tests / test_pgm_load.py
test_pgm_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_bgm_load`
24  ====================================================
25  
26  * Author(s):  Matt Land
27  
28  """
29  import os
30  from unittest import TestCase
31  from adafruit_imageload import pnm
32  from .displayio_shared_bindings import Bitmap_C_Interface, Palette_C_Interface
33  
34  
35  class TestPgmLoad(TestCase):
36      def test_load_works_p2_ascii(self):
37          test_file = os.path.join(
38              os.path.dirname(__file__),
39              "..",
40              "examples",
41              "images",
42              "netpbm_p2_ascii.pgm",
43          )
44          with open(test_file, "rb") as file:
45              bitmap, palette = pnm.load(
46                  file, b"P2", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
47              )
48          self.assertTrue(isinstance(bitmap, Bitmap_C_Interface), bitmap)
49          self.assertEqual(6, bitmap.colors)
50          self.assertEqual(8, bitmap.width)
51          self.assertEqual(8, bitmap.height)
52          bitmap.validate()
53          self.assertEqual(6, palette.num_colors)
54          palette.validate()
55          # self.fail(str(palette))
56  
57      def test_load_works_p5_binary(self):
58          test_file = os.path.join(
59              os.path.dirname(__file__),
60              "..",
61              "examples",
62              "images",
63              "netpbm_p5_binary.pgm",
64          )
65          with open(test_file, "rb") as file:
66              bitmap, palette = pnm.load(
67                  file, b"P5", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
68              )
69          self.assertTrue(isinstance(bitmap, Bitmap_C_Interface), bitmap)
70  
71          self.assertEqual(8, palette.num_colors)
72          palette.validate()
73          self.assertEqual(8, bitmap.colors)
74          self.assertEqual(8, bitmap.width)
75          self.assertEqual(8, bitmap.height)
76          bitmap.validate()
77          # self.fail(str(bitmap))