1
+ from unittest import TestCase
2
+ from adafruit_imageload import pnm
3
+ from io import BytesIO
4
+ import logging
5
+ import os
6
+
7
+ class Bitmap (object ):
8
+ def __init__ (self , width , height , colors ):
9
+ self .width = width
10
+ self .height = height
11
+ self .colors = colors
12
+
13
+ logging .getLogger ().setLevel (logging .INFO )
14
+
15
+ class TestPbmP1 (TestCase ):
16
+
17
+ def test_load_fails_with_no_header_data (self ):
18
+ f = BytesIO (b"some initial binary data: \x00 \x01 " )
19
+ try :
20
+ pnm .load (f , b'P1' )
21
+ self .fail ('should have failed' )
22
+ except Exception as e :
23
+ if "Unsupported image format" not in str (e ):
24
+ raise
25
+
26
+ def _test_load_works_p1_ascii (self ):
27
+ test_file = os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'examples' , 'images' , 'netpbm_p1_mono.pbm' )
28
+ with open (test_file ) as f :
29
+ f .seek (2 )
30
+ bitmap , palette = pnm .load (f , b'P1' )
31
+ self .assertTrue (isinstance (bitmap , Bitmap ))
32
+ self .fail (bitmap )
33
+ self .assertEqual (1 , bitmap .colors )
34
+ self .assertEqual (20 , bitmap .width )
35
+
36
+ def test_load_works_p4_binary (self ):
37
+ test_file = os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'examples' , 'images' , 'netpbm_p4_mono.pbm' )
38
+
39
+ with open (test_file , 'rb' ) as f :
40
+ f .seek (2 )
41
+ bitmap , palette = pnm .load (f , b'P4' , bitmap = Bitmap )
42
+ self .assertTrue (isinstance (bitmap , Bitmap ))
43
+ self .assertEqual (1 , bitmap .colors )
44
+ self .assertEqual (8 , bitmap .width )
45
+ self .assertEqual (8 , bitmap .height )
46
+
47
+ def test_load_works_p4_binary_big (self ):
48
+ test_file = os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'examples' , 'images' , 'MARBLES.PBM' )
49
+
50
+ with open (test_file , 'rb' ) as f :
51
+ f .seek (2 )
52
+ bitmap , palette = pnm .load (f , b'P4' , bitmap = Bitmap )
53
+ self .assertTrue (isinstance (bitmap , Bitmap ))
54
+ self .assertEqual (1 , bitmap .colors )
55
+ self .assertEqual (1152 , bitmap .width )
56
+ self .assertEqual (813 , bitmap .height )
0 commit comments