3DiVi Face SDK  3.25.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups
Context.h
1 
9 #ifndef CONTEXT_H
10 #define CONTEXT_H
11 
12 #ifndef WITHOUT_PROCESSING_BLOCK
13 
14 #if defined(_WIN32)
15 #define NOMINMAX
16 #endif
17 
18 #include <functional>
19 #include <iostream>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 
24 #include <pbio/DllHandle.h>
25 #include <pbio/RawImage.h>
26 #include <pbio/DynamicTemplateIndex.h>
27 
28 
29 namespace pbio {
30 
31 typedef LightSmartPtr<import::DllHandle>::tPtr DHPtr;
32 
33 inline void tdvCheckException(const DHPtr& dll_handle, ContextEH*& out_exception) {
34  if(out_exception)
35  {
36  auto err = Error(dll_handle->TDVException_getErrorCode(out_exception), dll_handle->TDVException_getMessage(out_exception));
37  dll_handle->TDVException_deleteException(out_exception);
38  out_exception = nullptr;
39  throw err;
40  }
41 }
42 
43 class ContextRef;
44 
45 
52 class Context
53 {
54 
55  friend class FacerecService;
56  friend class RawSample;
57 
58  template<bool isConst=false>
59  class ContextArrayIterator
60  {
61  class charsList {
62  public:
63  size_t length_;
64  char** ptr_;
65  charsList(size_t length, char** ptr) : length_(length), ptr_(ptr) {}
66  char* operator[](size_t index) const {
67  return (index<length_) ? *(ptr_+index) : nullptr;
68  }
69  };
70 
71  const Context& base_;
72  size_t length_;
73  size_t index_;
74  Context* curr_;
75  const bool isObj_;
76  std::shared_ptr<charsList> keys_;
77 
78  void charsList_deleter(charsList* ptr) {
79  for(size_t indx=0; indx < ptr->length_; ++indx)
80  base_.dll_handle->TDVContext_freePtr(ptr->ptr_[indx]);
81  base_.dll_handle->TDVContext_freePtr(ptr->ptr_);
82  };
83 
84  public:
85  typedef std::ptrdiff_t difference_type;
86  typedef std::forward_iterator_tag iterator_category;
87  typedef Context value_type;
88  typedef typename std::conditional<isConst, const value_type*, value_type*>::type pointer;
89  typedef typename std::conditional<isConst, const value_type&, value_type&>::type reference;
90  typedef const value_type& const_reference;
91 
92  ContextArrayIterator(const Context& ctx, long long index);
93  ContextArrayIterator(const Context& ctx, const std::string& key);
94 
95  ContextArrayIterator(const ContextArrayIterator& iter);
96  ContextArrayIterator& operator=(const ContextArrayIterator& iter) = delete;
97  ContextArrayIterator(ContextArrayIterator&& iter);
98  ContextArrayIterator& operator=(ContextArrayIterator&& iter) = delete;
99 
100  ~ContextArrayIterator() {
101  if(curr_)
102  delete curr_;
103  }
104 
105  bool operator==(const ContextArrayIterator& other) const {
106  return ((this->base_ == other.base_) && (this->curr_ == other.curr_));
107  }
108 
109  bool operator!=(const ContextArrayIterator& other) const {
110  return (!(this->base_ == other.base_) || !(this->curr_ == other.curr_));
111  }
112 
113  ContextArrayIterator& operator++();
114  ContextArrayIterator operator++(int);
115 
116  reference operator*() {
117  return *curr_;
118  }
119 
120  pointer operator->() {
121  return curr_;
122  }
123 
124  std::string key() const {
125  if (keys_ && index_ < length_)
126  return std::string(keys_->operator[](index_));
127  return std::string();
128  }
129  };
130 
131 public:
132  enum Format
133  {
134  FORMAT_BGR = 0,
135  FORMAT_RGB = 1,
136  FORMAT_BGRA8888 = 2,
137  FORMAT_YUV420 = 3,
138  FORMAT_YUV_NV12 = 4,
139  FORMAT_NV21 = 5,
140  };
141 
142 protected:
143 
144  Context(const DHPtr& dll_handle) : dll_handle(dll_handle), weak_(false), eh_(nullptr) {
145  handle_ = dll_handle->TDVContext_create(&eh_);
146  tdvCheckException(dll_handle, eh_);
147  }
148 
149  Context(const DHPtr& dll_handle, HContext* handle, bool weak = true) : dll_handle(dll_handle), weak_(weak), eh_(nullptr) {
150  if(weak_)
151  handle_ = handle;
152  else
153  {
154  handle_ = dll_handle->TDVContext_clone(handle, &eh_);
155  tdvCheckException(dll_handle, eh_);
156  }
157  }
158 
159  Context(const DHPtr& dll_handle, const uint8_t* data, uint64_t dataSize) :
160  dll_handle(dll_handle),
161  weak_(false),
162  eh_(nullptr)
163  {
164  handle_ = dll_handle->TDVContext_createFromEncodedImage(data, dataSize, &eh_);
165  tdvCheckException(dll_handle, eh_);
166  }
167 
168  Context(const DHPtr& dll_handle, uint8_t* data, int32_t width, int32_t height, Format format, int32_t baseAngle) :
169  dll_handle(dll_handle),
170  weak_(false),
171  eh_(nullptr)
172  {
173  handle_ = dll_handle->TDVContext_createFromFrame(data, width, height, static_cast<int32_t>(format), baseAngle, &eh_);
174  tdvCheckException(dll_handle, eh_);
175  }
176 
177  Context(const DHPtr& dll_handle, const char* path) :
178  dll_handle(dll_handle),
179  weak_(false),
180  eh_(nullptr)
181  {
182  handle_ = dll_handle->TDVContext_createFromJsonFile(path, &eh_);
183  tdvCheckException(dll_handle, eh_);
184  }
185 
186  DHPtr dll_handle;
187  HContext* handle_;
188  bool weak_;
189  mutable ContextEH* eh_;
190 
191 public:
192 
193  typedef ContextRef Ref;
194 
195  virtual ~Context()
196  {
197  if(!weak_) {
198  dll_handle->TDVContext_destroy(handle_, &eh_);
199  if (eh_ && std::uncaught_exception())
200  std::cerr << Error(dll_handle->TDVException_getErrorCode(eh_), dll_handle->TDVException_getMessage(eh_)).what();
201  else
202  tdvCheckException(dll_handle, eh_);
203  }
204  }
205 
206  Context(const Context& other) : dll_handle(other.dll_handle), weak_(false), eh_(nullptr) {
207  handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
208  tdvCheckException(dll_handle, eh_);
209  }
210 
211  Context& operator=(const Context& other) {
212  if (&other != this)
213  {
214  if(weak_)
215  dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
216  else {
217  HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_); // copy-swap
218  tdvCheckException(dll_handle, eh_);
219  std::swap(handle_, copy);
220  dll_handle->TDVContext_destroy(copy, &eh_);
221  }
222  tdvCheckException(dll_handle, eh_);
223  }
224  return *this;
225  }
226 
227  Context(Context&& other) : dll_handle(other.dll_handle), weak_(false), eh_(nullptr) {
228  if(other.weak_)
229  {
230  handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
231  tdvCheckException(dll_handle, eh_);
232  }
233  else
234  {
235  handle_ = other.handle_;
236  other.handle_ = nullptr;
237  other.weak_ = true;
238  }
239  }
240 
241  Context& operator=(Context&& other){
242  if (&other != this)
243  {
244  if(weak_) {
245  dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
246  tdvCheckException(dll_handle, eh_);
247  }
248  else
249  {
250  if(other.weak_)
251  {
252  HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_);
253  tdvCheckException(dll_handle, eh_);
254  std::swap(handle_, copy);
255  dll_handle->TDVContext_destroy(copy, &eh_);
256  tdvCheckException(dll_handle, eh_);
257  }
258  else
259  {
260  handle_ = other.handle_;
261  other.handle_ = nullptr;
262  other.weak_ = true;
263  }
264  }
265  }
266  return *this;
267  }
268 
269  operator ContextRef();
270 
279  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
280  Context& operator=(T&& value) {
281  setValue(std::forward<T>(value));
282  return *this;
283  }
284 
285  bool operator==(const Context& other) const
286  {return this->handle_ == other.handle_;}
287 
296  size_t size() const {
297  size_t lenght = dll_handle->TDVContext_getLength(handle_, &eh_);
298  tdvCheckException(dll_handle, eh_);
299  return lenght;
300  }
301 
302  ContextArrayIterator<> begin(){
303  return ContextArrayIterator<>(*this, 0);
304  }
305 
306  ContextArrayIterator<> end(){
307  return ContextArrayIterator<>(*this, -1);
308  }
309 
310  ContextArrayIterator<true> begin() const {
311  return ContextArrayIterator<true>(*this, 0);
312  }
313 
314  ContextArrayIterator<true> end() const {
315  return ContextArrayIterator<true>(*this, -1);
316  }
317 
318  ContextArrayIterator<true> cbegin() const {
319  return ContextArrayIterator<true>(*this, 0);
320  }
321 
322  ContextArrayIterator<true> cend() const {
323  return ContextArrayIterator<true>(*this, -1);
324  }
325 
334  Ref operator[](const std::string& key);
335  Ref operator[](const char* key);
336 
345  Ref operator[](const int index);
346 
347  const Ref operator[](const std::string& key) const;
348  const Ref operator[](const char* key) const;
349  const Ref operator[](const int index) const;
350 
351  Ref at(const char* key);
352  Ref at(const std::string& key);
353  Ref at(const int index);
354 
355  const Ref at(const char* key) const;
356  const Ref at(const std::string& key) const;
357  const Ref at(const int index) const;
358 
369  bool contains(const std::string& key) const {
370  bool result = dll_handle->TDVContext_contains(handle_, key.data(), &eh_);
371  tdvCheckException(dll_handle, eh_);
372 
373  return result;
374  }
375 
386  bool compare(const Context& other) const {
387  bool result = dll_handle->TDVContext_compare(handle_, other.handle_, &eh_);
388  tdvCheckException(dll_handle, eh_);
389 
390  return result;
391  }
392 
401  std::vector<std::string> getKeys(){
402  size_t length = size();
403 
404  auto keys = dll_handle->TDVContext_getKeys(handle_, length, &eh_);
405  tdvCheckException(dll_handle, eh_);
406 
407  std::vector<std::string> result(length);
408  for (size_t i = 0; i < length; ++i)
409  result.emplace_back(keys[i]);
410 
411  return result;
412  }
413 
414  ContextArrayIterator<> find(const std::string& key) {
415  return ContextArrayIterator<>(*this, key);
416  }
417 
418  ContextArrayIterator<true> find(const std::string& key) const {
419  return ContextArrayIterator<true>(*this, key);
420  }
421 
422  template<typename T>
423  typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type push_back(T&& data) {
424  Context elem(dll_handle);
425  elem.setValue(std::forward<T>(data));
426  // use r-value version of push_back without a copy
427  push_back(std::move(elem));
428  }
429 
438  void push_back(const Context& data) {
439  dll_handle->TDVContext_pushBack(handle_, data.handle_, true, &eh_);
440  tdvCheckException(dll_handle, eh_);
441  }
442 
443  void push_back(Context&& data) {
444  dll_handle->TDVContext_pushBack(handle_, data.handle_, weak_, &eh_); // can not move weak object
445  tdvCheckException(dll_handle, eh_);
446  }
447 
456  double getDouble() const {
457  double ret = dll_handle->TDVContext_getDouble(handle_, &eh_);
458  tdvCheckException(dll_handle, eh_);
459  return ret;
460  }
461 
470  long getLong() const {
471  long ret = dll_handle->TDVContext_getLong(handle_, &eh_);
472  tdvCheckException(dll_handle, eh_);
473  return ret;
474  }
475 
484  bool getBool() const {
485  bool ret = dll_handle->TDVContext_getBool(handle_, &eh_);
486  tdvCheckException(dll_handle, eh_);
487  return ret;
488  }
489 
498  std::string getString() const {
499  unsigned long str_size = dll_handle->TDVContext_getStrSize(handle_, &eh_);
500  tdvCheckException(dll_handle, eh_);
501  std::string ret;
502  ret.resize(str_size);
503  dll_handle->TDVContext_getStr(handle_, &ret[0], &eh_);
504  tdvCheckException(dll_handle, eh_);
505  return ret; // copy elision (NRVO)
506  }
507 
516  unsigned char* getDataPtr() const {
517  unsigned char* ret = dll_handle->TDVContext_getDataPtr(handle_, &eh_);
518  tdvCheckException(dll_handle, eh_);
519  return ret;
520  }
521 
522  pbio::DynamicTemplateIndex::Ptr getDynamicTemplateIndex() const {
523  void* index = dll_handle->TDVContext_getDynamicTemplateIndex(handle_, &eh_);
524  tdvCheckException(dll_handle, eh_);
525  return pbio::DynamicTemplateIndex::Ptr::make(dll_handle, index, true);
526  }
527 
528  pbio::ContextTemplate::Ptr getContextTemplate() const {
529  void* templ = dll_handle->TDVContext_getContextTemplate(handle_, &eh_);
530  tdvCheckException(dll_handle, eh_);
531  return pbio::ContextTemplate::Ptr::make(dll_handle, templ);
532  }
533 
542  void setString(const char* str) {
543  dll_handle->TDVContext_putStr(handle_, str, &eh_);
544  tdvCheckException(dll_handle, eh_);
545  }
546 
555  void setString(const std::string& str) {
556  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
557  tdvCheckException(dll_handle, eh_);
558  }
559 
568  void setLong(long val) {
569  dll_handle->TDVContext_putLong(handle_, val, &eh_);
570  tdvCheckException(dll_handle, eh_);
571  }
572 
581  void setDouble(double val) {
582  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
583  tdvCheckException(dll_handle, eh_);
584  }
585 
594  void setBool(bool val) {
595  dll_handle->TDVContext_putBool(handle_, val, &eh_);
596  tdvCheckException(dll_handle, eh_);
597  }
598 
609  unsigned char* setDataPtr(void* ptr, int copy_sz = 0) {
610  unsigned char* ret{nullptr};
611  if(copy_sz && !ptr)
612  ret = dll_handle->TDVContext_allocDataPtr(handle_, copy_sz, &eh_);
613  else
614  ret = dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), static_cast<uint64_t>(copy_sz), &eh_);
615  tdvCheckException(dll_handle, eh_);
616  return ret;
617  }
618 
619  unsigned char* setDataPtr(const void* ptr, int copy_sz) {
620  unsigned char* ret = dll_handle->TDVContext_putConstDataPtr(handle_, static_cast<const unsigned char*>(ptr), copy_sz, &eh_);
621  tdvCheckException(dll_handle, eh_);
622  return ret;
623  }
624 
625 
626  void setDynamicTemplateIndex(DynamicTemplateIndex::Ptr templateIndex) {
627  dll_handle->TDVContext_putDynamicTemplateIndex(handle_, templateIndex->_impl, &eh_);
628  tdvCheckException(dll_handle, eh_);
629  }
630 
631  void setContextTemplate(ContextTemplate::Ptr templ) {
632  dll_handle->TDVContext_putContextTemplate(handle_, templ->_impl, &eh_);
633  tdvCheckException(dll_handle, eh_);
634  }
635 
644  bool isNone() const {
645  bool value = dll_handle->TDVContext_isNone(handle_, &eh_);
646  tdvCheckException(dll_handle, eh_);
647  return value;
648  }
649 
658  bool isArray() const {
659  bool value = dll_handle->TDVContext_isArray(handle_, &eh_);
660  tdvCheckException(dll_handle, eh_);
661  return value;
662  }
663 
672  bool isObject() const {
673  bool value = dll_handle->TDVContext_isObject(handle_, &eh_);
674  tdvCheckException(dll_handle, eh_);
675  return value;
676  }
677 
686  bool isBool() const {
687  bool val = dll_handle->TDVContext_isBool(handle_, &eh_);
688  tdvCheckException(dll_handle, eh_);
689  return val;
690  }
691 
700  bool isLong() const {
701  bool val = dll_handle->TDVContext_isLong(handle_, &eh_);
702  tdvCheckException(dll_handle, eh_);
703  return val;
704  }
705 
714  bool isDouble() const {
715  bool val = dll_handle->TDVContext_isDouble(handle_, &eh_);
716  tdvCheckException(dll_handle, eh_);
717  return val;
718  }
719 
728  bool isString() const {
729  bool val = dll_handle->TDVContext_isString(handle_, &eh_);
730  tdvCheckException(dll_handle, eh_);
731  return val;
732  }
733 
742  bool isDataPtr() const {
743  bool val = dll_handle->TDVContext_isDataPtr(handle_, &eh_);
744  tdvCheckException(dll_handle, eh_);
745  return val;
746  }
747 
748  bool isDynamicTemplateIndex() const {
749  bool val = dll_handle->TDVContext_isDynamicTemplateIndex(handle_, &eh_);
750  tdvCheckException(dll_handle, eh_);
751  return val;
752  }
753 
754  bool isContextTemplate() const {
755  bool val = dll_handle->TDVContext_isContextTemplate(handle_, &eh_);
756  tdvCheckException(dll_handle, eh_);
757  return val;
758  }
759 
760  HContext* getHandle() {return handle_;}
761  const HContext* getHandle() const {return handle_;}
762 
769  void clear() {
770  dll_handle->TDVContext_clear(handle_, &eh_);
771  tdvCheckException(dll_handle,eh_);
772  }
773 
782  void erase(const char* str) {
783  dll_handle->TDVContext_erase(handle_, str, &eh_);
784  tdvCheckException(dll_handle,eh_);
785  }
786 
795  void erase(const std::string& str) {
796  erase(str.data());
797  }
798 
807  void reserve(const size_t size) {
808  dll_handle->TDVContext_reserve(handle_, size, &eh_);
809  tdvCheckException(dll_handle,eh_);
810  }
811 
820  void saveToJsonFile(std::string& path)
821  {
822  dll_handle->TDVContext_saveToJsonFile(handle_, path.c_str(), &eh_);
823  tdvCheckException(dll_handle,eh_);
824  }
825 
834  void saveToJsonFile(const char* path)
835  {
836  dll_handle->TDVContext_saveToJsonFile(handle_, path, &eh_);
837  tdvCheckException(dll_handle,eh_);
838  }
839 
840 protected:
841 
842  void setValue(const char* str) {
843  dll_handle->TDVContext_putStr(handle_, str, &eh_);
844  tdvCheckException(dll_handle, eh_);
845  }
846  void setValue(const std::string& str) {
847  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
848  tdvCheckException(dll_handle, eh_);
849  }
850 
851  template<typename T, typename = typename std::enable_if<std::is_enum<T>::value || std::is_integral<T>::value>::type>
852  void setValue(T val) {
853  dll_handle->TDVContext_putLong(handle_, val, &eh_);
854  tdvCheckException(dll_handle, eh_);
855  }
856 
857  void setValue(double val) {
858  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
859  tdvCheckException(dll_handle, eh_);
860  }
861  void setValue(float val) {
862  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
863  tdvCheckException(dll_handle, eh_);
864  }
865  void setValue(bool val) {
866  dll_handle->TDVContext_putBool(handle_, val, &eh_);
867  tdvCheckException(dll_handle, eh_);
868  }
869  void setValue(void* ptr, int copy_sz = 0) {
870  dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), copy_sz, &eh_);
871  tdvCheckException(dll_handle, eh_);
872  }
873  void setValue(DynamicTemplateIndex::Ptr templateIndex) {
874  dll_handle->TDVContext_putDynamicTemplateIndex(handle_, templateIndex->_impl, &eh_);
875  tdvCheckException(dll_handle, eh_);
876  }
877  void setValue(ContextTemplate::Ptr templ) {
878  dll_handle->TDVContext_putContextTemplate(handle_, templ->_impl, &eh_);
879  tdvCheckException(dll_handle, eh_);
880  }
881 };
882 
883 class ContextRef : public Context {
884 public:
885 
886  ContextRef(const DHPtr &dll_handle, HContext* handle) : Context(dll_handle ,handle, true) {}
887 
888  ContextRef* getContextPtr () const { return new ContextRef(dll_handle, handle_); }
889 
890  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
891  void operator=(T&& value) {
892  setValue(std::forward<T>(value));
893  }
894 };
895 
896 template<bool isConst>
897 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, long long index) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
898 {
899  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
900  tdvCheckException(base_.dll_handle, base_.eh_);
901  index_ = (index > -1) ? std::min<size_t>(static_cast<size_t>(index), length_) : length_;
902  if(index_ < length_) {
903  if(isObj_)
904  {
905  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
906  tdvCheckException(base_.dll_handle, base_.eh_);
907  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys), std::bind(&ContextArrayIterator::charsList_deleter, this, std::placeholders::_1));
908  curr_ = new Context(base_[keys_->operator[](index)]);
909  }
910  else
911  curr_ = new Context(base_[index]);
912  }
913 }
914 
915 template<bool isConst>
916 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, const std::string& key) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
917 {
918  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
919  tdvCheckException(base_.dll_handle, base_.eh_);
920  index_ = length_;
921  if(isObj_ && length_) {
922  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
923  tdvCheckException(base_.dll_handle, base_.eh_);
924  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys), std::bind(&ContextArrayIterator::charsList_deleter, this, std::placeholders::_1));
925  size_t i=0;
926  do {
927  if(!key.compare(keys_->operator[](i))) {
928  index_ = i;
929  curr_ = new Context(base_[key]);
930  break;
931  }
932  } while((++i)<length_);
933  }
934 }
935 
936 template<bool isConst>
937 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const ContextArrayIterator& iter) :
938  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_ ? new Context(*(iter.curr_)) : nullptr), isObj_(iter.isObj_), keys_(iter.keys_)
939 {};
940 
941 template<bool isConst>
942 Context::ContextArrayIterator<isConst>::ContextArrayIterator(ContextArrayIterator&& iter) :
943  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_), isObj_(iter.isObj_), keys_(std::move(iter.keys_)) {
944  iter.curr_ = nullptr;
945 }
946 
947 template<bool isConst>
948 Context::ContextArrayIterator<isConst>& Context::ContextArrayIterator<isConst>::operator++() {
949  if(curr_)
950  delete curr_;
951  index_ = std::min<size_t>(index_+1, length_);
952  if(isObj_)
953  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
954  else
955  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
956  return *this;
957 }
958 
959 template<bool isConst>
960 Context::ContextArrayIterator<isConst> Context::ContextArrayIterator<isConst>::operator++(int) {
961  ContextArrayIterator tmp = *this;
962  index_ = std::min<size_t>(index_+1, length_);
963  if(isObj_)
964  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
965  else
966  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
967  return tmp;
968 }
969 
970 inline Context::operator ContextRef()
971 {
972  return ContextRef(dll_handle, handle_);
973 }
974 
975 inline Context::Ref Context::operator[](const std::string& key) {
976  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key.c_str(), &eh_);
977  tdvCheckException(dll_handle, eh_);
978  return Ref(dll_handle, handle);
979 }
980 
981 inline Context::Ref Context::operator[](const char* key) {
982  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key, &eh_);
983  tdvCheckException(dll_handle, eh_);
984  return Ref(dll_handle, handle);
985 }
986 
987 inline Context::Ref Context::operator[](const int index) {
988  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
989  tdvCheckException(dll_handle, eh_);
990  return Ref(dll_handle, handle);
991 }
992 
993 inline const Context::Ref Context::operator[](const std::string& key) const {
994  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
995  tdvCheckException(dll_handle, eh_);
996  return Ref(dll_handle, handle);
997 }
998 
999 inline const Context::Ref Context::operator[](const char* key) const {
1000  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1001  tdvCheckException(dll_handle, eh_);
1002  return Ref(dll_handle, handle);
1003 }
1004 
1005 inline const Context::Ref Context::operator[](const int index) const {
1006  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1007  tdvCheckException(dll_handle, eh_);
1008  return Ref(dll_handle, handle);
1009 }
1010 
1011 inline Context::Ref Context::at(const char* key) {
1012  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1013  tdvCheckException(dll_handle, eh_);
1014  return Ref(dll_handle, handle);
1015 }
1016 
1017 inline Context::Ref Context::at(const std::string& key) {
1018  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1019  tdvCheckException(dll_handle, eh_);
1020  return Ref(dll_handle, handle);
1021 }
1022 
1023 inline Context::Ref Context::at(const int index) {
1024  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1025  tdvCheckException(dll_handle, eh_);
1026  return Ref(dll_handle, handle);
1027 }
1028 
1029 inline const Context::Ref Context::at(const char* key) const {
1030  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1031  tdvCheckException(dll_handle, eh_);
1032  return Ref(dll_handle, handle);
1033 }
1034 
1035 inline const Context::Ref Context::at(const std::string& key) const {
1036  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1037  tdvCheckException(dll_handle, eh_);
1038  return Ref(dll_handle, handle);
1039 }
1040 
1041 inline const Context::Ref Context::at(const int index) const {
1042  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1043  tdvCheckException(dll_handle, eh_);
1044  return Ref(dll_handle, handle);
1045 }
1046 
1047 
1048 namespace context_utils {
1049 
1050 static std::unordered_map<int, std::string> color_mode{{IRawImage::FORMAT_GRAY, "GRAY"}, {IRawImage::FORMAT_BGR, "BGR"}, {IRawImage::FORMAT_RGB, "RGB"}};
1051 
1052 inline void putImage(Context& ctx, const RawImage& raw_image) {
1053  ctx.clear();
1054  ctx["format"] = "NDARRAY";
1055  ctx["color_space"] = color_mode.at(raw_image.format);
1056  size_t channels = (raw_image.format == IRawImage::FORMAT_GRAY) ? 1 : 3;
1057  size_t copy_sz = raw_image.height * raw_image.width * channels * sizeof(uint8_t);
1058  if (raw_image.with_crop)
1059  {
1060  unsigned char* buff{nullptr};
1061  buff = ctx["blob"].setDataPtr(buff, copy_sz);
1062  size_t shift = (raw_image.crop_info_offset_y*raw_image.crop_info_data_image_width + raw_image.crop_info_offset_x)*channels*sizeof(uint8_t);
1063  size_t stride = raw_image.width*channels*sizeof(uint8_t);
1064  unsigned char* ptr = buff;
1065  for(int row=0; row<raw_image.height; ++row)
1066  {
1067  std::memcpy(ptr, raw_image.data+shift, stride);
1068  shift += raw_image.crop_info_data_image_width*channels*sizeof(uint8_t);
1069  ptr += stride;
1070  }
1071  }
1072  else
1073  ctx["blob"].setDataPtr(raw_image.data, copy_sz);
1074  ctx["dtype"] = "uint8_t";
1075  ctx["shape"].push_back(static_cast<int64_t>(raw_image.height));
1076  ctx["shape"].push_back(static_cast<int64_t>(raw_image.width));
1077  ctx["shape"].push_back(static_cast<int64_t>(channels));
1078 }
1079 
1080 inline void putImage(Context& ctx, unsigned char* data, size_t height, size_t width, pbio::IRawImage::Format format, bool copy) {
1081  ctx.clear();
1082  ctx["format"] = "NDARRAY";
1083  ctx["color_space"] = color_mode.at(format);
1084  size_t channels = (format == IRawImage::FORMAT_GRAY) ? 1 : 3;
1085  size_t copy_sz = copy ? height * width * channels * sizeof(uint8_t) : 0;
1086  ctx["blob"].setDataPtr(data, copy_sz);
1087  ctx["dtype"] = "uint8_t";
1088  ctx["shape"].push_back(static_cast<int64_t>(height));
1089  ctx["shape"].push_back(static_cast<int64_t>(width));
1090  ctx["shape"].push_back(static_cast<int64_t>(channels));
1091 }
1092 
1093 }
1094 }
1095 
1096 #endif // WITHOUT_PROCESSING_BLOCK
1097 #endif // CONTEXT_H
bool isString() const
checks whether the container is a long type string
Definition: Context.h:728
Interface object for creating other interface objects.
Definition: FacerecService.h:64
RGB, 24 bit per pixel, 8 bit per channel.
Definition: IRawImage.h:60
bool isDataPtr() const
checks whether the container is a pointer to the data
Definition: Context.h:742
Interface object that stores a captured face sample.
Definition: RawSample.h:49
void saveToJsonFile(std::string &path)
saves the contents of the container to a json file
Definition: Context.h:820
void erase(const char *str)
deletes the contents of the container-Context stored by key.
Definition: Context.h:782
void setString(const char *str)
adds a value of type string to the container
Definition: Context.h:542
void erase(const std::string &str)
deletes the contents of the container-Context stored by key.
Definition: Context.h:795
void saveToJsonFile(const char *path)
saves the contents of the container to a json file
Definition: Context.h:834
void reserve(const size_t size)
allocates memory for num elements in the array.
Definition: Context.h:807
Grayscale, 8 bit per pixel.
Definition: IRawImage.h:53
void setDouble(double val)
adds a value of type double to the container
Definition: Context.h:581
std::vector< std::string > getKeys()
returns a list of keys in the container-Context
Definition: Context.h:401
void setString(const std::string &str)
adds a value of type std::string to the container
Definition: Context.h:555
unsigned char * getDataPtr() const
returns a pointer to data from the container
Definition: Context.h:516
bool isDouble() const
checks whether the container is a long type double
Definition: Context.h:714
void push_back(const Context &data)
adds a object to the container.
Definition: Context.h:438
bool compare(const Context &other) const
compares two Context objects
Definition: Context.h:386
bool isBool() const
checks whether the container is a bool type value
Definition: Context.h:686
void clear()
clears the contents of the container-Context.
Definition: Context.h:769
BGR, 24 bit per pixel, 8 bit per channel.
Definition: IRawImage.h:67
size_t size() const
Get the size of the container.
Definition: Context.h:296
bool contains(const std::string &key) const
checks the existence of an element by a specific key
Definition: Context.h:369
bool isNone() const
checks if there are no elements in the container
Definition: Context.h:644
The class of exceptions thrown when errors occur.
Definition: Error.h:26
Context & operator=(T &&value)
adds a value to the container
Definition: Context.h:280
bool isLong() const
проверяет является ли контейнер значением типа long.
Definition: Context.h:700
void clear()
clears the contents of the container-Context.
Definition: Context.mm:166
LightSmartPtr< DynamicTemplateIndex >::tPtr Ptr
Alias for the type of a smart pointer to DynamicTemplateIndex.
Definition: DynamicTemplateIndex.h:34
long getLong() const
returns a long value from the container
Definition: Context.h:470
bool isObject() const
checks whether the container is an object
Definition: Context.h:672
Definition: Context.h:883
double getDouble() const
returns a double value from the container
Definition: Context.h:456
void setLong(long val)
adds a value of type long to the container
Definition: Context.h:568
bool getBool() const
returns a bool value from the container
Definition: Context.h:484
Context is an interface object for storing data and interacting with methods from the Processing Bloc...
Definition: Context.h:52
unsigned char * setDataPtr(void *ptr, int copy_sz=0)
adds a pointer to the data in the container
Definition: Context.h:609
LightSmartPtr< ContextTemplate >::tPtr Ptr
Alias for the type of a smart pointer to ContextTemplate.
Definition: ContextTemplate.h:47
std::string getString() const
returns a std::string value from the container
Definition: Context.h:498
void setBool(bool val)
adds a value of type bool to the container
Definition: Context.h:594
Context is an interface object for storing data and interacting with methods from the Processing Bloc...
Definition: Context.h:67
bool isArray() const
checks whether the container is an array
Definition: Context.h:658
int height
Image height.
Definition: RawImage.h:117
const unsigned char * data
Pointer to the image data buffer. All pixels must be stored continuously, row by row, without gaps at the end of each row.
Definition: RawImage.h:118
Format
Format of image data.
Definition: IRawImage.h:46
Struct that provides raw image data and optional cropping information.
Definition: RawImage.h:113
int width
Image width.
Definition: RawImage.h:116
Ref operator[](const std::string &key)
indexing by key.
Definition: Context.h:975