Skip to content

Commit

Permalink
Moved decoder names out of MODES
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Mar 6, 2022
1 parent 0215175 commit e0a5d56
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/PIL/PpmImagePlugin.py
Expand Up @@ -23,20 +23,19 @@
b_whitespace = b"\x20\x09\x0a\x0b\x0c\x0d"

MODES = {
# standard, plain
b"P1": ("ppm_plain", "1"),
b"P2": ("ppm_plain", "L"),
b"P3": ("ppm_plain", "RGB"),
# standard, raw
b"P4": ("raw", "1"),
b"P5": ("raw", "L"),
b"P6": ("raw", "RGB"),
# standard
b"P1": "1",
b"P2": "L",
b"P3": "RGB",
b"P4": "1",
b"P5": "L",
b"P6": "RGB",
# extensions
b"P0CMYK": ("raw", "CMYK"),
b"P0CMYK": "CMYK",
# PIL extensions (for test purposes only)
b"PyP": ("raw", "P"),
b"PyRGBA": ("raw", "RGBA"),
b"PyCMYK": ("raw", "CMYK"),
b"PyP": "P",
b"PyRGBA": "RGBA",
b"PyCMYK": "CMYK",
}


Expand Down Expand Up @@ -90,18 +89,16 @@ def _read_token(self):
def _open(self):
magic_number = self._read_magic()
try:
decoder, mode = MODES[magic_number]
mode = MODES[magic_number]
except KeyError:
raise SyntaxError("not a PPM file")

self.custom_mimetype = {
b"P1": "image/x-portable-bitmap",
b"P2": "image/x-portable-graymap",
b"P3": "image/x-portable-pixmap",
b"P4": "image/x-portable-bitmap",
b"P5": "image/x-portable-graymap",
b"P6": "image/x-portable-pixmap",
}.get(magic_number)
if magic_number in (b"P1", b"P4"):
self.custom_mimetype = "image/x-portable-bitmap"
elif magic_number in (b"P2", b"P5"):
self.custom_mimetype = "image/x-portable-graymap"
elif magic_number in (b"P3", b"P6"):
self.custom_mimetype = "image/x-portable-pixmap"

for ix in range(3):
token = int(self._read_token())
Expand All @@ -127,14 +124,12 @@ def _open(self):
self.mode = "I"
rawmode = "I;32B"

decoder_name = "raw"
if magic_number in (b"P1", b"P2", b"P3"):
decoder_name = "ppm_plain"
self._size = xsize, ysize
self.tile = [
(
decoder, # decoder
(0, 0, xsize, ysize), # region: whole image
self.fp.tell(), # offset to image data
(rawmode, 0, 1), # parameters for decoder
)
(decoder_name, (0, 0, xsize, ysize), self.fp.tell(), (rawmode, 0, 1))
]


Expand Down

0 comments on commit e0a5d56

Please sign in to comment.