Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:01

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_INTEROP_OPENCV_CORE_HPP
0012 #define BOOST_COMPUTE_INTEROP_OPENCV_CORE_HPP
0013 
0014 #include <opencv2/core/core.hpp>
0015 
0016 #include <boost/throw_exception.hpp>
0017 
0018 #include <boost/compute/algorithm/copy_n.hpp>
0019 #include <boost/compute/exception/opencl_error.hpp>
0020 #include <boost/compute/image/image2d.hpp>
0021 #include <boost/compute/image/image_format.hpp>
0022 #include <boost/compute/iterator/buffer_iterator.hpp>
0023 
0024 namespace boost {
0025 namespace compute {
0026 
0027 template<class T>
0028 inline void opencv_copy_mat_to_buffer(const cv::Mat &mat,
0029                                       buffer_iterator<T> buffer,
0030                                       command_queue &queue = system::default_queue())
0031 {
0032     BOOST_ASSERT(mat.isContinuous());
0033 
0034     ::boost::compute::copy_n(
0035         reinterpret_cast<T *>(mat.data), mat.rows * mat.cols, buffer, queue
0036     );
0037 }
0038 
0039 template<class T>
0040 inline void opencv_copy_buffer_to_mat(const buffer_iterator<T> buffer,
0041                                       cv::Mat &mat,
0042                                       command_queue &queue = system::default_queue())
0043 {
0044     BOOST_ASSERT(mat.isContinuous());
0045 
0046     ::boost::compute::copy_n(
0047         buffer, mat.cols * mat.rows, reinterpret_cast<T *>(mat.data), queue
0048     );
0049 }
0050 
0051 inline void opencv_copy_mat_to_image(const cv::Mat &mat,
0052                                      image2d &image,
0053                                      command_queue &queue = system::default_queue())
0054 {
0055     BOOST_ASSERT(mat.data != 0);
0056     BOOST_ASSERT(mat.isContinuous());
0057     BOOST_ASSERT(image.get_context() == queue.get_context());
0058 
0059     queue.enqueue_write_image(image, image.origin(), image.size(), mat.data);
0060 }
0061 
0062 inline void opencv_copy_image_to_mat(const image2d &image,
0063                                      cv::Mat &mat,
0064                                      command_queue &queue = system::default_queue())
0065 {
0066     BOOST_ASSERT(mat.isContinuous());
0067     BOOST_ASSERT(image.get_context() == queue.get_context());
0068 
0069     queue.enqueue_read_image(image, image.origin(), image.size(), mat.data);
0070 }
0071 
0072 inline image_format opencv_get_mat_image_format(const cv::Mat &mat)
0073 {
0074     switch(mat.type()){
0075         case CV_8UC4:
0076             return image_format(CL_BGRA, CL_UNORM_INT8);
0077         case CV_16UC4:
0078             return image_format(CL_BGRA, CL_UNORM_INT16);
0079         case CV_32F:
0080             return image_format(CL_INTENSITY, CL_FLOAT);
0081         case CV_32FC4:
0082             return image_format(CL_RGBA, CL_FLOAT);
0083         case CV_8UC1:
0084             return image_format(CL_INTENSITY, CL_UNORM_INT8);
0085     }
0086 
0087     BOOST_THROW_EXCEPTION(opencl_error(CL_IMAGE_FORMAT_NOT_SUPPORTED));
0088 }
0089 
0090 inline cv::Mat opencv_create_mat_with_image2d(const image2d &image,
0091                                               command_queue &queue = system::default_queue())
0092 {
0093     BOOST_ASSERT(image.get_context() == queue.get_context());
0094 
0095     cv::Mat mat;
0096     image_format format = image.get_format();
0097     const cl_image_format *cl_image_format = format.get_format_ptr();
0098 
0099     if(cl_image_format->image_channel_data_type == CL_UNORM_INT8 &&
0100             cl_image_format->image_channel_order == CL_BGRA)
0101     {
0102         mat = cv::Mat(image.height(), image.width(), CV_8UC4);
0103     }
0104     else if(cl_image_format->image_channel_data_type == CL_UNORM_INT16 &&
0105             cl_image_format->image_channel_order == CL_BGRA)
0106     {
0107         mat = cv::Mat(image.height(), image.width(), CV_16UC4);
0108     }
0109     else if(cl_image_format->image_channel_data_type == CL_FLOAT &&
0110             cl_image_format->image_channel_order == CL_INTENSITY)
0111     {
0112         mat = cv::Mat(image.height(), image.width(), CV_32FC1);
0113     }
0114     else
0115     {
0116         mat = cv::Mat(image.height(), image.width(), CV_8UC1);
0117     }
0118 
0119     opencv_copy_image_to_mat(image, mat, queue);
0120 
0121     return mat;
0122 }
0123 
0124 inline image2d opencv_create_image2d_with_mat(const cv::Mat &mat,
0125                                               cl_mem_flags flags,
0126                                               command_queue &queue = system::default_queue())
0127 {
0128     const context &context = queue.get_context();
0129     const image_format format = opencv_get_mat_image_format(mat);
0130 
0131     image2d image(context, mat.cols, mat.rows, format, flags);
0132 
0133     opencv_copy_mat_to_image(mat, image, queue);
0134 
0135     return image;
0136 }
0137 
0138 } // end compute namespace
0139 } // end boost namespace
0140 
0141 #endif // BOOST_COMPUTE_INTEROP_OPENCV_CORE_HPP