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 the RLE implementation #19

Merged
merged 5 commits into from
Nov 26, 2024
Merged
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
2 changes: 1 addition & 1 deletion playa/color.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, NamedTuple, Union, Tuple
from typing import Dict, NamedTuple, Union, Tuple

from playa.exceptions import PDFInterpreterError
from playa.parser import LIT, PDFObject, PSLiteral
Expand Down
25 changes: 12 additions & 13 deletions playa/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)
1 change: 0 additions & 1 deletion tests/test_lazy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pytest

import playa
from playa.parser import LIT
from playa.color import PREDEFINED_COLORSPACE, Color
from playa.exceptions import PDFEncryptionError

Expand Down
6 changes: 6 additions & 0 deletions tests/test_pdftypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from playa.data_structures import NameTree, NumberTree
from playa.runlength import rldecode

NUMTREE1 = {
"Kids": [
Expand Down Expand Up @@ -75,3 +76,8 @@ def test_name_tree():
(b"xylophone", 123),
(b"zzyzx", {"x": "y"}),
]


def test_rle():
large_white_image_encoded = bytes([129, 255] * (3 * 3000 * 4000 // 128))
_ = rldecode(large_white_image_encoded)