diff --git a/src/bb/image-io/bb.h b/src/bb/image-io/bb.h index 31fb81cf..1a475403 100644 --- a/src/bb/image-io/bb.h +++ b/src/bb/image-io/bb.h @@ -691,6 +691,7 @@ class U3VCameraN : public ion::BuildingBlock> { GeneratorInput exposure{ "exposure", Halide::type_of(), 1}; GeneratorOutput output{ "output", Halide::type_of(), D}; + GeneratorOutput device_info{ "device_info", Halide::type_of(), 1}; GeneratorOutput frame_count{ "frame_count", Halide::type_of(), 1 }; void generate() { @@ -748,6 +749,34 @@ class U3VCameraN : public ion::BuildingBlock> { } } + Func u3v_device_info("u3v_device_info"); + { + const std::string pixel_format(pixel_format_ptr); + Buffer pixel_format_buf(static_cast(pixel_format.size() + 1)); + pixel_format_buf.fill(0); + std::memcpy(pixel_format_buf.data(), pixel_format.c_str(), pixel_format.size()); + + std::vector params{ + cameraN, dispose, static_cast(num_devices), static_cast(frame_sync), + static_cast(realtime_diaplay_mode), pixel_format_buf + }; + + device_info.resize(num_devices); + std::vector output_type; + for (int i = 0; i < device_info.size(); i++) { + output_type.push_back(Halide::type_of()); + } + u3v_device_info.define_extern("ion_bb_image_io_u3v_device_info" + std::to_string(device_info.size()), params, output_type, 1); + u3v_device_info.compute_root(); + if (device_info.size() == 1){ + device_info[0](_) = u3v_device_info(_); + }else{ + for (int i = 0; i < device_info.size(); i++) { + device_info[i](_) = u3v_device_info(_)[i]; + } + } + } + Func cameraN_fc("u3v_cameraN_fc"); { const std::string pixel_format(pixel_format_ptr); @@ -756,7 +785,8 @@ class U3VCameraN : public ion::BuildingBlock> { std::memcpy(pixel_format_buf.data(), pixel_format.c_str(), pixel_format.size()); std::vector params{ - cameraN, dispose, static_cast(output.size()), static_cast(frame_sync), static_cast(realtime_diaplay_mode), pixel_format_buf + cameraN, dispose, static_cast(output.size()), static_cast(frame_sync), + static_cast(realtime_diaplay_mode), pixel_format_buf }; cameraN_fc.define_extern("ion_bb_image_io_u3v_multiple_camera_frame_count" + std::to_string(output.size()), params, type_of(), 1); cameraN_fc.compute_root(); @@ -797,82 +827,98 @@ class U3VGenDC : public ion::BuildingBlock { exposure_func(_) = exposure(_); exposure_func.compute_root(); - const std::string pixel_format(pixel_format_ptr); - Buffer pixel_format_buf(static_cast(pixel_format.size() + 1)); - pixel_format_buf.fill(0); - std::memcpy(pixel_format_buf.data(), pixel_format.c_str(), pixel_format.size()); + Func u3v_gendc("u3v_gendc"); + { + const std::string pixel_format(pixel_format_ptr); + Buffer pixel_format_buf(static_cast(pixel_format.size() + 1)); + pixel_format_buf.fill(0); + std::memcpy(pixel_format_buf.data(), pixel_format.c_str(), pixel_format.size()); - const std::string gain_key(gain_key_ptr); - Buffer gain_key_buf(static_cast(gain_key.size() + 1)); - gain_key_buf.fill(0); - std::memcpy(gain_key_buf.data(), gain_key.c_str(), gain_key.size()); + const std::string gain_key(gain_key_ptr); + Buffer gain_key_buf(static_cast(gain_key.size() + 1)); + gain_key_buf.fill(0); + std::memcpy(gain_key_buf.data(), gain_key.c_str(), gain_key.size()); - const std::string exposure_key(exposure_key_ptr); - Buffer exposure_key_buf(static_cast(exposure_key.size() + 1)); - exposure_key_buf.fill(0); - std::memcpy(exposure_key_buf.data(), exposure_key.c_str(), exposure_key.size()); + const std::string exposure_key(exposure_key_ptr); + Buffer exposure_key_buf(static_cast(exposure_key.size() + 1)); + exposure_key_buf.fill(0); + std::memcpy(exposure_key_buf.data(), exposure_key.c_str(), exposure_key.size()); - std::vector params{ - dispose, static_cast(frame_sync), static_cast(realtime_diaplay_mode), - gain_func, exposure_func, pixel_format_buf, - gain_key_buf, exposure_key_buf - }; + std::vector params{ + dispose, static_cast(frame_sync), static_cast(realtime_diaplay_mode), + gain_func, exposure_func, pixel_format_buf, + gain_key_buf, exposure_key_buf + }; - Func u3v_gendc("u3v_gendc"); - gendc.resize(num_devices); - device_info.resize(num_devices); - std::vector output_type; - for (int i = 0; i < gendc.size() * 2; i++) { - output_type.push_back(Halide::type_of()); + gendc.resize(num_devices); + std::vector output_type; + for (int i = 0; i < gendc.size(); i++) { + output_type.push_back(Halide::type_of()); + } + u3v_gendc.define_extern("ion_bb_image_io_u3v_gendc_camera" + std::to_string(gendc.size()), params, output_type, 1); + u3v_gendc.compute_root(); + if (gendc.size() == 1){ + gendc[0](_) = u3v_gendc(_); + }else{ + for (int i = 0; i < gendc.size(); i++) { + gendc[i](_) = u3v_gendc(_)[i]; + } + } } - u3v_gendc.define_extern("ion_bb_image_io_u3v_gendc_camera" + std::to_string(gendc.size()), params, output_type, 1); - u3v_gendc.compute_root(); - for (int i = 0; i < gendc.size(); i++) { - gendc[i](_) = u3v_gendc(_)[2*i]; - device_info[i](_) = u3v_gendc(_)[2*i+1]; + + Func u3v_device_info("u3v_device_info"); + { + const std::string pixel_format(pixel_format_ptr); + Buffer pixel_format_buf(static_cast(pixel_format.size() + 1)); + pixel_format_buf.fill(0); + std::memcpy(pixel_format_buf.data(), pixel_format.c_str(), pixel_format.size()); + + std::vector params{ + u3v_gendc, dispose, static_cast(num_devices), static_cast(frame_sync), + static_cast(realtime_diaplay_mode), pixel_format_buf + }; + + device_info.resize(num_devices); + std::vector output_type; + for (int i = 0; i < device_info.size(); i++) { + output_type.push_back(Halide::type_of()); + } + u3v_device_info.define_extern("ion_bb_image_io_u3v_device_info" + std::to_string(device_info.size()), params, output_type, 1); + u3v_device_info.compute_root(); + if (device_info.size() == 1){ + device_info[0](_) = u3v_device_info(_); + }else{ + for (int i = 0; i < device_info.size(); i++) { + device_info[i](_) = u3v_device_info(_)[i]; + } + } } + } }; -class BinarySaver : public ion::BuildingBlock { +template +class BinarySaver : public ion::BuildingBlock> { public: GeneratorParam output_directory_ptr{ "output_directory", "." }; - GeneratorParam fps{ "fps", 1.0 }; - GeneratorParam r_gain0{ "r_gain0", 1.0 }; - GeneratorParam g_gain0{ "g_gain0", 1.0 }; - GeneratorParam b_gain0{ "b_gain0", 1.0 }; - - GeneratorParam r_gain1{ "r_gain1", 1.0 }; - GeneratorParam g_gain1{ "g_gain1", 1.0 }; - GeneratorParam b_gain1{ "b_gain1", 1.0 }; - - GeneratorParam offset0_x{ "offset0_x", 0 }; - GeneratorParam offset0_y{ "offset0_y", 0 }; - GeneratorParam offset1_x{ "offset1_x", 0 }; - GeneratorParam offset1_y{ "offset1_y", 0 }; - - GeneratorParam outputsize0_x{ "outputsize0_x", 1 }; - GeneratorParam outputsize0_y{ "outputsize0_y", 1 }; - GeneratorParam outputsize1_x{ "outputsize1_x", 1 }; - GeneratorParam outputsize1_y{ "outputsize1_y", 1 }; - - Input input0{ "input0", UInt(16), 2 }; - Input input1{ "input1", UInt(16), 2 }; - Input frame_count{ "frame_count", UInt(32), 1 }; - Input dispose{ "dispose" }; - Input width{ "width", 0 }; - Input height{ "height", 0 }; + GeneratorParam num_devices{"num_devices", 2}; + + GeneratorInput input_images{ "input_images", Halide::type_of(), D }; + + GeneratorInput input_deviceinfo{ "input_deviceinfo", Halide::type_of(), 1 }; + GeneratorInput frame_count{ "frame_count", Halide::type_of(), 1 }; + + GeneratorInput dispose{ "dispose" }; + GeneratorInput width{ "width" }; + GeneratorInput height{ "height" }; + GeneratorInput color_channel{ "color_channel" }; + + GeneratorOutput output{"output"}; - Output output{ "output" }; void generate() { using namespace Halide; - Func in0; - in0(_) = input0(_); - in0.compute_root(); - Func in1; - in1(_) = input1(_); - in1.compute_root(); + int32_t num_gendc = static_cast(num_devices); const std::string output_directory(output_directory_ptr); Halide::Buffer output_directory_buf(static_cast(output_directory.size() + 1)); @@ -882,34 +928,66 @@ class BinarySaver : public ion::BuildingBlock { Func fc; fc(_) = frame_count(_); fc.compute_root(); - std::vector params = { in0, in1, fc, dispose, width, height, output_directory_buf, - static_cast(r_gain0), static_cast(g_gain0), static_cast(b_gain0), - static_cast(r_gain1), static_cast(g_gain1), static_cast(b_gain1), - static_cast(offset0_x), static_cast(offset0_x), - static_cast(offset0_x), static_cast(offset1_y), - static_cast(outputsize0_x), static_cast(outputsize0_y), - static_cast(outputsize1_x), static_cast(outputsize1_y), - cast(fps) }; - Func binarysaver; - binarysaver.define_extern("binarysaver", params, Int(32), 0); - binarysaver.compute_root(); - output() = binarysaver(); + + int32_t dim = D; + int32_t byte_depth = sizeof(T); + + if (num_gendc==1){ + Func image; + image(_) = input_images(_); + image.compute_root(); + + Func deviceinfo; + deviceinfo(_) = input_deviceinfo(_); + deviceinfo.compute_root(); + + std::vector params = { image, deviceinfo, fc, dispose, width, height, dim, byte_depth, output_directory_buf }; + Func ion_bb_image_io_binary_image_saver; + ion_bb_image_io_binary_image_saver.define_extern("ion_bb_image_io_binary_1image_saver", params, Int(32), 0); + ion_bb_image_io_binary_image_saver.compute_root(); + output() = ion_bb_image_io_binary_image_saver(); + }else if (num_devices==2){ + Func image0, image1; + image0(_) = input_images[0](_); + image1(_) = input_images[1](_); + image0.compute_root(); + image1.compute_root(); + + Func deviceinfo0, deviceinfo1; + deviceinfo0(_) = input_deviceinfo[0](_); + deviceinfo1(_) = input_deviceinfo[1](_); + deviceinfo0.compute_root(); + deviceinfo1.compute_root(); + + std::vector params = { image0, image1, deviceinfo0, deviceinfo1, fc, dispose, width, height, dim, byte_depth, output_directory_buf }; + Func ion_bb_image_io_binary_image_saver; + ion_bb_image_io_binary_image_saver.define_extern("ion_bb_image_io_binary_2image_saver", params, Int(32), 0); + ion_bb_image_io_binary_image_saver.compute_root(); + output() = ion_bb_image_io_binary_image_saver(); + }else{ + std::runtime_error("device number > 2 is not supported"); + } } }; + +using BinarySaver_U8x3 = BinarySaver; +using BinarySaver_U8x2 = BinarySaver; +using BinarySaver_U16x2 = BinarySaver; + class BinaryGenDCSaver : public ion::BuildingBlock { public: GeneratorParam output_directory_ptr{ "output_directory", "." }; GeneratorParam num_devices{"num_devices", 2}; - Input input_gendc{ "input_gendc", Halide::type_of(), 1 }; - Input input_deviceinfo{ "input_deviceinfo", Halide::type_of(), 1 }; + GeneratorInput input_gendc{ "input_gendc", Halide::type_of(), 1 }; + GeneratorInput input_deviceinfo{ "input_deviceinfo", Halide::type_of(), 1 }; - Input dispose{ "dispose" }; - Input payloadsize{ "payloadsize" }; + GeneratorInput dispose{ "dispose" }; + GeneratorInput payloadsize{ "payloadsize" }; - Output output{ "output" }; + GeneratorOutput output{ "output" }; void generate() { using namespace Halide; @@ -1033,7 +1111,11 @@ ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::U3VCameraN_U16x2, image_io_u3v_ca ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::U3VGenDC, image_io_u3v_gendc); -ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::BinarySaver, image_io_binarysaver); +ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::BinarySaver_U16x2, image_io_binarysaver); +ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::BinarySaver_U8x3, image_io_binarysaver_u8x3); +ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::BinarySaver_U8x2, image_io_binarysaver_u8x2); +ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::BinarySaver_U16x2, image_io_binarysaver_u16x2); + ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::BinaryLoader, image_io_binaryloader); ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::BinaryGenDCSaver, image_io_binary_gendc_saver); diff --git a/src/bb/image-io/rt_file.h b/src/bb/image-io/rt_file.h index 85e29f24..18cfeba8 100644 --- a/src/bb/image-io/rt_file.h +++ b/src/bb/image-io/rt_file.h @@ -12,6 +12,8 @@ #include "json/json.hpp" +#include "log.h" + #include "rt_common.h" #include "httplib.h" @@ -132,20 +134,11 @@ namespace { class Writer { public: - static Writer& get_instance(int width, int height, const ::std::string& output_directory, ion::bb::image_io::rawHeader header_info) - { - auto itr = instances.find(output_directory); - if (itr == instances.end()) { - instances[output_directory] = std::unique_ptr(new Writer(width, height, output_directory, header_info)); - } - return *instances[output_directory]; - } - - static Writer& get_instance(int total_payload_size, const ::std::string& output_directory, bool config_file) + static Writer& get_instance(std::vector& payload_size, const ::std::string& output_directory, bool write_framecount) { auto itr = instances.find(output_directory); if (itr == instances.end()) { - instances[output_directory] = std::unique_ptr(new Writer(total_payload_size, output_directory, config_file)); + instances[output_directory] = std::unique_ptr(new Writer(payload_size, output_directory, write_framecount)); } return *instances[output_directory]; } @@ -154,8 +147,12 @@ class Writer { dispose(); } - void post(uint32_t frame_count, const uint8_t* ptr0, const uint8_t* ptr1, size_t size) + void post_images(std::vector& outs, std::vector& size, + std::vector& header_infos, void* framecounts) { + if (with_header_){ + write_config_file(header_infos); + } ::std::unique_lock<::std::mutex> lock(mutex_); buf_cv_.wait(lock, [&] { return !buf_queue_.empty() || ep_; }); if (ep_) { @@ -163,13 +160,17 @@ class Writer { } uint8_t* buffer = buf_queue_.front(); buf_queue_.pop(); - ::std::memcpy(buffer, ptr0, size); - ::std::memcpy(buffer + size, ptr1, size); - task_queue_.push(::std::make_tuple(frame_count, buffer, 2 * size)); + size_t offset = 0; + for (int i = 0; i < outs.size(); ++i){ + ::std::memcpy(buffer + offset, reinterpret_cast(framecounts) + i, sizeof(int32_t)); + offset += sizeof(int32_t); + ::std::memcpy(buffer + offset, outs[i], size[i]); + offset += size[i]; + } + task_queue_.push(::std::make_tuple(0, buffer, offset)); task_cv_.notify_one(); } - void post_gendc(std::vector& outs, std::vector& size, std::vector& header_infos) { if (with_header_){ @@ -225,51 +226,23 @@ class Writer { } private: - Writer(int width, int height, const ::std::string& output_directory, - ion::bb::image_io::rawHeader header_info) - : keep_running_(true), width_(width), height_(height), output_directory_(output_directory), - header_info_(header_info), with_header_(true), with_framecount_(true) + Writer(std::vector& payload_size, const ::std::string& output_directory, bool write_framecount) + : keep_running_(true), output_directory_(output_directory), with_header_(true) { - int buffer_num = get_buffer_num(width, height); - for (int i = 0; i < buffer_num; ++i) { - buffers_.emplace_back(2 * width * height * sizeof(uint16_t)); - 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); - - // write header (size is 512) - ofs_.write(reinterpret_cast(&header_info_), sizeof(header_info_)); - char padding_item = '0'; - for (int i = 0; i < 512 - sizeof(header_info_); ++i) { - ofs_.write(reinterpret_cast(&padding_item), sizeof(padding_item)); + int total_payload_size = 0; + for (auto s : payload_size){ + total_payload_size += s; + if (write_framecount){ + total_payload_size += sizeof(int32_t); + } } - } - - Writer(int total_payload_size, const ::std::string& output_directory, bool config_file) - : keep_running_(true), output_directory_(output_directory), with_header_(!config_file), with_framecount_(false) - { int buffer_num = get_buffer_num(total_payload_size); + for (int i = 0; i < buffer_num; ++i) { buffers_.emplace_back(total_payload_size); buf_queue_.push(buffers_[i].data()); } thread_ = ::std::make_shared<::std::thread>(entry_point, this); - - // nlohmann::json j; - // j["num_device"] = header_infos.size(); - // for (int i = 0; i < header_infos.size(); ++i){ - // nlohmann::json j_ith_sensor; - // j_ith_sensor["framerate"] = header_infos[i].fps_; - // j_ith_sensor["width"] = header_infos[i].width_; - // j_ith_sensor["height"] = header_infos[i].height_; - // j_ith_sensor["pfnc_pixelformat"] = header_infos[i].pfnc_pixelformat; - // j["sensor" + std::to_string(i+1)] = j_ith_sensor; - // } - - // ::std::ofstream config(output_directory_ / "config.json"); - // config << std::setw(4) << j << std::endl; - // config.close(); ofs_ = ::std::ofstream(output_directory_ / "raw-0.bin", ::std::ios::binary); } @@ -319,20 +292,8 @@ class Writer { if (i == rotate_limit) { i = 0; ofs_ = ::std::ofstream(output_directory_ / ("raw-" + ::std::to_string(file_idx++) + ".bin"), ::std::ios::binary); - - if (with_header_){ - // write header (size is 512) - ofs_.write(reinterpret_cast(&header_info_), sizeof(header_info_)); - char padding_item = '0'; - for (int i = 0; i < 512 - sizeof(header_info_); ++i) { - ofs_.write(reinterpret_cast(&padding_item), sizeof(padding_item)); - } - } } - if (with_framecount_){ - ofs_.write(reinterpret_cast(&frame_count), sizeof(frame_count)); - } ofs_.write(reinterpret_cast(buffer), size); { @@ -358,20 +319,9 @@ class Writer { if (i == rotate_limit) { i = 0; ofs_ = ::std::ofstream(output_directory_ / ("raw-" + ::std::to_string(file_idx++) + ".bin"), ::std::ios::binary); - - if (with_header_){ - // write header (size is 512) - ofs_.write(reinterpret_cast(&header_info_), sizeof(header_info_)); - char padding_item = '0'; - for (int i = 0; i < 512 - sizeof(header_info_); ++i) { - ofs_.write(reinterpret_cast(&padding_item), sizeof(padding_item)); - } - } } - if (with_framecount_){ - ofs_.write(reinterpret_cast(&frame_count), sizeof(frame_count)); - } + ofs_.write(reinterpret_cast(buffer), size); { @@ -401,55 +351,50 @@ class Writer { uint32_t height_; std::filesystem::path output_directory_; - ion::bb::image_io::rawHeader header_info_; bool with_header_; - bool with_framecount_; }; ::std::unordered_map< ::std::string, std::unique_ptr> Writer::instances; // defines Writer::instance } // namespace - extern "C" ION_EXPORT -int binarysaver(halide_buffer_t * in0, halide_buffer_t * in1, halide_buffer_t * fc, - bool dispose, int width, int height, halide_buffer_t* output_directory_buf, - float r_gain0, float g_gain0, float b_gain0, float r_gain1, float g_gain1, float b_gain1, - int offset0_x, int offset0_y, int offset1_x, int offset1_y, - int outputsize0_x, int outputsize0_y, int outputsize1_x, int outputsize1_y, float fps, +int ion_bb_image_io_binary_2gendc_saver(halide_buffer_t * in0, halide_buffer_t * in1, halide_buffer_t * in2, halide_buffer_t * in3, + bool dispose, int payloadsize, halide_buffer_t* output_directory_buf, halide_buffer_t * out) { try { const ::std::string output_directory(reinterpret_cast(output_directory_buf->host)); - ion::bb::image_io::rawHeader header_info = { - 1, width, height, r_gain0, g_gain0, b_gain0, r_gain1, g_gain1, b_gain1, - offset0_x, offset0_y, offset1_x, offset1_y, - outputsize0_x, outputsize0_y, outputsize1_x, outputsize1_y, fps, PFNC_Mono12}; - - auto& w(Writer::get_instance(width, height, output_directory, header_info)); - if (in0->is_bounds_query() || in1->is_bounds_query()) { + std::vectorpayloadsize_list{payloadsize, payloadsize}; + auto& w(Writer::get_instance(payloadsize_list, output_directory, false)); + if (in0->is_bounds_query() || in1->is_bounds_query() || in2->is_bounds_query() || in3->is_bounds_query()) { + int i = 1; if (in0->is_bounds_query()) { in0->dim[0].min = 0; - in0->dim[0].extent = width; - in0->dim[1].min = 0; - in0->dim[1].extent = height; + in0->dim[0].extent = payloadsize; } if (in1->is_bounds_query()) { in1->dim[0].min = 0; - in1->dim[0].extent = width; - in1->dim[1].min = 0; - in1->dim[1].extent = height; + in1->dim[0].extent = payloadsize; } - if (fc->is_bounds_query()) { - if (fc->is_bounds_query()) { - fc->dim[0].min = 0; - fc->dim[0].extent = 1; - } - + if (in2->is_bounds_query()) { + in2->dim[0].min = 0; + in2->dim[0].extent = sizeof(ion::bb::image_io::rawHeader); } + if (in3->is_bounds_query()) { + in3->dim[0].min = 0; + in3->dim[0].extent = sizeof(ion::bb::image_io::rawHeader); + } + return 0; } else { - const uint32_t frame_count = reinterpret_cast(fc->host)[0]; - w.post(frame_count, in0->host, in1->host, in0->size_in_bytes()); + ion::bb::image_io::rawHeader header_info0, header_info1; + ::memcpy(&header_info0, in2->host, sizeof(ion::bb::image_io::rawHeader)); + ::memcpy(&header_info1, in3->host, sizeof(ion::bb::image_io::rawHeader)); + std::vector header_infos{header_info0, header_info1}; + + std::vector obufs{in0->host, in1->host}; + std::vector size_in_bytes{in0->size_in_bytes(), in1->size_in_bytes()}; + w.post_gendc(obufs, size_in_bytes, header_infos); if (dispose) { w.dispose(); @@ -470,44 +415,35 @@ int binarysaver(halide_buffer_t * in0, halide_buffer_t * in1, halide_buffer_t * } } -ION_REGISTER_EXTERN(binarysaver); +ION_REGISTER_EXTERN(ion_bb_image_io_binary_2gendc_saver); extern "C" ION_EXPORT -int ion_bb_image_io_binary_2gendc_saver(halide_buffer_t * in0, halide_buffer_t * in1, halide_buffer_t * in2, halide_buffer_t * in3, +int ion_bb_image_io_binary_1gendc_saver(halide_buffer_t * gendc, halide_buffer_t * deviceinfo, bool dispose, int payloadsize, halide_buffer_t* output_directory_buf, halide_buffer_t * out) { try { const ::std::string output_directory(reinterpret_cast(output_directory_buf->host)); - auto& w(Writer::get_instance(payloadsize + payloadsize, output_directory, false)); - if (in0->is_bounds_query() || in1->is_bounds_query() || in2->is_bounds_query() || in3->is_bounds_query()) { - int i = 1; - if (in0->is_bounds_query()) { - in0->dim[0].min = 0; - in0->dim[0].extent = payloadsize; - } - if (in1->is_bounds_query()) { - in1->dim[0].min = 0; - in1->dim[0].extent = payloadsize; + std::vectorpayloadsize_list{payloadsize}; + auto& w(Writer::get_instance(payloadsize_list, output_directory, false)); + if (gendc->is_bounds_query() || deviceinfo->is_bounds_query()) { + if (gendc->is_bounds_query()) { + gendc->dim[0].min = 0; + gendc->dim[0].extent = payloadsize; } - if (in2->is_bounds_query()) { - in2->dim[0].min = 0; - in2->dim[0].extent = 76; - } - if (in3->is_bounds_query()) { - in3->dim[0].min = 0; - in3->dim[0].extent = 76; + if (deviceinfo->is_bounds_query()) { + deviceinfo->dim[0].min = 0; + deviceinfo->dim[0].extent = sizeof(ion::bb::image_io::rawHeader); } return 0; } else { - ion::bb::image_io::rawHeader header_info0, header_info1; - ::memcpy(&header_info0, in2->host, sizeof(ion::bb::image_io::rawHeader)); - ::memcpy(&header_info1, in3->host, sizeof(ion::bb::image_io::rawHeader)); - std::vector header_infos{header_info0, header_info1}; + ion::bb::image_io::rawHeader header_info0; + ::memcpy(&header_info0, deviceinfo->host, sizeof(ion::bb::image_io::rawHeader)); + std::vector header_infos{header_info0}; - std::vector obufs{in0->host, in1->host}; - std::vector size_in_bytes{in0->size_in_bytes(), in1->size_in_bytes()}; + std::vector obufs{gendc->host}; + std::vector size_in_bytes{gendc->size_in_bytes()}; w.post_gendc(obufs, size_in_bytes, header_infos); if (dispose) { @@ -529,40 +465,136 @@ int ion_bb_image_io_binary_2gendc_saver(halide_buffer_t * in0, halide_buffer_t * } } -ION_REGISTER_EXTERN(ion_bb_image_io_binary_2gendc_saver); +ION_REGISTER_EXTERN(ion_bb_image_io_binary_1gendc_saver); extern "C" ION_EXPORT -int ion_bb_image_io_binary_1gendc_saver(halide_buffer_t * in0, halide_buffer_t * in1, - bool dispose, int payloadsize, halide_buffer_t* output_directory_buf, +int ion_bb_image_io_binary_1image_saver( + halide_buffer_t * image, halide_buffer_t * deviceinfo, halide_buffer_t * frame_count, + bool dispose, int width, int height, int dim, int byte_depth, halide_buffer_t* output_directory_buf, halide_buffer_t * out) { try { + int num_output = 1; + int32_t frame_size = dim == 2 ? width * height * byte_depth : width * height * 3 * byte_depth; + std::vectorframe_size_list{frame_size}; const ::std::string output_directory(reinterpret_cast(output_directory_buf->host)); - auto& w(Writer::get_instance(payloadsize, output_directory, false)); - if (in0->is_bounds_query() || in1->is_bounds_query()) { - if (in0->is_bounds_query()) { - in0->dim[0].min = 0; - in0->dim[0].extent = payloadsize; + auto& w(Writer::get_instance(frame_size_list, output_directory, true)); + + if (image->is_bounds_query() || deviceinfo->is_bounds_query() || frame_count->is_bounds_query()) { + if (image->is_bounds_query()) { + image->dim[0].min = 0; + image->dim[0].extent = width; + image->dim[1].min = 0; + image->dim[1].extent = height; + if (dim == 3){ + image->dim[2].min = 0; + image->dim[2].extent = 3; + } } - if (in1->is_bounds_query()) { - in1->dim[0].min = 0; - in1->dim[0].extent = 76; + if (deviceinfo->is_bounds_query()) { + deviceinfo->dim[0].min = 0; + deviceinfo->dim[0].extent = sizeof(ion::bb::image_io::rawHeader); } + if (frame_count->is_bounds_query()) { + frame_count->dim[0].min = 0; + frame_count->dim[0].extent = num_output; + } + return 0; } else { - ion::bb::image_io::rawHeader header_info0; - ::memcpy(&header_info0, in1->host, sizeof(ion::bb::image_io::rawHeader)); + if (dispose) { + w.dispose(); + w.release_instance(output_directory); + return 0; + } + + ion::bb::image_io::rawHeader header_info0; + ::memcpy(&header_info0, deviceinfo->host, sizeof(ion::bb::image_io::rawHeader)); std::vector header_infos{header_info0}; - std::vector obufs{in0->host}; - std::vector size_in_bytes{in0->size_in_bytes()}; - w.post_gendc(obufs, size_in_bytes, header_infos); + std::vector obufs{image->host}; + std::vector size_in_bytes{image->size_in_bytes()}; + w.post_images(obufs, size_in_bytes, header_infos, frame_count->host); + } + + return 0; + } + catch (const ::std::exception& e) { + ::std::cerr << e.what() << ::std::endl; + return -1; + } + catch (...) { + ::std::cerr << "Unknown error" << ::std::endl; + return -1; + } +} + +ION_REGISTER_EXTERN(ion_bb_image_io_binary_1image_saver); +extern "C" ION_EXPORT +int ion_bb_image_io_binary_2image_saver( + halide_buffer_t * image0, halide_buffer_t * image1, + halide_buffer_t * deviceinfo0, halide_buffer_t * deviceinfo1, halide_buffer_t * frame_count, + bool dispose, int32_t width, int32_t height, int32_t dim, int byte_depth, halide_buffer_t* output_directory_buf, + halide_buffer_t * out) + { + try { + int num_output = 2; + int32_t frame_size = dim == 2 ? width * height * byte_depth : width * height * 3 * byte_depth; + std::vectorframe_size_list{frame_size, frame_size}; + const ::std::string output_directory(reinterpret_cast(output_directory_buf->host)); + auto& w(Writer::get_instance(frame_size_list, output_directory, true)); + + if (image0->is_bounds_query() || deviceinfo0->is_bounds_query() || + image1->is_bounds_query() || deviceinfo1->is_bounds_query() || frame_count->is_bounds_query()) { + if (image0->is_bounds_query()) { + image0->dim[0].min = 0; + image0->dim[0].extent = width; + image0->dim[1].min = 0; + image0->dim[1].extent = height; + if (dim == 3){ + image0->dim[2].min = 0; + image0->dim[2].extent = 3; + } + } + if (deviceinfo0->is_bounds_query()) { + deviceinfo0->dim[0].min = 0; + deviceinfo0->dim[0].extent = sizeof(ion::bb::image_io::rawHeader); + } + if (image1->is_bounds_query()) { + image1->dim[0].min = 0; + image1->dim[0].extent = width; + image1->dim[1].min = 0; + image1->dim[1].extent = height; + if (dim == 3){ + image1->dim[2].min = 0; + image1->dim[2].extent = 3; + } + } + if (deviceinfo1->is_bounds_query()) { + deviceinfo1->dim[0].min = 0; + deviceinfo1->dim[0].extent = sizeof(ion::bb::image_io::rawHeader); + } + if (frame_count->is_bounds_query()) { + frame_count->dim[0].min = 0; + frame_count->dim[0].extent = num_output; + } + return 0; + } + else { if (dispose) { w.dispose(); w.release_instance(output_directory); return 0; } + ion::bb::image_io::rawHeader header_info0, header_info1; + ::memcpy(&header_info0, deviceinfo0->host, sizeof(ion::bb::image_io::rawHeader)); + ::memcpy(&header_info1, deviceinfo1->host, sizeof(ion::bb::image_io::rawHeader)); + std::vector header_infos{header_info0, header_info1}; + + std::vector obufs{image0->host, image1->host}; + std::vector size_in_bytes{image0->size_in_bytes(), image1->size_in_bytes()}; + w.post_images(obufs, size_in_bytes, header_infos, frame_count->host); } return 0; @@ -577,7 +609,7 @@ int ion_bb_image_io_binary_1gendc_saver(halide_buffer_t * in0, halide_buffer_t * } } -ION_REGISTER_EXTERN(ion_bb_image_io_binary_1gendc_saver); +ION_REGISTER_EXTERN(ion_bb_image_io_binary_2image_saver); namespace { diff --git a/src/bb/image-io/rt_u3v.h b/src/bb/image-io/rt_u3v.h index 9647e77e..2eb2f62d 100644 --- a/src/bb/image-io/rt_u3v.h +++ b/src/bb/image-io/rt_u3v.h @@ -384,7 +384,7 @@ class U3V { } } - for (int i = 0; i < num_sensor_; ++i){ + for (int i = 0; i < num_sensor_; ++i){ ::memcpy(outs[i], arv_buffer_get_part_data(bufs[i], 0, nullptr), devices_[i].image_payload_size_); arv_stream_push_buffer(devices_[i].stream_, bufs[i]); log::trace("Obtained Frame from USB{}: {}", i, devices_[i].frame_count_); @@ -566,8 +566,8 @@ class U3V { } for (int i = 0; i < num_sensor_; ++i){ - ::memcpy(outs[i*num_sensor_], arv_buffer_get_data(bufs[i], nullptr), devices_[i].u3v_payload_size_); - ::memcpy(outs[i*num_sensor_+1], &(devices_[i].header_info_), sizeof(ion::bb::image_io::rawHeader)); + ::memcpy(outs[i], arv_buffer_get_data(bufs[i], nullptr), devices_[i].u3v_payload_size_); + // ::memcpy(outs[i*num_sensor_+1], &(devices_[i].header_info_), sizeof(ion::bb::image_io::rawHeader)); arv_stream_push_buffer(devices_[i].stream_, bufs[i]); log::trace("Obtained Frame from USB{}: {}", i, devices_[i].frame_count_); } @@ -644,12 +644,24 @@ class U3V { frame_cnt_ = latest_cnt; ::memcpy(outs[0], arv_buffer_get_data(bufs[cameN_idx_], nullptr), devices_[cameN_idx_].u3v_payload_size_); - ::memcpy(outs[1], &(devices_[cameN_idx_].header_info_), sizeof(ion::bb::image_io::rawHeader)); + // ::memcpy(outs[1], &(devices_[cameN_idx_].header_info_), sizeof(ion::bb::image_io::rawHeader)); arv_stream_push_buffer(devices_[cameN_idx_].stream_, bufs[cameN_idx_]); log::trace("Obtained Frame from USB{}: {}", cameN_idx_, frame_cnt_); } } + void get_device_info(std::vector& outs){ + if (operation_mode_ == OperationMode::Came2USB2 || operation_mode_ == OperationMode::Came1USB1){ + for (int i = 0; i < num_sensor_; ++i){ + ::memcpy(outs[i], &(devices_[i].header_info_), sizeof(ion::bb::image_io::rawHeader)); + log::trace("Obtained Device info USB{}", i); + } + } else if (operation_mode_ == OperationMode::Came1USB2) { + ::memcpy(outs[0], &(devices_[cameN_idx_].header_info_), sizeof(ion::bb::image_io::rawHeader)); + log::trace("Obtained Device info (OperationMode::Came1USB2)"); + } + } + private: U3V(std::string pixel_format, int32_t num_sensor, bool frame_sync, bool realtime_display_mode, char* dev_id = nullptr) : gobject_(GOBJECT_FILE, true), aravis_(ARAVIS_FILE, true), @@ -659,7 +671,7 @@ class U3V { { init_symbols(); - log::debug("ion-kit with framecount-log for 23-11-14 Update framecount Came1USB2"); + log::debug("U3V:: 23-11-18 : updating obtain and write"); log::info("Using aravis-{}.{}.{}", arv_get_major_version(), arv_get_minor_version(), arv_get_micro_version()); arv_update_device_list(); @@ -812,6 +824,7 @@ class U3V { log::info("The pixel format is not supported for header info"); } + devices_[i].header_info_ = { 1, wi, hi, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, wi, hi, wi, hi, static_cast(fps), px @@ -1096,7 +1109,8 @@ int u3v_camera_frame_count( auto &u3v(ion::bb::image_io::U3V::get_instance(pixel_format, num_sensor, frame_sync, realtime_display_mode)); if (out->is_bounds_query()) { out->dim[0].min = 0; - out->dim[0].extent = 1; + out->dim[0].extent = num_sensor; + return 0; } else { u3v.get_frame_count(reinterpret_cast(out->host)); @@ -1219,7 +1233,7 @@ int ION_EXPORT ion_bb_image_io_u3v_gendc_camera1( bool frame_sync, bool realtime_display_mode, halide_buffer_t* gain, halide_buffer_t* exposure, halide_buffer_t* pixel_format_buf, halide_buffer_t * gain_key_buf, halide_buffer_t * exposure_key_buf, - halide_buffer_t * out_gendc, halide_buffer_t * out_deviceinfo + halide_buffer_t * out_gendc ) { using namespace Halide; @@ -1229,7 +1243,7 @@ int ION_EXPORT ion_bb_image_io_u3v_gendc_camera1( const ::std::string exposure_key(reinterpret_cast(exposure_key_buf->host)); const ::std::string pixel_format(reinterpret_cast(pixel_format_buf->host)); auto &u3v(ion::bb::image_io::U3V::get_instance(pixel_format, num_output, false, realtime_display_mode)); - if (out_gendc->is_bounds_query() || out_deviceinfo->is_bounds_query() || gain->is_bounds_query() || exposure->is_bounds_query()) { + if (out_gendc->is_bounds_query() || gain->is_bounds_query() || exposure->is_bounds_query()) { gain->dim[0].min = 0; gain->dim[0].extent = num_output; exposure->dim[0].min = 0; @@ -1241,8 +1255,7 @@ int ION_EXPORT ion_bb_image_io_u3v_gendc_camera1( u3v.SetGain(i, gain_key, (reinterpret_cast(gain->host))[i]); u3v.SetExposure(i, exposure_key, (reinterpret_cast(exposure->host))[i]); } - - std::vector obufs{out_gendc->host, out_deviceinfo->host}; + std::vector obufs{out_gendc->host}; u3v.get_gendc(obufs); if(dispose){ @@ -1267,8 +1280,7 @@ int ION_EXPORT ion_bb_image_io_u3v_gendc_camera2( bool frame_sync, bool realtime_display_mode, halide_buffer_t* gain, halide_buffer_t* exposure, halide_buffer_t* pixel_format_buf, halide_buffer_t * gain_key_buf, halide_buffer_t * exposure_key_buf, - halide_buffer_t * gendc0, halide_buffer_t * gendc1, - halide_buffer_t * deviceinfo0, halide_buffer_t * deviceinfo1 + halide_buffer_t * gendc0, halide_buffer_t * gendc1 ) { using namespace Halide; @@ -1291,14 +1303,13 @@ int ION_EXPORT ion_bb_image_io_u3v_gendc_camera2( u3v.SetExposure(i, exposure_key, (reinterpret_cast(exposure->host))[i]); } - std::vector obufs{gendc0->host, gendc1->host,deviceinfo0->host, deviceinfo1->host}; + std::vector obufs{gendc0->host, gendc1->host}; u3v.get_gendc(obufs); if(dispose){ u3v.dispose(); } } - return 0; } catch (const std::exception &e) { ion::log::error("Exception was thrown: {}", e.what()); @@ -1310,6 +1321,87 @@ int ION_EXPORT ion_bb_image_io_u3v_gendc_camera2( } ION_REGISTER_EXTERN(ion_bb_image_io_u3v_gendc_camera2); +extern "C" +int ION_EXPORT ion_bb_image_io_u3v_device_info1( + halide_buffer_t *, + bool dispose, int32_t num_sensor, + bool frame_sync, bool realtime_display_mode, + halide_buffer_t* pixel_format_buf, + halide_buffer_t * out_deviceinfo + ) +{ + using namespace Halide; + int num_output = 1; + try { + const ::std::string pixel_format(reinterpret_cast(pixel_format_buf->host)); + auto &u3v(ion::bb::image_io::U3V::get_instance(pixel_format, num_sensor, false, realtime_display_mode)); + + if (out_deviceinfo->is_bounds_query()){ + out_deviceinfo->dim[0].min = 0; + out_deviceinfo->dim[0].extent = sizeof(ion::bb::image_io::rawHeader); + return 0; + }else{ + std::vector obufs{out_deviceinfo->host}; + u3v.get_device_info(obufs); + + if(dispose){ + u3v.dispose(); + } + } + return 0; + } catch (const std::exception &e) { + ion::log::error("Exception was thrown: {}", e.what()); + return 1; + } catch (...) { + ion::log::error("Unknown exception was thrown"); + return 1; + } +} +ION_REGISTER_EXTERN(ion_bb_image_io_u3v_device_info1); + +extern "C" +int ION_EXPORT ion_bb_image_io_u3v_device_info2( + halide_buffer_t *, halide_buffer_t *, + bool dispose, int32_t num_sensor, + bool frame_sync, bool realtime_display_mode, + halide_buffer_t* pixel_format_buf, + halide_buffer_t * deviceinfo0, halide_buffer_t * deviceinfo1 + ) +{ + using namespace Halide; + try { + int num_output = 2; + const ::std::string pixel_format(reinterpret_cast(pixel_format_buf->host)); + auto &u3v(ion::bb::image_io::U3V::get_instance(pixel_format, num_sensor, frame_sync, realtime_display_mode)); + if (deviceinfo0->is_bounds_query() || deviceinfo1->is_bounds_query()) { + if (deviceinfo0->is_bounds_query()){ + deviceinfo0->dim[0].min = 0; + deviceinfo0->dim[0].extent = sizeof(ion::bb::image_io::rawHeader); + } + if (deviceinfo1->is_bounds_query()){ + deviceinfo1->dim[0].min = 0; + deviceinfo1->dim[0].extent = sizeof(ion::bb::image_io::rawHeader); + } + return 0; + }else{ + std::vector obufs{deviceinfo0->host, deviceinfo1->host}; + u3v.get_device_info(obufs); + + if(dispose){ + u3v.dispose(); + } + } + return 0; + } catch (const std::exception &e) { + ion::log::error("Exception was thrown: {}", e.what()); + return 1; + } catch (...) { + ion::log::error("Unknown exception was thrown"); + return 1; + } +} +ION_REGISTER_EXTERN(ion_bb_image_io_u3v_device_info2); + extern "C" int ION_EXPORT ion_bb_image_io_u3v_multiple_camera1( bool dispose, @@ -1381,7 +1473,6 @@ int ION_EXPORT ion_bb_image_io_u3v_multiple_camera2( u3v.SetGain(i, gain_key, (reinterpret_cast(gain->host))[i]); u3v.SetExposure(i, exposure_key, (reinterpret_cast(exposure->host))[i]); } - std::vector obufs{out0->host, out1->host}; u3v.get(obufs); if(dispose){ @@ -1422,4 +1513,4 @@ int ION_EXPORT ion_bb_image_io_u3v_multiple_camera_frame_count2( ION_REGISTER_EXTERN(ion_bb_image_io_u3v_multiple_camera_frame_count2); -#endif +#endif \ No newline at end of file