vnl_matlab_read.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_matlab_read.h
2 #ifndef vnl_matlab_read_h_
3 #define vnl_matlab_read_h_
4 //:
5 // \file
6 // \brief Read from MATLAB files
7 // \author fsm
8 //
9 // \verbatim
10 // Modifications
11 // LSB (Manchester) 23 Mar 2001 documentation tidied
12 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
13 // 21 Apr 2009 Kent Williams - Taking care of the byte ordering of the MAT file
14 // \endverbatim
15 
16 #include <iosfwd>
17 #include <complex>
18 #ifdef _MSC_VER
19 # include <vcl_msvc_warnings.h>
20 #endif
21 #include <vnl/vnl_matlab_header.h>
22 #include "vnl/vnl_export.h"
23 
24 
25 // ------------------------------ easy ------------------------------
26 
27 template <class T> class vnl_vector;
28 template <class T> class vnl_matrix;
29 
30 //: Attempt to read vector or matrix.
31 // If the MATLAB header cannot be read, return false.
32 // Else, if a name is given, and it doesn't match what's in the file, abort().
33 // If the data in the file cannot reasonably be read into the destination, abort().
34 //
35 // The vector/matrix will be resized if necessary.
36 template <class T> VNL_EXPORT bool vnl_matlab_read_or_die(std::istream &, vnl_vector<T> &, char const *name =nullptr);
37 template <class T> VNL_EXPORT bool vnl_matlab_read_or_die(std::istream &, vnl_matrix<T> &, char const *name =nullptr);
38 
39 // ------------------------------ less easy ------------------------------
40 
41 //: MATLAB stores its data as a real block followed by an imaginary block.
42 // This function will read both blocks and interleave them into the area
43 // pointed to by ptr. For real T, it is equivalent to s.read(ptr, sizeof(T)*n);
44 template <class T> VNL_EXPORT void vnl_matlab_read_data(std::istream &s, T *ptr, unsigned n);
45 
46 class VNL_EXPORT vnl_matlab_readhdr
47 {
48  private:
49 
50  public:
51  vnl_matlab_readhdr(std::istream &);
53 
54  explicit operator bool () const;
55  bool operator!() const;
56  void read_next(); // skip to next header in file
57 
58  bool is_single() const;
59  bool is_rowwise() const;
60  bool is_bigendian() const; // don't use this
61  long rows() const { return hdr.rows; }
62  long cols() const { return hdr.cols; }
63  bool is_complex() const { return hdr.imag != 0; }
64  char const *name() const { return varname; }
65 
66  // bah! no member templates
67  //template <class T> bool read_data(T &); // scalar
68  //template <class T> bool read_data(T *); // vector
69  //template <class T> bool read_data(T * const *); // 2D array
70 #define fsm_declare_methods(T) \
71  private: \
72  bool type_chck(T &); \
73  public: \
74  bool read_data(T &); \
75  bool read_data(T *); \
76  bool read_data(T * const *) // no ; here, please.
77 fsm_declare_methods(float);
78 fsm_declare_methods(double);
79 fsm_declare_methods(std::complex<float>);
80 fsm_declare_methods(std::complex<double>);
81 #undef fsm_declare_methods
82 
83  private:
84  std::istream &s;
86  char *varname;
87  bool data_read;
88  bool need_swap;
89  void read_hdr(); // internal work routine
90 };
91 
92 #endif // vnl_matlab_read_h_
bool is_complex() const
MATLAB header structure.
vnl_matlab_header hdr
#define fsm_declare_methods(T)
char const * name() const
VNL_EXPORT void vnl_matlab_read_data(std::istream &s, T *ptr, unsigned n)
MATLAB stores its data as a real block followed by an imaginary block.
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
Mathematical vector class, templated by type of element.
Definition: vnl_fwd.h:16
std::istream & s
VNL_EXPORT bool vnl_matlab_read_or_die(std::istream &, vnl_vector< T > &, char const *name=nullptr)
Attempt to read vector or matrix.