-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecompress.m
52 lines (46 loc) · 1.68 KB
/
decompress.m
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
function decompressed_data = decompress(input)
% Decompresses bitstream using Lempel-Ziv compression algorithm.
% Input: input = Compressed bitstream to decompress.
% Output: decompressed_data = Decompressed image bitstream.
% Initialize dictionary and variables.
keySet = {'0' '1'};
valueSet = [0 1];
lzw_dict = containers.Map(valueSet, keySet);
char = '';
val = 1;
b_len = 13; % Length of binary string.
% 1. input first code, store in OCODE
ocode_b = input(1:b_len);
ocode = bi2de(ocode_b, 'left-msb');
% 2. output translation of OCODE
decoded = lzw_dict(ocode);
for i = 2:(length(input)/b_len)
% 3. input next code, store in NCODE
ncode_b = input((i-1)*b_len+1:i*b_len);
ncode = bi2de(ncode_b, 'left-msb');
% 4. is NCODE in table?
if isKey(lzw_dict, ncode) % yes
% 7. STRING = translation of NCODE
string_ = lzw_dict(ncode);
else % no
% 5. string = translation of OCODE
string_ = lzw_dict(ocode);
% 6. STRING = STRING+CHAR
string_ = strcat(string_, char);
end
% 8. output STRING
decoded = strcat(decoded, string_);
% 9. CHAR = the first character in STRING
char = string_(1);
% 10. add entry in table for OCODE + CHAR
lzw_dict(val+1) = strcat(lzw_dict(ocode), char);
% 11. OCODE = NCODE
ocode = ncode;
val = val+1;
end
% Turn the decoded string into an array.
decompressed_data = zeros(size(decoded));
for i = 1:length(decoded)
decompressed_data(i) = str2double(decoded(i));
end
end