conversion_utils.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * camera_aravis
4  *
5  * Copyright © 2022 Fraunhofer IOSB and contributors
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  ****************************************************************************/
23 
24 #ifndef CAMERA_ARAVIS_CONVERSION_UTILS
25 #define CAMERA_ARAVIS_CONVERSION_UTILS
26 
27 #include <functional>
28 
29 #include <sensor_msgs/Image.h>
31 
32 namespace camera_aravis
33 {
34 
35 // Conversions from integers to Arv types.
36 static const char *szBufferStatusFromInt[] = {"ARV_BUFFER_STATUS_SUCCESS", "ARV_BUFFER_STATUS_CLEARED",
37  "ARV_BUFFER_STATUS_TIMEOUT", "ARV_BUFFER_STATUS_MISSING_PACKETS",
38  "ARV_BUFFER_STATUS_WRONG_PACKET_ID", "ARV_BUFFER_STATUS_SIZE_MISMATCH",
39  "ARV_BUFFER_STATUS_FILLING", "ARV_BUFFER_STATUS_ABORTED"};
40 
41 // Conversion functions from Genicam to ROS formats
42 using ConversionFunction = std::function<void(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out)>;
43 
44 void renameImg(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out, const std::string out_format);
45 void shiftImg(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out, const size_t n_digits, const std::string out_format);
46 void interleaveImg(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out, const size_t n_digits, const std::string out_format);
47 void unpack10p32Img(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out, const std::string out_format);
48 void unpack10PackedImg(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out, const std::string out_format);
49 void unpack10pMonoImg(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out, const std::string out_format);
50 void unpack10PackedMonoImg(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out, const std::string out_format);
51 void unpack12pImg(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out, const std::string out_format);
52 void unpack12PackedImg(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out, const std::string out_format);
53 void unpack565pImg(sensor_msgs::ImagePtr& in, sensor_msgs::ImagePtr& out, const std::string out_format);
54 
55 const std::map<std::string, ConversionFunction> CONVERSIONS_DICTIONARY =
56 {
57  // equivalent to official ROS color encodings
58  { "RGB8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGB8) },
59  { "RGBa8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGBA8) },
60  { "RGB16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGB16) },
61  { "RGBa16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGBA16) },
62  { "BGR8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BGR8) },
63  { "BGRa8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BGRA8) },
64  { "BGR16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BGR16) },
65  { "BGRa16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BGRA16) },
66  { "Mono8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO8) },
67  { "Raw8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO8) },
68  { "R8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO8) },
69  { "G8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO8) },
70  { "B8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO8) },
71  { "Mono16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO16) },
72  { "Raw16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO16) },
73  { "R16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO16) },
74  { "G16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO16) },
75  { "B16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO16) },
76  { "BayerRG8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_RGGB8) },
77  { "BayerBG8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_BGGR8) },
78  { "BayerGB8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GBRG8) },
79  { "BayerGR8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GRBG8) },
80  { "BayerRG16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_RGGB16) },
81  { "BayerBG16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_BGGR16) },
82  { "BayerGB16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GBRG16) },
83  { "BayerGR16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GRBG16) },
84  { "YUV422_8_UYVY", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::YUV422) },
85  { "YUV422_8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::YUV422) },
86  // non-color contents
87  { "Data8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::TYPE_8UC1) },
88  { "Confidence8", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::TYPE_8UC1) },
89  { "Data8s", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::TYPE_8SC1) },
90  { "Data16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::TYPE_16UC1) },
91  { "Confidence16", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::TYPE_16UC1) },
92  { "Data16s", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::TYPE_16SC1) },
93  { "Data32s", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::TYPE_32SC1) },
94  { "Data32f", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::TYPE_32FC1) },
95  { "Confidence32f", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::TYPE_32FC1) },
96  { "Data64f", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::TYPE_64FC1) },
97  // unthrifty formats. Shift away padding Bits for use with ROS.
98  { "Mono10", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 6, sensor_msgs::image_encodings::MONO16) },
99  { "Mono12", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 4, sensor_msgs::image_encodings::MONO16) },
100  { "Mono14", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 2, sensor_msgs::image_encodings::MONO16) },
101  { "RGB10", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 6, sensor_msgs::image_encodings::RGB16) },
102  { "RGB12", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 4, sensor_msgs::image_encodings::RGB16) },
103  { "BGR10", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 6, sensor_msgs::image_encodings::BGR16) },
104  { "BGR12", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 4, sensor_msgs::image_encodings::BGR16) },
105  { "BayerRG10", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 6, sensor_msgs::image_encodings::BAYER_RGGB16) },
106  { "BayerBG10", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 6, sensor_msgs::image_encodings::BAYER_BGGR16) },
107  { "BayerGB10", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 6, sensor_msgs::image_encodings::BAYER_GBRG16) },
108  { "BayerGR10", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 6, sensor_msgs::image_encodings::BAYER_GRBG16) },
109  { "BayerRG12", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 4, sensor_msgs::image_encodings::BAYER_RGGB16) },
110  { "BayerBG12", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 4, sensor_msgs::image_encodings::BAYER_BGGR16) },
111  { "BayerGB12", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 4, sensor_msgs::image_encodings::BAYER_GBRG16) },
112  { "BayerGR12", std::bind(&shiftImg, std::placeholders::_1, std::placeholders::_2, 4, sensor_msgs::image_encodings::BAYER_GRBG16) },
113  // planar instead pixel-by-pixel encodings
114  { "RGB8_Planar", std::bind(&interleaveImg, std::placeholders::_1, std::placeholders::_2, 0, sensor_msgs::image_encodings::RGB8) },
115  { "RGB10_Planar", std::bind(&interleaveImg, std::placeholders::_1, std::placeholders::_2, 6, sensor_msgs::image_encodings::RGB16) },
116  { "RGB12_Planar", std::bind(&interleaveImg, std::placeholders::_1, std::placeholders::_2, 4, sensor_msgs::image_encodings::RGB16) },
117  { "RGB16_Planar", std::bind(&interleaveImg, std::placeholders::_1, std::placeholders::_2, 0, sensor_msgs::image_encodings::RGB16) },
118  // packed, non-Byte aligned formats
119  { "Mono10p", std::bind(&unpack10pMonoImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO16) },
120  { "RGB10p", std::bind(&unpack10p32Img, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGB16) },
121  { "RGB10p32", std::bind(&unpack10p32Img, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGB16) },
122  { "RGBa10p", std::bind(&unpack10p32Img, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGBA16) },
123  { "BGR10p", std::bind(&unpack10p32Img, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BGR16) },
124  { "BGRa10p", std::bind(&unpack10p32Img, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BGRA16) },
125  { "BayerRG10p", std::bind(&unpack10pMonoImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_RGGB16) },
126  { "BayerBG10p", std::bind(&unpack10pMonoImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_BGGR16) },
127  { "BayerGB10p", std::bind(&unpack10pMonoImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GBRG16) },
128  { "BayerGR10p", std::bind(&unpack10pMonoImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GRBG16) },
129  { "Mono12p", std::bind(&unpack12pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO16) },
130  { "RGB12p", std::bind(&unpack12pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGB16) },
131  { "RGBa12p", std::bind(&unpack12pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGBA16) },
132  { "BGR12p", std::bind(&unpack12pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BGR16) },
133  { "BGRa12p", std::bind(&unpack12pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BGRA16) },
134  { "BayerRG12p", std::bind(&unpack12pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_RGGB16) },
135  { "BayerBG12p", std::bind(&unpack12pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_BGGR16) },
136  { "BayerGB12p", std::bind(&unpack12pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GBRG16) },
137  { "BayerGR12p", std::bind(&unpack12pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GRBG16) },
138  { "RGB565p", std::bind(&unpack565pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGB8) },
139  { "BGR565p", std::bind(&unpack565pImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BGR8) },
140  // GigE-Vision specific format naming
141  { "RGB10V1Packed", std::bind(&unpack10PackedImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGB16) },
142  { "RGB10V2Packed", std::bind(&unpack10p32Img, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGB16) },
143  { "RGB12V1Packed", std::bind(&unpack12PackedImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::RGB16) },
144  { "Mono10Packed", std::bind(&unpack10PackedMonoImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO16) },
145  { "Mono12Packed", std::bind(&unpack12PackedImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::MONO16) },
146  { "BayerRG10Packed", std::bind(&unpack10PackedMonoImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_RGGB16) },
147  { "BayerBG10Packed", std::bind(&unpack10PackedMonoImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_BGGR16) },
148  { "BayerGB10Packed", std::bind(&unpack10PackedMonoImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GBRG16) },
149  { "BayerGR10Packed", std::bind(&unpack10PackedMonoImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GRBG16) },
150  { "BayerRG12Packed", std::bind(&unpack12PackedImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_RGGB16) },
151  { "BayerBG12Packed", std::bind(&unpack12PackedImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_BGGR16) },
152  { "BayerGB12Packed", std::bind(&unpack12PackedImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GBRG16) },
153  { "BayerGR12Packed", std::bind(&unpack12PackedImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::BAYER_GRBG16) },
154  { "YUV422Packed", std::bind(&renameImg, std::placeholders::_1, std::placeholders::_2, sensor_msgs::image_encodings::YUV422) }
155 };
156 
157 } // end namespace camera_aravis
158 
159 #endif /* CAMERA_ARAVIS_CONVERSION_UTILS */
camera_aravis::unpack10PackedMonoImg
void unpack10PackedMonoImg(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out, const std::string out_format)
Definition: conversion_utils.cpp:274
camera_aravis::unpack10pMonoImg
void unpack10pMonoImg(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out, const std::string out_format)
Definition: conversion_utils.cpp:224
sensor_msgs::image_encodings::BAYER_RGGB8
const std::string BAYER_RGGB8
sensor_msgs::image_encodings::TYPE_8UC1
const std::string TYPE_8UC1
image_encodings.h
camera_aravis::unpack10p32Img
void unpack10p32Img(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out, const std::string out_format)
Definition: conversion_utils.cpp:130
camera_aravis::szBufferStatusFromInt
static const char * szBufferStatusFromInt[]
Definition: conversion_utils.h:57
sensor_msgs::image_encodings::RGBA8
const std::string RGBA8
sensor_msgs::image_encodings::BAYER_GBRG8
const std::string BAYER_GBRG8
camera_aravis
Definition: camera_aravis_nodelet.h:82
camera_aravis::unpack10PackedImg
void unpack10PackedImg(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out, const std::string out_format)
Definition: conversion_utils.cpp:178
sensor_msgs::image_encodings::RGB8
const std::string RGB8
sensor_msgs::image_encodings::TYPE_16UC1
const std::string TYPE_16UC1
camera_aravis::unpack12pImg
void unpack12pImg(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out, const std::string out_format)
Definition: conversion_utils.cpp:318
sensor_msgs::image_encodings::TYPE_8SC1
const std::string TYPE_8SC1
sensor_msgs::image_encodings::BGR16
const std::string BGR16
camera_aravis::unpack565pImg
void unpack565pImg(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out, const std::string out_format)
Definition: conversion_utils.cpp:404
camera_aravis::interleaveImg
void interleaveImg(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out, const size_t n_digits, const std::string out_format)
Definition: conversion_utils.cpp:85
sensor_msgs::image_encodings::BAYER_BGGR8
const std::string BAYER_BGGR8
camera_aravis::shiftImg
void shiftImg(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out, const size_t n_digits, const std::string out_format)
Definition: conversion_utils.cpp:70
sensor_msgs::image_encodings::YUV422
const std::string YUV422
sensor_msgs::image_encodings::BAYER_BGGR16
const std::string BAYER_BGGR16
sensor_msgs::image_encodings::BGRA16
const std::string BGRA16
sensor_msgs::image_encodings::BAYER_GBRG16
const std::string BAYER_GBRG16
sensor_msgs::image_encodings::RGBA16
const std::string RGBA16
sensor_msgs::image_encodings::BGRA8
const std::string BGRA8
sensor_msgs::image_encodings::TYPE_32SC1
const std::string TYPE_32SC1
sensor_msgs::image_encodings::TYPE_32FC1
const std::string TYPE_32FC1
sensor_msgs::image_encodings::MONO8
const std::string MONO8
sensor_msgs::image_encodings::MONO16
const std::string MONO16
sensor_msgs::image_encodings::TYPE_16SC1
const std::string TYPE_16SC1
sensor_msgs::image_encodings::BGR8
const std::string BGR8
camera_aravis::unpack12PackedImg
void unpack12PackedImg(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out, const std::string out_format)
Definition: conversion_utils.cpp:360
sensor_msgs::image_encodings::BAYER_GRBG8
const std::string BAYER_GRBG8
sensor_msgs::image_encodings::TYPE_64FC1
const std::string TYPE_64FC1
sensor_msgs::image_encodings::BAYER_GRBG16
const std::string BAYER_GRBG16
camera_aravis::CONVERSIONS_DICTIONARY
const std::map< std::string, ConversionFunction > CONVERSIONS_DICTIONARY
Definition: conversion_utils.h:76
camera_aravis::ConversionFunction
std::function< void(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out)> ConversionFunction
Definition: conversion_utils.h:63
camera_aravis::renameImg
void renameImg(sensor_msgs::ImagePtr &in, sensor_msgs::ImagePtr &out, const std::string out_format)
Definition: conversion_utils.cpp:52
sensor_msgs::image_encodings::RGB16
const std::string RGB16
sensor_msgs::image_encodings::BAYER_RGGB16
const std::string BAYER_RGGB16


camera_aravis
Author(s): Boitumelo Ruf, Fraunhofer IOSB , Dominik Kleiser, Fraunhofer IOSB , Dominik A. Klein, Fraunhofer FKIE , Steve Safarik, Straw Lab , Andrew Straw, Straw Lab , Floris van Breugel, van Breugel Lab
autogenerated on Wed Sep 25 2024 02:23:21