使用 boost zlib 从 std::vector 中的文件中读取压缩的 zraw 数据
Read compressed zraw data from file in std::vector with boost zlib
我正在尝试使用 boost 和 zlib 读取压缩(3d 体素化图像)文件,但到目前为止没有成功。
我的目标是解压缩数据并将它们存储在 std::vector 中。除了 boost 文档的示例之外,是否还有来自此任务的更具体的示例?
这是我尝试做的示例:
std::vector<DATATYPE> data;
{
ifstream file_in("image.zraw", ios_base::in | ios_base::binary);
filtering_istream in;
in.push(zlib_decompressor());
in.push(file_in);
//copy somehow memory from in the vector data... DATATYPE is the type of data to be stored in the vector...
}
提前致谢,
康斯坦丁诺斯
既然你不能具体说明,我就运行假设一下:
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <fstream>
namespace bio = boost::iostreams;
template <typename T>
void write_data(std::string const& fname, std::vector<T> const& data) {
std::ofstream ofs(fname, std::ios_base::binary);
bio::filtering_ostream out;
out.push(bio::zlib_compressor());
out.push(ofs);
boost::archive::binary_oarchive oa(out);
oa << data;
}
template <typename T>
std::vector<T> read_data(std::string const& fname) {
std::vector<T> result;
std::ifstream file_in(fname, std::ios_base::binary);
bio::filtering_istream in;
in.push(bio::zlib_decompressor());
in.push(file_in);
boost::archive::binary_iarchive ia(in);
ia >> result;
return result;
}
#include <iostream>
int main() {
write_data("floats.zraw", std::vector<float> { 1,2,3 });
write_data("ints.zraw", std::vector<int> { 4,5,6 });
auto floats = read_data<float>("floats.zraw");
auto ints = read_data<int>("ints.zraw");
std::copy(floats.begin(), floats.end(), std::ostream_iterator<float>(std::cout << '\n', " "));
std::copy(ints .begin(), ints .end(), std::ostream_iterator<int >(std::cout << '\n', " "));
}
版画
1 2 3
4 5 6
它使用Boost Serialization 和Boost Iostreams 实现二进制(反)序列化为压缩文件。该格式是通用的,不限于 POD 数据类型,并且不 platform/library 版本无关。
解决我的问题我post可能对其他人有帮助的答案
previous stuff.....
// Clear the data storing vector of the image.
this->data_.clear();
// Reserve enough space for the data vector according to image's size.
this->data_.reserve(this->num_voxels_);
// Here happens the decompression.
{
std::ifstream file_in("../data/image.zraw", std::ios::in | std::ios_base::binary);
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::zlib_decompressor());
in.push(file_in);
// Write the data from the filtering istream to the data_ vector.
char *dptr = reinterpret_cast<char*>(this->data_.data());
// DATATYPE is the templated storage type of the vector.
for(int i=0;i<sizeof(DATATYPE)*this->num_voxels_;i++)
in.read(&dptr[i],1);
}
next stuff.......
我正在尝试使用 boost 和 zlib 读取压缩(3d 体素化图像)文件,但到目前为止没有成功。
我的目标是解压缩数据并将它们存储在 std::vector 中。除了 boost 文档的示例之外,是否还有来自此任务的更具体的示例?
这是我尝试做的示例:
std::vector<DATATYPE> data;
{
ifstream file_in("image.zraw", ios_base::in | ios_base::binary);
filtering_istream in;
in.push(zlib_decompressor());
in.push(file_in);
//copy somehow memory from in the vector data... DATATYPE is the type of data to be stored in the vector...
}
提前致谢, 康斯坦丁诺斯
既然你不能具体说明,我就运行假设一下:
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <fstream>
namespace bio = boost::iostreams;
template <typename T>
void write_data(std::string const& fname, std::vector<T> const& data) {
std::ofstream ofs(fname, std::ios_base::binary);
bio::filtering_ostream out;
out.push(bio::zlib_compressor());
out.push(ofs);
boost::archive::binary_oarchive oa(out);
oa << data;
}
template <typename T>
std::vector<T> read_data(std::string const& fname) {
std::vector<T> result;
std::ifstream file_in(fname, std::ios_base::binary);
bio::filtering_istream in;
in.push(bio::zlib_decompressor());
in.push(file_in);
boost::archive::binary_iarchive ia(in);
ia >> result;
return result;
}
#include <iostream>
int main() {
write_data("floats.zraw", std::vector<float> { 1,2,3 });
write_data("ints.zraw", std::vector<int> { 4,5,6 });
auto floats = read_data<float>("floats.zraw");
auto ints = read_data<int>("ints.zraw");
std::copy(floats.begin(), floats.end(), std::ostream_iterator<float>(std::cout << '\n', " "));
std::copy(ints .begin(), ints .end(), std::ostream_iterator<int >(std::cout << '\n', " "));
}
版画
1 2 3
4 5 6
它使用Boost Serialization 和Boost Iostreams 实现二进制(反)序列化为压缩文件。该格式是通用的,不限于 POD 数据类型,并且不 platform/library 版本无关。
解决我的问题我post可能对其他人有帮助的答案
previous stuff.....
// Clear the data storing vector of the image.
this->data_.clear();
// Reserve enough space for the data vector according to image's size.
this->data_.reserve(this->num_voxels_);
// Here happens the decompression.
{
std::ifstream file_in("../data/image.zraw", std::ios::in | std::ios_base::binary);
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::zlib_decompressor());
in.push(file_in);
// Write the data from the filtering istream to the data_ vector.
char *dptr = reinterpret_cast<char*>(this->data_.data());
// DATATYPE is the templated storage type of the vector.
for(int i=0;i<sizeof(DATATYPE)*this->num_voxels_;i++)
in.read(&dptr[i],1);
}
next stuff.......