Skip to content

Commit

Permalink
seperate bin file
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinyu Li committed Apr 9, 2024
1 parent ccf54ec commit eed343d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/bb/image-io/bb.h
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ class BinarySaver : public ion::BuildingBlock<BinarySaver<T, D>> {
public:
BuildingBlockParam<std::string> output_directory_ptr{ "output_directory", "." };
BuildingBlockParam<int32_t> num_devices{"num_devices", 2};
BuildingBlockParam<std::string> prefix_ptr{"prefix", "raw-"};

Input<Halide::Func> input_images{"input", Halide::type_of<T>(), D};
Input<Halide::Func> input_deviceinfo{ "input_deviceinfo", Halide::type_of<uint8_t>(), 1 };
Expand All @@ -1131,6 +1132,11 @@ class BinarySaver : public ion::BuildingBlock<BinarySaver<T, D>> {
output_directory_buf.fill(0);
std::memcpy(output_directory_buf.data(), output_directory.c_str(), output_directory.size());

const std::string prefix(prefix_ptr);
Halide::Buffer<uint8_t> prefix_buf(static_cast<int>(prefix.size() + 1));
prefix_buf.fill(0);
std::memcpy(prefix_buf.data(), prefix.c_str(), prefix.size());

Func fc;
fc(_) = frame_count(_);
fc.compute_root();
Expand All @@ -1148,7 +1154,7 @@ class BinarySaver : public ion::BuildingBlock<BinarySaver<T, D>> {
deviceinfo(_) = input_deviceinfo(_);
deviceinfo.compute_root();

std::vector<ExternFuncArgument> params = {id_buf, image, deviceinfo, fc, width, height, dim, byte_depth, output_directory_buf };
std::vector<ExternFuncArgument> params = {id_buf, image, deviceinfo, fc, width, height, dim, byte_depth, output_directory_buf, prefix_buf };
Func ion_bb_image_io_binary_image_saver;
ion_bb_image_io_binary_image_saver.define_extern("ion_bb_image_io_binary_image_saver", params, Int(32), 0);
ion_bb_image_io_binary_image_saver.compute_root();
Expand Down
20 changes: 12 additions & 8 deletions src/bb/image-io/rt_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ namespace {

class Writer {
public:
static Writer& get_instance(const std::string& id, std::vector<int32_t>& payload_size, const ::std::string& output_directory, bool write_framecount)
static Writer& get_instance(const std::string& id, std::vector<int32_t>& payload_size, const ::std::string& output_directory, bool write_framecount, const std::string& prefix = "raw-")
{

if (instances.count(id) == 0) {
instances[id] = std::unique_ptr<Writer>(new Writer(payload_size, output_directory, write_framecount));
instances[id] = std::unique_ptr<Writer>(new Writer(payload_size, output_directory, write_framecount, prefix ));
}
return *instances[id];
}
Expand Down Expand Up @@ -229,8 +229,8 @@ class Writer {
}

private:
Writer(std::vector<int32_t>& payload_size, const ::std::string& output_directory, bool write_framecount)
: keep_running_(true), output_directory_(output_directory), with_header_(true), disposed_(false)
Writer(std::vector<int32_t>& payload_size, const std::string& output_directory, bool write_framecount, const std::string& prefix)
: keep_running_(true), output_directory_(output_directory), with_header_(true), disposed_(false), prefix_(prefix)
{
int total_payload_size = 0;
for (auto s : payload_size){
Expand All @@ -246,7 +246,8 @@ class Writer {
buf_queue_.push(buffers_[i].data());
}
thread_ = ::std::make_shared<::std::thread>(entry_point, this);
ofs_ = ::std::ofstream(output_directory_ / "raw-0.bin", ::std::ios::binary);
auto filename = prefix_ + std::to_string(0) + ".bin";
ofs_ = ::std::ofstream(output_directory_ / filename, ::std::ios::binary);
}

int get_buffer_num(int width, int height, int num_sensor = 2, int data_in_byte = 2) {
Expand Down Expand Up @@ -294,7 +295,7 @@ class Writer {

if (i == rotate_limit) {
i = 0;
ofs_ = ::std::ofstream(output_directory_ / ("raw-" + ::std::to_string(file_idx++) + ".bin"), ::std::ios::binary);
ofs_ = ::std::ofstream(output_directory_ / (prefix_ + ::std::to_string(file_idx++) + ".bin"), ::std::ios::binary);
}

ofs_.write(reinterpret_cast<const char*>(buffer), size);
Expand All @@ -321,7 +322,7 @@ class Writer {

if (i == rotate_limit) {
i = 0;
ofs_ = ::std::ofstream(output_directory_ / ("raw-" + ::std::to_string(file_idx++) + ".bin"), ::std::ios::binary);
ofs_ = ::std::ofstream(output_directory_ / (prefix_ + ::std::to_string(file_idx++) + ".bin"), ::std::ios::binary);
}


Expand Down Expand Up @@ -353,6 +354,7 @@ class Writer {
uint32_t width_;
uint32_t height_;
std::filesystem::path output_directory_;
std::string prefix_;
bool disposed_;

bool with_header_;
Expand Down Expand Up @@ -478,6 +480,7 @@ int ion_bb_image_io_binary_image_saver(
halide_buffer_t * id_buf,
halide_buffer_t * image, halide_buffer_t * deviceinfo, halide_buffer_t * frame_count,
int width, int height, int dim, int byte_depth, halide_buffer_t* output_directory_buf,
halide_buffer_t* prefix_buf,
halide_buffer_t * out)
{
try {
Expand All @@ -486,7 +489,8 @@ int ion_bb_image_io_binary_image_saver(
int32_t frame_size = dim == 2 ? width * height * byte_depth : width * height * 3 * byte_depth;
std::vector<int32_t>frame_size_list{frame_size};
const ::std::string output_directory(reinterpret_cast<const char*>(output_directory_buf->host));
auto& w(Writer::get_instance(id, frame_size_list, output_directory, true));
const ::std::string prefix(reinterpret_cast<const char*>(prefix_buf->host));
auto& w(Writer::get_instance(id, frame_size_list, output_directory, true, prefix));

if (image->is_bounds_query() || deviceinfo->is_bounds_query() || frame_count->is_bounds_query()) {
if (image->is_bounds_query()) {
Expand Down

0 comments on commit eed343d

Please sign in to comment.