3DiVi Face SDK  3.24.2
 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 
27 
28 namespace pbio {
29 
30 typedef LightSmartPtr<import::DllHandle>::tPtr DHPtr;
31 
32 inline void tdvCheckException(const DHPtr& dll_handle, ContextEH*& out_exception) {
33  if(out_exception)
34  {
35  auto err = Error(dll_handle->TDVException_getErrorCode(out_exception), dll_handle->TDVException_getMessage(out_exception));
36  dll_handle->TDVException_deleteException(out_exception);
37  out_exception = nullptr;
38  throw err;
39  }
40 }
41 
42 class ContextRef;
43 
44 
51 class Context
52 {
53 
54  friend class FacerecService;
55  friend class RawSample;
56 
57  template<bool isConst=false>
58  class ContextArrayIterator
59  {
60  class charsList {
61  public:
62  size_t length_;
63  char** ptr_;
64  charsList(size_t length, char** ptr) : length_(length), ptr_(ptr) {}
65  char* operator[](size_t index) const {
66  return (index<length_) ? *(ptr_+index) : nullptr;
67  }
68  };
69 
70  const Context& base_;
71  size_t length_;
72  size_t index_;
73  Context* curr_;
74  const bool isObj_;
75  std::shared_ptr<charsList> keys_;
76 
77  void charsList_deleter(charsList* ptr) {
78  for(size_t indx=0; indx < ptr->length_; ++indx)
79  base_.dll_handle->TDVContext_freePtr(ptr->ptr_[indx]);
80  base_.dll_handle->TDVContext_freePtr(ptr->ptr_);
81  };
82 
83  public:
84  typedef std::ptrdiff_t difference_type;
85  typedef std::forward_iterator_tag iterator_category;
86  typedef Context value_type;
87  typedef typename std::conditional<isConst, const value_type*, value_type*>::type pointer;
88  typedef typename std::conditional<isConst, const value_type&, value_type&>::type reference;
89  typedef const value_type& const_reference;
90 
91  ContextArrayIterator(const Context& ctx, long long index);
92  ContextArrayIterator(const Context& ctx, const std::string& key);
93 
94  ContextArrayIterator(const ContextArrayIterator& iter);
95  ContextArrayIterator& operator=(const ContextArrayIterator& iter) = delete;
96  ContextArrayIterator(ContextArrayIterator&& iter);
97  ContextArrayIterator& operator=(ContextArrayIterator&& iter) = delete;
98 
99  ~ContextArrayIterator() {
100  if(curr_)
101  delete curr_;
102  }
103 
104  bool operator==(const ContextArrayIterator& other) const {
105  return ((this->base_ == other.base_) && (this->curr_ == other.curr_));
106  }
107 
108  bool operator!=(const ContextArrayIterator& other) const {
109  return (!(this->base_ == other.base_) || !(this->curr_ == other.curr_));
110  }
111 
112  ContextArrayIterator& operator++();
113  ContextArrayIterator operator++(int);
114 
115  reference operator*() {
116  return *curr_;
117  }
118 
119  pointer operator->() {
120  return curr_;
121  }
122 
123  std::string key() const {
124  if (keys_ && index_ < length_)
125  return std::string(keys_->operator[](index_));
126  return std::string();
127  }
128  };
129 
130 public:
131  enum Format
132  {
133  FORMAT_BGR = 0,
134  FORMAT_RGB = 1,
135  FORMAT_BGRA8888 = 2,
136  FORMAT_YUV420 = 3,
137  FORMAT_YUV_NV12 = 4,
138  FORMAT_NV21 = 5,
139  };
140 
141 protected:
142 
143  Context(const DHPtr& dll_handle) : dll_handle(dll_handle), weak_(false), eh_(nullptr) {
144  handle_ = dll_handle->TDVContext_create(&eh_);
145  tdvCheckException(dll_handle, eh_);
146  }
147 
148  Context(const DHPtr& dll_handle, HContext* handle, bool weak = true) : dll_handle(dll_handle), weak_(weak), eh_(nullptr) {
149  if(weak_)
150  handle_ = handle;
151  else
152  {
153  handle_ = dll_handle->TDVContext_clone(handle, &eh_);
154  tdvCheckException(dll_handle, eh_);
155  }
156  }
157 
158  Context(const DHPtr& dll_handle, const uint8_t* data, uint64_t dataSize) :
159  dll_handle(dll_handle),
160  weak_(false),
161  eh_(nullptr)
162  {
163  handle_ = dll_handle->TDVContext_createFromEncodedImage(data, dataSize, &eh_);
164  tdvCheckException(dll_handle, eh_);
165  }
166 
167  Context(const DHPtr& dll_handle, uint8_t* data, int32_t width, int32_t height, Format format, int32_t baseAngle) :
168  dll_handle(dll_handle),
169  weak_(false),
170  eh_(nullptr)
171  {
172  handle_ = dll_handle->TDVContext_createFromFrame(data, width, height, static_cast<int32_t>(format), baseAngle, &eh_);
173  tdvCheckException(dll_handle, eh_);
174  }
175 
176  Context(const DHPtr& dll_handle, const char* path) :
177  dll_handle(dll_handle),
178  weak_(false),
179  eh_(nullptr)
180  {
181  handle_ = dll_handle->TDVContext_createFromJsonFile(path, &eh_);
182  tdvCheckException(dll_handle, eh_);
183  }
184 
185  DHPtr dll_handle;
186  HContext* handle_;
187  bool weak_;
188  mutable ContextEH* eh_;
189 
190 public:
191 
192  typedef ContextRef Ref;
193 
194  virtual ~Context()
195  {
196  if(!weak_) {
197  dll_handle->TDVContext_destroy(handle_, &eh_);
198  if (eh_ && std::uncaught_exception())
199  std::cerr << Error(dll_handle->TDVException_getErrorCode(eh_), dll_handle->TDVException_getMessage(eh_)).what();
200  else
201  tdvCheckException(dll_handle, eh_);
202  }
203  }
204 
205  Context(const Context& other) : dll_handle(other.dll_handle), weak_(false), eh_(nullptr) {
206  handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
207  tdvCheckException(dll_handle, eh_);
208  }
209 
210  Context& operator=(const Context& other) {
211  if (&other != this)
212  {
213  if(weak_)
214  dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
215  else {
216  HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_); // copy-swap
217  tdvCheckException(dll_handle, eh_);
218  std::swap(handle_, copy);
219  dll_handle->TDVContext_destroy(copy, &eh_);
220  }
221  tdvCheckException(dll_handle, eh_);
222  }
223  return *this;
224  }
225 
226  Context(Context&& other) : dll_handle(other.dll_handle), weak_(false), eh_(nullptr) {
227  if(other.weak_)
228  {
229  handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
230  tdvCheckException(dll_handle, eh_);
231  }
232  else
233  {
234  handle_ = other.handle_;
235  other.handle_ = nullptr;
236  other.weak_ = true;
237  }
238  }
239 
240  Context& operator=(Context&& other){
241  if (&other != this)
242  {
243  if(weak_) {
244  dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
245  tdvCheckException(dll_handle, eh_);
246  }
247  else
248  {
249  if(other.weak_)
250  {
251  HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_);
252  tdvCheckException(dll_handle, eh_);
253  std::swap(handle_, copy);
254  dll_handle->TDVContext_destroy(copy, &eh_);
255  tdvCheckException(dll_handle, eh_);
256  }
257  else
258  {
259  handle_ = other.handle_;
260  other.handle_ = nullptr;
261  other.weak_ = true;
262  }
263  }
264  }
265  return *this;
266  }
267 
268  operator ContextRef();
269 
278  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
279  Context& operator=(T&& value) {
280  setValue(std::forward<T>(value));
281  return *this;
282  }
283 
284  bool operator==(const Context& other) const
285  {return this->handle_ == other.handle_;}
286 
295  size_t size() const {
296  size_t lenght = dll_handle->TDVContext_getLength(handle_, &eh_);
297  tdvCheckException(dll_handle, eh_);
298  return lenght;
299  }
300 
301  ContextArrayIterator<> begin(){
302  return ContextArrayIterator<>(*this, 0);
303  }
304 
305  ContextArrayIterator<> end(){
306  return ContextArrayIterator<>(*this, -1);
307  }
308 
309  ContextArrayIterator<true> begin() const {
310  return ContextArrayIterator<true>(*this, 0);
311  }
312 
313  ContextArrayIterator<true> end() const {
314  return ContextArrayIterator<true>(*this, -1);
315  }
316 
317  ContextArrayIterator<true> cbegin() const {
318  return ContextArrayIterator<true>(*this, 0);
319  }
320 
321  ContextArrayIterator<true> cend() const {
322  return ContextArrayIterator<true>(*this, -1);
323  }
324 
333  Ref operator[](const std::string& key);
334  Ref operator[](const char* key);
335 
344  Ref operator[](const int index);
345 
346  const Ref operator[](const std::string& key) const;
347  const Ref operator[](const char* key) const;
348  const Ref operator[](const int index) const;
349 
350  Ref at(const char* key);
351  Ref at(const std::string& key);
352  Ref at(const int index);
353 
354  const Ref at(const char* key) const;
355  const Ref at(const std::string& key) const;
356  const Ref at(const int index) const;
357 
368  bool contains(const std::string& key) const {
369  bool result = dll_handle->TDVContext_contains(handle_, key.data(), &eh_);
370  tdvCheckException(dll_handle, eh_);
371 
372  return result;
373  }
374 
385  bool compare(const Context& other) const {
386  bool result = dll_handle->TDVContext_compare(handle_, other.handle_, &eh_);
387  tdvCheckException(dll_handle, eh_);
388 
389  return result;
390  }
391 
400  std::vector<std::string> getKeys(){
401  size_t length = size();
402 
403  auto keys = dll_handle->TDVContext_getKeys(handle_, length, &eh_);
404  tdvCheckException(dll_handle, eh_);
405 
406  std::vector<std::string> result(length);
407  for (size_t i = 0; i < length; ++i)
408  result.emplace_back(keys[i]);
409 
410  return result;
411  }
412 
413  ContextArrayIterator<> find(const std::string& key) {
414  return ContextArrayIterator<>(*this, key);
415  }
416 
417  ContextArrayIterator<true> find(const std::string& key) const {
418  return ContextArrayIterator<true>(*this, key);
419  }
420 
421  template<typename T>
422  typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type push_back(T&& data) {
423  Context elem(dll_handle);
424  elem.setValue(std::forward<T>(data));
425  // use r-value version of push_back without a copy
426  push_back(std::move(elem));
427  }
428 
437  void push_back(const Context& data) {
438  dll_handle->TDVContext_pushBack(handle_, data.handle_, true, &eh_);
439  tdvCheckException(dll_handle, eh_);
440  }
441 
442  void push_back(Context&& data) {
443  dll_handle->TDVContext_pushBack(handle_, data.handle_, weak_, &eh_); // can not move weak object
444  tdvCheckException(dll_handle, eh_);
445  }
446 
455  double getDouble() const {
456  double ret = dll_handle->TDVContext_getDouble(handle_, &eh_);
457  tdvCheckException(dll_handle, eh_);
458  return ret;
459  }
460 
469  long getLong() const {
470  long ret = dll_handle->TDVContext_getLong(handle_, &eh_);
471  tdvCheckException(dll_handle, eh_);
472  return ret;
473  }
474 
483  bool getBool() const {
484  bool ret = dll_handle->TDVContext_getBool(handle_, &eh_);
485  tdvCheckException(dll_handle, eh_);
486  return ret;
487  }
488 
497  std::string getString() const {
498  unsigned long str_size = dll_handle->TDVContext_getStrSize(handle_, &eh_);
499  tdvCheckException(dll_handle, eh_);
500  std::string ret;
501  ret.resize(str_size);
502  dll_handle->TDVContext_getStr(handle_, &ret[0], &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 
529  void setString(const char* str) {
530  dll_handle->TDVContext_putStr(handle_, str, &eh_);
531  tdvCheckException(dll_handle, eh_);
532  }
533 
542  void setString(const std::string& str) {
543  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
544  tdvCheckException(dll_handle, eh_);
545  }
546 
555  void setLong(long val) {
556  dll_handle->TDVContext_putLong(handle_, val, &eh_);
557  tdvCheckException(dll_handle, eh_);
558  }
559 
568  void setDouble(double val) {
569  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
570  tdvCheckException(dll_handle, eh_);
571  }
572 
581  void setBool(bool val) {
582  dll_handle->TDVContext_putBool(handle_, val, &eh_);
583  tdvCheckException(dll_handle, eh_);
584  }
585 
596  unsigned char* setDataPtr(void* ptr, int copy_sz = 0) {
597  unsigned char* ret{nullptr};
598  if(copy_sz && !ptr)
599  ret = dll_handle->TDVContext_allocDataPtr(handle_, copy_sz, &eh_);
600  else
601  ret = dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), copy_sz, &eh_);
602  tdvCheckException(dll_handle, eh_);
603  return ret;
604  }
605 
606  unsigned char* setDataPtr(const void* ptr, int copy_sz) {
607  unsigned char* ret = dll_handle->TDVContext_putConstDataPtr(handle_, static_cast<const unsigned char*>(ptr), copy_sz, &eh_);
608  tdvCheckException(dll_handle, eh_);
609  return ret;
610  }
611 
620  bool isNone() const {
621  bool value = dll_handle->TDVContext_isNone(handle_, &eh_);
622  tdvCheckException(dll_handle, eh_);
623  return value;
624  }
625 
634  bool isArray() const {
635  bool value = dll_handle->TDVContext_isArray(handle_, &eh_);
636  tdvCheckException(dll_handle, eh_);
637  return value;
638  }
639 
648  bool isObject() const {
649  bool value = dll_handle->TDVContext_isObject(handle_, &eh_);
650  tdvCheckException(dll_handle, eh_);
651  return value;
652  }
653 
662  bool isBool() const {
663  bool val = dll_handle->TDVContext_isBool(handle_, &eh_);
664  tdvCheckException(dll_handle, eh_);
665  return val;
666  }
667 
676  bool isLong() const {
677  bool val = dll_handle->TDVContext_isLong(handle_, &eh_);
678  tdvCheckException(dll_handle, eh_);
679  return val;
680  }
681 
690  bool isDouble() const {
691  bool val = dll_handle->TDVContext_isDouble(handle_, &eh_);
692  tdvCheckException(dll_handle, eh_);
693  return val;
694  }
695 
704  bool isString() const {
705  bool val = dll_handle->TDVContext_isString(handle_, &eh_);
706  tdvCheckException(dll_handle, eh_);
707  return val;
708  }
709 
718  bool isDataPtr() const {
719  bool val = dll_handle->TDVContext_isDataPtr(handle_, &eh_);
720  tdvCheckException(dll_handle, eh_);
721  return val;
722  }
723 
724  HContext* getHandle() {return handle_;}
725  const HContext* getHandle() const {return handle_;}
726 
733  void clear() {
734  dll_handle->TDVContext_clear(handle_, &eh_);
735  tdvCheckException(dll_handle,eh_);
736  }
737 
746  void erase(const char* str) {
747  dll_handle->TDVContext_erase(handle_, str, &eh_);
748  tdvCheckException(dll_handle,eh_);
749  }
750 
759  void erase(const std::string& str) {
760  erase(str.data());
761  }
762 
771  void reserve(const size_t size) {
772  dll_handle->TDVContext_reserve(handle_, size, &eh_);
773  tdvCheckException(dll_handle,eh_);
774  }
775 
784  void saveToJsonFile(std::string& path)
785  {
786  dll_handle->TDVContext_saveToJsonFile(handle_, path.c_str(), &eh_);
787  tdvCheckException(dll_handle,eh_);
788  }
789 
798  void saveToJsonFile(const char* path)
799  {
800  dll_handle->TDVContext_saveToJsonFile(handle_, path, &eh_);
801  tdvCheckException(dll_handle,eh_);
802  }
803 
804 protected:
805 
806  void setValue(const char* str) {
807  dll_handle->TDVContext_putStr(handle_, str, &eh_);
808  tdvCheckException(dll_handle, eh_);
809  }
810  void setValue(const std::string& str) {
811  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
812  tdvCheckException(dll_handle, eh_);
813  }
814 
815  template<typename T, typename = typename std::enable_if<std::is_enum<T>::value || std::is_integral<T>::value>::type>
816  void setValue(T val) {
817  dll_handle->TDVContext_putLong(handle_, val, &eh_);
818  tdvCheckException(dll_handle, eh_);
819  }
820 
821  void setValue(double val) {
822  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
823  tdvCheckException(dll_handle, eh_);
824  }
825  void setValue(float val) {
826  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
827  tdvCheckException(dll_handle, eh_);
828  }
829  void setValue(bool val) {
830  dll_handle->TDVContext_putBool(handle_, val, &eh_);
831  tdvCheckException(dll_handle, eh_);
832  }
833  void setValue(void* ptr, int copy_sz = 0) {
834  dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), copy_sz, &eh_);
835  tdvCheckException(dll_handle, eh_);
836  }
837 };
838 
839 class ContextRef : public Context {
840 public:
841 
842  ContextRef(const DHPtr &dll_handle, HContext* handle) : Context(dll_handle ,handle, true) {}
843 
844  ContextRef* getContextPtr () const { return new ContextRef(dll_handle, handle_); }
845 
846  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
847  void operator=(T&& value) {
848  setValue(std::forward<T>(value));
849  }
850 };
851 
852 template<bool isConst>
853 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, long long index) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
854 {
855  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
856  tdvCheckException(base_.dll_handle, base_.eh_);
857  index_ = (index > -1) ? std::min<size_t>(static_cast<size_t>(index), length_) : length_;
858  if(index_ < length_) {
859  if(isObj_)
860  {
861  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
862  tdvCheckException(base_.dll_handle, base_.eh_);
863  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys), std::bind(&ContextArrayIterator::charsList_deleter, this, std::placeholders::_1));
864  curr_ = new Context(base_[keys_->operator[](index)]);
865  }
866  else
867  curr_ = new Context(base_[index]);
868  }
869 }
870 
871 template<bool isConst>
872 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, const std::string& key) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
873 {
874  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
875  tdvCheckException(base_.dll_handle, base_.eh_);
876  index_ = length_;
877  if(isObj_ && length_) {
878  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
879  tdvCheckException(base_.dll_handle, base_.eh_);
880  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys), std::bind(&ContextArrayIterator::charsList_deleter, this, std::placeholders::_1));
881  size_t i=0;
882  do {
883  if(!key.compare(keys_->operator[](i))) {
884  index_ = i;
885  curr_ = new Context(base_[key]);
886  break;
887  }
888  } while((++i)<length_);
889  }
890 }
891 
892 template<bool isConst>
893 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const ContextArrayIterator& iter) :
894  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_ ? new Context(*(iter.curr_)) : nullptr), isObj_(iter.isObj_), keys_(iter.keys_)
895 {};
896 
897 template<bool isConst>
898 Context::ContextArrayIterator<isConst>::ContextArrayIterator(ContextArrayIterator&& iter) :
899  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_), isObj_(iter.isObj_), keys_(std::move(iter.keys_)) {
900  iter.curr_ = nullptr;
901 }
902 
903 template<bool isConst>
904 Context::ContextArrayIterator<isConst>& Context::ContextArrayIterator<isConst>::operator++() {
905  if(curr_)
906  delete curr_;
907  index_ = std::min(index_+1, length_);
908  if(isObj_)
909  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
910  else
911  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
912  return *this;
913 }
914 
915 template<bool isConst>
916 Context::ContextArrayIterator<isConst> Context::ContextArrayIterator<isConst>::operator++(int) {
917  ContextArrayIterator tmp = *this;
918  index_ = std::min(index_+1, length_);
919  if(isObj_)
920  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
921  else
922  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
923  return tmp;
924 }
925 
926 inline Context::operator ContextRef()
927 {
928  return ContextRef(dll_handle, handle_);
929 }
930 
931 inline Context::Ref Context::operator[](const std::string& key) {
932  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key.c_str(), &eh_);
933  tdvCheckException(dll_handle, eh_);
934  return Ref(dll_handle, handle);
935 }
936 
937 inline Context::Ref Context::operator[](const char* key) {
938  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key, &eh_);
939  tdvCheckException(dll_handle, eh_);
940  return Ref(dll_handle, handle);
941 }
942 
943 inline Context::Ref Context::operator[](const int index) {
944  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
945  tdvCheckException(dll_handle, eh_);
946  return Ref(dll_handle, handle);
947 }
948 
949 inline const Context::Ref Context::operator[](const std::string& key) const {
950  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
951  tdvCheckException(dll_handle, eh_);
952  return Ref(dll_handle, handle);
953 }
954 
955 inline const Context::Ref Context::operator[](const char* key) const {
956  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
957  tdvCheckException(dll_handle, eh_);
958  return Ref(dll_handle, handle);
959 }
960 
961 inline const Context::Ref Context::operator[](const int index) const {
962  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
963  tdvCheckException(dll_handle, eh_);
964  return Ref(dll_handle, handle);
965 }
966 
967 inline Context::Ref Context::at(const char* key) {
968  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
969  tdvCheckException(dll_handle, eh_);
970  return Ref(dll_handle, handle);
971 }
972 
973 inline Context::Ref Context::at(const std::string& key) {
974  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
975  tdvCheckException(dll_handle, eh_);
976  return Ref(dll_handle, handle);
977 }
978 
979 inline Context::Ref Context::at(const int index) {
980  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
981  tdvCheckException(dll_handle, eh_);
982  return Ref(dll_handle, handle);
983 }
984 
985 inline const Context::Ref Context::at(const char* key) const {
986  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
987  tdvCheckException(dll_handle, eh_);
988  return Ref(dll_handle, handle);
989 }
990 
991 inline const Context::Ref Context::at(const std::string& key) const {
992  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
993  tdvCheckException(dll_handle, eh_);
994  return Ref(dll_handle, handle);
995 }
996 
997 inline const Context::Ref Context::at(const int index) const {
998  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
999  tdvCheckException(dll_handle, eh_);
1000  return Ref(dll_handle, handle);
1001 }
1002 
1003 
1004 namespace context_utils {
1005 
1006 static std::unordered_map<int, std::string> color_mode{{IRawImage::FORMAT_GRAY, "GRAY"}, {IRawImage::FORMAT_BGR, "BGR"}, {IRawImage::FORMAT_RGB, "RGB"}};
1007 
1008 inline void putImage(Context& ctx, const RawImage& raw_image) {
1009  ctx.clear();
1010  ctx["format"] = "NDARRAY";
1011  ctx["color_space"] = color_mode.at(raw_image.format);
1012  size_t channels = (raw_image.format == IRawImage::FORMAT_GRAY) ? 1 : 3;
1013  size_t copy_sz = raw_image.height * raw_image.width * channels * sizeof(uint8_t);
1014  if (raw_image.with_crop)
1015  {
1016  unsigned char* buff{nullptr};
1017  buff = ctx["blob"].setDataPtr(buff, copy_sz);
1018  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);
1019  size_t stride = raw_image.width*channels*sizeof(uint8_t);
1020  unsigned char* ptr = buff;
1021  for(int row=0; row<raw_image.height; ++row)
1022  {
1023  std::memcpy(ptr, raw_image.data+shift, stride);
1024  shift += raw_image.crop_info_data_image_width*channels*sizeof(uint8_t);
1025  ptr += stride;
1026  }
1027  }
1028  else
1029  ctx["blob"].setDataPtr(raw_image.data, copy_sz);
1030  ctx["dtype"] = "uint8_t";
1031  ctx["shape"].push_back(static_cast<int64_t>(raw_image.height));
1032  ctx["shape"].push_back(static_cast<int64_t>(raw_image.width));
1033  ctx["shape"].push_back(static_cast<int64_t>(channels));
1034 }
1035 
1036 inline void putImage(Context& ctx, unsigned char* data, size_t height, size_t width, pbio::IRawImage::Format format, bool copy) {
1037  ctx.clear();
1038  ctx["format"] = "NDARRAY";
1039  ctx["color_space"] = color_mode.at(format);
1040  size_t channels = (format == IRawImage::FORMAT_GRAY) ? 1 : 3;
1041  size_t copy_sz = copy ? height * width * channels * sizeof(uint8_t) : 0;
1042  ctx["blob"].setDataPtr(data, copy_sz);
1043  ctx["dtype"] = "uint8_t";
1044  ctx["shape"].push_back(static_cast<int64_t>(height));
1045  ctx["shape"].push_back(static_cast<int64_t>(width));
1046  ctx["shape"].push_back(static_cast<int64_t>(channels));
1047 }
1048 
1049 }
1050 }
1051 
1052 #endif // WITHOUT_PROCESSING_BLOCK
1053 #endif // CONTEXT_H
bool isString() const
checks whether the container is a long type string
Definition: Context.h:704
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:718
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:784
void erase(const char *str)
deletes the contents of the container-Context stored by key.
Definition: Context.h:746
void setString(const char *str)
adds a value of type string to the container
Definition: Context.h:529
void erase(const std::string &str)
deletes the contents of the container-Context stored by key.
Definition: Context.h:759
void saveToJsonFile(const char *path)
saves the contents of the container to a json file
Definition: Context.h:798
void reserve(const size_t size)
allocates memory for num elements in the array.
Definition: Context.h:771
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:568
std::vector< std::string > getKeys()
returns a list of keys in the container-Context
Definition: Context.h:400
void setString(const std::string &str)
adds a value of type std::string to the container
Definition: Context.h:542
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:690
void push_back(const Context &data)
adds a object to the container.
Definition: Context.h:437
bool compare(const Context &other) const
compares two Context objects
Definition: Context.h:385
bool isBool() const
checks whether the container is a bool type value
Definition: Context.h:662
void clear()
clears the contents of the container-Context.
Definition: Context.h:733
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:295
bool contains(const std::string &key) const
checks the existence of an element by a specific key
Definition: Context.h:368
bool isNone() const
checks if there are no elements in the container
Definition: Context.h:620
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:279
bool isLong() const
проверяет является ли контейнер значением типа long.
Definition: Context.h:676
void clear()
clears the contents of the container-Context.
Definition: Context.mm:129
long getLong() const
returns a long value from the container
Definition: Context.h:469
bool isObject() const
checks whether the container is an object
Definition: Context.h:648
Definition: Context.h:839
double getDouble() const
returns a double value from the container
Definition: Context.h:455
void setLong(long val)
adds a value of type long to the container
Definition: Context.h:555
bool getBool() const
returns a bool value from the container
Definition: Context.h:483
Context is an interface object for storing data and interacting with methods from the Processing Bloc...
Definition: Context.h:51
unsigned char * setDataPtr(void *ptr, int copy_sz=0)
adds a pointer to the data in the container
Definition: Context.h:596
std::string getString() const
returns a std::string value from the container
Definition: Context.h:497
void setBool(bool val)
adds a value of type bool to the container
Definition: Context.h:581
Context is an interface object for storing data and interacting with methods from the Processing Bloc...
Definition: Context.h:65
bool isArray() const
checks whether the container is an array
Definition: Context.h:634
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:931