| 1 |
#include "SPERR3D_OMP_D.h" |
| 2 |
#include "SPERR3D_Stream_Tools.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 |
int main(int argc, char* argv[]) |
| 14 |
{ |
| 15 |
// Parse command line options |
| 16 |
CLI::App app( |
| 17 |
"Truncate a SPERR3D bitstream to keep a percentage of its original length.\n" |
| 18 |
"Optionally, it can also evaluate the compression quality after truncation.\n"); |
| 19 |
|
| 20 |
// Input specification |
| 21 |
auto input_file = std::string(); |
| 22 |
app.add_option("filename", input_file, "The original SPERR3D bitstream to be truncated.\n") |
| 23 |
->check(CLI::ExistingFile); |
| 24 |
|
| 25 |
// |
| 26 |
// Truncation settings |
| 27 |
// |
| 28 |
auto pct = uint32_t{0}; |
| 29 |
app.add_option("--pct", pct, "Percentage (1--100) of the original bitstream to truncate.") |
| 30 |
->required() |
| 31 |
->group("Truncation settings"); |
| 32 |
|
| 33 |
auto omp_num_threads = size_t{0}; // meaning to use the maximum number of threads. |
| 34 |
#ifdef USE_OMP |
| 35 |
app.add_option("--omp", omp_num_threads, |
| 36 |
"Number of OpenMP threads to use. Default (or 0) to use all.") |
| 37 |
->group("Truncation settings"); |
| 38 |
#endif |
| 39 |
|
| 40 |
// |
| 41 |
// Output settings |
| 42 |
// |
| 43 |
auto out_file = std::string(); |
| 44 |
app.add_option("-o", out_file, "Write out the truncated bitstream.")->group("Output settings"); |
| 45 |
|
| 46 |
// |
| 47 |
// Input settings |
| 48 |
// |
| 49 |
auto orig32_file = std::string(); |
| 50 |
app.add_option("--orig32", orig32_file, |
| 51 |
"Original raw data in 32-bit precision to calculate compression\n" |
| 52 |
"quality using the truncated bitstream.") |
| 53 |
->group("Input settings"); |
| 54 |
auto orig64_file = std::string(); |
| 55 |
app.add_option("--orig64", orig64_file, |
| 56 |
"Original raw data in 64-bit precision to calculate compression\n" |
| 57 |
"quality using the truncated bitstream.") |
| 58 |
->group("Input settings"); |
| 59 |
|
| 60 |
CLI11_PARSE(app, argc, argv); |
| 61 |
|
| 62 |
// |
| 63 |
// A little sanity check |
| 64 |
// |
| 65 |
if (!orig32_file.empty() && !orig64_file.empty()) { |
| 66 |
std::cout << "Is the original data in 32 or 64 bit precision?" << std::endl; |
| 67 |
return __LINE__; |
| 68 |
} |
| 69 |
|
| 70 |
// |
| 71 |
// Really starting the real work! |
| 72 |
// |
| 73 |
auto tool = sperr::SPERR3D_Stream_Tools(); |
| 74 |
auto stream_trunc = tool.progressive_read(input_file, pct); |
| 75 |
if (stream_trunc.empty()) { |
| 76 |
std::cout << "Error while truncating bitstream " << input_file << std::endl; |
| 77 |
return __LINE__; |
| 78 |
} |
| 79 |
auto header = tool.get_stream_header(stream_trunc.data()); |
| 80 |
auto total_vals = header.vol_dims[0] * header.vol_dims[1] * header.vol_dims[2]; |
| 81 |
auto real_bpp = stream_trunc.size() * 8.0 / double(total_vals); |
| 82 |
std::printf("Truncation resulting BPP = %.2f\n", real_bpp); |
| 83 |
|
| 84 |
if (!out_file.empty()) { |
| 85 |
auto rtn = sperr::write_n_bytes(out_file, stream_trunc.size(), stream_trunc.data()); |
| 86 |
if (rtn != sperr::RTNType::Good) |
| 87 |
return __LINE__; |
| 88 |
} |
| 89 |
|
| 90 |
// |
| 91 |
// Decompress the truncated bitstream and evaluate compression quality. |
| 92 |
// |
| 93 |
if (!orig32_file.empty() || !orig64_file.empty()) { |
| 94 |
auto decoder = std::make_unique<sperr::SPERR3D_OMP_D>(); |
| 95 |
decoder->set_num_threads(omp_num_threads); |
| 96 |
decoder->use_bitstream(stream_trunc.data(), stream_trunc.size()); |
| 97 |
auto rtn = decoder->decompress(stream_trunc.data()); |
| 98 |
if (rtn != sperr::RTNType::Good) { |
| 99 |
std::cout << "Decompression failed!" << std::endl; |
| 100 |
return __LINE__; |
| 101 |
} |
| 102 |
auto outputd = decoder->release_decoded_data(); |
| 103 |
decoder.reset(); |
| 104 |
stream_trunc.clear(); |
| 105 |
stream_trunc.shrink_to_fit(); |
| 106 |
|
| 107 |
double linfy = 0.0, psnr = 0.0, rmse = 0.0, sigma = 0.0; |
| 108 |
|
| 109 |
if (!orig64_file.empty()) { |
| 110 |
auto orig = sperr::read_whole_file<double>(orig64_file); |
| 111 |
if (orig.empty()) { |
| 112 |
std::cout << "Read original data failed: " << orig64_file << std::endl; |
| 113 |
return __LINE__; |
| 114 |
} |
| 115 |
auto stats = sperr::calc_stats(orig.data(), outputd.data(), total_vals, omp_num_threads); |
| 116 |
auto mean_var = sperr::calc_mean_var(orig.data(), total_vals, omp_num_threads); |
| 117 |
rmse = stats[0]; |
| 118 |
linfy = stats[1]; |
| 119 |
psnr = stats[2]; |
| 120 |
sigma = std::sqrt(mean_var[1]); |
| 121 |
} |
| 122 |
else { |
| 123 |
auto outputf = std::vector<float>(total_vals); |
| 124 |
std::copy(outputd.begin(), outputd.end(), outputf.begin()); |
| 125 |
outputd.clear(); |
| 126 |
outputd.shrink_to_fit(); |
| 127 |
|
| 128 |
auto orig = sperr::read_whole_file<float>(orig32_file); |
| 129 |
if (orig.empty()) { |
| 130 |
std::cout << "Read original data failed: " << orig32_file << std::endl; |
| 131 |
return __LINE__; |
| 132 |
} |
| 133 |
auto stats = sperr::calc_stats(orig.data(), outputf.data(), total_vals, omp_num_threads); |
| 134 |
auto mean_var = sperr::calc_mean_var(orig.data(), total_vals, omp_num_threads); |
| 135 |
rmse = stats[0]; |
| 136 |
linfy = stats[1]; |
| 137 |
psnr = stats[2]; |
| 138 |
sigma = std::sqrt(mean_var[1]); |
| 139 |
} |
| 140 |
|
| 141 |
std::printf("PSNR = %.2f, L-Infty = %.2e, Accuracy Gain = %.2f\n", psnr, linfy, |
| 142 |
std::log2(sigma / rmse) - real_bpp); |
| 143 |
} |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |