Coverage Report

Created: 2023-06-07 08:11

/work/install-coverage/include/opencv4/opencv2/dnn/dnn.inl.hpp
Line
Count
Source (jump to first uncovered line)
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
//  By downloading, copying, installing or using the software you agree to this license.
6
//  If you do not agree to this license, do not download, install,
7
//  copy or use the software.
8
//
9
//
10
//                           License Agreement
11
//                For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
//   * Redistribution's of source code must retain the above copyright notice,
20
//     this list of conditions and the following disclaimer.
21
//
22
//   * Redistribution's in binary form must reproduce the above copyright notice,
23
//     this list of conditions and the following disclaimer in the documentation
24
//     and/or other materials provided with the distribution.
25
//
26
//   * The name of the copyright holders may not be used to endorse or promote products
27
//     derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#ifndef OPENCV_DNN_DNN_INL_HPP
43
#define OPENCV_DNN_DNN_INL_HPP
44
45
#include <opencv2/dnn.hpp>
46
47
namespace cv {
48
namespace dnn {
49
CV__DNN_INLINE_NS_BEGIN
50
51
template<typename TypeIter>
52
DictValue DictValue::arrayInt(TypeIter begin, int size)
53
{
54
    DictValue res(Param::INT, new AutoBuffer<int64, 1>(size));
55
    for (int j = 0; j < size; begin++, j++)
56
        (*res.pi)[j] = *begin;
57
    return res;
58
}
59
60
template<typename TypeIter>
61
DictValue DictValue::arrayReal(TypeIter begin, int size)
62
{
63
    DictValue res(Param::REAL, new AutoBuffer<double, 1>(size));
64
    for (int j = 0; j < size; begin++, j++)
65
        (*res.pd)[j] = *begin;
66
    return res;
67
}
68
69
template<typename TypeIter>
70
DictValue DictValue::arrayString(TypeIter begin, int size)
71
{
72
    DictValue res(Param::STRING, new AutoBuffer<String, 1>(size));
73
    for (int j = 0; j < size; begin++, j++)
74
        (*res.ps)[j] = *begin;
75
    return res;
76
}
77
78
template<>
79
inline DictValue DictValue::get<DictValue>(int idx) const
80
0
{
81
0
    CV_Assert(idx == -1);
82
0
    return *this;
83
0
}
84
85
template<>
86
inline int64 DictValue::get<int64>(int idx) const
87
{
88
    CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size()));
89
    idx = (idx == -1) ? 0 : idx;
90
91
    if (type == Param::INT)
92
    {
93
        return (*pi)[idx];
94
    }
95
    else if (type == Param::REAL)
96
    {
97
        double doubleValue = (*pd)[idx];
98
99
        double fracpart, intpart;
100
        fracpart = std::modf(doubleValue, &intpart);
101
        CV_Assert(fracpart == 0.0);
102
103
        return (int64)doubleValue;
104
    }
105
    else if (type == Param::STRING)
106
    {
107
        return std::atoi((*ps)[idx].c_str());
108
    }
109
    else
110
    {
111
        CV_Assert(isInt() || isReal() || isString());
112
        return 0;
113
    }
114
}
115
116
template<>
117
inline int DictValue::get<int>(int idx) const
118
0
{
119
0
    return (int)get<int64>(idx);
120
0
}
121
122
inline int DictValue::getIntValue(int idx) const
123
0
{
124
0
    return (int)get<int64>(idx);
125
0
}
126
127
template<>
128
inline unsigned DictValue::get<unsigned>(int idx) const
129
0
{
130
0
    return (unsigned)get<int64>(idx);
131
0
}
132
133
template<>
134
inline bool DictValue::get<bool>(int idx) const
135
{
136
    return (get<int64>(idx) != 0);
137
}
138
139
template<>
140
inline double DictValue::get<double>(int idx) const
141
0
{
142
0
    CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size()));
143
0
    idx = (idx == -1) ? 0 : idx;
144
0
145
0
    if (type == Param::REAL)
146
0
    {
147
0
        return (*pd)[idx];
148
0
    }
149
0
    else if (type == Param::INT)
150
0
    {
151
0
        return (double)(*pi)[idx];
152
0
    }
153
0
    else if (type == Param::STRING)
154
0
    {
155
0
        return std::atof((*ps)[idx].c_str());
156
0
    }
157
0
    else
158
0
    {
159
0
        CV_Assert(isReal() || isInt() || isString());
160
0
        return 0;
161
0
    }
162
0
}
163
164
inline double DictValue::getRealValue(int idx) const
165
0
{
166
0
    return get<double>(idx);
167
0
}
168
169
template<>
170
inline float DictValue::get<float>(int idx) const
171
0
{
172
0
    return (float)get<double>(idx);
173
0
}
174
175
template<>
176
inline String DictValue::get<String>(int idx) const
177
0
{
178
0
    CV_Assert(isString());
179
0
    CV_Assert((idx == -1 && ps->size() == 1) || (idx >= 0 && idx < (int)ps->size()));
180
0
    return (*ps)[(idx == -1) ? 0 : idx];
181
0
}
182
183
184
inline String DictValue::getStringValue(int idx) const
185
0
{
186
0
    return get<String>(idx);
187
0
}
188
189
inline void DictValue::release()
190
{
191
    switch (type)
192
    {
193
    case Param::INT:
194
        delete pi;
195
        break;
196
    case Param::STRING:
197
        delete ps;
198
        break;
199
    case Param::REAL:
200
        delete pd;
201
        break;
202
    case Param::BOOLEAN:
203
    case Param::MAT:
204
    case Param::MAT_VECTOR:
205
    case Param::ALGORITHM:
206
    case Param::FLOAT:
207
    case Param::UNSIGNED_INT:
208
    case Param::UINT64:
209
    case Param::UCHAR:
210
    case Param::SCALAR:
211
        break; // unhandled
212
    }
213
}
214
215
inline DictValue::~DictValue()
216
{
217
    release();
218
}
219
220
inline DictValue & DictValue::operator=(const DictValue &r)
221
0
{
222
0
    if (&r == this)
223
0
        return *this;
224
0
225
0
    if (r.type == Param::INT)
226
0
    {
227
0
        AutoBuffer<int64, 1> *tmp = new AutoBuffer<int64, 1>(*r.pi);
228
0
        release();
229
0
        pi = tmp;
230
0
    }
231
0
    else if (r.type == Param::STRING)
232
0
    {
233
0
        AutoBuffer<String, 1> *tmp = new AutoBuffer<String, 1>(*r.ps);
234
0
        release();
235
0
        ps = tmp;
236
0
    }
237
0
    else if (r.type == Param::REAL)
238
0
    {
239
0
        AutoBuffer<double, 1> *tmp = new AutoBuffer<double, 1>(*r.pd);
240
0
        release();
241
0
        pd = tmp;
242
0
    }
243
0
244
0
    type = r.type;
245
0
246
0
    return *this;
247
0
}
248
249
inline DictValue::DictValue(const DictValue &r)
250
    : pv(NULL)
251
{
252
    type = r.type;
253
254
    if (r.type == Param::INT)
255
        pi = new AutoBuffer<int64, 1>(*r.pi);
256
    else if (r.type == Param::STRING)
257
        ps = new AutoBuffer<String, 1>(*r.ps);
258
    else if (r.type == Param::REAL)
259
        pd = new AutoBuffer<double, 1>(*r.pd);
260
}
261
262
inline bool DictValue::isString() const
263
0
{
264
0
    return (type == Param::STRING);
265
0
}
266
267
inline bool DictValue::isInt() const
268
0
{
269
0
    return (type == Param::INT);
270
0
}
271
272
inline bool DictValue::isReal() const
273
0
{
274
0
    return (type == Param::REAL || type == Param::INT);
275
0
}
276
277
inline int DictValue::size() const
278
{
279
    switch (type)
280
    {
281
    case Param::INT:
282
        return (int)pi->size();
283
    case Param::STRING:
284
        return (int)ps->size();
285
    case Param::REAL:
286
        return (int)pd->size();
287
    case Param::BOOLEAN:
288
    case Param::MAT:
289
    case Param::MAT_VECTOR:
290
    case Param::ALGORITHM:
291
    case Param::FLOAT:
292
    case Param::UNSIGNED_INT:
293
    case Param::UINT64:
294
    case Param::UCHAR:
295
    case Param::SCALAR:
296
        break; // unhandled
297
    }
298
    CV_Error_(Error::StsInternal, ("Unhandled type (%d)", static_cast<int>(type)));
299
}
300
301
inline std::ostream &operator<<(std::ostream &stream, const DictValue &dictv)
302
0
{
303
0
    int i;
304
0
305
0
    if (dictv.isInt())
306
0
    {
307
0
        for (i = 0; i < dictv.size() - 1; i++)
308
0
            stream << dictv.get<int64>(i) << ", ";
309
0
        stream << dictv.get<int64>(i);
310
0
    }
311
0
    else if (dictv.isReal())
312
0
    {
313
0
        for (i = 0; i < dictv.size() - 1; i++)
314
0
            stream << dictv.get<double>(i) << ", ";
315
0
        stream << dictv.get<double>(i);
316
0
    }
317
0
    else if (dictv.isString())
318
0
    {
319
0
        for (i = 0; i < dictv.size() - 1; i++)
320
0
            stream << "\"" << dictv.get<String>(i) << "\", ";
321
0
        stream << dictv.get<String>(i);
322
0
    }
323
0
324
0
    return stream;
325
0
}
326
327
/////////////////////////////////////////////////////////////////
328
329
inline bool Dict::has(const String &key) const
330
{
331
    return dict.count(key) != 0;
332
}
333
334
inline DictValue *Dict::ptr(const String &key)
335
0
{
336
0
    _Dict::iterator i = dict.find(key);
337
0
    return (i == dict.end()) ? NULL : &i->second;
338
0
}
339
340
inline const DictValue *Dict::ptr(const String &key) const
341
0
{
342
0
    _Dict::const_iterator i = dict.find(key);
343
0
    return (i == dict.end()) ? NULL : &i->second;
344
0
}
345
346
inline const DictValue &Dict::get(const String &key) const
347
0
{
348
0
    _Dict::const_iterator i = dict.find(key);
349
0
    if (i == dict.end())
350
0
        CV_Error(Error::StsObjectNotFound, "Required argument \"" + key + "\" not found into dictionary");
351
0
    return i->second;
352
0
}
353
354
template <typename T>
355
inline T Dict::get(const String &key) const
356
{
357
    return this->get(key).get<T>();
358
}
359
360
template <typename T>
361
inline T Dict::get(const String &key, const T &defaultValue) const
362
{
363
    _Dict::const_iterator i = dict.find(key);
364
365
    if (i != dict.end())
366
        return i->second.get<T>();
367
    else
368
        return defaultValue;
369
}
370
371
template<typename T>
372
inline const T &Dict::set(const String &key, const T &value)
373
{
374
    _Dict::iterator i = dict.find(key);
375
376
    if (i != dict.end())
377
        i->second = DictValue(value);
378
    else
379
        dict.insert(std::make_pair(key, DictValue(value)));
380
381
    return value;
382
}
383
384
inline void Dict::erase(const String &key)
385
0
{
386
0
    dict.erase(key);
387
0
}
388
389
inline std::ostream &operator<<(std::ostream &stream, const Dict &dict)
390
0
{
391
0
    Dict::_Dict::const_iterator it;
392
0
    for (it = dict.dict.begin(); it != dict.dict.end(); it++)
393
0
        stream << it->first << " : " << it->second << "\n";
394
0
395
0
    return stream;
396
0
}
397
398
inline std::map<String, DictValue>::const_iterator Dict::begin() const
399
0
{
400
0
    return dict.begin();
401
0
}
402
403
inline std::map<String, DictValue>::const_iterator Dict::end() const
404
0
{
405
0
    return dict.end();
406
0
}
407
408
CV__DNN_INLINE_NS_END
409
}
410
}
411
412
#endif