libidx
/home/rex/ebltrunk/core/libidx/include/idxIO.h
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Pierre Sermanet *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source
00014  *       Initiative (http://www.opensource.org) must display the
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef IDXIO_H_
00034 #define IDXIO_H_
00035 
00036 #include "idx.h"
00037 
00038 #ifndef __NOSTL__
00039 #include <iterator>
00040 #include <iostream>
00041 #include <fstream>
00042 using namespace std;
00043 #endif
00044 
00045 // DUMPING /////////////////////////////////////////////////////////////////////
00046 
00047 /* #ifdef __DUMP_STATES__ */
00048 
00049 #ifdef __ANDROID__
00050 #define DUMP_ROOT "/sdcard/"
00051 #else
00052 #define DUMP_ROOT "dump/";
00053 #endif
00054 
00055 extern uint dump_count;
00056 extern std::string dump_prefix;
00057 
00058 #define INIT_DUMP()                             \
00059     uint dump_count = 0;                        \
00060     std::string dump_prefix;
00061 
00062 #define RESET_DUMP() dump_count = 0;
00063 #define DUMP_PREFIX(s) { dump_prefix.clear(); dump_prefix << s << "_"; }
00064 #define DUMP(mat, fname) {                                              \
00065     std::string n = DUMP_ROOT;                                          \
00066     mkdir_full(n);                                                      \
00067     n << dump_prefix << (dump_count < 10? "00": (dump_count < 100? "0":"")) \
00068       << dump_count << "_" << fname << "_" << mat << ".mat";            \
00069     dump_count = dump_count + 1;                                        \
00070     if (save_matrix(mat, n))                                            \
00071       cout << "Dumped " << n << " (min " << idx_min(mat)                \
00072            << " max " << idx_max(mat) << " mean " << idx_mean(mat)      \
00073            << ")" <<endl;                                               \
00074     else                                                                \
00075       cerr << "Failed to dump " << n << endl;                           \
00076   }
00077 
00078 /* #else */
00079 /* #define DUMP(mat, fname) */
00080 /* #endif /\* DUMP_STATES *\/ */
00082 
00083 // standard lush magic numbers
00084 #define MAGIC_FLOAT_MATRIX      0x1e3d4c51
00085 #define MAGIC_PACKED_MATRIX     0x1e3d4c52
00086 #define MAGIC_DOUBLE_MATRIX     0x1e3d4c53
00087 #define MAGIC_INTEGER_MATRIX    0x1e3d4c54
00088 #define MAGIC_BYTE_MATRIX       0x1e3d4c55
00089 #define MAGIC_SHORT_MATRIX      0x1e3d4c56
00090 #define MAGIC_SHORT8_MATRIX     0x1e3d4c57
00091 #define MAGIC_LONG_MATRIX       0x1e3d4c58
00092 #define MAGIC_ASCII_MATRIX      0x2e4d4154
00093 
00094 // non-standard magic numbers
00095 #define MAGIC_UINT_MATRIX       0x1e3d4c59
00096 #define MAGIC_UINT64_MATRIX     0x1e3d4c5a
00097 #define MAGIC_INT64_MATRIX      0x1e3d4c5b
00098 
00099 // pascal vincent's magic numbers
00100 #define MAGIC_UBYTE_VINCENT     0x0800
00101 #define MAGIC_BYTE_VINCENT      0x0900
00102 #define MAGIC_SHORT_VINCENT     0x0B00
00103 #define MAGIC_INT_VINCENT       0x0C00
00104 #define MAGIC_FLOAT_VINCENT     0x0D00
00105 #define MAGIC_DOUBLE_VINCENT    0x0E00
00106 
00107 namespace ebl {
00108   
00109   // TODO: implement all types.
00110   // TODO: is check for endianess required?
00111 
00113   // loading
00114   
00118   template <typename T>
00119     idx<T> load_matrix(const char *filename);
00123   template <typename T>
00124     idx<T> load_matrix(const std::string &filename);
00128   template <typename T>
00129     void load_matrix(idx<T>& m, const char *filename);
00133   template <typename T>
00134     void load_matrix(idx<T>& m, const std::string &filename);
00142   template <typename T>
00143     idx<T> load_matrix(FILE *fp, idx<T> *out);
00150   template <typename T>
00151     midx<T> load_matrices(const std::string &filename, bool ondemand = true);
00152   
00154   // saving
00155   
00158   template <typename T>
00159     bool save_matrix(idx<T>& m, const std::string &filename);
00164   template <typename T2, typename T>
00165     bool save_matrix(idx<T>& m, const std::string &filename);
00168   template <typename T>
00169     bool save_matrix(idx<T>& m, const char *filename);
00173   template <typename T>
00174     bool save_matrix(idx<T>& m, FILE *fp);
00178   template <typename T>
00179     bool save_matrices(midx<T>& m, const std::string &filename);
00183   template <typename T>
00184     bool save_matrices_individually(midx<T>& m, const std::string &root,
00185                                     bool print = false);
00191   template <typename T>
00192     bool save_matrices(midx<T>& m1, midx<T> &m2, const std::string &filename);
00196   template <typename T>
00197     bool save_matrices(std::list<std::string> &filenames,
00198                        const std::string &filename);
00203   template <typename T>
00204     bool save_matrix(std::list<std::string> &filenames,
00205                      const std::string &filename);
00206 
00208   // helper functions
00209   
00215   EXPORT int get_matrix_type(const char *filename, std::string &type);
00218   EXPORT int get_matrix_type(const char *filename);
00220   EXPORT bool is_matrix(const char *filename);
00222   EXPORT bool is_magic_vincent(int magic);
00224   EXPORT bool is_magic(int magic);
00226   EXPORT idxdim get_matrix_dims(const char *filename);
00229   EXPORT idxdim read_matrix_header(FILE *fp, int &magic);
00231   EXPORT bool has_multiple_matrices(const char *filename);
00232 
00233 } // end namespace ebl
00234 
00235 #include "idxIO.hpp"
00236 
00237 #endif /* IDXIO_H_ */
00238