def test_decompress_3(self): """ Decompress string that ends with repeated sequence: service byte: 00000001""" b_array = bytearray([1]) + bytearray(b'abcdefg') + bytearray([0, 99]) actual = LZ77.decompress(b_array) expected = 'abcdefgabcde' self.assertEqual(actual, expected)
def test_compress_offset_less_len2(self): """ Compress string that has repeated sequences where offset is less then sequence lenth """ text = 'abcdabcdab' actual = LZ77.compress(text) expected = bytearray([8]) + bytearray(b'abcd') + bytearray([0, 52]) self.assertEqual(actual, expected)
def test_decompress_seq_diff_9_char(self): """ Decompress string of 9 char (2 service bytes) """ b_array = bytearray([0]) + bytearray(b'12345678') \ + bytearray([0]) + bytearray(b'9') actual = LZ77.decompress(b_array) expected = '123456789' self.assertEqual(actual, expected)
def test_decompress_offset_less_len2(self): """ Decompress string that has repeated sequences where offset is less then sequence lenth """ b_array = bytearray([8]) + bytearray(b'abcd') + bytearray([0, 52]) actual = LZ77.decompress(b_array) expected = 'abcdabcdab' self.assertEqual(actual, expected)
def test_find_sequence_max_1(self): a_int = ord('a') seq = ''.join(map(chr, range(a_int, a_int + LZ77.max_seq + 1))) text = seq * 2 actual, pos = LZ77.find_best_subsequence(text, len(seq)) expected = seq[:LZ77.max_seq] self.assertEqual(actual, expected)
def test_compress_3(self): """ Compress string of seq1_seq2_seq_1: service byte: 00000001""" text = 'abcdefgabcde' actual = LZ77.compress(text) expected = bytearray([1]) + bytearray(b'abcdefg') + bytearray([0, 99]) self.assertEqual(actual, expected)
def test_compress_2(self): """ Compress string of seq1_seq2_seq2_seq_1 """ text = 'abcdefdeabc' actual = LZ77.compress(text) expected = bytearray([3]) + bytearray(b'abcdef')\ + bytearray([0, 32]) + bytearray([0, 113]) self.assertEqual(actual, expected)
def test_compress_seq_diff_9_char(self): """ Compress string of 9 char (2 service bytes) """ text = '123456789' actual = LZ77.compress(text) expected = bytearray([0]) + bytearray(b'12345678') \ + bytearray([0]) + bytearray(b'9') self.assertEqual(actual, expected)
def test_decompress_2(self): """ Decompress string that has 2 repeated sequences """ b_array = bytearray([3]) + bytearray(b'abcdef')\ + bytearray([0, 32]) + bytearray([0, 113]) actual = LZ77.decompress(b_array) expected = 'abcdefdeabc' self.assertEqual(actual, expected)
def test_decompress_4(self): """ Decompress string that has repeated sequences at the begin of service_byte: service bytes: 00000000, 10000000 """ b_array = bytearray([0]) + bytearray(b'abcdefgh')\ + bytearray([128]) + bytearray([0, 84]) actual = LZ77.decompress(b_array) expected = 'abcdefghcdefgh' self.assertEqual(actual, expected)
def test_compress_5(self): """ Compress string of seq1_seq11_seq_12: where seq11, seq12 are in seq1 """ text = 'abcdefcdeab' actual = LZ77.compress(text) expected = bytearray([3]) + bytearray(b'abcdef')\ + bytearray([0, 49]) + bytearray([0, 128]) self.assertEqual(actual, expected)
def test_compress_4(self): """ Compress string of seq1_seq2_seq_2: service bytes: 00000000, 10000000 """ text = 'abcdefghcdefgh' actual = LZ77.compress(text) expected = bytearray([0]) + bytearray(b'abcdefgh')\ + bytearray([128]) + bytearray([0, 84]) self.assertEqual(actual, expected)
def test_compress_max_seq_len(self): """ Compress string that has repeated sequence of max len (17) """ a_int = ord('a') seq = ''.join(map(chr, range(a_int, a_int + LZ77.max_seq))) text = '123' + seq + '345' + seq actual = LZ77.compress(text) expected = bytearray([0]) + bytearray(text[:8], 'utf-8')\ + bytearray([0]) + bytearray(text[8: 16], 'utf-8')\ + bytearray([1]) + bytearray(text[16: 23], 'utf-8')\ + bytearray([1, 63]) self.assertEqual(actual, expected)
def test_compress_max_1_seq_len(self): """ Compress string that has repeated sequence of max+1 len (18) """ a_int = ord('a') seq = ''.join(map(chr, range(a_int, a_int + LZ77.max_seq + 1))) text = seq + '12' + seq + '1234' actual = LZ77.compress(text) expected = bytearray([0]) + bytearray(text[:8], 'utf-8')\ + bytearray([0]) + bytearray(text[8: 16], 'utf-8')\ + bytearray([12]) + bytearray(text[16: 20], 'utf-8')\ + bytearray([1, 63]) + bytearray([1, 49])\ + bytearray('34', 'utf-8') self.assertEqual(actual, expected)
def compresion(self,carpeta,arreglo): for i in range(len(arreglo)): filas,columnas=arreglo[i]["arreglo"].shape compressor=lz.LZ77(30) ImgCom=vc.vecinoCompresion(arreglo[i]["arreglo"],0.25).astype(np.uint8) Concatenacion=np.concatenate(ImgCom) origin=list(Concatenacion) ImgCom2=compressor.compress(origin) df=pd.DataFrame(np.array(ImgCom2)) df.to_csv(carpeta+"/"+arreglo[i]["nombre"],index=False) plt.imshow(ImgCom2) plt.show()
def decode(sliding_window_size,look_ahead_size, rows, columns, resultImage="result.jpg", encodedFile="encoded.npy"): codes = numpy.load(encodedFile) # codes2 = numpy.load('original.npy') # lastexcluded = codes[-1] + 1 # print(lastexcluded) # end = codes[len(codes)-lastexcluded:len(codes)-1] # codes = codes[0:len(codes)-lastexcluded] print("Decoding Started") img = e.decode(codes[0:sliding_window_size-look_ahead_size], codes[sliding_window_size-look_ahead_size:]) print("Decoding Done:") # for i in range(0, 50320): # print(img[i], codes2[i]) img = img[:rows * columns] img = img.reshape(rows, columns) cv2.imwrite(resultImage, img) print(" ->" + resultImage + ' is created\n')
def encode(sliding_window_size, look_ahead_size, imagePath="test.jpg", encodedFile="encoded"): img = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE).flatten() codes = numpy.array([]) print("Encoding Started") numpy.save('original.npy', img) codes = e.encode(sliding_window_size,look_ahead_size, img) print("Encoding Done:") integer_type = 'uint64' if sliding_window_size < 256: integer_type = 'uint8' elif sliding_window_size < 65536: integer_type = 'uint16' elif sliding_window_size < 4294967296: integer_type = 'uint32' numpy.save(encodedFile, codes.astype(integer_type)) # save print(" ->" + encodedFile + " is created") return codes
def test_compress_1(self): """ Compress string of type: seq1_char_se1 """ text = 'abcdabc' actual = LZ77.compress(text) expected = bytearray([8]) + bytearray(b'abcd') + bytearray([0, 49]) self.assertEqual(actual, expected)
def test_decompress_1(self): """ Decompress string that has repeated sequence """ b_array = bytearray([8]) + bytearray(b'abcd') + bytearray([0, 49]) actual = LZ77.decompress(b_array) expected = 'abcdabc' self.assertEqual(actual, expected)
def test_get_subseq_len3(self): actual = LZ77.get_subseq_len('hahaah', 'ha', 4) expected = 0 self.assertEqual(actual, expected)
def ejecutarLZ(self, sb=11, lab=5, ss=8, texto=""): codificador = lz.LZ77() resultado = codificador.codificar(sb=sb, lab=lab, ss=ss, texto=texto) self.textoR.insert(INSERT, self.listaACadena(resultado[0]) + "\n") print(codificador.decodificar(resultado[1])) return resultado
def test_find_sequence_max_1_pos(self): text = 'abc' + ('d' * (LZ77.max_block - 2)) + 'abc' actual, pos = LZ77.find_best_subsequence(text, LZ77.max_block + 1) expected = '' self.assertEqual(actual, expected)
def test_get_subseq_len2(self): actual = LZ77.get_subseq_len('hihiha', 'hi', 4) expected = 1 self.assertEqual(actual, expected)
def test_find_sequence_pos1(self): text = 'abab' actual, pos = LZ77.find_best_subsequence(text, 2) expected = 'ab' self.assertEqual(actual, expected) self.assertEqual(pos, 0)
def test_find_sequence_pos3(self): text = 'abbbabb' actual, pos = LZ77.find_best_subsequence(text, 4) expected = 'abb' self.assertEqual(actual, expected)
def test_find_sequence_neg2(self): text = 'aabbxbb' actual, pos = LZ77.find_best_subsequence(text, 2) expected = '' self.assertEqual(actual, expected)
def test_find_sequence_best(self): text = 'abcabcdeabcd' actual, pos = LZ77.find_best_subsequence(text, 8) expected = 'abcd' self.assertEqual(actual, expected)
def test_decompress_1_char(self): """ Decompress string of 1 char """ b_array = bytearray([0]) + bytearray(b'a') actual = LZ77.decompress(b_array) expected = 'a' self.assertEqual(actual, expected)
def test_decompress_seq_diff_8_char(self): """ Decompress string of 8 char (full 1 service byte) """ b_array = bytearray([0]) + bytearray(b'12345678') actual = LZ77.decompress(b_array) expected = '12345678' self.assertEqual(actual, expected)
def descompresion(self,carpetaOrigen,carpetaDestino): for i in os.listdir(carpetaOrigen): im=pd.read_csv(carpetaOrigen+"/"+i) decompressor=lz.LZ77(30) arreglo=decompressor.decompress(im.values) columnas=i.split(".") filasf=int(columnas[2]) filasff=int(filasf/4) columnasf=int(columnas[3]) columnasff=int(columnasf/4) descompresion=np.array(arreglo).reshape(filasff,columnasff) arreglov=vc.vecinoDescompresion(np.asarray(descompresion),4).astype(np.uint8) df=pd.DataFrame(np.array(arreglov)) df.to_csv(carpetaDestino+"/"+i,header=None,index=False) plt.imshow(arreglov) plt.show()