2222#ifndef DD_DTO_TYPES_HPP
2323#define DD_DTO_TYPES_HPP
2424
25+ #include < opencv2/opencv.hpp>
26+
2527#include " oatpp/core/Types.hpp"
2628#include " oatpp/parser/json/mapping/ObjectMapper.hpp"
2729#include " apidata.h"
30+ #include " utils/cv_utils.hpp"
2831
2932namespace dd
3033{
@@ -46,10 +49,51 @@ namespace dd
4649 }
4750 };
4851
52+ struct VImage
53+ {
54+ cv::Mat _img;
55+ #ifdef USE_CUDA_CV
56+ cv::cuda::GpuMat _cuda_img;
57+ #endif
58+ std::string _ext = " .png" ;
59+
60+ VImage (const cv::Mat &img, const std::string &ext = " .png" )
61+ : _img(img), _ext(ext)
62+ {
63+ }
64+ #ifdef USE_CUDA_CV
65+ VImage (const cv::cuda::GpuMat &cuda_img, const std::string &ext = " .png" )
66+ : _cuda_img(cuda_img), _ext(ext)
67+ {
68+ }
69+ #endif
70+ bool is_cuda () const
71+ {
72+ #ifdef USE_CUDA_CV
73+ return !_cuda_img.empty ();
74+ #else
75+ return false ;
76+ #endif
77+ }
78+
79+ /* * get image on CPU whether it's on GPU or not */
80+ const cv::Mat &get_img ()
81+ {
82+ #ifdef USE_CUDA_CV
83+ if (is_cuda ())
84+ {
85+ _cuda_img.download (_img);
86+ }
87+ #endif
88+ return _img;
89+ }
90+ };
91+
4992 namespace __class
5093 {
5194 class APIDataClass ;
5295 class GpuIdsClass ;
96+ class ImageClass ;
5397 template <typename T> class DTOVectorClass ;
5498 }
5599
@@ -59,6 +103,8 @@ namespace dd
59103 typedef oatpp::data::mapping::type::Primitive<VGpuIds,
60104 __class::GpuIdsClass>
61105 GpuIds;
106+ typedef oatpp::data::mapping::type::Primitive<VImage, __class::ImageClass>
107+ DTOImage;
62108 template <typename T>
63109 using DTOVector
64110 = oatpp::data::mapping::type::Primitive<std::vector<T>,
@@ -89,6 +135,18 @@ namespace dd
89135 }
90136 };
91137
138+ class ImageClass
139+ {
140+ public:
141+ static const oatpp::ClassId CLASS_ID;
142+
143+ static oatpp::Type *getType ()
144+ {
145+ static oatpp::Type type (CLASS_ID);
146+ return &type;
147+ }
148+ };
149+
92150 template <typename T> class DTOVectorClass
93151 {
94152 public:
@@ -113,6 +171,9 @@ namespace dd
113171 {
114172 (void )type;
115173 (void )deserializer;
174+ // XXX: this has a failure case if the stream contains excaped "{" or "}"
175+ // Since this is a temporary workaround until we use DTO everywhere, it
176+ // might not be required to be fixed
116177 if (caret.isAtChar (' {' ))
117178 {
118179 auto start = caret.getCurrData ();
@@ -221,6 +282,30 @@ namespace dd
221282 }
222283 }
223284
285+ static inline oatpp::Void
286+ imageDeserialize (oatpp::parser::json::mapping::Deserializer *deserializer,
287+ oatpp::parser::Caret &caret,
288+ const oatpp::Type *const type)
289+ {
290+ (void )type;
291+ auto str_base64
292+ = deserializer->deserialize (caret, oatpp::String::Class::getType ())
293+ .cast <oatpp::String>();
294+ return DTOImage (VImage{ cv_utils::base64_to_image (*str_base64) });
295+ }
296+
297+ static inline void
298+ imageSerialize (oatpp::parser::json::mapping::Serializer *serializer,
299+ oatpp::data::stream::ConsistentOutputStream *stream,
300+ const oatpp::Void &obj)
301+ {
302+ (void )serializer;
303+ auto img_dto = obj.cast <DTOImage>();
304+ std::string encoded
305+ = cv_utils::image_to_base64 (img_dto->get_img (), img_dto->_ext );
306+ stream->writeSimple (encoded);
307+ }
308+
224309 // Inspired by oatpp json deserializer
225310 template <typename T> inline T readVecElement (oatpp::parser::Caret &caret);
226311
0 commit comments