3DiVi Face SDK  3.26.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  };
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  pbio::DynamicTemplateIndex::Ptr getDynamicTemplateIndex() const {
522  void* index = dll_handle->TDVContext_getDynamicTemplateIndex(handle_, &eh_);
523  tdvCheckException(dll_handle, eh_);
524  return pbio::DynamicTemplateIndex::Ptr::make(dll_handle, index, true);
525  }
526 
527  pbio::ContextTemplate::Ptr getContextTemplate() const {
528  void* templ = dll_handle->TDVContext_getContextTemplate(handle_, &eh_);
529  tdvCheckException(dll_handle, eh_);
530  return pbio::ContextTemplate::Ptr::make(dll_handle, templ);
531  }
532 
541  void setString(const char* str) {
542  dll_handle->TDVContext_putStr(handle_, str, &eh_);
543  tdvCheckException(dll_handle, eh_);
544  }
545 
554  void setString(const std::string& str) {
555  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
556  tdvCheckException(dll_handle, eh_);
557  }
558 
567  void setLong(long val) {
568  dll_handle->TDVContext_putLong(handle_, val, &eh_);
569  tdvCheckException(dll_handle, eh_);
570  }
571 
580  void setDouble(double val) {
581  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
582  tdvCheckException(dll_handle, eh_);
583  }
584 
593  void setBool(bool val) {
594  dll_handle->TDVContext_putBool(handle_, val, &eh_);
595  tdvCheckException(dll_handle, eh_);
596  }
597 
608  unsigned char* setDataPtr(void* ptr, int copy_sz = 0) {
609  unsigned char* ret{nullptr};
610  if(copy_sz && !ptr)
611  ret = dll_handle->TDVContext_allocDataPtr(handle_, copy_sz, &eh_);
612  else
613  ret = dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), static_cast<uint64_t>(copy_sz), &eh_);
614  tdvCheckException(dll_handle, eh_);
615  return ret;
616  }
617 
618  unsigned char* setDataPtr(const void* ptr, int copy_sz) {
619  unsigned char* ret = dll_handle->TDVContext_putConstDataPtr(handle_, static_cast<const unsigned char*>(ptr), copy_sz, &eh_);
620  tdvCheckException(dll_handle, eh_);
621  return ret;
622  }
623 
624 
625  void setDynamicTemplateIndex(DynamicTemplateIndex::Ptr templateIndex) {
626  dll_handle->TDVContext_putDynamicTemplateIndex(handle_, templateIndex->_impl, &eh_);
627  tdvCheckException(dll_handle, eh_);
628  }
629 
630  void setContextTemplate(ContextTemplate::Ptr templ) {
631  dll_handle->TDVContext_putContextTemplate(handle_, templ->_impl, &eh_);
632  tdvCheckException(dll_handle, eh_);
633  }
634 
643  bool isNone() const {
644  bool value = dll_handle->TDVContext_isNone(handle_, &eh_);
645  tdvCheckException(dll_handle, eh_);
646  return value;
647  }
648 
657  bool isArray() const {
658  bool value = dll_handle->TDVContext_isArray(handle_, &eh_);
659  tdvCheckException(dll_handle, eh_);
660  return value;
661  }
662 
671  bool isObject() const {
672  bool value = dll_handle->TDVContext_isObject(handle_, &eh_);
673  tdvCheckException(dll_handle, eh_);
674  return value;
675  }
676 
685  bool isBool() const {
686  bool val = dll_handle->TDVContext_isBool(handle_, &eh_);
687  tdvCheckException(dll_handle, eh_);
688  return val;
689  }
690 
699  bool isLong() const {
700  bool val = dll_handle->TDVContext_isLong(handle_, &eh_);
701  tdvCheckException(dll_handle, eh_);
702  return val;
703  }
704 
713  bool isDouble() const {
714  bool val = dll_handle->TDVContext_isDouble(handle_, &eh_);
715  tdvCheckException(dll_handle, eh_);
716  return val;
717  }
718 
727  bool isString() const {
728  bool val = dll_handle->TDVContext_isString(handle_, &eh_);
729  tdvCheckException(dll_handle, eh_);
730  return val;
731  }
732 
741  bool isDataPtr() const {
742  bool val = dll_handle->TDVContext_isDataPtr(handle_, &eh_);
743  tdvCheckException(dll_handle, eh_);
744  return val;
745  }
746 
747  bool isDynamicTemplateIndex() const {
748  bool val = dll_handle->TDVContext_isDynamicTemplateIndex(handle_, &eh_);
749  tdvCheckException(dll_handle, eh_);
750  return val;
751  }
752 
753  bool isContextTemplate() const {
754  bool val = dll_handle->TDVContext_isContextTemplate(handle_, &eh_);
755  tdvCheckException(dll_handle, eh_);
756  return val;
757  }
758 
759  HContext* getHandle() {return handle_;}
760  const HContext* getHandle() const {return handle_;}
761 
768  void clear() {
769  dll_handle->TDVContext_clear(handle_, &eh_);
770  tdvCheckException(dll_handle,eh_);
771  }
772 
781  void erase(const char* str) {
782  dll_handle->TDVContext_erase(handle_, str, &eh_);
783  tdvCheckException(dll_handle,eh_);
784  }
785 
794  void erase(const std::string& str) {
795  erase(str.data());
796  }
797 
806  void reserve(const size_t size) {
807  dll_handle->TDVContext_reserve(handle_, size, &eh_);
808  tdvCheckException(dll_handle,eh_);
809  }
810 
819  void saveToJsonFile(std::string& path)
820  {
821  dll_handle->TDVContext_saveToJsonFile(handle_, path.c_str(), &eh_);
822  tdvCheckException(dll_handle,eh_);
823  }
824 
833  void saveToJsonFile(const char* path)
834  {
835  dll_handle->TDVContext_saveToJsonFile(handle_, path, &eh_);
836  tdvCheckException(dll_handle,eh_);
837  }
838 
839  std::string serializeToJson() const
840  {
841  const char* data = dll_handle->TDVContext_serializeToJson(handle_, &eh_);
842 
843  tdvCheckException(dll_handle, eh_);
844 
845  std::string result(data);
846 
847  dll_handle->TDVContext_deleteString(data, &eh_);
848 
849  tdvCheckException(dll_handle, eh_);
850 
851  return result;
852  }
853 
854 protected:
855 
856  void setValue(const char* str) {
857  dll_handle->TDVContext_putStr(handle_, str, &eh_);
858  tdvCheckException(dll_handle, eh_);
859  }
860  void setValue(const std::string& str) {
861  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
862  tdvCheckException(dll_handle, eh_);
863  }
864 
865  template<typename T, typename = typename std::enable_if<std::is_enum<T>::value || std::is_integral<T>::value>::type>
866  void setValue(T val) {
867  dll_handle->TDVContext_putLong(handle_, val, &eh_);
868  tdvCheckException(dll_handle, eh_);
869  }
870 
871  void setValue(double val) {
872  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
873  tdvCheckException(dll_handle, eh_);
874  }
875  void setValue(float val) {
876  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
877  tdvCheckException(dll_handle, eh_);
878  }
879  void setValue(bool val) {
880  dll_handle->TDVContext_putBool(handle_, val, &eh_);
881  tdvCheckException(dll_handle, eh_);
882  }
883  void setValue(void* ptr, int copy_sz = 0) {
884  dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), copy_sz, &eh_);
885  tdvCheckException(dll_handle, eh_);
886  }
887  void setValue(DynamicTemplateIndex::Ptr templateIndex) {
888  dll_handle->TDVContext_putDynamicTemplateIndex(handle_, templateIndex->_impl, &eh_);
889  tdvCheckException(dll_handle, eh_);
890  }
891  void setValue(ContextTemplate::Ptr templ) {
892  dll_handle->TDVContext_putContextTemplate(handle_, templ->_impl, &eh_);
893  tdvCheckException(dll_handle, eh_);
894  }
895 };
896 
897 class ContextRef : public Context {
898 public:
899 
900  ContextRef(const DHPtr &dll_handle, HContext* handle) : Context(dll_handle ,handle, true) {}
901 
902  ContextRef* getContextPtr () const { return new ContextRef(dll_handle, handle_); }
903 
904  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
905  void operator=(T&& value) {
906  setValue(std::forward<T>(value));
907  }
908 };
909 
910 template<bool isConst>
911 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, long long index) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
912 {
913  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
914  tdvCheckException(base_.dll_handle, base_.eh_);
915  index_ = (index > -1) ? (std::min<size_t>)(static_cast<size_t>(index), length_) : length_;
916  if(index_ < length_) {
917  if(isObj_)
918  {
919  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
920  tdvCheckException(base_.dll_handle, base_.eh_);
921  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys), std::bind(&ContextArrayIterator::charsList_deleter, this, std::placeholders::_1));
922  curr_ = new Context(base_[keys_->operator[](index)]);
923  }
924  else
925  curr_ = new Context(base_[index]);
926  }
927 }
928 
929 template<bool isConst>
930 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, const std::string& key) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
931 {
932  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
933  tdvCheckException(base_.dll_handle, base_.eh_);
934  index_ = length_;
935  if(isObj_ && length_) {
936  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
937  tdvCheckException(base_.dll_handle, base_.eh_);
938  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys), std::bind(&ContextArrayIterator::charsList_deleter, this, std::placeholders::_1));
939  size_t i=0;
940  do {
941  if(!key.compare(keys_->operator[](i))) {
942  index_ = i;
943  curr_ = new Context(base_[key]);
944  break;
945  }
946  } while((++i)<length_);
947  }
948 }
949 
950 template<bool isConst>
951 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const ContextArrayIterator& iter) :
952  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_ ? new Context(*(iter.curr_)) : nullptr), isObj_(iter.isObj_), keys_(iter.keys_)
953 {};
954 
955 template<bool isConst>
956 Context::ContextArrayIterator<isConst>::ContextArrayIterator(ContextArrayIterator&& iter) :
957  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_), isObj_(iter.isObj_), keys_(std::move(iter.keys_)) {
958  iter.curr_ = nullptr;
959 }
960 
961 template<bool isConst>
962 Context::ContextArrayIterator<isConst>& Context::ContextArrayIterator<isConst>::operator++() {
963  if(curr_)
964  delete curr_;
965  index_ = (std::min<size_t>)(index_+1, length_);
966  if(isObj_)
967  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
968  else
969  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
970  return *this;
971 }
972 
973 template<bool isConst>
974 Context::ContextArrayIterator<isConst> Context::ContextArrayIterator<isConst>::operator++(int) {
975  ContextArrayIterator tmp = *this;
976  index_ = (std::min<size_t>)(index_+1, length_);
977  if(isObj_)
978  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
979  else
980  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
981  return tmp;
982 }
983 
984 inline Context::operator ContextRef()
985 {
986  return ContextRef(dll_handle, handle_);
987 }
988 
989 inline Context::Ref Context::operator[](const std::string& key) {
990  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key.c_str(), &eh_);
991  tdvCheckException(dll_handle, eh_);
992  return Ref(dll_handle, handle);
993 }
994 
995 inline Context::Ref Context::operator[](const char* key) {
996  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key, &eh_);
997  tdvCheckException(dll_handle, eh_);
998  return Ref(dll_handle, handle);
999 }
1000 
1001 inline Context::Ref Context::operator[](const int index) {
1002  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1003  tdvCheckException(dll_handle, eh_);
1004  return Ref(dll_handle, handle);
1005 }
1006 
1007 inline const Context::Ref Context::operator[](const std::string& key) const {
1008  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1009  tdvCheckException(dll_handle, eh_);
1010  return Ref(dll_handle, handle);
1011 }
1012 
1013 inline const Context::Ref Context::operator[](const char* key) const {
1014  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1015  tdvCheckException(dll_handle, eh_);
1016  return Ref(dll_handle, handle);
1017 }
1018 
1019 inline const Context::Ref Context::operator[](const int index) const {
1020  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1021  tdvCheckException(dll_handle, eh_);
1022  return Ref(dll_handle, handle);
1023 }
1024 
1025 inline Context::Ref Context::at(const char* key) {
1026  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1027  tdvCheckException(dll_handle, eh_);
1028  return Ref(dll_handle, handle);
1029 }
1030 
1031 inline Context::Ref Context::at(const std::string& key) {
1032  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1033  tdvCheckException(dll_handle, eh_);
1034  return Ref(dll_handle, handle);
1035 }
1036 
1037 inline Context::Ref Context::at(const int index) {
1038  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1039  tdvCheckException(dll_handle, eh_);
1040  return Ref(dll_handle, handle);
1041 }
1042 
1043 inline const Context::Ref Context::at(const char* key) const {
1044  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
1045  tdvCheckException(dll_handle, eh_);
1046  return Ref(dll_handle, handle);
1047 }
1048 
1049 inline const Context::Ref Context::at(const std::string& key) const {
1050  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
1051  tdvCheckException(dll_handle, eh_);
1052  return Ref(dll_handle, handle);
1053 }
1054 
1055 inline const Context::Ref Context::at(const int index) const {
1056  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
1057  tdvCheckException(dll_handle, eh_);
1058  return Ref(dll_handle, handle);
1059 }
1060 
1061 
1062 namespace context_utils {
1063 
1064 static std::unordered_map<int, std::string> color_mode{{IRawImage::FORMAT_GRAY, "GRAY"}, {IRawImage::FORMAT_BGR, "BGR"}, {IRawImage::FORMAT_RGB, "RGB"}};
1065 
1066 inline void putImage(Context& ctx, const RawImage& raw_image) {
1067  ctx.clear();
1068  ctx["format"] = "NDARRAY";
1069  ctx["color_space"] = color_mode.at(raw_image.format);
1070  size_t channels = (raw_image.format == IRawImage::FORMAT_GRAY) ? 1 : 3;
1071  size_t copy_sz = raw_image.height * raw_image.width * channels * sizeof(uint8_t);
1072  if (raw_image.with_crop)
1073  {
1074  unsigned char* buff{nullptr};
1075  buff = ctx["blob"].setDataPtr(buff, copy_sz);
1076  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);
1077  size_t stride = raw_image.width*channels*sizeof(uint8_t);
1078  unsigned char* ptr = buff;
1079  for(int row=0; row<raw_image.height; ++row)
1080  {
1081  std::memcpy(ptr, raw_image.data+shift, stride);
1082  shift += raw_image.crop_info_data_image_width*channels*sizeof(uint8_t);
1083  ptr += stride;
1084  }
1085  }
1086  else
1087  ctx["blob"].setDataPtr(raw_image.data, copy_sz);
1088  ctx["dtype"] = "uint8_t";
1089  ctx["shape"].push_back(static_cast<int64_t>(raw_image.height));
1090  ctx["shape"].push_back(static_cast<int64_t>(raw_image.width));
1091  ctx["shape"].push_back(static_cast<int64_t>(channels));
1092 }
1093 
1094 inline void putImage(Context& ctx, unsigned char* data, size_t height, size_t width, pbio::IRawImage::Format format, bool copy) {
1095  ctx.clear();
1096  ctx["format"] = "NDARRAY";
1097  ctx["color_space"] = color_mode.at(format);
1098  size_t channels = (format == IRawImage::FORMAT_GRAY) ? 1 : 3;
1099  size_t copy_sz = copy ? height * width * channels * sizeof(uint8_t) : 0;
1100  ctx["blob"].setDataPtr(data, copy_sz);
1101  ctx["dtype"] = "uint8_t";
1102  ctx["shape"].push_back(static_cast<int64_t>(height));
1103  ctx["shape"].push_back(static_cast<int64_t>(width));
1104  ctx["shape"].push_back(static_cast<int64_t>(channels));
1105 }
1106 
1107 }
1108 }
1109 
1110 #endif // WITHOUT_PROCESSING_BLOCK
1111 #endif // CONTEXT_H
bool isString() const
проверяет является ли контейнер значением типа string.
Definition: Context.h:727
Интерфейсный объект для создания других интерфейсных объектов.
Definition: FacerecService.h:64
RGB, 24 бита на пиксел, 8 бит на канал.
Definition: IRawImage.h:60
bool isDataPtr() const
проверяет является ли контейнер указателем на данные
Definition: Context.h:741
Интерфейсный объект, хранящий образец лица.
Definition: RawSample.h:49
void saveToJsonFile(std::string &path)
сохраняет содержимое контейнера в json файл
Definition: Context.h:819
void erase(const char *str)
удаляет содержимое контейнера-Context хранящиеся по ключу.
Definition: Context.h:781
void setString(const char *str)
добавляет значение типа string в контейнер
Definition: Context.h:541
void erase(const std::string &str)
удаляет содержимое контейнера-Context хранящиеся по ключу.
Definition: Context.h:794
void saveToJsonFile(const char *path)
сохраняет содержимое контейнера в json файл
Definition: Context.h:833
void reserve(const size_t size)
выделяет память под num элементов в массиве.
Definition: Context.h:806
Оттенки серого, 8 бит на пиксел.
Definition: IRawImage.h:53
void setDouble(double val)
добавляет значение типа double в контейнер
Definition: Context.h:580
std::vector< std::string > getKeys()
возвращает список ключей в контейнере-Context.
Definition: Context.h:402
void setString(const std::string &str)
добавляет значение типа std::string в контейнер
Definition: Context.h:554
unsigned char * getDataPtr() const
возвращает указатель на данные из контейнера
Definition: Context.h:515
bool isDouble() const
проверяет является ли контейнер значением типа double.
Definition: Context.h:713
void push_back(const Context &data)
добавляет объект в контейнер.
Definition: Context.h:439
bool compare(const Context &other) const
сравнивает два объекта Context.
Definition: Context.h:387
bool isBool() const
проверяет является ли контейнер значением типа bool.
Definition: Context.h:685
void clear()
очищает содержимое контейнера-Context.
Definition: Context.h:768
BGR, 24 бита на пиксел, 8 бит на канал.
Definition: IRawImage.h:67
size_t size() const
Получить размер контейнера.
Definition: Context.h:297
bool contains(const std::string &key) const
проверяет существование элемента по определённому ключу
Definition: Context.h:370
bool isNone() const
проверяет нет ли в контейнере элементов
Definition: Context.h:643
Класс исключений, выбрасываемых при возникновении ошибок.
Definition: Error.h:26
Context & operator=(T &&value)
добавляет значение в контейнер
Definition: Context.h:281
bool isLong() const
проверяет является ли контейнер значением типа long.
Definition: Context.h:699
void clear()
очищает содержимое контейнера-Context.
Definition: Context.mm:166
LightSmartPtr< DynamicTemplateIndex >::tPtr Ptr
Псевдоним для типа умного указателя на DynamicTemplateIndex.
Definition: DynamicTemplateIndex.h:34
long getLong() const
возвращает значение типа long из контейнера
Definition: Context.h:471
bool isObject() const
проверяет является ли контейнер объектом
Definition: Context.h:671
Definition: Context.h:897
double getDouble() const
возвращает значение типа double из контейнера
Definition: Context.h:457
void setLong(long val)
добавляет значение типа long в контейнер
Definition: Context.h:567
bool getBool() const
возвращает значение типа bool из контейнера
Definition: Context.h:485
Context - интерфейсный объект для хранения данных и взаимодействия с методами из Processing Block API...
Definition: Context.h:48
unsigned char * setDataPtr(void *ptr, int copy_sz=0)
добавляет указатель на данные в контейнер
Definition: Context.h:608
LightSmartPtr< ContextTemplate >::tPtr Ptr
Псевдоним для типа умного указателя на ContextTemplate.
Definition: ContextTemplate.h:47
std::string getString() const
возвращает значение типа std::string из контейнера
Definition: Context.h:499
void setBool(bool val)
добавляет значение типа bool в контейнер
Definition: Context.h:593
Context - интерфейсный объект для хранения данных и взаимодействия с методами из Processing Block API...
Definition: Context.h:67
bool isArray() const
проверяет является ли контейнере массивом
Definition: Context.h:657
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:989