Merge pull request #7698 from radarhere/ppm

This commit is contained in:
Hugo van Kemenade 2024-01-09 16:08:05 +02:00 committed by GitHub
commit 605e664678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -100,6 +100,7 @@ class PpmImageFile(ImageFile.ImageFile):
except KeyError:
msg = "not a PPM file"
raise SyntaxError(msg)
self._mode = mode
if magic_number in (b"P1", b"P4"):
self.custom_mimetype = "image/x-portable-bitmap"
@ -108,49 +109,40 @@ class PpmImageFile(ImageFile.ImageFile):
elif magic_number in (b"P3", b"P6"):
self.custom_mimetype = "image/x-portable-pixmap"
maxval = None
self._size = int(self._read_token()), int(self._read_token())
decoder_name = "raw"
if magic_number in (b"P1", b"P2", b"P3"):
decoder_name = "ppm_plain"
for ix in range(3):
if mode == "F" and ix == 2:
scale = float(self._read_token())
if scale == 0.0 or not math.isfinite(scale):
msg = "scale must be finite and non-zero"
raise ValueError(msg)
rawmode = "F;32F" if scale < 0 else "F;32BF"
self.info["scale"] = abs(scale)
continue
token = int(self._read_token())
if ix == 0: # token is the x size
xsize = token
elif ix == 1: # token is the y size
ysize = token
if mode == "1":
self._mode = "1"
rawmode = "1;I"
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 mode == "1":
args = "1;I"
elif mode == "F":
scale = float(self._read_token())
if scale == 0.0 or not math.isfinite(scale):
msg = "scale must be finite and non-zero"
raise ValueError(msg)
self.info["scale"] = abs(scale)
if decoder_name != "ppm_plain":
# If maxval matches a bit depth, use the raw decoder directly
if maxval == 65535 and mode == "L":
rawmode = "I;16B"
elif maxval != 255:
decoder_name = "ppm"
rawmode = "F;32F" if scale < 0 else "F;32BF"
args = (rawmode, 0, -1)
else:
maxval = int(self._read_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"
row_order = -1 if mode == "F" else 1
args = (rawmode, 0, row_order) if decoder_name == "raw" else (rawmode, maxval)
self._size = xsize, ysize
self.tile = [(decoder_name, (0, 0, xsize, ysize), self.fp.tell(), args)]
rawmode = mode
if decoder_name != "ppm_plain":
# If maxval matches a bit depth, use the raw decoder directly
if maxval == 65535 and mode == "L":
rawmode = "I;16B"
elif maxval != 255:
decoder_name = "ppm"
args = rawmode if decoder_name == "raw" else (rawmode, maxval)
self.tile = [(decoder_name, (0, 0) + self.size, self.fp.tell(), args)]
#
@ -319,7 +311,6 @@ class PpmDecoder(ImageFile.PyDecoder):
def _save(im, fp, filename):
row_order = 1
if im.mode == "1":
rawmode, head = "1;I", b"P4"
elif im.mode == "L":
@ -330,7 +321,6 @@ def _save(im, fp, filename):
rawmode, head = "RGB", b"P6"
elif im.mode == "F":
rawmode, head = "F;32F", b"Pf"
row_order = -1
else:
msg = f"cannot write mode {im.mode} as PPM"
raise OSError(msg)
@ -344,6 +334,7 @@ def _save(im, fp, filename):
fp.write(b"65535\n")
elif head == b"Pf":
fp.write(b"-1.0\n")
row_order = -1 if im.mode == "F" else 1
ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, row_order))])