compressed_subscriber.cpp
Go to the documentation of this file.
00001 #include "compressed_image_transport/compressed_subscriber.h"
00002 #include <sensor_msgs/image_encodings.h>
00003 #include <cv_bridge/cv_bridge.h>
00004 #include <opencv/cvwimage.h>
00005 #include <opencv/highgui.h>
00006 #include <opencv2/imgproc/imgproc.hpp>
00007 
00008 #include "compressed_image_transport/compression_common.h"
00009 
00010 #include <limits>
00011 #include <vector>
00012 
00013 using namespace cv;
00014 
00015 namespace enc = sensor_msgs::image_encodings;
00016 
00017 namespace compressed_image_transport
00018 {
00019 
00020 void CompressedSubscriber::internalCallback(const sensor_msgs::CompressedImageConstPtr& message,
00021                                             const Callback& user_cb)
00022 
00023 {
00024 
00025   cv_bridge::CvImagePtr cv_ptr(new cv_bridge::CvImage);
00026 
00027   // Copy message header
00028   cv_ptr->header = message->header;
00029 
00030   // Decode color/mono image
00031   try
00032   {
00033     cv_ptr->image = cv::imdecode(cv::Mat(message->data), CV_LOAD_IMAGE_UNCHANGED);
00034 
00035     // Assign image encoding string
00036     const size_t split_pos = message->format.find(';');
00037     if (split_pos==string::npos)
00038     {
00039       // Older version of compressed_image_transport does not signal image format
00040       switch (cv_ptr->image.channels())
00041       {
00042         case 1:
00043           cv_ptr->encoding = enc::MONO8;
00044           break;
00045         case 3:
00046           cv_ptr->encoding = enc::BGR8;
00047           break;
00048         default:
00049           ROS_ERROR("Unsupported number of channels: %i", cv_ptr->image.channels());
00050           break;
00051       }
00052     } else
00053     {
00054       string image_encoding = message->format.substr(0, split_pos);
00055 
00056       cv_ptr->encoding = image_encoding;
00057 
00058       if ( enc::isColor(image_encoding))
00059       {
00060         string compressed_encoding = message->format.substr(split_pos);
00061         bool compressed_bgr_image = (compressed_encoding.find("compressed bgr")!=string::npos);
00062 
00063         // Revert color transformation
00064         if (compressed_bgr_image)
00065         {
00066           // if necessary convert colors from bgr to rgb
00067           if ((image_encoding == enc::RGB8) || (image_encoding == enc::RGB16))
00068             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2RGB);
00069 
00070           if ((image_encoding == enc::RGBA8) || (image_encoding == enc::RGBA16))
00071             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2RGBA);
00072 
00073           if ((image_encoding == enc::BGRA8) || (image_encoding == enc::BGRA16))
00074             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2BGRA);
00075         } else
00076         {
00077           // if necessary convert colors from rgb to bgr
00078           if ((image_encoding == enc::BGR8) || (image_encoding == enc::BGR16))
00079             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2BGR);
00080 
00081           if ((image_encoding == enc::BGRA8) || (image_encoding == enc::BGRA16))
00082             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2BGRA);
00083 
00084           if ((image_encoding == enc::RGBA8) || (image_encoding == enc::RGBA16))
00085             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2RGBA);
00086         }
00087       }
00088     }
00089   }
00090   catch (cv::Exception& e)
00091   {
00092     ROS_ERROR("%s", e.what());
00093   }
00094 
00095   size_t rows = cv_ptr->image.rows;
00096   size_t cols = cv_ptr->image.cols;
00097 
00098   if ((rows > 0) && (cols > 0))
00099     // Publish message to user callback
00100     user_cb(cv_ptr->toImageMsg());
00101 }
00102 
00103 } //namespace compressed_image_transport


compressed_image_transport
Author(s): Patrick Mihelich, Julius Kammerl
autogenerated on Sat Dec 28 2013 17:06:09