-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkko.cpp
More file actions
244 lines (206 loc) · 7.21 KB
/
Copy pathkko.cpp
File metadata and controls
244 lines (206 loc) · 7.21 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// KKO - image data compression using dictionary-based compression algorithm
// Last change: 13.5.2025
#include <stdint.h>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <getopt.h>
#include <fstream>
#include "utils.h"
#include "lzss.h"
#include "rle.h"
#include "blocks.h"
// parse arguments from command line, return them in a Params struct
struct Params parse_arguments(int argc, char **argv)
{
Params params;
std::string help_msg = "Usage: ./lz_codec [-c|-d] [-m] [-a] -i <input_file> -o <output_file> [-w <width>]";
int c;
while((c = getopt(argc, argv, "hcdmai:o:w:")) != -1)
{
switch (c)
{
case 'h':
std::cout << help_msg << std::endl;
exit(0);
case 'c':
params.compress = true;
break;
case 'd':
params.decompress = true;
break;
case 'm':
params.model = true;
break;
case 'a':
params.adaptive = true;
break;
case 'i':
params.input = std::string(optarg);
break;
case 'o':
params.output = std::string(optarg);
break;
case 'w':
params.width = atoi(optarg);
break;
default:
std::cerr << help_msg << std::endl;
exit(1);
}
}
if(params.compress && params.decompress)
throw std::runtime_error("wrong arguments: cannot compress and decompress at the same time");
if(!params.compress && !params.decompress)
throw std::runtime_error("wrong arguments: missing operation mode, use either -c or -d");
if(params.input.length() == 0)
throw std::runtime_error("wrong arguments: missing input file (-i)");
if(params.output.length() == 0)
throw std::runtime_error("wrong arguments: missing output file (-o)");
if(params.compress && (params.width < 1 || params.width % 256 != 0))
throw std::runtime_error("wrong arguments: width must be equal or larger than 1 and also be divisible by 256");
return params;
}
// read binary file (given a filepath), return a vector of bytes
std::vector<uint8_t> read_file(std::string file_name)
{
// open file
std::ifstream file(file_name, std::ios::binary);
if(!file)
{
std::cerr << "failed to open file: " << file_name << std::endl;
exit(1);
}
// get size
file.seekg(0, std::ios::end);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
// read into vector
std::vector<uint8_t> raw_data(size);
if(!file.read(reinterpret_cast<char*>(raw_data.data()), size))
{
std::cerr << "failed to read file: " + file_name << std::endl;
exit(1);
}
return raw_data;
}
// load metadata from raw file, also remove the metadata bits (and padding bits at the end)
struct Params load(struct Params params, BitBuffer &bits)
{
params.width = (ungamma_from_bitfield(bits) + 1) * 256;
params.height = (ungamma_from_bitfield(bits) + 1) * 256;
params.blocks = (ungamma_from_bitfield(bits));
params.model = bits.read(0, 1) ? true : false;
// next 3 bits are the number of padding bits
params.padding_bits = bits.read(1, 3);
//finish the byte
uint64_t used_bits = gamma(params.width / 256 - 1) + gamma(params.height / 256 - 1) + gamma(params.blocks) + 1 + 3;
uint64_t unused_bits = 8 - (used_bits % 8);
bits.remove(4 + unused_bits);
return params;
}
// save encoded data (+ metadata) to a file
bool save(struct Params params, BitBuffer bits)
{
// create output file
std::ofstream file(params.output, std::ios::binary);
if(!file)
{
std::cerr << "failed to write output" << std::endl;
exit(1);
}
// create header
BitBuffer header;
uint64_t width = (params.width / 256) - 1;
uint64_t width_gamma = gamma(width);
header.append(width+1, width_gamma);
uint64_t height = (params.width / 256) - 1;
uint64_t height_gamma = gamma(height);
header.append(height+1, height_gamma);
uint64_t blocks_gamma = gamma(params.blocks);
header.append(params.blocks+1, blocks_gamma);
header.append(params.model, 1);
// calculate padding size
uint64_t padding = (8 - ((bits.size()) % 8)) % 8;
// add padding to the data
header.append(padding, 3);
// write header to file
auto header_bytes = header.to_byte_vector();
file.write(reinterpret_cast<const char*>(header_bytes.data()), header_bytes.size());
// write data to file
auto data_bytes = bits.to_byte_vector();
file.write(reinterpret_cast<const char*>(data_bytes.data()), data_bytes.size());
file.close();
std::cout << "Saved as: " << params.output << "\nFilesize: " << (header_bytes.size() + data_bytes.size()) << " bytes" << std::endl;
std::cout << "Final BPC: " << static_cast<float>((header_bytes.size() + data_bytes.size()) * 8) / (params.input_size_bytes) << std::endl;
return true;
}
int main(int argc, char **argv)
{
// parse arguments
Params params;
try
{
params = parse_arguments(argc, argv);
}
catch(const std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
// ###########
// COMPRESSION
// ###########
if(params.compress)
{
// read data from file
std::vector<uint8_t> raw_data = read_file(params.input);
params.height = raw_data.size() / params.width;
params.input_size_bytes = raw_data.size();
// non-adaptive mode
BitBuffer h_compressed_lzss;
if(!params.adaptive)
{
h_compressed_lzss = compression_pipeline(params, raw_data);
save(params, h_compressed_lzss);
}
// block compression
BitBuffer block_compressed_lzss;
if(params.adaptive)
{
block_compressed_lzss = block_compress(params, raw_data);
params.blocks = BLOCK_SIZE;
save(params, block_compressed_lzss);
}
}
// #############
// DECOMPRESSION
// #############
else if(params.decompress)
{
// read data from file
std::vector<uint8_t> raw_data = read_file(params.input);
BitBuffer bits;
bits.load_from_byte_vector(raw_data);
// load metadata
params = load(params, bits);
bits.adjust_padding(params.padding_bits);
// decompress
std::vector<uint8_t> decompressed;
if(params.blocks > 0) // block compression was used
decompressed = block_decompress(params, bits);
else
decompressed = decompression_pipeline(params, bits);
// create output file and write the result
std::ofstream file(params.output, std::ios::binary);
if(!file)
{
std::cerr << "failed to write output" << std::endl;
return 1;
}
file.write(reinterpret_cast<const char*>(decompressed.data()), decompressed.size());
file.close();
std::cout << "Saved as: " << params.output << "\nFilesize: " << decompressed.size() << " bytes" << std::endl;
}
return 0;
}