1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_INTEROP_OPENCV_CORE_HPP
12 #define BOOST_COMPUTE_INTEROP_OPENCV_CORE_HPP
14 #include <opencv2/core/core.hpp>
16 #include <boost/throw_exception.hpp>
18 #include <boost/compute/algorithm/copy_n.hpp>
19 #include <boost/compute/exception/opencl_error.hpp>
20 #include <boost/compute/image/image2d.hpp>
21 #include <boost/compute/image/image_format.hpp>
22 #include <boost/compute/iterator/buffer_iterator.hpp>
28 inline void opencv_copy_mat_to_buffer(const cv::Mat &mat,
29 buffer_iterator<T> buffer,
30 command_queue &queue = system::default_queue())
32 BOOST_ASSERT(mat.isContinuous());
34 ::boost::compute::copy_n(
35 reinterpret_cast<T *>(mat.data), mat.rows * mat.cols, buffer, queue
40 inline void opencv_copy_buffer_to_mat(const buffer_iterator<T> buffer,
42 command_queue &queue = system::default_queue())
44 BOOST_ASSERT(mat.isContinuous());
46 ::boost::compute::copy_n(
47 buffer, mat.cols * mat.rows, reinterpret_cast<T *>(mat.data), queue
51 inline void opencv_copy_mat_to_image(const cv::Mat &mat,
53 command_queue &queue = system::default_queue())
55 BOOST_ASSERT(mat.data != 0);
56 BOOST_ASSERT(mat.isContinuous());
57 BOOST_ASSERT(image.get_context() == queue.get_context());
59 queue.enqueue_write_image(image, image.origin(), image.size(), mat.data);
62 inline void opencv_copy_image_to_mat(const image2d &image,
64 command_queue &queue = system::default_queue())
66 BOOST_ASSERT(mat.isContinuous());
67 BOOST_ASSERT(image.get_context() == queue.get_context());
69 queue.enqueue_read_image(image, image.origin(), image.size(), mat.data);
72 inline image_format opencv_get_mat_image_format(const cv::Mat &mat)
76 return image_format(CL_BGRA, CL_UNORM_INT8);
78 return image_format(CL_BGRA, CL_UNORM_INT16);
80 return image_format(CL_INTENSITY, CL_FLOAT);
82 return image_format(CL_RGBA, CL_FLOAT);
84 return image_format(CL_INTENSITY, CL_UNORM_INT8);
87 BOOST_THROW_EXCEPTION(opencl_error(CL_IMAGE_FORMAT_NOT_SUPPORTED));
90 inline cv::Mat opencv_create_mat_with_image2d(const image2d &image,
91 command_queue &queue = system::default_queue())
93 BOOST_ASSERT(image.get_context() == queue.get_context());
96 image_format format = image.get_format();
97 const cl_image_format *cl_image_format = format.get_format_ptr();
99 if(cl_image_format->image_channel_data_type == CL_UNORM_INT8 &&
100 cl_image_format->image_channel_order == CL_BGRA)
102 mat = cv::Mat(image.height(), image.width(), CV_8UC4);
104 else if(cl_image_format->image_channel_data_type == CL_UNORM_INT16 &&
105 cl_image_format->image_channel_order == CL_BGRA)
107 mat = cv::Mat(image.height(), image.width(), CV_16UC4);
109 else if(cl_image_format->image_channel_data_type == CL_FLOAT &&
110 cl_image_format->image_channel_order == CL_INTENSITY)
112 mat = cv::Mat(image.height(), image.width(), CV_32FC1);
116 mat = cv::Mat(image.height(), image.width(), CV_8UC1);
119 opencv_copy_image_to_mat(image, mat, queue);
124 inline image2d opencv_create_image2d_with_mat(const cv::Mat &mat,
126 command_queue &queue = system::default_queue())
128 const context &context = queue.get_context();
129 const image_format format = opencv_get_mat_image_format(mat);
131 image2d image(context, mat.cols, mat.rows, format, flags);
133 opencv_copy_mat_to_image(mat, image, queue);
138 } // end compute namespace
139 } // end boost namespace
141 #endif // BOOST_COMPUTE_INTEROP_OPENCV_CORE_HPP