Skip to content

Commit

Permalink
speedup runlength decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
helpmefindaname authored Oct 23, 2024
1 parent 1a8bd2f commit 1d6a3bd
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions pdfminer/runlength.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@ def rldecode(data: bytes) -> bytes:
(2 to 128) times during decompression. A length value of 128
denotes EOD.
"""
decoded = b""
i = 0
while i < len(data):
length = data[i]
decoded_array = []
data_iter = iter(data)

while True:
length = next(data_iter, 128)
if length == 128:
break

if length >= 0 and length < 128:
for j in range(i + 1, (i + 1) + (length + 1)):
decoded += bytes((data[j],))
i = (i + 1) + (length + 1)
decoded_array.extend((next(data_iter) for _ in range(length + 1)))

if length > 128:
run = bytes((data[i + 1],)) * (257 - length)
decoded += run
i = (i + 1) + 1

return decoded
run = [next(data_iter)] * (257 - length)
decoded_array.extend(run)
return bytes(decoded_array)

0 comments on commit 1d6a3bd

Please sign in to comment.