00001 /*!@file Video/VideoFormat.C definitions of possible image grabbing modes */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Video/VideoFormat.C $ 00035 // $Id: VideoFormat.C 12951 2010-03-05 19:06:12Z lior $ 00036 // 00037 00038 #include "Video/VideoFormat.H" 00039 00040 #include "Image/Dims.H" 00041 #include "Util/StringConversions.H" 00042 #include "Util/StringUtil.H" // for toUpperCase() 00043 #include "Util/log.H" 00044 00045 // ###################################################################### 00046 std::string convertToString(const VideoFormat val) 00047 { 00048 switch (val) 00049 { 00050 case VIDFMT_GREY: return "GREY"; 00051 case VIDFMT_RAW: return "RAW"; 00052 case VIDFMT_RGB555: return "RGB555"; 00053 case VIDFMT_RGB565: return "RGB565"; 00054 case VIDFMT_RGB24: return "RGB24"; 00055 case VIDFMT_RGB32: return "RGB32"; 00056 case VIDFMT_YUV24: return "YUV24"; 00057 case VIDFMT_YUYV: return "YUYV"; 00058 case VIDFMT_UYVY: return "UYVY"; 00059 case VIDFMT_YUV444: return "YUV444"; 00060 case VIDFMT_YUV422: return "YUV422"; 00061 case VIDFMT_YUV411: return "YUV411"; 00062 case VIDFMT_YUV420: return "YUV420"; 00063 case VIDFMT_YUV410: return "YUV410"; 00064 case VIDFMT_YUV444P: return "YUV444P"; 00065 case VIDFMT_YUV422P: return "YUV422P"; 00066 case VIDFMT_YUV411P: return "YUV411P"; 00067 case VIDFMT_YUV420P: return "YUV420P"; 00068 case VIDFMT_YUV410P: return "YUV410P"; 00069 case VIDFMT_HM12: return "HM12"; 00070 case VIDFMT_BAYER_GB: return "BAYER_GB"; 00071 case VIDFMT_BAYER_GR: return "BAYER_GR"; 00072 case VIDFMT_BAYER_RG: return "BAYER_RG"; 00073 case VIDFMT_BAYER_BG: return "BAYER_BG"; 00074 case VIDFMT_BAYER_GB12: return "BAYER_GB12"; 00075 case VIDFMT_BAYER_GR12: return "BAYER_GR12"; 00076 case VIDFMT_BAYER_RG12: return "BAYER_RG12"; 00077 case VIDFMT_BAYER_BG12: return "BAYER_BG12"; 00078 case VIDFMT_MJPEG: return "MJPEG"; 00079 case VIDFMT_AUTO: return "Auto"; 00080 } 00081 00082 LFATAL("Invalid VideoFormat value %d (valid range is 0-%d inclusive)", 00083 int(val), int(VIDFMT_AUTO)); 00084 00085 /* can't happen */ return std::string(); 00086 } 00087 00088 // ###################################################################### 00089 void convertFromString(const std::string& orig, VideoFormat& val) 00090 { 00091 const std::string upper = toUpperCase(orig); 00092 00093 for (int i = 0; i <= VIDFMT_AUTO; ++i) 00094 if (upper.compare(toUpperCase(convertToString(VideoFormat(i)))) == 0) 00095 { val = VideoFormat(i); return; } 00096 00097 conversion_error::raise<VideoFormat>(orig); 00098 } 00099 00100 // ###################################################################### 00101 bool isSimplePackedMode(const VideoFormat vidformat) 00102 { 00103 switch (vidformat) 00104 { 00105 case VIDFMT_GREY: return true; 00106 case VIDFMT_RAW: return false; // unknown format? 00107 case VIDFMT_RGB555: return false; // not a simple bytewise format, so not easily deinterlaceable 00108 case VIDFMT_RGB565: return false; // not a simple bytewise format, so not easily deinterlaceable 00109 case VIDFMT_RGB24: return true; 00110 case VIDFMT_RGB32: return true; 00111 case VIDFMT_YUV24: return true; 00112 case VIDFMT_YUYV: return true; 00113 case VIDFMT_UYVY: return true; 00114 case VIDFMT_YUV444: return true; 00115 case VIDFMT_YUV422: return true; 00116 case VIDFMT_YUV411: return false; 00117 case VIDFMT_YUV420: return false; 00118 case VIDFMT_YUV410: return false; 00119 case VIDFMT_YUV444P: return false; 00120 case VIDFMT_YUV422P: return false; 00121 case VIDFMT_YUV411P: return false; 00122 case VIDFMT_YUV420P: return false; 00123 case VIDFMT_YUV410P: return false; 00124 case VIDFMT_HM12: return false; 00125 case VIDFMT_BAYER_GB: return false; 00126 case VIDFMT_BAYER_GR: return false; 00127 case VIDFMT_BAYER_RG: return false; 00128 case VIDFMT_BAYER_BG: return false; 00129 case VIDFMT_BAYER_GB12: return false; 00130 case VIDFMT_BAYER_GR12: return false; 00131 case VIDFMT_BAYER_RG12: return false; 00132 case VIDFMT_BAYER_BG12: return false; 00133 case VIDFMT_MJPEG: return true; 00134 00135 case VIDFMT_AUTO: return false; 00136 } 00137 00138 LFATAL("invalid VideoFormat %d", int(vidformat)); 00139 /* can't happen */ return -1; 00140 } 00141 00142 // ###################################################################### 00143 int getScanlineWidth(const VideoFormat vidformat, 00144 const int imgwidth) 00145 { 00146 // we return -1 if vidformat is not a packed pixel format: 00147 00148 switch (vidformat) 00149 { 00150 case VIDFMT_GREY: return imgwidth; 00151 case VIDFMT_RAW: return -1; // unknown format? 00152 case VIDFMT_RGB555: return -1; // not a simple bytewise format, so not easily deinterlaceable 00153 case VIDFMT_RGB565: return -1; // not a simple bytewise format, so not easily deinterlaceable 00154 case VIDFMT_RGB24: return imgwidth * 3; 00155 case VIDFMT_RGB32: return imgwidth * 4; 00156 case VIDFMT_YUV24: return imgwidth * 3; 00157 case VIDFMT_YUYV: return imgwidth * 2; 00158 case VIDFMT_UYVY: return imgwidth * 2; 00159 case VIDFMT_YUV444: return imgwidth * 3; 00160 case VIDFMT_YUV422: return imgwidth * 2; 00161 case VIDFMT_YUV411: return -1; 00162 case VIDFMT_YUV420: return -1; 00163 case VIDFMT_YUV410: return -1; 00164 case VIDFMT_YUV444P: return -1; 00165 case VIDFMT_YUV422P: return -1; 00166 case VIDFMT_YUV411P: return -1; 00167 case VIDFMT_YUV420P: return -1; 00168 case VIDFMT_YUV410P: return -1; 00169 case VIDFMT_HM12: return -1; 00170 case VIDFMT_BAYER_GB: return imgwidth; 00171 case VIDFMT_BAYER_GR: return imgwidth; 00172 case VIDFMT_BAYER_RG: return imgwidth; 00173 case VIDFMT_BAYER_BG: return imgwidth; 00174 case VIDFMT_BAYER_GB12: return imgwidth; 00175 case VIDFMT_BAYER_GR12: return imgwidth; 00176 case VIDFMT_BAYER_RG12: return imgwidth; 00177 case VIDFMT_BAYER_BG12: return imgwidth; 00178 case VIDFMT_MJPEG: return imgwidth; 00179 case VIDFMT_AUTO: return -1; 00180 } 00181 00182 LFATAL("invalid VideoFormat %d", int(vidformat)); 00183 /* can't happen */ return -1; 00184 } 00185 00186 // ###################################################################### 00187 void getBytesPerPixelForMode(const VideoFormat vidformat, 00188 unsigned int* numer, 00189 unsigned int* denom) 00190 { 00191 ASSERT(numer != 0); 00192 ASSERT(denom != 0); 00193 00194 switch (vidformat) 00195 { 00196 case VIDFMT_GREY: *numer=1; *denom=1; break; 00197 case VIDFMT_RAW: *numer=4; *denom=1; break; // FIXME is this right? 00198 case VIDFMT_RGB555: *numer=2; *denom=1; break; 00199 case VIDFMT_RGB565: *numer=2; *denom=1; break; 00200 case VIDFMT_RGB24: *numer=3; *denom=1; break; 00201 case VIDFMT_RGB32: *numer=4; *denom=1; break; 00202 case VIDFMT_YUV24: *numer=3; *denom=1; break; 00203 case VIDFMT_YUYV: *numer=2; *denom=1; break; // (Y=1, U=1/2, V=1/2) 00204 case VIDFMT_UYVY: *numer=2; *denom=1; break; // (Y=1, U=1/2, V=1/2) 00205 case VIDFMT_YUV444: *numer=3; *denom=1; break; // (Y=1, U=1, V=1) 00206 case VIDFMT_YUV422: *numer=2; *denom=1; break; // (Y=1, U=1/2, V=1/2) 00207 case VIDFMT_YUV411: *numer=3; *denom=2; break; // (Y=1, U=1/4, V=1/4) 00208 case VIDFMT_YUV420: *numer=3; *denom=2; break; // (Y=1, U=1/4, V=1/4) 00209 case VIDFMT_YUV410: *numer=9; *denom=8; break; // (Y=1, U=1/16, V=1/16) 00210 case VIDFMT_YUV444P: *numer=3; *denom=1; break; // (Y=1, U=1, V=1) 00211 case VIDFMT_YUV422P: *numer=2; *denom=1; break; // (Y=1, U=1/2, V=1/2) 00212 case VIDFMT_YUV411P: *numer=3; *denom=2; break; // (Y=1, U=1/4, V=1/4) 00213 case VIDFMT_YUV420P: *numer=3; *denom=2; break; // (Y=1, U=1/4, V=1/4) 00214 case VIDFMT_YUV410P: *numer=9; *denom=8; break; // (Y=1, U=1/16, V=1/16) 00215 case VIDFMT_HM12: *numer=3; *denom=2; break; // (Y=1, U=1/4, V=1/4) 00216 case VIDFMT_BAYER_GB: *numer=1; *denom=1; break; 00217 case VIDFMT_BAYER_GR: *numer=1; *denom=1; break; 00218 case VIDFMT_BAYER_RG: *numer=1; *denom=1; break; 00219 case VIDFMT_BAYER_BG: *numer=1; *denom=1; break; 00220 case VIDFMT_BAYER_GB12: *numer=2; *denom=1; break; 00221 case VIDFMT_BAYER_GR12: *numer=2; *denom=1; break; 00222 case VIDFMT_BAYER_RG12: *numer=2; *denom=1; break; 00223 case VIDFMT_BAYER_BG12: *numer=2; *denom=1; break; 00224 case VIDFMT_MJPEG: *numer=0; *denom=0; break; 00225 00226 case VIDFMT_AUTO: LFATAL("can't determine bytes per pixel for VIDFMT_AUTO"); 00227 default: LFATAL("invalid VideoFormat %d", int(vidformat)); 00228 } 00229 } 00230 00231 // ###################################################################### 00232 unsigned int getFrameSize(const VideoFormat vidformat, 00233 const Dims& imgdims) 00234 { 00235 const unsigned int sz = imgdims.sz(); 00236 00237 unsigned int numer=0, denom=0; 00238 00239 getBytesPerPixelForMode(vidformat, &numer, &denom); 00240 ASSERT(numer > 0); 00241 ASSERT(denom > 0); 00242 00243 return (sz * numer) / denom; 00244 } 00245 00246 // ###################################################################### 00247 /* So things look consistent in everyone's emacs... */ 00248 /* Local Variables: */ 00249 /* indent-tabs-mode: nil */ 00250 /* End: */