Pillow/src/PIL/PpmImagePlugin.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

374 lines
12 KiB
Python
Raw Normal View History

2010-07-31 06:52:47 +04:00
#
# The Python Imaging Library.
# $Id$
#
# PPM support for PIL
#
# History:
# 96-03-24 fl Created
# 98-03-06 fl Write RGBA images (as RGB, that is)
#
# Copyright (c) Secret Labs AB 1997-98.
# Copyright (c) Fredrik Lundh 1996.
#
# See the README file for information on usage and redistribution.
#
from __future__ import annotations
2010-07-31 06:52:47 +04:00
import math
2024-02-13 14:26:23 +03:00
from typing import IO
from . import Image, ImageFile
2022-03-09 14:29:45 +03:00
from ._binary import i16be as i16
from ._binary import o8
from ._binary import o32le as o32
2010-07-31 06:52:47 +04:00
#
# --------------------------------------------------------------------
b_whitespace = b"\x20\x09\x0a\x0b\x0c\x0d"
2010-07-31 06:52:47 +04:00
MODES = {
2022-03-12 09:32:15 +03:00
# standard
b"P1": "1",
b"P2": "L",
b"P3": "RGB",
b"P4": "1",
b"P5": "L",
b"P6": "RGB",
2010-07-31 06:52:47 +04:00
# extensions
2022-03-12 09:32:15 +03:00
b"P0CMYK": "CMYK",
b"Pf": "F",
2010-07-31 06:52:47 +04:00
# PIL extensions (for test purposes only)
2022-03-12 09:32:15 +03:00
b"PyP": "P",
b"PyRGBA": "RGBA",
b"PyCMYK": "CMYK",
2010-07-31 06:52:47 +04:00
}
2014-08-26 17:47:10 +04:00
2024-01-17 11:22:45 +03:00
def _accept(prefix: bytes) -> bool:
return prefix[0:1] == b"P" and prefix[1] in b"0123456fy"
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
##
# Image plugin for PBM, PGM, and PPM images.
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
class PpmImageFile(ImageFile.ImageFile):
format = "PPM"
format_description = "Pbmplus image"
2024-01-17 11:22:45 +03:00
def _read_magic(self) -> bytes:
assert self.fp is not None
magic = b""
# read until whitespace or longest available magic number
for _ in range(6):
2021-01-04 07:49:19 +03:00
c = self.fp.read(1)
if not c or c in b_whitespace:
2021-01-04 07:49:19 +03:00
break
magic += c
return magic
2021-01-04 07:49:19 +03:00
2024-01-17 11:22:45 +03:00
def _read_token(self) -> bytes:
assert self.fp is not None
token = b""
while len(token) <= 10: # read until next whitespace or limit of 10 characters
2020-12-21 06:56:30 +03:00
c = self.fp.read(1)
if not c:
2010-07-31 06:52:47 +04:00
break
elif c in b_whitespace: # token ended
if not token:
# skip whitespace at start
continue
break
elif c == b"#":
# ignores rest of the line; stops at CR, LF or EOF
while self.fp.read(1) not in b"\r\n":
pass
continue
2020-12-21 06:56:30 +03:00
token += c
if not token:
# Token was not even 1 byte
msg = "Reached EOF while reading header"
raise ValueError(msg)
elif len(token) > 10:
msg = f"Token too long in file header: {token.decode()}"
raise ValueError(msg)
2020-12-21 06:56:30 +03:00
return token
2010-07-31 06:52:47 +04:00
2024-01-17 11:22:45 +03:00
def _open(self) -> None:
assert self.fp is not None
2021-01-04 07:49:19 +03:00
magic_number = self._read_magic()
try:
2022-03-12 09:32:15 +03:00
mode = MODES[magic_number]
2021-01-04 07:49:19 +03:00
except KeyError:
msg = "not a PPM file"
raise SyntaxError(msg)
2024-01-09 04:46:55 +03:00
self._mode = mode
2019-03-04 10:17:12 +03:00
2022-03-12 09:32:15 +03:00
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"
2010-07-31 06:52:47 +04:00
2024-01-07 10:49:01 +03:00
self._size = int(self._read_token()), int(self._read_token())
2022-03-09 14:29:45 +03:00
decoder_name = "raw"
if magic_number in (b"P1", b"P2", b"P3"):
decoder_name = "ppm_plain"
2024-01-17 11:22:45 +03:00
args: str | tuple[str | int, ...]
2024-01-07 10:49:01 +03:00
if mode == "1":
args = "1;I"
2024-01-09 04:46:55 +03:00
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)
rawmode = "F;32F" if scale < 0 else "F;32BF"
args = (rawmode, 0, -1)
2024-01-07 10:49:01 +03:00
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)
2024-01-09 04:46:55 +03:00
if maxval > 255 and mode == "L":
self._mode = "I"
2024-01-07 10:49:01 +03:00
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"
2024-01-09 04:46:55 +03:00
args = rawmode if decoder_name == "raw" else (rawmode, maxval)
2024-01-07 10:49:01 +03:00
self.tile = [(decoder_name, (0, 0) + self.size, self.fp.tell(), args)]
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
#
# --------------------------------------------------------------------
2019-03-21 16:28:20 +03:00
2021-01-16 22:52:40 +03:00
class PpmPlainDecoder(ImageFile.PyDecoder):
_pulls_fd = True
2024-01-17 11:22:45 +03:00
_comment_spans: bool
def _read_block(self) -> bytes:
assert self.fd is not None
2021-01-16 22:52:40 +03:00
return self.fd.read(ImageFile.SAFEBLOCK)
2021-01-16 22:52:40 +03:00
2024-01-17 11:22:45 +03:00
def _find_comment_end(self, block: bytes, start: int = 0) -> int:
2021-01-16 22:52:40 +03:00
a = block.find(b"\n", start)
b = block.find(b"\r", start)
return min(a, b) if a * b > 0 else max(a, b) # lowest nonnegative index (or -1)
2024-01-17 11:22:45 +03:00
def _ignore_comments(self, block: bytes) -> bytes:
if self._comment_spans:
# Finish current comment
while block:
comment_end = self._find_comment_end(block)
if comment_end != -1:
# Comment ends in this block
# Delete tail of comment
block = block[comment_end + 1 :]
break
else:
# Comment spans whole block
# So read the next block, looking for the end
block = self._read_block()
# Search for any further comments
self._comment_spans = False
2021-01-16 22:52:40 +03:00
while True:
comment_start = block.find(b"#")
if comment_start == -1:
# No comment found
2021-01-16 22:52:40 +03:00
break
comment_end = self._find_comment_end(block, comment_start)
if comment_end != -1:
# Comment ends in this block
# Delete comment
2022-03-12 09:32:15 +03:00
block = block[:comment_start] + block[comment_end + 1 :]
else:
# Comment continues to next block(s)
2021-01-16 22:52:40 +03:00
block = block[:comment_start]
self._comment_spans = True
2021-01-16 22:52:40 +03:00
break
return block
2021-01-16 22:52:40 +03:00
2024-01-17 11:22:45 +03:00
def _decode_bitonal(self) -> bytearray:
2021-01-16 22:52:40 +03:00
"""
This is a separate method because in the plain PBM format, all data tokens are
exactly one byte, so the inter-token whitespace is optional.
2021-01-16 22:52:40 +03:00
"""
data = bytearray()
total_bytes = self.state.xsize * self.state.ysize
2021-01-16 22:52:40 +03:00
while len(data) != total_bytes:
2021-01-16 22:52:40 +03:00
block = self._read_block() # read next block
if not block:
# eof
break
2021-01-16 22:52:40 +03:00
block = self._ignore_comments(block)
2021-01-16 22:52:40 +03:00
tokens = b"".join(block.split())
for token in tokens:
2022-03-12 09:40:08 +03:00
if token not in (48, 49):
msg = b"Invalid token for this mode: %s" % bytes([token])
raise ValueError(msg)
data = (data + tokens)[:total_bytes]
2022-03-13 05:36:26 +03:00
invert = bytes.maketrans(b"01", b"\xFF\x00")
return data.translate(invert)
2021-01-16 22:52:40 +03:00
2024-01-17 11:22:45 +03:00
def _decode_blocks(self, maxval: int) -> bytearray:
data = bytearray()
2021-01-16 22:53:24 +03:00
max_len = 10
out_byte_count = 4 if self.mode == "I" else 1
out_max = 65535 if self.mode == "I" else 255
bands = Image.getmodebands(self.mode)
total_bytes = self.state.xsize * self.state.ysize * bands * out_byte_count
2021-01-16 22:53:24 +03:00
2024-01-17 11:22:45 +03:00
half_token = b""
while len(data) != total_bytes:
2021-01-16 22:53:24 +03:00
block = self._read_block() # read next block
if not block:
2022-03-12 09:40:08 +03:00
if half_token:
2021-01-16 22:53:24 +03:00
block = bytearray(b" ") # flush half_token
else:
# eof
break
2021-01-16 22:53:24 +03:00
block = self._ignore_comments(block)
2021-01-16 22:53:24 +03:00
2022-03-12 09:40:08 +03:00
if half_token:
2021-01-16 22:53:24 +03:00
block = half_token + block # stitch half_token to new block
2024-01-17 11:22:45 +03:00
half_token = b""
2021-01-16 22:53:24 +03:00
tokens = block.split()
if block and not block[-1:].isspace(): # block might split token
half_token = tokens.pop() # save half token for later
if len(half_token) > max_len: # prevent buildup of half_token
msg = (
2022-12-21 06:20:47 +03:00
b"Token too long found in data: %s" % half_token[: max_len + 1]
2021-01-16 22:53:24 +03:00
)
raise ValueError(msg)
2021-01-16 22:53:24 +03:00
for token in tokens:
if len(token) > max_len:
msg = b"Token too long found in data: %s" % token[: max_len + 1]
raise ValueError(msg)
value = int(token)
if value < 0:
msg_str = f"Channel value is negative: {value}"
raise ValueError(msg_str)
if value > maxval:
2024-01-17 11:22:45 +03:00
msg_str = f"Channel value too large for this mode: {value}"
raise ValueError(msg_str)
value = round(value / maxval * out_max)
data += o32(value) if self.mode == "I" else o8(value)
if len(data) == total_bytes: # finished!
2022-03-13 05:36:26 +03:00
break
return data
2021-01-16 22:52:40 +03:00
2024-01-17 11:22:45 +03:00
def decode(self, buffer: bytes) -> tuple[int, int]:
self._comment_spans = False
2021-01-16 22:52:40 +03:00
if self.mode == "1":
data = self._decode_bitonal()
2021-01-16 22:52:40 +03:00
rawmode = "1;8"
else:
maxval = self.args[-1]
data = self._decode_blocks(maxval)
rawmode = "I;32" if self.mode == "I" else self.mode
self.set_as_raw(bytes(data), rawmode)
2021-01-16 22:52:40 +03:00
return -1, 0
2022-03-09 14:29:45 +03:00
class PpmDecoder(ImageFile.PyDecoder):
_pulls_fd = True
2024-01-17 11:22:45 +03:00
def decode(self, buffer: bytes) -> tuple[int, int]:
assert self.fd is not None
2022-03-09 14:29:45 +03:00
data = bytearray()
maxval = self.args[-1]
2022-03-09 14:29:45 +03:00
in_byte_count = 1 if maxval < 256 else 2
out_byte_count = 4 if self.mode == "I" else 1
out_max = 65535 if self.mode == "I" else 255
bands = Image.getmodebands(self.mode)
dest_length = self.state.xsize * self.state.ysize * bands * out_byte_count
while len(data) < dest_length:
2022-03-09 14:29:45 +03:00
pixels = self.fd.read(in_byte_count * bands)
if len(pixels) < in_byte_count * bands:
# eof
break
for b in range(bands):
value = (
pixels[b] if in_byte_count == 1 else i16(pixels, b * in_byte_count)
)
value = min(out_max, round(value / maxval * out_max))
data += o32(value) if self.mode == "I" else o8(value)
2022-03-10 01:55:47 +03:00
rawmode = "I;32" if self.mode == "I" else self.mode
self.set_as_raw(bytes(data), rawmode)
2022-03-09 14:29:45 +03:00
return -1, 0
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
2021-01-16 22:52:40 +03:00
#
# --------------------------------------------------------------------
2024-06-10 07:15:28 +03:00
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
2010-07-31 06:52:47 +04:00
if im.mode == "1":
rawmode, head = "1;I", b"P4"
2010-07-31 06:52:47 +04:00
elif im.mode == "L":
rawmode, head = "L", b"P5"
2014-04-08 09:23:04 +04:00
elif im.mode == "I":
2022-03-10 01:55:47 +03:00
rawmode, head = "I;16B", b"P5"
elif im.mode in ("RGB", "RGBA"):
rawmode, head = "RGB", b"P6"
elif im.mode == "F":
rawmode, head = "F;32F", b"Pf"
2010-07-31 06:52:47 +04:00
else:
msg = f"cannot write mode {im.mode} as PPM"
raise OSError(msg)
2022-03-10 01:55:47 +03:00
fp.write(head + b"\n%d %d\n" % im.size)
2014-04-08 09:23:04 +04:00
if head == b"P6":
fp.write(b"255\n")
2022-03-09 14:29:45 +03:00
elif head == b"P5":
if rawmode == "L":
2014-04-08 09:23:04 +04:00
fp.write(b"255\n")
2022-03-10 01:55:47 +03:00
else:
2014-04-08 09:23:04 +04:00
fp.write(b"65535\n")
elif head == b"Pf":
fp.write(b"-1.0\n")
2024-01-09 04:47:27 +03:00
row_order = -1 if im.mode == "F" else 1
ImageFile._save(
im, fp, [ImageFile._Tile("raw", (0, 0) + im.size, 0, (rawmode, 0, row_order))]
)
2010-07-31 06:52:47 +04:00
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
#
# --------------------------------------------------------------------
Image.register_open(PpmImageFile.format, PpmImageFile, _accept)
Image.register_save(PpmImageFile.format, _save)
2010-07-31 06:52:47 +04:00
2022-03-09 14:29:45 +03:00
Image.register_decoder("ppm", PpmDecoder)
Image.register_decoder("ppm_plain", PpmPlainDecoder)
2022-03-09 14:29:45 +03:00
Image.register_extensions(PpmImageFile.format, [".pbm", ".pgm", ".ppm", ".pnm", ".pfm"])
2019-03-04 10:17:12 +03:00
2019-03-06 00:06:19 +03:00
Image.register_mime(PpmImageFile.format, "image/x-portable-anymap")