-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecompress.py
94 lines (76 loc) · 2.25 KB
/
decompress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#CMiC Reference Decoder
#CMiC Image Compressor Starter file
#first some imports
import sys
import scipy
import scipy.ndimage
import numpy as np
import PIL
import pywt
from collections import Counter
from heapq import merge
import re
import struct
import itertools
import json
import os
def show(image):
scipy.misc.toimage(image).show()
def main():
#quantization factor. Might change
try:
input_file_name = sys.argv[1]
print "Attempting to open %s..." % input_file_name
input_file = open(sys.argv[1], 'rb')
except:
print "Unable to open input file. Qutting."
quit()
try:
output_file_name = sys.argv[2]
print "Attempting to open %s..." % output_file_name
output_file = open(sys.argv[2], 'wb')
except:
print "Unable to open output file. Qutting."
quit()
header = json.loads(input_file.readline())
code_dict = json.loads(input_file.readline())
print header
height = int(header['height'])
width = int(header['width'])
wavelet = header['wavelet']
q = int(header['q'])
lq = int(header['lq'])
decode_dict = {v.encode() : k for k, v in code_dict.iteritems()}
#print decode_dict
if height%2 != 0:
height += 1
if width%2 != 0:
width += 1
binary_data = input_file.read()
binary_string = ""
for byte in binary_data:
binary_string += format(ord(byte),'08b')
decdoed_data = []
while len(decdoed_data) != 4*(0.5*height * 0.5*width):
sub_str = ""
i = 0
while sub_str not in decode_dict:
sub_str = binary_string[0:i]
i=i+1
decdoed_data += [int(decode_dict[sub_str])]
binary_string = binary_string[i-1:]
print height, width
LL = (np.cumsum(np.array(decdoed_data[0:int(height/2 * width/2)])*lq)).reshape(height/2, width/2)
LH = (np.array(decdoed_data[int(height/2 * width/2):2*int(height/2 * width/2)])*q).reshape(height/2, width/2)
HL = (np.array(decdoed_data[2*int(height/2 * width/2):3*int(height/2 * width/2)])*q).reshape(height/2, width/2)
HH = (np.array(decdoed_data[3*int(height/2 * width/2):4*int(height/2 * width/2)])*q).reshape(height/2, width/2)
im = pywt.idwt2( (LL, (LH, HL, HH)), wavelet,mode='periodization' )
'''Adjust for odd heigh or width'''
if height%2 != 0:
np.delete(im,height,0)
if width%2 != 0:
np.delete(im,width,1)
show(im)
scipy.misc.toimage(im).save(output_file)
if __name__ == '__main__':
main()