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