vnl_matlab_write.cxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_matlab_write.cxx
2 //:
3 // \file
4 // \author fsm
5 
6 #include <iostream>
7 #include <complex>
8 #include <cstring>
9 
10 #include <vnl/vnl_matlab_header.h>
11 
12 #if VXL_LITTLE_ENDIAN // #ifdef i386
13 # define native_BYTE_ORDER vnl_matlab_header::vnl_LITTLE_ENDIAN
14 #else
15 # define native_BYTE_ORDER vnl_matlab_header::vnl_BIG_ENDIAN
16 #endif
17 
18 static void vnl_write_bytes(std::ostream &s, void const *p, unsigned bytes)
19 {
20  s.write((char const *)p, bytes);
21 }
22 
23 // ------------------------------ traits without tears ------------------------------
24 
25 // template <class T> long scalar_precision(T const &);
26 static long vnl_scalar_precision(float const &) { return vnl_matlab_header::vnl_SINGLE_PRECISION; }
27 static long vnl_scalar_precision(double const &) { return vnl_matlab_header::vnl_DOUBLE_PRECISION; }
28 static long vnl_scalar_precision(std::complex<float> const &) { return vnl_matlab_header::vnl_SINGLE_PRECISION; }
29 static long vnl_scalar_precision(std::complex<double> const &) { return vnl_matlab_header::vnl_DOUBLE_PRECISION; }
30 
31 // template <class T> long is_complex(T const &);
32 static long vnl_is_complex(float const &) { return 0; }
33 static long vnl_is_complex(double const &) { return 0; }
34 static long vnl_is_complex(std::complex<float> const &) { return 1; }
35 static long vnl_is_complex(std::complex<double> const &) { return 1; }
36 
37 // template <class T> void vnl_write_real(std::ostream &, T const *, unsigned );
38 static void vnl_write_real(std::ostream &s, float const *data, unsigned n)
39 { ::vnl_write_bytes(s, data, n*sizeof(*data)); }
40 
41 static void vnl_write_real(std::ostream &s, double const *data, unsigned n)
42 { ::vnl_write_bytes(s, data, n*sizeof(*data)); }
43 
44 static void vnl_write_real(std::ostream &s, std::complex<float> const *data, unsigned n)
45 {
46  float dummy;
47  for (unsigned i=0; i<n; ++i) { // real block
48  dummy = std::real(data[i]);
49  ::vnl_write_bytes(s, &dummy, sizeof(dummy));
50  }
51 }
52 
53 static void vnl_write_real(std::ostream &s, std::complex<double> const *data, unsigned n)
54 {
55  double dummy;
56  for (unsigned i=0; i<n; ++i) { // real block
57  dummy = std::real(data[i]);
58  ::vnl_write_bytes(s, &dummy, sizeof(dummy));
59  }
60 }
61 
62 // template <class T> void vnl_write_imag(std::ostream &, T const *, unsigned );
63 
64 static void vnl_write_imag(std::ostream &, float const *, unsigned ) { }
65 
66 static void vnl_write_imag(std::ostream &, double const *, unsigned ) { }
67 
68 static void vnl_write_imag(std::ostream &s, std::complex<float> const *data, unsigned n)
69 {
70  float dummy;
71  for (unsigned i=0; i<n; ++i) { // imag block
72  dummy = std::imag(data[i]);
73  ::vnl_write_bytes(s, &dummy, sizeof(dummy));
74  }
75 }
76 
77 static void vnl_write_imag(std::ostream &s, std::complex<double> const *data, unsigned n)
78 {
79  double dummy;
80  for (unsigned i=0; i<n; ++i) { // imag block
81  dummy = std::imag(data[i]);
82  ::vnl_write_bytes(s, &dummy, sizeof(dummy));
83  }
84 }
85 
86 //--------------------------------------------------------------------------------
87 
88 //: scalars
89 template <class T>
90 bool vnl_matlab_write(std::ostream &s, T const & x, char const *name)
91 {
93  hdr.type = native_BYTE_ORDER + vnl_matlab_header::vnl_COLUMN_WISE + vnl_scalar_precision(x);
94  hdr.rows = 1;
95  hdr.cols = 1;
96  hdr.imag = vnl_is_complex(x);
97  hdr.namlen = (unsigned long)std::strlen(name)+1L;
98 
99  ::vnl_write_bytes(s, &hdr, sizeof(hdr));
100  ::vnl_write_bytes(s, name, hdr.namlen);
101  vnl_write_real(s, &x, 1);
102  vnl_write_imag(s, &x, 1);
103 
104  return s.good() != 0;
105 }
106 #define scalar_instantiate(T) \
107 template VNL_EXPORT bool vnl_matlab_write(std::ostream &, T const &, char const *);
108 
109 //: 1D array
110 template <class T>
111 bool vnl_matlab_write(std::ostream &s, T const *v, unsigned n, char const *name)
112 {
113  vnl_matlab_header hdr;
114  hdr.type = native_BYTE_ORDER + vnl_matlab_header::vnl_COLUMN_WISE + vnl_scalar_precision(v[0]);
115  hdr.rows = (long)n;
116  hdr.cols = 1L;
117  hdr.imag = vnl_is_complex(v[0]);
118  hdr.namlen = (unsigned long)std::strlen(name)+1L;
119 
120  ::vnl_write_bytes(s, &hdr, sizeof(hdr));
121  ::vnl_write_bytes(s, name, hdr.namlen);
122  vnl_write_real(s, v, n);
123  vnl_write_imag(s, v, n);
124 
125  return s.good() != 0;
126 }
127 #define array1D_instantiate(T) \
128 template VNL_EXPORT bool vnl_matlab_write(std::ostream &, T const *, unsigned, char const *);
129 
130 //: 2D array
131 template <class T>
132 bool vnl_matlab_write(std::ostream &s,
133  T const * const *data,
134  unsigned rows, unsigned cols,
135  char const *name)
136 {
137  vnl_matlab_header hdr;
138  hdr.type = native_BYTE_ORDER + vnl_matlab_header::vnl_ROW_WISE + vnl_scalar_precision(data[0][0]);
139  hdr.rows = (long)rows;
140  hdr.cols = (long)cols;
141  hdr.imag = vnl_is_complex(data[0][0]);
142  hdr.namlen = (unsigned long)std::strlen(name)+1L;
143 
144  ::vnl_write_bytes(s, &hdr, sizeof(hdr));
145  ::vnl_write_bytes(s, name, hdr.namlen);
146  for (unsigned i=0; i<rows; ++i)
147  vnl_write_real(s, data[i], cols);
148  for (unsigned i=0; i<rows; ++i)
149  vnl_write_imag(s, data[i], cols);
150 
151  return s.good() != 0;
152 }
153 #define array2D_instantiate(T) \
154 template VNL_EXPORT bool vnl_matlab_write(std::ostream &, T const * const *, unsigned, unsigned, char const *);
155 
156 //--------------------------------------------------------------------------------
157 
158 scalar_instantiate(float);
159 scalar_instantiate(double);
160 scalar_instantiate(std::complex<float>);
161 scalar_instantiate(std::complex<double>);
162 
163 array1D_instantiate(float);
164 array1D_instantiate(double);
165 array1D_instantiate(std::complex<float>);
166 array1D_instantiate(std::complex<double>);
167 
168 array2D_instantiate(float);
169 array2D_instantiate(double);
170 array2D_instantiate(std::complex<float>);
171 array2D_instantiate(std::complex<double>);
MATLAB header structure.
#define array2D_instantiate(T)
#define native_BYTE_ORDER
bool vnl_matlab_write(std::ostream &s, T const &x, char const *name)
scalars.
#define v
Definition: vnl_vector.h:42
#define array1D_instantiate(T)
#define scalar_instantiate(T)