Removed PPM loop to read header tokens

This commit is contained in:
Andrew Murray 2024-01-07 18:49:01 +11:00
parent a786a0551b
commit bb55274845

View File

@ -105,40 +105,31 @@ class PpmImageFile(ImageFile.ImageFile):
elif magic_number in (b"P3", b"P6"): elif magic_number in (b"P3", b"P6"):
self.custom_mimetype = "image/x-portable-pixmap" self.custom_mimetype = "image/x-portable-pixmap"
maxval = None self._size = int(self._read_token()), int(self._read_token())
decoder_name = "raw" decoder_name = "raw"
if magic_number in (b"P1", b"P2", b"P3"): if magic_number in (b"P1", b"P2", b"P3"):
decoder_name = "ppm_plain" decoder_name = "ppm_plain"
for ix in range(3): if mode == "1":
token = int(self._read_token()) self._mode = "1"
if ix == 0: # token is the x size args = "1;I"
xsize = token else:
elif ix == 1: # token is the y size maxval = int(self._read_token())
ysize = token if not 0 < maxval < 65536:
if mode == "1": msg = "maxval must be greater than 0 and less than 65536"
self._mode = "1" raise ValueError(msg)
rawmode = "1;I" self._mode = "I" if maxval > 255 and mode == "L" else mode
break
else:
self._mode = rawmode = mode
elif ix == 2: # token is maxval
maxval = token
if not 0 < maxval < 65536:
msg = "maxval must be greater than 0 and less than 65536"
raise ValueError(msg)
if maxval > 255 and mode == "L":
self._mode = "I"
if decoder_name != "ppm_plain": rawmode = mode
# If maxval matches a bit depth, use the raw decoder directly if decoder_name != "ppm_plain":
if maxval == 65535 and mode == "L": # If maxval matches a bit depth, use the raw decoder directly
rawmode = "I;16B" if maxval == 65535 and mode == "L":
elif maxval != 255: rawmode = "I;16B"
decoder_name = "ppm" elif maxval != 255:
decoder_name = "ppm"
args = (rawmode, 0, 1) if decoder_name == "raw" else (rawmode, maxval) args = (rawmode, 0, 1) if decoder_name == "raw" else (rawmode, maxval)
self._size = xsize, ysize self.tile = [(decoder_name, (0, 0) + self.size, self.fp.tell(), args)]
self.tile = [(decoder_name, (0, 0, xsize, ysize), self.fp.tell(), args)]
# #