compressed_subscriber.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 * 
00004 *  Copyright (c) 20012, Willow Garage, Inc.
00005 *  All rights reserved.
00006 * 
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 * 
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 * 
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 #include "compressed_image_transport/compressed_subscriber.h"
00036 #include <sensor_msgs/image_encodings.h>
00037 #include <cv_bridge/cv_bridge.h>
00038 #include <opencv/cvwimage.h>
00039 #include <opencv/highgui.h>
00040 #include <opencv2/imgproc/imgproc.hpp>
00041 
00042 #include "compressed_image_transport/compression_common.h"
00043 
00044 #include <limits>
00045 #include <vector>
00046 
00047 using namespace cv;
00048 
00049 namespace enc = sensor_msgs::image_encodings;
00050 
00051 namespace compressed_image_transport
00052 {
00053 
00054 void CompressedSubscriber::internalCallback(const sensor_msgs::CompressedImageConstPtr& message,
00055                                             const Callback& user_cb)
00056 
00057 {
00058 
00059   cv_bridge::CvImagePtr cv_ptr(new cv_bridge::CvImage);
00060 
00061   // Copy message header
00062   cv_ptr->header = message->header;
00063 
00064   // Decode color/mono image
00065   try
00066   {
00067     cv_ptr->image = cv::imdecode(cv::Mat(message->data), CV_LOAD_IMAGE_UNCHANGED);
00068 
00069     // Assign image encoding string
00070     const size_t split_pos = message->format.find(';');
00071     if (split_pos==string::npos)
00072     {
00073       // Older version of compressed_image_transport does not signal image format
00074       switch (cv_ptr->image.channels())
00075       {
00076         case 1:
00077           cv_ptr->encoding = enc::MONO8;
00078           break;
00079         case 3:
00080           cv_ptr->encoding = enc::BGR8;
00081           break;
00082         default:
00083           ROS_ERROR("Unsupported number of channels: %i", cv_ptr->image.channels());
00084           break;
00085       }
00086     } else
00087     {
00088       string image_encoding = message->format.substr(0, split_pos);
00089 
00090       cv_ptr->encoding = image_encoding;
00091 
00092       if ( enc::isColor(image_encoding))
00093       {
00094         string compressed_encoding = message->format.substr(split_pos);
00095         bool compressed_bgr_image = (compressed_encoding.find("compressed bgr")!=string::npos);
00096 
00097         // Revert color transformation
00098         if (compressed_bgr_image)
00099         {
00100           // if necessary convert colors from bgr to rgb
00101           if ((image_encoding == enc::RGB8) || (image_encoding == enc::RGB16))
00102             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2RGB);
00103 
00104           if ((image_encoding == enc::RGBA8) || (image_encoding == enc::RGBA16))
00105             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2RGBA);
00106 
00107           if ((image_encoding == enc::BGRA8) || (image_encoding == enc::BGRA16))
00108             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2BGRA);
00109         } else
00110         {
00111           // if necessary convert colors from rgb to bgr
00112           if ((image_encoding == enc::BGR8) || (image_encoding == enc::BGR16))
00113             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2BGR);
00114 
00115           if ((image_encoding == enc::BGRA8) || (image_encoding == enc::BGRA16))
00116             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2BGRA);
00117 
00118           if ((image_encoding == enc::RGBA8) || (image_encoding == enc::RGBA16))
00119             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2RGBA);
00120         }
00121       }
00122     }
00123   }
00124   catch (cv::Exception& e)
00125   {
00126     ROS_ERROR("%s", e.what());
00127   }
00128 
00129   size_t rows = cv_ptr->image.rows;
00130   size_t cols = cv_ptr->image.cols;
00131 
00132   if ((rows > 0) && (cols > 0))
00133     // Publish message to user callback
00134     user_cb(cv_ptr->toImageMsg());
00135 }
00136 
00137 } //namespace compressed_image_transport


compressed_image_transport
Author(s): Patrick Mihelich, Julius Kammerl
autogenerated on Fri Aug 28 2015 11:08:23