core/vbl/io/vbl_io_array_2d.txx
Go to the documentation of this file.
00001 // This is core/vbl/io/vbl_io_array_2d.txx
00002 #ifndef vbl_io_array_2d_txx_
00003 #define vbl_io_array_2d_txx_
00004 //:
00005 // \file
00006 // \brief  binary IO functions for vbl_array_2d<T>
00007 // \author K.Y.McGaul
00008 
00009 #include "vbl_io_array_2d.h"
00010 #include <vsl/vsl_binary_io.h>
00011 #include <vbl/vbl_array_2d.h>
00012 
00013 //=======================================================================
00014 //: Binary save self to stream.
00015 template<class T>
00016 void vsl_b_write(vsl_b_ostream & os, const vbl_array_2d<T> &p)
00017 {
00018   const short io_version_no = 1;
00019   vsl_b_write(os, io_version_no);
00020 
00021   typedef typename vbl_array_2d<T>::size_type size_type;
00022   size_type array_rows = p.rows();
00023   size_type array_cols = p.cols();
00024   vsl_b_write(os, array_rows);
00025   vsl_b_write(os, array_cols);
00026   for (size_type i=0; i<array_rows; i++)
00027   {
00028     for (size_type j=0; j<array_cols; j++)
00029       vsl_b_write(os, p(i,j));
00030   }
00031 }
00032 
00033 //=======================================================================
00034 //: Binary load self from stream.
00035 template<class T>
00036 void vsl_b_read(vsl_b_istream &is, vbl_array_2d<T> &p)
00037 {
00038   if (!is) return;
00039 
00040   short ver;
00041   typedef typename vbl_array_2d<T>::size_type size_type;
00042   size_type array_rows, array_cols;
00043   vsl_b_read(is, ver);
00044   switch (ver)
00045   {
00046    case 1:
00047     vsl_b_read(is, array_rows);
00048     vsl_b_read(is, array_cols);
00049     p.resize(array_rows, array_cols);
00050     for (size_type i=0; i<array_rows; i++)
00051     {
00052       for (size_type j=0; j<array_cols; j++)
00053         vsl_b_read(is, p(i,j));
00054     }
00055     break;
00056 
00057    default:
00058     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_array_2d<T>&)\n"
00059              << "           Unknown version number "<< ver << '\n';
00060     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00061     return;
00062   }
00063 }
00064 
00065 //=======================================================================
00066 //: Output a human readable summary to the stream
00067 template<class T>
00068 void vsl_print_summary(vcl_ostream & os,const vbl_array_2d<T> & p)
00069 {
00070   os << "Rows: " << p.rows() << vcl_endl
00071      << "Columns: " << p.cols() << vcl_endl;
00072   typedef typename vbl_array_2d<T>::size_type size_type;
00073   for (size_type i =0; i<p.rows() && i<5; i++)
00074   {
00075     for (size_type j=0; j<p.cols() && j<5; j++)
00076     {
00077       os << ' ';
00078       vsl_print_summary(os, p(i,j));
00079     }
00080     if (p.cols() > 5)
00081       os << "...";
00082     os << vcl_endl;
00083   }
00084   if (p.rows() > 5)
00085     os << " ...\n";
00086 }
00087 
00088 #define VBL_IO_ARRAY_2D_INSTANTIATE(T) \
00089 template void vsl_print_summary(vcl_ostream &, const vbl_array_2d<T > &); \
00090 template void vsl_b_read(vsl_b_istream &, vbl_array_2d<T > &); \
00091 template void vsl_b_write(vsl_b_ostream &, const vbl_array_2d<T > &)
00092 
00093 #endif // vbl_io_array_2d_txx_