From 6dd6c823f11b6612c8dc05f4aa306d15b48d911d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Hern=C3=A1ndez=20Cordero?= Date: Mon, 22 Jan 2024 16:05:37 +0100 Subject: [PATCH] Removed Boost dependency (#909) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removed Boost dependency. Related with https://github.com/ros-perception/image_pipeline/issues/407 --------- Signed-off-by: Alejandro Hernández Cordero --- image_view/CMakeLists.txt | 2 - .../image_view/extract_images_node.hpp | 4 +- .../include/image_view/image_saver_node.hpp | 4 +- .../include/image_view/image_view_node.hpp | 4 +- .../include/image_view/stereo_view_node.hpp | 5 +-- image_view/src/extract_images_node.cpp | 8 ++-- image_view/src/image_saver_node.cpp | 25 ++--------- image_view/src/image_view_node.cpp | 8 ++-- image_view/src/stereo_view_node.cpp | 8 ++-- image_view/src/utils.hpp | 41 +++++++++++++++++++ 10 files changed, 61 insertions(+), 48 deletions(-) create mode 100644 image_view/src/utils.hpp diff --git a/image_view/CMakeLists.txt b/image_view/CMakeLists.txt index 27231ef73..da98a0979 100644 --- a/image_view/CMakeLists.txt +++ b/image_view/CMakeLists.txt @@ -12,7 +12,6 @@ endif() find_package(ament_cmake_auto REQUIRED) ament_auto_find_build_dependencies() -find_package(Boost REQUIRED) find_package(OpenCV REQUIRED) # Deal with the GUI's @@ -30,7 +29,6 @@ ament_auto_add_library(${PROJECT_NAME}_nodes SHARED ) target_link_libraries(${PROJECT_NAME}_nodes ${OpenCV_LIBRARIES} - ${Boost_LIBRARIES} ) target_compile_definitions(${PROJECT_NAME}_nodes PRIVATE "COMPOSITION_BUILDING_DLL" diff --git a/image_view/include/image_view/extract_images_node.hpp b/image_view/include/image_view/extract_images_node.hpp index 59c131936..37d07ffbe 100644 --- a/image_view/include/image_view/extract_images_node.hpp +++ b/image_view/include/image_view/extract_images_node.hpp @@ -56,8 +56,6 @@ #include #include -#include - namespace image_view { @@ -74,7 +72,7 @@ class ExtractImagesNode std::mutex image_mutex_; std::string window_name_; - boost::format filename_format_; + std::string filename_format_; int count_; rclcpp::Time _time; double sec_per_frame_; diff --git a/image_view/include/image_view/image_saver_node.hpp b/image_view/include/image_view/image_saver_node.hpp index af3b0434c..5d8c76b2b 100644 --- a/image_view/include/image_view/image_saver_node.hpp +++ b/image_view/include/image_view/image_saver_node.hpp @@ -52,8 +52,6 @@ #include #include -#include - #include #include #include @@ -69,7 +67,7 @@ class ImageSaverNode explicit ImageSaverNode(const rclcpp::NodeOptions & options); private: - boost::format g_format; + std::string g_format; bool stamped_filename_; bool save_all_image_{false}; bool save_image_service_{false}; diff --git a/image_view/include/image_view/image_view_node.hpp b/image_view/include/image_view/image_view_node.hpp index 8162607d5..a98290840 100644 --- a/image_view/include/image_view/image_view_node.hpp +++ b/image_view/include/image_view/image_view_node.hpp @@ -28,8 +28,6 @@ #include #include -#include - namespace image_view { @@ -61,7 +59,7 @@ class ImageViewNode bool autosize_; int window_height_, window_width_; bool g_gui; - boost::format filename_format_; + std::string filename_format_; image_transport::Subscriber sub_; int count_; double min_image_value_, max_image_value_; diff --git a/image_view/include/image_view/stereo_view_node.hpp b/image_view/include/image_view/stereo_view_node.hpp index f5bd4c5ba..f2c40c625 100644 --- a/image_view/include/image_view/stereo_view_node.hpp +++ b/image_view/include/image_view/stereo_view_node.hpp @@ -51,6 +51,7 @@ #include #include +#include #include "message_filters/subscriber.h" #include "message_filters/sync_policies/approximate_time.h" @@ -63,8 +64,6 @@ #include #include -#include - namespace image_view { @@ -101,7 +100,7 @@ class StereoViewNode cv::Mat_ disparity_color_; std::mutex image_mutex_; - boost::format filename_format_; + std::string filename_format_; int save_count_; rclcpp::TimerBase::SharedPtr check_synced_timer_; diff --git a/image_view/src/extract_images_node.cpp b/image_view/src/extract_images_node.cpp index db572f8fb..271ced3a9 100644 --- a/image_view/src/extract_images_node.cpp +++ b/image_view/src/extract_images_node.cpp @@ -59,9 +59,8 @@ #include #include -#include - #include "image_view/extract_images_node.hpp" +#include "utils.hpp" namespace image_view { @@ -95,8 +94,7 @@ ExtractImagesNode::ExtractImagesNode(const rclcpp::NodeOptions & options) } this->declare_parameter("filename_format", std::string("frame%04i.jpg")); - std::string format_string = this->get_parameter("filename_format").as_string(); - filename_format_.parse(format_string); + filename_format_ = this->get_parameter("filename_format").as_string(); this->declare_parameter("sec_per_frame", 0.1); sec_per_frame_ = this->get_parameter("sec_per_frame").as_double(); @@ -130,7 +128,7 @@ void ExtractImagesNode::image_cb(const sensor_msgs::msg::Image::ConstSharedPtr & _time = this->now(); if (!image.empty()) { - std::string filename = (filename_format_ % count_).str(); + std::string filename = string_format(filename_format_, count_); cv::imwrite(filename, image); diff --git a/image_view/src/image_saver_node.cpp b/image_view/src/image_saver_node.cpp index f473e7aa2..4982732d0 100644 --- a/image_view/src/image_saver_node.cpp +++ b/image_view/src/image_saver_node.cpp @@ -55,7 +55,6 @@ #include "image_view/image_saver_node.hpp" -#include #include #include @@ -65,6 +64,8 @@ #include #include +#include "utils.hpp" + namespace image_view { @@ -92,13 +93,11 @@ ImageSaverNode::ImageSaverNode(const rclcpp::NodeOptions & options) &ImageSaverNode::callbackWithoutCameraInfo, this, std::placeholders::_1), hints.getTransport()); - std::string format_string; - format_string = this->declare_parameter("filename_format", std::string("left%04i.%s")); + g_format = this->declare_parameter("filename_format", std::string("left%04i.%s")); encoding_ = this->declare_parameter("encoding", std::string("bgr8")); save_all_image_ = this->declare_parameter("save_all_image", true); stamped_filename_ = this->declare_parameter("stamped_filename", false); request_start_end_ = this->declare_parameter("request_start_end", false); - g_format.parse(format_string); save_srv_ = this->create_service( "save", @@ -131,23 +130,7 @@ bool ImageSaverNode::saveImage( } if (!image.empty()) { - try { - filename = (g_format).str(); - } catch (...) { - g_format.clear(); - } - - try { - filename = (g_format % count_).str(); - } catch (...) { - g_format.clear(); - } - - try { - filename = (g_format % count_ % "jpg").str(); - } catch (...) { - g_format.clear(); - } + filename = string_format(g_format, count_, "jpg"); if (save_all_image_ || save_image_service_) { if (stamped_filename_) { diff --git a/image_view/src/image_view_node.cpp b/image_view/src/image_view_node.cpp index 604745870..fe607d9d2 100644 --- a/image_view/src/image_view_node.cpp +++ b/image_view/src/image_view_node.cpp @@ -58,7 +58,6 @@ #include "image_view/image_view_node.hpp" -#include #include #include @@ -66,6 +65,8 @@ #include #include +#include "utils.hpp" + namespace image_view { @@ -135,9 +136,8 @@ ImageViewNode::ImageViewNode(const rclcpp::NodeOptions & options) autosize_ = this->declare_parameter("autosize", false); window_height_ = this->declare_parameter("height", -1); window_width_ = this->declare_parameter("width", -1); - std::string format_string = + filename_format_ = this->declare_parameter("filename_format", std::string("frame%04i.jpg")); - filename_format_.parse(format_string); rcl_interfaces::msg::ParameterDescriptor colormap_paramDescriptor; colormap_paramDescriptor.name = "colormap"; @@ -253,7 +253,7 @@ void ImageViewNode::mouseCb(int event, int /* x */, int /* y */, int /* flags */ return; } - std::string filename = (this_->filename_format_ % this_->count_).str(); + std::string filename = string_format(this_->filename_format_, this_->count_); if (cv::imwrite(filename, image->image)) { RCLCPP_INFO(this_->get_logger(), "Saved image %s", filename.c_str()); diff --git a/image_view/src/stereo_view_node.cpp b/image_view/src/stereo_view_node.cpp index 72b7bb56e..5b876d859 100644 --- a/image_view/src/stereo_view_node.cpp +++ b/image_view/src/stereo_view_node.cpp @@ -60,7 +60,6 @@ #include "image_view/stereo_view_node.hpp" -#include #include #include @@ -70,6 +69,8 @@ #include #include +#include "utils.hpp" + namespace image_view { @@ -94,8 +95,7 @@ StereoViewNode::StereoViewNode(const rclcpp::NodeOptions & options) bool autosize = this->declare_parameter("autosize", true); this->declare_parameter("filename_format", std::string("frame%04i.jpg")); - std::string format_string = this->get_parameter("filename_format").as_string(); - filename_format_.parse(format_string); + filename_format_ = this->get_parameter("filename_format").as_string(); // TransportHints does not actually declare the parameter this->declare_parameter("image_transport", std::string("raw")); @@ -265,7 +265,7 @@ void StereoViewNode::imageCb( void StereoViewNode::saveImage(const char * prefix, const cv::Mat & image) { if (!image.empty()) { - std::string filename = (filename_format_ % prefix % save_count_).str(); + std::string filename = string_format(filename_format_, prefix, save_count_); cv::imwrite(filename, image); RCLCPP_INFO(this->get_logger(), "Saved image %s", filename.c_str()); } else { diff --git a/image_view/src/utils.hpp b/image_view/src/utils.hpp new file mode 100644 index 000000000..4b4ae700b --- /dev/null +++ b/image_view/src/utils.hpp @@ -0,0 +1,41 @@ +// Copyright 2024 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef UTILS_HPP_ +#define UTILS_HPP_ + +#include +#include +#include + +namespace image_view +{ +// TODO(ahcorde): Use std::format when C++20 is available +template +std::string string_format(const std::string & format, Args ... args) +{ + // Extra space for '\0' + int size_s = std::snprintf(nullptr, 0, format.c_str(), args ...) + 1; + if (size_s <= 0) { + throw std::runtime_error("Error during formatting."); + } + auto size = static_cast(size_s); + std::unique_ptr buf(new char[size]); + std::snprintf(buf.get(), size, format.c_str(), args ...); + // We don't want the '\0' inside + return std::string(buf.get(), buf.get() + size - 1); +} +} // namespace image_view + +#endif // UTILS_HPP_