3DiVi Face SDK  3.31.0
 Указатель Классы Пространства имен Файлы Функции Переменные Определения типов Перечисления Элементы перечислений Свойства Группы
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  ~charsList() {
66  for(size_t indx = 0; indx < length_; ++indx)
67  free(ptr_[indx]);
68  free(ptr_);
69  }
70  };
71 
72  const Context& base_;
73  size_t length_;
74  size_t index_;
75  Context* curr_;
76  const bool isObj_;
77  std::shared_ptr<charsList> keys_;
78  public:
79  typedef std::ptrdiff_t difference_type;
80  typedef std::forward_iterator_tag iterator_category;
81  typedef Context value_type;
82  typedef typename std::conditional<isConst, const value_type*, value_type*>::type pointer;
83  typedef typename std::conditional<isConst, const value_type&, value_type&>::type reference;
84  typedef const value_type& const_reference;
85 
86  ContextArrayIterator(const Context& ctx, long long index);
87  ContextArrayIterator(const Context& ctx, const std::string& key);
88 
89  ContextArrayIterator(const ContextArrayIterator& iter);
90  ContextArrayIterator& operator=(const ContextArrayIterator& iter) = delete;
91  ContextArrayIterator(ContextArrayIterator&& iter);
92  ContextArrayIterator& operator=(ContextArrayIterator&& iter) = delete;
93 
94  ~ContextArrayIterator() {
95  if(curr_)
96  delete curr_;
97  }
98 
99  bool operator==(const ContextArrayIterator& other) const {
100  return ((this->base_ == other.base_) && (this->curr_ == other.curr_));
101  }
102 
103  bool operator!=(const ContextArrayIterator& other) const {
104  return (!(this->base_ == other.base_) || !(this->curr_ == other.curr_));
105  }
106 
107  ContextArrayIterator& operator++();
108  ContextArrayIterator operator++(int);
109 
110  reference operator*() {
111  return *curr_;
112  }
113 
114  pointer operator->() {
115  return curr_;
116  }
117 
118  std::string key() const {
119  if (keys_ && index_ < length_)
120  return std::string(keys_->operator[](index_));
121  return std::string();
122  }
123  };
124 
125 public:
126  enum Format
127  {
128  FORMAT_BGR = 0,
129  FORMAT_RGB = 1,
130  FORMAT_BGRA8888 = 2,
131  FORMAT_YUV420 = 3,
132  FORMAT_YUV_NV12 = 4,
133  FORMAT_NV21 = 5,
134  };
135 
136 protected:
137 
138  Context(const DHPtr& dll_handle) : dll_handle(dll_handle), weak_(false), eh_(nullptr) {
139  handle_ = dll_handle->TDVContext_create(&eh_);
140  tdvCheckException(dll_handle, eh_);
141  }
142 
143  Context(const DHPtr& dll_handle, HContext* handle, bool weak = true) :
144  dll_handle(dll_handle), handle_(handle), weak_(weak), eh_(nullptr) { }
145 
146  Context(const DHPtr& dll_handle, const uint8_t* data, uint64_t dataSize) :
147  dll_handle(dll_handle),
148  weak_(false),
149  eh_(nullptr)
150  {
151  handle_ = dll_handle->TDVContext_createFromEncodedImage(data, dataSize, &eh_);
152  tdvCheckException(dll_handle, eh_);
153  }
154 
155  Context(const DHPtr& dll_handle, uint8_t* data, int32_t width, int32_t height, Format format, int32_t baseAngle) :
156  dll_handle(dll_handle),
157  weak_(false),
158  eh_(nullptr)
159  {
160  handle_ = dll_handle->TDVContext_createFromFrame(data, width, height, static_cast<int32_t>(format), baseAngle, &eh_);
161  tdvCheckException(dll_handle, eh_);
162  }
163 
164  Context(const DHPtr& dll_handle, const char* path) :
165  dll_handle(dll_handle),
166  weak_(false),
167  eh_(nullptr)
168  {
169  handle_ = dll_handle->TDVContext_createFromJsonFile(path, &eh_);
170  tdvCheckException(dll_handle, eh_);
171  }
172 
173  DHPtr dll_handle;
174  HContext* handle_;
175  bool weak_;
176  mutable ContextEH* eh_;
177 
178 public:
179 
180  typedef ContextRef Ref;
181 
182  virtual ~Context()
183  {
184  if(!weak_) {
185  dll_handle->TDVContext_destroy(handle_, &eh_);
186 
187 #if __cplusplus >= 201703L
188  if (eh_ && std::uncaught_exceptions() > 0)
189 #else
190  if (eh_ && std::uncaught_exception())
191 #endif
192  std::cerr << Error(dll_handle->TDVException_getErrorCode(eh_), dll_handle->TDVException_getMessage(eh_)).what();
193  else
194  tdvCheckException(dll_handle, eh_);
195  }
196  }
197 
198  Context(const Context& other) : dll_handle(other.dll_handle), weak_(false), eh_(nullptr) {
199  handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
200  tdvCheckException(dll_handle, eh_);
201  }
202 
203  Context& operator=(const Context& other) {
204  if (&other != this)
205  {
206  if(weak_)
207  dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
208  else {
209  HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_); // copy-swap
210  tdvCheckException(dll_handle, eh_);
211  std::swap(handle_, copy);
212  dll_handle->TDVContext_destroy(copy, &eh_);
213  }
214  tdvCheckException(dll_handle, eh_);
215  }
216  return *this;
217  }
218 
219  Context(Context&& other) : dll_handle(other.dll_handle), weak_(false), eh_(nullptr) {
220  if(other.weak_)
221  {
222  handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
223  tdvCheckException(dll_handle, eh_);
224  }
225  else
226  {
227  handle_ = other.handle_;
228  other.handle_ = nullptr;
229  other.weak_ = true;
230  }
231  }
232 
233  Context& operator=(Context&& other){
234  if (&other != this)
235  {
236  if(weak_) {
237  dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
238  tdvCheckException(dll_handle, eh_);
239  }
240  else
241  {
242  if(other.weak_)
243  {
244  HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_);
245  tdvCheckException(dll_handle, eh_);
246  std::swap(handle_, copy);
247  dll_handle->TDVContext_destroy(copy, &eh_);
248  tdvCheckException(dll_handle, eh_);
249  }
250  else
251  {
252  handle_ = other.handle_;
253  other.handle_ = nullptr;
254  other.weak_ = true;
255  }
256  }
257  }
258  return *this;
259  }
260 
261  operator ContextRef();
262 
271  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
272  Context& operator=(T&& value) {
273  setValue(std::forward<T>(value));
274  return *this;
275  }
276 
277  bool operator==(const Context& other) const
278  {return this->handle_ == other.handle_;}
279 
288  size_t size() const {
289  size_t lenght = dll_handle->TDVContext_getLength(handle_, &eh_);
290  tdvCheckException(dll_handle, eh_);
291  return lenght;
292  }
293 
294  ContextArrayIterator<> begin(){
295  return ContextArrayIterator<>(*this, 0);
296  }
297 
298  ContextArrayIterator<> end(){
299  return ContextArrayIterator<>(*this, -1);
300  }
301 
302  ContextArrayIterator<true> begin() const {
303  return ContextArrayIterator<true>(*this, 0);
304  }
305 
306  ContextArrayIterator<true> end() const {
307  return ContextArrayIterator<true>(*this, -1);
308  }
309 
310  ContextArrayIterator<true> cbegin() const {
311  return ContextArrayIterator<true>(*this, 0);
312  }
313 
314  ContextArrayIterator<true> cend() const {
315  return ContextArrayIterator<true>(*this, -1);
316  }
317 
326  Ref operator[](const std::string& key);
327  Ref operator[](const char* key);
328 
337  Ref operator[](const int index);
338 
339  const Ref operator[](const std::string& key) const;
340  const Ref operator[](const char* key) const;
341  const Ref operator[](const int index) const;
342 
343  Ref at(const char* key);
344  Ref at(const std::string& key);
345  Ref at(const int index);
346 
347  const Ref at(const char* key) const;
348  const Ref at(const std::string& key) const;
349  const Ref at(const int index) const;
350 
361  bool contains(const std::string& key) const {
362  bool result = dll_handle->TDVContext_contains(handle_, key.data(), &eh_);
363  tdvCheckException(dll_handle, eh_);
364 
365  return result;
366  }
367 
378  bool compare(const Context& other) const {
379  bool result = dll_handle->TDVContext_compare(handle_, other.handle_, &eh_);
380  tdvCheckException(dll_handle, eh_);
381 
382  return result;
383  }
384 
393  std::vector<std::string> getKeys(){
394  size_t length = size();
395 
396  auto keys = dll_handle->TDVContext_getKeys(handle_, length, &eh_);
397  tdvCheckException(dll_handle, eh_);
398 
399  std::vector<std::string> result;
400  result.reserve(length);
401  for (size_t i = 0; i < length; ++i)
402  result.emplace_back(keys[i]);
403  for (size_t i = 0; i < length; ++i)
404  dll_handle->TDVContext_freePtr(keys[i]);
405  dll_handle->TDVContext_freePtr(keys);
406 
407  return result;
408  }
409 
410  ContextArrayIterator<> find(const std::string& key) {
411  return ContextArrayIterator<>(*this, key);
412  }
413 
414  ContextArrayIterator<true> find(const std::string& key) const {
415  return ContextArrayIterator<true>(*this, key);
416  }
417 
418  template<typename T>
419  typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type push_back(T&& data) {
420  Context elem(dll_handle);
421  elem.setValue(std::forward<T>(data));
422  // use r-value version of push_back without a copy
423  push_back(std::move(elem));
424  }
425 
434  void push_back(const Context& data) {
435  dll_handle->TDVContext_pushBack(handle_, data.handle_, true, &eh_);
436  tdvCheckException(dll_handle, eh_);
437  }
438 
439  void push_back(Context&& data) {
440  dll_handle->TDVContext_pushBack(handle_, data.handle_, weak_, &eh_); // can not move weak object
441  tdvCheckException(dll_handle, eh_);
442  }
443 
452  double getDouble() const {
453  double ret = dll_handle->TDVContext_getDouble(handle_, &eh_);
454  tdvCheckException(dll_handle, eh_);
455  return ret;
456  }
457 
466  long getLong() const {
467  long ret = dll_handle->TDVContext_getLong(handle_, &eh_);
468  tdvCheckException(dll_handle, eh_);
469  return ret;
470  }
471 
480  bool getBool() const {
481  bool ret = dll_handle->TDVContext_getBool(handle_, &eh_);
482  tdvCheckException(dll_handle, eh_);
483  return ret;
484  }
485 
494  std::string getString() const {
495  unsigned long str_size = dll_handle->TDVContext_getStrSize(handle_, &eh_);
496  tdvCheckException(dll_handle, eh_);
497  std::string ret = dll_handle->TDVContext_getStr(handle_, &eh_);
498  tdvCheckException(dll_handle, eh_);
499  return ret; // copy elision (NRVO)
500  }
501 
510  unsigned char* getDataPtr() const {
511  unsigned char* ret = dll_handle->TDVContext_getDataPtr(handle_, &eh_);
512  tdvCheckException(dll_handle, eh_);
513  return ret;
514  }
515 
516  std::pair<uint8_t*, size_t> getBlobData() const {
517  size_t length;
518  uint8_t* ret = dll_handle->TDVContext_getBlobData(handle_, &length, &eh_);
519  tdvCheckException(dll_handle, eh_);
520  return {ret, length};
521  }
522 
523  pbio::DynamicTemplateIndex::Ptr getDynamicTemplateIndex() const {
524  void* index = dll_handle->TDVContext_getDynamicTemplateIndex(handle_, &eh_);
525  tdvCheckException(dll_handle, eh_);
526  return pbio::DynamicTemplateIndex::Ptr::make(dll_handle, index, true);
527  }
528 
529  pbio::ContextTemplate::Ptr getContextTemplate() const {
530  void* templ = dll_handle->TDVContext_getContextTemplate(handle_, &eh_);
531  tdvCheckException(dll_handle, eh_);
532  return pbio::ContextTemplate::Ptr::make(dll_handle, templ);
533  }
534 
543  void setString(const char* str) {
544  dll_handle->TDVContext_putStr(handle_, str, &eh_);
545  tdvCheckException(dll_handle, eh_);
546  }
547 
556  void setString(const std::string& str) {
557  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
558  tdvCheckException(dll_handle, eh_);
559  }
560 
569  void setLong(long val) {
570  dll_handle->TDVContext_putLong(handle_, val, &eh_);
571  tdvCheckException(dll_handle, eh_);
572  }
573 
582  void setDouble(double val) {
583  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
584  tdvCheckException(dll_handle, eh_);
585  }
586 
595  void setBool(bool val) {
596  dll_handle->TDVContext_putBool(handle_, val, &eh_);
597  tdvCheckException(dll_handle, eh_);
598  }
599 
610  unsigned char* setDataPtr(void* ptr, int copy_sz = 0) {
611  unsigned char* ret{nullptr};
612  if(copy_sz && !ptr)
613  ret = dll_handle->TDVContext_allocDataPtr(handle_, copy_sz, &eh_);
614  else
615  ret = dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), static_cast<uint64_t>(copy_sz), &eh_);
616  tdvCheckException(dll_handle, eh_);
617  return ret;
618  }
619 
620  unsigned char* setDataPtr(const void* ptr, int copy_sz) {
621  unsigned char* ret = dll_handle->TDVContext_putConstDataPtr(handle_, static_cast<const unsigned char*>(ptr), copy_sz, &eh_);
622  tdvCheckException(dll_handle, eh_);
623  return ret;
624  }
625 
626 
627  void setDynamicTemplateIndex(DynamicTemplateIndex::Ptr templateIndex) {
628  dll_handle->TDVContext_putDynamicTemplateIndex(handle_, templateIndex->_impl, &eh_);
629  tdvCheckException(dll_handle, eh_);
630  }
631 
632  void setContextTemplate(ContextTemplate::Ptr templ) {
633  dll_handle->TDVContext_putContextTemplate(handle_, templ->_impl, &eh_);
634  tdvCheckException(dll_handle, eh_);
635  }
636 
645  bool isNone() const {
646  bool value = dll_handle->TDVContext_isNone(handle_, &eh_);
647  tdvCheckException(dll_handle, eh_);
648  return value;
649  }
650 
659  bool isArray() const {
660  bool value = dll_handle->TDVContext_isArray(handle_, &eh_);
661  tdvCheckException(dll_handle, eh_);
662  return value;
663  }
664 
673  bool isObject() const {
674  bool value = dll_handle->TDVContext_isObject(handle_, &eh_);
675  tdvCheckException(dll_handle, eh_);
676  return value;
677  }
678 
687  bool isBool() const {
688  bool val = dll_handle->TDVContext_isBool(handle_, &eh_);
689  tdvCheckException(dll_handle, eh_);
690  return val;
691  }
692 
701  bool isLong() const {
702  bool val = dll_handle->TDVContext_isLong(handle_, &eh_);
703  tdvCheckException(dll_handle, eh_);
704  return val;
705  }
706 
715  bool isDouble() const {
716  bool val = dll_handle->TDVContext_isDouble(handle_, &eh_);
717  tdvCheckException(dll_handle, eh_);
718  return val;
719  }
720 
729  bool isString() const {
730  bool val = dll_handle->TDVContext_isString(handle_, &eh_);
731  tdvCheckException(dll_handle, eh_);
732  return val;
733  }
734 
743  bool isDataPtr() const {
744  bool val = dll_handle->TDVContext_isDataPtr(handle_, &eh_);
745  tdvCheckException(dll_handle, eh_);
746  return val;
747  }
748 
749  bool isDynamicTemplateIndex() const {
750  bool val = dll_handle->TDVContext_isDynamicTemplateIndex(handle_, &eh_);
751  tdvCheckException(dll_handle, eh_);
752  return val;
753  }
754 
755  bool isContextTemplate() const {
756  bool val = dll_handle->TDVContext_isContextTemplate(handle_, &eh_);
757  tdvCheckException(dll_handle, eh_);
758  return val;
759  }
760 
761  HContext* getHandle() {return handle_;}
762  const HContext* getHandle() const {return handle_;}
763 
770  void clear() {
771  dll_handle->TDVContext_clear(handle_, &eh_);
772  tdvCheckException(dll_handle,eh_);
773  }
774 
783  void erase(const char* str) {
784  dll_handle->TDVContext_erase(handle_, str, &eh_);
785  tdvCheckException(dll_handle,eh_);
786  }
787 
796  void erase(const std::string& str) {
797  erase(str.data());
798  }
799 
808  void reserve(const size_t size) {
809  dll_handle->TDVContext_reserve(handle_, size, &eh_);
810  tdvCheckException(dll_handle,eh_);
811  }
812 
821  void saveToJsonFile(std::string& path)
822  {
823  dll_handle->TDVContext_saveToJsonFile(handle_, path.c_str(), &eh_);
824  tdvCheckException(dll_handle,eh_);
825  }
826 
835  void saveToJsonFile(const char* path)
836  {
837  dll_handle->TDVContext_saveToJsonFile(handle_, path, &eh_);
838  tdvCheckException(dll_handle,eh_);
839  }
840 
841  std::string serializeToJson() const
842  {
843  const char* data = dll_handle->TDVContext_serializeToJson(handle_, &eh_);
844 
845  tdvCheckException(dll_handle, eh_);
846 
847  std::string result(data);
848 
849  dll_handle->TDVContext_deleteString(data, &eh_);
850 
851  tdvCheckException(dll_handle, eh_);
852 
853  return result;
854  }
855 
856 protected:
857 
858  void setValue(const char* str) {
859  dll_handle->TDVContext_putStr(handle_, str, &eh_);
860  tdvCheckException(dll_handle, eh_);
861  }
862  void setValue(const std::string& str) {
863  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
864  tdvCheckException(dll_handle, eh_);
865  }
866 
867  template<typename T, typename = typename std::enable_if<std::is_enum<T>::value || std::is_integral<T>::value>::type>
868  void setValue(T val) {
869  dll_handle->TDVContext_putLong(handle_, val, &eh_);
870  tdvCheckException(dll_handle, eh_);
871  }
872 
873  void setValue(double val) {
874  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
875  tdvCheckException(dll_handle, eh_);
876  }
877  void setValue(float val) {
878  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
879  tdvCheckException(dll_handle, eh_);
880  }
881  void setValue(bool val) {
882  dll_handle->TDVContext_putBool(handle_, val, &eh_);
883  tdvCheckException(dll_handle, eh_);
884  }
885  void setValue(void* ptr, int copy_sz = 0) {
886  dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), copy_sz, &eh_);
887  tdvCheckException(dll_handle, eh_);
888  }
889  void setValue(DynamicTemplateIndex::Ptr templateIndex) {
890  dll_handle->TDVContext_putDynamicTemplateIndex(handle_, templateIndex->_impl, &eh_);
891  tdvCheckException(dll_handle, eh_);
892  }
893  void setValue(ContextTemplate::Ptr templ) {
894  dll_handle->TDVContext_putContextTemplate(handle_, templ->_impl, &eh_);
895  tdvCheckException(dll_handle, eh_);
896  }
897 };
898 
899 class ContextRef : public Context {
900 public:
901 
902  ContextRef(const DHPtr &dll_handle, HContext* handle) : Context(dll_handle ,handle, true) {}
903 
904  ContextRef* getContextPtr () const { return new ContextRef(dll_handle, handle_); }
905 
906  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
907  void operator=(T&& value) {
908  setValue(std::forward<T>(value));
909  }
910 };
911 
912 template<bool isConst>
913 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, long long index) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
914 {
915  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
916  tdvCheckException(base_.dll_handle, base_.eh_);
917  index_ = (index > -1) ? (std::min<size_t>)(static_cast<size_t>(index), length_) : length_;
918  if(index_ < length_) {
919  if(isObj_)
920  {
921  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
922  tdvCheckException(base_.dll_handle, base_.eh_);
923  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys));
924  curr_ = new Context(base_[keys_->operator[](index)]);
925  }
926  else
927  curr_ = new Context(base_[index]);
928  }
929 }
930 
931 template<bool isConst>
932 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, const std::string& key) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
933 {
934  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
935  tdvCheckException(base_.dll_handle, base_.eh_);
936  index_ = length_;
937  if(isObj_ && length_) {
938  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
939  tdvCheckException(base_.dll_handle, base_.eh_);
940  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys));
941  size_t i=0;
942  do {
943  if(!key.compare(keys_->operator[](i))) {
944  index_ = i;
945  curr_ = new Context(base_[key]);
946  break;
947  }
948  } while((++i)<length_);
949  }
950 }
951 
952 template<bool isConst>
953 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const ContextArrayIterator& iter) :
954  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_ ? new Context(*(iter.curr_)) : nullptr), isObj_(iter.isObj_), keys_(iter.keys_)
955 {};
956 
957 template<bool isConst>
958 Context::ContextArrayIterator<isConst>::ContextArrayIterator(ContextArrayIterator&& iter) :
959  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_), isObj_(iter.isObj_), keys_(std::move(iter.keys_)) {
960  iter.curr_ = nullptr;
961 }
962 
963 template<bool isConst>
964 Context::ContextArrayIterator<isConst>& Context::ContextArrayIterator<isConst>::operator++() {
965  if(curr_)
966  delete curr_;
967  index_ = (std::min<size_t>)(index_+1, length_);
968  if(isObj_)
969  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
970  else
971  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
972  return *this;
973 }
974 
975 template<bool isConst>
976 Context::ContextArrayIterator<isConst> Context::ContextArrayIterator<isConst>::operator++(int) {
977  ContextArrayIterator tmp = *this;
978  index_ = (std::min<size_t>)(index_+1, length_);
979  if(isObj_)
980  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
981  else
982  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
983  return tmp;
984 }
985 
986 inline Context::operator ContextRef()
987 {
988  return ContextRef(dll_handle, handle_);
989 }
990 
991 inline Context::Ref Context::operator[](const std::string& key) {
992  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key.c_str(), &eh_);
993  tdvCheckException(dll_handle, eh_);
994  return Ref(dll_handle, handle);
995 }
996 
997 inline Context::Ref Context::operator[](const char* key) {
998  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key, &eh_);
999  tdvCheckException(dll_handle, eh_);
1000  return Ref(dll_handle, handle);
1001 }
1002 
1003 inline Context::Ref Context::operator[](const int index) {
1004  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1005  tdvCheckException(dll_handle, eh_);
1006  return Ref(dll_handle, handle);
1007 }
1008 
1009 inline const Context::Ref Context::operator[](const std::string& key) const {
1010  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1011  tdvCheckException(dll_handle, eh_);
1012  return Ref(dll_handle, handle);
1013 }
1014 
1015 inline const Context::Ref Context::operator[](const char* key) const {
1016  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1017  tdvCheckException(dll_handle, eh_);
1018  return Ref(dll_handle, handle);
1019 }
1020 
1021 inline const Context::Ref Context::operator[](const int index) const {
1022  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1023  tdvCheckException(dll_handle, eh_);
1024  return Ref(dll_handle, handle);
1025 }
1026 
1027 inline Context::Ref Context::at(const char* key) {
1028  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1029  tdvCheckException(dll_handle, eh_);
1030  return Ref(dll_handle, handle);
1031 }
1032 
1033 inline Context::Ref Context::at(const std::string& key) {
1034  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1035  tdvCheckException(dll_handle, eh_);
1036  return Ref(dll_handle, handle);
1037 }
1038 
1039 inline Context::Ref Context::at(const int index) {
1040  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1041  tdvCheckException(dll_handle, eh_);
1042  return Ref(dll_handle, handle);
1043 }
1044 
1045 inline const Context::Ref Context::at(const char* key) const {
1046  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1047  tdvCheckException(dll_handle, eh_);
1048  return Ref(dll_handle, handle);
1049 }
1050 
1051 inline const Context::Ref Context::at(const std::string& key) const {
1052  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1053  tdvCheckException(dll_handle, eh_);
1054  return Ref(dll_handle, handle);
1055 }
1056 
1057 inline const Context::Ref Context::at(const int index) const {
1058  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1059  tdvCheckException(dll_handle, eh_);
1060  return Ref(dll_handle, handle);
1061 }
1062 
1063 
1064 namespace context_utils {
1065 
1066 static std::unordered_map<int, std::string> color_mode{{IRawImage::FORMAT_GRAY, "GRAY"}, {IRawImage::FORMAT_BGR, "BGR"}, {IRawImage::FORMAT_RGB, "RGB"}};
1067 
1068 inline void putImage(Context& ctx, const RawImage& raw_image) {
1069  ctx.clear();
1070  ctx["format"] = "NDARRAY";
1071  ctx["color_space"] = color_mode.at(raw_image.format);
1072  size_t channels = (raw_image.format == IRawImage::FORMAT_GRAY) ? 1 : 3;
1073  size_t copy_sz = raw_image.height * raw_image.width * channels * sizeof(uint8_t);
1074  if (raw_image.with_crop)
1075  {
1076  unsigned char* buff{nullptr};
1077  buff = ctx["blob"].setDataPtr(buff, copy_sz);
1078  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);
1079  size_t stride = raw_image.width*channels*sizeof(uint8_t);
1080  unsigned char* ptr = buff;
1081  for(int row=0; row<raw_image.height; ++row)
1082  {
1083  std::memcpy(ptr, raw_image.data+shift, stride);
1084  shift += raw_image.crop_info_data_image_width*channels*sizeof(uint8_t);
1085  ptr += stride;
1086  }
1087  }
1088  else
1089  ctx["blob"].setDataPtr(raw_image.data, copy_sz);
1090  ctx["dtype"] = "uint8_t";
1091  ctx["shape"].push_back(static_cast<int64_t>(raw_image.height));
1092  ctx["shape"].push_back(static_cast<int64_t>(raw_image.width));
1093  ctx["shape"].push_back(static_cast<int64_t>(channels));
1094 }
1095 
1096 inline void putImage(Context& ctx, unsigned char* data, size_t height, size_t width, pbio::IRawImage::Format format, bool copy) {
1097  ctx.clear();
1098  ctx["format"] = "NDARRAY";
1099  ctx["color_space"] = color_mode.at(format);
1100  size_t channels = (format == IRawImage::FORMAT_GRAY) ? 1 : 3;
1101  size_t copy_sz = copy ? height * width * channels * sizeof(uint8_t) : 0;
1102  ctx["blob"].setDataPtr(data, copy_sz);
1103  ctx["dtype"] = "uint8_t";
1104  ctx["shape"].push_back(static_cast<int64_t>(height));
1105  ctx["shape"].push_back(static_cast<int64_t>(width));
1106  ctx["shape"].push_back(static_cast<int64_t>(channels));
1107 }
1108 
1109 }
1110 }
1111 
1112 #endif // WITHOUT_PROCESSING_BLOCK
1113 #endif // CONTEXT_H
bool isString() const
проверяет является ли контейнер значением типа string.
Definition: Context.h:729
Интерфейсный объект для создания других интерфейсных объектов.
Definition: FacerecService.h:64
RGB, 24 бита на пиксел, 8 бит на канал.
Definition: IRawImage.h:60
bool isDataPtr() const
проверяет является ли контейнер указателем на данные
Definition: Context.h:743
Интерфейсный объект, хранящий образец лица.
Definition: RawSample.h:49
void saveToJsonFile(std::string &path)
сохраняет содержимое контейнера в json файл
Definition: Context.h:821
void erase(const char *str)
удаляет содержимое контейнера-Context хранящиеся по ключу.
Definition: Context.h:783
void setString(const char *str)
добавляет значение типа string в контейнер
Definition: Context.h:543
void erase(const std::string &str)
удаляет содержимое контейнера-Context хранящиеся по ключу.
Definition: Context.h:796
void saveToJsonFile(const char *path)
сохраняет содержимое контейнера в json файл
Definition: Context.h:835
void reserve(const size_t size)
выделяет память под num элементов в массиве.
Definition: Context.h:808
Оттенки серого, 8 бит на пиксел.
Definition: IRawImage.h:53
void setDouble(double val)
добавляет значение типа double в контейнер
Definition: Context.h:582
std::vector< std::string > getKeys()
возвращает список ключей в контейнере-Context.
Definition: Context.h:393
void setString(const std::string &str)
добавляет значение типа std::string в контейнер
Definition: Context.h:556
unsigned char * getDataPtr() const
возвращает указатель на данные из контейнера
Definition: Context.h:510
bool isDouble() const
проверяет является ли контейнер значением типа double.
Definition: Context.h:715
void push_back(const Context &data)
добавляет объект в контейнер.
Definition: Context.h:434
bool compare(const Context &other) const
сравнивает два объекта Context.
Definition: Context.h:378
bool isBool() const
проверяет является ли контейнер значением типа bool.
Definition: Context.h:687
void clear()
очищает содержимое контейнера-Context.
Definition: Context.h:770
BGR, 24 бита на пиксел, 8 бит на канал.
Definition: IRawImage.h:67
size_t size() const
Получить размер контейнера.
Definition: Context.h:288
bool contains(const std::string &key) const
проверяет существование элемента по определённому ключу
Definition: Context.h:361
bool isNone() const
проверяет нет ли в контейнере элементов
Definition: Context.h:645
Класс исключений, выбрасываемых при возникновении ошибок.
Definition: Error.h:26
Context & operator=(T &&value)
добавляет значение в контейнер
Definition: Context.h:272
bool isLong() const
проверяет является ли контейнер значением типа long.
Definition: Context.h:701
void clear()
очищает содержимое контейнера-Context.
Definition: Context.mm:166
LightSmartPtr< DynamicTemplateIndex >::tPtr Ptr
Псевдоним для типа умного указателя на DynamicTemplateIndex.
Definition: DynamicTemplateIndex.h:34
long getLong() const
возвращает значение типа long из контейнера
Definition: Context.h:466
bool isObject() const
проверяет является ли контейнер объектом
Definition: Context.h:673
Definition: Context.h:899
double getDouble() const
возвращает значение типа double из контейнера
Definition: Context.h:452
void setLong(long val)
добавляет значение типа long в контейнер
Definition: Context.h:569
bool getBool() const
возвращает значение типа bool из контейнера
Definition: Context.h:480
Context - интерфейсный объект для хранения данных и взаимодействия с методами из Processing Block API...
Definition: Context.h:48
unsigned char * setDataPtr(void *ptr, int copy_sz=0)
добавляет указатель на данные в контейнер
Definition: Context.h:610
LightSmartPtr< ContextTemplate >::tPtr Ptr
Псевдоним для типа умного указателя на ContextTemplate.
Definition: ContextTemplate.h:47
std::string getString() const
возвращает значение типа std::string из контейнера
Definition: Context.h:494
void setBool(bool val)
добавляет значение типа bool в контейнер
Definition: Context.h:595
Context - интерфейсный объект для хранения данных и взаимодействия с методами из Processing Block API...
Definition: Context.h:67
bool isArray() const
проверяет является ли контейнере массивом
Definition: Context.h:659
int height
Высота изображения.
Definition: RawImage.h:117
const unsigned char * data
Указатель на данные изображения. Все пикселы должны быть сохранены последовательно, строка за строкой, без разрывов.
Definition: RawImage.h:118
Format
Формат данных изображения.
Definition: IRawImage.h:46
Структура, предоставляющая данные изображения в &quot;сыром&quot; формате и опциональную информацию для обрезки...
Definition: RawImage.h:113
int width
Ширина изображения.
Definition: RawImage.h:116
Ref operator[](const std::string &key)
индексация по ключу.
Definition: Context.h:991