Skip to content

Commit

Permalink
Updated U3V module to support various type and dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixstars-iizuka committed Dec 4, 2021
1 parent 763d3d6 commit 5f711ab
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 23 deletions.
87 changes: 72 additions & 15 deletions include/ion-bb-image-io/bb.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,68 @@ class ImageSaver : public ion::BuildingBlock<ImageSaver> {
}
};

class U3VCamera : public ion::BuildingBlock<U3VCamera> {
template<typename T, int D>
class U3VCamera1 : public ion::BuildingBlock<U3VCamera1<T, D>> {
public:

GeneratorParam<bool> frame_sync{"frame_sync", false};
GeneratorParam<std::string> pixel_format_ptr{"pixel_format_ptr", "RGB8"};
GeneratorParam<std::string> gain_key_ptr{"gain_key", "Gain"};
GeneratorParam<std::string> exposure_key_ptr{"exposure_key", "Exposure"};

GeneratorInput<int32_t> gain0{ "gain0" };
GeneratorInput<int32_t> exposure0{ "exposure0" };

GeneratorOutput<Halide::Func> output0{ "output0", Halide::type_of<T>(), D};
GeneratorOutput<Halide::Func> frame_count{ "frame_count", Halide::type_of<uint32_t>(), 1 };

void generate() {
using namespace Halide;

const std::string pixel_format(pixel_format_ptr);
Buffer<uint8_t> pixel_format_buf(static_cast<int>(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<uint8_t> gain_key_buf(static_cast<int>(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<uint8_t> exposure_key_buf(static_cast<int>(exposure_key.size() + 1));
exposure_key_buf.fill(0);
std::memcpy(exposure_key_buf.data(), exposure_key.c_str(), exposure_key.size());

std::vector<ExternFuncArgument> params{
static_cast<bool>(frame_sync),
gain0, exposure0, pixel_format_buf,
gain_key_buf, exposure_key_buf
};

Func camera("u3vcamera_func");
camera.define_extern("u3v_camera1", params, Halide::type_of<T>(), D);
camera.compute_root();
output0(_) = camera(_);

Buffer<uint8_t> pixel_format_buf_cpy(static_cast<int>(pixel_format.size() + 1));
pixel_format_buf_cpy.fill(0);
std::memcpy(pixel_format_buf_cpy.data(), pixel_format.c_str(), pixel_format.size());

Func camera_frame_count;
camera_frame_count.define_extern("camera_frame_count", { static_cast<bool>(frame_sync), pixel_format_buf_cpy}, type_of<uint32_t>(), 1);
camera_frame_count.compute_root();
frame_count(_) = camera_frame_count(_);
}
};

using U3VCamera1_U8x3 = U3VCamera1<uint8_t, 3>;
using U3VCamera1_U16x2 = U3VCamera1<uint16_t, 2>;

template<typename T, int D>
class U3VCamera2 : public ion::BuildingBlock<U3VCamera2<T, D>> {
public:

GeneratorParam<int32_t> num_sensor{"num_sensor", 1};
GeneratorParam<bool> frame_sync{"frame_sync", false};
GeneratorParam<std::string> pixel_format_ptr{"pixel_format_ptr", "RGB8"};
GeneratorParam<std::string> gain_key_ptr{"gain_key", "Gain"};
Expand All @@ -559,8 +617,8 @@ class U3VCamera : public ion::BuildingBlock<U3VCamera> {
GeneratorInput<int32_t> exposure0{ "exposure0" };
GeneratorInput<int32_t> exposure1{ "exposure1" };

GeneratorOutput<Halide::Func> output0{ "output0", Halide::type_of<uint16_t>(), 2 };
GeneratorOutput<Halide::Func> output1{ "output1", Halide::type_of<uint16_t>(), 2 };
GeneratorOutput<Halide::Func> output0{ "output0", Halide::type_of<T>(), D};
GeneratorOutput<Halide::Func> output1{ "output1", Halide::type_of<T>(), D};
GeneratorOutput<Halide::Func> frame_count{ "frame_count", Halide::type_of<uint32_t>(), 1 };

void generate() {
Expand All @@ -582,13 +640,13 @@ class U3VCamera : public ion::BuildingBlock<U3VCamera> {
std::memcpy(exposure_key_buf.data(), exposure_key.c_str(), exposure_key.size());

std::vector<ExternFuncArgument> params{
static_cast<int32_t>(num_sensor), static_cast<bool>(frame_sync),
static_cast<bool>(frame_sync),
gain0, gain1, exposure0, exposure1, pixel_format_buf,
gain_key_buf, exposure_key_buf
};

Func camera("u3vcamera_func");
camera.define_extern("u3v_camera", params, {Halide::type_of<uint16_t>(), Halide::type_of<uint16_t>()}, 2);
camera.define_extern("u3v_camera2", params, { Halide::type_of<T>(), Halide::type_of<T>() }, D);
camera.compute_root();
output0(_) = camera(_)[0];
output1(_) = camera(_)[1];
Expand All @@ -599,14 +657,14 @@ class U3VCamera : public ion::BuildingBlock<U3VCamera> {

Func camera_frame_count;
camera_frame_count.define_extern("camera_frame_count",
{ static_cast<int32_t>(num_sensor), static_cast<bool>(frame_sync), pixel_format_buf_cpy}, type_of<uint32_t>(), 1);
{ static_cast<bool>(frame_sync), pixel_format_buf_cpy}, type_of<uint32_t>(), 1);
camera_frame_count.compute_root();
frame_count(_) = camera_frame_count(_);

}

};

using U3VCamera2_U8x3 = U3VCamera2<uint8_t, 3>;
using U3VCamera2_U16x2 = U3VCamera2<uint16_t, 2>;

class BinarySaver : public ion::BuildingBlock<BinarySaver> {
public:
Expand Down Expand Up @@ -712,12 +770,10 @@ class BinaryLoader : public ion::BuildingBlock<BinaryLoader> {
}
};




} // namespace image_io
} // namespace bb
} // namespace ion

#ifndef _WIN32
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::IMX219, image_io_imx219);
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::D435, image_io_d435);
Expand All @@ -730,9 +786,10 @@ ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::FBDisplay, image_io_fb_display);
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::ColorDataLoader, image_io_color_data_loader);
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::GrayscaleDataLoader, image_io_grayscale_data_loader);
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::ImageSaver, image_io_image_saver);
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::U3VCamera, u3v_camera);

ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::U3VCamera1_U8x3, u3v_camera1_u8x3);
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::U3VCamera1_U16x2, u3v_camera1_u16x2);
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::U3VCamera2_U8x3, u3v_camera2_u8x3);
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::U3VCamera2_U16x2, u3v_camera2_u16x2);
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::BinarySaver, image_io_binarysaver);
ION_REGISTER_BUILDING_BLOCK(ion::bb::image_io::BinaryLoader, image_io_binaryloader);

#endif
2 changes: 1 addition & 1 deletion include/ion-bb-image-io/rt_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class ImageSequence {
std::ifstream ifs(path, std::ios::binary);
ifs.read(reinterpret_cast<char*>(frame.ptr()), size);
} else {
frame = cv::imread(path, imread_flags);
frame = cv::imread(path.string(), imread_flags);
if (frame.empty()) {
throw std::runtime_error("Failed to load data : " + path.string());
}
Expand Down
47 changes: 40 additions & 7 deletions include/ion-bb-image-io/rt_u3v.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ion {
namespace bb {
namespace image_io {

class U3V{
class U3V {

struct GError
{
Expand Down Expand Up @@ -479,9 +479,43 @@ class U3V{
} // namespace ion

extern "C"
int ION_EXPORT u3v_camera(
int32_t num_sensor, bool frame_sync,
int32_t gain0, int32_t gain1, int32_t exposure0, int32_t exposure1,
int ION_EXPORT u3v_camera1(
bool frame_sync, int32_t gain0, int32_t exposure0,
halide_buffer_t* pixel_format_buf, halide_buffer_t * gain_key_buf, halide_buffer_t * exposure_key_buf,
halide_buffer_t * out0)
{
using namespace Halide;
try {
const ::std::string gain_key(reinterpret_cast<const char*>(gain_key_buf->host));
const ::std::string exposure_key(reinterpret_cast<const char*>(exposure_key_buf->host));
const ::std::string pixel_format(reinterpret_cast<const char*>(pixel_format_buf->host));
auto &u3v(ion::bb::image_io::U3V::get_instance(pixel_format, 1, frame_sync));
if (out0->is_bounds_query()) {
//bounds query
return 0;
}else{
// set gain & exposure
u3v.SetGain(0, gain_key, gain0);
u3v.SetExposure(0, exposure_key, exposure0);

std::vector<void *> obufs{out0->host};
u3v.get(obufs);
}

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(u3v_camera1);

extern "C"
int ION_EXPORT u3v_camera2(
bool frame_sync, int32_t gain0, int32_t gain1, int32_t exposure0, int32_t exposure1,
halide_buffer_t* pixel_format_buf, halide_buffer_t * gain_key_buf, halide_buffer_t * exposure_key_buf,
halide_buffer_t * out0, halide_buffer_t * out1)
{
Expand All @@ -490,7 +524,7 @@ int ION_EXPORT u3v_camera(
const ::std::string gain_key(reinterpret_cast<const char*>(gain_key_buf->host));
const ::std::string exposure_key(reinterpret_cast<const char*>(exposure_key_buf->host));
const ::std::string pixel_format(reinterpret_cast<const char*>(pixel_format_buf->host));
auto &u3v(ion::bb::image_io::U3V::get_instance(pixel_format, num_sensor, frame_sync));
auto &u3v(ion::bb::image_io::U3V::get_instance(pixel_format, 2, frame_sync));
if (out0->is_bounds_query() || out1->is_bounds_query()) {
//bounds query
return 0;
Expand All @@ -501,7 +535,6 @@ int ION_EXPORT u3v_camera(
u3v.SetExposure(0, exposure_key, exposure0);
u3v.SetExposure(1, exposure_key, exposure1);

// tentatively say pixel format is RGB8
std::vector<void *> obufs{out0->host, out1->host};
u3v.get(obufs);
}
Expand All @@ -515,7 +548,7 @@ int ION_EXPORT u3v_camera(
return -1;
}
}
ION_REGISTER_EXTERN(u3v_camera);
ION_REGISTER_EXTERN(u3v_camera2);

extern "C"
int ION_EXPORT camera_frame_count(
Expand Down

0 comments on commit 5f711ab

Please sign in to comment.