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>
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     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 
~DictValue()215 inline DictValue::~DictValue()
216 {
217     release();
218 }
219 
operator =(const DictValue & r)220 inline DictValue & DictValue::operator=(const DictValue &r)
221 {
222     if (&r == this)
223         return *this;
224 
225     if (r.type == Param::INT)
226     {
227         AutoBuffer<int64, 1> *tmp = new AutoBuffer<int64, 1>(*r.pi);
228         release();
229         pi = tmp;
230     }
231     else if (r.type == Param::STRING)
232     {
233         AutoBuffer<String, 1> *tmp = new AutoBuffer<String, 1>(*r.ps);
234         release();
235         ps = tmp;
236     }
237     else if (r.type == Param::REAL)
238     {
239         AutoBuffer<double, 1> *tmp = new AutoBuffer<double, 1>(*r.pd);
240         release();
241         pd = tmp;
242     }
243 
244     type = r.type;
245 
246     return *this;
247 }
248 
DictValue(const DictValue & r)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 
isString() const262 inline bool DictValue::isString() const
263 {
264     return (type == Param::STRING);
265 }
266 
isInt() const267 inline bool DictValue::isInt() const
268 {
269     return (type == Param::INT);
270 }
271 
isReal() const272 inline bool DictValue::isReal() const
273 {
274     return (type == Param::REAL || type == Param::INT);
275 }
276 
size() const277 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 
operator <<(std::ostream & stream,const DictValue & dictv)301 inline std::ostream &operator<<(std::ostream &stream, const DictValue &dictv)
302 {
303     int i;
304 
305     if (dictv.isInt())
306     {
307         for (i = 0; i < dictv.size() - 1; i++)
308             stream << dictv.get<int64>(i) << ", ";
309         stream << dictv.get<int64>(i);
310     }
311     else if (dictv.isReal())
312     {
313         for (i = 0; i < dictv.size() - 1; i++)
314             stream << dictv.get<double>(i) << ", ";
315         stream << dictv.get<double>(i);
316     }
317     else if (dictv.isString())
318     {
319         for (i = 0; i < dictv.size() - 1; i++)
320             stream << "\"" << dictv.get<String>(i) << "\", ";
321         stream << dictv.get<String>(i);
322     }
323 
324     return stream;
325 }
326 
327 /////////////////////////////////////////////////////////////////
328 
has(const String & key) const329 inline bool Dict::has(const String &key) const
330 {
331     return dict.count(key) != 0;
332 }
333 
ptr(const String & key)334 inline DictValue *Dict::ptr(const String &key)
335 {
336     _Dict::iterator i = dict.find(key);
337     return (i == dict.end()) ? NULL : &i->second;
338 }
339 
ptr(const String & key) const340 inline const DictValue *Dict::ptr(const String &key) const
341 {
342     _Dict::const_iterator i = dict.find(key);
343     return (i == dict.end()) ? NULL : &i->second;
344 }
345 
get(const String & key) const346 inline const DictValue &Dict::get(const String &key) const
347 {
348     _Dict::const_iterator i = dict.find(key);
349     if (i == dict.end())
350         CV_Error(Error::StsObjectNotFound, "Required argument \"" + key + "\" not found into dictionary");
351     return i->second;
352 }
353 
354 template <typename T>
get(const String & key) const355 inline T Dict::get(const String &key) const
356 {
357     return this->get(key).get<T>();
358 }
359 
360 template <typename T>
get(const String & key,const T & defaultValue) const361 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>
set(const String & key,const T & value)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 
erase(const String & key)384 inline void Dict::erase(const String &key)
385 {
386     dict.erase(key);
387 }
388 
operator <<(std::ostream & stream,const Dict & dict)389 inline std::ostream &operator<<(std::ostream &stream, const Dict &dict)
390 {
391     Dict::_Dict::const_iterator it;
392     for (it = dict.dict.begin(); it != dict.dict.end(); it++)
393         stream << it->first << " : " << it->second << "\n";
394 
395     return stream;
396 }
397 
begin() const398 inline std::map<String, DictValue>::const_iterator Dict::begin() const
399 {
400     return dict.begin();
401 }
402 
end() const403 inline std::map<String, DictValue>::const_iterator Dict::end() const
404 {
405     return dict.end();
406 }
407 
408 CV__DNN_INLINE_NS_END
409 }
410 }
411 
412 #endif
413