sp_image.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef SP_IMAGE_H
  2. #define SP_IMAGE_H
  3. #include "core_v2/ndarray_helper.hpp"
  4. #include "core_v2/meta_helper.hpp"
  5. #include <opencv2/core/types.hpp>
  6. constexpr auto image_rank = 2;
  7. struct sp_image : ndarray_proxy<image_rank>,
  8. meta_proxy {
  9. std::type_index type = typeid(void);
  10. //@formatter:off
  11. using base_type = ndarray_proxy;
  12. base_type *array_base() { return this; }
  13. [[nodiscard]] cv::Size cv_size() const;
  14. [[nodiscard]] int cv_type() const;
  15. [[nodiscard]] cv::Mat cv_mat(void *ptr) const;
  16. [[nodiscard]] cv::cuda::GpuMat cv_gpu_mat(void *ptr) const;
  17. [[nodiscard]] sp_image sub_view(cv::Size size, cv::Size start = {}) const;
  18. //@formatter:on
  19. template<typename T>
  20. static sp_image create(const cv::Size size, const size_t align = 1) {
  21. return create_impl(size, align, typeid(T));
  22. }
  23. static sp_image create(const int cv_type, const cv::Size size, const size_t align = 1) {
  24. return create_impl(size, align, cv_type);
  25. }
  26. template<typename T>
  27. static sp_image create(const cv::Size size, const void *ptr) {
  28. return create_impl(size, ptr, typeid(T));
  29. }
  30. static sp_image create(const cv::Mat &mat);
  31. template<typename T>
  32. [[nodiscard]] sp_image cast_view() const {
  33. return cast_view_impl(typeid(T));
  34. }
  35. [[nodiscard]] sp_image cast_view(int cv_type) const;
  36. protected:
  37. //@formatter:off
  38. static sp_image create_impl(cv::Size size, size_t align, std::type_index type);
  39. static sp_image create_impl(cv::Size size, size_t align, int cv_type);
  40. static sp_image create_impl(cv::Size size, const void *ptr, std::type_index type);
  41. [[nodiscard]] sp_image cast_view_impl(std::type_index type) const;
  42. //@formatter:on
  43. };
  44. void copy_sp_image(const sp_image &src, sp_image &dst,
  45. cudaMemcpyKind kind = cudaMemcpyDefault);
  46. template<typename T>
  47. using image_ndarray = ndarray<T, image_rank>;
  48. #include "core/image_utility_v2.h"
  49. template<typename T>
  50. image_type_v2<T> to_cuda_v2(image_ndarray<T> img) {
  51. auto ret = image_type_v2<T>();
  52. ret.ptr = (T *) img.data;
  53. ret.width = img.width();
  54. ret.height = img.height();
  55. ret.pitch = img.pitch();
  56. return ret;
  57. }
  58. image_mem_info to_mem_v1(const sp_image &img, void *ptr,
  59. memory_location loc = MEM_CUDA);
  60. #endif //SP_IMAGE_H