image_encodings.h
Go to the documentation of this file.
1 
2 /*********************************************************************
3 * Software License Agreement (BSD License)
4 *
5 * Copyright (c) 2009, Willow Garage, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of the Willow Garage nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *********************************************************************/
35 
36 #ifndef SENSOR_MSGS_IMAGE_ENCODINGS_H
37 #define SENSOR_MSGS_IMAGE_ENCODINGS_H
38 
39 #include <cstdlib>
40 #include <stdexcept>
41 #include <string>
42 
43 namespace sensor_msgs
44 {
45  namespace image_encodings
46  {
47  const std::string RGB8 = "rgb8";
48  const std::string RGBA8 = "rgba8";
49  const std::string RGB16 = "rgb16";
50  const std::string RGBA16 = "rgba16";
51  const std::string BGR8 = "bgr8";
52  const std::string BGRA8 = "bgra8";
53  const std::string BGR16 = "bgr16";
54  const std::string BGRA16 = "bgra16";
55  const std::string MONO8="mono8";
56  const std::string MONO16="mono16";
57 
58  // OpenCV CvMat types
59  const std::string TYPE_8UC1="8UC1";
60  const std::string TYPE_8UC2="8UC2";
61  const std::string TYPE_8UC3="8UC3";
62  const std::string TYPE_8UC4="8UC4";
63  const std::string TYPE_8SC1="8SC1";
64  const std::string TYPE_8SC2="8SC2";
65  const std::string TYPE_8SC3="8SC3";
66  const std::string TYPE_8SC4="8SC4";
67  const std::string TYPE_16UC1="16UC1";
68  const std::string TYPE_16UC2="16UC2";
69  const std::string TYPE_16UC3="16UC3";
70  const std::string TYPE_16UC4="16UC4";
71  const std::string TYPE_16SC1="16SC1";
72  const std::string TYPE_16SC2="16SC2";
73  const std::string TYPE_16SC3="16SC3";
74  const std::string TYPE_16SC4="16SC4";
75  const std::string TYPE_32SC1="32SC1";
76  const std::string TYPE_32SC2="32SC2";
77  const std::string TYPE_32SC3="32SC3";
78  const std::string TYPE_32SC4="32SC4";
79  const std::string TYPE_32FC1="32FC1";
80  const std::string TYPE_32FC2="32FC2";
81  const std::string TYPE_32FC3="32FC3";
82  const std::string TYPE_32FC4="32FC4";
83  const std::string TYPE_64FC1="64FC1";
84  const std::string TYPE_64FC2="64FC2";
85  const std::string TYPE_64FC3="64FC3";
86  const std::string TYPE_64FC4="64FC4";
87 
88  // Bayer encodings
89  const std::string BAYER_RGGB8="bayer_rggb8";
90  const std::string BAYER_BGGR8="bayer_bggr8";
91  const std::string BAYER_GBRG8="bayer_gbrg8";
92  const std::string BAYER_GRBG8="bayer_grbg8";
93  const std::string BAYER_RGGB16="bayer_rggb16";
94  const std::string BAYER_BGGR16="bayer_bggr16";
95  const std::string BAYER_GBRG16="bayer_gbrg16";
96  const std::string BAYER_GRBG16="bayer_grbg16";
97 
98  // Miscellaneous
99  // This is the UYVY version of YUV422 codec http://www.fourcc.org/yuv.php#UYVY
100  // with an 8-bit depth
101  const std::string YUV422="yuv422";
102 
103  // Prefixes for abstract image encodings
104  const std::string ABSTRACT_ENCODING_PREFIXES[] = {
105  "8UC", "8SC", "16UC", "16SC", "32SC", "32FC", "64FC"};
106 
107  // Utility functions for inspecting an encoding string
108  static inline bool isColor(const std::string& encoding)
109  {
110  return encoding == RGB8 || encoding == BGR8 ||
111  encoding == RGBA8 || encoding == BGRA8 ||
112  encoding == RGB16 || encoding == BGR16 ||
113  encoding == RGBA16 || encoding == BGRA16;
114  }
115 
116  static inline bool isMono(const std::string& encoding)
117  {
118  return encoding == MONO8 || encoding == MONO16;
119  }
120 
121  static inline bool isBayer(const std::string& encoding)
122  {
123  return encoding == BAYER_RGGB8 || encoding == BAYER_BGGR8 ||
124  encoding == BAYER_GBRG8 || encoding == BAYER_GRBG8 ||
125  encoding == BAYER_RGGB16 || encoding == BAYER_BGGR16 ||
126  encoding == BAYER_GBRG16 || encoding == BAYER_GRBG16;
127  }
128 
129  static inline bool hasAlpha(const std::string& encoding)
130  {
131  return encoding == RGBA8 || encoding == BGRA8 ||
132  encoding == RGBA16 || encoding == BGRA16;
133  }
134 
135  static inline int numChannels(const std::string& encoding)
136  {
137  // First do the common-case encodings
138  if (encoding == MONO8 ||
139  encoding == MONO16)
140  return 1;
141  if (encoding == BGR8 ||
142  encoding == RGB8 ||
143  encoding == BGR16 ||
144  encoding == RGB16)
145  return 3;
146  if (encoding == BGRA8 ||
147  encoding == RGBA8 ||
148  encoding == BGRA16 ||
149  encoding == RGBA16)
150  return 4;
151  if (encoding == BAYER_RGGB8 ||
152  encoding == BAYER_BGGR8 ||
153  encoding == BAYER_GBRG8 ||
154  encoding == BAYER_GRBG8 ||
155  encoding == BAYER_RGGB16 ||
156  encoding == BAYER_BGGR16 ||
157  encoding == BAYER_GBRG16 ||
158  encoding == BAYER_GRBG16)
159  return 1;
160 
161  // Now all the generic content encodings
162  // TODO: Rewrite with regex when ROS supports C++11
163  for (size_t i=0; i < sizeof(ABSTRACT_ENCODING_PREFIXES) / sizeof(*ABSTRACT_ENCODING_PREFIXES); i++)
164  {
165  std::string prefix = ABSTRACT_ENCODING_PREFIXES[i];
166  if (encoding.substr(0, prefix.size()) != prefix)
167  continue;
168  if (encoding.size() == prefix.size())
169  return 1; // ex. 8UC -> 1
170  int n_channel = atoi(encoding.substr(prefix.size(),
171  encoding.size() - prefix.size()).c_str()); // ex. 8UC5 -> 5
172  if (n_channel != 0)
173  return n_channel; // valid encoding string
174  }
175 
176  if (encoding == YUV422)
177  return 2;
178 
179  throw std::runtime_error("Unknown encoding " + encoding);
180  return -1;
181  }
182 
183  static inline int bitDepth(const std::string& encoding)
184  {
185  if (encoding == MONO16)
186  return 16;
187  if (encoding == MONO8 ||
188  encoding == BGR8 ||
189  encoding == RGB8 ||
190  encoding == BGRA8 ||
191  encoding == RGBA8 ||
192  encoding == BAYER_RGGB8 ||
193  encoding == BAYER_BGGR8 ||
194  encoding == BAYER_GBRG8 ||
195  encoding == BAYER_GRBG8)
196  return 8;
197 
198  if (encoding == MONO16 ||
199  encoding == BGR16 ||
200  encoding == RGB16 ||
201  encoding == BGRA16 ||
202  encoding == RGBA16 ||
203  encoding == BAYER_RGGB16 ||
204  encoding == BAYER_BGGR16 ||
205  encoding == BAYER_GBRG16 ||
206  encoding == BAYER_GRBG16)
207  return 16;
208 
209  // Now all the generic content encodings
210  // TODO: Rewrite with regex when ROS supports C++11
211  for (size_t i=0; i < sizeof(ABSTRACT_ENCODING_PREFIXES) / sizeof(*ABSTRACT_ENCODING_PREFIXES); i++)
212  {
213  std::string prefix = ABSTRACT_ENCODING_PREFIXES[i];
214  if (encoding.substr(0, prefix.size()) != prefix)
215  continue;
216  if (encoding.size() == prefix.size())
217  return atoi(prefix.c_str()); // ex. 8UC -> 8
218  int n_channel = atoi(encoding.substr(prefix.size(),
219  encoding.size() - prefix.size()).c_str()); // ex. 8UC10 -> 10
220  if (n_channel != 0)
221  return atoi(prefix.c_str()); // valid encoding string
222  }
223 
224  if (encoding == YUV422)
225  return 8;
226 
227  throw std::runtime_error("Unknown encoding " + encoding);
228  return -1;
229  }
230  }
231 }
232 
233 #endif
const std::string TYPE_16UC4
const std::string BAYER_GRBG8
const std::string BAYER_GRBG16
const std::string TYPE_64FC3
const std::string BAYER_RGGB16
static bool isBayer(const std::string &encoding)
const std::string TYPE_32FC4
static bool hasAlpha(const std::string &encoding)
const std::string BAYER_GBRG8
const std::string BAYER_GBRG16
static bool isMono(const std::string &encoding)
const std::string BAYER_BGGR16
static bool isColor(const std::string &encoding)
Tools for manipulating sensor_msgs.
const std::string ABSTRACT_ENCODING_PREFIXES[]
const std::string TYPE_32FC1
const std::string TYPE_16SC3
const std::string TYPE_16UC1
const std::string TYPE_64FC4
const std::string TYPE_16UC3
const std::string TYPE_16UC2
const std::string TYPE_32SC2
const std::string BAYER_BGGR8
const std::string TYPE_64FC1
static int numChannels(const std::string &encoding)
const std::string TYPE_64FC2
const std::string TYPE_32SC3
const std::string BAYER_RGGB8
const std::string TYPE_32SC4
const std::string TYPE_32FC3
static int bitDepth(const std::string &encoding)
const std::string TYPE_16SC2
const std::string TYPE_32FC2
const std::string TYPE_32SC1
const std::string TYPE_16SC4
const std::string TYPE_16SC1


sensor_msgs
Author(s): Tully Foote
autogenerated on Thu Jan 14 2021 03:25:16