compressed_subscriber.cpp
Go to the documentation of this file.
00001 #include "compressed_imagem_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_imagem_transport {
00018 
00019         void CompressedSubscriber::internalCallback(const sensor_msgs::CompressedImageConstPtr& message,
00020                         const message_transport::SimpleSubscriberPlugin<sensor_msgs::Image,sensor_msgs::CompressedImage>::Callback& user_cb)
00021     {
00022         cv_bridge::CvImagePtr cv_ptr(new cv_bridge::CvImage);
00023 
00024         // Copy message header
00025         cv_ptr->header = message->header;
00026 
00027         // Decode color/mono image
00028         try {
00029             cv_ptr->image = cv::imdecode(cv::Mat(message->data), CV_LOAD_IMAGE_UNCHANGED);
00030 
00031             // Assign image encoding string
00032             const size_t split_pos = message->format.find(';');
00033             if (split_pos==string::npos) {
00034                 // Older version of compressed_image_transport does not signal image format
00035                 switch (cv_ptr->image.channels())
00036                 {
00037                     case 1:
00038                         cv_ptr->encoding = enc::MONO8;
00039                         break;
00040                     case 3:
00041                         cv_ptr->encoding = enc::BGR8;
00042                         break;
00043                     default:
00044                         ROS_ERROR("Unsupported number of channels: %i", cv_ptr->image.channels());
00045                         break;
00046                 }
00047             } else {
00048                 string image_encoding = message->format.substr(0, message->format.find(';'));
00049                 cv_ptr->encoding = image_encoding;
00050 
00051                 if ( enc::isColor(image_encoding)) {
00052                     // Revert color transformation
00053                     if ((image_encoding == enc::BGR8) || (image_encoding == enc::BGR16)) {
00054                         cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2BGR);
00055                     }
00056 
00057                     if ((image_encoding == enc::BGRA8) || (image_encoding == enc::BGRA16)) {
00058                         cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2BGRA);
00059                     }
00060 
00061                     if ((image_encoding == enc::RGBA8) || (image_encoding == enc::RGBA16)) {
00062                         cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2RGBA);
00063                     }
00064                 }
00065             }
00066         } catch (cv::Exception& e) {
00067             ROS_ERROR("%s", e.what());
00068         }
00069 
00070         size_t rows = cv_ptr->image.rows;
00071         size_t cols = cv_ptr->image.cols;
00072 
00073         if ((rows > 0) && (cols > 0)) {
00074             // Publish message to user callback
00075             user_cb(cv_ptr->toImageMsg());
00076         }
00077     }
00078 
00079 } //namespace compressed_image_transport


compressed_imagem_transport
Author(s): Cedric Pradalier
autogenerated on Sun Oct 5 2014 23:49:37