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

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

14 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 

8 

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

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

11 version 1.4 section 3.3.4: 

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

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

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

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

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

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

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

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

20 denotes EOD. 

21 """ 

22 decoded_array: list[int] = [] 

23 data_iter = iter(data) 

24 

25 while True: 

26 length = next(data_iter, 128) 

27 if length == 128: 

28 break 

29 

30 if 0 <= length < 128: 

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

32 

33 if length > 128: 

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

35 decoded_array.extend(run) 

36 return bytes(decoded_array)