Removed token_spans variable

This commit is contained in:
Andrew Murray 2022-03-12 17:40:08 +11:00
parent 073acd4c82
commit 9e04416c6c

View File

@ -196,16 +196,13 @@ class PpmPlainDecoder(ImageFile.PyDecoder):
tokens = b"".join(block.split()) tokens = b"".join(block.split())
for token in tokens: for token in tokens:
if token in (48, 49): if token not in (48, 49):
tokens_read += 1
else:
raise ValueError(f"Invalid token for this mode: {bytes([token])}") raise ValueError(f"Invalid token for this mode: {bytes([token])}")
tokens_read += 1
decoded_data.append(token) decoded_data.append(token)
if tokens_read == total_tokens: # finished! if tokens_read == total_tokens: # finished!
invert = bytes.maketrans(b"01", b"\xFF\x00") invert = bytes.maketrans(b"01", b"\xFF\x00")
decoded_data = decoded_data.translate(invert) return decoded_data.translate(invert)
return decoded_data
def _decode_blocks(self, channels=1, depth=8): def _decode_blocks(self, channels=1, depth=8):
decoded_data = bytearray() decoded_data = bytearray()
@ -215,14 +212,13 @@ class PpmPlainDecoder(ImageFile.PyDecoder):
bytes_per_sample = depth // 8 bytes_per_sample = depth // 8
total_tokens = self.size * channels total_tokens = self.size * channels
token_spans = False
comment_spans = False comment_spans = False
half_token = False half_token = False
tokens_read = 0 tokens_read = 0
while True: while True:
block = self._read_block() # read next block block = self._read_block() # read next block
if not block: if not block:
if token_spans: if half_token:
block = bytearray(b" ") # flush half_token block = bytearray(b" ") # flush half_token
else: else:
raise ValueError("Reached EOF while reading data") raise ValueError("Reached EOF while reading data")
@ -237,14 +233,12 @@ class PpmPlainDecoder(ImageFile.PyDecoder):
block, comment_spans = self._ignore_comments(block) block, comment_spans = self._ignore_comments(block)
if token_spans: if half_token:
block = half_token + block # stitch half_token to new block block = half_token + block # stitch half_token to new block
token_spans = False
tokens = block.split() tokens = block.split()
if block and not block[-1:].isspace(): # block might split token if block and not block[-1:].isspace(): # block might split token
token_spans = True
half_token = tokens.pop() # save half token for later half_token = tokens.pop() # save half token for later
if len(half_token) > max_len: # prevent buildup of half_token if len(half_token) > max_len: # prevent buildup of half_token
raise ValueError( raise ValueError(