Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved code into sequential order #29

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions src/PIL/PpmImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,43 +113,46 @@ def _open(self):
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 not math.isfinite(scale) or scale == 0.0:
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())
token = self._read_token()
if ix == 0: # token is the x size
xsize = token
xsize = int(token)
elif ix == 1: # token is the y size
ysize = token
ysize = int(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 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"

row_order = -1 if mode == "F" else 1
args = (rawmode, 0, row_order) if decoder_name == "raw" else (rawmode, maxval)
if mode == "F":
scale = float(token)
if not math.isfinite(scale) or scale == 0.0:
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)
else:
maxval = int(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":
# 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"

self._size = xsize, ysize
if decoder_name == "raw":
row_order = -1 if mode == "F" else 1
args = (rawmode, 0, row_order)
else:
args = (rawmode, maxval)
self.tile = [(decoder_name, (0, 0, xsize, ysize), self.fp.tell(), args)]


Expand Down Expand Up @@ -319,7 +322,6 @@ def decode(self, buffer):


def _save(im, fp, filename):
row_order = 1
if im.mode == "1":
rawmode, head = "1;I", b"P4"
elif im.mode == "L":
Expand All @@ -330,7 +332,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)
Expand All @@ -344,6 +345,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))])


Expand Down
Loading