|
| 1 | +""" |
| 2 | +Name: Hew Ye Zea |
| 3 | +Student ID: 29035546 |
| 4 | +Date Created : 16/6/2020 |
| 5 | +Date Last Edited : 26/6/2020 |
| 6 | +""" |
| 7 | +import sys |
| 8 | +from huffman import * |
| 9 | +from elias import * |
| 10 | +from z_algo import * |
| 11 | +from bitarray import bitarray |
| 12 | + |
| 13 | + |
| 14 | +def encode(filename,w,l): |
| 15 | + |
| 16 | + # read file |
| 17 | + string = readfile(filename) |
| 18 | + |
| 19 | + # encode the string using huffman |
| 20 | + encoded , uniq_char = encodeHuffman(string) |
| 21 | + |
| 22 | + # encode header and data |
| 23 | + header = encode_header(uniq_char,encoded) |
| 24 | + data = encode_data(string,w,l,encoded) |
| 25 | + |
| 26 | + # concat header and data |
| 27 | + result = header + data |
| 28 | + |
| 29 | + # output result to file |
| 30 | + output = open('output_encoder_lzss.bin', 'wb') |
| 31 | + result.tofile(output) |
| 32 | + output.close() |
| 33 | + |
| 34 | + |
| 35 | +def encode_header(uniq_char, encoded): |
| 36 | + """ |
| 37 | + Encode the header |
| 38 | + :param uniq_char: number of unique characters |
| 39 | + :param encoded: the huffman code of each unique character |
| 40 | + :return: an encoded header |
| 41 | + """ |
| 42 | + # header consists of |
| 43 | + # 1. unique char - elias coded |
| 44 | + # 2. ascii code of each char - elias coded |
| 45 | + # 3. length of huffman code - elias coded |
| 46 | + # 4. huffman code |
| 47 | + result = bitarray() |
| 48 | + result += eliasEncode(uniq_char) # add elias encoded # of unique char to header [1] |
| 49 | + |
| 50 | + for ascii,huffman_code in enumerate(encoded): |
| 51 | + if huffman_code is not None : |
| 52 | + ascii_in_binary = fromDecToBinary(ascii) |
| 53 | + ascii_encoded = bitarray('0' * (7 - len(ascii_in_binary))) + ascii_in_binary # elias coded ascii code [2] |
| 54 | + huffman_code_length = eliasEncode(len(huffman_code)) # elias coded - length of huffman code [3] |
| 55 | + result = result + ascii_encoded + huffman_code_length + huffman_code # concat huffman code |
| 56 | + |
| 57 | + return result |
| 58 | + |
| 59 | + |
| 60 | +def encode_data(string,w,l,encoded): |
| 61 | + """ |
| 62 | + Encode the data part |
| 63 | + :param string: string to be encoded |
| 64 | + :param w: window size |
| 65 | + :param l: lookahead buffer size |
| 66 | + :param encoded: the huffman code of each unique character |
| 67 | + :return: a bitstring that contains the encoded data |
| 68 | + """ |
| 69 | + result = bitarray() |
| 70 | + number_of_formats = 0 |
| 71 | + index = 0 |
| 72 | + |
| 73 | + while index < len(string): |
| 74 | + # increment the number of formats |
| 75 | + number_of_formats += 1 |
| 76 | + |
| 77 | + # run z algorithm on string to calculate the matched length |
| 78 | + z_arr = z_algo(string,w,l,index) |
| 79 | + |
| 80 | + maximum_length = -1 |
| 81 | + maximum_offset = -1 |
| 82 | + |
| 83 | + # obtain the maximum match length |
| 84 | + for i in range(len(z_arr)): |
| 85 | + if z_arr[i] >= maximum_length: |
| 86 | + maximum_offset = i |
| 87 | + maximum_length = z_arr[i] |
| 88 | + |
| 89 | + # matched length > 3 , encode data in the format of : |
| 90 | + # 0 + offset [1] + matched length [2] |
| 91 | + if maximum_length >= 3 : |
| 92 | + zero = bitarray('0') |
| 93 | + offset = eliasEncode(len(z_arr) - maximum_offset) # [1] |
| 94 | + length = eliasEncode(maximum_length) # [2] |
| 95 | + result = result + zero + offset + length |
| 96 | + index += maximum_length # increment index |
| 97 | + |
| 98 | + # matched length < 3 , encode data in the format of : |
| 99 | + # 1 + ascii huffman code [1] |
| 100 | + else: |
| 101 | + one = bitarray('1') |
| 102 | + char = string[index] |
| 103 | + ascii_huffman = encoded[ord(char)] # [1] |
| 104 | + |
| 105 | + result = result + one + ascii_huffman |
| 106 | + index += 1 # increment index |
| 107 | + |
| 108 | + elias_format = eliasEncode(number_of_formats) # elias encode the total number of format |
| 109 | + result = elias_format + result # prepend the elias encoded format to result |
| 110 | + |
| 111 | + return result |
| 112 | + |
| 113 | + |
| 114 | +def readfile(filename): |
| 115 | + file = open(filename,"r") |
| 116 | + lines = [] |
| 117 | + for line in file: |
| 118 | + lines.append(line) |
| 119 | + file.close() |
| 120 | + return ''.join(lines) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == '__main__': |
| 124 | + _, filename, w, l = sys.argv |
| 125 | + encode(filename, int(w), int(l)) |
| 126 | + |
| 127 | + |
0 commit comments