Skip to content

Commit

Permalink
Merge pull request #158 from Sensing-Dev/feature/image-io-save-image
Browse files Browse the repository at this point in the history
Feature/image io save image
  • Loading branch information
iitaku authored Nov 18, 2023
2 parents 630cb85 + 9a57e7a commit 6336605
Show file tree
Hide file tree
Showing 3 changed files with 449 additions and 244 deletions.
242 changes: 162 additions & 80 deletions src/bb/image-io/bb.h
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ class U3VCameraN : public ion::BuildingBlock<U3VCameraN<T, D>> {
GeneratorInput<Halide::Func> exposure{ "exposure", Halide::type_of<double>(), 1};

GeneratorOutput<Halide::Func[]> output{ "output", Halide::type_of<T>(), D};
GeneratorOutput<Halide::Func[]> device_info{ "device_info", Halide::type_of<uint8_t>(), 1};
GeneratorOutput<Halide::Func> frame_count{ "frame_count", Halide::type_of<uint32_t>(), 1 };

void generate() {
Expand Down Expand Up @@ -748,6 +749,34 @@ class U3VCameraN : public ion::BuildingBlock<U3VCameraN<T, D>> {
}
}

Func u3v_device_info("u3v_device_info");
{
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());

std::vector<ExternFuncArgument> params{
cameraN, dispose, static_cast<int32_t>(num_devices), static_cast<bool>(frame_sync),
static_cast<bool>(realtime_diaplay_mode), pixel_format_buf
};

device_info.resize(num_devices);
std::vector<Halide::Type> output_type;
for (int i = 0; i < device_info.size(); i++) {
output_type.push_back(Halide::type_of<uint8_t>());
}
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);
Expand All @@ -756,7 +785,8 @@ class U3VCameraN : public ion::BuildingBlock<U3VCameraN<T, D>> {
std::memcpy(pixel_format_buf.data(), pixel_format.c_str(), pixel_format.size());

std::vector<ExternFuncArgument> params{
cameraN, dispose, static_cast<int32_t>(output.size()), static_cast<bool>(frame_sync), static_cast<bool>(realtime_diaplay_mode), pixel_format_buf
cameraN, dispose, static_cast<int32_t>(output.size()), static_cast<bool>(frame_sync),
static_cast<bool>(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<uint32_t>(), 1);
cameraN_fc.compute_root();
Expand Down Expand Up @@ -797,82 +827,98 @@ class U3VGenDC : public ion::BuildingBlock<U3VGenDC> {
exposure_func(_) = exposure(_);
exposure_func.compute_root();

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());
Func u3v_gendc("u3v_gendc");
{
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 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());
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{
dispose, static_cast<bool>(frame_sync), static_cast<bool>(realtime_diaplay_mode),
gain_func, exposure_func, pixel_format_buf,
gain_key_buf, exposure_key_buf
};
std::vector<ExternFuncArgument> params{
dispose, static_cast<bool>(frame_sync), static_cast<bool>(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<Halide::Type> output_type;
for (int i = 0; i < gendc.size() * 2; i++) {
output_type.push_back(Halide::type_of<uint8_t>());
gendc.resize(num_devices);
std::vector<Halide::Type> output_type;
for (int i = 0; i < gendc.size(); i++) {
output_type.push_back(Halide::type_of<uint8_t>());
}
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<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());

std::vector<ExternFuncArgument> params{
u3v_gendc, dispose, static_cast<int32_t>(num_devices), static_cast<bool>(frame_sync),
static_cast<bool>(realtime_diaplay_mode), pixel_format_buf
};

device_info.resize(num_devices);
std::vector<Halide::Type> output_type;
for (int i = 0; i < device_info.size(); i++) {
output_type.push_back(Halide::type_of<uint8_t>());
}
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<BinarySaver> {
template<typename T, int D>
class BinarySaver : public ion::BuildingBlock<BinarySaver<T, D>> {
public:
GeneratorParam<std::string> output_directory_ptr{ "output_directory", "." };
GeneratorParam<float> fps{ "fps", 1.0 };
GeneratorParam<float> r_gain0{ "r_gain0", 1.0 };
GeneratorParam<float> g_gain0{ "g_gain0", 1.0 };
GeneratorParam<float> b_gain0{ "b_gain0", 1.0 };

GeneratorParam<float> r_gain1{ "r_gain1", 1.0 };
GeneratorParam<float> g_gain1{ "g_gain1", 1.0 };
GeneratorParam<float> b_gain1{ "b_gain1", 1.0 };

GeneratorParam<int32_t> offset0_x{ "offset0_x", 0 };
GeneratorParam<int32_t> offset0_y{ "offset0_y", 0 };
GeneratorParam<int32_t> offset1_x{ "offset1_x", 0 };
GeneratorParam<int32_t> offset1_y{ "offset1_y", 0 };

GeneratorParam<int32_t> outputsize0_x{ "outputsize0_x", 1 };
GeneratorParam<int32_t> outputsize0_y{ "outputsize0_y", 1 };
GeneratorParam<int32_t> outputsize1_x{ "outputsize1_x", 1 };
GeneratorParam<int32_t> outputsize1_y{ "outputsize1_y", 1 };

Input<Halide::Func> input0{ "input0", UInt(16), 2 };
Input<Halide::Func> input1{ "input1", UInt(16), 2 };
Input<Halide::Func> frame_count{ "frame_count", UInt(32), 1 };
Input<bool> dispose{ "dispose" };
Input<int32_t> width{ "width", 0 };
Input<int32_t> height{ "height", 0 };
GeneratorParam<int32_t> num_devices{"num_devices", 2};

GeneratorInput<Halide::Func[]> input_images{ "input_images", Halide::type_of<T>(), D };

GeneratorInput<Halide::Func[]> input_deviceinfo{ "input_deviceinfo", Halide::type_of<uint8_t>(), 1 };
GeneratorInput<Halide::Func> frame_count{ "frame_count", Halide::type_of<uint32_t>(), 1 };

GeneratorInput<bool> dispose{ "dispose" };
GeneratorInput<int32_t> width{ "width" };
GeneratorInput<int32_t> height{ "height" };
GeneratorInput<int32_t> color_channel{ "color_channel" };

GeneratorOutput<int32_t> output{"output"};

Output<int> 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<int32_t>(num_devices);

const std::string output_directory(output_directory_ptr);
Halide::Buffer<uint8_t> output_directory_buf(static_cast<int>(output_directory.size() + 1));
Expand All @@ -882,34 +928,66 @@ class BinarySaver : public ion::BuildingBlock<BinarySaver> {
Func fc;
fc(_) = frame_count(_);
fc.compute_root();
std::vector<ExternFuncArgument> params = { in0, in1, fc, dispose, width, height, output_directory_buf,
static_cast<float>(r_gain0), static_cast<float>(g_gain0), static_cast<float>(b_gain0),
static_cast<float>(r_gain1), static_cast<float>(g_gain1), static_cast<float>(b_gain1),
static_cast<int32_t>(offset0_x), static_cast<int32_t>(offset0_x),
static_cast<int32_t>(offset0_x), static_cast<int32_t>(offset1_y),
static_cast<int32_t>(outputsize0_x), static_cast<int32_t>(outputsize0_y),
static_cast<int32_t>(outputsize1_x), static_cast<int32_t>(outputsize1_y),
cast<float>(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<ExternFuncArgument> 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<ExternFuncArgument> 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<uint8_t, 3>;
using BinarySaver_U8x2 = BinarySaver<uint8_t, 2>;
using BinarySaver_U16x2 = BinarySaver<uint16_t, 2>;

class BinaryGenDCSaver : public ion::BuildingBlock<BinaryGenDCSaver> {
public:
GeneratorParam<std::string> output_directory_ptr{ "output_directory", "." };

GeneratorParam<int32_t> num_devices{"num_devices", 2};

Input<Halide::Func[]> input_gendc{ "input_gendc", Halide::type_of<uint8_t>(), 1 };
Input<Halide::Func[]> input_deviceinfo{ "input_deviceinfo", Halide::type_of<uint8_t>(), 1 };
GeneratorInput<Halide::Func[]> input_gendc{ "input_gendc", Halide::type_of<uint8_t>(), 1 };
GeneratorInput<Halide::Func[]> input_deviceinfo{ "input_deviceinfo", Halide::type_of<uint8_t>(), 1 };

Input<bool> dispose{ "dispose" };
Input<int32_t> payloadsize{ "payloadsize" };
GeneratorInput<bool> dispose{ "dispose" };
GeneratorInput<int32_t> payloadsize{ "payloadsize" };

Output<int> output{ "output" };
GeneratorOutput<int> output{ "output" };

void generate() {
using namespace Halide;
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 6336605

Please sign in to comment.