@@ -57,10 +57,17 @@ def __str__(self):
5757 for y in range (self .height ):
5858 for x in range (self .width ):
5959 data = self [x , y ]
60- out += f"{ data } "
60+ out += f"{ data } "
6161 out += "\n "
6262 return out
6363
64+ class Palette_C_Interface (object ):
65+ def __init__ (self , num_colors ):
66+ self .num_colors = num_colors
67+ self .colors = {}
68+
69+ def __setitem__ (self , key , value ):
70+ self .colors [key ] = value
6471
6572logging .getLogger ().setLevel (logging .INFO )
6673
@@ -136,6 +143,26 @@ def test_decode(self):
136143 encoded = b ._abs_pos (3 , 3 )
137144 self .assertEqual ((3 , 3 ), b ._decode (encoded ))
138145
146+ class testPgmLoad (TestCase ):
147+ def test_load_works_p1_ascii (self ):
148+ test_file = os .path .join (
149+ os .path .dirname (__file__ ),
150+ ".." ,
151+ ".." ,
152+ "examples" ,
153+ "images" ,
154+ "netpbm_p2_ascii.pgm" ,
155+ )
156+ with open (test_file , "rb" ) as f :
157+ bitmap , palette = pnm .load (f , b"P2" , bitmap = Bitmap_C_Interface , palette = Palette_C_Interface )
158+ self .assertTrue (isinstance (bitmap , Bitmap_C_Interface ), bitmap )
159+ self .assertEqual (255 , bitmap .colors )
160+ self .assertEqual (8 , bitmap .width )
161+ self .assertEqual (8 , bitmap .height )
162+ bitmap .validate ()
163+ self .fail (str (bitmap ))
164+
165+
139166
140167class TestPnmLoad (TestCase ):
141168 def test_load_fails_with_no_header_data (self ):
0 commit comments