-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.py
65 lines (52 loc) · 2.61 KB
/
decoder.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
import numpy as np
from HuffmanCode.huffman_decode import huffman_decode
from ZigzagTransform.reverse_zigzag_transform import reverse_zigzag_transform
from RunLength.Run_Length_Decocder import run_length_decoder
from Quantizer.dequantize import dequantize
from DCT.IDCT import IDCT
from DCT.IDCT_Basis import IDCT_Basis
from Blockify.deblockify_image import deblockify_image
def decoder(encoded_image, N, CompressionMode, VerticalPadding, HoriziontalPadding, HuffmanTree, no_vertical_blocks,no_horizontal_blocks):
"""
Decode the encoded image
:param encoded_image: encoded image
:param N: block size (N*N)
:param CompressionMode: Compression mode (HIGH, LOW)
:param VerticalPadding: Vertical padding length
:param HoriziontalPadding: Horizontal padding length
:param HuffmanTree: Huffman tree used for encoding
:param no_vertical_blocks: Number of vertical blocks
:param no_horizontal_blocks: Number of horizontal blocks
:return: decoded image
"""
# [1] apply Entropy decoding to encoded image
entropy_decoded_image = np.array(huffman_decode(encoded_image,HuffmanTree))
# [2] apply run-length decoding
runlength_decoded_image = run_length_decoder(entropy_decoded_image, no_vertical_blocks, no_horizontal_blocks, N)
# [3] apply reverse zigzag transform (1D to 2D)
#zigzag_transformed_image = np.zeros((no_vertical_blocks,no_horizontal_blocks,N,N))
blocks = np.zeros((no_vertical_blocks, no_horizontal_blocks ,N,N))
# image_total_pixels = no_vertical_blocks*no_horizontal_blocks*N*N
index = 0
for i in range(no_vertical_blocks):
for j in range(no_horizontal_blocks):
block = reverse_zigzag_transform(runlength_decoded_image[index:index+N*N],N)
blocks[i][j] = block.reshape((N, N))
index += N*N
# [4] apply dequantization to each block
dequantized_blocks = np.zeros(blocks.shape)
for i in range(no_vertical_blocks):
for j in range(no_horizontal_blocks):
dequantized_blocks[i][j] = dequantize(blocks[i][j], CompressionMode)
# [5] apply IDCT to each block
idct_basis = np.zeros((N,N,N,N))
for x in range(N):
for y in range(N):
idct_basis[x][y] = IDCT_Basis(x,y,N)
idct_blocks = np.zeros(blocks.shape)
for i in range(no_vertical_blocks):
for j in range(no_horizontal_blocks):
idct_blocks[i][j] = IDCT(dequantized_blocks[i][j], idct_basis)
# [6] deblockify the image
decoded_image = deblockify_image(idct_blocks)
return decoded_image[:decoded_image.shape[0] - VerticalPadding, :decoded_image.shape[1] - HoriziontalPadding]