-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdmc_compressor_img.cpp
285 lines (256 loc) · 10.3 KB
/
dmc_compressor_img.cpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <string>
#include <iomanip>
#include <chrono>
#include <filesystem>
#include <iostream>
#include <fstream>
#include "dmc.hpp"
#include "myargs/myargs.hpp"
#include "the_matrix.hpp"
#include "img_load.hpp"
#include "mio.hpp"
#include "timed.hpp"
using myargs::Args;
using pack::DMC_decompressor;
using pack::DMCModelConfig;
using pack::DMC_compressor;
using namespace profiling;
inline std::vector<uint8_t> compress(const std::vector<the_matrix>& planes, bool lossless, int lines_pack_log2) {
timed comp_compresion_total_time("total component compression");
DMCModelConfig config;
config.threshold = 4;
config.bigthresh = 40;
config.reset_on_overflow = false;
config.maxnodes = 1ULL << 23;
DMC_compressor enc(config);
enc.put(lossless);
if (!lossless) {
enc.put_symbol(lines_pack_log2,0); //power of two
}
enc.put_symbol(planes.size(),0); //channels nb
enc.put_symbol(planes[0][0].size(),0); //width
enc.put_symbol(planes[0].size(),0); //height
size_t packed_size = 0;
for (auto& plane : planes ) {
timed comp_compresion_time("component compression");
int width = plane[0].size();
int height = plane.size();
if (lossless)
{
for (decltype( height) h=0; h<height; h++)
for (decltype( width) x=0; x<width; x++)
{
const auto left = x ? plane[h][x-1] :0;
const auto top = h ? plane[h-1][x] :0;
const auto topleft = (x && h) ? plane[h-1][x-1] :0;
const auto predict = median( top, left, top + left - topleft );
enc.put_symbol( plane[h][x] - predict, 1 );
}
} else {
int predict = 128;
#if 0
for (decltype( height) h=0; h<(height & ~1); h+=2)
{
int predict_1 = plane[h][0];
int predict_2 = plane[h+1][0];
enc.put_symbol( predict_1 - prev_2line, 1);
enc.put_symbol( predict_2 - predict_1,1);
prev_2line = predict_2;
for (decltype( width) x=1; x<width; x++)
{
int delta = int_to_tinyint( plane[h][x] - predict_1 );
predict_1 = std::clamp( predict_1 + tinyint_to_int(delta), 0, 255);
enc.put_symbol( delta, 1 );
delta = int_to_tinyint( plane[h+1][x] - predict_2 );
predict_2 = std::clamp( predict_2 + tinyint_to_int(delta), 0, 255);
enc.put_symbol( delta, 1 );
}
}
if (height & 1) {
for (int x = 0; x < width; x++)
{
int delta = int_to_tinyint( plane[height-1][x] - prev_2line );
prev_2line = std::clamp( prev_2line + tinyint_to_int(delta), 0, 255);
enc.put_symbol( delta, 1 );
}
}
#endif
int lines_pack = height;
int height_truncated = height;
if ( (1 << lines_pack_log2) < height )
{
lines_pack = (1 << lines_pack_log2);
height_truncated = height & ~(lines_pack-1);
}
std::vector<int> predict_lines(lines_pack);
for (int h = 0; h < height_truncated; h+=lines_pack)
{
for (int i = 0; i < lines_pack; ++i)
{
const int p = predict;
predict = predict_lines[i] = plane[h+i][0];
enc.put_symbol(predict - p, 1);
}
for (int x=1; x < width; ++x)
for (int i=0; i<lines_pack; i++) {
// const int delta = int_to_tinyint( plane[h+i][x] - predict_lines[i] );
// predict_lines[i] = std::clamp( predict_lines[i] + tinyint_to_int(delta),0,255);
// enc.put_symbol( delta, 1);
const int delta = int_to_tinyint( plane[h+i][x] - predict_lines[i] );
predict_lines[i] = std::clamp( predict_lines[i] + tinyint_to_int(delta),0,255);
enc.put_symbol(delta, 1);
}
}
lines_pack = ( height - height_truncated );
if (lines_pack > 0) {
for (int i=0; i<lines_pack; ++i) {
const int p = predict;
predict = predict_lines[i] = plane[height_truncated + i][0];
enc.put_symbol(predict - p, 1);
}
for (int x = 1; x < width; x++) {
for (int i=0; i<lines_pack; i++) {
const int delta = int_to_tinyint( plane[height_truncated + i][x] - predict_lines[i] );
predict_lines[i] = std::clamp( predict_lines[i] + tinyint_to_int(delta),0,255);
enc.put_symbol( delta, 1);
}
}
}
}
enc.reset_model();
}
return enc.finish();
}
template <typename Iterator>
inline std::vector<the_matrix> decompress(Iterator begin, Iterator end) {
DMCModelConfig config;
config.threshold = 4;
config.bigthresh = 40;
config.reset_on_overflow = false;
config.maxnodes = 1ULL << 23;
DMC_decompressor dec(begin, end, config);
bool lossless = dec.get();
int lines_pack_log2 = lossless ? 0 : dec.get_symbol(0);
auto planes = dec.get_symbol(0);
auto width = dec.get_symbol(0);
auto height = dec.get_symbol(0);
std::vector<the_matrix> result;
result.emplace_back(height, std::vector<int>(width) );
if (planes == 2) {
result.emplace_back(height, std::vector<int>(width) );
} else
if (planes > 2)
{
result.emplace_back((height+1)>>1, std::vector<int>((width+1)>>1));
result.emplace_back((height+1)>>1, std::vector<int>((width+1)>>1));
if (planes == 4)
result.emplace_back(height, std::vector<int>(width) );
}
for (auto & plane : result)
{
int width = plane[0].size();
int height = plane.size();
if (lossless)
{
for (int h=0; h<height; h++)
for (int x=0; x<width; x++)
{
const auto left = x ? plane[h][x-1] :0;
const auto top = h ? plane[h-1][x] :0;
const auto topleft = (x && h) ? plane[h-1][x-1] :0;
const auto predict = median( top, left, top + left - topleft );
plane[h][x] = dec.get_symbol(1) + predict;
}
} else {
int predict = 128;
int lines_pack = height;
int height_truncated = height;
if ( (1 << lines_pack_log2) < height )
{
lines_pack = (1 << lines_pack_log2);
height_truncated = height & ~(lines_pack-1);
}
std::vector<int> predict_lines( lines_pack );
for (decltype( height) h=0; h<height_truncated; h+=lines_pack)
{
#if 0
int predict_1 = dec.get_symbol(1) + prev_2line;
int predict_2 = dec.get_symbol(1) + predict_1;
prev_2line = predict_2;
plane[h][0] = predict_1;
plane[h+1][0] = predict_2;
#endif
for (int i = 0; i< lines_pack; ++i) {
predict = dec.get_symbol(1) + predict;
predict_lines[i] = plane[h+i][0] = predict;
}
for (decltype( width) x=1; x<width; x++)
{
#if 0
predict_1 = std::clamp( predict_1 + tinyint_to_int( dec.get_symbol(1) ), 0, 255);
predict_2 = std::clamp( predict_2 + tinyint_to_int( dec.get_symbol(1) ), 0, 255);
plane[h][x] = predict_1;
plane[h+1][x] = predict_2;
#endif
for (int i=0; i<lines_pack;++i)
{
predict_lines[i] = plane[h+i][x] = std::clamp( predict_lines[i] + tinyint_to_int( dec.get_symbol(1) ),0,255);
}
}
}
if (height_truncated < height)
{
lines_pack = height - height_truncated;
int h = height_truncated;
for (int i = 0; i< lines_pack; ++i) {
predict = dec.get_symbol(1) + predict;
predict_lines[i] = plane[h+i][0] = predict;
}
for (int x = 1; x < width; x++)
{
for (int i=0; i<lines_pack;++i)
{
predict_lines[i] = plane[h+i][x] = std::clamp( predict_lines[i] + tinyint_to_int( dec.get_symbol(1)) ,0,255);
}
}
}
}
dec.reset_model();
}
return result;
}
int main(int argc, char **argv)
{
Args args;
args.parse(argc, argv);
if (args.size() < 2) {
std::cout << "usage: \tdmc_img [-d] [--100] [-p<log2_lines>] <image_in> [image_out]\n";
return 1;
}
if (!args.has('d')) {
auto planes = load_image_as_yuv420(args[1].data());
auto packed= compress(planes, args.has("100"), args.get('p',1,0,15));
std::cout << "compressed size : " << packed.size() << std::endl;
std::ofstream output;
std::string output_filename = ( args.size() > 2 ) ? args[2] : args[1] + ".dmc";
std::cout << "create " << output_filename << std::endl;
output.open(output_filename, std::ios::binary | std::ios::out);
if ( output.fail() ) {
std::cerr << "can not create output file!!!" << std::endl;
}
else {
output.write(reinterpret_cast<const char *>(packed.data()),packed.size());
}
} else {
std::error_code error;
mio::ummap_source mmap = mio::make_mmap<mio::ummap_source>(args[1], 0, 0, error);
if (error)
{
std::cout << error.message() << std::endl;
return 1;
}
auto planes = decompress(mmap.cbegin(), mmap.cend());
std::string output_filename = ( args.size() > 2 ) ? args[2] : args[1] + ".bmp";
store_image_as_yuv420(planes, output_filename.data() );
}
}