| 1 |
#include "SPECK2D_FLT.h" |
| 2 |
|
| 3 |
#include "CLI/App.hpp" |
| 4 |
#include "CLI/Config.hpp" |
| 5 |
#include "CLI/Formatter.hpp" |
| 6 |
|
| 7 |
#include <cassert> |
| 8 |
#include <cstdlib> |
| 9 |
#include <cstring> |
| 10 |
#include <iostream> |
| 11 |
|
| 12 |
// This functions takes in a filename, and a full resolution. It then creates a list of |
| 13 |
// filenames, each has the coarsened resolution appended. |
| 14 |
auto create_filenames(std::string name, sperr::dims_type dims) -> std::vector<std::string> |
| 15 |
{ |
| 16 |
auto filenames = std::vector<std::string>(); |
| 17 |
auto resolutions = sperr::coarsened_resolutions(dims); |
| 18 |
filenames.reserve(resolutions.size()); |
| 19 |
for (auto res : resolutions) |
| 20 |
filenames.push_back(name + "." + std::to_string(res[0]) + "x" + std::to_string(res[1])); |
| 21 |
|
| 22 |
return filenames; |
| 23 |
} |
| 24 |
|
| 25 |
// This function is used to output coarsened levels of the resolution hierarchy. |
| 26 |
auto output_hierarchy(const std::vector<std::vector<double>>& hierarchy, |
| 27 |
sperr::dims_type dims, |
| 28 |
const std::string& lowres_f64, |
| 29 |
const std::string& lowres_f32) -> int |
| 30 |
{ |
| 31 |
// If specified, output the low-res decompressed slices in double precision. |
| 32 |
if (!lowres_f64.empty()) { |
| 33 |
auto filenames = create_filenames(lowres_f64, dims); |
| 34 |
assert(hierarchy.size() == filenames.size()); |
| 35 |
for (size_t i = 0; i < filenames.size(); i++) { |
| 36 |
const auto& level = hierarchy[i]; |
| 37 |
auto rtn = sperr::write_n_bytes(filenames[i], level.size() * sizeof(double), level.data()); |
| 38 |
if (rtn != sperr::RTNType::Good) { |
| 39 |
std::cout << "Writing decompressed hierarchy failed: " << filenames[i] << std::endl; |
| 40 |
return __LINE__; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// If specified, output the low-res decompressed slices in single precision. |
| 46 |
if (!lowres_f32.empty()) { |
| 47 |
auto filenames = create_filenames(lowres_f32, dims); |
| 48 |
assert(hierarchy.size() == filenames.size()); |
| 49 |
auto buf = std::vector<float>(hierarchy.back().size()); |
| 50 |
for (size_t i = 0; i < filenames.size(); i++) { |
| 51 |
const auto& level = hierarchy[i]; |
| 52 |
std::copy(level.begin(), level.end(), buf.begin()); |
| 53 |
auto rtn = sperr::write_n_bytes(filenames[i], level.size() * sizeof(float), buf.data()); |
| 54 |
if (rtn != sperr::RTNType::Good) { |
| 55 |
std::cout << "Writing decompressed hierarchy failed: " << filenames[i] << std::endl; |
| 56 |
return __LINE__; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
// This function is used to output the decompressed slice at its native resolution. |
| 65 |
auto output_buffer(const sperr::vecd_type& buf, |
| 66 |
const std::string& name_f64, |
| 67 |
const std::string& name_f32) -> int |
| 68 |
{ |
| 69 |
// If specified, output the decompressed slice in double precision. |
| 70 |
if (!name_f64.empty()) { |
| 71 |
auto rtn = sperr::write_n_bytes(name_f64, buf.size() * sizeof(double), buf.data()); |
| 72 |
if (rtn != sperr::RTNType::Good) { |
| 73 |
std::cout << "Writing decompressed data failed: " << name_f64 << std::endl; |
| 74 |
return __LINE__; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// If specified, output the decompressed slice in single precision. |
| 79 |
if (!name_f32.empty()) { |
| 80 |
auto outputf = sperr::vecf_type(buf.size()); |
| 81 |
std::copy(buf.cbegin(), buf.cend(), outputf.begin()); |
| 82 |
auto rtn = sperr::write_n_bytes(name_f32, outputf.size() * sizeof(float), outputf.data()); |
| 83 |
if (rtn != sperr::RTNType::Good) { |
| 84 |
std::cout << "Writing decompressed data failed: " << name_f32 << std::endl; |
| 85 |
return __LINE__; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
int main(int argc, char* argv[]) |
| 93 |
{ |
| 94 |
// Parse command line options |
| 95 |
CLI::App app("2D SPERR compression and decompression\n"); |
| 96 |
|
| 97 |
// Input specification |
| 98 |
auto input_file = std::string(); |
| 99 |
app.add_option("filename", input_file, |
| 100 |
"A data slice to be compressed, or\n" |
| 101 |
"a bitstream to be decompressed.") |
| 102 |
->check(CLI::ExistingFile); |
| 103 |
|
| 104 |
// |
| 105 |
// Execution settings |
| 106 |
// |
| 107 |
auto cflag = bool{false}; |
| 108 |
auto* cptr = |
| 109 |
app.add_flag("-c", cflag, "Perform a compression task.")->group("Execution settings"); |
| 110 |
auto dflag = bool{false}; |
| 111 |
auto* dptr = app.add_flag("-d", dflag, "Perform a decompression task.") |
| 112 |
->excludes(cptr) |
| 113 |
->group("Execution settings"); |
| 114 |
|
| 115 |
// |
| 116 |
// Input properties |
| 117 |
// |
| 118 |
auto ftype = size_t{0}; |
| 119 |
app.add_option("--ftype", ftype, "Specify the input float type in bits. Must be 32 or 64.") |
| 120 |
->group("Input properties"); |
| 121 |
|
| 122 |
auto dim2d = std::array<uint32_t, 2>{0, 0}; |
| 123 |
app.add_option("--dims", dim2d, |
| 124 |
"Dimensions of the input slice. E.g., `--dims 128 128`\n" |
| 125 |
"(The fastest-varying dimension appears first.)") |
| 126 |
->group("Input properties"); |
| 127 |
|
| 128 |
// |
| 129 |
// Output settings |
| 130 |
// |
| 131 |
auto bitstream = std::string(); |
| 132 |
app.add_option("--bitstream", bitstream, "Output compressed bitstream.") |
| 133 |
->needs(cptr) |
| 134 |
->group("Output settings"); |
| 135 |
|
| 136 |
auto decomp_f32 = std::string(); |
| 137 |
app.add_option("--decomp_f", decomp_f32, "Output decompressed slice in f32 precision.") |
| 138 |
->group("Output settings"); |
| 139 |
|
| 140 |
auto decomp_f64 = std::string(); |
| 141 |
app.add_option("--decomp_d", decomp_f64, "Output decompressed slice in f64 precision.") |
| 142 |
->group("Output settings"); |
| 143 |
|
| 144 |
auto decomp_lowres_f32 = std::string(); |
| 145 |
app.add_option("--decomp_lowres_f", decomp_lowres_f32, |
| 146 |
"Output lower resolutions of the decompressed slice in f32 precision.") |
| 147 |
->group("Output settings"); |
| 148 |
|
| 149 |
auto decomp_lowres_f64 = std::string(); |
| 150 |
app.add_option("--decomp_lowres_d", decomp_lowres_f64, |
| 151 |
"Output lower resolutions of the decompressed slice in f64 precision.") |
| 152 |
->group("Output settings"); |
| 153 |
|
| 154 |
auto print_stats = bool{false}; |
| 155 |
app.add_flag("--print_stats", print_stats, "Show statistics measuring the compression quality.") |
| 156 |
->needs(cptr) |
| 157 |
->group("Output settings"); |
| 158 |
|
| 159 |
// |
| 160 |
// Compression settings |
| 161 |
// |
| 162 |
auto pwe = 0.0; |
| 163 |
auto* pwe_ptr = app.add_option("--pwe", pwe, "Maximum point-wise error (PWE) tolerance.") |
| 164 |
->group("Compression settings"); |
| 165 |
|
| 166 |
auto psnr = 0.0; |
| 167 |
auto* psnr_ptr = app.add_option("--psnr", psnr, "Target PSNR to achieve.") |
| 168 |
->excludes(pwe_ptr) |
| 169 |
->group("Compression settings"); |
| 170 |
|
| 171 |
auto bpp = 0.0; |
| 172 |
auto* bpp_ptr = app.add_option("--bpp", bpp, "Target bit-per-pixel (bpp) to achieve.") |
| 173 |
->check(CLI::Range(0.0, 64.0)) |
| 174 |
->excludes(pwe_ptr) |
| 175 |
->excludes(psnr_ptr) |
| 176 |
->group("Compression settings"); |
| 177 |
|
| 178 |
#ifdef EXPERIMENTING |
| 179 |
auto direct_q = 0.0; |
| 180 |
auto* dq_ptr = app.add_option("--dq", direct_q, "Directly provide the quantization step size q.") |
| 181 |
->excludes(bpp_ptr) |
| 182 |
->excludes(pwe_ptr) |
| 183 |
->excludes(psnr_ptr) |
| 184 |
->group("Compression settings"); |
| 185 |
#endif |
| 186 |
|
| 187 |
CLI11_PARSE(app, argc, argv); |
| 188 |
|
| 189 |
// |
| 190 |
// A little extra sanity check. |
| 191 |
// |
| 192 |
if (input_file.empty()) { |
| 193 |
std::cout << "What's the input file?" << std::endl; |
| 194 |
return __LINE__; |
| 195 |
} |
| 196 |
if (!cflag && !dflag) { |
| 197 |
std::cout << "Is this compressing (-c) or decompressing (-d) ?" << std::endl; |
| 198 |
return __LINE__; |
| 199 |
} |
| 200 |
if (cflag && dim2d == std::array{uint32_t{0}, uint32_t{0}}) { |
| 201 |
std::cout << "What's the dimensions of this 2D slice (--dims) ?" << std::endl; |
| 202 |
return __LINE__; |
| 203 |
} |
| 204 |
if (cflag && ftype != 32 && ftype != 64) { |
| 205 |
std::cout << "What's the floating-type precision (--ftype) ?" << std::endl; |
| 206 |
return __LINE__; |
| 207 |
} |
| 208 |
#ifndef EXPERIMENTING |
| 209 |
if (cflag && pwe == 0.0 && psnr == 0.0 && bpp == 0.0) { |
| 210 |
std::cout << "What's the compression quality (--psnr, --pwe, --bpp) ?" << std::endl; |
| 211 |
return __LINE__; |
| 212 |
} |
| 213 |
#endif |
| 214 |
if (cflag && (pwe < 0.0 || psnr < 0.0)) { |
| 215 |
std::cout << "Compression quality (--psnr, --pwe) must be positive!" << std::endl; |
| 216 |
return __LINE__; |
| 217 |
} |
| 218 |
if (dflag && decomp_f32.empty() && decomp_f64.empty() && decomp_lowres_f32.empty() && |
| 219 |
decomp_lowres_f64.empty()) { |
| 220 |
std::cout << "SPERR needs an output destination when decoding!" << std::endl; |
| 221 |
return __LINE__; |
| 222 |
} |
| 223 |
// Print a warning message if there's no output specified |
| 224 |
if (cflag && bitstream.empty()) |
| 225 |
std::cout << "Warning: no output file provided. Consider using --bitstream option." |
| 226 |
<< std::endl; |
| 227 |
if (dflag && decomp_f64.empty() && decomp_f32.empty() && decomp_lowres_f64.empty() && |
| 228 |
decomp_lowres_f32.empty()) |
| 229 |
std::cout << "Warning: no output file provided." << std::endl; |
| 230 |
|
| 231 |
// |
| 232 |
// Really starting the real work! |
| 233 |
// Note: the header has the same format as in SPERR3D_OMP_C. |
| 234 |
// |
| 235 |
const auto header_len = 10ul; |
| 236 |
auto input = sperr::read_whole_file<uint8_t>(input_file); |
| 237 |
if (cflag) { |
| 238 |
const auto dims = sperr::dims_type{dim2d[0], dim2d[1], 1ul}; |
| 239 |
const auto total_vals = dims[0] * dims[1] * dims[2]; |
| 240 |
if ((ftype == 32 && (total_vals * 4 != input.size())) || |
| 241 |
(ftype == 64 && (total_vals * 8 != input.size()))) { |
| 242 |
std::cout << "Input file size wrong!" << std::endl; |
| 243 |
return __LINE__ % 256; |
| 244 |
} |
| 245 |
auto encoder = std::make_unique<sperr::SPECK2D_FLT>(); |
| 246 |
encoder->set_dims(dims); |
| 247 |
if (ftype == 32) |
| 248 |
encoder->copy_data(reinterpret_cast<const float*>(input.data()), total_vals); |
| 249 |
else |
| 250 |
encoder->copy_data(reinterpret_cast<const double*>(input.data()), total_vals); |
| 251 |
|
| 252 |
if (pwe != 0.0) |
| 253 |
encoder->set_tolerance(pwe); |
| 254 |
else if (psnr != 0.0) |
| 255 |
encoder->set_psnr(psnr); |
| 256 |
#ifdef EXPERIMENTING |
| 257 |
else if (direct_q != 0) |
| 258 |
encoder->set_direct_q(direct_q); |
| 259 |
#endif |
| 260 |
else { |
| 261 |
assert(bpp != 0.0); |
| 262 |
encoder->set_bitrate(bpp); |
| 263 |
} |
| 264 |
|
| 265 |
// If not calculating stats, we can free up some memory now! |
| 266 |
if (!print_stats) { |
| 267 |
input.clear(); |
| 268 |
input.shrink_to_fit(); |
| 269 |
} |
| 270 |
|
| 271 |
auto rtn = encoder->compress(); |
| 272 |
if (rtn != sperr::RTNType::Good) { |
| 273 |
std::cout << "Compression failed!" << std::endl; |
| 274 |
return __LINE__ % 256; |
| 275 |
} |
| 276 |
|
| 277 |
// Assemble the output bitstream. |
| 278 |
auto stream = sperr::vec8_type(header_len); |
| 279 |
stream[0] = static_cast<uint8_t>(SPERR_VERSION_MAJOR); |
| 280 |
const auto b8 = std::array{false, // not a portion |
| 281 |
false, // 2D |
| 282 |
ftype == 32, |
| 283 |
false, // unused |
| 284 |
false, // unused |
| 285 |
false, // unused |
| 286 |
false, // unused |
| 287 |
false}; // unused |
| 288 |
stream[1] = sperr::pack_8_booleans(b8); |
| 289 |
std::memcpy(stream.data() + 2, dim2d.data(), sizeof(dim2d)); |
| 290 |
encoder->append_encoded_bitstream(stream); |
| 291 |
encoder.reset(); // Free up some more memory. |
| 292 |
|
| 293 |
// Output the compressed bitstream (maybe). |
| 294 |
if (!bitstream.empty()) { |
| 295 |
rtn = sperr::write_n_bytes(bitstream, stream.size(), stream.data()); |
| 296 |
if (rtn != sperr::RTNType::Good) { |
| 297 |
std::cout << "Writing compressed bitstream failed: " << bitstream << std::endl; |
| 298 |
return __LINE__ % 256; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
// |
| 303 |
// Need to do a decompression in the following cases. |
| 304 |
// |
| 305 |
const auto multi_res = (!decomp_lowres_f32.empty()) || (!decomp_lowres_f64.empty()); |
| 306 |
if (print_stats || !decomp_f64.empty() || !decomp_f32.empty() || multi_res) { |
| 307 |
auto decoder = std::make_unique<sperr::SPECK2D_FLT>(); |
| 308 |
decoder->set_dims(dims); |
| 309 |
// !! Remember the header thing !! |
| 310 |
decoder->use_bitstream(stream.data() + header_len, stream.size() - header_len); |
| 311 |
rtn = decoder->decompress(multi_res); |
| 312 |
if (rtn != sperr::RTNType::Good) { |
| 313 |
std::cout << "Decompression failed!" << std::endl; |
| 314 |
return __LINE__ % 256; |
| 315 |
} |
| 316 |
|
| 317 |
// Save the decompressed data, and then deconstruct the decoder to free up some memory! |
| 318 |
auto hierarchy = decoder->release_hierarchy(); |
| 319 |
auto outputd = decoder->release_decoded_data(); |
| 320 |
decoder.reset(); |
| 321 |
|
| 322 |
// Output the hierarchy (maybe), and then destroy it. |
| 323 |
auto ret = output_hierarchy(hierarchy, dims, decomp_lowres_f64, decomp_lowres_f32); |
| 324 |
if (ret) |
| 325 |
return __LINE__ % 256; |
| 326 |
hierarchy.clear(); |
| 327 |
hierarchy.shrink_to_fit(); |
| 328 |
|
| 329 |
// Output the decompressed slice (maybe). |
| 330 |
ret = output_buffer(outputd, decomp_f64, decomp_f32); |
| 331 |
if (ret) |
| 332 |
return __LINE__ % 256; |
| 333 |
|
| 334 |
// Calculate statistics. |
| 335 |
if (print_stats) { |
| 336 |
const double print_bpp = stream.size() * 8.0 / total_vals; |
| 337 |
double rmse, linfy, print_psnr, min, max, sigma; |
| 338 |
if (ftype == 32) { |
| 339 |
const float* inputf = reinterpret_cast<const float*>(input.data()); |
| 340 |
auto outputf = sperr::vecf_type(total_vals); |
| 341 |
std::copy(outputd.cbegin(), outputd.cend(), outputf.begin()); |
| 342 |
auto stats = sperr::calc_stats(inputf, outputf.data(), total_vals); |
| 343 |
rmse = stats[0]; |
| 344 |
linfy = stats[1]; |
| 345 |
print_psnr = stats[2]; |
| 346 |
min = stats[3]; |
| 347 |
max = stats[4]; |
| 348 |
auto mean_var = sperr::calc_mean_var(inputf, total_vals); |
| 349 |
sigma = std::sqrt(mean_var[1]); |
| 350 |
} |
| 351 |
else { |
| 352 |
const double* inputd = reinterpret_cast<const double*>(input.data()); |
| 353 |
auto stats = sperr::calc_stats(inputd, outputd.data(), total_vals); |
| 354 |
rmse = stats[0]; |
| 355 |
linfy = stats[1]; |
| 356 |
print_psnr = stats[2]; |
| 357 |
min = stats[3]; |
| 358 |
max = stats[4]; |
| 359 |
auto mean_var = sperr::calc_mean_var(inputd, total_vals); |
| 360 |
sigma = std::sqrt(mean_var[1]); |
| 361 |
} |
| 362 |
std::printf("Input range = (%.2e, %.2e), L-Infty = %.2e\n", min, max, linfy); |
| 363 |
std::printf("Bitrate = %.2f, PSNR = %.2fdB, Accuracy Gain = %.2f\n", print_bpp, print_psnr, |
| 364 |
std::log2(sigma / rmse) - print_bpp); |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
// |
| 369 |
// Decompression |
| 370 |
// |
| 371 |
else { |
| 372 |
assert(dflag); |
| 373 |
|
| 374 |
if (input[0] != (SPERR_VERSION_MAJOR)) { |
| 375 |
std::cout << "This bitstream is produced by a compressor of a different version!" |
| 376 |
<< std::endl; |
| 377 |
return __LINE__ % 256; |
| 378 |
} |
| 379 |
auto booleans = sperr::unpack_8_booleans(input[1]); |
| 380 |
if (booleans[1]) { |
| 381 |
std::cout << "This bitstream appears to represent a 3D volume!" << std::endl; |
| 382 |
return __LINE__ % 256; |
| 383 |
} |
| 384 |
|
| 385 |
// Retrieve the slice dimension from the header. |
| 386 |
std::memcpy(dim2d.data(), input.data() + 2, sizeof(dim2d)); |
| 387 |
const auto dims = sperr::dims_type{dim2d[0], dim2d[1], 1ul}; |
| 388 |
auto decoder = std::make_unique<sperr::SPECK2D_FLT>(); |
| 389 |
decoder->set_dims(dims); |
| 390 |
decoder->use_bitstream(input.data() + header_len, input.size() - header_len); |
| 391 |
const auto multi_res = (!decomp_lowres_f32.empty()) || (!decomp_lowres_f64.empty()); |
| 392 |
auto rtn = decoder->decompress(multi_res); |
| 393 |
if (rtn != sperr::RTNType::Good) { |
| 394 |
std::cout << "Decompression failed!" << std::endl; |
| 395 |
return __LINE__ % 256; |
| 396 |
} |
| 397 |
|
| 398 |
// Save the decompressed data, and then deconstruct the decoder to free up some memory! |
| 399 |
auto hierarchy = decoder->release_hierarchy(); |
| 400 |
auto outputd = decoder->release_decoded_data(); |
| 401 |
decoder.reset(); |
| 402 |
|
| 403 |
// Output the hierarchy (maybe). |
| 404 |
auto ret = output_hierarchy(hierarchy, dims, decomp_lowres_f64, decomp_lowres_f32); |
| 405 |
if (ret) |
| 406 |
return __LINE__ % 256; |
| 407 |
|
| 408 |
// Free up the hierarchy. |
| 409 |
hierarchy.clear(); |
| 410 |
hierarchy.shrink_to_fit(); |
| 411 |
|
| 412 |
// Output the decompressed slice (maybe). |
| 413 |
ret = output_buffer(outputd, decomp_f64, decomp_f32); |
| 414 |
if (ret) |
| 415 |
return __LINE__ % 256; |
| 416 |
} |
| 417 |
|
| 418 |
return 0; |
| 419 |
} |