core/vil/vil_print.cxx
Go to the documentation of this file.
00001 // This is core/vil/vil_print.cxx
00002 #include "vil_print.h"
00003 //:
00004 // \file
00005 // \brief Various functions for manipulating image views
00006 // \author Tim Cootes - Manchester
00007 //
00008 // \verbatim
00009 //  Modifications
00010 //   23 Oct.2003 - Peter Vanroose - Added support for 64-bit int pixels
00011 // \endverbatim
00012 
00013 #include <vxl_config.h> // for vxl_uint_32 etc.
00014 #include <vcl_complex.h>
00015 #include <vil/vil_rgb.h>
00016 #include <vil/vil_rgba.h>
00017 
00018 //: Explicit overload for bool
00019 VCL_DEFINE_SPECIALIZATION
00020 void vil_print_value(vcl_ostream& os, const bool& value, unsigned)
00021 {
00022   os<<int(value);
00023 }
00024 
00025 //: Explicit overload for byte
00026 VCL_DEFINE_SPECIALIZATION
00027 void vil_print_value(vcl_ostream& os, const vxl_byte& value, unsigned)
00028 {
00029   os.width(3);
00030   os<<int(value);
00031 }
00032 
00033 //: Explicit overload for signed byte
00034 VCL_DEFINE_SPECIALIZATION
00035 void vil_print_value(vcl_ostream& os, const vxl_sbyte& value, unsigned)
00036 {
00037   os.width(3);
00038   os<<int(value);
00039 }
00040 
00041 //: Explicit overload for short
00042 VCL_DEFINE_SPECIALIZATION
00043 void vil_print_value(vcl_ostream& os, const vxl_int_16& value, unsigned width/*=0*/)
00044 {
00045   if (width==0) width=5;
00046   int v=value;
00047   if (v<0) { v=-v; os<<'-'; } else os<<' ';
00048   if (v<10 && width > 1)    os<<'0';
00049   if (v<100 && width > 2)   os<<'0';
00050   if (v<1000 && width > 3)  os<<'0';
00051   if (v<10000 && width > 4) os<<'0';
00052   os<<v;
00053 }
00054 
00055 //: Explicit overload for unsigned short
00056 VCL_DEFINE_SPECIALIZATION
00057 void vil_print_value(vcl_ostream& os, const vxl_uint_16& value, unsigned width/*=0*/)
00058 {
00059   if (width==0) width=5;
00060   if (value<10 && width > 1)    os<<'0';
00061   if (value<100 && width > 2)   os<<'0';
00062   if (value<1000 && width > 3)  os<<'0';
00063   if (value<10000 && width > 4) os<<'0';
00064   os<<value;
00065 }
00066 
00067 //: Explicit overload for int
00068 VCL_DEFINE_SPECIALIZATION
00069 void vil_print_value(vcl_ostream& os, const vxl_int_32& value, unsigned width/*=0*/)
00070 {
00071   if (width==0) width=8;
00072   int v=value;
00073   if (v<0) { v=-v; os<<'-'; } else os<<' ';
00074   if (v<10 && width > 1)       os<<'0';
00075   if (v<100 && width > 2)      os<<'0';
00076   if (v<1000 && width > 3)     os<<'0';
00077   if (v<10000 && width > 4)    os<<'0';
00078   if (v<100000 && width > 5)   os<<'0';
00079   if (v<1000000 && width > 6)  os<<'0';
00080   if (v<10000000 && width > 7) os<<'0';
00081   os<<v;
00082 }
00083 
00084 //: Explicit overload for unsigned int
00085 VCL_DEFINE_SPECIALIZATION
00086 void vil_print_value(vcl_ostream& os, const vxl_uint_32& value, unsigned width/*=0*/)
00087 {
00088   if (width==0) width=8;
00089   if (value<10 && width > 1)       os<<'0';
00090   if (value<100 && width > 2)      os<<'0';
00091   if (value<1000 && width > 3)     os<<'0';
00092   if (value<10000 && width > 4)    os<<'0';
00093   if (value<100000 && width > 5)   os<<'0';
00094   if (value<1000000 && width > 6)  os<<'0';
00095   if (value<10000000 && width > 7) os<<'0';
00096   os<<value;
00097 }
00098 
00099 #if VXL_HAS_INT_64
00100 
00101 //: Explicit overload for unsigned long
00102 VCL_DEFINE_SPECIALIZATION
00103 void vil_print_value(vcl_ostream& os, const vxl_uint_64& value, unsigned width/*=0*/)
00104 {
00105   if (width==0) width=8;
00106 #ifdef VCL_VC_6 // IMS. This Hack could be replaced by code which
00107   os << "****"; // splits the 64bit int and does the right thing.
00108 #else
00109   if (value<10 && width > 1)       os<<'0';
00110   if (value<100 && width > 2)      os<<'0';
00111   if (value<1000 && width > 3)     os<<'0';
00112   if (value<10000 && width > 4)    os<<'0';
00113   if (value<100000 && width > 5)   os<<'0';
00114   if (value<1000000 && width > 6)  os<<'0';
00115   if (value<10000000 && width > 7) os<<'0';
00116   os<<value;
00117 #endif
00118 }
00119 
00120 //: Explicit overload for long
00121 VCL_DEFINE_SPECIALIZATION
00122 void vil_print_value(vcl_ostream& os, const vxl_int_64& value, unsigned width/*=0*/)
00123 {
00124   if (width==0) width=8;
00125 #ifdef VCL_VC_6 // IMS. This Hack could be replaced by code which
00126   os << "****"; // splits the 64bit int and does the right thing.
00127 #else
00128   vxl_int_64 v=value;
00129   if (v<0) { v=-v; os<<'-'; } else os<<' ';
00130   if (v<10 && width > 1)       os<<'0';
00131   if (v<100 && width > 2)      os<<'0';
00132   if (v<1000 && width > 3)     os<<'0';
00133   if (v<10000 && width > 4)    os<<'0';
00134   if (v<100000 && width > 5)   os<<'0';
00135   if (v<1000000 && width > 6)  os<<'0';
00136   if (v<10000000 && width > 7) os<<'0';
00137   os<<v;
00138 #endif
00139 }
00140 
00141 #endif
00142 
00143 //: Explicit overload for float
00144 VCL_DEFINE_SPECIALIZATION
00145 void vil_print_value(vcl_ostream& os, const float& value, unsigned)
00146 {
00147   os<<value;
00148 }
00149 
00150 //: Explicit overload for double
00151 VCL_DEFINE_SPECIALIZATION
00152 void vil_print_value(vcl_ostream& os, const double& value, unsigned)
00153 {
00154   os<<value;
00155 }
00156 
00157 //: Explicit overload for complex float
00158 VCL_DEFINE_SPECIALIZATION
00159 void vil_print_value(vcl_ostream& os, const vcl_complex<float>& value, unsigned)
00160 {
00161   os<<value;
00162 }
00163 
00164 //: Explicit overload for complex double
00165 VCL_DEFINE_SPECIALIZATION
00166 void vil_print_value(vcl_ostream& os, const vcl_complex<double>& value, unsigned)
00167 {
00168   os<<value;
00169 }
00170 
00171 //: Explicit overload of print for rgb<byte>
00172 VCL_DEFINE_SPECIALIZATION
00173 void vil_print_value(vcl_ostream& os, const vil_rgb<vxl_byte>& value, unsigned)
00174 {
00175   int r = int(value.r);
00176   if (r<10)  os<<'0';
00177   if (r<100) os<<'0';
00178   os<<r<<'/';
00179   int g = int(value.g);
00180   if (g<10)  os<<'0';
00181   if (g<100) os<<'0';
00182   os<<g<<'/';
00183   int b = int(value.b);
00184   if (b<10)  os<<'0';
00185   if (b<100) os<<'0';
00186   os<<b;
00187 }
00188 
00189 //: Explicit overload of print for rgb<sbyte>
00190 VCL_DEFINE_SPECIALIZATION
00191 void vil_print_value(vcl_ostream& os, vil_rgb<vxl_sbyte> const& value, unsigned)
00192 {
00193   int r = int(value.r);
00194   if (r<0) r=-r,os<<'-'; else os<<'+';
00195   if (r<10)  os<<'0';
00196   if (r<100) os<<'0';
00197   os<<r<<'/';
00198   int g = int(value.g);
00199   if (g<0) g=-g,os<<'-'; else os<<'+';
00200   if (g<10)  os<<'0';
00201   if (g<100) os<<'0';
00202   os<<g<<'/';
00203   int b = int(value.b);
00204   if (b<0) b=-b,os<<'-'; else os<<'+';
00205   if (b<10)  os<<'0';
00206   if (b<100) os<<'0';
00207   os<<b;
00208 }
00209 
00210 //: Explicit overload of print for rgb<short>
00211 VCL_DEFINE_SPECIALIZATION
00212 void vil_print_value(vcl_ostream& os, const vil_rgb<vxl_int_16>& value, unsigned width)
00213 {
00214   vil_print_value(os, value.r, width);
00215   os<<'/';
00216   vil_print_value(os, value.g, width);
00217   os<<'/';
00218   vil_print_value(os, value.b, width);
00219 }
00220 
00221 //: Explicit overload of print for rgb<unsigned short>
00222 VCL_DEFINE_SPECIALIZATION
00223 void vil_print_value(vcl_ostream& os, const vil_rgb<vxl_uint_16>& value, unsigned width)
00224 {
00225   vil_print_value(os, value.r, width);
00226   os<<'/';
00227   vil_print_value(os, value.g, width);
00228   os<<'/';
00229   vil_print_value(os, value.b, width);
00230 }
00231 
00232 //: Explicit overload of print for rgb<int>
00233 VCL_DEFINE_SPECIALIZATION
00234 void vil_print_value(vcl_ostream& os, const vil_rgb<vxl_int_32>& value, unsigned width)
00235 {
00236   vil_print_value(os, value.r, width);
00237   os<<'/';
00238   vil_print_value(os, value.g, width);
00239   os<<'/';
00240   vil_print_value(os, value.b, width);
00241 }
00242 
00243 //: Explicit overload of print for rgb<unsigned int>
00244 VCL_DEFINE_SPECIALIZATION
00245 void vil_print_value(vcl_ostream& os, const vil_rgb<vxl_uint_32>& value, unsigned width)
00246 {
00247   vil_print_value(os, value.r, width);
00248   os<<'/';
00249   vil_print_value(os, value.g, width);
00250   os<<'/';
00251   vil_print_value(os, value.b, width);
00252 }
00253 
00254 #if VXL_HAS_INT_64
00255 
00256 //: Explicit overload of print for rgb<long>
00257 VCL_DEFINE_SPECIALIZATION
00258 void vil_print_value(vcl_ostream& os, const vil_rgb<vxl_int_64>& value, unsigned width)
00259 {
00260   vil_print_value(os, value.r, width);
00261   os<<'/';
00262   vil_print_value(os, value.g, width);
00263   os<<'/';
00264   vil_print_value(os, value.b, width);
00265 }
00266 
00267 //: Explicit overload of print for rgb<unsigned long>
00268 VCL_DEFINE_SPECIALIZATION
00269 void vil_print_value(vcl_ostream& os, const vil_rgb<vxl_uint_64>& value, unsigned width)
00270 {
00271   vil_print_value(os, value.r, width);
00272   os<<'/';
00273   vil_print_value(os, value.g, width);
00274   os<<'/';
00275   vil_print_value(os, value.b, width);
00276 }
00277 
00278 #endif
00279 
00280 //: Explicit overload of print for rgb<float>
00281 VCL_DEFINE_SPECIALIZATION
00282 void vil_print_value(vcl_ostream& os, const vil_rgb<float>& value, unsigned)
00283 {
00284   os<<value.r<<'/'<<value.g<<'/'<<value.b;
00285 }
00286 
00287 
00288 //: Explicit overload of print for rgb<double>
00289 VCL_DEFINE_SPECIALIZATION
00290 void vil_print_value(vcl_ostream& os, const vil_rgb<double>& value, unsigned)
00291 {
00292   os<<value.r<<'/'<<value.g<<'/'<<value.b;
00293 }
00294 
00295 //: Explicit overload of print for rgba<byte>
00296 VCL_DEFINE_SPECIALIZATION
00297 void vil_print_value(vcl_ostream& os, const vil_rgba<vxl_byte>& value, unsigned)
00298 {
00299   int r = int(value.r);
00300   if (r<10)  os<<'0';
00301   if (r<100) os<<'0';
00302   os<<r<<'/';
00303   int g = int(value.g);
00304   if (g<10)  os<<'0';
00305   if (g<100) os<<'0';
00306   os<<g<<'/';
00307   int b = int(value.b);
00308   if (b<10)  os<<'0';
00309   if (b<100) os<<'0';
00310   os<<b<<'/';
00311   int a = int(value.a);
00312   if (a<10)  os<<'0';
00313   if (a<100) os<<'0';
00314   os<<a;
00315 }
00316 
00317 //: Explicit overload of print for rgba<sbyte>
00318 VCL_DEFINE_SPECIALIZATION
00319 void vil_print_value(vcl_ostream& os, const vil_rgba<vxl_sbyte>& value, unsigned)
00320 {
00321   int r = int(value.r);
00322   if (r<0) r=-r,os<<'-'; else os<<'+';
00323   if (r<10)  os<<'0';
00324   if (r<100) os<<'0';
00325   os<<r<<'/';
00326   int g = int(value.g);
00327   if (g<0) g=-g,os<<'-'; else os<<'+';
00328   if (g<10)  os<<'0';
00329   if (g<100) os<<'0';
00330   os<<g<<'/';
00331   int b = int(value.b);
00332   if (b<0) b=-b,os<<'-'; else os<<'+';
00333   if (b<10)  os<<'0';
00334   if (b<100) os<<'0';
00335   os<<b<<'/';
00336   int a = int(value.a);
00337   if (a<0) a=-a,os<<'-'; else os<<'+';
00338   if (a<10)  os<<'0';
00339   if (a<100) os<<'0';
00340   os<<a;
00341 }
00342 
00343 //: Explicit overload of print for rgba<short>
00344 VCL_DEFINE_SPECIALIZATION
00345 void vil_print_value(vcl_ostream& os, const vil_rgba<vxl_int_16>& value, unsigned width)
00346 {
00347   vil_print_value(os, value.r, width);
00348   os<<'/';
00349   vil_print_value(os, value.g, width);
00350   os<<'/';
00351   vil_print_value(os, value.b, width);
00352   os<<'/';
00353   vil_print_value(os, value.a, width);
00354 }
00355 
00356 //: Explicit overload of print for rgba<unsigned short>
00357 VCL_DEFINE_SPECIALIZATION
00358 void vil_print_value(vcl_ostream& os, const vil_rgba<vxl_uint_16>& value, unsigned width)
00359 {
00360   vil_print_value(os, value.r, width);
00361   os<<'/';
00362   vil_print_value(os, value.g, width);
00363   os<<'/';
00364   vil_print_value(os, value.b, width);
00365   os<<'/';
00366   vil_print_value(os, value.a, width);
00367 }
00368 
00369 //: Explicit overload of print for rgba<int>
00370 VCL_DEFINE_SPECIALIZATION
00371 void vil_print_value(vcl_ostream& os, const vil_rgba<vxl_int_32>& value, unsigned width)
00372 {
00373   vil_print_value(os, value.r, width);
00374   os<<'/';
00375   vil_print_value(os, value.g, width);
00376   os<<'/';
00377   vil_print_value(os, value.b, width);
00378   os<<'/';
00379   vil_print_value(os, value.a, width);
00380 }
00381 
00382 //: Explicit overload of print for rgba<unsigned int>
00383 VCL_DEFINE_SPECIALIZATION
00384 void vil_print_value(vcl_ostream& os, const vil_rgba<vxl_uint_32>& value, unsigned width)
00385 {
00386   vil_print_value(os, value.r, width);
00387   os<<'/';
00388   vil_print_value(os, value.g, width);
00389   os<<'/';
00390   vil_print_value(os, value.b, width);
00391   os<<'/';
00392   vil_print_value(os, value.a, width);
00393 }
00394 
00395 #if VXL_HAS_INT_64
00396 
00397 //: Explicit overload of print for rgba<long>
00398 VCL_DEFINE_SPECIALIZATION
00399 void vil_print_value(vcl_ostream& os, const vil_rgba<vxl_int_64>& value, unsigned width)
00400 {
00401   vil_print_value(os, value.r, width);
00402   os<<'/';
00403   vil_print_value(os, value.g, width);
00404   os<<'/';
00405   vil_print_value(os, value.b, width);
00406   os<<'/';
00407   vil_print_value(os, value.a, width);
00408 }
00409 
00410 //: Explicit overload of print for rgba<unsigned long>
00411 VCL_DEFINE_SPECIALIZATION
00412 void vil_print_value(vcl_ostream& os, const vil_rgba<vxl_uint_64>& value, unsigned width)
00413 {
00414   vil_print_value(os, value.r, width);
00415   os<<'/';
00416   vil_print_value(os, value.g, width);
00417   os<<'/';
00418   vil_print_value(os, value.b, width);
00419   os<<'/';
00420   vil_print_value(os, value.a, width);
00421 }
00422 
00423 #endif
00424 
00425 //: Explicit overload of print for rgba<float>
00426 VCL_DEFINE_SPECIALIZATION
00427 void vil_print_value(vcl_ostream& os, const vil_rgba<float>& value, unsigned)
00428 {
00429   os<<value.r<<'/'<<value.g<<'/'<<value.b<<'/'<<value.a;
00430 }
00431 
00432 //: Explicit overload of print for rgba<double>
00433 VCL_DEFINE_SPECIALIZATION
00434 void vil_print_value(vcl_ostream& os, const vil_rgba<double>& value, unsigned)
00435 {
00436   os<<value.r<<'/'<<value.g<<'/'<<value.b<<'/'<<value.a;
00437 }
00438 
00439 void vil_print_all(vcl_ostream& os, vil_image_view_base_sptr const& view)
00440 {
00441 #define docase(T) \
00442    case T: \
00443      vil_print_all(os, static_cast<vil_image_view< vil_pixel_format_type_of<T >::type > >(view) );\
00444      break
00445 
00446   switch ( view->pixel_format() )
00447   {
00448 #if VXL_HAS_INT_64
00449     docase( VIL_PIXEL_FORMAT_UINT_64 );
00450     docase( VIL_PIXEL_FORMAT_INT_64 );
00451 #endif
00452     docase( VIL_PIXEL_FORMAT_UINT_32 );
00453     docase( VIL_PIXEL_FORMAT_INT_32 );
00454     docase( VIL_PIXEL_FORMAT_UINT_16 );
00455     docase( VIL_PIXEL_FORMAT_INT_16 );
00456     docase( VIL_PIXEL_FORMAT_BYTE );
00457     docase( VIL_PIXEL_FORMAT_SBYTE );
00458     docase( VIL_PIXEL_FORMAT_FLOAT );
00459     docase( VIL_PIXEL_FORMAT_DOUBLE );
00460     docase( VIL_PIXEL_FORMAT_BOOL );
00461 
00462 #if VXL_HAS_INT_64
00463     docase( VIL_PIXEL_FORMAT_RGB_UINT_64 );
00464     docase( VIL_PIXEL_FORMAT_RGB_INT_64 );
00465 #endif
00466     docase( VIL_PIXEL_FORMAT_RGB_UINT_32 );
00467     docase( VIL_PIXEL_FORMAT_RGB_INT_32 );
00468     docase( VIL_PIXEL_FORMAT_RGB_UINT_16 );
00469     docase( VIL_PIXEL_FORMAT_RGB_INT_16 );
00470     docase( VIL_PIXEL_FORMAT_RGB_BYTE );
00471     docase( VIL_PIXEL_FORMAT_RGB_SBYTE );
00472     docase( VIL_PIXEL_FORMAT_RGB_FLOAT );
00473     docase( VIL_PIXEL_FORMAT_RGB_DOUBLE );
00474 
00475 #if VXL_HAS_INT_64
00476     docase( VIL_PIXEL_FORMAT_RGBA_UINT_64 );
00477     docase( VIL_PIXEL_FORMAT_RGBA_INT_64 );
00478 #endif
00479     docase( VIL_PIXEL_FORMAT_RGBA_UINT_32 );
00480     docase( VIL_PIXEL_FORMAT_RGBA_INT_32 );
00481     docase( VIL_PIXEL_FORMAT_RGBA_UINT_16 );
00482     docase( VIL_PIXEL_FORMAT_RGBA_INT_16 );
00483     docase( VIL_PIXEL_FORMAT_RGBA_BYTE );
00484     docase( VIL_PIXEL_FORMAT_RGBA_SBYTE );
00485     docase( VIL_PIXEL_FORMAT_RGBA_FLOAT );
00486     docase( VIL_PIXEL_FORMAT_RGBA_DOUBLE );
00487 
00488     docase( VIL_PIXEL_FORMAT_COMPLEX_FLOAT );
00489     docase( VIL_PIXEL_FORMAT_COMPLEX_DOUBLE );
00490 
00491     default: ;
00492   }
00493 #undef docase
00494 }