core/vnl/vnl_matlab_read.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_matlab_read.h
00002 #ifndef vnl_matlab_read_h_
00003 #define vnl_matlab_read_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Read from MATLAB files
00010 // \author fsm
00011 //
00012 // \verbatim
00013 //  Modifications
00014 //   LSB (Manchester) 23 Mar 2001 documentation tidied
00015 //   Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
00016 //   21 Apr 2009 Kent Williams - Taking care of the byte ordering of the MAT file
00017 // \endverbatim
00018 
00019 #include <vcl_iosfwd.h>
00020 #include <vcl_complex.h>
00021 #include <vnl/vnl_matlab_header.h>
00022 
00023 // ------------------------------ easy ------------------------------
00024 
00025 template <class T> class vnl_vector;
00026 template <class T> class vnl_matrix;
00027 
00028 //: Attempt to read vector or matrix.
00029 // If the MATLAB header cannot be read, return false.
00030 // Else, if a name is given, and it doesn't match what's in the file, abort().
00031 // If the data in the file cannot reasonably be read into the destination, abort().
00032 //
00033 // The vector/matrix will be resized if necessary.
00034 template <class T> bool vnl_matlab_read_or_die(vcl_istream &, vnl_vector<T> &, char const *name =0);
00035 template <class T> bool vnl_matlab_read_or_die(vcl_istream &, vnl_matrix<T> &, char const *name =0);
00036 
00037 // ------------------------------ less easy ------------------------------
00038 
00039 //: MATLAB stores its data as a real block followed by an imaginary block.
00040 // This function will read both blocks and interleave them into the area
00041 // pointed to by ptr. For real T, it is equivalent to s.read(ptr, sizeof(T)*n);
00042 template <class T> void vnl_matlab_read_data(vcl_istream &s, T *ptr, unsigned n);
00043 
00044 class vnl_matlab_readhdr
00045 {
00046   VCL_SAFE_BOOL_DEFINE;
00047  public:
00048   vnl_matlab_readhdr(vcl_istream &);
00049   ~vnl_matlab_readhdr();
00050 
00051   operator safe_bool () const;
00052   bool operator!() const;
00053   void read_next(); // skip to next header in file
00054 
00055   bool is_single() const;
00056   bool is_rowwise() const;
00057   bool is_bigendian() const; // don't use this
00058   long rows() const { return hdr.rows; }
00059   long cols() const { return hdr.cols; }
00060   bool is_complex() const { return hdr.imag != 0; }
00061   char const *name() const { return varname; }
00062 
00063   // bah! no member templates
00064   //template <class T> bool read_data(T &); // scalar
00065   //template <class T> bool read_data(T *); // vector
00066   //template <class T> bool read_data(T * const *); // 2D array
00067 #define fsm_declare_methods(T) \
00068  private: \
00069   bool type_chck(T &); \
00070  public: \
00071   bool read_data(T &); \
00072   bool read_data(T *); \
00073   bool read_data(T * const *) // no ; here, please. SunPro 5.0 barfs.
00074 fsm_declare_methods(float);
00075 fsm_declare_methods(double);
00076 fsm_declare_methods(vcl_complex<float>);
00077 fsm_declare_methods(vcl_complex<double>);
00078 #undef fsm_declare_methods
00079 
00080  private:
00081   vcl_istream &s;
00082   vnl_matlab_header hdr;
00083   char *varname;
00084   bool data_read;
00085   bool need_swap;
00086   void read_hdr(); // internal work routine
00087 };
00088 
00089 #endif // vnl_matlab_read_h_