core/vnl/io/vnl_io_matrix_fixed.txx
Go to the documentation of this file.
00001 // This is core/vnl/io/vnl_io_matrix_fixed.txx
00002 #ifndef vnl_io_matrix_fixed_txx_
00003 #define vnl_io_matrix_fixed_txx_
00004 //:
00005 // \file
00006 
00007 #include "vnl_io_matrix_fixed.h"
00008 #include <vnl/vnl_matrix_fixed.h>
00009 #include <vsl/vsl_b_read_block_old.h>
00010 #include <vsl/vsl_block_binary.h>
00011 #include <vsl/vsl_indent.h>
00012 
00013 //=================================================================================
00014 //: Binary save self to stream.
00015 template<class T, unsigned m, unsigned n>
00016 void vsl_b_write(vsl_b_ostream & os, const vnl_matrix_fixed<T,m,n> & p)
00017 {
00018   const short version_no = 2;
00019   vsl_b_write(os, version_no);
00020   vsl_b_write(os, p.rows());
00021   vsl_b_write(os, p.cols());
00022 
00023   // Calling p.begin() on empty matrix_fixed causes segfault
00024   if (p.size()>0)
00025     vsl_block_binary_write(os, p.data_block(), m*n);
00026 }
00027 
00028 //=================================================================================
00029 //: Binary load self from stream.
00030 template<class T, unsigned m, unsigned n>
00031 void vsl_b_read(vsl_b_istream &is, vnl_matrix_fixed<T,m,n> & p)
00032 {
00033   if (!is) return;
00034 
00035   short v;
00036   unsigned stream_m, stream_n;
00037   vsl_b_read(is, v);
00038   switch (v)
00039   {
00040    case 1:
00041     vsl_b_read(is, stream_m);
00042     vsl_b_read(is, stream_n);
00043     if ( stream_n != n || stream_m != m ) {
00044       vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_matrix_fixed<T>&)\n"
00045                << "           Expected size " << m << ',' << n << "; got " << stream_m << ',' << stream_n << '\n';
00046       is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00047       return;
00048     }
00049     // Calling begin() on empty matrix_fixed causes segfault
00050     if (m*n>0)
00051       vsl_b_read_block_old(is, p.begin(), p.size());
00052     break;
00053 
00054    case 2:
00055     vsl_b_read(is, stream_m);
00056     vsl_b_read(is, stream_n);
00057     if ( stream_n != n || stream_m != m ) {
00058       vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_matrix_fixed<T>&)\n"
00059                << "           Expected size " << m << ',' << n << "; got " << stream_m << ',' << stream_n << '\n';
00060       is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00061       return;
00062     }
00063     // Calling begin() on empty matrix_fixed causes segfault
00064     if (m*n>0)
00065       vsl_block_binary_read(is, p.data_block(), m*n);
00066     break;
00067 
00068    default:
00069     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_matrix_fixed<T>&)\n"
00070              << "           Unknown version number "<< v << '\n';
00071     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00072     return;
00073   }
00074 }
00075 
00076 //====================================================================================
00077 //: Output a human readable summary to the stream
00078 template<class T, unsigned nrows, unsigned ncols>
00079 void vsl_print_summary(vcl_ostream & os,const vnl_matrix_fixed<T,nrows,ncols> & p)
00080 {
00081   os<<"Size: "<<p.rows()<<" x "<<p.cols()<<vcl_endl;
00082 
00083   unsigned int m = 5; unsigned int n = 5;
00084 
00085 
00086   if (m>p.rows()) m=p.rows();
00087   if (n>p.cols()) n=p.cols();
00088 
00089   vsl_indent_inc(os);
00090   for (unsigned int i=0;i<m;i++)
00091   {
00092      os<<vsl_indent()<<" (";
00093 
00094      for ( unsigned int j=0; j<n; j++)
00095         os<<p(i,j)<<' ';
00096       if (p.cols()>n) os<<"...";
00097         os<<")\n";
00098   }
00099   if (p.rows()>m) os <<vsl_indent()<<" (...\n";
00100   vsl_indent_dec(os);
00101 }
00102 
00103 
00104 #define VNL_IO_MATRIX_FIXED_INSTANTIATE(T,m,n) \
00105 template void vsl_print_summary(vcl_ostream &, const vnl_matrix_fixed<T,m,n > &); \
00106 template void vsl_b_read(vsl_b_istream &, vnl_matrix_fixed<T,m,n > &); \
00107 template void vsl_b_write(vsl_b_ostream &, const vnl_matrix_fixed<T,m,n > &)
00108 
00109 #endif // vnl_io_matrix_fixed_txx_