Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions genicam_driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ install(DIRECTORY config DESTINATION share/${PROJECT_NAME})
# Tests

if(BUILD_TESTING)
add_executable(conversion_contract tests/conversion_contract.cpp)
target_link_libraries(conversion_contract ${LIBRARY_NAME})
add_test(NAME conversion_contract COMMAND conversion_contract)

find_package(ament_cmake_copyright REQUIRED)
ament_copyright()
find_package(ament_cmake_cppcheck REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion genicam_driver/include/genicam_driver/conversion_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace genicam_driver
{

// Conversion functions from GenICam to ROS formats
using ConversionFunction = std::function<void(sensor_msgs::msg::Image::SharedPtr& in,
using ConversionFunction = std::function<bool(sensor_msgs::msg::Image::SharedPtr& in,
sensor_msgs::msg::Image::SharedPtr& out)>;

[[nodiscard]] bool renameImg(sensor_msgs::msg::Image::SharedPtr& in,
Expand Down
10 changes: 8 additions & 2 deletions genicam_driver/src/camera_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,12 @@ void CameraDriver::processStreamBuffer(const uint stream_id)
{
sensor_msgs::msg::Image::SharedPtr p_cvt_img_msg =
stream.p_buffer_pool->getRecyclableImg();
stream.cvt_pixel_format(p_img_msg, p_cvt_img_msg);
if (!stream.cvt_pixel_format(p_img_msg, p_cvt_img_msg))
{
RCLCPP_WARN_ONCE(logger_, "Image data does not match pixel format '%s'.",
p_img_msg->encoding.c_str());
continue;
}
p_img_msg = p_cvt_img_msg;
}

Expand Down Expand Up @@ -1898,7 +1903,8 @@ void CameraDriver::fillImageMsgMetadata(sensor_msgs::msg::Image::SharedPtr& p_im
p_img_msg->width = img_roi.width;
p_img_msg->height = img_roi.height;
p_img_msg->encoding = sensor.pixel_format;
p_img_msg->step = (img_roi.width * sensor.n_bits_pixel) / 8;
const auto row_bits = static_cast<uint64_t>(p_img_msg->width) * sensor.n_bits_pixel;
p_img_msg->step = static_cast<uint32_t>((row_bits + 7) / 8);
}

//==================================================================================================
Expand Down
154 changes: 70 additions & 84 deletions genicam_driver/src/conversion_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include "genicam_driver/conversion_utils.h"

#include <limits>

// ROS
#include <rclcpp/rclcpp.hpp>

Expand Down Expand Up @@ -64,14 +66,30 @@ void shift(uint16_t* data, const size_t length, const size_t digits)
sensor_msgs::msg::Image::SharedPtr& out,
const size_t n_digits, const std::string out_format)
{
if (!in)
if (!in || n_digits >= 16 || in->height == 0 ||
in->step > in->data.size() / in->height)
return false;

// make a shallow copy (in-place operation on input)
out = in;
const auto channels = sensor_msgs::image_encodings::numChannels(out_format);
const auto samples_per_row = static_cast<uint64_t>(in->width) * channels;
if (samples_per_row * 2 > in->step)
return false;

// shift
shift(reinterpret_cast<uint16_t*>(out->data.data()), out->data.size() / 2, n_digits);
out = in;
const size_t low = out->is_bigendian ? 1 : 0;
const size_t high = 1 - low;
for (uint32_t y = 0; y < out->height; ++y)
{
auto* row = out->data.data() + static_cast<size_t>(y) * out->step;
for (size_t x = 0; x < samples_per_row; ++x)
{
auto* sample = row + 2 * x;
const uint32_t value = sample[low] | (static_cast<uint32_t>(sample[high]) << 8);
const uint32_t shifted = value << n_digits;
sample[low] = static_cast<uint8_t>(shifted);
sample[high] = static_cast<uint8_t>(shifted >> 8);
}
}
out->encoding = out_format;

return true;
Expand Down Expand Up @@ -225,56 +243,59 @@ void shift(uint16_t* data, const size_t length, const size_t digits)
}

//==================================================================================================
[[nodiscard]] bool unpack10pMonoImg(sensor_msgs::msg::Image::SharedPtr& in,
sensor_msgs::msg::Image::SharedPtr& out,
const std::string out_format)
namespace
{
if (!in)
bool unpackLsbRows(sensor_msgs::msg::Image::SharedPtr& in, sensor_msgs::msg::Image::SharedPtr& out,
const unsigned bits, const unsigned channels, const std::string& out_format)
{
if (!in || in->width == 0 || in->height == 0 || in->step > in->data.size() / in->height)
return false;

if (!out)
out.reset(new sensor_msgs::msg::Image);
const auto samples_per_row = static_cast<uint64_t>(in->width) * channels;
const auto output_step = samples_per_row * 2;
if ((samples_per_row * bits + 7) / 8 > in->step ||
output_step > std::numeric_limits<uint32_t>::max() ||
output_step > std::numeric_limits<size_t>::max() / in->height)
return false;

out->header = in->header;
out->height = in->height;
out->width = in->width;
out->is_bigendian = in->is_bigendian;
out->step = (8 * in->step) / 5;
out->data.resize((8 * in->data.size()) / 5);
// Keep reading the original bytes if the caller requests in-place conversion.
const auto input = in;
if (!out || out == input)
out = std::make_shared<sensor_msgs::msg::Image>();

// change pixel bit alignment from every 4*10 = 40 Bit = 5 Byte format LSB
// byte 4 | byte 3 | byte 2 | byte 1 | byte 0
// DDDDDDDD DDCCCCCC CCCCBBBB BBBBBBAA AAAAAAAA
// into 4*16 = 64 Bit = 8 Byte format
// bytes 7+6 | bytes 5+4 | bytes 3+2 | bytes 1+0
// DDDDDDDD DD000000 CCCCCCCC CC000000 BBBBBBBB BB000000 AAAAAAAA AA000000
out->header = input->header;
out->height = input->height;
out->width = input->width;
out->is_bigendian = false;
out->step = static_cast<uint32_t>(output_step);
out->encoding = out_format;
out->data.resize(static_cast<size_t>(out->step) * out->height);

uint8_t* from = in->data.data();
uint16_t* to = reinterpret_cast<uint16_t*>(out->data.data());
// unpack 4 mono pixels per iteration
for (size_t i = 0; i < in->data.size() / 5; ++i)
const uint32_t mask = (1u << bits) - 1;
for (uint32_t y = 0; y < input->height; ++y)
{
std::memcpy(to, from, 2);
to[0] <<= 6;

std::memcpy(&to[1], &from[1], 2);
to[1] <<= 4;
to[1] &= 0b1111111111000000;

std::memcpy(&to[2], &from[2], 2);
to[2] <<= 2;
to[2] &= 0b1111111111000000;

std::memcpy(&to[3], &from[3], 2);
to[3] &= 0b1111111111000000;

to += 4;
from += 5;
const auto* source = input->data.data() + static_cast<size_t>(y) * input->step;
auto* target = out->data.data() + static_cast<size_t>(y) * out->step;
for (size_t x = 0; x < samples_per_row; ++x)
{
const uint64_t bit = static_cast<uint64_t>(x) * bits;
const auto* bytes = source + bit / 8;
const uint32_t word = bytes[0] | (static_cast<uint32_t>(bytes[1]) << 8);
const uint32_t sample = ((word >> (bit % 8)) & mask) << (16 - bits);
target[2 * x] = static_cast<uint8_t>(sample);
target[2 * x + 1] = static_cast<uint8_t>(sample >> 8);
}
}
out->encoding = out_format;

return true;
}
} // namespace

[[nodiscard]] bool unpack10pMonoImg(sensor_msgs::msg::Image::SharedPtr& in,
sensor_msgs::msg::Image::SharedPtr& out,
const std::string out_format)
{
return unpackLsbRows(in, out, 10, 1, out_format);
}

//==================================================================================================
[[nodiscard]] bool unpack10PackedMonoImg(sensor_msgs::msg::Image::SharedPtr& in,
Expand Down Expand Up @@ -305,13 +326,13 @@ void shift(uint16_t* data, const size_t length, const size_t digits)

uint8_t* from = in->data.data();
uint8_t* to = out->data.data();
// unpack 4 mono pixels per iteration
// unpack 2 mono pixels per iteration
for (size_t i = 0; i < in->data.size() / 3; ++i)
{
to[0] = from[1] << 6;
to[1] = from[0];

to[2] = from[1] & 0b11000000;
to[2] = (from[1] & 0b00110000) << 2;
to[3] = from[2];

to += 4;
Expand All @@ -327,43 +348,8 @@ void shift(uint16_t* data, const size_t length, const size_t digits)
sensor_msgs::msg::Image::SharedPtr& out,
const std::string out_format)
{
if (!in)
return false;

if (!out)
out.reset(new sensor_msgs::msg::Image);

out->header = in->header;
out->height = in->height;
out->width = in->width;
out->is_bigendian = in->is_bigendian;
out->step = (4 * in->step) / 3;
out->data.resize((4 * in->data.size()) / 3);

// change pixel bit alignment from every 2*12 = 24 Bit = 3 Byte format LSB
// byte 2 | byte 1 | byte 0
// BBBBBBBB BBBBAAAA AAAAAAAA
// into 2*16 = 32 Bit = 4 Byte format
// bytes 3+2 | bytes 1+0
// BBBBBBBB BBBB0000 AAAAAAAA AAAA0000

uint8_t* from = in->data.data();
uint16_t* to = reinterpret_cast<uint16_t*>(out->data.data());
// unpack 2 values per iteration
for (size_t i = 0; i < in->data.size() / 3; ++i)
{
std::memcpy(to, from, 2);
to[0] <<= 4;

std::memcpy(&to[1], &from[1], 2);
to[1] &= 0b1111111111110000;

to += 2;
from += 3;
}
out->encoding = out_format;

return true;
const auto channels = sensor_msgs::image_encodings::numChannels(out_format);
return unpackLsbRows(in, out, 12, channels, out_format);
}

//==================================================================================================
Expand Down
93 changes: 93 additions & 0 deletions genicam_driver/tests/conversion_contract.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) 2026 Xia Chao
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include <cstdint>
#include <initializer_list>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include "genicam_driver/conversion_utils.h"

namespace
{
using Image = sensor_msgs::msg::Image;

bool check(const std::string& scenario, const std::string& encoding, uint32_t width,
uint32_t height, uint32_t input_step, bool big_endian,
std::initializer_list<uint8_t> bytes, uint32_t expected_step, bool expected_endian,
std::initializer_list<uint8_t> expected)
{
auto input = std::make_shared<Image>();
input->width = width;
input->height = height;
input->step = input_step;
input->is_bigendian = big_endian;
input->data = bytes;
Image::SharedPtr output;
const auto& convert = genicam_driver::CONVERSIONS_DICTIONARY.at(encoding);
const bool ok = convert(input, output);
const bool valid = ok && output->step == expected_step && output->width == width &&
output->height == height && output->is_bigendian == expected_endian &&
output->data == std::vector<uint8_t>(expected);
std::cout << scenario << (valid ? " PASS" : " FAIL");
if (ok)
{
std::cout << " step=" << output->step << " big_endian=" << +output->is_bigendian
<< " data=";
for (uint8_t byte : output->data)
std::cout << ' ' << std::hex << static_cast<unsigned>(byte);
std::cout << std::dec;
}
std::cout << '\n';
return valid;
}
} // namespace

int main()
{
bool valid = true;
valid &= check("big-endian Mono12", "Mono12", 1, 1, 2, true,
{0x00, 0xff}, 2, true, {0x0f, 0xf0});
valid &= check("RGB12 with row padding", "RGB12", 1, 1, 8, false,
{0x23, 0x01, 0x56, 0x04, 0x89, 0x07, 0xa1, 0xb2}, 8, false,
{0x30, 0x12, 0x60, 0x45, 0x90, 0x78, 0xa1, 0xb2});
valid &= check("Mono10p row tails", "Mono10p", 5, 2, 7, false,
{1, 8, 48, 0, 1, 5, 0, 6, 28, 128, 64, 2, 10, 0}, 10, false,
{64, 0, 128, 0, 192, 0, 0, 1, 64, 1, 128, 1, 192, 1, 0, 2, 64, 2, 128, 2});
valid &= check("Mono12p row tails", "Mono12p", 3, 2, 5, false,
{0x23, 0x61, 0x45, 0x89, 0x07, 0xbc, 0xfa, 0xde, 0x21, 0x03}, 6, false,
{0x30, 0x12, 0x60, 0x45, 0x90, 0x78, 0xc0, 0xab, 0xf0, 0xde, 0x10, 0x32});
valid &= check("RGB12p row tails", "RGB12p", 1, 2, 5, false,
{0x23, 0x61, 0x45, 0x89, 0x07, 0xbc, 0xfa, 0xde, 0x21, 0x03}, 6, false,
{0x30, 0x12, 0x60, 0x45, 0x90, 0x78, 0xc0, 0xab, 0xf0, 0xde, 0x10, 0x32});
valid &= check("Mono10Packed second sample", "Mono10Packed", 2, 1, 3, false,
{0x55, 0x31, 0xaa}, 4, false, {0x40, 0x55, 0xc0, 0xaa});
return valid ? 0 : 1;
}
Loading