244 {
245 destination.height = source.cv_mat().rows;
246 destination.width = source.cv_mat().cols;
247 switch (source.cv_mat().type()) {
248 case CV_8UC1:
249 destination.encoding = "mono8";
250 break;
251 case CV_8UC3:
252 destination.encoding = "bgr8";
253 break;
254 case CV_16SC1:
255 destination.encoding = "mono16";
256 break;
257 case CV_8UC4:
258 destination.encoding = "rgba8";
259 break;
260 default:
261 throw std::runtime_error("unsupported encoding type");
262 }
263 destination.step = static_cast<sensor_msgs::msg::Image::_step_type>(source.cv_mat().step);
264 size_t size = source.cv_mat().step * source.cv_mat().rows;
265 destination.data.resize(size);
266 memcpy(&destination.data[0], source.cv_mat().data, size);
267 destination.header = source.header();
268 }