core/vnl/io/vnl_io_matrix.txx
Go to the documentation of this file.
00001 // This is core/vnl/io/vnl_io_matrix.txx
00002 #ifndef vnl_io_matrix_txx_
00003 #define vnl_io_matrix_txx_
00004 //:
00005 // \file
00006 
00007 #include "vnl_io_matrix.h"
00008 #include <vnl/vnl_matrix.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>
00016 void vsl_b_write(vsl_b_ostream & os, const vnl_matrix<T> & 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 causes segfault
00024   if (p.size()>0)
00025     vsl_block_binary_write(os, p.begin(), p.size());
00026 }
00027 
00028 //=================================================================================
00029 //: Binary load self from stream.
00030 template<class T>
00031 void vsl_b_read(vsl_b_istream &is, vnl_matrix<T> & p)
00032 {
00033   if (!is) return;
00034 
00035   short v;
00036   unsigned m, n;
00037   vsl_b_read(is, v);
00038   switch (v)
00039   {
00040    case 1:
00041     vsl_b_read(is, m);
00042     vsl_b_read(is, n);
00043     p.set_size(m, n);
00044     // Calling begin() on empty matrix causes segfault
00045     if (m*n>0)
00046       vsl_b_read_block_old(is, p.begin(), p.size());
00047     break;
00048 
00049    case 2:
00050     vsl_b_read(is, m);
00051     vsl_b_read(is, n);
00052     p.set_size(m, n);
00053     // Calling begin() on empty matrix causes segfault
00054     if (m*n>0)
00055       vsl_block_binary_read(is, p.data_block(), p.size());
00056     break;
00057 
00058    default:
00059     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_matrix<T>&)\n"
00060              << "           Unknown version number "<< v << '\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 vnl_matrix<T> & p)
00070 {
00071   os<<"Size: "<<p.rows()<<" x "<<p.cols()<<vcl_endl;
00072 
00073   unsigned int m = 5; unsigned int n = 5;
00074 
00075 
00076   if (m>p.rows()) m=p.rows();
00077   if (n>p.cols()) n=p.cols();
00078 
00079   vsl_indent_inc(os);
00080   for (unsigned int i=0;i<m;i++)
00081   {
00082      os<<vsl_indent()<<" (";
00083 
00084      for ( unsigned int j=0; j<n; j++)
00085         os<<p(i,j)<<' ';
00086       if (p.cols()>n) os<<"...";
00087         os<<")\n";
00088   }
00089   if (p.rows()>m) os <<vsl_indent()<<" (...\n";
00090   vsl_indent_dec(os);
00091 }
00092 
00093 
00094 #define VNL_IO_MATRIX_INSTANTIATE(T) \
00095 template void vsl_print_summary(vcl_ostream &, const vnl_matrix<T > &); \
00096 template void vsl_b_read(vsl_b_istream &, vnl_matrix<T > &); \
00097 template void vsl_b_write(vsl_b_ostream &, const vnl_matrix<T > &)
00098 
00099 #endif // vnl_io_matrix_txx_