Пример #1
0
    def test_encode_decode(self):
        base_dictionary = ["t", "i", "y", "n", "b", " "]
        input = "itty bitty bit bin"

        encoded = lzw.encode("itty bitty bit bin", base_dictionary)
        decoded = lzw.decode(encoded, base_dictionary)
        self.assertEqual(input, decoded)
Пример #2
0
 def toFile(self,f):
   colorTableSize = None
   if self.colorTable:
     colorTableSize = int(math.log(len(self.colorTable)/3,2))
     packed_fields = (1<<7)+colorTableSize-1
   else:
     packed_fields = 0
   ImageDescriptor(*self.shape,packed_fields=packed_fields).toFile(f)
   if self.colorTable:
     f.write(self.colorTable)
   # table based image data
   codesize = self.bitsPerColor if colorTableSize is None else max(2,colorTableSize)
   f.write(chr(codesize))
   data = lzw.encode(self.imageData.ravel(),codeSize=codesize)
   DataBlock(data).toFile(f)
Пример #3
0
W = [2**i for i in [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]]

for input_file, results_file in pairs:
    b = open(input_file, mode="rb").read()
    out = csv.writer(open(results_file, mode="w"))
    out.writerow(["W", "Encode Time", "Decode Time"])

    for w in W:
        print("{input_file} {w}".format(input_file=input_file, w=w))
        total_deflate = 0
        total_inflate = 0

        for i in range(10):
            print(i)
            deflate_t0 = time()
            x = encode(b, w)
            deflate_t1 = time()
            inflate_t0 = time()
            decode(x, w)
            inflate_t1 = time()

            total_deflate += deflate_t1 - deflate_t0
            total_inflate += inflate_t1 - inflate_t0

        out.writerow([
            w,
            total_deflate / 10.0,
            total_inflate / 10.0,
        ])
print(F"Loading {IMG_NAME} at {IMG_PATH}")
image = ImageSource().load_from_file(IMG_PATH)
print(image)
# uncomment if you want to display the loaded image
# image.show()
# uncomment if you want to show the histogram of the colors
# image.show_color_hist()

# ================================================================
input_lzw = image.get_pixel_seq().copy()
# ======================= SOURCE ENCODING ========================
# ====================== Lempel-Ziv-Welch ========================
print("-------START LZW ENCODING-------")
print("ingang LZW:", input_lzw)
print("lengte origineel:", len(input_lzw))
encoded_msg, dictonary = lzw.encode(input_lzw)  #encoded_msg geeft UINT32 terug
print("source encoded msg (uint32): ", encoded_msg)
#uint32 omzetten naar uint8
encoded_msg_bit = util.uint32_to_bit(encoded_msg)
encoded_msg_8uint = util.bit_to_uint8(encoded_msg_bit)
uint8_stream = np.array(encoded_msg, dtype=np.uint8)
# ====================== CHANNEL ENCODING ========================
# ======================== Reed-Solomon ==========================
print("-------START CHANNEL ENCODING-------")
initiele_lengte = len(uint8_stream)
print("ingang channel encoder:", uint8_stream)
# as we are working with symbols of 8 bits
# choose n such that m is divisable by 8 when n=2^m−1
# Example: 255 + 1 = 2^m -> m = 8
n = 255  # code_word_length in symbols
k = 223  # message_length in symbols
Пример #5
0
 def test_encode_with_non_default_dictionary(self):
     base_dictionary = ["t", "i", "y", "n", "b", " "]
     self.assertEqual([2, 1, 1, 3, 6, 5, 7, 9, 11, 7, 17], lzw.encode("itty bitty bit bin", base_dictionary))
Пример #6
0
 def test_encode(self):
     self.assertEqual([1, 4, 3, 2, 6, 8], lzw.encode("aaacbcbcbc"))
Пример #7
0
import csv
import lz77
import lzss
import lzw

deflate = lambda b, W, L: lzw.encode(b, W)

impls = [
    ['lz77-', lz77.deflate],
    ['lzss-', lzss.deflate],
    ['lzw-', deflate],
]

W = [2**i - 1 for i in [8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32]]
L = [2**i - 1 for i in [8]]
b = open("corpus/genesis.txt", mode="rb").read()

for prefix, deflate in impls:
    out = csv.writer(open("results/" + prefix + "genesis.csv", mode="w"))
    out.writerow(["W", "L", "Compression Ratio"])
    for w in W:
        for l in L:
            print(prefix, w, l)
            x = deflate(b, W=w, L=l)
            x.fill()
            out.writerow([
                w,
                l,
                (len(x) // 8) / len(b),
            ])
Пример #8
0
# TODO print-out the codebook and validate the codebook (include your findings in the report)
encoded_message = huffman.encode(huffman_tree.codebook, image.get_pixel_seq())
print(len(encoded_message))
print("Enc: {}".format(t.toc()))

t.tic()
decoded_message = huffman.decode(huffman_tree, encoded_message)
print("Dec: {}".format(t.toc()))

input_lzw = img.get_pixel_seq().copy()

# ======================= SOURCE ENCODING ========================
# ====================== Lempel-Ziv-Welch ========================

t.tic()
encoded_msg, dictonary = lzw.encode(input_lzw)
print("Enc: {}".format(t.toc()))

t.tic()
decoded_msg = lzw.decode(encoded_msg)
print("Enc: {0:.4f}".format(t.toc()))

uint8_stream = np.array(decoded_msg, dtype=np.uint8)
# ====================== CHANNEL ENCODING ========================
# ======================== Reed-Solomon ==========================

# as we are working with symbols of 8 bits
# choose n such that m is divisible by 8 when n=2^m−1
# Example: 255 + 1 = 2^m -> m = 8
n = 255  # code_word_length in symbols
k = 223  # message_length in symbols
Пример #9
0
def main(rec, step, dead, img, out):
    image = (cv2.imread(img, 1)).astype(int)
    w = image.shape[0]
    h = image.shape[1]
    
    image[:,:,[0,1,2]] = image[:,:,[2,1,0]] #To RGB
    
    cv2.imshow('img',256*image[:,:,[2,1,0]]) #Il faut inverser, car cv2 interprète RGB dans l'ordre BGR
    #somehow aussi il faut multiplier par 256

    img_yuv = RGBtoYUV(image)
    
    #cv2.imshow('yuv',256*img_yuv)
    #sampling 4:2:0
    for i in range(0, len(img_yuv), 2):
        for j in range (0, len(img_yuv[0]), 2):
            moyU = (img_yuv[i,j,1]/4 + img_yuv[i+1,j,1]/4 +img_yuv[i,j+1,1]/4 +img_yuv[i+1,j+1,1]/4)
            img_yuv[i,j,1] = moyU
            img_yuv[i+1,j,1] = moyU
            img_yuv[i,j+1,1] = moyU
            img_yuv[i+1,j+1,1] = moyU
            moyV = (img_yuv[i,j,2]/4 + img_yuv[i+1,j,2]/4 +img_yuv[i,j+1,2]/4 +img_yuv[i+1,j+1,2]/4) 
            img_yuv[i,j,2] = moyV
            img_yuv[i+1,j,2] = moyV
            img_yuv[i,j+1,2] = moyV
            img_yuv[i+1,j+1,2] = moyV
        
    #cv2.imshow('yuv_s',256*img_yuv)
    
    result = dwt.dwt(img_yuv, rec)
    # fonction dwt travaille recursivement
    # result est un dictionnaire avec ces valeurs:
    # / /f11  f1h\          \
    # | |        |      f1h |
    # | \fh1  fhh/          |
    # |                     |
    # | fh1             fhh |
    # \                     /
    # f11 se "deroule" selon le niveau de recursion souhaite

    # LZW Compression
    v1d = to_1d(result)
    info = v1d[0:3]
    #Quantification
    q1d = quant(v1d[3:], dead, step)
    print("Compression LZW")
    print("length original : ", len(q1d))
    vecLZW = lzw.encode(q1d)

    # LZW Decompression
    print("Decompression LZW")
    vecDeLZW = lzw.decode(vecLZW)
    print("length reconstructed : ", len(vecDeLZW))
    v3d = from_1d(np.append(info,vecDeLZW))

    reconstructed = dwt.inverse_dwt(v3d)

    #cv2.imshow('rec',256*reconstructed)

    recolored = YUVtoRGB(reconstructed)
    cv2.imshow('col',256*recolored[:,:,[2,1,0]])

    cv2.imshow('qnt',256*quant(recolored[:,:,[2,1,0]], dead, step))

    if (out is not None):
        cv2.imwrite(out, recolored[:,:,[2,1,0]])

    cv2.waitKey(10000)
Пример #10
0
print("-------START HUFFMAN DECODING-------")
#print("Dit is de Huffman geëncodeerde data:", encoded_message)
t.tic()
decoded_message = huffman.decode(huffman_tree, encoded_message)
print(F"Dec: {t.toc_str()}")

print("len dec msg huff: ", (8 * len(decoded_message)), " bits")
print(F"Tijd van volledig Huffman proces: {t2.toc_str()}")
# ======================= SOURCE ENCODING ========================
# ====================== Lempel-Ziv-Welch ========================
print("-------START LZW ENCODING-------")
t2.tic()
input_lzw = image.get_pixel_seq().copy()
print("lengte initiele data:", (8 * len(image.get_pixel_seq())), " bits")
t.tic()
encoded_msg, dictonary = lzw.encode(
    input_lzw)  #Intersante delen van de dict weergeven
print(F"Enc: {t.toc_str()}")
print("Dit is de lengte van de encoded data", (32 * len(encoded_msg)), " bits")
t.tic()
print("-------START LZW DECODING-------")
decoded_msg = lzw.decode(encoded_msg)
print(F"Dec: {t.toc_str()}")
#print("formaat decoded LZW:",decoded_msg)
print("len dec msg LZW: ", (8 * len(decoded_msg)), " bits")
uint8_stream = np.array(decoded_msg, dtype=np.uint8)
print(F"Tijd van volledig LZW proces: {t2.toc_str()}")
# ====================== CHANNEL ENCODING ========================
# ======================== Reed-Solomon ==========================
print("-------START CHANNEL ENCODING-------")
# print("dit formaat komt binnen in het channel:", uint8_stream)
initiele_lengte = len(uint8_stream)