3DiVi Face SDK  3.30.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 #include <functional>
15 #include <iostream>
16 #include <memory>
17 #include <string>
18 #include <unordered_map>
19 
20 #include <pbio/DllHandle.h>
21 #include <pbio/RawImage.h>
22 #include <pbio/DynamicTemplateIndex.h>
23 
24 
25 namespace pbio {
26 
27 typedef LightSmartPtr<import::DllHandle>::tPtr DHPtr;
28 
29 inline void tdvCheckException(const DHPtr& dll_handle, ContextEH*& out_exception) {
30  if(out_exception)
31  {
32  auto err = Error(dll_handle->TDVException_getErrorCode(out_exception), dll_handle->TDVException_getMessage(out_exception));
33  dll_handle->TDVException_deleteException(out_exception);
34  out_exception = nullptr;
35  throw err;
36  }
37 }
38 
39 class ContextRef;
40 
41 
48 class Context
49 {
50 
51  friend class FacerecService;
52  friend class RawSample;
53 
54  template<bool isConst=false>
55  class ContextArrayIterator
56  {
57  class charsList {
58  public:
59  size_t length_;
60  char** ptr_;
61  charsList(size_t length, char** ptr) : length_(length), ptr_(ptr) {}
62  char* operator[](size_t index) const {
63  return (index<length_) ? *(ptr_+index) : nullptr;
64  }
65  };
66 
67  const Context& base_;
68  size_t length_;
69  size_t index_;
70  Context* curr_;
71  const bool isObj_;
72  std::shared_ptr<charsList> keys_;
73 
74  void charsList_deleter(charsList* ptr) {
75  for(size_t indx=0; indx < ptr->length_; ++indx)
76  base_.dll_handle->TDVContext_freePtr(ptr->ptr_[indx]);
77  base_.dll_handle->TDVContext_freePtr(ptr->ptr_);
78  };
79 
80  public:
81  typedef std::ptrdiff_t difference_type;
82  typedef std::forward_iterator_tag iterator_category;
83  typedef Context value_type;
84  typedef typename std::conditional<isConst, const value_type*, value_type*>::type pointer;
85  typedef typename std::conditional<isConst, const value_type&, value_type&>::type reference;
86  typedef const value_type& const_reference;
87 
88  ContextArrayIterator(const Context& ctx, long long index);
89  ContextArrayIterator(const Context& ctx, const std::string& key);
90 
91  ContextArrayIterator(const ContextArrayIterator& iter);
92  ContextArrayIterator& operator=(const ContextArrayIterator& iter) = delete;
93  ContextArrayIterator(ContextArrayIterator&& iter);
94  ContextArrayIterator& operator=(ContextArrayIterator&& iter) = delete;
95 
96  ~ContextArrayIterator() {
97  if(curr_)
98  delete curr_;
99  }
100 
101  bool operator==(const ContextArrayIterator& other) const {
102  return ((this->base_ == other.base_) && (this->curr_ == other.curr_));
103  }
104 
105  bool operator!=(const ContextArrayIterator& other) const {
106  return (!(this->base_ == other.base_) || !(this->curr_ == other.curr_));
107  }
108 
109  ContextArrayIterator& operator++();
110  ContextArrayIterator operator++(int);
111 
112  reference operator*() {
113  return *curr_;
114  }
115 
116  pointer operator->() {
117  return curr_;
118  }
119 
120  std::string key() const {
121  if (keys_ && index_ < length_)
122  return std::string(keys_->operator[](index_));
123  return std::string();
124  }
125  };
126 
127 public:
128  enum Format
129  {
130  FORMAT_BGR = 0,
131  FORMAT_RGB = 1,
132  FORMAT_BGRA8888 = 2,
133  FORMAT_YUV420 = 3,
134  FORMAT_YUV_NV12 = 4,
135  FORMAT_NV21 = 5,
136  };
137 
138 protected:
139 
140  Context(const DHPtr& dll_handle) : dll_handle(dll_handle), weak_(false), eh_(nullptr) {
141  handle_ = dll_handle->TDVContext_create(&eh_);
142  tdvCheckException(dll_handle, eh_);
143  }
144 
145  Context(const DHPtr& dll_handle, HContext* handle, bool weak = true) : dll_handle(dll_handle), weak_(weak), eh_(nullptr) {
146  if(weak_)
147  handle_ = handle;
148  else
149  {
150  handle_ = dll_handle->TDVContext_clone(handle, &eh_);
151  tdvCheckException(dll_handle, eh_);
152  }
153  }
154 
155  Context(const DHPtr& dll_handle, const uint8_t* data, uint64_t dataSize) :
156  dll_handle(dll_handle),
157  weak_(false),
158  eh_(nullptr)
159  {
160  handle_ = dll_handle->TDVContext_createFromEncodedImage(data, dataSize, &eh_);
161  tdvCheckException(dll_handle, eh_);
162  }
163 
164  Context(const DHPtr& dll_handle, uint8_t* data, int32_t width, int32_t height, Format format, int32_t baseAngle) :
165  dll_handle(dll_handle),
166  weak_(false),
167  eh_(nullptr)
168  {
169  handle_ = dll_handle->TDVContext_createFromFrame(data, width, height, static_cast<int32_t>(format), baseAngle, &eh_);
170  tdvCheckException(dll_handle, eh_);
171  }
172 
173  Context(const DHPtr& dll_handle, const char* path) :
174  dll_handle(dll_handle),
175  weak_(false),
176  eh_(nullptr)
177  {
178  handle_ = dll_handle->TDVContext_createFromJsonFile(path, &eh_);
179  tdvCheckException(dll_handle, eh_);
180  }
181 
182  DHPtr dll_handle;
183  HContext* handle_;
184  bool weak_;
185  mutable ContextEH* eh_;
186 
187 public:
188 
189  typedef ContextRef Ref;
190 
191  virtual ~Context()
192  {
193  if(!weak_) {
194  dll_handle->TDVContext_destroy(handle_, &eh_);
195 
196 #if __cplusplus >= 201703L
197  if (eh_ && std::uncaught_exceptions() > 0)
198 #else
199  if (eh_ && std::uncaught_exception())
200 #endif
201  std::cerr << Error(dll_handle->TDVException_getErrorCode(eh_), dll_handle->TDVException_getMessage(eh_)).what();
202  else
203  tdvCheckException(dll_handle, eh_);
204  }
205  }
206 
207  Context(const Context& other) : dll_handle(other.dll_handle), weak_(false), eh_(nullptr) {
208  handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
209  tdvCheckException(dll_handle, eh_);
210  }
211 
212  Context& operator=(const Context& other) {
213  if (&other != this)
214  {
215  if(weak_)
216  dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
217  else {
218  HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_); // copy-swap
219  tdvCheckException(dll_handle, eh_);
220  std::swap(handle_, copy);
221  dll_handle->TDVContext_destroy(copy, &eh_);
222  }
223  tdvCheckException(dll_handle, eh_);
224  }
225  return *this;
226  }
227 
228  Context(Context&& other) : dll_handle(other.dll_handle), weak_(false), eh_(nullptr) {
229  if(other.weak_)
230  {
231  handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
232  tdvCheckException(dll_handle, eh_);
233  }
234  else
235  {
236  handle_ = other.handle_;
237  other.handle_ = nullptr;
238  other.weak_ = true;
239  }
240  }
241 
242  Context& operator=(Context&& other){
243  if (&other != this)
244  {
245  if(weak_) {
246  dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
247  tdvCheckException(dll_handle, eh_);
248  }
249  else
250  {
251  if(other.weak_)
252  {
253  HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_);
254  tdvCheckException(dll_handle, eh_);
255  std::swap(handle_, copy);
256  dll_handle->TDVContext_destroy(copy, &eh_);
257  tdvCheckException(dll_handle, eh_);
258  }
259  else
260  {
261  handle_ = other.handle_;
262  other.handle_ = nullptr;
263  other.weak_ = true;
264  }
265  }
266  }
267  return *this;
268  }
269 
270  operator ContextRef();
271 
280  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
281  Context& operator=(T&& value) {
282  setValue(std::forward<T>(value));
283  return *this;
284  }
285 
286  bool operator==(const Context& other) const
287  {return this->handle_ == other.handle_;}
288 
297  size_t size() const {
298  size_t lenght = dll_handle->TDVContext_getLength(handle_, &eh_);
299  tdvCheckException(dll_handle, eh_);
300  return lenght;
301  }
302 
303  ContextArrayIterator<> begin(){
304  return ContextArrayIterator<>(*this, 0);
305  }
306 
307  ContextArrayIterator<> end(){
308  return ContextArrayIterator<>(*this, -1);
309  }
310 
311  ContextArrayIterator<true> begin() const {
312  return ContextArrayIterator<true>(*this, 0);
313  }
314 
315  ContextArrayIterator<true> end() const {
316  return ContextArrayIterator<true>(*this, -1);
317  }
318 
319  ContextArrayIterator<true> cbegin() const {
320  return ContextArrayIterator<true>(*this, 0);
321  }
322 
323  ContextArrayIterator<true> cend() const {
324  return ContextArrayIterator<true>(*this, -1);
325  }
326 
335  Ref operator[](const std::string& key);
336  Ref operator[](const char* key);
337 
346  Ref operator[](const int index);
347 
348  const Ref operator[](const std::string& key) const;
349  const Ref operator[](const char* key) const;
350  const Ref operator[](const int index) const;
351 
352  Ref at(const char* key);
353  Ref at(const std::string& key);
354  Ref at(const int index);
355 
356  const Ref at(const char* key) const;
357  const Ref at(const std::string& key) const;
358  const Ref at(const int index) const;
359 
370  bool contains(const std::string& key) const {
371  bool result = dll_handle->TDVContext_contains(handle_, key.data(), &eh_);
372  tdvCheckException(dll_handle, eh_);
373 
374  return result;
375  }
376 
387  bool compare(const Context& other) const {
388  bool result = dll_handle->TDVContext_compare(handle_, other.handle_, &eh_);
389  tdvCheckException(dll_handle, eh_);
390 
391  return result;
392  }
393 
402  std::vector<std::string> getKeys(){
403  size_t length = size();
404 
405  auto keys = dll_handle->TDVContext_getKeys(handle_, length, &eh_);
406  tdvCheckException(dll_handle, eh_);
407 
408  std::vector<std::string> result(length);
409  for (size_t i = 0; i < length; ++i)
410  result.emplace_back(keys[i]);
411 
412  return result;
413  }
414 
415  ContextArrayIterator<> find(const std::string& key) {
416  return ContextArrayIterator<>(*this, key);
417  }
418 
419  ContextArrayIterator<true> find(const std::string& key) const {
420  return ContextArrayIterator<true>(*this, key);
421  }
422 
423  template<typename T>
424  typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type push_back(T&& data) {
425  Context elem(dll_handle);
426  elem.setValue(std::forward<T>(data));
427  // use r-value version of push_back without a copy
428  push_back(std::move(elem));
429  }
430 
439  void push_back(const Context& data) {
440  dll_handle->TDVContext_pushBack(handle_, data.handle_, true, &eh_);
441  tdvCheckException(dll_handle, eh_);
442  }
443 
444  void push_back(Context&& data) {
445  dll_handle->TDVContext_pushBack(handle_, data.handle_, weak_, &eh_); // can not move weak object
446  tdvCheckException(dll_handle, eh_);
447  }
448 
457  double getDouble() const {
458  double ret = dll_handle->TDVContext_getDouble(handle_, &eh_);
459  tdvCheckException(dll_handle, eh_);
460  return ret;
461  }
462 
471  long getLong() const {
472  long ret = dll_handle->TDVContext_getLong(handle_, &eh_);
473  tdvCheckException(dll_handle, eh_);
474  return ret;
475  }
476 
485  bool getBool() const {
486  bool ret = dll_handle->TDVContext_getBool(handle_, &eh_);
487  tdvCheckException(dll_handle, eh_);
488  return ret;
489  }
490 
499  std::string getString() const {
500  unsigned long str_size = dll_handle->TDVContext_getStrSize(handle_, &eh_);
501  tdvCheckException(dll_handle, eh_);
502  std::string ret = dll_handle->TDVContext_getStr(handle_, &eh_);
503  tdvCheckException(dll_handle, eh_);
504  return ret; // copy elision (NRVO)
505  }
506 
515  unsigned char* getDataPtr() const {
516  unsigned char* ret = dll_handle->TDVContext_getDataPtr(handle_, &eh_);
517  tdvCheckException(dll_handle, eh_);
518  return ret;
519  }
520 
521  std::pair<uint8_t*, size_t> getBlobData() const {
522  size_t length;
523  uint8_t* ret = dll_handle->TDVContext_getBlobData(handle_, &length, &eh_);
524  tdvCheckException(dll_handle, eh_);
525  return {ret, length};
526  }
527 
528  pbio::DynamicTemplateIndex::Ptr getDynamicTemplateIndex() const {
529  void* index = dll_handle->TDVContext_getDynamicTemplateIndex(handle_, &eh_);
530  tdvCheckException(dll_handle, eh_);
531  return pbio::DynamicTemplateIndex::Ptr::make(dll_handle, index, true);
532  }
533 
534  pbio::ContextTemplate::Ptr getContextTemplate() const {
535  void* templ = dll_handle->TDVContext_getContextTemplate(handle_, &eh_);
536  tdvCheckException(dll_handle, eh_);
537  return pbio::ContextTemplate::Ptr::make(dll_handle, templ);
538  }
539 
548  void setString(const char* str) {
549  dll_handle->TDVContext_putStr(handle_, str, &eh_);
550  tdvCheckException(dll_handle, eh_);
551  }
552 
561  void setString(const std::string& str) {
562  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
563  tdvCheckException(dll_handle, eh_);
564  }
565 
574  void setLong(long val) {
575  dll_handle->TDVContext_putLong(handle_, val, &eh_);
576  tdvCheckException(dll_handle, eh_);
577  }
578 
587  void setDouble(double val) {
588  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
589  tdvCheckException(dll_handle, eh_);
590  }
591 
600  void setBool(bool val) {
601  dll_handle->TDVContext_putBool(handle_, val, &eh_);
602  tdvCheckException(dll_handle, eh_);
603  }
604 
615  unsigned char* setDataPtr(void* ptr, int copy_sz = 0) {
616  unsigned char* ret{nullptr};
617  if(copy_sz && !ptr)
618  ret = dll_handle->TDVContext_allocDataPtr(handle_, copy_sz, &eh_);
619  else
620  ret = dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), static_cast<uint64_t>(copy_sz), &eh_);
621  tdvCheckException(dll_handle, eh_);
622  return ret;
623  }
624 
625  unsigned char* setDataPtr(const void* ptr, int copy_sz) {
626  unsigned char* ret = dll_handle->TDVContext_putConstDataPtr(handle_, static_cast<const unsigned char*>(ptr), copy_sz, &eh_);
627  tdvCheckException(dll_handle, eh_);
628  return ret;
629  }
630 
631 
632  void setDynamicTemplateIndex(DynamicTemplateIndex::Ptr templateIndex) {
633  dll_handle->TDVContext_putDynamicTemplateIndex(handle_, templateIndex->_impl, &eh_);
634  tdvCheckException(dll_handle, eh_);
635  }
636 
637  void setContextTemplate(ContextTemplate::Ptr templ) {
638  dll_handle->TDVContext_putContextTemplate(handle_, templ->_impl, &eh_);
639  tdvCheckException(dll_handle, eh_);
640  }
641 
650  bool isNone() const {
651  bool value = dll_handle->TDVContext_isNone(handle_, &eh_);
652  tdvCheckException(dll_handle, eh_);
653  return value;
654  }
655 
664  bool isArray() const {
665  bool value = dll_handle->TDVContext_isArray(handle_, &eh_);
666  tdvCheckException(dll_handle, eh_);
667  return value;
668  }
669 
678  bool isObject() const {
679  bool value = dll_handle->TDVContext_isObject(handle_, &eh_);
680  tdvCheckException(dll_handle, eh_);
681  return value;
682  }
683 
692  bool isBool() const {
693  bool val = dll_handle->TDVContext_isBool(handle_, &eh_);
694  tdvCheckException(dll_handle, eh_);
695  return val;
696  }
697 
706  bool isLong() const {
707  bool val = dll_handle->TDVContext_isLong(handle_, &eh_);
708  tdvCheckException(dll_handle, eh_);
709  return val;
710  }
711 
720  bool isDouble() const {
721  bool val = dll_handle->TDVContext_isDouble(handle_, &eh_);
722  tdvCheckException(dll_handle, eh_);
723  return val;
724  }
725 
734  bool isString() const {
735  bool val = dll_handle->TDVContext_isString(handle_, &eh_);
736  tdvCheckException(dll_handle, eh_);
737  return val;
738  }
739 
748  bool isDataPtr() const {
749  bool val = dll_handle->TDVContext_isDataPtr(handle_, &eh_);
750  tdvCheckException(dll_handle, eh_);
751  return val;
752  }
753 
754  bool isDynamicTemplateIndex() const {
755  bool val = dll_handle->TDVContext_isDynamicTemplateIndex(handle_, &eh_);
756  tdvCheckException(dll_handle, eh_);
757  return val;
758  }
759 
760  bool isContextTemplate() const {
761  bool val = dll_handle->TDVContext_isContextTemplate(handle_, &eh_);
762  tdvCheckException(dll_handle, eh_);
763  return val;
764  }
765 
766  HContext* getHandle() {return handle_;}
767  const HContext* getHandle() const {return handle_;}
768 
775  void clear() {
776  dll_handle->TDVContext_clear(handle_, &eh_);
777  tdvCheckException(dll_handle,eh_);
778  }
779 
788  void erase(const char* str) {
789  dll_handle->TDVContext_erase(handle_, str, &eh_);
790  tdvCheckException(dll_handle,eh_);
791  }
792 
801  void erase(const std::string& str) {
802  erase(str.data());
803  }
804 
813  void reserve(const size_t size) {
814  dll_handle->TDVContext_reserve(handle_, size, &eh_);
815  tdvCheckException(dll_handle,eh_);
816  }
817 
826  void saveToJsonFile(std::string& path)
827  {
828  dll_handle->TDVContext_saveToJsonFile(handle_, path.c_str(), &eh_);
829  tdvCheckException(dll_handle,eh_);
830  }
831 
840  void saveToJsonFile(const char* path)
841  {
842  dll_handle->TDVContext_saveToJsonFile(handle_, path, &eh_);
843  tdvCheckException(dll_handle,eh_);
844  }
845 
846  std::string serializeToJson() const
847  {
848  const char* data = dll_handle->TDVContext_serializeToJson(handle_, &eh_);
849 
850  tdvCheckException(dll_handle, eh_);
851 
852  std::string result(data);
853 
854  dll_handle->TDVContext_deleteString(data, &eh_);
855 
856  tdvCheckException(dll_handle, eh_);
857 
858  return result;
859  }
860 
861 protected:
862 
863  void setValue(const char* str) {
864  dll_handle->TDVContext_putStr(handle_, str, &eh_);
865  tdvCheckException(dll_handle, eh_);
866  }
867  void setValue(const std::string& str) {
868  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
869  tdvCheckException(dll_handle, eh_);
870  }
871 
872  template<typename T, typename = typename std::enable_if<std::is_enum<T>::value || std::is_integral<T>::value>::type>
873  void setValue(T val) {
874  dll_handle->TDVContext_putLong(handle_, val, &eh_);
875  tdvCheckException(dll_handle, eh_);
876  }
877 
878  void setValue(double val) {
879  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
880  tdvCheckException(dll_handle, eh_);
881  }
882  void setValue(float val) {
883  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
884  tdvCheckException(dll_handle, eh_);
885  }
886  void setValue(bool val) {
887  dll_handle->TDVContext_putBool(handle_, val, &eh_);
888  tdvCheckException(dll_handle, eh_);
889  }
890  void setValue(void* ptr, int copy_sz = 0) {
891  dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), copy_sz, &eh_);
892  tdvCheckException(dll_handle, eh_);
893  }
894  void setValue(DynamicTemplateIndex::Ptr templateIndex) {
895  dll_handle->TDVContext_putDynamicTemplateIndex(handle_, templateIndex->_impl, &eh_);
896  tdvCheckException(dll_handle, eh_);
897  }
898  void setValue(ContextTemplate::Ptr templ) {
899  dll_handle->TDVContext_putContextTemplate(handle_, templ->_impl, &eh_);
900  tdvCheckException(dll_handle, eh_);
901  }
902 };
903 
904 class ContextRef : public Context {
905 public:
906 
907  ContextRef(const DHPtr &dll_handle, HContext* handle) : Context(dll_handle ,handle, true) {}
908 
909  ContextRef* getContextPtr () const { return new ContextRef(dll_handle, handle_); }
910 
911  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
912  void operator=(T&& value) {
913  setValue(std::forward<T>(value));
914  }
915 };
916 
917 template<bool isConst>
918 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, long long index) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
919 {
920  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
921  tdvCheckException(base_.dll_handle, base_.eh_);
922  index_ = (index > -1) ? (std::min<size_t>)(static_cast<size_t>(index), length_) : length_;
923  if(index_ < length_) {
924  if(isObj_)
925  {
926  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
927  tdvCheckException(base_.dll_handle, base_.eh_);
928  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys), std::bind(&ContextArrayIterator::charsList_deleter, this, std::placeholders::_1));
929  curr_ = new Context(base_[keys_->operator[](index)]);
930  }
931  else
932  curr_ = new Context(base_[index]);
933  }
934 }
935 
936 template<bool isConst>
937 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, const std::string& key) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
938 {
939  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
940  tdvCheckException(base_.dll_handle, base_.eh_);
941  index_ = length_;
942  if(isObj_ && length_) {
943  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
944  tdvCheckException(base_.dll_handle, base_.eh_);
945  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys), std::bind(&ContextArrayIterator::charsList_deleter, this, std::placeholders::_1));
946  size_t i=0;
947  do {
948  if(!key.compare(keys_->operator[](i))) {
949  index_ = i;
950  curr_ = new Context(base_[key]);
951  break;
952  }
953  } while((++i)<length_);
954  }
955 }
956 
957 template<bool isConst>
958 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const ContextArrayIterator& iter) :
959  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_ ? new Context(*(iter.curr_)) : nullptr), isObj_(iter.isObj_), keys_(iter.keys_)
960 {};
961 
962 template<bool isConst>
963 Context::ContextArrayIterator<isConst>::ContextArrayIterator(ContextArrayIterator&& iter) :
964  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_), isObj_(iter.isObj_), keys_(std::move(iter.keys_)) {
965  iter.curr_ = nullptr;
966 }
967 
968 template<bool isConst>
969 Context::ContextArrayIterator<isConst>& Context::ContextArrayIterator<isConst>::operator++() {
970  if(curr_)
971  delete curr_;
972  index_ = (std::min<size_t>)(index_+1, length_);
973  if(isObj_)
974  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
975  else
976  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
977  return *this;
978 }
979 
980 template<bool isConst>
981 Context::ContextArrayIterator<isConst> Context::ContextArrayIterator<isConst>::operator++(int) {
982  ContextArrayIterator tmp = *this;
983  index_ = (std::min<size_t>)(index_+1, length_);
984  if(isObj_)
985  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
986  else
987  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
988  return tmp;
989 }
990 
991 inline Context::operator ContextRef()
992 {
993  return ContextRef(dll_handle, handle_);
994 }
995 
996 inline Context::Ref Context::operator[](const std::string& key) {
997  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key.c_str(), &eh_);
998  tdvCheckException(dll_handle, eh_);
999  return Ref(dll_handle, handle);
1000 }
1001 
1002 inline Context::Ref Context::operator[](const char* key) {
1003  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key, &eh_);
1004  tdvCheckException(dll_handle, eh_);
1005  return Ref(dll_handle, handle);
1006 }
1007 
1008 inline Context::Ref Context::operator[](const int index) {
1009  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1010  tdvCheckException(dll_handle, eh_);
1011  return Ref(dll_handle, handle);
1012 }
1013 
1014 inline const Context::Ref Context::operator[](const std::string& key) const {
1015  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1016  tdvCheckException(dll_handle, eh_);
1017  return Ref(dll_handle, handle);
1018 }
1019 
1020 inline const Context::Ref Context::operator[](const char* key) const {
1021  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1022  tdvCheckException(dll_handle, eh_);
1023  return Ref(dll_handle, handle);
1024 }
1025 
1026 inline const Context::Ref Context::operator[](const int index) const {
1027  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1028  tdvCheckException(dll_handle, eh_);
1029  return Ref(dll_handle, handle);
1030 }
1031 
1032 inline Context::Ref Context::at(const char* key) {
1033  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1034  tdvCheckException(dll_handle, eh_);
1035  return Ref(dll_handle, handle);
1036 }
1037 
1038 inline Context::Ref Context::at(const std::string& key) {
1039  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1040  tdvCheckException(dll_handle, eh_);
1041  return Ref(dll_handle, handle);
1042 }
1043 
1044 inline Context::Ref Context::at(const int index) {
1045  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1046  tdvCheckException(dll_handle, eh_);
1047  return Ref(dll_handle, handle);
1048 }
1049 
1050 inline const Context::Ref Context::at(const char* key) const {
1051  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1052  tdvCheckException(dll_handle, eh_);
1053  return Ref(dll_handle, handle);
1054 }
1055 
1056 inline const Context::Ref Context::at(const std::string& key) const {
1057  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1058  tdvCheckException(dll_handle, eh_);
1059  return Ref(dll_handle, handle);
1060 }
1061 
1062 inline const Context::Ref Context::at(const int index) const {
1063  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1064  tdvCheckException(dll_handle, eh_);
1065  return Ref(dll_handle, handle);
1066 }
1067 
1068 
1069 namespace context_utils {
1070 
1071 static std::unordered_map<int, std::string> color_mode{{IRawImage::FORMAT_GRAY, "GRAY"}, {IRawImage::FORMAT_BGR, "BGR"}, {IRawImage::FORMAT_RGB, "RGB"}};
1072 
1073 inline void putImage(Context& ctx, const RawImage& raw_image) {
1074  ctx.clear();
1075  ctx["format"] = "NDARRAY";
1076  ctx["color_space"] = color_mode.at(raw_image.format);
1077  size_t channels = (raw_image.format == IRawImage::FORMAT_GRAY) ? 1 : 3;
1078  size_t copy_sz = raw_image.height * raw_image.width * channels * sizeof(uint8_t);
1079  if (raw_image.with_crop)
1080  {
1081  unsigned char* buff{nullptr};
1082  buff = ctx["blob"].setDataPtr(buff, copy_sz);
1083  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);
1084  size_t stride = raw_image.width*channels*sizeof(uint8_t);
1085  unsigned char* ptr = buff;
1086  for(int row=0; row<raw_image.height; ++row)
1087  {
1088  std::memcpy(ptr, raw_image.data+shift, stride);
1089  shift += raw_image.crop_info_data_image_width*channels*sizeof(uint8_t);
1090  ptr += stride;
1091  }
1092  }
1093  else
1094  ctx["blob"].setDataPtr(raw_image.data, copy_sz);
1095  ctx["dtype"] = "uint8_t";
1096  ctx["shape"].push_back(static_cast<int64_t>(raw_image.height));
1097  ctx["shape"].push_back(static_cast<int64_t>(raw_image.width));
1098  ctx["shape"].push_back(static_cast<int64_t>(channels));
1099 }
1100 
1101 inline void putImage(Context& ctx, unsigned char* data, size_t height, size_t width, pbio::IRawImage::Format format, bool copy) {
1102  ctx.clear();
1103  ctx["format"] = "NDARRAY";
1104  ctx["color_space"] = color_mode.at(format);
1105  size_t channels = (format == IRawImage::FORMAT_GRAY) ? 1 : 3;
1106  size_t copy_sz = copy ? height * width * channels * sizeof(uint8_t) : 0;
1107  ctx["blob"].setDataPtr(data, copy_sz);
1108  ctx["dtype"] = "uint8_t";
1109  ctx["shape"].push_back(static_cast<int64_t>(height));
1110  ctx["shape"].push_back(static_cast<int64_t>(width));
1111  ctx["shape"].push_back(static_cast<int64_t>(channels));
1112 }
1113 
1114 }
1115 }
1116 
1117 #endif // WITHOUT_PROCESSING_BLOCK
1118 #endif // CONTEXT_H
bool isString() const
checks whether the container is a long type string
Definition: Context.h:734
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:748
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:826
void erase(const char *str)
deletes the contents of the container-Context stored by key.
Definition: Context.h:788
void setString(const char *str)
adds a value of type string to the container
Definition: Context.h:548
void erase(const std::string &str)
deletes the contents of the container-Context stored by key.
Definition: Context.h:801
void saveToJsonFile(const char *path)
saves the contents of the container to a json file
Definition: Context.h:840
void reserve(const size_t size)
allocates memory for num elements in the array.
Definition: Context.h:813
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:587
std::vector< std::string > getKeys()
returns a list of keys in the container-Context
Definition: Context.h:402
void setString(const std::string &str)
adds a value of type std::string to the container
Definition: Context.h:561
unsigned char * getDataPtr() const
returns a pointer to data from the container
Definition: Context.h:515
bool isDouble() const
checks whether the container is a long type double
Definition: Context.h:720
void push_back(const Context &data)
adds a object to the container.
Definition: Context.h:439
bool compare(const Context &other) const
compares two Context objects
Definition: Context.h:387
bool isBool() const
checks whether the container is a bool type value
Definition: Context.h:692
void clear()
clears the contents of the container-Context.
Definition: Context.h:775
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:297
bool contains(const std::string &key) const
checks the existence of an element by a specific key
Definition: Context.h:370
bool isNone() const
checks if there are no elements in the container
Definition: Context.h:650
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:281
bool isLong() const
проверяет является ли контейнер значением типа long.
Definition: Context.h:706
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:471
bool isObject() const
checks whether the container is an object
Definition: Context.h:678
Definition: Context.h:904
double getDouble() const
returns a double value from the container
Definition: Context.h:457
void setLong(long val)
adds a value of type long to the container
Definition: Context.h:574
bool getBool() const
returns a bool value from the container
Definition: Context.h:485
Context is an interface object for storing data and interacting with methods from the Processing Bloc...
Definition: Context.h:48
unsigned char * setDataPtr(void *ptr, int copy_sz=0)
adds a pointer to the data in the container
Definition: Context.h:615
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:499
void setBool(bool val)
adds a value of type bool to the container
Definition: Context.h:600
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:664
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:996