From bfe00a16c4f7fce65f24887dc7a8bc41de09a819 Mon Sep 17 00:00:00 2001 From: Xia Chao Date: Tue, 24 Nov 2026 21:38:53 +0800 Subject: [PATCH 1/2] Preserve sample values and row boundaries during pixel conversion --- genicam_driver/CMakeLists.txt | 4 + .../include/genicam_driver/conversion_utils.h | 2 +- genicam_driver/src/camera_driver.cpp | 10 +- genicam_driver/src/conversion_utils.cpp | 154 ++++++++---------- genicam_driver/tests/conversion_contract.cpp | 68 ++++++++ 5 files changed, 151 insertions(+), 87 deletions(-) create mode 100644 genicam_driver/tests/conversion_contract.cpp diff --git a/genicam_driver/CMakeLists.txt b/genicam_driver/CMakeLists.txt index 2f5b159..a2100db 100644 --- a/genicam_driver/CMakeLists.txt +++ b/genicam_driver/CMakeLists.txt @@ -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) diff --git a/genicam_driver/include/genicam_driver/conversion_utils.h b/genicam_driver/include/genicam_driver/conversion_utils.h index 01ff169..8d8a795 100644 --- a/genicam_driver/include/genicam_driver/conversion_utils.h +++ b/genicam_driver/include/genicam_driver/conversion_utils.h @@ -42,7 +42,7 @@ namespace genicam_driver { // Conversion functions from GenICam to ROS formats -using ConversionFunction = std::function; [[nodiscard]] bool renameImg(sensor_msgs::msg::Image::SharedPtr& in, diff --git a/genicam_driver/src/camera_driver.cpp b/genicam_driver/src/camera_driver.cpp index 9f1ae29..15393dd 100644 --- a/genicam_driver/src/camera_driver.cpp +++ b/genicam_driver/src/camera_driver.cpp @@ -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; } @@ -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(p_img_msg->width) * sensor.n_bits_pixel; + p_img_msg->step = static_cast((row_bits + 7) / 8); } //================================================================================================== diff --git a/genicam_driver/src/conversion_utils.cpp b/genicam_driver/src/conversion_utils.cpp index f98e774..befed03 100644 --- a/genicam_driver/src/conversion_utils.cpp +++ b/genicam_driver/src/conversion_utils.cpp @@ -28,6 +28,8 @@ #include "genicam_driver/conversion_utils.h" +#include + // ROS #include @@ -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(in->width) * channels; + if (samples_per_row * 2 > in->step) + return false; - // shift - shift(reinterpret_cast(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(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(sample[high]) << 8); + const uint32_t shifted = value << n_digits; + sample[low] = static_cast(shifted); + sample[high] = static_cast(shifted >> 8); + } + } out->encoding = out_format; return true; @@ -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(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::max() || + output_step > std::numeric_limits::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(); - // 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(output_step); + out->encoding = out_format; + out->data.resize(static_cast(out->step) * out->height); - uint8_t* from = in->data.data(); - uint16_t* to = reinterpret_cast(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(y) * input->step; + auto* target = out->data.data() + static_cast(y) * out->step; + for (size_t x = 0; x < samples_per_row; ++x) + { + const uint64_t bit = static_cast(x) * bits; + const auto* bytes = source + bit / 8; + const uint32_t word = bytes[0] | (static_cast(bytes[1]) << 8); + const uint32_t sample = ((word >> (bit % 8)) & mask) << (16 - bits); + target[2 * x] = static_cast(sample); + target[2 * x + 1] = static_cast(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, @@ -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; @@ -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(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); } //================================================================================================== diff --git a/genicam_driver/tests/conversion_contract.cpp b/genicam_driver/tests/conversion_contract.cpp new file mode 100644 index 0000000..0497015 --- /dev/null +++ b/genicam_driver/tests/conversion_contract.cpp @@ -0,0 +1,68 @@ +// Copyright 2026 Xia Chao +// SPDX-License-Identifier: BSD-3-Clause + +#include +#include +#include +#include +#include +#include + +#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 bytes, uint32_t expected_step, bool expected_endian, + std::initializer_list expected) +{ + auto input = std::make_shared(); + 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(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(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; +} From 5e46973eed937bd75877ba0c0191137b6bae75d2 Mon Sep 17 00:00:00 2001 From: Xia Chao Date: Tue, 24 Nov 2026 21:44:21 +0800 Subject: [PATCH 2/2] Include the full BSD notice in conversion tests --- genicam_driver/tests/conversion_contract.cpp | 29 ++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/genicam_driver/tests/conversion_contract.cpp b/genicam_driver/tests/conversion_contract.cpp index 0497015..060ef15 100644 --- a/genicam_driver/tests/conversion_contract.cpp +++ b/genicam_driver/tests/conversion_contract.cpp @@ -1,5 +1,30 @@ -// Copyright 2026 Xia Chao -// SPDX-License-Identifier: BSD-3-Clause +// 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 #include