$search
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2009, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Willow Garage nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 #include "sensor_msgs/image_encodings.h" 00036 #include <stdexcept> 00037 00038 namespace sensor_msgs 00039 { 00040 namespace image_encodings 00041 { 00042 const std::string RGB8 = "rgb8"; 00043 const std::string RGBA8 = "rgba8"; 00044 const std::string RGB16 = "rgb16"; 00045 const std::string RGBA16 = "rgba16"; 00046 const std::string BGR8 = "bgr8"; 00047 const std::string BGRA8 = "bgra8"; 00048 const std::string BGR16 = "bgr16"; 00049 const std::string BGRA16 = "bgra16"; 00050 const std::string MONO8="mono8"; 00051 const std::string MONO16="mono16"; 00052 00053 // OpenCV CvMat encodings 00054 const std::string TYPE_8UC1="8UC1"; 00055 const std::string TYPE_8UC2="8UC2"; 00056 const std::string TYPE_8UC3="8UC3"; 00057 const std::string TYPE_8UC4="8UC4"; 00058 const std::string TYPE_8SC1="8SC1"; 00059 const std::string TYPE_8SC2="8SC2"; 00060 const std::string TYPE_8SC3="8SC3"; 00061 const std::string TYPE_8SC4="8SC4"; 00062 const std::string TYPE_16UC1="16UC1"; 00063 const std::string TYPE_16UC2="16UC2"; 00064 const std::string TYPE_16UC3="16UC3"; 00065 const std::string TYPE_16UC4="16UC4"; 00066 const std::string TYPE_16SC1="16SC1"; 00067 const std::string TYPE_16SC2="16SC2"; 00068 const std::string TYPE_16SC3="16SC3"; 00069 const std::string TYPE_16SC4="16SC4"; 00070 const std::string TYPE_32SC1="32SC1"; 00071 const std::string TYPE_32SC2="32SC2"; 00072 const std::string TYPE_32SC3="32SC3"; 00073 const std::string TYPE_32SC4="32SC4"; 00074 const std::string TYPE_32FC1="32FC1"; 00075 const std::string TYPE_32FC2="32FC2"; 00076 const std::string TYPE_32FC3="32FC3"; 00077 const std::string TYPE_32FC4="32FC4"; 00078 const std::string TYPE_64FC1="64FC1"; 00079 const std::string TYPE_64FC2="64FC2"; 00080 const std::string TYPE_64FC3="64FC3"; 00081 const std::string TYPE_64FC4="64FC4"; 00082 00083 // Bayer encodings 00084 const std::string BAYER_RGGB8="bayer_rggb8"; 00085 const std::string BAYER_BGGR8="bayer_bggr8"; 00086 const std::string BAYER_GBRG8="bayer_gbrg8"; 00087 const std::string BAYER_GRBG8="bayer_grbg8"; 00088 const std::string BAYER_RGGB16="bayer_rggb16"; 00089 const std::string BAYER_BGGR16="bayer_bggr16"; 00090 const std::string BAYER_GBRG16="bayer_gbrg16"; 00091 const std::string BAYER_GRBG16="bayer_grbg16"; 00092 00093 // Miscellaneous 00094 const std::string YUV422="yuv422"; 00095 00096 bool isColor(const std::string& encoding) 00097 { 00098 return encoding == RGB8 || encoding == BGR8 || 00099 encoding == RGBA8 || encoding == BGRA8 || 00100 encoding == RGB16 || encoding == BGR16 || 00101 encoding == RGBA16 || encoding == BGRA16; 00102 } 00103 00104 bool isMono(const std::string& encoding) 00105 { 00106 return encoding == MONO8 || encoding == MONO16; 00107 } 00108 00109 bool isBayer(const std::string& encoding) 00110 { 00111 return encoding == BAYER_RGGB8 || encoding == BAYER_BGGR8 || 00112 encoding == BAYER_GBRG8 || encoding == BAYER_GRBG8 || 00113 encoding == BAYER_RGGB16 || encoding == BAYER_BGGR16 || 00114 encoding == BAYER_GBRG16 || encoding == BAYER_GRBG16; 00115 } 00116 00117 bool hasAlpha(const std::string& encoding) 00118 { 00119 return encoding == RGBA8 || encoding == BGRA8 || 00120 encoding == RGBA16 || encoding == BGRA16; 00121 } 00122 00123 int numChannels(const std::string& encoding) 00124 { 00125 // First do the common-case encodings 00126 if (encoding == MONO8 || 00127 encoding == MONO16) 00128 return 1; 00129 if (encoding == BGR8 || 00130 encoding == RGB8 || 00131 encoding == BGR16 || 00132 encoding == RGB16) 00133 return 3; 00134 if (encoding == BGRA8 || 00135 encoding == RGBA8 || 00136 encoding == BGRA16 || 00137 encoding == RGBA16) 00138 return 4; 00139 if (encoding == BAYER_RGGB8 || 00140 encoding == BAYER_BGGR8 || 00141 encoding == BAYER_GBRG8 || 00142 encoding == BAYER_GRBG8 || 00143 encoding == BAYER_RGGB16 || 00144 encoding == BAYER_BGGR16 || 00145 encoding == BAYER_GBRG16 || 00146 encoding == BAYER_GRBG16) 00147 return 1; 00148 00149 // Now all the generic content encodings 00150 #define CHECK_CHANNELS(N) \ 00151 if (encoding == TYPE_8UC##N || \ 00152 encoding == TYPE_8SC##N || \ 00153 encoding == TYPE_16UC##N || \ 00154 encoding == TYPE_16SC##N || \ 00155 encoding == TYPE_32SC##N || \ 00156 encoding == TYPE_32FC##N || \ 00157 encoding == TYPE_64FC##N) \ 00158 return N; \ 00159 /***/ 00160 00161 CHECK_CHANNELS(1); 00162 CHECK_CHANNELS(2); 00163 CHECK_CHANNELS(3); 00164 CHECK_CHANNELS(4); 00165 00166 #undef CHECK_CHANNELS 00167 00168 throw std::runtime_error("Unknown encoding " + encoding); 00169 return -1; 00170 } 00171 00172 int bitDepth(const std::string& encoding) 00173 { 00174 if (encoding == MONO16) 00175 return 16; 00176 if (encoding == MONO8 || 00177 encoding == BGR8 || 00178 encoding == RGB8 || 00179 encoding == BGRA8 || 00180 encoding == RGBA8 || 00181 encoding == BAYER_RGGB8 || 00182 encoding == BAYER_BGGR8 || 00183 encoding == BAYER_GBRG8 || 00184 encoding == BAYER_GRBG8) 00185 return 8; 00186 00187 if (encoding == MONO16 || 00188 encoding == BGR16 || 00189 encoding == RGB16 || 00190 encoding == BGRA16 || 00191 encoding == RGBA16 || 00192 encoding == BAYER_RGGB16 || 00193 encoding == BAYER_BGGR16 || 00194 encoding == BAYER_GBRG16 || 00195 encoding == BAYER_GRBG16) 00196 return 16; 00197 00198 // B = bits (8, 16, ...), T = type (U, S, F) 00199 #define CHECK_BIT_DEPTH(B, T) \ 00200 if (encoding == TYPE_##B##T##C1 || \ 00201 encoding == TYPE_##B##T##C2 || \ 00202 encoding == TYPE_##B##T##C3 || \ 00203 encoding == TYPE_##B##T##C4) \ 00204 return B; \ 00205 /***/ 00206 00207 CHECK_BIT_DEPTH(8, U); 00208 CHECK_BIT_DEPTH(8, S); 00209 CHECK_BIT_DEPTH(16, U); 00210 CHECK_BIT_DEPTH(16, S); 00211 CHECK_BIT_DEPTH(32, S); 00212 CHECK_BIT_DEPTH(32, F); 00213 CHECK_BIT_DEPTH(64, F); 00214 00215 #undef CHECK_BIT_DEPTH 00216 00217 throw std::runtime_error("Unknown encoding " + encoding); 00218 return -1; 00219 } 00220 } 00221 }