core/vnl/vnl_matlab_write.cxx
Go to the documentation of this file.
00001 // This is core/vnl/vnl_matlab_write.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "vnl_matlab_write.h"
00010 
00011 #include <vcl_iostream.h>
00012 #include <vcl_cstring.h>  // strlen()
00013 #include <vcl_complex.h>
00014 #include <vnl/vnl_matlab_header.h>
00015 
00016 #include <vxl_config.h>
00017 #if VXL_LITTLE_ENDIAN // #ifdef i386
00018 # define native_BYTE_ORDER vnl_matlab_header::vnl_LITTLE_ENDIAN
00019 #else
00020 # define native_BYTE_ORDER vnl_matlab_header::vnl_BIG_ENDIAN
00021 #endif
00022 
00023 // SGI needs char * as first argument to vcl_ostream::write
00024 void vnl_write_bytes(vcl_ostream &s, void const *p, unsigned bytes)
00025 {
00026   s.write((char const *)p, bytes);
00027 }
00028 
00029 // ------------------------------ traits without tears ------------------------------
00030 
00031 //awf: these cannot be static for sunpro 5...
00032 
00033 // template <class T> long scalar_precision(T const &);
00034 long vnl_scalar_precision(float  const &) { return vnl_matlab_header::vnl_SINGLE_PRECISION; }
00035 long vnl_scalar_precision(double const &) { return vnl_matlab_header::vnl_DOUBLE_PRECISION; }
00036 long vnl_scalar_precision(vcl_complex<float>  const &) { return vnl_matlab_header::vnl_SINGLE_PRECISION; }
00037 long vnl_scalar_precision(vcl_complex<double> const &) { return vnl_matlab_header::vnl_DOUBLE_PRECISION; }
00038 
00039 // template <class T> long is_complex(T const &);
00040 long vnl_is_complex(float  const &) { return 0; }
00041 long vnl_is_complex(double const &) { return 0; }
00042 long vnl_is_complex(vcl_complex<float>  const &) { return 1; }
00043 long vnl_is_complex(vcl_complex<double> const &) { return 1; }
00044 
00045 // template <class T> void vnl_write_real(vcl_ostream &, T const *, unsigned );
00046 void vnl_write_real(vcl_ostream &s, float const *data, unsigned n)
00047 { ::vnl_write_bytes(s, data, n*sizeof(*data)); }
00048 
00049 void vnl_write_real(vcl_ostream &s, double const *data, unsigned n)
00050 { ::vnl_write_bytes(s, data, n*sizeof(*data)); }
00051 
00052 void vnl_write_real(vcl_ostream &s, vcl_complex<float> const *data, unsigned n)
00053 {
00054   float dummy;
00055   for (unsigned i=0; i<n; ++i) { // real block
00056     dummy = vcl_real(data[i]);
00057     ::vnl_write_bytes(s, &dummy, sizeof(dummy));
00058   }
00059 }
00060 
00061 void vnl_write_real(vcl_ostream &s, vcl_complex<double> const *data, unsigned n)
00062 {
00063   double dummy;
00064   for (unsigned i=0; i<n; ++i) { // real block
00065     dummy = vcl_real(data[i]);
00066     ::vnl_write_bytes(s, &dummy, sizeof(dummy));
00067   }
00068 }
00069 
00070 // template <class T> void vnl_write_imag(vcl_ostream &, T const *, unsigned );
00071 
00072 void vnl_write_imag(vcl_ostream &, float const *, unsigned ) { }
00073 
00074 void vnl_write_imag(vcl_ostream &, double const *, unsigned ) { }
00075 
00076 void vnl_write_imag(vcl_ostream &s, vcl_complex<float> const *data, unsigned n)
00077 {
00078   float dummy;
00079   for (unsigned i=0; i<n; ++i) { // imag block
00080     dummy = vcl_imag(data[i]);
00081     ::vnl_write_bytes(s, &dummy, sizeof(dummy));
00082   }
00083 }
00084 
00085 void vnl_write_imag(vcl_ostream &s, vcl_complex<double> const *data, unsigned n)
00086 {
00087   double dummy;
00088   for (unsigned i=0; i<n; ++i) { // imag block
00089     dummy = vcl_imag(data[i]);
00090     ::vnl_write_bytes(s, &dummy, sizeof(dummy));
00091   }
00092 }
00093 
00094 //--------------------------------------------------------------------------------
00095 
00096 //: scalars
00097 template <class T>
00098 bool vnl_matlab_write(vcl_ostream &s, T const & x, char const *name)
00099 {
00100   vnl_matlab_header hdr;
00101   hdr.type = native_BYTE_ORDER + vnl_matlab_header::vnl_COLUMN_WISE + vnl_scalar_precision(x);
00102   hdr.rows = 1;
00103   hdr.cols = 1;
00104   hdr.imag = vnl_is_complex(x);
00105   hdr.namlen = (unsigned long)vcl_strlen(name)+1L;
00106 
00107   ::vnl_write_bytes(s, &hdr, sizeof(hdr));
00108   ::vnl_write_bytes(s, name, hdr.namlen);
00109   vnl_write_real(s, &x, 1);
00110   vnl_write_imag(s, &x, 1);
00111 
00112   return s.good() != 0;
00113 }
00114 #define scalar_instantiate(T) \
00115 template bool vnl_matlab_write(vcl_ostream &, T const &, char const *);
00116 
00117 //: 1D array
00118 template <class T>
00119 bool vnl_matlab_write(vcl_ostream &s, T const *v, unsigned n, char const *name)
00120 {
00121   vnl_matlab_header hdr;
00122   hdr.type = native_BYTE_ORDER + vnl_matlab_header::vnl_COLUMN_WISE + vnl_scalar_precision(v[0]);
00123   hdr.rows = (long)n;
00124   hdr.cols = 1L;
00125   hdr.imag = vnl_is_complex(v[0]);
00126   hdr.namlen = (unsigned long)vcl_strlen(name)+1L;
00127 
00128   ::vnl_write_bytes(s, &hdr, sizeof(hdr));
00129   ::vnl_write_bytes(s, name, hdr.namlen);
00130   vnl_write_real(s, v, n);
00131   vnl_write_imag(s, v, n);
00132 
00133   return s.good() != 0;
00134 }
00135 #define array1D_instantiate(T) \
00136 template bool vnl_matlab_write(vcl_ostream &, T const *, unsigned, char const *);
00137 
00138 //: 2D array
00139 template <class T>
00140 bool vnl_matlab_write(vcl_ostream &s,
00141                       T const * const *data,
00142                       unsigned rows, unsigned cols,
00143                       char const *name)
00144 {
00145   vnl_matlab_header hdr;
00146   hdr.type = native_BYTE_ORDER + vnl_matlab_header::vnl_ROW_WISE + vnl_scalar_precision(data[0][0]);
00147   hdr.rows = (long)rows;
00148   hdr.cols = (long)cols;
00149   hdr.imag = vnl_is_complex(data[0][0]);
00150   hdr.namlen = (unsigned long)vcl_strlen(name)+1L;
00151 
00152   ::vnl_write_bytes(s, &hdr, sizeof(hdr));
00153   ::vnl_write_bytes(s, name, hdr.namlen);
00154   for (unsigned i=0; i<rows; ++i)
00155     vnl_write_real(s, data[i], cols);
00156   for (unsigned i=0; i<rows; ++i)
00157     vnl_write_imag(s, data[i], cols);
00158 
00159   return s.good() != 0;
00160 }
00161 #define array2D_instantiate(T) \
00162 template bool vnl_matlab_write(vcl_ostream &, T const * const *, unsigned, unsigned, char const *);
00163 
00164 //--------------------------------------------------------------------------------
00165 
00166 scalar_instantiate(float);
00167 scalar_instantiate(double);
00168 scalar_instantiate(vcl_complex<float>);
00169 scalar_instantiate(vcl_complex<double>);
00170 
00171 array1D_instantiate(float);
00172 array1D_instantiate(double);
00173 array1D_instantiate(vcl_complex<float>);
00174 array1D_instantiate(vcl_complex<double>);
00175 
00176 array2D_instantiate(float);
00177 array2D_instantiate(double);
00178 array2D_instantiate(vcl_complex<float>);
00179 array2D_instantiate(vcl_complex<double>);