-
Notifications
You must be signed in to change notification settings - Fork 46
/
encoder.py
170 lines (131 loc) · 5.26 KB
/
encoder.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import argparse
import os
import math
import numpy as np
from utils import *
from scipy import fftpack
from PIL import Image
from huffman import HuffmanTree
def quantize(block, component):
q = load_quantization_table(component)
return (block / q).round().astype(np.int32)
def block_to_zigzag(block):
return np.array([block[point] for point in zigzag_points(*block.shape)])
def dct_2d(image):
return fftpack.dct(fftpack.dct(image.T, norm='ortho').T, norm='ortho')
def run_length_encode(arr):
# determine where the sequence is ending prematurely
last_nonzero = -1
for i, elem in enumerate(arr):
if elem != 0:
last_nonzero = i
# each symbol is a (RUNLENGTH, SIZE) tuple
symbols = []
# values are binary representations of array elements using SIZE bits
values = []
run_length = 0
for i, elem in enumerate(arr):
if i > last_nonzero:
symbols.append((0, 0))
values.append(int_to_binstr(0))
break
elif elem == 0 and run_length < 15:
run_length += 1
else:
size = bits_required(elem)
symbols.append((run_length, size))
values.append(int_to_binstr(elem))
run_length = 0
return symbols, values
def write_to_file(filepath, dc, ac, blocks_count, tables):
try:
f = open(filepath, 'w')
except FileNotFoundError as e:
raise FileNotFoundError(
"No such directory: {}".format(
os.path.dirname(filepath))) from e
for table_name in ['dc_y', 'ac_y', 'dc_c', 'ac_c']:
# 16 bits for 'table_size'
f.write(uint_to_binstr(len(tables[table_name]), 16))
for key, value in tables[table_name].items():
if table_name in {'dc_y', 'dc_c'}:
# 4 bits for the 'category'
# 4 bits for 'code_length'
# 'code_length' bits for 'huffman_code'
f.write(uint_to_binstr(key, 4))
f.write(uint_to_binstr(len(value), 4))
f.write(value)
else:
# 4 bits for 'run_length'
# 4 bits for 'size'
# 8 bits for 'code_length'
# 'code_length' bits for 'huffman_code'
f.write(uint_to_binstr(key[0], 4))
f.write(uint_to_binstr(key[1], 4))
f.write(uint_to_binstr(len(value), 8))
f.write(value)
# 32 bits for 'blocks_count'
f.write(uint_to_binstr(blocks_count, 32))
for b in range(blocks_count):
for c in range(3):
category = bits_required(dc[b, c])
symbols, values = run_length_encode(ac[b, :, c])
dc_table = tables['dc_y'] if c == 0 else tables['dc_c']
ac_table = tables['ac_y'] if c == 0 else tables['ac_c']
f.write(dc_table[category])
f.write(int_to_binstr(dc[b, c]))
for i in range(len(symbols)):
f.write(ac_table[tuple(symbols[i])])
f.write(values[i])
f.close()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("input", help="path to the input image")
parser.add_argument("output", help="path to the output image")
args = parser.parse_args()
input_file = args.input
output_file = args.output
image = Image.open(input_file)
ycbcr = image.convert('YCbCr')
npmat = np.array(ycbcr, dtype=np.uint8)
rows, cols = npmat.shape[0], npmat.shape[1]
# block size: 8x8
if rows % 8 == cols % 8 == 0:
blocks_count = rows // 8 * cols // 8
else:
raise ValueError(("the width and height of the image "
"should both be mutiples of 8"))
# dc is the top-left cell of the block, ac are all the other cells
dc = np.empty((blocks_count, 3), dtype=np.int32)
ac = np.empty((blocks_count, 63, 3), dtype=np.int32)
for i in range(0, rows, 8):
for j in range(0, cols, 8):
try:
block_index += 1
except NameError:
block_index = 0
for k in range(3):
# split 8x8 block and center the data range on zero
# [0, 255] --> [-128, 127]
block = npmat[i:i+8, j:j+8, k] - 128
dct_matrix = dct_2d(block)
quant_matrix = quantize(dct_matrix,
'lum' if k == 0 else 'chrom')
zz = block_to_zigzag(quant_matrix)
dc[block_index, k] = zz[0]
ac[block_index, :, k] = zz[1:]
H_DC_Y = HuffmanTree(np.vectorize(bits_required)(dc[:, 0]))
H_DC_C = HuffmanTree(np.vectorize(bits_required)(dc[:, 1:].flat))
H_AC_Y = HuffmanTree(
flatten(run_length_encode(ac[i, :, 0])[0]
for i in range(blocks_count)))
H_AC_C = HuffmanTree(
flatten(run_length_encode(ac[i, :, j])[0]
for i in range(blocks_count) for j in [1, 2]))
tables = {'dc_y': H_DC_Y.value_to_bitstring_table(),
'ac_y': H_AC_Y.value_to_bitstring_table(),
'dc_c': H_DC_C.value_to_bitstring_table(),
'ac_c': H_AC_C.value_to_bitstring_table()}
write_to_file(output_file, dc, ac, blocks_count, tables)
if __name__ == "__main__":
main()