Coverage Report

Created: 2023-06-07 08:11

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