Skip to content

Commit bb55274

Browse files
committed
Removed PPM loop to read header tokens
1 parent a786a05 commit bb55274

1 file changed

Lines changed: 22 additions & 31 deletions

File tree

src/PIL/PpmImagePlugin.py

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -105,40 +105,31 @@ def _open(self):
105105
elif magic_number in (b"P3", b"P6"):
106106
self.custom_mimetype = "image/x-portable-pixmap"
107107

108-
maxval = None
108+
self._size = int(self._read_token()), int(self._read_token())
109+
109110
decoder_name = "raw"
110111
if magic_number in (b"P1", b"P2", b"P3"):
111112
decoder_name = "ppm_plain"
112-
for ix in range(3):
113-
token = int(self._read_token())
114-
if ix == 0: # token is the x size
115-
xsize = token
116-
elif ix == 1: # token is the y size
117-
ysize = token
118-
if mode == "1":
119-
self._mode = "1"
120-
rawmode = "1;I"
121-
break
122-
else:
123-
self._mode = rawmode = mode
124-
elif ix == 2: # token is maxval
125-
maxval = token
126-
if not 0 < maxval < 65536:
127-
msg = "maxval must be greater than 0 and less than 65536"
128-
raise ValueError(msg)
129-
if maxval > 255 and mode == "L":
130-
self._mode = "I"
131-
132-
if decoder_name != "ppm_plain":
133-
# If maxval matches a bit depth, use the raw decoder directly
134-
if maxval == 65535 and mode == "L":
135-
rawmode = "I;16B"
136-
elif maxval != 255:
137-
decoder_name = "ppm"
138-
139-
args = (rawmode, 0, 1) if decoder_name == "raw" else (rawmode, maxval)
140-
self._size = xsize, ysize
141-
self.tile = [(decoder_name, (0, 0, xsize, ysize), self.fp.tell(), args)]
113+
if mode == "1":
114+
self._mode = "1"
115+
args = "1;I"
116+
else:
117+
maxval = int(self._read_token())
118+
if not 0 < maxval < 65536:
119+
msg = "maxval must be greater than 0 and less than 65536"
120+
raise ValueError(msg)
121+
self._mode = "I" if maxval > 255 and mode == "L" else mode
122+
123+
rawmode = mode
124+
if decoder_name != "ppm_plain":
125+
# If maxval matches a bit depth, use the raw decoder directly
126+
if maxval == 65535 and mode == "L":
127+
rawmode = "I;16B"
128+
elif maxval != 255:
129+
decoder_name = "ppm"
130+
131+
args = (rawmode, 0, 1) if decoder_name == "raw" else (rawmode, maxval)
132+
self.tile = [(decoder_name, (0, 0) + self.size, self.fp.tell(), args)]
142133

143134

144135
#

0 commit comments

Comments
 (0)