Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

vidl2_pixel_format.h

Go to the documentation of this file.
00001 // This is brl/bbas/vidl2/vidl2_pixel_format.h
00002 #ifndef vidl2_pixel_format_h_
00003 #define vidl2_pixel_format_h_
00004 //:
00005 // \file
00006 // \brief Supported pixel formats for video frames
00007 //
00008 // \author Matt Leotta
00009 // \date  13 Jan 2006
00010 //
00011 // This file defines the set of known video frame pixel formats.
00012 // The enum vidl2_pixel_format enumerates the types while the
00013 // template specializations of vidl2_pixel_traits define their
00014 // basic traits.  These specializations are defined using the
00015 // macro vidl2_pt_mac.  The pixel traits are:
00016 //  - <b>name</b> a string representation for the format
00017 //  - <b>type</b> the VXL C++ type used to return a pixel component
00018 //  - <b>bits per pixel</b> the number of bits used to represent a pixel
00019 //  - <b>number of channels</b> the number of color channels encoded
00020 //                              (now determined by color)
00021 //  - <b>color</b> the color encoding of the pixels (i.e. RGB, YUV)
00022 //  - <b>arrangement</b> the way pixels are arranged in memory.  This
00023 //       could be in single file, packed into macropixels, or in planes.
00024 //  - <b>chroma shift X</b> the chroma subsampling factor is 2 ^ shift
00025 //       in the horizontal direction.
00026 //  - <b>chroma shift Y</b> the chroma subsampling factor is 2 ^ shift
00027 //       in the vertical direction.
00028 //
00029 // vidl2_pixel_format differs from vil_pixel_format in that the
00030 // vidl2 formats are representations typically used by video
00031 // hardware and in video codecs to encode a frame.  The vil pixel
00032 // formats are more useful for image processing and are related
00033 // to the C++ data types that may be arranged in a regular array
00034 // to make an image.  A vidl2_pixel_format may have components
00035 // in multiple planes of different sizes or may have data from
00036 // multiple image pixels encoded as a single macro pixel.
00037 //
00038 // This file also contains several functions to check the traits
00039 // of a pixel format at runtime.  These functions use template
00040 // metaprogramming to generate conditionals that probe the formats
00041 // defined in this file.  So you don't need to modify these functions
00042 // when you add a new pixel format into this header file.
00043 
00044 #include <vcl_string.h>
00045 #include <vcl_cstddef.h>
00046 #include <vcl_iosfwd.h>
00047 #include <vxl_config.h>
00048 
00049 //: Describes the format of pixel encoding in a video frame buffer
00050 enum vidl2_pixel_format
00051 {
00052   VIDL2_PIXEL_FORMAT_UNKNOWN = -1,
00053 
00054   VIDL2_PIXEL_FORMAT_RGB_24,
00055   VIDL2_PIXEL_FORMAT_RGB_24P,
00056   VIDL2_PIXEL_FORMAT_BGR_24,
00057   VIDL2_PIXEL_FORMAT_RGBA_32,
00058   VIDL2_PIXEL_FORMAT_RGBA_32P,
00059   VIDL2_PIXEL_FORMAT_RGB_565,
00060   VIDL2_PIXEL_FORMAT_RGB_555,
00061 
00062   VIDL2_PIXEL_FORMAT_YUV_444P,
00063   VIDL2_PIXEL_FORMAT_YUV_422P,
00064   VIDL2_PIXEL_FORMAT_YUV_420P,
00065   VIDL2_PIXEL_FORMAT_YVU_420P,
00066   VIDL2_PIXEL_FORMAT_YUV_411P,
00067   VIDL2_PIXEL_FORMAT_YUV_410P,
00068   VIDL2_PIXEL_FORMAT_UYV_444,
00069   VIDL2_PIXEL_FORMAT_YUYV_422,
00070   VIDL2_PIXEL_FORMAT_UYVY_422,
00071   VIDL2_PIXEL_FORMAT_UYVY_411,
00072 
00073   VIDL2_PIXEL_FORMAT_MONO_1,
00074   VIDL2_PIXEL_FORMAT_MONO_8,
00075   VIDL2_PIXEL_FORMAT_MONO_16,
00076 
00077   // Add values here
00078 
00079   VIDL2_PIXEL_FORMAT_ENUM_END
00080 };
00081 
00082 
00083 //: Describes the color encoding of a pixel format
00084 enum vidl2_pixel_color
00085 {
00086   VIDL2_PIXEL_COLOR_UNKNOWN = -1,
00087 
00088   VIDL2_PIXEL_COLOR_MONO,
00089   VIDL2_PIXEL_COLOR_RGB,
00090   VIDL2_PIXEL_COLOR_RGBA,
00091   VIDL2_PIXEL_COLOR_YUV,
00092 
00093   // Add values here
00094 
00095   VIDL2_PIXEL_COLOR_ENUM_END
00096 };
00097 
00098 
00099 //: Describes the arrangement of pixels in a pixel format
00100 enum vidl2_pixel_arrangement
00101 {
00102   VIDL2_PIXEL_ARRANGE_UNKNOWN = -1,
00103 
00104   VIDL2_PIXEL_ARRANGE_SINGLE,
00105   VIDL2_PIXEL_ARRANGE_PACKED,
00106   VIDL2_PIXEL_ARRANGE_PLANAR,
00107   VIDL2_PIXEL_ARRANGE_PALETTE,
00108 
00109   // Add values here
00110 
00111   VIDL2_PIXEL_ARRANGE_ENUM_END
00112 };
00113 
00114 
00115 //: Traits of the pixel formats
00116 // - name a string name for the format
00117 // - bits_per_pixel the effective number of bits per pixel
00118 // - color the color mode used
00119 // - planar are the pixel components arranged on multiple planes
00120 // - packed are the pixels packed into macro pixels
00121 struct vidl2_pixel_traits
00122 {
00123   vcl_string name;
00124   unsigned bits_per_pixel;
00125   unsigned num_channels;
00126   vidl2_pixel_color color;
00127   vidl2_pixel_arrangement arrangement;
00128   unsigned chroma_shift_x;
00129   unsigned chroma_shift_y;
00130 };
00131 
00132 // ***** Start: temporary hack to avoid conflict *****
00133 #ifdef min
00134 #undef min
00135 #endif
00136 #ifdef max
00137 #undef max
00138 #endif
00139 // ***** End: temporary hack to avoid conflict *****
00140 
00141 //: Define limits on the minimum and maximum pixel values
00142 template <class T>
00143 struct vidl2_pixel_limits;
00144 
00145 VCL_DEFINE_SPECIALIZATION
00146 struct vidl2_pixel_limits<vxl_byte>
00147 {
00148   static inline vxl_byte min() {return 0x00;}
00149   static inline vxl_byte max() {return 0xFF;}
00150 };
00151 
00152 VCL_DEFINE_SPECIALIZATION
00153 struct vidl2_pixel_limits<bool>
00154 {
00155   static inline bool min() {return false;}
00156   static inline bool max() {return true;}
00157 };
00158 
00159 VCL_DEFINE_SPECIALIZATION
00160 struct vidl2_pixel_limits<vxl_uint_16>
00161 {
00162   static inline vxl_uint_16 min() {return 0x0000;}
00163   static inline vxl_uint_16 max() {return 0xFFFF;}
00164 };
00165 
00166 VCL_DEFINE_SPECIALIZATION
00167     struct vidl2_pixel_limits<float>
00168 {
00169   static inline float min() {return 0.0f;}
00170   static inline float max() {return 1.0f;}
00171 };
00172 
00173 VCL_DEFINE_SPECIALIZATION
00174 struct vidl2_pixel_limits<double>
00175 {
00176   static inline double min() {return 0.0;}
00177   static inline double max() {return 1.0;}
00178 };
00179 
00180 
00181 //: Define the color traits for each vidl2_pixel_color
00182 // For now this is just the number of channels
00183 template <vidl2_pixel_color color_type>
00184     struct vidl2_color_traits_of;
00185 #define vidl2_ct_mac(COL,NC)\
00186 VCL_DEFINE_SPECIALIZATION \
00187 struct vidl2_color_traits_of<VIDL2_PIXEL_COLOR_##COL> \
00188 {\
00189   enum { num_channels = NC }; \
00190 }
00191 
00192 vidl2_ct_mac( UNKNOWN,  0 );
00193 vidl2_ct_mac( MONO,     1 );
00194 vidl2_ct_mac( RGB,      3 );
00195 vidl2_ct_mac( RGBA,     4 );
00196 vidl2_ct_mac( YUV,      3 );
00197 
00198 #undef vidl2_ct_mac
00199 
00200 
00201 //: Define traits for a given vidl2_pixel_format
00202 // All pixel traits should be defined using the macro below
00203 // The anonymous enums allow the values available to the
00204 // compiler to for use in generic programming.  For values
00205 // that are already enums, a function is provided so the
00206 // user does not need to worry about enum type clashes.
00207 template <vidl2_pixel_format pix_type>
00208 struct vidl2_pixel_traits_of;
00209 #define vidl2_pt_mac(FMT,NAME,T,BPP,CLR,ARNG,XCS,YCS)\
00210 VCL_DEFINE_SPECIALIZATION \
00211 struct vidl2_pixel_traits_of<VIDL2_PIXEL_FORMAT_##FMT> \
00212 {\
00213   static inline vcl_string name() { return NAME; }\
00214   typedef T type;\
00215   enum { bits_per_pixel = BPP };\
00216   enum { num_channels = vidl2_color_traits_of<VIDL2_PIXEL_COLOR_##CLR>::num_channels };\
00217   static inline vidl2_pixel_color color() { return VIDL2_PIXEL_COLOR_##CLR; }\
00218   enum { color_idx = VIDL2_PIXEL_COLOR_##CLR };\
00219   static inline vidl2_pixel_arrangement arrangement() { return VIDL2_PIXEL_ARRANGE_##ARNG; }\
00220   enum { arrangement_idx = VIDL2_PIXEL_ARRANGE_##ARNG };\
00221   enum { chroma_shift_x = XCS };\
00222   enum { chroma_shift_y = YCS };\
00223 }
00224 
00225 //            format    name        type         bpp  color    arrange  xcs  ycs
00226 //            ------    ---------   ----         ---  -------  -------  ---  ---
00227 vidl2_pt_mac( UNKNOWN,  "unknown",  void,        0,   UNKNOWN, UNKNOWN, 0,   0  );
00228 
00229 vidl2_pt_mac( RGB_24,   "RGB 24",   vxl_byte,    24,  RGB,     SINGLE,  0,   0  );
00230 vidl2_pt_mac( RGB_24P,  "RGB 24P",  vxl_byte,    24,  RGB,     PLANAR,  0,   0  );
00231 vidl2_pt_mac( BGR_24,   "BGR 24",   vxl_byte,    24,  RGB,     SINGLE,  0,   0  );
00232 vidl2_pt_mac( RGBA_32,  "RGBA 32",  vxl_byte,    32,  RGBA,    SINGLE,  0,   0  );
00233 vidl2_pt_mac( RGBA_32P, "RGBA 32P", vxl_byte,    32,  RGBA,    PLANAR,  0,   0  );
00234 vidl2_pt_mac( RGB_565,  "RGB 565",  vxl_byte,    16,  RGB,     SINGLE,  0,   0  );
00235 vidl2_pt_mac( RGB_555,  "RGB 555",  vxl_byte,    16,  RGB,     SINGLE,  0,   0  );
00236 
00237 vidl2_pt_mac( YUV_444P, "YUV 444P", vxl_byte,    24,  YUV,     PLANAR,  0,   0  );
00238 vidl2_pt_mac( YUV_422P, "YUV 422P", vxl_byte,    16,  YUV,     PLANAR,  1,   0  );
00239 vidl2_pt_mac( YUV_420P, "YUV 420P", vxl_byte,    12,  YUV,     PLANAR,  1,   1  );
00240 vidl2_pt_mac( YVU_420P, "YVU 420P", vxl_byte,    12,  YUV,     PLANAR,  1,   1  );
00241 vidl2_pt_mac( YUV_411P, "YUV 411P", vxl_byte,    12,  YUV,     PLANAR,  2,   0  );
00242 vidl2_pt_mac( YUV_410P, "YUV 410P", vxl_byte,    10,  YUV,     PLANAR,  2,   1  );
00243 vidl2_pt_mac( UYV_444,  "UYV 444",  vxl_byte,    24,  YUV,     SINGLE,  0,   0  );
00244 vidl2_pt_mac( YUYV_422, "YUYV 422", vxl_byte,    16,  YUV,     PACKED,  1,   0  );
00245 vidl2_pt_mac( UYVY_422, "UYVY 422", vxl_byte,    16,  YUV,     PACKED,  1,   0  );
00246 vidl2_pt_mac( UYVY_411, "UYVY 411", vxl_byte,    12,  YUV,     PACKED,  2,   0  );
00247 
00248 vidl2_pt_mac( MONO_1,   "Mono 1",   bool,        1,   MONO,    SINGLE,  0,   0  );
00249 vidl2_pt_mac( MONO_8,   "Mono 8",   vxl_byte,    8,   MONO,    SINGLE,  0,   0  );
00250 vidl2_pt_mac( MONO_16,  "Mono 16",  vxl_uint_16, 16,  MONO,    SINGLE,  0,   0  );
00251 
00252 #undef vidl2_pt_mac
00253 
00254 
00255 //: Define the packing order for each packed vidl2_pixel_format
00256 // The main purpose of this struct is to define a static
00257 // array of pointer offsets to describe the packing.
00258 //
00259 // The array vidl2_pixel_pack_of<format>::offset is a 2D array
00260 // of pointer offsets from the start of the macro pixel.  The
00261 // size of the array is macro-pixel-size by number-of-channels.
00262 // The value offset[i][j] gives the offset to the jth channel
00263 // of the ith pixel in the current macro-pixel.  For example,
00264 // offset[1][0] gives the 'Y' channel (if YUV) or 'R' channel
00265 // (if RGB) of the second pixel in the macro pixel
00266 //
00267 // \note the offset arrays are defined in vidl2_pixel_format.cxx
00268 template <vidl2_pixel_format pix_type>
00269 struct vidl2_pixel_pack_of;
00270 #define vidl2_pp_mac(FMT)\
00271 VCL_DEFINE_SPECIALIZATION \
00272 struct vidl2_pixel_pack_of<VIDL2_PIXEL_FORMAT_##FMT> \
00273 {\
00274   enum { macro_pix_size = 1<<vidl2_pixel_traits_of<VIDL2_PIXEL_FORMAT_##FMT>::chroma_shift_x }; \
00275   enum { num_channels = vidl2_pixel_traits_of<VIDL2_PIXEL_FORMAT_##FMT>::num_channels }; \
00276   static const vcl_ptrdiff_t offset[macro_pix_size][num_channels]; \
00277 }
00278 
00279 vidl2_pp_mac( YUYV_422 );
00280 vidl2_pp_mac( UYVY_422 );
00281 vidl2_pp_mac( UYVY_411 );
00282 
00283 #undef vidl2_pp_mac
00284 
00285 
00286 //=============================================================================
00287 // The following functions provide runtime lookup of pixel traits
00288 // These use template metaprogramming to generate the conditionals to
00289 // check the traits of each vidl2_pixel_format.  You do not need to
00290 // modify the function definitions when adding new types.
00291 
00292 
00293 //: Return the number of channels needed in a color mode
00294 unsigned
00295 vidl2_pixel_color_num_channels(vidl2_pixel_color c);
00296 
00297 
00298 //: Return the set of traits for pixel format f
00299 vidl2_pixel_traits
00300 vidl2_pixel_format_traits(vidl2_pixel_format f);
00301 
00302 
00303 //: Return the effective number of bits per image pixel in pixel format f
00304 inline unsigned
00305 vidl2_pixel_format_bpp(vidl2_pixel_format f)
00306 {
00307   return vidl2_pixel_format_traits(f).bits_per_pixel;
00308 }
00309 
00310 
00311 //: Return the number of color channels encoded in pixel format f
00312 inline unsigned
00313 vidl2_pixel_format_num_channels(vidl2_pixel_format f)
00314 {
00315   return vidl2_pixel_format_traits(f).num_channels;
00316 }
00317 
00318 
00319 //: Return the color encoding for the pixel format
00320 inline vidl2_pixel_color
00321 vidl2_pixel_format_color(vidl2_pixel_format f)
00322 {
00323   return vidl2_pixel_format_traits(f).color;
00324 }
00325 
00326 
00327 //: Return the pixel arrangement for a given format
00328 inline vidl2_pixel_arrangement
00329 vidl2_pixel_format_arrangement(vidl2_pixel_format f)
00330 {
00331   return vidl2_pixel_format_traits(f).arrangement;
00332 }
00333 
00334 
00335 //: Return the chroma shift in the horizontal direction
00336 inline unsigned
00337 vidl2_pixel_format_chroma_shift_x(vidl2_pixel_format f)
00338 {
00339   return vidl2_pixel_format_traits(f).chroma_shift_x;
00340 }
00341 
00342 
00343 //: Return the chroma shift in the vertical direction
00344 inline unsigned
00345 vidl2_pixel_format_chroma_shift_y(vidl2_pixel_format f)
00346 {
00347   return vidl2_pixel_format_traits(f).chroma_shift_y;
00348 }
00349 
00350 
00351 //: Output a pretty string representing the pixel format.
00352 vcl_ostream &
00353 operator << (vcl_ostream &os, vidl2_pixel_format f);
00354 
00355 
00356 //: Convert a string into a pixel format.
00357 inline vcl_string
00358 vidl2_pixel_format_to_string(vidl2_pixel_format f)
00359 {
00360   return vidl2_pixel_format_traits(f).name;
00361 }
00362 
00363 
00364 //: Convert a string into a pixel format.
00365 vidl2_pixel_format
00366 vidl2_pixel_format_from_string(const vcl_string& s);
00367 
00368 
00369 //: Compute the size (in bytes) of a \a ni x \a nj image buffer of pixel format \a f
00370 unsigned
00371 vidl2_pixel_format_buffer_size(unsigned ni, unsigned nj, vidl2_pixel_format f);
00372 
00373 #endif // vidl2_pixel_format_h_

Generated on Thu Jan 10 14:51:31 2008 for contrib/brl/bbas/vidl2 by  doxygen 1.4.4