File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44See LICENSE.txt for license details.
55"""
66
7- from array import array
8-
97RLE_MAX_BLKSIZE = 128
108
9+ def to_byte (b ):
10+ return bytes ([b ])
11+
1112def encode (data ):
1213 def check_block (data ):
1314 n = 1
@@ -18,9 +19,9 @@ def check_block(data):
1819 print ('Performing RLE compression ...' )
1920 orig_len = len (data )
2021
21- result = array ( 'B' )
22+ result = b''
2223 while len (data ):
23- tmp = array ( 'B' )
24+ tmp = b''
2425 while len (data ) and len (tmp ) < RLE_MAX_BLKSIZE :
2526 n , val = check_block (data )
2627 if n > 2 :
@@ -29,25 +30,22 @@ def check_block(data):
2930 elif n == 2 :
3031 n = 1
3132 data = data [n :]
32- tmp . append (val )
33+ tmp += to_byte (val )
3334 if len (tmp ):
34- result . append (len (tmp ) - 1 )
35- result . extend ( tmp )
35+ result += to_byte (len (tmp ) - 1 )
36+ result += tmp
3637 if n < 2 :
3738 continue
38- result . append ((n - 2 ) | 0x80 )
39- result . append (val )
39+ result += to_byte ((n - 2 ) | 0x80 )
40+ result += to_byte (val )
4041
4142 comp_len = len (result )
4243 comp_ratio = 100 * comp_len / orig_len
4344
4445 print (f'Compressed size { comp_len } bytes, ratio { comp_ratio :.1f} %' )
45- return bytes ( result )
46+ return result
4647
4748def decode (data ):
48- def to_byte (b ):
49- return b .to_bytes (1 , 'little' )
50-
5149 result = b''
5250 while (len (data )):
5351 if data [0 ] & 0x80 :
You can’t perform that action at this time.
0 commit comments