diff --git a/include/web_video_server/image_streamer.h b/include/web_video_server/image_streamer.h index b5caba2..83d6817 100644 --- a/include/web_video_server/image_streamer.h +++ b/include/web_video_server/image_streamer.h @@ -55,6 +55,7 @@ class ImageTransportImageStreamer : public ImageStreamer virtual void start(); protected: + virtual cv::Mat decodeImage(const sensor_msgs::ImageConstPtr& msg); virtual void sendImage(const cv::Mat &, const ros::Time &time) = 0; virtual void restreamFrame(double max_age); virtual void initialize(const cv::Mat &); diff --git a/include/web_video_server/png_streamers.h b/include/web_video_server/png_streamers.h index c8f4176..6b72b66 100644 --- a/include/web_video_server/png_streamers.h +++ b/include/web_video_server/png_streamers.h @@ -19,6 +19,7 @@ class PngStreamer : public ImageTransportImageStreamer ~PngStreamer(); protected: virtual void sendImage(const cv::Mat &, const ros::Time &time); + virtual cv::Mat decodeImage(const sensor_msgs::ImageConstPtr& msg); private: MultipartStream stream_; @@ -42,6 +43,7 @@ class PngSnapshotStreamer : public ImageTransportImageStreamer ~PngSnapshotStreamer(); protected: virtual void sendImage(const cv::Mat &, const ros::Time &time); + virtual cv::Mat decodeImage(const sensor_msgs::ImageConstPtr& msg); private: int quality_; diff --git a/src/image_streamer.cpp b/src/image_streamer.cpp index 2981557..4a54523 100644 --- a/src/image_streamer.cpp +++ b/src/image_streamer.cpp @@ -81,6 +81,29 @@ void ImageTransportImageStreamer::restreamFrame(double max_age) } } +cv::Mat ImageTransportImageStreamer::decodeImage(const sensor_msgs::ImageConstPtr& 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::ImageConstPtr &msg) { if (inactive_) @@ -89,25 +112,7 @@ void ImageTransportImageStreamer::imageCallback(const sensor_msgs::ImageConstPtr 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 d26f232..666c6ed 100644 --- a/src/png_streamers.cpp +++ b/src/png_streamers.cpp @@ -1,6 +1,8 @@ #include "web_video_server/png_streamers.h" #include "async_web_server_cpp/http_reply.hpp" +#include + namespace web_video_server { @@ -18,6 +20,21 @@ PngStreamer::~PngStreamer() boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage. } +cv::Mat PngStreamer::decodeImage(const sensor_msgs::ImageConstPtr& 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 ros::Time &time) { std::vector encode_params; @@ -64,6 +81,20 @@ PngSnapshotStreamer::~PngSnapshotStreamer() boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage. } +cv::Mat PngSnapshotStreamer::decodeImage(const sensor_msgs::ImageConstPtr& 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 ros::Time &time) { std::vector encode_params;