def decompress(compressed_data, c): br = BitReader(compressed_data) state = LzwState(c) state.clear() d = {} htab = {} output = [] output2 = [] while True: if br.isEnd(): return [] cur_code = br.getBits(state.req_bits) incode = cur_code if cur_code == state.M_EOD: break if cur_code == state.M_CLR: state.clear() else: if cur_code == state.next_code: cur_code = state.oldcode output.append(state.finchar) while cur_code >= state.M_CLR: if cur_code not in htab: return [] output.append(htab[cur_code]) cur_code = d[cur_code + 1] state.finchar = cur_code while True: output2.append(cur_code) if not output: break cur_code = output.pop() if state.next_code < 4096 and state.oldcode != -1: d[state.next_code + 1] = state.oldcode htab[state.next_code] = state.finchar state.next_code += 1 if state.next_code >= state.next_shift: if state.req_bits < 12: state.req_bits += 1 state.next_shift = 1 << state.req_bits state.oldcode = incode return bytes(output2)
args = args.parse_args() filename_no_ext = os.path.splitext(os.path.basename(args.input_file))[0] out_path = args.outdir if not os.path.exists(out_path): os.makedirs(out_path) data = open(args.input_file, 'rb').read() br = BitReader(data) palette = mkutils.getPalette(br) print('Number of palette colors:', len(palette), '\n') file_idx = 0 files = [] while not br.isEnd(): print('Processing frame nr :', file_idx) cp = br.data_pos print('Compressed data offset:', hex(cp)) width, height, c, compressed_block = mkutils.getCompressedData(br) output = mklzw.decompress(compressed_block, c) if len(output) == 0: print('Decompression error, exiting.') break print('Compressed data size :', br.data_pos - cp) print('Decompressed data size:', len(output)) print('Frame width :', width) print('Frame height :', height) print('LZW parameter :', c) if args.raw: