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