3DiVi Face SDK  3.21.0
 Указатель Классы Пространства имен Файлы Функции Переменные Определения типов Перечисления Элементы перечислений Свойства Группы
Context.h
1 #ifndef CONTEXT_H
2 #define CONTEXT_H
3 
4 #ifndef WITHOUT_PROCESSING_BLOCK
5 
6 #if defined(_WIN32)
7 #define NOMINMAX
8 #endif
9 
10 #include <functional>
11 #include <iostream>
12 #include <memory>
13 #include <string>
14 #include <unordered_map>
15 
16 #include <pbio/DllHandle.h>
17 #include <pbio/RawImage.h>
18 #include <pbio/RawSample.h>
19 
20 
21 namespace pbio {
22 
23 typedef LightSmartPtr<import::DllHandle>::tPtr DHPtr;
24 
25 inline void tdvCheckException(const DHPtr& dll_handle, ContextEH*& out_exception) {
26  if(out_exception)
27  {
28  auto err = Error(dll_handle->TDVException_getErrorCode(out_exception), dll_handle->TDVException_getMessage(out_exception));
29  dll_handle->TDVException_deleteException(out_exception);
30  out_exception = nullptr;
31  throw err;
32  }
33 }
34 
35 class ContextRef;
36 
37 class Context
38 {
39 
40  friend class FacerecService;
41  friend class RawSample;
42 
43  template<bool isConst=false>
44  class ContextArrayIterator
45  {
46  class charsList {
47  public:
48  size_t length_;
49  char** ptr_;
50  charsList(size_t length, char** ptr) : length_(length), ptr_(ptr) {}
51  char* operator[](size_t index) const {
52  return (index<length_) ? *(ptr_+index) : nullptr;
53  }
54  };
55 
56  const Context& base_;
57  size_t length_;
58  size_t index_;
59  Context* curr_;
60  const bool isObj_;
61  std::shared_ptr<charsList> keys_;
62 
63  void charsList_deleter(charsList* ptr) {
64  for(size_t indx=0; indx < ptr->length_; ++indx)
65  base_.dll_handle->TDVContext_freePtr(ptr->ptr_[indx]);
66  base_.dll_handle->TDVContext_freePtr(ptr->ptr_);
67  };
68 
69  public:
70  typedef std::ptrdiff_t difference_type;
71  typedef std::forward_iterator_tag iterator_category;
72  typedef Context value_type;
73  typedef typename std::conditional<isConst, const value_type*, value_type*>::type pointer;
74  typedef typename std::conditional<isConst, const value_type&, value_type&>::type reference;
75  typedef const value_type& const_reference;
76 
77  ContextArrayIterator(const Context& ctx, long long index);
78  ContextArrayIterator(const Context& ctx, const std::string& key);
79 
80  ContextArrayIterator(const ContextArrayIterator& iter);
81  ContextArrayIterator& operator=(const ContextArrayIterator& iter) = delete;
82  ContextArrayIterator(ContextArrayIterator&& iter);
83  ContextArrayIterator& operator=(ContextArrayIterator&& iter) = delete;
84 
85  ~ContextArrayIterator() {
86  if(curr_)
87  delete curr_;
88  }
89 
90  bool operator==(const ContextArrayIterator& other) const {
91  return ((this->base_ == other.base_) && (this->curr_ == other.curr_));
92  }
93 
94  bool operator!=(const ContextArrayIterator& other) const {
95  return (!(this->base_ == other.base_) || !(this->curr_ == other.curr_));
96  }
97 
98  ContextArrayIterator& operator++();
99  ContextArrayIterator operator++(int);
100 
101  reference operator*() {
102  return *curr_;
103  }
104 
105  pointer operator->() {
106  return curr_;
107  }
108 
109  std::string key() const {
110  if (keys_ && index_ < length_)
111  return std::string(keys_->operator[](index_));
112  return std::string();
113  }
114  };
115 
116 protected:
117 
118  Context(const DHPtr &dll_handle) : dll_handle(dll_handle), weak_(false), eh_(nullptr) {
119  handle_ = dll_handle->TDVContext_create(&eh_);
120  tdvCheckException(dll_handle, eh_);
121  }
122 
123  Context(const DHPtr &dll_handle, HContext* handle, bool weak = true) : dll_handle(dll_handle), weak_(weak), eh_(nullptr) {
124  if(weak_)
125  handle_ = handle;
126  else
127  {
128  handle_ = dll_handle->TDVContext_clone(handle, &eh_);
129  tdvCheckException(dll_handle, eh_);
130  }
131  }
132 
133  DHPtr dll_handle;
134  HContext* handle_;
135  bool weak_;
136  mutable ContextEH* eh_;
137 
138 public:
139 
140  typedef ContextRef Ref;
141 
142  virtual ~Context()
143  {
144  if(!weak_) {
145  dll_handle->TDVContext_destroy(handle_, &eh_);
146  if (eh_ && std::uncaught_exception())
147  std::cerr << Error(dll_handle->TDVException_getErrorCode(eh_), dll_handle->TDVException_getMessage(eh_)).what();
148  else
149  tdvCheckException(dll_handle, eh_);
150  }
151  }
152 
153  Context(const Context& other) : dll_handle(other.dll_handle), weak_(false), eh_(nullptr) {
154  handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
155  tdvCheckException(dll_handle, eh_);
156  }
157 
158  Context& operator=(const Context& other) {
159  if (&other != this)
160  {
161  if(weak_)
162  dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
163  else {
164  HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_); // copy-swap
165  tdvCheckException(dll_handle, eh_);
166  std::swap(handle_, copy);
167  dll_handle->TDVContext_destroy(copy, &eh_);
168  }
169  tdvCheckException(dll_handle, eh_);
170  }
171  return *this;
172  }
173 
174  Context(Context&& other) : dll_handle(other.dll_handle), weak_(false), eh_(nullptr) {
175  if(other.weak_)
176  {
177  handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
178  tdvCheckException(dll_handle, eh_);
179  }
180  else
181  {
182  handle_ = other.handle_;
183  other.handle_ = nullptr;
184  other.weak_ = true;
185  }
186  }
187 
188  Context& operator=(Context&& other){
189  if (&other != this)
190  {
191  if(weak_) {
192  dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
193  tdvCheckException(dll_handle, eh_);
194  }
195  else
196  {
197  if(other.weak_)
198  {
199  HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_);
200  tdvCheckException(dll_handle, eh_);
201  std::swap(handle_, copy);
202  dll_handle->TDVContext_destroy(copy, &eh_);
203  tdvCheckException(dll_handle, eh_);
204  }
205  else
206  {
207  handle_ = other.handle_;
208  other.handle_ = nullptr;
209  other.weak_ = true;
210  }
211  }
212  }
213  return *this;
214  }
215 
216  operator ContextRef();
217 
218  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
219  Context& operator=(T&& value) {
220  setValue(std::forward<T>(value));
221  return *this;
222  }
223 
224  bool operator==(const Context& other) const
225  {return this->handle_ == other.handle_;}
226 
227  size_t size() const {
228  size_t lenght = dll_handle->TDVContext_getLength(handle_, &eh_);
229  tdvCheckException(dll_handle, eh_);
230  return lenght;
231  }
232 
233  ContextArrayIterator<> begin(){
234  return ContextArrayIterator<>(*this, 0);
235  }
236 
237  ContextArrayIterator<> end(){
238  return ContextArrayIterator<>(*this, -1);
239  }
240 
241  ContextArrayIterator<true> begin() const {
242  return ContextArrayIterator<true>(*this, 0);
243  }
244 
245  ContextArrayIterator<true> end() const {
246  return ContextArrayIterator<true>(*this, -1);
247  }
248 
249  ContextArrayIterator<true> cbegin() const {
250  return ContextArrayIterator<true>(*this, 0);
251  }
252 
253  ContextArrayIterator<true> cend() const {
254  return ContextArrayIterator<true>(*this, -1);
255  }
256 
257  Ref operator[](const std::string& key);
258  Ref operator[](const char* key);
259  Ref operator[](const int index);
260 
261  const Ref operator[](const std::string& key) const;
262  const Ref operator[](const char* key) const;
263  const Ref operator[](const int index) const;
264 
265  Ref at(const char* key);
266  Ref at(const std::string& key);
267  Ref at(const int index);
268 
269  const Ref at(const char* key) const;
270  const Ref at(const std::string& key) const;
271  const Ref at(const int index) const;
272 
273  bool contains(const std::string& key) const {
274  if (isObject())
275  {
276  /* discard return value as it does not produce new allocation */
277  static_cast<void>(dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_));
278  if(eh_)
279  {
280  dll_handle->TDVException_deleteException(eh_);
281  eh_ = nullptr;
282  }
283  else
284  return true;
285  }
286  return false;
287  }
288 
289  ContextArrayIterator<> find(const std::string& key) {
290  return ContextArrayIterator<>(*this, key);
291  }
292 
293  ContextArrayIterator<true> find(const std::string& key) const {
294  return ContextArrayIterator<true>(*this, key);
295  }
296 
297  template<typename T>
298  typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type push_back(T&& data) {
299  Context elem(dll_handle);
300  elem.setValue(std::forward<T>(data));
301  // use r-value version of push_back without a copy
302  push_back(std::move(elem));
303  }
304 
305  void push_back(const Context& data) {
306  dll_handle->TDVContext_pushBack(handle_, data.handle_, true, &eh_);
307  tdvCheckException(dll_handle, eh_);
308  }
309 
310  void push_back(Context&& data) {
311  dll_handle->TDVContext_pushBack(handle_, data.handle_, weak_, &eh_); // can not move weak object
312  tdvCheckException(dll_handle, eh_);
313  }
314 
315  double getDouble() const {
316  double ret = dll_handle->TDVContext_getDouble(handle_, &eh_);
317  tdvCheckException(dll_handle, eh_);
318  return ret;
319  }
320 
321  long getLong() const {
322  long ret = dll_handle->TDVContext_getLong(handle_, &eh_);
323  tdvCheckException(dll_handle, eh_);
324  return ret;
325  }
326 
327  bool getBool() const {
328  bool ret = dll_handle->TDVContext_getBool(handle_, &eh_);
329  tdvCheckException(dll_handle, eh_);
330  return ret;
331  }
332 
333  std::string getString() const {
334  unsigned long str_size = dll_handle->TDVContext_getStrSize(handle_, &eh_);
335  tdvCheckException(dll_handle, eh_);
336  std::string ret;
337  ret.resize(str_size);
338  dll_handle->TDVContext_getStr(handle_, &ret[0], &eh_);
339  tdvCheckException(dll_handle, eh_);
340  return ret; // copy elision (NRVO)
341  }
342 
343  unsigned char* getDataPtr() const {
344  unsigned char* ret = dll_handle->TDVContext_getDataPtr(handle_, &eh_);
345  tdvCheckException(dll_handle, eh_);
346  return ret;
347  }
348 
349  void setString(const char* str) {
350  dll_handle->TDVContext_putStr(handle_, str, &eh_);
351  tdvCheckException(dll_handle, eh_);
352  }
353  void setString(const std::string& str) {
354  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
355  tdvCheckException(dll_handle, eh_);
356  }
357 
358  void setLong(long val) {
359  dll_handle->TDVContext_putLong(handle_, val, &eh_);
360  tdvCheckException(dll_handle, eh_);
361  }
362 
363  void setDouble(double val) {
364  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
365  tdvCheckException(dll_handle, eh_);
366  }
367 
368  void setBool(bool val) {
369  dll_handle->TDVContext_putBool(handle_, val, &eh_);
370  tdvCheckException(dll_handle, eh_);
371  }
372 
373  unsigned char* setDataPtr(void* ptr, int copy_sz = 0) {
374  unsigned char* ret{nullptr};
375  if(copy_sz && !ptr)
376  ret = dll_handle->TDVContext_allocDataPtr(handle_, copy_sz, &eh_);
377  else
378  ret = dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), copy_sz, &eh_);
379  tdvCheckException(dll_handle, eh_);
380  return ret;
381  }
382 
383  unsigned char* setDataPtr(const void* ptr, int copy_sz) {
384  unsigned char* ret = dll_handle->TDVContext_putConstDataPtr(handle_, static_cast<const unsigned char*>(ptr), copy_sz, &eh_);
385  tdvCheckException(dll_handle, eh_);
386  return ret;
387  }
388 
389  bool isNone() const {
390  bool value = dll_handle->TDVContext_isNone(handle_, &eh_);
391  tdvCheckException(dll_handle, eh_);
392  return value;
393  }
394 
395  bool isArray() const {
396  bool value = dll_handle->TDVContext_isArray(handle_, &eh_);
397  tdvCheckException(dll_handle, eh_);
398  return value;
399  }
400 
401  bool isObject() const {
402  bool value = dll_handle->TDVContext_isObject(handle_, &eh_);
403  tdvCheckException(dll_handle, eh_);
404  return value;
405  }
406 
407  bool isBool() const {
408  bool val = dll_handle->TDVContext_isBool(handle_, &eh_);
409  tdvCheckException(dll_handle, eh_);
410  return val;
411  }
412 
413  bool isLong() const {
414  bool val = dll_handle->TDVContext_isLong(handle_, &eh_);
415  tdvCheckException(dll_handle, eh_);
416  return val;
417  }
418 
419  bool isDouble() const {
420  bool val = dll_handle->TDVContext_isDouble(handle_, &eh_);
421  tdvCheckException(dll_handle, eh_);
422  return val;
423  }
424 
425  bool isString() const {
426  bool val = dll_handle->TDVContext_isString(handle_, &eh_);
427  tdvCheckException(dll_handle, eh_);
428  return val;
429  }
430 
431  bool isDataPtr() const {
432  bool val = dll_handle->TDVContext_isDataPtr(handle_, &eh_);
433  tdvCheckException(dll_handle, eh_);
434  return val;
435  }
436 
437  HContext* getHandle() {return handle_;}
438  const HContext* getHandle() const {return handle_;}
439 
440  void clear() {
441  dll_handle->TDVContext_clear(handle_, &eh_);
442  tdvCheckException(dll_handle,eh_);
443  }
444 
445 protected:
446 
447  void setValue(const char* str) {
448  dll_handle->TDVContext_putStr(handle_, str, &eh_);
449  tdvCheckException(dll_handle, eh_);
450  }
451  void setValue(const std::string& str) {
452  dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
453  tdvCheckException(dll_handle, eh_);
454  }
455 
456  template<typename T, typename = typename std::enable_if<std::is_enum<T>::value || std::is_integral<T>::value>::type>
457  void setValue(T val) {
458  dll_handle->TDVContext_putLong(handle_, val, &eh_);
459  tdvCheckException(dll_handle, eh_);
460  }
461 
462  void setValue(double val) {
463  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
464  tdvCheckException(dll_handle, eh_);
465  }
466  void setValue(float val) {
467  dll_handle->TDVContext_putDouble(handle_, val, &eh_);
468  tdvCheckException(dll_handle, eh_);
469  }
470  void setValue(bool val) {
471  dll_handle->TDVContext_putBool(handle_, val, &eh_);
472  tdvCheckException(dll_handle, eh_);
473  }
474  void setValue(void* ptr, int copy_sz = 0) {
475  dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), copy_sz, &eh_);
476  tdvCheckException(dll_handle, eh_);
477  }
478 };
479 
480 class ContextRef : public Context {
481 public:
482 
483  ContextRef(const DHPtr &dll_handle, HContext* handle) : Context(dll_handle ,handle, true) {}
484 
485  template <typename T, typename = typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type>
486  void operator=(T&& value) {
487  setValue(std::forward<T>(value));
488  }
489 };
490 
491 template<bool isConst>
492 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, long long index) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
493 {
494  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
495  tdvCheckException(base_.dll_handle, base_.eh_);
496  index_ = (index > -1) ? std::min<size_t>(static_cast<size_t>(index), length_) : length_;
497  if(index_ < length_) {
498  if(isObj_)
499  {
500  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
501  tdvCheckException(base_.dll_handle, base_.eh_);
502  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys), std::bind(&ContextArrayIterator::charsList_deleter, this, std::placeholders::_1));
503  curr_ = new Context(base_[keys_->operator[](index)]);
504  }
505  else
506  curr_ = new Context(base_[index]);
507  }
508 }
509 
510 template<bool isConst>
511 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const Context& ctx, const std::string& key) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
512 {
513  length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
514  tdvCheckException(base_.dll_handle, base_.eh_);
515  index_ = length_;
516  if(isObj_ && length_) {
517  auto keys = base_.dll_handle->TDVContext_getKeys(base_.handle_, length_, &(base_.eh_));
518  tdvCheckException(base_.dll_handle, base_.eh_);
519  keys_ = std::shared_ptr<charsList>(new charsList(length_, keys), std::bind(&ContextArrayIterator::charsList_deleter, this, std::placeholders::_1));
520  size_t i=0;
521  do {
522  if(!key.compare(keys_->operator[](i))) {
523  index_ = i;
524  curr_ = new Context(base_[key]);
525  break;
526  }
527  } while((++i)<length_);
528  }
529 }
530 
531 template<bool isConst>
532 Context::ContextArrayIterator<isConst>::ContextArrayIterator(const ContextArrayIterator& iter) :
533  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_ ? new Context(*(iter.curr_)) : nullptr), isObj_(iter.isObj_), keys_(iter.keys_)
534 {};
535 
536 template<bool isConst>
537 Context::ContextArrayIterator<isConst>::ContextArrayIterator(ContextArrayIterator&& iter) :
538  base_(iter.base_), length_(iter.length_), index_(iter.index_), curr_(iter.curr_), isObj_(iter.isObj_), keys_(std::move(iter.keys_)) {
539  iter.curr_ = nullptr;
540 }
541 
542 template<bool isConst>
543 Context::ContextArrayIterator<isConst>& Context::ContextArrayIterator<isConst>::operator++() {
544  if(curr_)
545  delete curr_;
546  index_ = std::min(index_+1, length_);
547  if(isObj_)
548  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
549  else
550  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
551  return *this;
552 }
553 
554 template<bool isConst>
555 Context::ContextArrayIterator<isConst> Context::ContextArrayIterator<isConst>::operator++(int) {
556  ContextArrayIterator tmp = *this;
557  index_ = std::min(index_+1, length_);
558  if(isObj_)
559  curr_ = (index_ < length_) ? new Context(base_[keys_->operator[](index_)]) : nullptr;
560  else
561  curr_ = (index_ < length_) ? new Context(base_[index_]) : nullptr;
562  return tmp;
563 }
564 
565 inline Context::operator ContextRef()
566 {
567  return ContextRef(dll_handle, handle_);
568 }
569 
570 inline Context::Ref Context::operator[](const std::string& key) {
571  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key.c_str(), &eh_);
572  tdvCheckException(dll_handle, eh_);
573  return Ref(dll_handle, handle);
574 }
575 
576 inline Context::Ref Context::operator[](const char* key) {
577  HContext* handle = dll_handle->TDVContext_getOrInsertByKey(handle_, key, &eh_);
578  tdvCheckException(dll_handle, eh_);
579  return Ref(dll_handle, handle);
580 }
581 
582 inline Context::Ref Context::operator[](const int index) {
583  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
584  tdvCheckException(dll_handle, eh_);
585  return Ref(dll_handle, handle);
586 }
587 
588 inline const Context::Ref Context::operator[](const std::string& key) const {
589  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
590  tdvCheckException(dll_handle, eh_);
591  return Ref(dll_handle, handle);
592 }
593 
594 inline const Context::Ref Context::operator[](const char* key) const {
595  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
596  tdvCheckException(dll_handle, eh_);
597  return Ref(dll_handle, handle);
598 }
599 
600 inline const Context::Ref Context::operator[](const int index) const {
601  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
602  tdvCheckException(dll_handle, eh_);
603  return Ref(dll_handle, handle);
604 }
605 
606 inline Context::Ref Context::at(const char* key) {
607  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
608  tdvCheckException(dll_handle, eh_);
609  return Ref(dll_handle, handle);
610 }
611 
612 inline Context::Ref Context::at(const std::string& key) {
613  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
614  tdvCheckException(dll_handle, eh_);
615  return Ref(dll_handle, handle);
616 }
617 
618 inline Context::Ref Context::at(const int index) {
619  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
620  tdvCheckException(dll_handle, eh_);
621  return Ref(dll_handle, handle);
622 }
623 
624 inline const Context::Ref Context::at(const char* key) const {
625  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key, &eh_);
626  tdvCheckException(dll_handle, eh_);
627  return Ref(dll_handle, handle);
628 }
629 
630 inline const Context::Ref Context::at(const std::string& key) const {
631  HContext* handle = dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_);
632  tdvCheckException(dll_handle, eh_);
633  return Ref(dll_handle, handle);
634 }
635 
636 inline const Context::Ref Context::at(const int index) const {
637  HContext* handle = dll_handle->TDVContext_getByIndex(handle_, index, &eh_);
638  tdvCheckException(dll_handle, eh_);
639  return Ref(dll_handle, handle);
640 }
641 
642 
643 namespace context_utils {
644 
645 static std::unordered_map<int, std::string> color_mode{{IRawImage::FORMAT_GRAY, "GRAY"}, {IRawImage::FORMAT_BGR, "BGR"}, {IRawImage::FORMAT_RGB, "RGB"}};
646 
647 inline void putImage(Context& ctx, const RawImage& raw_image) {
648  ctx.clear();
649  ctx["format"] = "NDARRAY";
650  ctx["color_space"] = color_mode.at(raw_image.format);
651  size_t channels = (raw_image.format == IRawImage::FORMAT_GRAY) ? 1 : 3;
652  size_t copy_sz = raw_image.height * raw_image.width * channels * sizeof(uint8_t);
653  if (raw_image.with_crop)
654  {
655  unsigned char* buff{nullptr};
656  buff = ctx["blob"].setDataPtr(buff, copy_sz);
657  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);
658  size_t stride = raw_image.width*channels*sizeof(uint8_t);
659  unsigned char* ptr = buff;
660  for(int row=0; row<raw_image.height; ++row)
661  {
662  std::memcpy(ptr, raw_image.data+shift, stride);
663  shift += raw_image.crop_info_data_image_width*channels*sizeof(uint8_t);
664  ptr += stride;
665  }
666  }
667  else
668  ctx["blob"].setDataPtr(raw_image.data, copy_sz);
669  ctx["dtype"] = "uint8_t";
670  ctx["shape"].push_back(static_cast<int64_t>(raw_image.height));
671  ctx["shape"].push_back(static_cast<int64_t>(raw_image.width));
672  ctx["shape"].push_back(static_cast<int64_t>(channels));
673 }
674 
675 inline void putImage(Context& ctx, unsigned char* data, size_t height, size_t width, pbio::IRawImage::Format format, bool copy) {
676  ctx.clear();
677  ctx["format"] = "NDARRAY";
678  ctx["color_space"] = color_mode.at(format);
679  size_t channels = (format == IRawImage::FORMAT_GRAY) ? 1 : 3;
680  size_t copy_sz = copy ? height * width * channels * sizeof(uint8_t) : 0;
681  ctx["blob"].setDataPtr(data, copy_sz);
682  ctx["dtype"] = "uint8_t";
683  ctx["shape"].push_back(static_cast<int64_t>(height));
684  ctx["shape"].push_back(static_cast<int64_t>(width));
685  ctx["shape"].push_back(static_cast<int64_t>(channels));
686 }
687 
688 }
689 }
690 
691 #endif // WITHOUT_PROCESSING_BLOCK
692 #endif // CONTEXT_H
Интерфейсный объект для создания других интерфейсных объектов.
Definition: FacerecService.h:64
RGB, 24 бита на пиксел, 8 бит на канал.
Definition: IRawImage.h:60
Интерфейсный объект, хранящий образец лица.
Definition: RawSample.h:49
Оттенки серого, 8 бит на пиксел.
Definition: IRawImage.h:53
RawSample - Интерфейсный объект, хранящий образец лица.
BGR, 24 бита на пиксел, 8 бит на канал.
Definition: IRawImage.h:67
Класс исключений, выбрасываемых при возникновении ошибок.
Definition: Error.h:26
Definition: Context.h:480
Definition: Context.h:37
Format
Формат данных изображения.
Definition: IRawImage.h:46