Skip to content

Commit

Permalink
Fix the RLE implementation (#19)
Browse files Browse the repository at this point in the history
* speedup runlength decoding

* fix typing

* add PR to changelog

* test: sort of kind of test the rle fix

---------

Co-authored-by: Benedikt Fuchs <[email protected]>
Co-authored-by: Benedikt Fuchs <[email protected]>
  • Loading branch information
3 people authored Nov 26, 2024
1 parent 7972d04 commit 03ca1b1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
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)

0 comments on commit 03ca1b1

Please sign in to comment.