问候所有,
我加载了一组图像并生成了体积数据.我将这个体积数据保存在一个
unsigned char*volume
阵列.
现在我想将这个数组保存在一个文件中并检索.但在保存之前我想压缩字节数组,因为体积数据很大.
关于这个的任何提示?
提前致谢.
volume在您的示例中不是数组.至于压缩,有关于该主题的书籍.有关使用C++快速且易于使用的内容,请查看boost.iostream库,它随zlib,gzip和bzip2压缩器一起提供.
为了抵消我的挑剔,这里是一个例子(改为char因为它的unsigned chars 更加冗长)
#include <fstream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/copy.hpp>
namespace io = boost::iostreams;
int main()
{
const size_t N = 1000000;
char* volume = new char[N];
std::fill_n(volume, N, 'a'); // 100,000 letters 'a'
io::stream< io::array_source > source (volume, N);
{
std::ofstream file("volume.bz2", std::ios::out | std::ios::binary);
io::filtering_streambuf<io::output> outStream;
outStream.push(io::bzip2_compressor());
outStream.push(file);
io::copy(source, outStream);
}
// at this point, volume.bz2 is written and closed. It is 48 bytes long
}
Run Code Online (Sandbox Code Playgroud)