saving.h 5.8 KB

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