18 template <
class U,
class V>
26 s.read((
char *)p, bytes);
37 #define implement_read_complex_data(T) \ 39 void vnl_matlab_read_data(std::istream &s, std::complex<T > *ptr, unsigned n) { \ 40 T *re = vnl_c_vector<T >::allocate_T(n); \ 41 T *im = vnl_c_vector<T >::allocate_T(n); \ 42 ::vnl_read_bytes(s, re, n*sizeof(T)); \ 43 ::vnl_read_bytes(s, im, n*sizeof(T)); \ 44 for (unsigned i=0; i<n; ++i) \ 45 ptr[i] = std::complex<T >(re[i], im[i]); \ 46 vnl_c_vector<T >::deallocate(re, n); \ 47 vnl_c_vector<T >::deallocate(im, n); \ 53 #undef implement_read_complex_data 69 vnl_matlab_readhdr::operator bool ()
const 71 return (s.good() && !s.eof())?
true :
false;
76 return (
s.good() && !
s.eof())?
false :
true;
98 std::memset(&
hdr, 0,
sizeof hdr);
143 std::cerr <<
"type:" <<
hdr.
type << std::endl
144 <<
"rows:" <<
hdr.
rows << std::endl
145 <<
"cols:" <<
hdr.
cols << std::endl
146 <<
"imag:" <<
hdr.
imag << std::endl
159 unsigned long n =
rows()*
cols()*
sizeof(float);
166 s.seekg(n, std::ios::cur);
176 bool vnl_matlab_readhdr::type_chck(std::complex<float> &) {
return is_single() &&
is_complex(); }
177 bool vnl_matlab_readhdr::type_chck(std::complex<double> &) {
return !
is_single() &&
is_complex(); }
179 #define fsm_define_methods(T) \ 180 bool vnl_matlab_readhdr::read_data(T &v) { \ 181 if (!type_chck(v)) { std::cerr << "type_check\n"; return false; }\ 182 if (rows()!=1U || cols()!=1U) { std::cerr << "size0\n"; return false; } \ 183 vnl_matlab_read_data(s, &v, 1); \ 185 if (sizeof(v) == 4U) byteswap::swap32(&v); else byteswap::swap64(&v); \ 187 data_read = true; return !!*this; \ 189 bool vnl_matlab_readhdr::read_data(T *p) { \ 190 if (!type_chck(p[0])) { std::cerr << "type_check\n"; return false; } \ 191 if (rows()!=1U && cols()!=1U) { std::cerr << "size1\n"; return false; } \ 192 vnl_matlab_read_data(s, p, rows()*cols()); \ 194 for (long i = 0; i < rows()*cols(); ++i) { \ 195 if (sizeof(*p) == 4U) byteswap::swap32(&(p[i])); else byteswap::swap64(&(p[i])); \ 198 data_read = true; return !!*this; \ 200 bool vnl_matlab_readhdr::read_data(T * const *m) { \ 201 if (!type_chck(m[0][0])) { std::cerr << "type_check\n"; return false; } \ 202 T *tmp = vnl_c_vector<T >::allocate_T(rows()*cols()); \ 203 vnl_matlab_read_data(s, tmp, rows()*cols()); \ 205 for (long i = 0; i < rows()*cols(); ++i) { \ 206 if (sizeof(T) == 4U) byteswap::swap32(&(tmp[i])); else byteswap::swap64(&(tmp[i])); \ 210 if (is_rowwise()) { a = cols(); b = 1; } \ 211 else { a = 1; b = rows(); } \ 212 for (long i=0; i<rows(); ++i) \ 213 for (long j=0; j<cols(); ++j) \ 214 m[i][j] = tmp[a*i + b*j]; \ 215 vnl_c_vector<T >::deallocate(tmp, rows()*cols()); \ 216 data_read = true; return !!*this; \ 223 #undef fsm_define_methods 239 if (std::strcmp(name, h.
name())!=0) {
240 std::cerr <<
"vnl_matlab_read_or_die: names do not match\n";
244 if (
v.size() != (
unsigned long)(h.
rows()*h.
cols()))
249 if ( ! h.read_data(
v.begin()) ) {
250 std::cerr <<
"vnl_matlab_read_or_die: failed to read data\n";
265 if (std::strcmp(name, h.
name())!=0) {
266 std::cerr <<
"vnl_matlab_read_or_die: names do not match\n";
270 if (M.
rows() != (
unsigned long)(h.
rows()) || M.
cols() != (
unsigned long)(h.
cols()))
276 std::cerr <<
"vnl_matlab_read_or_die: failed to read data\n";
283 template VNL_EXPORT bool vnl_matlab_read_or_die(std::istream &, vnl_vector<T> &, char const *); \ 284 template VNL_EXPORT bool vnl_matlab_read_or_die(std::istream &, vnl_matrix<T> &, char const *);
unsigned int cols() const
Return the number of columns.
bool vnl_matlab_read_or_die(std::istream &s, vnl_vector< T > &v, char const *name)
Attempt to read vector or matrix.
An ordinary mathematical matrix.
bool is_bigendian() const
void vnl_read_bytes(std::istream &s, void *p, unsigned bytes)
char const * name() const
An ordinary mathematical matrix.
T const *const * data_array() const
Access the 2D array, so that elements can be accessed with array[row][col] directly.
#define fsm_define_methods(T)
Mathematical vector class, templated by type of element.
void vnl_matlab_read_data(std::istream &s, float *p, unsigned n)
void vcl_construct(U *p, V const &value)
unsigned int rows() const
Return the number of rows.
#define implement_read_complex_data(T)
vnl_matlab_readhdr(std::istream &)
Math on blocks of memory.