00001 /*********************************************************************** 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved. 00005 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * 1. Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE NNIndexGOODS OR SERVICES; LOSS OF USE, 00023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 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 FLANN_INT8; } }; 00041 template<> struct Datatype<short> { static flann_datatype_t type() { return FLANN_INT16; } }; 00042 template<> struct Datatype<int> { static flann_datatype_t type() { return FLANN_INT32; } }; 00043 template<> struct Datatype<unsigned char> { static flann_datatype_t type() { return FLANN_UINT8; } }; 00044 template<> struct Datatype<unsigned short> { static flann_datatype_t type() { return FLANN_UINT16; } }; 00045 template<> struct Datatype<unsigned int> { static flann_datatype_t type() { return FLANN_UINT32; } }; 00046 template<> struct Datatype<float> { static flann_datatype_t type() { return FLANN_FLOAT32; } }; 00047 template<> struct Datatype<double> { static flann_datatype_t type() { return FLANN_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 } // namespace cvflann 00114 00115 #endif /* _OPENCV_SAVING_H_ */