image_utility_v2.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include "image_utility_v2_impl.h"
  2. #include "core/image_utility_v2.h"
  3. namespace image_utility_impl {
  4. std::type_index cv_type_id(int type) {
  5. switch (type) {
  6. // @formatter:off
  7. case CV_8UC1: { return typeid(uchar1); }
  8. case CV_8UC2: { return typeid(uchar2); }
  9. case CV_8UC3: { return typeid(uchar3); }
  10. case CV_8UC4: { return typeid(uchar4); }
  11. case CV_16UC1: { return typeid(ushort1); }
  12. case CV_32FC1: { return typeid(float1); }
  13. case CV_32FC2: { return typeid(float2); }
  14. // @formatter:on
  15. default: {
  16. RET_ERROR;
  17. }
  18. }
  19. }
  20. }
  21. void *image_memory::start_ptr(int component) const {
  22. switch (img->pixel_format()) {
  23. case PIX_NORMAL: {
  24. assert(component == 0);
  25. return ptr.get();
  26. }
  27. case PIX_NV12: {
  28. if (component == 0) { return ptr.get(); }
  29. if (component == 1) { return (uint8_t *) ptr.get() + pitch * img->height(); }
  30. RET_ERROR_E;
  31. }
  32. default: {
  33. RET_ERROR_E;
  34. }
  35. }
  36. }
  37. void *image_memory::at(int row, int col, int component) {
  38. auto sp = (uint8_t *) start_ptr(component);
  39. return sp + row * pitch + col * img->elem_size();
  40. }
  41. void image_memory::modified(smart_cuda_stream *stream) {
  42. auto &pimpl = img->pimpl;
  43. if (ptr == pimpl->store_host.ptr) {
  44. pimpl->q_this->host_modified(stream);
  45. } else {
  46. assert(ptr == pimpl->store_cuda.ptr);
  47. pimpl->q_this->cuda_modified(stream);
  48. }
  49. }
  50. void *generic_image::impl::storage_info::row_start(size_t row) {
  51. return (uint8_t *) ptr.get() + row * pitch;
  52. }
  53. void generic_image::impl::storage_info::reset() {
  54. ptr = nullptr;
  55. pitch = 0;
  56. }
  57. generic_image::impl::impl(generic_image::create_config conf) {
  58. type = conf.type;
  59. pix_fmt = conf.pixel;
  60. size = conf.size;
  61. // adjust display size to storage size
  62. switch (pix_fmt) {
  63. case PIX_NORMAL: {
  64. break;
  65. }
  66. case PIX_NV12: {
  67. size.height = nv12_storage_height(size.height);
  68. break;
  69. }
  70. default: {
  71. assert(false);
  72. }
  73. }
  74. }
  75. cv::Size generic_image::impl::display_size() const {
  76. cv::Size ret = size;
  77. switch (pix_fmt) {
  78. case PIX_NORMAL: {
  79. break;
  80. }
  81. case PIX_NV12: {
  82. ret.height = nv12_display_height(ret.height);
  83. break;
  84. }
  85. default: {
  86. RET_ERROR_E;
  87. }
  88. }
  89. return ret;
  90. }
  91. size_t generic_image::impl::elem_bytes() const {
  92. return CV_ELEM_SIZE(type);
  93. }
  94. size_t generic_image::impl::width_in_bytes() const {
  95. return size.width * elem_bytes();
  96. }
  97. size_t generic_image::impl::size_in_bytes() const {
  98. return size.height * width_in_bytes();
  99. }
  100. void generic_image::impl::create_host(smart_cuda_stream *stream) {
  101. if (store_host.ptr != nullptr) {
  102. SYNC_CREATE(store_host.ptr, stream);
  103. return;
  104. }
  105. store_host.ptr = ALLOC_PITCH_SHARED(
  106. uint8_t, width_in_bytes(), size.height, MEM_HOST, &store_host.pitch);
  107. if (store_cuda.ptr != nullptr) {
  108. SYNC_CREATE(store_cuda.ptr, stream);
  109. CUDA_API_CHECK(cudaMemcpy2DAsync(store_host.ptr.get(), store_host.pitch, // dst
  110. store_cuda.ptr.get(), store_cuda.pitch, // src
  111. width_in_bytes(), size.height,
  112. cudaMemcpyDeviceToHost, cuda_stream(stream)));
  113. REC_CREATE(store_host.ptr, stream);
  114. }
  115. }
  116. void generic_image::impl::create_cuda(smart_cuda_stream *stream) {
  117. if (store_cuda.ptr != nullptr) {
  118. SYNC_CREATE(store_cuda.ptr, stream);
  119. return;
  120. }
  121. store_cuda.ptr = ALLOC_PITCH_SHARED(
  122. uint8_t, width_in_bytes(), size.height, MEM_CUDA, &store_cuda.pitch);
  123. if (store_host.ptr != nullptr) {
  124. SYNC_CREATE(store_host.ptr, stream);
  125. CUDA_API_CHECK(cudaMemcpy2DAsync(store_cuda.ptr.get(), store_cuda.pitch, // dst
  126. store_host.ptr.get(), store_host.pitch, // src
  127. width_in_bytes(), size.height,
  128. cudaMemcpyHostToDevice, stream->cuda));
  129. REC_CREATE(store_cuda.ptr, stream);
  130. }
  131. }
  132. image_mem_info generic_image::impl::get_memory_v1(smart_cuda_stream *stream) const {
  133. auto ret = image_mem_info{
  134. .width = width_in_bytes(),
  135. .height = (size_t) size.height
  136. };
  137. if (store_cuda.ptr != nullptr) {
  138. ret.loc = MEM_CUDA;
  139. SYNC_CREATE(store_cuda.ptr, stream);
  140. ret.ptr = store_cuda.ptr;
  141. ret.pitch = store_cuda.pitch;
  142. } else {
  143. assert(store_host.ptr != nullptr);
  144. ret.loc = MEM_HOST;
  145. SYNC_CREATE(store_host.ptr, stream);
  146. ret.ptr = store_host.ptr;
  147. ret.pitch = store_host.pitch;
  148. }
  149. return ret;
  150. }
  151. image_memory generic_image::impl::get_memory(memory_location loc,
  152. smart_cuda_stream *stream) {
  153. auto ret = image_memory();
  154. ret.img = q_this->shared_from_this();
  155. ret.width = width_in_bytes();
  156. ret.height = size.height;
  157. switch (loc) {
  158. case MEM_HOST: {
  159. create_host(stream);
  160. ret.ptr = store_host.ptr;
  161. ret.pitch = store_host.pitch;
  162. break;
  163. }
  164. case MEM_CUDA: {
  165. create_cuda(stream);
  166. ret.ptr = store_cuda.ptr;
  167. ret.pitch = store_cuda.pitch;
  168. break;
  169. }
  170. default: {
  171. RET_ERROR_E;
  172. }
  173. }
  174. return ret;
  175. }
  176. cv::Mat generic_image::impl::get_cv_mat(smart_cuda_stream *stream) {
  177. create_host(stream);
  178. return cv::Mat(size, type, store_host.ptr.get(), store_host.pitch);
  179. }
  180. cv::cuda::GpuMat generic_image::impl::get_cv_gpumat(smart_cuda_stream *stream) {
  181. create_cuda(stream);
  182. return cv::cuda::GpuMat(size, type, store_cuda.ptr.get(), store_cuda.pitch);
  183. }
  184. template<typename T>
  185. image_type_v2<T> generic_image::impl::get_image_type_v2(smart_cuda_stream *stream) {
  186. create_cuda(stream);
  187. assert(cv_type_id(type) == typeid(T));
  188. assert(size.width <= std::numeric_limits<ushort>::max());
  189. assert(size.height <= std::numeric_limits<ushort>::max());
  190. return image_type_v2<T>(
  191. (T *) store_cuda.ptr.get(), size.width, size.height, store_cuda.pitch);
  192. }
  193. template<typename T>
  194. std::shared_ptr<smart_image<T>> generic_image::impl::get_image_v1() const {
  195. using ret_type = std::shared_ptr<smart_image<T>>;
  196. auto ret = ret_type();
  197. using info_type = image_info_type<T>;
  198. if (store_host.ptr != nullptr) {
  199. auto host_info = info_type{
  200. .ptr = std::reinterpret_pointer_cast<T>(store_host.ptr),
  201. .loc = MEM_HOST, .size = size, .pitch = store_host.pitch,
  202. };
  203. ret = std::make_shared<smart_image<T>>(host_info);
  204. }
  205. if (store_cuda.ptr != nullptr) {
  206. auto cuda_info = info_type{
  207. .ptr = std::reinterpret_pointer_cast<T>(store_cuda.ptr),
  208. .loc= MEM_CUDA, .size = size, .pitch = store_cuda.pitch,
  209. };
  210. if (ret == nullptr) {
  211. ret = std::make_shared<smart_image<T>>(cuda_info);
  212. } else {
  213. ret->cuda_info = cuda_info;
  214. }
  215. }
  216. assert(ret != nullptr);
  217. return ret;
  218. }
  219. void generic_image::impl::pixel_at(int row, int col, int component,
  220. void *dst, size_t _size, smart_cuda_stream *stream) {
  221. if (store_host.ptr != nullptr) {
  222. auto ptr = get_memory(MEM_HOST, stream).at(row, col, component);
  223. memcpy(dst, ptr, _size);
  224. } else {
  225. assert(store_cuda.ptr != nullptr);
  226. auto ptr = get_memory(MEM_CUDA, stream).at(row, col, component);
  227. CUDA_API_CHECK(cudaMemcpyAsync(dst, ptr, _size,
  228. cudaMemcpyDeviceToHost, stream->cuda));
  229. CUDA_API_CHECK(cudaStreamSynchronize(stream->cuda));
  230. }
  231. }
  232. template<typename T>
  233. void generic_image::impl::create_from_v1(const std::shared_ptr<smart_image<T>> &img) {
  234. if (img->host_info.ptr != nullptr) {
  235. store_host.ptr = img->host_info.ptr;
  236. store_host.pitch = img->host_info.pitch;
  237. }
  238. if (img->cuda_info.ptr != nullptr) {
  239. store_cuda.ptr = img->cuda_info.ptr;
  240. store_cuda.pitch = img->cuda_info.pitch;
  241. }
  242. }
  243. void generic_image::impl::sub_image_inplace(int row, int col, int width, int height) {
  244. if (width == -1) { width = size.width - col; }
  245. if (height == -1) { height = size.height - row; }
  246. assert(width + col <= size.width);
  247. assert(height + row <= size.height);
  248. if (pix_fmt == PIX_NV12) {
  249. assert(row == 0 && height == size.height);
  250. } else {
  251. // sub-image of other formats are not implemented
  252. assert(pix_fmt == PIX_NORMAL);
  253. }
  254. size = cv::Size(width, height);
  255. if (store_host.ptr != nullptr) {
  256. store_host.ptr = std::shared_ptr<void>(
  257. (uint8_t *) store_host.row_start(row) + col * elem_bytes(),
  258. [p = store_host.ptr](void *) {});
  259. }
  260. if (store_cuda.ptr != nullptr) {
  261. store_cuda.ptr = std::shared_ptr<void>(
  262. (uint8_t *) store_cuda.row_start(row) + col * elem_bytes(),
  263. [p = store_cuda.ptr](void *) {});
  264. }
  265. }
  266. void generic_image::impl::type_cast_inplace(int _type) {
  267. // bit-cast of other formats are not implemented
  268. assert(pix_fmt == PIX_NORMAL);
  269. auto _width = width_in_bytes() / CV_ELEM_SIZE(_type);
  270. assert(_width * CV_ELEM_SIZE(_type) == width_in_bytes());
  271. size.width = _width;
  272. type = _type;
  273. }
  274. void generic_image::impl::host_modified(smart_cuda_stream *stream) {
  275. assert(store_host.ptr != nullptr);
  276. store_cuda.reset();
  277. REC_CREATE(store_host.ptr, stream);
  278. }
  279. void generic_image::impl::cuda_modified(smart_cuda_stream *stream) {
  280. assert(store_cuda.ptr != nullptr);
  281. store_host.reset();
  282. REC_CREATE(store_cuda.ptr, stream);
  283. }
  284. bool generic_image::basic_info_type::operator==(const basic_info_type &o) const {
  285. if (size != o.size) return false;
  286. if (cv_type != o.cv_type) return false;
  287. if (pixel_format != o.pixel_format) return false;
  288. return true;
  289. }
  290. generic_image::generic_image(std::unique_ptr<impl> _pimpl)
  291. : meta_base(_pimpl.get()) {
  292. pimpl = std::move(_pimpl);
  293. assert(pimpl != nullptr);
  294. pimpl->q_this = this;
  295. }
  296. generic_image::pointer generic_image::create(create_config conf) {
  297. auto pimpl = std::make_unique<impl>(conf);
  298. auto ret = std::make_shared<generic_image>(std::move(pimpl));
  299. return ret;
  300. }
  301. generic_image::pointer generic_image::create(cv::Size size, int type, pixel_format_enum pixel) {
  302. auto conf = create_config{
  303. .size = size, .type = type, .pixel = pixel,
  304. };
  305. return create(conf);
  306. }
  307. template<typename T>
  308. generic_image::pointer generic_image::create(const std::shared_ptr<smart_image<T>> &img) {
  309. auto ret = create(img->size(), get_cv_type<T>());
  310. ret->pimpl->create_from_v1(img);
  311. return ret;
  312. }
  313. // @formatter:off
  314. template generic_image::pointer generic_image::create(const image_u8c1 &);
  315. template generic_image::pointer generic_image::create(const image_u8c2 &);
  316. template generic_image::pointer generic_image::create(const image_u8c3 &);
  317. template generic_image::pointer generic_image::create(const image_u8c4 &);
  318. template generic_image::pointer generic_image::create(const image_u16c1 &);
  319. template generic_image::pointer generic_image::create(const image_f32c1 &);
  320. // @formatter:on
  321. cv::Size generic_image::size() const {
  322. return pimpl->display_size();
  323. }
  324. size_t generic_image::size_in_bytes() const {
  325. return pimpl->size_in_bytes();
  326. }
  327. size_t generic_image::width_in_bytes() const {
  328. return pimpl->width_in_bytes();
  329. }
  330. int generic_image::cv_type() const {
  331. return pimpl->type;
  332. }
  333. pixel_format_enum generic_image::pixel_format() const {
  334. return pimpl->pix_fmt;
  335. }
  336. generic_image::basic_info_type generic_image::basic_info() const {
  337. auto ret = basic_info_type();
  338. ret.size = size();
  339. ret.cv_type = cv_type();
  340. ret.pixel_format = pixel_format();
  341. return ret;
  342. }
  343. image_mem_info generic_image::memory_v1(smart_cuda_stream *stream) const {
  344. return pimpl->get_memory_v1(stream);
  345. }
  346. image_memory generic_image::memory(memory_location loc,
  347. smart_cuda_stream *stream) {
  348. return pimpl->get_memory(loc, stream);
  349. }
  350. cv::Mat generic_image::cv_mat(smart_cuda_stream *stream) {
  351. return pimpl->get_cv_mat(stream);
  352. }
  353. cv::cuda::GpuMat generic_image::cv_gpumat(smart_cuda_stream *stream) {
  354. return pimpl->get_cv_gpumat(stream);
  355. }
  356. template<typename T>
  357. image_type_v2<T> generic_image::cuda(smart_cuda_stream *stream) {
  358. return pimpl->get_image_type_v2<T>(stream);
  359. }
  360. // @formatter:off
  361. template image_type_v2<uchar1> generic_image::cuda(smart_cuda_stream *stream);
  362. template image_type_v2<uchar2> generic_image::cuda(smart_cuda_stream *stream);
  363. template image_type_v2<uchar3> generic_image::cuda(smart_cuda_stream *stream);
  364. template image_type_v2<uchar4> generic_image::cuda(smart_cuda_stream *stream);
  365. template image_type_v2<ushort1> generic_image::cuda(smart_cuda_stream *stream);
  366. template image_type_v2<float1> generic_image::cuda(smart_cuda_stream *stream);
  367. template image_type_v2<float2> generic_image::cuda(smart_cuda_stream *stream);
  368. // @formatter:on
  369. template<typename T>
  370. std::shared_ptr<smart_image<T>> generic_image::v1() const {
  371. return pimpl->get_image_v1<T>();
  372. }
  373. // @formatter:off
  374. template std::shared_ptr<smart_image<uchar1>> generic_image::v1() const;
  375. template std::shared_ptr<smart_image<uchar2>> generic_image::v1() const;
  376. template std::shared_ptr<smart_image<uchar3>> generic_image::v1() const;
  377. template std::shared_ptr<smart_image<uchar4>> generic_image::v1() const;
  378. template std::shared_ptr<smart_image<ushort1>> generic_image::v1() const;
  379. template std::shared_ptr<smart_image<float1>> generic_image::v1() const;
  380. // @formatter:on
  381. generic_image::pointer generic_image::shallow_clone() const {
  382. auto pimpl_c = std::make_unique<impl>(*pimpl);
  383. auto ret = std::make_shared<generic_image>(std::move(pimpl_c));
  384. return ret;
  385. }
  386. void generic_image::pixel_at_impl(int row, int col, int component,
  387. void *dst, size_t size, smart_cuda_stream *stream) {
  388. return pimpl->pixel_at(row, col, component, dst, size, stream);
  389. }
  390. generic_image::pointer generic_image::sub_image(int row, int col, int width, int height) const {
  391. auto ret = shallow_clone();
  392. ret->pimpl->sub_image_inplace(row, col, width, height);
  393. return ret;
  394. }
  395. generic_image::pointer generic_image::bit_cast(int type) {
  396. auto ret = shallow_clone();
  397. ret->pimpl->type_cast_inplace(type);
  398. return ret;
  399. }
  400. void generic_image::host_modified(smart_cuda_stream *stream) {
  401. pimpl->host_modified(stream);
  402. }
  403. void generic_image::cuda_modified(smart_cuda_stream *stream) {
  404. pimpl->cuda_modified(stream);
  405. }
  406. image_ptr to_image(obj_name_type name) {
  407. if (name == invalid_obj_name) return nullptr;
  408. auto img_type = OBJ_TYPE(name);
  409. if (OBJ_TYPE(name) == typeid(image_ptr)) {
  410. return OBJ_QUERY(image_ptr, name);
  411. }
  412. // convert from v1
  413. auto ret = image_ptr();
  414. auto impl_func = [&](auto V) {
  415. using T = std::remove_cvref_t<decltype(V)>;
  416. if (img_type == typeid(T)) {
  417. auto img = OBJ_QUERY(T, name);
  418. if (img == nullptr) return;
  419. ret = create_image(img);
  420. }
  421. };
  422. FORALL_IMG_TYPE;
  423. OBJ_MERGE_META(name, ret.get());
  424. return ret;
  425. }