diff --git a/include/web_video_server/image_streamer.hpp b/include/web_video_server/image_streamer.hpp index 81c6029..c8a25c1 100644 --- a/include/web_video_server/image_streamer.hpp +++ b/include/web_video_server/image_streamer.hpp @@ -60,6 +60,7 @@ class ImageTransportImageStreamer : public ImageStreamer virtual void start(); protected: + virtual cv::Mat decodeImage(const sensor_msgs::msg::Image::ConstSharedPtr & msg); virtual void sendImage(const cv::Mat &, const rclcpp::Time & time) = 0; virtual void restreamFrame(double max_age); virtual void initialize(const cv::Mat &); diff --git a/include/web_video_server/png_streamers.hpp b/include/web_video_server/png_streamers.hpp index 20cd569..a47692a 100644 --- a/include/web_video_server/png_streamers.hpp +++ b/include/web_video_server/png_streamers.hpp @@ -21,6 +21,7 @@ class PngStreamer : public ImageTransportImageStreamer protected: virtual void sendImage(const cv::Mat &, const rclcpp::Time & time); + virtual cv::Mat decodeImage(const sensor_msgs::msg::Image::ConstSharedPtr & msg); private: MultipartStream stream_; @@ -47,6 +48,7 @@ class PngSnapshotStreamer : public ImageTransportImageStreamer protected: virtual void sendImage(const cv::Mat &, const rclcpp::Time & time); + virtual cv::Mat decodeImage(const sensor_msgs::msg::Image::ConstSharedPtr & msg); private: int quality_; diff --git a/src/image_streamer.cpp b/src/image_streamer.cpp index 1347759..12a0989 100644 --- a/src/image_streamer.cpp +++ b/src/image_streamer.cpp @@ -108,6 +108,26 @@ void ImageTransportImageStreamer::restreamFrame(double max_age) } } +cv::Mat ImageTransportImageStreamer::decodeImage( + const sensor_msgs::msg::Image::ConstSharedPtr & msg) +{ + if (msg->encoding.find("F") != std::string::npos) { + // scale floating point images + cv::Mat float_image_bridge = cv_bridge::toCvCopy(msg, msg->encoding)->image; + cv::Mat_ float_image = float_image_bridge; + double max_val; + cv::minMaxIdx(float_image, 0, &max_val); + + if (max_val > 0) { + float_image *= (255 / max_val); + } + return float_image; + } else { + // Convert to OpenCV native BGR color + return cv_bridge::toCvCopy(msg, "bgr8")->image; + } +} + void ImageTransportImageStreamer::imageCallback(const sensor_msgs::msg::Image::ConstSharedPtr & msg) { if (inactive_) { @@ -116,22 +136,7 @@ void ImageTransportImageStreamer::imageCallback(const sensor_msgs::msg::Image::C cv::Mat img; try { - if (msg->encoding.find("F") != std::string::npos) { - // scale floating point images - cv::Mat float_image_bridge = cv_bridge::toCvCopy(msg, msg->encoding)->image; - cv::Mat_ float_image = float_image_bridge; - double max_val; - cv::minMaxIdx(float_image, 0, &max_val); - - if (max_val > 0) { - float_image *= (255 / max_val); - } - img = float_image; - } else { - // Convert to OpenCV native BGR color - img = cv_bridge::toCvCopy(msg, "bgr8")->image; - } - + img = decodeImage(msg); int input_width = img.cols; int input_height = img.rows; diff --git a/src/png_streamers.cpp b/src/png_streamers.cpp index 9fb4e56..3995830 100644 --- a/src/png_streamers.cpp +++ b/src/png_streamers.cpp @@ -1,6 +1,12 @@ #include "web_video_server/png_streamers.hpp" #include "async_web_server_cpp/http_reply.hpp" +#ifdef CV_BRIDGE_USES_OLD_HEADERS +#include "cv_bridge/cv_bridge.h" +#else +#include "cv_bridge/cv_bridge.hpp" +#endif + namespace web_video_server { @@ -20,6 +26,17 @@ PngStreamer::~PngStreamer() boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage. } +cv::Mat PngStreamer::decodeImage(const sensor_msgs::msg::Image::ConstSharedPtr & msg) +{ + // Handle alpha values since PNG supports it + if (sensor_msgs::image_encodings::hasAlpha(msg->encoding)) { + return cv_bridge::toCvCopy(msg, "bgra8")->image; + } else { + // Use the normal decode otherwise + return ImageTransportImageStreamer::decodeImage(msg); + } +} + void PngStreamer::sendImage(const cv::Mat & img, const rclcpp::Time & time) { std::vector encode_params; @@ -64,6 +81,17 @@ PngSnapshotStreamer::~PngSnapshotStreamer() boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage. } +cv::Mat PngSnapshotStreamer::decodeImage(const sensor_msgs::msg::Image::ConstSharedPtr & msg) +{ + // Handle alpha values since PNG supports it + if (sensor_msgs::image_encodings::hasAlpha(msg->encoding)) { + return cv_bridge::toCvCopy(msg, "bgra8")->image; + } else { + // Use the normal decode otherwise + return ImageTransportImageStreamer::decodeImage(msg); + } +} + void PngSnapshotStreamer::sendImage(const cv::Mat & img, const rclcpp::Time & time) { std::vector encode_params;