Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00050 #include "image.h"
00051 #include <sensor_msgs/Image.h>
00052 #include <sensor_msgs/image_encodings.h>
00053 #include <boost/format.hpp>
00054 #include <stdexcept>
00055
00056 namespace visp_bridge
00057 {
00058
00059 sensor_msgs::Image toSensorMsgsImage(const vpImage<unsigned char>& src)
00060 {
00061 sensor_msgs::Image dst;
00062 dst.width = src.getWidth();
00063 dst.height = src.getHeight();
00064 dst.encoding = sensor_msgs::image_encodings::MONO8;
00065 dst.step = src.getWidth();
00066 dst.data.resize(dst.height * dst.step);
00067 memcpy(&dst.data[0], src.bitmap, dst.height * dst.step * sizeof(unsigned char));
00068
00069 return dst;
00070 }
00071
00072 vpImage<unsigned char> toVispImage(const sensor_msgs::Image& src)
00073 {
00074 using sensor_msgs::image_encodings::MONO8;
00075 using sensor_msgs::image_encodings::RGB8;
00076 using sensor_msgs::image_encodings::RGBA8;
00077 using sensor_msgs::image_encodings::BGR8;
00078 using sensor_msgs::image_encodings::BGRA8;
00079
00080
00081 vpImage<unsigned char> dst(src.height, src.width);
00082
00083 if (src.encoding == MONO8)
00084 memcpy(dst.bitmap, &(src.data[0]), dst.getHeight() * src.step * sizeof(unsigned char));
00085 else if (src.encoding == RGB8 || src.encoding == RGBA8 || src.encoding == BGR8 || src.encoding == BGRA8){
00086 unsigned nc = sensor_msgs::image_encodings::numChannels(src.encoding);
00087 unsigned cEnd = (src.encoding == RGBA8 || src.encoding == BGRA8) ? nc - 1 : nc;
00088
00089 for (unsigned i = 0; i < dst.getWidth(); ++i){
00090 for (unsigned j = 0; j < dst.getHeight(); ++j){
00091 int acc = 0;
00092 for (unsigned c = 0; c < cEnd; ++c)
00093 acc += src.data[j * src.step + i * nc + c];
00094 dst[j][i] = acc / nc;
00095 }
00096 }
00097 }
00098 return dst;
00099 }
00100
00101 vpImage<vpRGBa> toVispImageRGBa(const sensor_msgs::Image& src)
00102 {
00103 using sensor_msgs::image_encodings::MONO8;
00104 using sensor_msgs::image_encodings::RGB8;
00105 using sensor_msgs::image_encodings::RGBA8;
00106 using sensor_msgs::image_encodings::BGR8;
00107 using sensor_msgs::image_encodings::BGRA8;
00108
00109
00110 vpImage<vpRGBa> dst(src.height, src.width);
00111
00112 if (src.encoding == MONO8)
00113 for (unsigned i = 0; i < dst.getWidth(); ++i){
00114 for (unsigned j = 0; j < dst.getHeight(); ++j){
00115
00116 dst[j][i] = vpRGBa(src.data[j * src.step + i],src.data[j * src.step + i],src.data[j * src.step + i]);
00117 }
00118 }
00119 else{
00120 unsigned nc = sensor_msgs::image_encodings::numChannels(src.encoding);
00121
00122 for (unsigned i = 0; i < dst.getWidth(); ++i){
00123 for (unsigned j = 0; j < dst.getHeight(); ++j){
00124 dst[j][i] = vpRGBa(src.data[j * src.step + i * nc + 0],src.data[j * src.step + i * nc + 1],src.data[j * src.step + i * nc + 2]);
00125 }
00126 }
00127 }
00128 return dst;
00129 }
00130
00131 sensor_msgs::Image toSensorMsgsImage(const vpImage<vpRGBa>& src){
00132 sensor_msgs::Image dst;
00133 dst.width = src.getWidth();
00134 dst.height = src.getHeight();
00135 dst.encoding = sensor_msgs::image_encodings::RGB8;
00136 unsigned nc = sensor_msgs::image_encodings::numChannels(dst.encoding);
00137 dst.step = src.getWidth()*nc;
00138
00139 dst.data.resize(dst.height * dst.step);
00140 for (unsigned i = 0; i < src.getWidth(); ++i){
00141 for (unsigned j = 0; j < src.getHeight(); ++j){
00142 dst.data[j * dst.step + i * nc + 0] = src.bitmap[j * src.getWidth() + i].R;
00143 dst.data[j * dst.step + i * nc + 1] = src.bitmap[j * src.getWidth() + i].G;
00144 dst.data[j * dst.step + i * nc + 2] = src.bitmap[j * src.getWidth() + i].B;
00145
00146 }
00147 }
00148 return dst;
00149 }
00150 }