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

Fix #1054: speedup runlength decoding #1055

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `RecursionError` when corrupt PDF specifies a recursive /Pages object ([#998](https://github.com/pdfminer/pdfminer.six/pull/998))
- `TypeError` when corrupt PDF specifies text-positioning operators with invalid values ([#1000](https://github.com/pdfminer/pdfminer.six/pull/1000))
- inline image parsing fails when stream data contains "EI\n" ([#1008](https://github.com/pdfminer/pdfminer.six/issues/1008))
- fix memory overhead on runlength encoding ([#1008](https://github.com/pdfminer/pdfminer.six/issues/1008))

### Removed

Expand Down
25 changes: 12 additions & 13 deletions pdfminer/runlength.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# * public domain *
#

from typing import List


def rldecode(data: bytes) -> bytes:
"""RunLength decoder (Adobe version) implementation based on PDF Reference
Expand All @@ -19,21 +21,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: List[int] = []
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)
if 0 <= length < 128:
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)
Loading