Coverage for /pythoncovmergedfiles/medio/medio/src/pdfminer.six/pdfminer/runlength.py: 13%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

15 statements  

1# 

2# RunLength decoder (Adobe version) implementation based on PDF Reference 

3# version 1.4 section 3.3.4. 

4# 

5# * public domain * 

6# 

7 

8from typing import List 

9 

10 

11def rldecode(data: bytes) -> bytes: 

12 """RunLength decoder (Adobe version) implementation based on PDF Reference 

13 version 1.4 section 3.3.4: 

14 The RunLengthDecode filter decodes data that has been encoded in a 

15 simple byte-oriented format based on run length. The encoded data 

16 is a sequence of runs, where each run consists of a length byte 

17 followed by 1 to 128 bytes of data. If the length byte is in the 

18 range 0 to 127, the following length + 1 (1 to 128) bytes are 

19 copied literally during decompression. If length is in the range 

20 129 to 255, the following single byte is to be copied 257 - length 

21 (2 to 128) times during decompression. A length value of 128 

22 denotes EOD. 

23 """ 

24 decoded_array: List[int] = [] 

25 data_iter = iter(data) 

26 

27 while True: 

28 length = next(data_iter, 128) 

29 if length == 128: 

30 break 

31 

32 if 0 <= length < 128: 

33 decoded_array.extend((next(data_iter) for _ in range(length + 1))) 

34 

35 if length > 128: 

36 run = [next(data_iter)] * (257 - length) 

37 decoded_array.extend(run) 

38 return bytes(decoded_array)