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_EXPERIMENTAL_NS_BEGIN
50 
51 template<typename TypeIter>
arrayInt(TypeIter begin,int size)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>
arrayReal(TypeIter begin,int size)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>
arrayString(TypeIter begin,int size)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<>
get(int idx) const79 inline DictValue DictValue::get<DictValue>(int idx) const
80 {
81     CV_Assert(idx == -1);
82     return *this;
83 }
84 
85 template<>
get(int idx) const86 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<>
get(int idx) const117 inline int DictValue::get<int>(int idx) const
118 {
119     return (int)get<int64>(idx);
120 }
121 
getIntValue(int idx) const122 inline int DictValue::getIntValue(int idx) const
123 {
124     return (int)get<int64>(idx);
125 }
126 
127 template<>
get(int idx) const128 inline unsigned DictValue::get<unsigned>(int idx) const
129 {
130     return (unsigned)get<int64>(idx);
131 }
132 
133 template<>
get(int idx) const134 inline bool DictValue::get<bool>(int idx) const
135 {
136     return (get<int64>(idx) != 0);
137 }
138 
139 template<>
get(int idx) const140 inline double DictValue::get<double>(int idx) const
141 {
142     CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size()));
143     idx = (idx == -1) ? 0 : idx;
144 
145     if (type == Param::REAL)
146     {
147         return (*pd)[idx];
148     }
149     else if (type == Param::INT)
150     {
151         return (double)(*pi)[idx];
152     }
153     else if (type == Param::STRING)
154     {
155         return std::atof((*ps)[idx].c_str());
156     }
157     else
158     {
159         CV_Assert(isReal() || isInt() || isString());
160         return 0;
161     }
162 }
163 
getRealValue(int idx) const164 inline double DictValue::getRealValue(int idx) const
165 {
166     return get<double>(idx);
167 }
168 
169 template<>
get(int idx) const170 inline float DictValue::get<float>(int idx) const
171 {
172     return (float)get<double>(idx);
173 }
174 
175 template<>
get(int idx) const176 inline String DictValue::get<String>(int idx) const
177 {
178     CV_Assert(isString());
179     CV_Assert((idx == -1 && ps->size() == 1) || (idx >= 0 && idx < (int)ps->size()));
180     return (*ps)[(idx == -1) ? 0 : idx];
181 }
182 
183 
getStringValue(int idx) const184 inline String DictValue::getStringValue(int idx) const
185 {
186     return get<String>(idx);
187 }
188 
release()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     }
203 }
204 
~DictValue()205 inline DictValue::~DictValue()
206 {
207     release();
208 }
209 
operator =(const DictValue & r)210 inline DictValue & DictValue::operator=(const DictValue &r)
211 {
212     if (&r == this)
213         return *this;
214 
215     if (r.type == Param::INT)
216     {
217         AutoBuffer<int64, 1> *tmp = new AutoBuffer<int64, 1>(*r.pi);
218         release();
219         pi = tmp;
220     }
221     else if (r.type == Param::STRING)
222     {
223         AutoBuffer<String, 1> *tmp = new AutoBuffer<String, 1>(*r.ps);
224         release();
225         ps = tmp;
226     }
227     else if (r.type == Param::REAL)
228     {
229         AutoBuffer<double, 1> *tmp = new AutoBuffer<double, 1>(*r.pd);
230         release();
231         pd = tmp;
232     }
233 
234     type = r.type;
235 
236     return *this;
237 }
238 
DictValue(const DictValue & r)239 inline DictValue::DictValue(const DictValue &r)
240 {
241     type = r.type;
242 
243     if (r.type == Param::INT)
244         pi = new AutoBuffer<int64, 1>(*r.pi);
245     else if (r.type == Param::STRING)
246         ps = new AutoBuffer<String, 1>(*r.ps);
247     else if (r.type == Param::REAL)
248         pd = new AutoBuffer<double, 1>(*r.pd);
249 }
250 
isString() const251 inline bool DictValue::isString() const
252 {
253     return (type == Param::STRING);
254 }
255 
isInt() const256 inline bool DictValue::isInt() const
257 {
258     return (type == Param::INT);
259 }
260 
isReal() const261 inline bool DictValue::isReal() const
262 {
263     return (type == Param::REAL || type == Param::INT);
264 }
265 
size() const266 inline int DictValue::size() const
267 {
268     switch (type)
269     {
270     case Param::INT:
271         return (int)pi->size();
272     case Param::STRING:
273         return (int)ps->size();
274     case Param::REAL:
275         return (int)pd->size();
276     }
277 #ifdef __OPENCV_BUILD
278     CV_Error(Error::StsInternal, "");
279 #else
280     CV_ErrorNoReturn(Error::StsInternal, "");
281 #endif
282 }
283 
operator <<(std::ostream & stream,const DictValue & dictv)284 inline std::ostream &operator<<(std::ostream &stream, const DictValue &dictv)
285 {
286     int i;
287 
288     if (dictv.isInt())
289     {
290         for (i = 0; i < dictv.size() - 1; i++)
291             stream << dictv.get<int64>(i) << ", ";
292         stream << dictv.get<int64>(i);
293     }
294     else if (dictv.isReal())
295     {
296         for (i = 0; i < dictv.size() - 1; i++)
297             stream << dictv.get<double>(i) << ", ";
298         stream << dictv.get<double>(i);
299     }
300     else if (dictv.isString())
301     {
302         for (i = 0; i < dictv.size() - 1; i++)
303             stream << "\"" << dictv.get<String>(i) << "\", ";
304         stream << dictv.get<String>(i);
305     }
306 
307     return stream;
308 }
309 
310 /////////////////////////////////////////////////////////////////
311 
has(const String & key) const312 inline bool Dict::has(const String &key) const
313 {
314     return dict.count(key) != 0;
315 }
316 
ptr(const String & key)317 inline DictValue *Dict::ptr(const String &key)
318 {
319     _Dict::iterator i = dict.find(key);
320     return (i == dict.end()) ? NULL : &i->second;
321 }
322 
ptr(const String & key) const323 inline const DictValue *Dict::ptr(const String &key) const
324 {
325     _Dict::const_iterator i = dict.find(key);
326     return (i == dict.end()) ? NULL : &i->second;
327 }
328 
get(const String & key) const329 inline const DictValue &Dict::get(const String &key) const
330 {
331     _Dict::const_iterator i = dict.find(key);
332     if (i == dict.end())
333         CV_Error(Error::StsObjectNotFound, "Required argument \"" + key + "\" not found into dictionary");
334     return i->second;
335 }
336 
337 template <typename T>
get(const String & key) const338 inline T Dict::get(const String &key) const
339 {
340     return this->get(key).get<T>();
341 }
342 
343 template <typename T>
get(const String & key,const T & defaultValue) const344 inline T Dict::get(const String &key, const T &defaultValue) const
345 {
346     _Dict::const_iterator i = dict.find(key);
347 
348     if (i != dict.end())
349         return i->second.get<T>();
350     else
351         return defaultValue;
352 }
353 
354 template<typename T>
set(const String & key,const T & value)355 inline const T &Dict::set(const String &key, const T &value)
356 {
357     _Dict::iterator i = dict.find(key);
358 
359     if (i != dict.end())
360         i->second = DictValue(value);
361     else
362         dict.insert(std::make_pair(key, DictValue(value)));
363 
364     return value;
365 }
366 
erase(const String & key)367 inline void Dict::erase(const String &key)
368 {
369     dict.erase(key);
370 }
371 
operator <<(std::ostream & stream,const Dict & dict)372 inline std::ostream &operator<<(std::ostream &stream, const Dict &dict)
373 {
374     Dict::_Dict::const_iterator it;
375     for (it = dict.dict.begin(); it != dict.dict.end(); it++)
376         stream << it->first << " : " << it->second << "\n";
377 
378     return stream;
379 }
380 
begin() const381 inline std::map<String, DictValue>::const_iterator Dict::begin() const
382 {
383     return dict.begin();
384 }
385 
end() const386 inline std::map<String, DictValue>::const_iterator Dict::end() const
387 {
388     return dict.end();
389 }
390 
391 CV__DNN_EXPERIMENTAL_NS_END
392 }
393 }
394 
395 #endif
396