saving.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5  * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE NNIndexGOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *************************************************************************/
28 
29 #ifndef OPENCV_FLANN_SAVING_H_
30 #define OPENCV_FLANN_SAVING_H_
31 
32 #include <cstring>
33 #include <vector>
34 
35 #include "general.h"
36 #include "nn_index.h"
37 
38 #ifdef FLANN_SIGNATURE_
39 #undef FLANN_SIGNATURE_
40 #endif
41 #define FLANN_SIGNATURE_ "FLANN_INDEX"
42 
43 namespace cvflann
44 {
45 
46 template <typename T>
47 struct Datatype {};
48 template<>
49 struct Datatype<char> { static flann_datatype_t type() { return FLANN_INT8; } };
50 template<>
51 struct Datatype<short> { static flann_datatype_t type() { return FLANN_INT16; } };
52 template<>
53 struct Datatype<int> { static flann_datatype_t type() { return FLANN_INT32; } };
54 template<>
55 struct Datatype<unsigned char> { static flann_datatype_t type() { return FLANN_UINT8; } };
56 template<>
57 struct Datatype<unsigned short> { static flann_datatype_t type() { return FLANN_UINT16; } };
58 template<>
59 struct Datatype<unsigned int> { static flann_datatype_t type() { return FLANN_UINT32; } };
60 template<>
61 struct Datatype<float> { static flann_datatype_t type() { return FLANN_FLOAT32; } };
62 template<>
63 struct Datatype<double> { static flann_datatype_t type() { return FLANN_FLOAT64; } };
64 
65 
70 {
71  char signature[16];
72  char version[16];
75  size_t rows;
76  size_t cols;
77 };
78 
85 template<typename Distance>
86 void save_header(FILE* stream, const NNIndex<Distance>& index)
87 {
89  memset(header.signature, 0, sizeof(header.signature));
90  strcpy(header.signature, FLANN_SIGNATURE_);
91  memset(header.version, 0, sizeof(header.version));
92  strcpy(header.version, FLANN_VERSION_);
94  header.index_type = index.getType();
95  header.rows = index.size();
96  header.cols = index.veclen();
97 
98  std::fwrite(&header, sizeof(header),1,stream);
99 }
100 
101 
107 inline IndexHeader load_header(FILE* stream)
108 {
110  size_t read_size = fread(&header,sizeof(header),1,stream);
111 
112  if (read_size!=(size_t)1) {
113  throw FLANNException("Invalid index file, cannot read");
114  }
115 
116  if (strcmp(header.signature,FLANN_SIGNATURE_)!=0) {
117  throw FLANNException("Invalid index file, wrong signature");
118  }
119 
120  return header;
121 
122 }
123 
124 
125 template<typename T>
126 void save_value(FILE* stream, const T& value, size_t count = 1)
127 {
128  fwrite(&value, sizeof(value),count, stream);
129 }
130 
131 template<typename T>
132 void save_value(FILE* stream, const cvflann::Matrix<T>& value)
133 {
134  fwrite(&value, sizeof(value),1, stream);
135  fwrite(value.data, sizeof(T),value.rows*value.cols, stream);
136 }
137 
138 template<typename T>
139 void save_value(FILE* stream, const std::vector<T>& value)
140 {
141  size_t size = value.size();
142  fwrite(&size, sizeof(size_t), 1, stream);
143  fwrite(&value[0], sizeof(T), size, stream);
144 }
145 
146 template<typename T>
147 void load_value(FILE* stream, T& value, size_t count = 1)
148 {
149  size_t read_cnt = fread(&value, sizeof(value), count, stream);
150  if (read_cnt != count) {
151  throw FLANNException("Cannot read from file");
152  }
153 }
154 
155 template<typename T>
156 void load_value(FILE* stream, cvflann::Matrix<T>& value)
157 {
158  size_t read_cnt = fread(&value, sizeof(value), 1, stream);
159  if (read_cnt != 1) {
160  throw FLANNException("Cannot read from file");
161  }
162  value.data = new T[value.rows*value.cols];
163  read_cnt = fread(value.data, sizeof(T), value.rows*value.cols, stream);
164  if (read_cnt != (size_t)(value.rows*value.cols)) {
165  throw FLANNException("Cannot read from file");
166  }
167 }
168 
169 
170 template<typename T>
171 void load_value(FILE* stream, std::vector<T>& value)
172 {
173  size_t size;
174  size_t read_cnt = fread(&size, sizeof(size_t), 1, stream);
175  if (read_cnt!=1) {
176  throw FLANNException("Cannot read from file");
177  }
178  value.resize(size);
179  read_cnt = fread(&value[0], sizeof(T), size, stream);
180  if (read_cnt != size) {
181  throw FLANNException("Cannot read from file");
182  }
183 }
184 
185 }
186 
187 #endif /* OPENCV_FLANN_SAVING_H_ */
Definition: saving.h:69
void save_header(FILE *stream, const NNIndex< Distance > &index)
Definition: saving.h:86
flann_algorithm_t
Definition: defines.h:81
short float uchar uchar uchar uchar uchar ushort int uchar ushort int float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float int int int float int int int float int CV_CUDEV_IMPLEMENT_VEC_BINARY_OP char CV_CUDEV_IMPLEMENT_VEC_BINARY_OP ushort CV_CUDEV_IMPLEMENT_VEC_BINARY_OP short CV_CUDEV_IMPLEMENT_VEC_BINARY_OP int CV_CUDEV_IMPLEMENT_VEC_BINARY_OP uint CV_CUDEV_IMPLEMENT_VEC_BINARY_OP float CV_CUDEV_IMPLEMENT_VEC_BINARY_OP double char
Definition: vec_math.hpp:426
flann_datatype_t data_type
Definition: saving.h:73
virtual flann_algorithm_t getType() const =0
Definition: defines.h:161
flann_algorithm_t index_type
Definition: saving.h:74
size_t cols
Definition: matrix.h:52
virtual size_t size() const =0
CvSize size
Definition: calib3d.hpp:212
GLuint index
Definition: core_c.h:986
static flann_datatype_t type()
Definition: saving.h:53
Definition: general.h:41
const CvMat const CvMat const CvMat CvMat CvMat CvMat CvMat CvSize CvMat CvMat * T
Definition: calib3d.hpp:270
Definition: matrix.h:46
static flann_datatype_t type()
Definition: saving.h:63
Definition: defines.h:157
static flann_datatype_t type()
Definition: saving.h:61
size_t cols
Definition: saving.h:76
char version[16]
Definition: saving.h:72
flann_datatype_t
Definition: defines.h:154
IndexHeader load_header(FILE *stream)
Definition: saving.h:107
static flann_datatype_t type()
Definition: saving.h:57
Definition: saving.h:47
GLuint GLuint GLsizei count
Definition: core_c.h:973
Definition: defines.h:165
void load_value(FILE *stream, T &value, size_t count=1)
Definition: saving.h:147
static flann_datatype_t type()
Definition: saving.h:51
char signature[16]
Definition: saving.h:71
Definition: defines.h:164
GLsizei const GLfloat * value
Definition: core_c.h:341
T * data
Definition: matrix.h:54
int int type
Definition: core_c.h:109
size_t rows
Definition: saving.h:75
static flann_datatype_t type()
Definition: saving.h:49
Definition: defines.h:156
Definition: defines.h:160
Definition: defines.h:158
Definition: nn_index.h:48
size_t rows
Definition: matrix.h:51
static flann_datatype_t type()
Definition: saving.h:55
::max::max::max float
Definition: functional.hpp:326
::max::max int
Definition: functional.hpp:324
Definition: defines.h:162
short
Definition: vec_math.hpp:153
static flann_datatype_t type()
Definition: saving.h:59
CvMat * header
Definition: core_c.h:361
void save_value(FILE *stream, const T &value, size_t count=1)
Definition: saving.h:126
virtual size_t veclen() const =0
GLsizeiptr size
Definition: core_c.h:939
short float uchar uchar uchar uchar uchar ushort int uchar ushort int float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float float int int int float int int int float int CV_CUDEV_IMPLEMENT_VEC_BINARY_OP char CV_CUDEV_IMPLEMENT_VEC_BINARY_OP ushort CV_CUDEV_IMPLEMENT_VEC_BINARY_OP short CV_CUDEV_IMPLEMENT_VEC_BINARY_OP int CV_CUDEV_IMPLEMENT_VEC_BINARY_OP uint CV_CUDEV_IMPLEMENT_VEC_BINARY_OP float CV_CUDEV_IMPLEMENT_VEC_BINARY_OP double int int uint double
Definition: vec_math.hpp:432