4 #ifndef WITHOUT_PROCESSING_BLOCK
14 #include <unordered_map>
16 #include <pbio/DllHandle.h>
17 #include <pbio/RawImage.h>
23 typedef LightSmartPtr<import::DllHandle>::tPtr DHPtr;
25 inline void tdvCheckException(
const DHPtr& dll_handle, ContextEH*& out_exception) {
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;
43 template<
bool isConst=false>
44 class ContextArrayIterator
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;
61 std::shared_ptr<charsList> keys_;
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_);
70 typedef std::ptrdiff_t difference_type;
71 typedef std::forward_iterator_tag iterator_category;
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;
77 ContextArrayIterator(
const Context& ctx,
long long index);
78 ContextArrayIterator(
const Context& ctx,
const std::string& key);
80 ContextArrayIterator(
const ContextArrayIterator& iter);
81 ContextArrayIterator& operator=(
const ContextArrayIterator& iter) =
delete;
82 ContextArrayIterator(ContextArrayIterator&& iter);
83 ContextArrayIterator& operator=(ContextArrayIterator&& iter) =
delete;
85 ~ContextArrayIterator() {
90 bool operator==(
const ContextArrayIterator& other)
const {
91 return ((this->base_ == other.base_) && (this->curr_ == other.curr_));
94 bool operator!=(
const ContextArrayIterator& other)
const {
95 return (!(this->base_ == other.base_) || !(this->curr_ == other.curr_));
98 ContextArrayIterator& operator++();
99 ContextArrayIterator operator++(
int);
101 reference operator*() {
105 pointer operator->() {
109 std::string key()
const {
110 if (keys_ && index_ < length_)
111 return std::string(keys_->operator[](index_));
112 return std::string();
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_);
123 Context(
const DHPtr &dll_handle, HContext* handle,
bool weak =
true) : dll_handle(dll_handle), weak_(weak), eh_(
nullptr) {
128 handle_ = dll_handle->TDVContext_clone(handle, &eh_);
129 tdvCheckException(dll_handle, eh_);
136 mutable ContextEH* eh_;
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();
149 tdvCheckException(dll_handle, eh_);
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_);
162 dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
164 HContext* copy = dll_handle->TDVContext_clone(other.handle_, &eh_);
165 tdvCheckException(dll_handle, eh_);
166 std::swap(handle_, copy);
167 dll_handle->TDVContext_destroy(copy, &eh_);
169 tdvCheckException(dll_handle, eh_);
174 Context(
Context&& other) : dll_handle(other.dll_handle), weak_(
false), eh_(
nullptr) {
177 handle_ = dll_handle->TDVContext_clone(other.handle_, &eh_);
178 tdvCheckException(dll_handle, eh_);
182 handle_ = other.handle_;
183 other.handle_ =
nullptr;
192 dll_handle->TDVContext_copy(other.handle_, handle_, &eh_);
193 tdvCheckException(dll_handle, eh_);
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_);
207 handle_ = other.handle_;
208 other.handle_ =
nullptr;
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));
224 bool operator==(
const Context& other)
const
225 {
return this->handle_ == other.handle_;}
227 size_t size()
const {
228 size_t lenght = dll_handle->TDVContext_getLength(handle_, &eh_);
229 tdvCheckException(dll_handle, eh_);
233 ContextArrayIterator<> begin(){
234 return ContextArrayIterator<>(*
this, 0);
237 ContextArrayIterator<> end(){
238 return ContextArrayIterator<>(*
this, -1);
241 ContextArrayIterator<true> begin()
const {
242 return ContextArrayIterator<true>(*
this, 0);
245 ContextArrayIterator<true> end()
const {
246 return ContextArrayIterator<true>(*
this, -1);
249 ContextArrayIterator<true> cbegin()
const {
250 return ContextArrayIterator<true>(*
this, 0);
253 ContextArrayIterator<true> cend()
const {
254 return ContextArrayIterator<true>(*
this, -1);
257 Ref operator[](
const std::string& key);
258 Ref operator[](
const char* key);
259 Ref operator[](
const int index);
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;
265 Ref at(
const char* key);
266 Ref at(
const std::string& key);
267 Ref at(
const int index);
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;
273 bool contains(
const std::string& key)
const {
277 static_cast<void>(dll_handle->TDVContext_getByKey(handle_, key.c_str(), &eh_));
280 dll_handle->TDVException_deleteException(eh_);
289 ContextArrayIterator<> find(
const std::string& key) {
290 return ContextArrayIterator<>(*
this, key);
293 ContextArrayIterator<true> find(
const std::string& key)
const {
294 return ContextArrayIterator<true>(*
this, key);
298 typename std::enable_if<!std::is_base_of<Context, typename std::decay<T>::type>::value>::type push_back(T&& data) {
300 elem.setValue(std::forward<T>(data));
302 push_back(std::move(elem));
305 void push_back(
const Context& data) {
306 dll_handle->TDVContext_pushBack(handle_, data.handle_,
true, &eh_);
307 tdvCheckException(dll_handle, eh_);
310 void push_back(
Context&& data) {
311 dll_handle->TDVContext_pushBack(handle_, data.handle_, weak_, &eh_);
312 tdvCheckException(dll_handle, eh_);
315 double getDouble()
const {
316 double ret = dll_handle->TDVContext_getDouble(handle_, &eh_);
317 tdvCheckException(dll_handle, eh_);
321 long getLong()
const {
322 long ret = dll_handle->TDVContext_getLong(handle_, &eh_);
323 tdvCheckException(dll_handle, eh_);
327 bool getBool()
const {
328 bool ret = dll_handle->TDVContext_getBool(handle_, &eh_);
329 tdvCheckException(dll_handle, eh_);
333 std::string getString()
const {
334 unsigned long str_size = dll_handle->TDVContext_getStrSize(handle_, &eh_);
335 tdvCheckException(dll_handle, eh_);
337 ret.resize(str_size);
338 dll_handle->TDVContext_getStr(handle_, &ret[0], &eh_);
339 tdvCheckException(dll_handle, eh_);
343 unsigned char* getDataPtr()
const {
344 unsigned char* ret = dll_handle->TDVContext_getDataPtr(handle_, &eh_);
345 tdvCheckException(dll_handle, eh_);
349 void setString(
const char* str) {
350 dll_handle->TDVContext_putStr(handle_, str, &eh_);
351 tdvCheckException(dll_handle, eh_);
353 void setString(
const std::string& str) {
354 dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
355 tdvCheckException(dll_handle, eh_);
358 void setLong(
long val) {
359 dll_handle->TDVContext_putLong(handle_, val, &eh_);
360 tdvCheckException(dll_handle, eh_);
363 void setDouble(
double val) {
364 dll_handle->TDVContext_putDouble(handle_, val, &eh_);
365 tdvCheckException(dll_handle, eh_);
368 void setBool(
bool val) {
369 dll_handle->TDVContext_putBool(handle_, val, &eh_);
370 tdvCheckException(dll_handle, eh_);
373 unsigned char* setDataPtr(
void* ptr,
int copy_sz = 0) {
374 unsigned char* ret{
nullptr};
376 ret = dll_handle->TDVContext_allocDataPtr(handle_, copy_sz, &eh_);
378 ret = dll_handle->TDVContext_putDataPtr(handle_, static_cast<unsigned char*>(ptr), copy_sz, &eh_);
379 tdvCheckException(dll_handle, eh_);
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_);
389 bool isNone()
const {
390 bool value = dll_handle->TDVContext_isNone(handle_, &eh_);
391 tdvCheckException(dll_handle, eh_);
395 bool isArray()
const {
396 bool value = dll_handle->TDVContext_isArray(handle_, &eh_);
397 tdvCheckException(dll_handle, eh_);
401 bool isObject()
const {
402 bool value = dll_handle->TDVContext_isObject(handle_, &eh_);
403 tdvCheckException(dll_handle, eh_);
407 bool isBool()
const {
408 bool val = dll_handle->TDVContext_isBool(handle_, &eh_);
409 tdvCheckException(dll_handle, eh_);
413 bool isLong()
const {
414 bool val = dll_handle->TDVContext_isLong(handle_, &eh_);
415 tdvCheckException(dll_handle, eh_);
419 bool isDouble()
const {
420 bool val = dll_handle->TDVContext_isDouble(handle_, &eh_);
421 tdvCheckException(dll_handle, eh_);
425 bool isString()
const {
426 bool val = dll_handle->TDVContext_isString(handle_, &eh_);
427 tdvCheckException(dll_handle, eh_);
431 bool isDataPtr()
const {
432 bool val = dll_handle->TDVContext_isDataPtr(handle_, &eh_);
433 tdvCheckException(dll_handle, eh_);
437 HContext* getHandle() {
return handle_;}
438 const HContext* getHandle()
const {
return handle_;}
441 dll_handle->TDVContext_clear(handle_, &eh_);
442 tdvCheckException(dll_handle,eh_);
447 void setValue(
const char* str) {
448 dll_handle->TDVContext_putStr(handle_, str, &eh_);
449 tdvCheckException(dll_handle, eh_);
451 void setValue(
const std::string& str) {
452 dll_handle->TDVContext_putStr(handle_, str.c_str(), &eh_);
453 tdvCheckException(dll_handle, eh_);
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_);
462 void setValue(
double val) {
463 dll_handle->TDVContext_putDouble(handle_, val, &eh_);
464 tdvCheckException(dll_handle, eh_);
466 void setValue(
float val) {
467 dll_handle->TDVContext_putDouble(handle_, val, &eh_);
468 tdvCheckException(dll_handle, eh_);
470 void setValue(
bool val) {
471 dll_handle->TDVContext_putBool(handle_, val, &eh_);
472 tdvCheckException(dll_handle, eh_);
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_);
483 ContextRef(
const DHPtr &dll_handle, HContext* handle) :
Context(dll_handle ,handle,
true) {}
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));
491 template<
bool isConst>
492 Context::ContextArrayIterator<isConst>::ContextArrayIterator(
const Context& ctx,
long long index) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
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_) {
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)]);
506 curr_ =
new Context(base_[index]);
510 template<
bool isConst>
511 Context::ContextArrayIterator<isConst>::ContextArrayIterator(
const Context& ctx,
const std::string& key) : base_(ctx), curr_(nullptr), isObj_(ctx.isObject())
513 length_ = base_.dll_handle->TDVContext_getLength(base_.handle_, &(base_.eh_));
514 tdvCheckException(base_.dll_handle, base_.eh_);
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));
522 if(!key.compare(keys_->operator[](i))) {
524 curr_ =
new Context(base_[key]);
527 }
while((++i)<length_);
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_)
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;
542 template<
bool isConst>
543 Context::ContextArrayIterator<isConst>& Context::ContextArrayIterator<isConst>::operator++() {
546 index_ = std::min(index_+1, length_);
548 curr_ = (index_ < length_) ?
new Context(base_[keys_->operator[](index_)]) :
nullptr;
550 curr_ = (index_ < length_) ?
new Context(base_[index_]) :
nullptr;
554 template<
bool isConst>
555 Context::ContextArrayIterator<isConst> Context::ContextArrayIterator<isConst>::operator++(
int) {
556 ContextArrayIterator tmp = *
this;
557 index_ = std::min(index_+1, length_);
559 curr_ = (index_ < length_) ?
new Context(base_[keys_->operator[](index_)]) :
nullptr;
561 curr_ = (index_ < length_) ?
new Context(base_[index_]) :
nullptr;
565 inline Context::operator ContextRef()
567 return ContextRef(dll_handle, handle_);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
643 namespace context_utils {
647 inline void putImage(Context& ctx,
const RawImage& raw_image) {
649 ctx[
"format"] =
"NDARRAY";
650 ctx[
"color_space"] = color_mode.at(raw_image.format);
652 size_t copy_sz = raw_image.height * raw_image.width * channels *
sizeof(uint8_t);
653 if (raw_image.with_crop)
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)
662 std::memcpy(ptr, raw_image.data+shift, stride);
663 shift += raw_image.crop_info_data_image_width*channels*
sizeof(uint8_t);
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));
675 inline void putImage(Context& ctx,
unsigned char* data,
size_t height,
size_t width,
pbio::IRawImage::Format format,
bool copy) {
677 ctx[
"format"] =
"NDARRAY";
678 ctx[
"color_space"] = color_mode.at(format);
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));
691 #endif // WITHOUT_PROCESSING_BLOCK
Interface object for creating other interface objects.
Definition: FacerecService.h:64
RGB, 24 bit per pixel, 8 bit per channel.
Definition: IRawImage.h:60
Interface object that stores a captured face sample.
Definition: RawSample.h:49
Grayscale, 8 bit per pixel.
Definition: IRawImage.h:53
RawSample - Interface object that stores a captured face sample.
BGR, 24 bit per pixel, 8 bit per channel.
Definition: IRawImage.h:67
The class of exceptions thrown when errors occur.
Definition: Error.h:26
Definition: Context.h:480
Format
Format of image data.
Definition: IRawImage.h:46