saving.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #ifndef OPENCV_FLANN_SAVING_H_
  29. #define OPENCV_FLANN_SAVING_H_
  30. #include <cstring>
  31. #include <vector>
  32. #include "general.h"
  33. #include "nn_index.h"
  34. #ifdef FLANN_SIGNATURE_
  35. #undef FLANN_SIGNATURE_
  36. #endif
  37. #define FLANN_SIGNATURE_ "FLANN_INDEX"
  38. namespace cvflann
  39. {
  40. template <typename T>
  41. struct Datatype {};
  42. template<>
  43. struct Datatype<char> { static flann_datatype_t type() { return FLANN_INT8; } };
  44. template<>
  45. struct Datatype<short> { static flann_datatype_t type() { return FLANN_INT16; } };
  46. template<>
  47. struct Datatype<int> { static flann_datatype_t type() { return FLANN_INT32; } };
  48. template<>
  49. struct Datatype<unsigned char> { static flann_datatype_t type() { return FLANN_UINT8; } };
  50. template<>
  51. struct Datatype<unsigned short> { static flann_datatype_t type() { return FLANN_UINT16; } };
  52. template<>
  53. struct Datatype<unsigned int> { static flann_datatype_t type() { return FLANN_UINT32; } };
  54. template<>
  55. struct Datatype<float> { static flann_datatype_t type() { return FLANN_FLOAT32; } };
  56. template<>
  57. struct Datatype<double> { static flann_datatype_t type() { return FLANN_FLOAT64; } };
  58. /**
  59. * Structure representing the index header.
  60. */
  61. struct IndexHeader
  62. {
  63. char signature[16];
  64. char version[16];
  65. flann_datatype_t data_type;
  66. flann_algorithm_t index_type;
  67. size_t rows;
  68. size_t cols;
  69. };
  70. /**
  71. * Saves index header to stream
  72. *
  73. * @param stream - Stream to save to
  74. * @param index - The index to save
  75. */
  76. template<typename Distance>
  77. void save_header(FILE* stream, const NNIndex<Distance>& index)
  78. {
  79. IndexHeader header;
  80. memset(header.signature, 0, sizeof(header.signature));
  81. strcpy(header.signature, FLANN_SIGNATURE_);
  82. memset(header.version, 0, sizeof(header.version));
  83. strcpy(header.version, FLANN_VERSION_);
  84. header.data_type = Datatype<typename Distance::ElementType>::type();
  85. header.index_type = index.getType();
  86. header.rows = index.size();
  87. header.cols = index.veclen();
  88. std::fwrite(&header, sizeof(header),1,stream);
  89. }
  90. /**
  91. *
  92. * @param stream - Stream to load from
  93. * @return Index header
  94. */
  95. inline IndexHeader load_header(FILE* stream)
  96. {
  97. IndexHeader header;
  98. size_t read_size = fread(&header,sizeof(header),1,stream);
  99. if (read_size!=(size_t)1) {
  100. throw FLANNException("Invalid index file, cannot read");
  101. }
  102. if (strcmp(header.signature,FLANN_SIGNATURE_)!=0) {
  103. throw FLANNException("Invalid index file, wrong signature");
  104. }
  105. return header;
  106. }
  107. template<typename T>
  108. void save_value(FILE* stream, const T& value, size_t count = 1)
  109. {
  110. fwrite(&value, sizeof(value),count, stream);
  111. }
  112. template<typename T>
  113. void save_value(FILE* stream, const cvflann::Matrix<T>& value)
  114. {
  115. fwrite(&value, sizeof(value),1, stream);
  116. fwrite(value.data, sizeof(T),value.rows*value.cols, stream);
  117. }
  118. template<typename T>
  119. void save_value(FILE* stream, const std::vector<T>& value)
  120. {
  121. size_t size = value.size();
  122. fwrite(&size, sizeof(size_t), 1, stream);
  123. fwrite(&value[0], sizeof(T), size, stream);
  124. }
  125. template<typename T>
  126. void load_value(FILE* stream, T& value, size_t count = 1)
  127. {
  128. size_t read_cnt = fread(&value, sizeof(value), count, stream);
  129. if (read_cnt != count) {
  130. throw FLANNException("Cannot read from file");
  131. }
  132. }
  133. template<typename T>
  134. void load_value(FILE* stream, cvflann::Matrix<T>& value)
  135. {
  136. size_t read_cnt = fread(&value, sizeof(value), 1, stream);
  137. if (read_cnt != 1) {
  138. throw FLANNException("Cannot read from file");
  139. }
  140. value.data = new T[value.rows*value.cols];
  141. read_cnt = fread(value.data, sizeof(T), value.rows*value.cols, stream);
  142. if (read_cnt != (size_t)(value.rows*value.cols)) {
  143. throw FLANNException("Cannot read from file");
  144. }
  145. }
  146. template<typename T>
  147. void load_value(FILE* stream, std::vector<T>& value)
  148. {
  149. size_t size;
  150. size_t read_cnt = fread(&size, sizeof(size_t), 1, stream);
  151. if (read_cnt!=1) {
  152. throw FLANNException("Cannot read from file");
  153. }
  154. value.resize(size);
  155. read_cnt = fread(&value[0], sizeof(T), size, stream);
  156. if (read_cnt != size) {
  157. throw FLANNException("Cannot read from file");
  158. }
  159. }
  160. }
  161. #endif /* OPENCV_FLANN_SAVING_H_ */