2013-10-17 14:05:27 +00:00
|
|
|
#!/usr/bin/env python
|
2009-12-24 11:51:43 +00:00
|
|
|
#
|
|
|
|
# RunLength decoder (Adobe version) implementation based on PDF Reference
|
|
|
|
# version 1.4 section 3.3.4.
|
|
|
|
#
|
|
|
|
# * public domain *
|
|
|
|
#
|
|
|
|
|
2014-09-04 07:36:19 +00:00
|
|
|
import six #Python 2+3 compatibility
|
|
|
|
|
2009-12-24 11:51:43 +00:00
|
|
|
def rldecode(data):
|
|
|
|
"""
|
|
|
|
RunLength decoder (Adobe version) implementation based on PDF Reference
|
|
|
|
version 1.4 section 3.3.4:
|
|
|
|
The RunLengthDecode filter decodes data that has been encoded in a
|
|
|
|
simple byte-oriented format based on run length. The encoded data
|
|
|
|
is a sequence of runs, where each run consists of a length byte
|
|
|
|
followed by 1 to 128 bytes of data. If the length byte is in the
|
|
|
|
range 0 to 127, the following length + 1 (1 to 128) bytes are
|
|
|
|
copied literally during decompression. If length is in the range
|
|
|
|
129 to 255, the following single byte is to be copied 257 - length
|
|
|
|
(2 to 128) times during decompression. A length value of 128
|
|
|
|
denotes EOD.
|
|
|
|
"""
|
2014-09-04 07:36:19 +00:00
|
|
|
decoded = b''
|
2013-11-07 08:35:04 +00:00
|
|
|
i = 0
|
2009-12-24 11:51:43 +00:00
|
|
|
while i < len(data):
|
2014-06-30 10:05:56 +00:00
|
|
|
#print 'data[%d]=:%d:' % (i,ord(data[i]))
|
2014-09-04 07:36:19 +00:00
|
|
|
length = six.indexbytes(data,i)
|
2009-12-24 11:51:43 +00:00
|
|
|
if length == 128:
|
|
|
|
break
|
|
|
|
if length >= 0 and length < 128:
|
2014-09-04 07:36:19 +00:00
|
|
|
for j in range(i+1,(i+1)+(length+1)):
|
|
|
|
decoded+=six.int2byte(six.indexbytes(data,j))
|
2014-06-30 10:05:56 +00:00
|
|
|
#print 'length=%d, run=%s' % (length+1,run)
|
2014-09-04 07:36:19 +00:00
|
|
|
|
2009-12-24 11:51:43 +00:00
|
|
|
i = (i+1) + (length+1)
|
|
|
|
if length > 128:
|
2014-09-04 07:36:19 +00:00
|
|
|
run = six.int2byte(six.indexbytes(data,i+1))*(257-length)
|
2014-06-30 10:05:56 +00:00
|
|
|
#print 'length=%d, run=%s' % (257-length,run)
|
2014-09-04 07:36:19 +00:00
|
|
|
decoded+=run
|
2009-12-24 11:51:43 +00:00
|
|
|
i = (i+1) + 1
|
2014-09-04 07:36:19 +00:00
|
|
|
return decoded
|
2009-12-24 11:51:43 +00:00
|
|
|
|