32 #include <opencv2/highgui/highgui.hpp> 33 #include <boost/make_shared.hpp> 55 sensor_msgs::CompressedImage
compressImage(
const sensor_msgs::Image& image)
63 sensor_msgs::CompressedImage compressed;
64 compressed.header = image.header;
65 compressed.format = image.encoding;
68 std::vector<int> params;
71 int bitDepth = enc::bitDepth(image.encoding);
73 params.push_back(CV_IMWRITE_PXM_BINARY);
77 compressed.format +=
"; iz compressed ";
80 if ((bitDepth == 8) || (bitDepth == 16))
84 stringstream targetFormat;
85 if (enc::isColor(image.encoding))
88 targetFormat <<
"bgr" << bitDepth;
89 compressed.format += targetFormat.str();
101 std::vector<unsigned char> ppm_buffer;
103 if (cv::imencode(
".ppm", cv_ptr->image, ppm_buffer, params))
105 if (!pi.readHeader((
const unsigned char*) &ppm_buffer[0]))
107 std::stringstream ss;
108 for (
int i = 0; i < 20; i++)
112 ROS_ERROR(
"Unable to read PPM produced by OpenCV. First few bytes: %s", ss.str().c_str());
113 return sensor_msgs::CompressedImage();
115 if (pi.components() != 3)
117 ROS_ERROR(
"Can only handle 24-bit PPM files.");
118 return sensor_msgs::CompressedImage();
120 unsigned char *dest =
static_cast<unsigned char*
>(malloc(pi.height() * pi.width() * 4 + 33));
123 size_t size = destEnd - dest;
124 compressed.data.resize(size);
125 memcpy(&compressed.data[0], dest, size);
129 (iz_time - now).toNSec()/1000000L,
131 compressed.data.size(),
132 (double)image.data.size() / (double)image.data.size());
136 ROS_ERROR(
"cv::imencode (ppm) failed on input image");
137 return sensor_msgs::CompressedImage();
143 return sensor_msgs::CompressedImage();
145 catch (cv::Exception& e)
148 return sensor_msgs::CompressedImage();
156 "Compressed Image Transport - ImageZero compression requires 8/16-bit encoded color format (input format is: %s)",
157 image.encoding.c_str());
160 return sensor_msgs::CompressedImage();
163 sensor_msgs::Image
decompressImage(
const sensor_msgs::CompressedImageConstPtr& compressed)
175 unsigned char* dest = (
unsigned char*) malloc(dataSize + 33);
179 std::vector<unsigned char> ppm_data;
180 size_t size = pi.
data() - dest + dataSize;
181 ppm_data.resize(size);
182 memcpy(&ppm_data[0], dest, size);
188 cv_ptr->header = compressed->header;
193 cv_ptr->image = cv::imdecode(cv::Mat(ppm_data), CV_LOAD_IMAGE_COLOR);
196 const size_t split_pos = compressed->format.find(
';');
197 if (split_pos == std::string::npos)
199 switch (cv_ptr->image.channels())
202 cv_ptr->encoding = enc::MONO8;
205 cv_ptr->encoding = enc::BGR8;
208 ROS_ERROR(
"Unsupported number of channels: %i", cv_ptr->image.channels());
214 std::string image_encoding = compressed->format.substr(0, split_pos);
216 cv_ptr->encoding = image_encoding;
218 if (enc::isColor(image_encoding))
220 std::string compressed_encoding = compressed->format.substr(split_pos);
221 bool compressed_bgr_image = (compressed_encoding.find(
"compressed bgr") != std::string::npos);
224 if (compressed_bgr_image)
227 if ((image_encoding == enc::RGB8) || (image_encoding == enc::RGB16))
229 cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2RGB);
232 if ((image_encoding == enc::RGBA8) || (image_encoding == enc::RGBA16))
234 cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2RGBA);
237 if ((image_encoding == enc::BGRA8) || (image_encoding == enc::BGRA16))
239 cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2BGRA);
245 if ((image_encoding == enc::BGR8) || (image_encoding == enc::BGR16))
247 cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2BGR);
250 if ((image_encoding == enc::BGRA8) || (image_encoding == enc::BGRA16))
252 cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2BGRA);
255 if ((image_encoding == enc::RGBA8) || (image_encoding == enc::RGBA16))
257 cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2RGBA);
263 catch (cv::Exception& e)
268 size_t rows = cv_ptr->image.rows;
269 size_t cols = cv_ptr->image.cols;
271 if ((rows > 0) && (cols > 0))
274 sensor_msgs::Image msg;
275 cv_ptr->toImageMsg(msg);
279 return sensor_msgs::Image();
unsigned char * data() const
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
void setComponents(int components)
sensor_msgs::Image decompressImage(const sensor_msgs::CompressedImageConstPtr &compressed)
unsigned char * writeHeader(unsigned char *data)
static bool decode_tables_initialized
unsigned char * encodeImage(const Image<> &im, unsigned char *dest)
static bool encode_tables_initialized
const unsigned char * decodeImage(Image<> &im, const unsigned char *src)
void decodeImageSize(Image<> &im, const unsigned char *src)
sensor_msgs::CompressedImage compressImage(const sensor_msgs::Image &image)
#define ROS_DEBUG_THROTTLE(period,...)