core/vil/vil_pixel_format.cxx
Go to the documentation of this file.
00001 // This is core/vil/vil_pixel_format.cxx
00002 #include "vil_pixel_format.h"
00003 //:
00004 // \file
00005 // \author Ian Scott.
00006 //
00007 // \verbatim
00008 //  Modifications
00009 //   23 Oct.2003 - Peter Vanroose - Added support for 64-bit int pixels
00010 // \endverbatim
00011 
00012 #include <vcl_cassert.h>
00013 #include <vcl_complex.h>
00014 #include <vcl_string.h>
00015 
00016 static unsigned component_size[] =
00017 {
00018   0,                   //  VIL_PIXEL_FORMAT_UNKNOWN
00019 
00020 #if VXL_HAS_INT_64
00021   sizeof(vxl_uint_64), //  VIL_PIXEL_FORMAT_UINT_64
00022   sizeof(vxl_int_64),  //  VIL_PIXEL_FORMAT_INT_64
00023 #else
00024   0,                   //  VIL_PIXEL_FORMAT_UINT_64
00025   0,                   //  VIL_PIXEL_FORMAT_INT_64
00026 #endif
00027   sizeof(vxl_uint_32), //  VIL_PIXEL_FORMAT_UINT_32
00028   sizeof(vxl_int_32),  //  VIL_PIXEL_FORMAT_INT_32
00029   sizeof(vxl_uint_16), //  VIL_PIXEL_FORMAT_UINT_16
00030   sizeof(vxl_int_16),  //  VIL_PIXEL_FORMAT_INT_16
00031   sizeof(vxl_byte),    //  VIL_PIXEL_FORMAT_BYTE
00032   sizeof(vxl_sbyte),   //  VIL_PIXEL_FORMAT_SBYTE
00033   sizeof(float),       //  VIL_PIXEL_FORMAT_FLOAT
00034   sizeof(double),      //  VIL_PIXEL_FORMAT_DOUBLE
00035   0,                   //  VIL_PIXEL_FORMAT_LONG_DOUBLE
00036   sizeof(bool),        //  VIL_PIXEL_FORMAT_BOOL
00037 
00038 #if VXL_HAS_INT_64
00039   sizeof(vxl_uint_64), //  VIL_PIXEL_FORMAT_RGB_UINT_64
00040   sizeof(vxl_int_64),  //  VIL_PIXEL_FORMAT_RGB_INT_64
00041 #else
00042   0,                   //  VIL_PIXEL_FORMAT_RGB_UINT_64
00043   0,                   //  VIL_PIXEL_FORMAT_RGB_INT_64
00044 #endif
00045   sizeof(vxl_uint_32), //  VIL_PIXEL_FORMAT_RGB_UINT_32
00046   sizeof(vxl_int_32),  //  VIL_PIXEL_FORMAT_RGB_INT_32
00047   sizeof(vxl_uint_16), //  VIL_PIXEL_FORMAT_RGB_UINT_16
00048   sizeof(vxl_int_16),  //  VIL_PIXEL_FORMAT_RGB_INT_16
00049   sizeof(vxl_byte),    //  VIL_PIXEL_FORMAT_RGB_BYTE
00050   sizeof(vxl_sbyte),   //  VIL_PIXEL_FORMAT_RGB_SBYTE
00051   sizeof(float),       //  VIL_PIXEL_FORMAT_RGB_FLOAT
00052   sizeof(double),      //  VIL_PIXEL_FORMAT_RGB_DOUBLE
00053   0,                   //  VIL_PIXEL_FORMAT_RGB_LONG_DOUBLE
00054 
00055 #if VXL_HAS_INT_64
00056   sizeof(vxl_uint_64), //  VIL_PIXEL_FORMAT_RGBA_UINT_64
00057   sizeof(vxl_int_64),  //  VIL_PIXEL_FORMAT_RGBA_INT_64
00058 #else
00059   0,                   //  VIL_PIXEL_FORMAT_RGBA_UINT_64
00060   0,                   //  VIL_PIXEL_FORMAT_RGBA_INT_64
00061 #endif
00062   sizeof(vxl_uint_32), //  VIL_PIXEL_FORMAT_RGBA_UINT_32
00063   sizeof(vxl_int_32),  //  VIL_PIXEL_FORMAT_RGBA_INT_32
00064   sizeof(vxl_uint_16), //  VIL_PIXEL_FORMAT_RGBA_UINT_16
00065   sizeof(vxl_int_16),  //  VIL_PIXEL_FORMAT_RGBA_INT_16
00066   sizeof(vxl_byte),    //  VIL_PIXEL_FORMAT_RGBA_BYTE
00067   sizeof(vxl_sbyte),   //  VIL_PIXEL_FORMAT_RGBA_SBYTE
00068   sizeof(float),       //  VIL_PIXEL_FORMAT_RGBA_FLOAT
00069   0,                   //  VIL_PIXEL_FORMAT_RGBA_LONG_DOUBLE
00070 
00071   sizeof(vcl_complex<float>),   // VIL_PIXEL_FORMAT_COMPLEX_FLOAT
00072   sizeof(vcl_complex<double>),  // VIL_PIXEL_FORMAT_COMPLEX_DOUBLE
00073 };
00074 
00075 
00076 static unsigned num_components[] =
00077 {
00078   0,  //  VIL_PIXEL_FORMAT_UNKNOWN
00079 
00080 #if VXL_HAS_INT_64
00081   1,  //  VIL_PIXEL_FORMAT_UINT_64
00082   1,  //  VIL_PIXEL_FORMAT_INT_64
00083 #else
00084   0,  //  VIL_PIXEL_FORMAT_UINT_64
00085   0,  //  VIL_PIXEL_FORMAT_INT_64
00086 #endif
00087   1,  //  VIL_PIXEL_FORMAT_UINT_32
00088   1,  //  VIL_PIXEL_FORMAT_INT_32
00089   1,  //  VIL_PIXEL_FORMAT_UINT_16
00090   1,  //  VIL_PIXEL_FORMAT_INT_16
00091   1,  //  VIL_PIXEL_FORMAT_BYTE
00092   1,  //  VIL_PIXEL_FORMAT_SBYTE
00093   1,  //  VIL_PIXEL_FORMAT_FLOAT
00094   1,  //  VIL_PIXEL_FORMAT_DOUBLE
00095   0,  //  VIL_PIXEL_FORMAT_LONG_DOUBLE
00096   1,  //  VIL_PIXEL_FORMAT_BOOL
00097 
00098 #if VXL_HAS_INT_64
00099   3,  //  VIL_PIXEL_FORMAT_RGB_UINT_64
00100   3,  //  VIL_PIXEL_FORMAT_RGB_INT_64
00101 #else
00102   0,  //  VIL_PIXEL_FORMAT_RGB_UINT_64
00103   0,  //  VIL_PIXEL_FORMAT_RGB_INT_64
00104 #endif
00105   3,  //  VIL_PIXEL_FORMAT_RGB_UINT_32
00106   3,  //  VIL_PIXEL_FORMAT_RGB_INT_32
00107   3,  //  VIL_PIXEL_FORMAT_RGB_UINT_16
00108   3,  //  VIL_PIXEL_FORMAT_RGB_INT_16
00109   3,  //  VIL_PIXEL_FORMAT_RGB_BYTE
00110   3,  //  VIL_PIXEL_FORMAT_RGB_SBYTE
00111   3,  //  VIL_PIXEL_FORMAT_RGB_FLOAT
00112   3,  //  VIL_PIXEL_FORMAT_RGB_DOUBLE
00113   0,  //  VIL_PIXEL_FORMAT_RGB_LONG_DOUBLE
00114 
00115 #if VXL_HAS_INT_64
00116   4,  //  VIL_PIXEL_FORMAT_RGBA_UINT_64
00117   4,  //  VIL_PIXEL_FORMAT_RGBA_INT_64
00118 #else
00119   0,  //  VIL_PIXEL_FORMAT_RGBA_UINT_64
00120   0,  //  VIL_PIXEL_FORMAT_RGBA_INT_64
00121 #endif
00122   4,  //  VIL_PIXEL_FORMAT_RGBA_UINT_32
00123   4,  //  VIL_PIXEL_FORMAT_RGBA_INT_32
00124   4,  //  VIL_PIXEL_FORMAT_RGBA_UINT_16
00125   4,  //  VIL_PIXEL_FORMAT_RGBA_INT_16
00126   4,  //  VIL_PIXEL_FORMAT_RGBA_BYTE
00127   4,  //  VIL_PIXEL_FORMAT_RGBA_SBYTE
00128   4,  //  VIL_PIXEL_FORMAT_RGBA_FLOAT
00129   4,  //  VIL_PIXEL_FORMAT_RGBA_DOUBLE
00130   0,  //  VIL_PIXEL_FORMAT_RGBA_LONG_DOUBLE
00131 
00132   1,  //  VIL_PIXEL_FORMAT_COMPLEX_FLOAT
00133   1,  //  VIL_PIXEL_FORMAT_COMPLEX_DOUBLE
00134 };
00135 
00136 
00137 static vil_pixel_format component_format[] =
00138 {
00139   VIL_PIXEL_FORMAT_UNKNOWN,  //  VIL_PIXEL_FORMAT_UNKNOWN
00140 
00141 #if VXL_HAS_INT_64
00142   VIL_PIXEL_FORMAT_UINT_64,  //  VIL_PIXEL_FORMAT_UINT_64
00143   VIL_PIXEL_FORMAT_INT_64,   //  VIL_PIXEL_FORMAT_INT_64
00144 #else
00145   VIL_PIXEL_FORMAT_UNKNOWN,  //  VIL_PIXEL_FORMAT_UINT_64
00146   VIL_PIXEL_FORMAT_UNKNOWN,  //  VIL_PIXEL_FORMAT_INT_64
00147 #endif
00148   VIL_PIXEL_FORMAT_UINT_32,  //  VIL_PIXEL_FORMAT_UINT_32
00149   VIL_PIXEL_FORMAT_INT_32,   //  VIL_PIXEL_FORMAT_INT_32
00150   VIL_PIXEL_FORMAT_UINT_16,  //  VIL_PIXEL_FORMAT_UINT_16
00151   VIL_PIXEL_FORMAT_INT_16,   //  VIL_PIXEL_FORMAT_INT_16
00152   VIL_PIXEL_FORMAT_BYTE,     //  VIL_PIXEL_FORMAT_BYTE
00153   VIL_PIXEL_FORMAT_SBYTE,    //  VIL_PIXEL_FORMAT_SBYTE
00154   VIL_PIXEL_FORMAT_FLOAT,    //  VIL_PIXEL_FORMAT_FLOAT
00155   VIL_PIXEL_FORMAT_DOUBLE,   //  VIL_PIXEL_FORMAT_DOUBLE
00156   VIL_PIXEL_FORMAT_UNKNOWN,  //  VIL_PIXEL_FORMAT_LONG_DOUBLE
00157   VIL_PIXEL_FORMAT_BOOL,     //  VIL_PIXEL_FORMAT_BOOL
00158 
00159 #if VXL_HAS_INT_64
00160   VIL_PIXEL_FORMAT_UINT_64,  //  VIL_PIXEL_FORMAT_RGB_UINT_64
00161   VIL_PIXEL_FORMAT_INT_64,   //  VIL_PIXEL_FORMAT_RGB_INT_64
00162 #else
00163   VIL_PIXEL_FORMAT_UNKNOWN,  //  VIL_PIXEL_FORMAT_RGB_UINT_64
00164   VIL_PIXEL_FORMAT_UNKNOWN,  //  VIL_PIXEL_FORMAT_RGB_INT_64
00165 #endif
00166   VIL_PIXEL_FORMAT_UINT_32,  //  VIL_PIXEL_FORMAT_RGB_UINT_32
00167   VIL_PIXEL_FORMAT_INT_32,   //  VIL_PIXEL_FORMAT_RGB_INT_32
00168   VIL_PIXEL_FORMAT_UINT_16,  //  VIL_PIXEL_FORMAT_RGB_UINT_16
00169   VIL_PIXEL_FORMAT_INT_16,   //  VIL_PIXEL_FORMAT_RGB_INT_16
00170   VIL_PIXEL_FORMAT_BYTE,     //  VIL_PIXEL_FORMAT_RGB_BYTE
00171   VIL_PIXEL_FORMAT_SBYTE,    //  VIL_PIXEL_FORMAT_RGB_SBYTE
00172   VIL_PIXEL_FORMAT_FLOAT,    //  VIL_PIXEL_FORMAT_RGB_FLOAT
00173   VIL_PIXEL_FORMAT_DOUBLE,   //  VIL_PIXEL_FORMAT_RGB_DOUBLE
00174   VIL_PIXEL_FORMAT_UNKNOWN,  //  VIL_PIXEL_FORMAT_RGB_LONG_DOUBLE
00175 
00176 #if VXL_HAS_INT_64
00177   VIL_PIXEL_FORMAT_UINT_64,  //  VIL_PIXEL_FORMAT_RGBA_UINT_64
00178   VIL_PIXEL_FORMAT_INT_64,   //  VIL_PIXEL_FORMAT_RGBA_INT_64
00179 #else
00180   VIL_PIXEL_FORMAT_UNKNOWN,  //  VIL_PIXEL_FORMAT_RGBA_UINT_64
00181   VIL_PIXEL_FORMAT_UNKNOWN,  //  VIL_PIXEL_FORMAT_RGBA_INT_64
00182 #endif
00183   VIL_PIXEL_FORMAT_UINT_32,  //  VIL_PIXEL_FORMAT_RGBA_UINT_32
00184   VIL_PIXEL_FORMAT_INT_32,   //  VIL_PIXEL_FORMAT_RGBA_INT_32
00185   VIL_PIXEL_FORMAT_UINT_16,  //  VIL_PIXEL_FORMAT_RGBA_UINT_16
00186   VIL_PIXEL_FORMAT_INT_16,   //  VIL_PIXEL_FORMAT_RGBA_INT_16
00187   VIL_PIXEL_FORMAT_BYTE,     //  VIL_PIXEL_FORMAT_RGBA_BYTE
00188   VIL_PIXEL_FORMAT_SBYTE,    //  VIL_PIXEL_FORMAT_RGBA_SBYTE
00189   VIL_PIXEL_FORMAT_FLOAT,    //  VIL_PIXEL_FORMAT_RGBA_FLOAT
00190   VIL_PIXEL_FORMAT_DOUBLE,   //  VIL_PIXEL_FORMAT_RGBA_DOUBLE
00191   VIL_PIXEL_FORMAT_UNKNOWN,  //  VIL_PIXEL_FORMAT_RGBA_LONG_DOUBLE
00192 
00193   VIL_PIXEL_FORMAT_COMPLEX_FLOAT, //  VIL_PIXEL_FORMAT_COMPLEX_FLOAT
00194   VIL_PIXEL_FORMAT_COMPLEX_DOUBLE,//  VIL_PIXEL_FORMAT_COMPLEX_DOUBLE
00195 };
00196 
00197 
00198 //: Return the number of bytes used by each component of pixel format f
00199 unsigned vil_pixel_format_sizeof_components(enum vil_pixel_format f)
00200 {
00201   assert (f >= VIL_PIXEL_FORMAT_UNKNOWN && f < VIL_PIXEL_FORMAT_ENUM_END);
00202   return component_size[f];
00203 }
00204 
00205 //: Return the number of components in pixel format f
00206 unsigned vil_pixel_format_num_components(enum vil_pixel_format f)
00207 {
00208   assert (f >= VIL_PIXEL_FORMAT_UNKNOWN && f < VIL_PIXEL_FORMAT_ENUM_END);
00209   return num_components[f];
00210 }
00211 
00212 
00213 //: Return the number of components in pixel format f
00214 vil_pixel_format vil_pixel_format_component_format(enum vil_pixel_format f)
00215 {
00216   assert (f >= VIL_PIXEL_FORMAT_UNKNOWN && f < VIL_PIXEL_FORMAT_ENUM_END);
00217   return component_format[f];
00218 }
00219 
00220 
00221 //: output a pretty string representing the pixel format.
00222 vcl_ostream & operator << (vcl_ostream &os, vil_pixel_format f)
00223 {
00224   switch (f)
00225   {
00226     case VIL_PIXEL_FORMAT_UNKNOWN: os << "VIL_PIXEL_FORMAT_UNKNOWN";  break;
00227 
00228 #if VXL_HAS_INT_64
00229     case VIL_PIXEL_FORMAT_UINT_64: os << "vxl_uint_64";  break;
00230     case VIL_PIXEL_FORMAT_INT_64:  os << "vxl_int_64";  break;
00231 #endif
00232     case VIL_PIXEL_FORMAT_UINT_32: os << "vxl_uint_32";  break;
00233     case VIL_PIXEL_FORMAT_INT_32:  os << "vxl_int_32";  break;
00234     case VIL_PIXEL_FORMAT_UINT_16: os << "vxl_uint_16";  break;
00235     case VIL_PIXEL_FORMAT_INT_16:  os << "vxl_int_16";  break;
00236     case VIL_PIXEL_FORMAT_BYTE:    os << "vxl_byte";  break;
00237     case VIL_PIXEL_FORMAT_SBYTE:   os << "vxl_sbyte";  break;
00238     case VIL_PIXEL_FORMAT_FLOAT:   os << "float";  break;
00239     case VIL_PIXEL_FORMAT_DOUBLE:  os << "double";  break;
00240     case VIL_PIXEL_FORMAT_BOOL:    os << "bool";  break;
00241 
00242 #if VXL_HAS_INT_64
00243     case VIL_PIXEL_FORMAT_RGB_INT_64:  os << "vil_rgb<vxl_int_64>";  break;
00244     case VIL_PIXEL_FORMAT_RGB_UINT_64: os << "vil_rgb<vxl_uint_64>";  break;
00245 #endif
00246     case VIL_PIXEL_FORMAT_RGB_INT_32:  os << "vil_rgb<vxl_int_32>";  break;
00247     case VIL_PIXEL_FORMAT_RGB_UINT_32: os << "vil_rgb<vxl_uint_32>";  break;
00248     case VIL_PIXEL_FORMAT_RGB_INT_16:  os << "vil_rgb<vxl_int_16>";  break;
00249     case VIL_PIXEL_FORMAT_RGB_UINT_16: os << "vil_rgb<vxl_uint_16>";  break;
00250     case VIL_PIXEL_FORMAT_RGB_BYTE:    os << "vil_rgb<vxl_byte>";  break;
00251     case VIL_PIXEL_FORMAT_RGB_SBYTE:   os << "vil_rgb<vxl_sbyte>";  break;
00252     case VIL_PIXEL_FORMAT_RGB_FLOAT:   os << "vil_rgb<float>";  break;
00253     case VIL_PIXEL_FORMAT_RGB_DOUBLE:  os << "vil_rgb<double>";  break;
00254 
00255 #if VXL_HAS_INT_64
00256     case VIL_PIXEL_FORMAT_RGBA_INT_64:  os << "vil_rgba<vxl_int_64>";  break;
00257     case VIL_PIXEL_FORMAT_RGBA_UINT_64: os << "vil_rgba<vxl_uint_64>";  break;
00258 #endif
00259     case VIL_PIXEL_FORMAT_RGBA_INT_32:  os << "vil_rgba<vxl_int_32>";  break;
00260     case VIL_PIXEL_FORMAT_RGBA_UINT_32: os << "vil_rgba<vxl_uint_32>";  break;
00261     case VIL_PIXEL_FORMAT_RGBA_INT_16:  os << "vil_rgba<vxl_int_16>";  break;
00262     case VIL_PIXEL_FORMAT_RGBA_UINT_16: os << "vil_rgba<vxl_uint_16>";  break;
00263     case VIL_PIXEL_FORMAT_RGBA_BYTE:    os << "vil_rgba<vxl_byte>";  break;
00264     case VIL_PIXEL_FORMAT_RGBA_SBYTE:   os << "vil_rgba<vxl_sbyte>";  break;
00265     case VIL_PIXEL_FORMAT_RGBA_FLOAT:   os << "vil_rgba<float>";  break;
00266     case VIL_PIXEL_FORMAT_RGBA_DOUBLE:  os << "vil_rgba<double>";  break;
00267 
00268     case VIL_PIXEL_FORMAT_COMPLEX_FLOAT:   os << "complex<float>";  break;
00269     case VIL_PIXEL_FORMAT_COMPLEX_DOUBLE:  os << "complex<double>";  break;
00270 
00271     default:  os << "VIL_PIXEL_FORMAT_INVALID";  break;
00272   }
00273   return os;
00274 }
00275 
00276 
00277 //: Convert a string into a pixel format.
00278 // This uses the same encoding as operator<<.
00279 vil_pixel_format vil_pixel_format_from_string(const char * s)
00280 {
00281   vcl_string str(s);
00282   if (str == "VIL_PIXEL_FORMAT_UNKNOWN") return VIL_PIXEL_FORMAT_UNKNOWN;
00283 #if VXL_HAS_INT_64
00284   if (str == "vxl_uint_64") return VIL_PIXEL_FORMAT_UINT_64;
00285   if (str == "vxl_int_64") return VIL_PIXEL_FORMAT_INT_64;
00286 #endif
00287   if (str == "vxl_uint_32") return VIL_PIXEL_FORMAT_UINT_32;
00288   if (str == "vxl_int_32") return VIL_PIXEL_FORMAT_INT_32;
00289   if (str == "vxl_uint_16") return VIL_PIXEL_FORMAT_UINT_16;
00290   if (str == "vxl_int_16") return VIL_PIXEL_FORMAT_INT_16;
00291   if (str == "vxl_byte") return VIL_PIXEL_FORMAT_BYTE;
00292   if (str == "vxl_sbyte") return VIL_PIXEL_FORMAT_SBYTE;
00293   if (str == "float") return VIL_PIXEL_FORMAT_FLOAT;
00294   if (str == "double") return VIL_PIXEL_FORMAT_DOUBLE;
00295   if (str == "bool") return VIL_PIXEL_FORMAT_BOOL;
00296 
00297 #if VXL_HAS_INT_64
00298   if (str == "vil_rgb<vxl_int_64>") return VIL_PIXEL_FORMAT_RGB_INT_64;
00299   if (str == "vil_rgb<vxl_uint_64>") return VIL_PIXEL_FORMAT_RGB_UINT_64;
00300 #endif
00301   if (str == "vil_rgb<vxl_int_32>") return VIL_PIXEL_FORMAT_RGB_INT_32;
00302   if (str == "vil_rgb<vxl_uint_32>") return VIL_PIXEL_FORMAT_RGB_UINT_32;
00303   if (str == "vil_rgb<vxl_int_16>") return VIL_PIXEL_FORMAT_RGB_INT_16;
00304   if (str == "vil_rgb<vxl_uint_16>") return VIL_PIXEL_FORMAT_RGB_UINT_16;
00305   if (str == "vil_rgb<vxl_byte>") return VIL_PIXEL_FORMAT_RGB_BYTE;
00306   if (str == "vil_rgb<vxl_sbyte>") return VIL_PIXEL_FORMAT_RGB_SBYTE;
00307   if (str == "vil_rgb<float>") return VIL_PIXEL_FORMAT_RGB_FLOAT;
00308   if (str == "vil_rgb<double>") return VIL_PIXEL_FORMAT_RGB_DOUBLE;
00309 
00310 #if VXL_HAS_INT_64
00311   if (str == "vil_rgba<vxl_int_64>") return VIL_PIXEL_FORMAT_RGBA_INT_64;
00312   if (str == "vil_rgba<vxl_uint_64>") return VIL_PIXEL_FORMAT_RGBA_UINT_64;
00313 #endif
00314   if (str == "vil_rgba<vxl_int_32>") return VIL_PIXEL_FORMAT_RGBA_INT_32;
00315   if (str == "vil_rgba<vxl_uint_32>") return VIL_PIXEL_FORMAT_RGBA_UINT_32;
00316   if (str == "vil_rgba<vxl_int_16>") return VIL_PIXEL_FORMAT_RGBA_INT_16;
00317   if (str == "vil_rgba<vxl_uint_16>") return VIL_PIXEL_FORMAT_RGBA_UINT_16;
00318   if (str == "vil_rgba<vxl_byte>") return VIL_PIXEL_FORMAT_RGBA_BYTE;
00319   if (str == "vil_rgba<vxl_sbyte>") return VIL_PIXEL_FORMAT_RGBA_SBYTE;
00320   if (str == "vil_rgba<float>") return VIL_PIXEL_FORMAT_RGBA_FLOAT;
00321   if (str == "vil_rgba<double>") return VIL_PIXEL_FORMAT_RGBA_DOUBLE;
00322 
00323   if (str == "complex<float>") return VIL_PIXEL_FORMAT_COMPLEX_FLOAT;
00324   if (str == "complex<double>") return VIL_PIXEL_FORMAT_COMPLEX_DOUBLE;
00325 
00326   return VIL_PIXEL_FORMAT_UNKNOWN;
00327 }