mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-05 20:33:24 +03:00
Expand preamble and _open
function
This commit is contained in:
parent
d1124cd2b9
commit
1ed05715d2
|
@ -23,21 +23,25 @@ from . import Image, ImageFile
|
||||||
b_whitespace = b"\x20\x09\x0a\x0b\x0c\x0d"
|
b_whitespace = b"\x20\x09\x0a\x0b\x0c\x0d"
|
||||||
|
|
||||||
MODES = {
|
MODES = {
|
||||||
# standard
|
# standard, plain
|
||||||
b"P4": "1",
|
b"P1": ("plain", "1"),
|
||||||
b"P5": "L",
|
b"P2": ("plain", "L"),
|
||||||
b"P6": "RGB",
|
b"P3": ("plain", "RGB"),
|
||||||
|
# standard, raw
|
||||||
|
b"P4": ("raw", "1"),
|
||||||
|
b"P5": ("raw", "L"),
|
||||||
|
b"P6": ("raw", "RGB"),
|
||||||
# extensions
|
# extensions
|
||||||
b"P0CMYK": "CMYK",
|
b"P0CMYK": ("raw", "CMYK"),
|
||||||
# PIL extensions (for test purposes only)
|
# PIL extensions (for test purposes only)
|
||||||
b"PyP": "P",
|
b"PyP": ("raw", "P"),
|
||||||
b"PyRGBA": "RGBA",
|
b"PyRGBA": ("raw", "RGBA"),
|
||||||
b"PyCMYK": "CMYK",
|
b"PyCMYK": ("raw", "CMYK"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _accept(prefix):
|
def _accept(prefix):
|
||||||
return prefix[0:1] == b"P" and prefix[1] in b"0456y"
|
return prefix[0:1] == b"P" and prefix[1] in b"0123456y"
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -86,22 +90,19 @@ class PpmImageFile(ImageFile.ImageFile):
|
||||||
def _open(self):
|
def _open(self):
|
||||||
magic_number = self._read_magic()
|
magic_number = self._read_magic()
|
||||||
try:
|
try:
|
||||||
mode = MODES[magic_number]
|
decoder, mode = MODES[magic_number]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise SyntaxError("not a PPM file")
|
raise SyntaxError("not a PPM file")
|
||||||
|
|
||||||
self.custom_mimetype = {
|
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"P4": "image/x-portable-bitmap",
|
||||||
b"P5": "image/x-portable-graymap",
|
b"P5": "image/x-portable-graymap",
|
||||||
b"P6": "image/x-portable-pixmap",
|
b"P6": "image/x-portable-pixmap",
|
||||||
}.get(magic_number)
|
}.get(magic_number)
|
||||||
|
|
||||||
if mode == "1":
|
|
||||||
self.mode = "1"
|
|
||||||
rawmode = "1;I"
|
|
||||||
else:
|
|
||||||
self.mode = rawmode = mode
|
|
||||||
|
|
||||||
for ix in range(3):
|
for ix in range(3):
|
||||||
token = int(self._read_token())
|
token = int(self._read_token())
|
||||||
if ix == 0: # token is the x size
|
if ix == 0: # token is the x size
|
||||||
|
@ -109,12 +110,16 @@ class PpmImageFile(ImageFile.ImageFile):
|
||||||
elif ix == 1: # token is the y size
|
elif ix == 1: # token is the y size
|
||||||
ysize = token
|
ysize = token
|
||||||
if mode == "1":
|
if mode == "1":
|
||||||
|
self.mode = "1"
|
||||||
|
rawmode = "1;I"
|
||||||
break
|
break
|
||||||
|
else:
|
||||||
|
self.mode = rawmode = mode
|
||||||
elif ix == 2: # token is maxval
|
elif ix == 2: # token is maxval
|
||||||
maxval = token
|
maxval = token
|
||||||
if maxval > 255:
|
if maxval > 255:
|
||||||
if not mode == "L":
|
if not mode == "L":
|
||||||
raise ValueError(f"Too many colors for band: {token}")
|
raise ValueError(f"Too many colors for band: {maxval}")
|
||||||
if maxval < 2 ** 16:
|
if maxval < 2 ** 16:
|
||||||
self.mode = "I"
|
self.mode = "I"
|
||||||
rawmode = "I;16B"
|
rawmode = "I;16B"
|
||||||
|
@ -123,7 +128,14 @@ class PpmImageFile(ImageFile.ImageFile):
|
||||||
rawmode = "I;32B"
|
rawmode = "I;32B"
|
||||||
|
|
||||||
self._size = xsize, ysize
|
self._size = xsize, ysize
|
||||||
self.tile = [("raw", (0, 0, xsize, ysize), self.fp.tell(), (rawmode, 0, 1))]
|
self.tile = [
|
||||||
|
(
|
||||||
|
decoder, # decoder
|
||||||
|
(0, 0, xsize, ysize), # region: whole image
|
||||||
|
self.fp.tell(), # offset to image data
|
||||||
|
(rawmode, 0, 1), # parameters for decoder
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in New Issue
Block a user