-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompressor.cpp
More file actions
160 lines (122 loc) · 5.42 KB
/
decompressor.cpp
File metadata and controls
160 lines (122 loc) · 5.42 KB
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
/**
* @file decompressor.cpp
* @author Marián Tarageľ (xtarag01)
* @brief Implementation of decompression algorithm
* @date 8.5.2024
*/
#include <cstdint>
#include <vector>
#include <fstream>
#include "utils.hpp"
#include "huffman.hpp"
#include "model.hpp"
#include "decompressor.hpp"
void static_decompression(std::vector<uint8_t> buffer, unsigned long int model, const char *ofile)
{
std::vector<uint8_t> symbols;
std::vector<uint8_t> lengths;
for (uint16_t i = 0; i < 256; i++) {
lengths.push_back(read_bits(buffer, 8));
symbols.push_back(static_cast<uint8_t>(i));
}
auto p = sort_permutation(lengths);
symbols = apply_permutation(symbols, p);
lengths = apply_permutation(lengths, p);
// initialize codebook
std::vector<unsigned long int> count_lengths = count_bit_lengths(lengths);
std::pair<std::vector<unsigned long int>, std::vector<unsigned long int>> fcfs_pair = fcfs(count_lengths);
std::vector<unsigned long int> first_code = fcfs_pair.first;
std::vector<unsigned long int> first_symbol = fcfs_pair.second;
// decode symbols
std::vector<uint8_t> output = fast_decoder(buffer, first_code, first_symbol, symbols);
// apply inverse model
if (model)
output = inverse_diff_model(inverse_rle(output));
// write decompressed data to output file
std::ofstream binaryFile (ofile, std::ios::out | std::ios::binary);
for (uint8_t byte : output)
binaryFile << byte;
binaryFile.close();
}
void adaptive_decompression(std::vector<uint8_t> buffer, unsigned long int model, const char *ofile)
{
unsigned long int width = read_bits(buffer, 16);
unsigned long int block_width = width / ADAPTIVE_SCAN_WIDTH;
unsigned long int last_block_width = width % ADAPTIVE_SCAN_WIDTH;
std::vector<std::vector<uint8_t>> blocks;
unsigned long int size = 0;
unsigned long int current_block = 1;
while (buffer_index != buffer.size()) {
unsigned long int current_block_width = ADAPTIVE_SCAN_WIDTH;
if ((last_block_width) && (current_block % (block_width + 1) == 0))
current_block_width = last_block_width;
std::vector<uint8_t> block = decompress_block(buffer, model, current_block_width);
size += block.size();
blocks.push_back(block);
current_block++;
if (buffer_index == (buffer.size() - 1)) {
while (current_bit)
read_next_bit(buffer);
}
}
unsigned long int last_block_height = (size / width) % ADAPTIVE_SCAN_HEIGHT;
std::ofstream binaryFile (ofile, std::ios::out | std::ios::binary);
for (unsigned long int row_block = 0; row_block < blocks.size(); row_block+=block_width) {
unsigned long int block_height = ADAPTIVE_SCAN_HEIGHT;
if ((last_block_height) && (row_block + block_width + 1 == blocks.size()))
block_height = last_block_height;
for (unsigned long int block_height_index = 0; block_height_index < block_height; block_height_index++) {
for (unsigned long int row_block_index = 0; row_block_index < block_width; row_block_index++) {
std::vector<uint8_t> block = blocks[row_block + row_block_index];
for (unsigned long int i = 0; i < ADAPTIVE_SCAN_WIDTH; i++) {
binaryFile << block[block_height_index * ADAPTIVE_SCAN_WIDTH + i];
}
}
if (last_block_width) {
std::vector<uint8_t> block = blocks[row_block + block_width];
for (unsigned long int i = 0; i < last_block_width; i++) {
binaryFile << block[block_height_index * last_block_width + i];
}
}
}
if (last_block_width)
row_block++;
}
binaryFile.close();
}
std::vector<uint8_t> decompress_block(std::vector<uint8_t> buffer, unsigned long int model, unsigned long int current_block_width)
{
bool compression = read_next_bit(buffer);
if (!compression) {
std::vector<uint8_t> output;
unsigned long int size = read_bits(buffer, 10);
for (unsigned long int i = 0; i < size; i++) {
output.push_back(read_bits(buffer, 8));
}
if (model)
output = inverse_diff_model(inverse_rle(output));
return output;
}
unsigned long int count_elements = read_bits(buffer, 8);
std::vector<unsigned long int> count_lengths;
std::vector<uint8_t> symbols;
unsigned long int num_symbol = 0;
for (unsigned long int i = 0; i < count_elements; i++) {
unsigned long int len = read_bits(buffer, 9);
count_lengths.push_back(len);
num_symbol += len;
}
for (unsigned long int i = 0; i < num_symbol; i++)
symbols.push_back(read_bits(buffer, 8));
std::pair<std::vector<unsigned long int>, std::vector<unsigned long int>> fcfs_pair = fcfs(count_lengths);
std::vector<unsigned long int> first_code = fcfs_pair.first;
std::vector<unsigned long int> first_symbol = fcfs_pair.second;
unsigned long int size = read_bits(buffer, 10);
bool direction = read_next_bit(buffer); // horizontal -> 0, vertical -> 1
std::vector<uint8_t> output = fast_decoder(buffer, first_code, first_symbol, symbols, size);
if (model)
output = inverse_diff_model(inverse_rle(output));
if (direction)
output = transpose(output, current_block_width);
return output;
}