00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _OPENCV_SAVING_H_
00030 #define _OPENCV_SAVING_H_
00031
00032 #include "opencv2/flann/general.h"
00033 #include "opencv2/flann/nn_index.h"
00034 #include <cstdio>
00035 #include <cstring>
00036
00037 namespace cvflann
00038 {
00039 template <typename T> struct Datatype {};
00040 template<> struct Datatype<char> { static flann_datatype_t type() { return INT8; } };
00041 template<> struct Datatype<short> { static flann_datatype_t type() { return INT16; } };
00042 template<> struct Datatype<int> { static flann_datatype_t type() { return INT32; } };
00043 template<> struct Datatype<unsigned char> { static flann_datatype_t type() { return UINT8; } };
00044 template<> struct Datatype<unsigned short> { static flann_datatype_t type() { return UINT16; } };
00045 template<> struct Datatype<unsigned int> { static flann_datatype_t type() { return UINT32; } };
00046 template<> struct Datatype<float> { static flann_datatype_t type() { return FLOAT32; } };
00047 template<> struct Datatype<double> { static flann_datatype_t type() { return FLOAT64; } };
00048
00049
00050 CV_EXPORTS const char* FLANN_SIGNATURE();
00051 CV_EXPORTS const char* FLANN_VERSION();
00052
00056 struct CV_EXPORTS IndexHeader
00057 {
00058 char signature[16];
00059 char version[16];
00060 flann_datatype_t data_type;
00061 flann_algorithm_t index_type;
00062 int rows;
00063 int cols;
00064 };
00065
00072 template<typename ELEM_TYPE>
00073 void save_header(FILE* stream, const NNIndex<ELEM_TYPE>& index)
00074 {
00075 IndexHeader header;
00076 memset(header.signature, 0 , sizeof(header.signature));
00077 strcpy(header.signature, FLANN_SIGNATURE());
00078 memset(header.version, 0 , sizeof(header.version));
00079 strcpy(header.version, FLANN_VERSION());
00080 header.data_type = Datatype<ELEM_TYPE>::type();
00081 header.index_type = index.getType();
00082 header.rows = (int)index.size();
00083 header.cols = index.veclen();
00084
00085 std::fwrite(&header, sizeof(header),1,stream);
00086 }
00087
00088
00094 CV_EXPORTS IndexHeader load_header(FILE* stream);
00095
00096
00097 template<typename T>
00098 void save_value(FILE* stream, const T& value, int count = 1)
00099 {
00100 fwrite(&value, 1, sizeof(value)*count, stream);
00101 }
00102
00103
00104 template<typename T>
00105 void load_value(FILE* stream, T& value, int count = 1)
00106 {
00107 int read_cnt = (int)fread(&value, sizeof(value),count, stream);
00108 if (read_cnt!=count) {
00109 throw FLANNException("Cannot read from file");
00110 }
00111 }
00112
00113 }
00114
00115 #endif