00001
00002 #ifndef bsta_io_histogram_h_
00003 #define bsta_io_histogram_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <bsta/bsta_histogram.h>
00016 #include <bsta/bsta_joint_histogram.h>
00017 #include <bsta/bsta_histogram_sptr.h>
00018 #include <bsta/bsta_joint_histogram_sptr.h>
00019 #include <vsl/vsl_binary_io.h>
00020 #include <vsl/vsl_vector_io.h>
00021 #include <vbl/vbl_array_2d.h>
00022 #include <vbl/io/vbl_io_array_2d.h>
00023
00024 template <class T>
00025 void
00026 vsl_b_write(vsl_b_ostream &os, const bsta_histogram<T>& h)
00027 {
00028 const short io_version_no = 1;
00029 vsl_b_write(os, io_version_no);
00030 bsta_histogram_base::bsta_hist_type type = h.type_;
00031 int itype = static_cast<int>(type);
00032 int nbins = h.nbins();
00033 T min = h.min();
00034 T max = h.max();
00035 T min_prob = h.min_prob();
00036 vcl_vector<T> values = h.value_array();
00037 vcl_vector<T> counts = h.count_array();
00038 vsl_b_write(os, itype);
00039 vsl_b_write(os, nbins);
00040 vsl_b_write(os, min);
00041 vsl_b_write(os, max);
00042 vsl_b_write(os, min_prob);
00043 vsl_b_write(os, values);
00044 vsl_b_write(os, counts);
00045 }
00046
00047
00048 template <class T>
00049 void
00050 vsl_b_read(vsl_b_istream &is, bsta_histogram<T>& h, bool skip = false)
00051 {
00052 short ver;
00053 int itype = 0;
00054 if (!skip){
00055 vsl_b_read(is, ver);
00056 if (ver != 1)
00057 return;
00058 vsl_b_read(is, itype);
00059 }
00060 int nbins;
00061 T min, max, min_prob;
00062 vcl_vector<T> values, counts;
00063 vsl_b_read(is, nbins);
00064 vsl_b_read(is, min);
00065 vsl_b_read(is, max);
00066 vsl_b_read(is, min_prob);
00067 vsl_b_read(is, values);
00068 vsl_b_read(is, counts);
00069 bsta_histogram<T> temp(min, max, nbins, min_prob);
00070 for (unsigned i = 0; i<static_cast<unsigned>(nbins); ++i)
00071 temp.upcount(values[i], counts[i]);
00072 h = temp;
00073 }
00074
00075
00076 template <class T>
00077 void
00078 vsl_print_summary(vcl_ostream &os, const bsta_histogram<T>& h)
00079 {
00080 os << "bsta_histogram\n";
00081 h.print(os);
00082 }
00083
00084
00085 template <class T>
00086 void
00087 vsl_b_write(vsl_b_ostream &os, const bsta_joint_histogram<T>& h)
00088 {
00089 int nbins_a = h.nbins_a();
00090 int nbins_b = h.nbins_b();
00091 T min_a = h.min_a();
00092 T max_a = h.max_a();
00093 T min_b = h.min_b();
00094 T max_b = h.max_b();
00095 T min_prob = h.min_prob();
00096 vbl_array_2d<T> counts = h.counts();
00097 const short io_version_no = 1;
00098 vsl_b_write(os, io_version_no);
00099 vsl_b_write(os, nbins_a);
00100 vsl_b_write(os, min_a);
00101 vsl_b_write(os, max_a);
00102 vsl_b_write(os, nbins_b);
00103 vsl_b_write(os, min_b);
00104 vsl_b_write(os, max_b);
00105 vsl_b_write(os, min_prob);
00106 vsl_b_write(os, counts);
00107 }
00108
00109
00110 template <class T>
00111 void
00112 vsl_b_read(vsl_b_istream &is, bsta_joint_histogram<T>& h)
00113 {
00114 short ver;
00115 vsl_b_read(is, ver);
00116 if (ver != 1)
00117 return;
00118 int nbins_a, nbins_b;
00119 T min_a, max_a, min_b, max_b, min_prob;
00120 vbl_array_2d<T> counts;
00121 vsl_b_read(is, nbins_a);
00122 vsl_b_read(is, min_a);
00123 vsl_b_read(is, max_a);
00124 vsl_b_read(is, nbins_b);
00125 vsl_b_read(is, min_b);
00126 vsl_b_read(is, max_b);
00127 vsl_b_read(is, min_prob);
00128 vsl_b_read(is, counts);
00129 bsta_joint_histogram<T> temp(min_a, max_a, nbins_a, min_b, max_b, nbins_b, min_prob);
00130 unsigned nr = counts.rows(), nc = counts.cols();
00131 for (unsigned r = 0; r<nr; ++r)
00132 for (unsigned c = 0; c<nc; ++c)
00133 temp.set_count(r, c, counts[r][c]);
00134 h = temp;
00135 }
00136
00137
00138 template <class T>
00139 void
00140 vsl_print_summary(vcl_ostream &os, const bsta_joint_histogram<T>& h)
00141 {
00142 os << "bsta_joint_histogram\n";
00143 h.print(os);
00144 }
00145
00146
00147 void vsl_b_write(vsl_b_ostream &os, const bsta_histogram_sptr& hptr)
00148 {
00149 bsta_histogram<float>* hf = dynamic_cast<bsta_histogram<float>*>(hptr.ptr());
00150 if (hf) { vsl_b_write(os, *hf); return; }
00151 bsta_histogram<double>* hd=dynamic_cast<bsta_histogram<double>*>(hptr.ptr());
00152 if (hd) { vsl_b_write(os, *hd); return; }
00153 }
00154
00155 void vsl_b_read(vsl_b_istream &is, bsta_histogram_sptr& hptr)
00156 {
00157 short ver;
00158 vsl_b_read(is, ver);
00159 if (ver != 1)
00160 return;
00161 hptr = 0;
00162 int itype = 0;
00163 vsl_b_read(is, itype);
00164 bsta_histogram_base::bsta_hist_type type =
00165 static_cast<bsta_histogram_base::bsta_hist_type>(itype);
00166 if (type == bsta_histogram_base::HIST_TYPE_UNKNOWN) return;
00167 if (type == bsta_histogram_base::HIST_TYPE_FLOAT)
00168 {
00169 bsta_histogram<float> h;
00170 vsl_b_read(is, h, true);
00171 hptr = new bsta_histogram<float>(h);
00172 return;
00173 }
00174 if (type == bsta_histogram_base::HIST_TYPE_DOUBLE)
00175 {
00176 bsta_histogram<double> h;
00177 vsl_b_read(is, h, true);
00178 hptr = new bsta_histogram<double>(h);
00179 return;
00180 }
00181 }
00182
00183 void vsl_b_write(vsl_b_ostream &os, const bsta_joint_histogram_sptr& );
00184
00185 void vsl_b_read(vsl_b_istream &is, bsta_joint_histogram_sptr& );
00186
00187 #endif // bsta_io_histogram_h_