@@ -39,14 +39,37 @@ def load(file, header, *, bitmap=None, palette=None):
3939 to do the actual data loading.
4040 Formats P1, P4 have two space padded pieces of information: width and height.
4141 All other formats have three: width, height, and max color value.
42+ This load function will move the file stream pointer to the start of data in all cases.
4243 """
44+ # pylint: disable=too-many-branches
4345 magic_number = header [:2 ]
4446 file .seek (2 )
4547 pnm_header = []
48+ next_value = bytearray ()
4649 while True :
4750 # We have all we need at length 3 for formats P2, P3, P5, P6
4851 if len (pnm_header ) == 3 :
49- break
52+ if magic_number in [b"P2" , b"P5" ]:
53+ from . import pgm
54+
55+ return pgm .load (
56+ file , magic_number , pnm_header , bitmap = bitmap , palette = palette
57+ )
58+
59+ if magic_number == b"P3" :
60+ from . import ppm_ascii
61+
62+ return ppm_ascii .load (
63+ file , pnm_header [0 ], pnm_header [1 ], bitmap = bitmap , palette = palette
64+ )
65+
66+ if magic_number == b"P6" :
67+ from . import ppm_binary
68+
69+ return ppm_binary .load (
70+ file , pnm_header [0 ], pnm_header [1 ], bitmap = bitmap , palette = palette
71+ )
72+
5073 if len (pnm_header ) == 2 and magic_number in [b"P1" , b"P4" ]:
5174 bitmap = bitmap (pnm_header [0 ], pnm_header [1 ], 1 )
5275 if palette :
@@ -66,46 +89,15 @@ def load(file, header, *, bitmap=None, palette=None):
6689 )
6790
6891 next_byte = file .read (1 )
69- if next_byte == b"#" :
70- while True :
71- next_byte = file .read (1 )
72- if not next_byte :
73- raise RuntimeError ("Unsupported image format" )
74- if next_byte == b"\n " :
75- break
76- if next_byte .isdigit ():
77- value = bytearray ()
78- while True :
79- if not next_byte .isdigit ():
80- break
81- value += next_byte
82- next_byte = file .read (1 )
83- if not next_byte :
84- raise RuntimeError ("Unsupported image format" )
85-
86- pnm_header .append (int ("" .join (["%c" % char for char in value ])))
87- continue
88-
89- if not next_byte :
90- raise RuntimeError ("Unsupported image format" )
91-
92- if magic_number in [b"P2" , b"P5" ]:
93- from . import pgm
94-
95- return pgm .load (file , magic_number , pnm_header , bitmap = bitmap , palette = palette )
96-
97- if magic_number == b"P3" :
98- from . import ppm_ascii
99-
100- return ppm_ascii .load (
101- file , pnm_header [0 ], pnm_header [1 ], bitmap = bitmap , palette = palette
102- )
103-
104- if magic_number == b"P6" :
105- from . import ppm_binary
106-
107- return ppm_binary .load (
108- file , pnm_header [0 ], pnm_header [1 ], bitmap = bitmap , palette = palette
109- )
110-
111- raise RuntimeError ("Unsupported image format {}" .format (magic_number ))
92+ if next_byte == b"" :
93+ raise RuntimeError ("Unsupported image format {}" .format (magic_number ))
94+ if next_byte == b"#" : # comment found, seek until a newline or EOF is found
95+ while file .read (1 ) not in [b"" , b"\n " ]: # EOF or NL
96+ pass
97+ elif not next_byte .isdigit (): # boundary found in header data
98+ if next_value :
99+ # pull values until space is found
100+ pnm_header .append (int ("" .join (["%c" % char for char in next_value ])))
101+ next_value = bytearray () # reset the byte array
102+ else :
103+ next_value += next_byte # push the digit into the byte array
0 commit comments