| |
| |
| |
| |
| |
|
|
|
|
| #ifndef OPENCV_GAPI_OWN_MAT_HPP |
| #define OPENCV_GAPI_OWN_MAT_HPP |
|
|
| #include <opencv2/gapi/opencv_includes.hpp> |
| #include <opencv2/gapi/own/types.hpp> |
| #include <opencv2/gapi/own/scalar.hpp> |
| #include <opencv2/gapi/own/saturate.hpp> |
| #include <opencv2/gapi/own/assert.hpp> |
|
|
| #include <memory> |
| #include <cstring> |
| #include <numeric> |
| #include <vector> |
| #include <opencv2/gapi/util/throw.hpp> |
|
|
| namespace cv { namespace gapi { namespace own { |
| namespace detail { |
| template <typename T, unsigned char channels> |
| void assign_row(void* ptr, int cols, Scalar const& s) |
| { |
| auto p = static_cast<T*>(ptr); |
| for (int c = 0; c < cols; c++) |
| { |
| for (int ch = 0; ch < channels; ch++) |
| { |
| p[c * channels + ch] = saturate<T>(s[ch], roundd); |
| } |
| } |
| } |
|
|
| inline size_t default_step(int type, int cols) |
| { |
| return CV_ELEM_SIZE(type) * cols; |
| } |
| |
| |
| struct MatHeader{ |
| enum { AUTO_STEP = 0}; |
| enum { TYPE_MASK = 0x00000FFF }; |
|
|
| MatHeader() = default; |
|
|
| MatHeader(int _rows, int _cols, int type, void* _data, size_t _step) |
| : flags((type & TYPE_MASK)), rows(_rows), cols(_cols), data((uchar*)_data), step(_step == AUTO_STEP ? detail::default_step(type, _cols) : _step) |
| {} |
|
|
| MatHeader(const std::vector<int> &_dims, int type, void* _data) |
| : flags((type & TYPE_MASK)), data((uchar*)_data), step(0), dims(_dims) |
| {} |
|
|
| MatHeader(const MatHeader& ) = default; |
| MatHeader(MatHeader&& src) : MatHeader(src) |
| { |
| MatHeader empty; |
| src = empty; |
| } |
| MatHeader& operator=(const MatHeader& ) = default; |
| MatHeader& operator=(MatHeader&& src) |
| { |
| *this = src; |
| MatHeader empty; |
| src = empty; |
| return *this; |
| } |
| |
| |
| |
| |
| int flags = 0; |
|
|
| |
| int rows = 0, cols = 0; |
| |
| uchar* data = nullptr; |
| size_t step = 0; |
| |
| std::vector<int> dims; |
| }; |
| } |
| |
| class Mat : public detail::MatHeader{ |
| public: |
|
|
| Mat() = default; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Mat(int _rows, int _cols, int _type, void* _data, size_t _step = AUTO_STEP) |
| : MatHeader (_rows, _cols, _type, _data, _step) |
| {} |
|
|
| Mat(const std::vector<int> &_dims, int _type, void* _data) |
| : MatHeader (_dims, _type, _data) |
| {} |
|
|
| Mat(std::vector<int> &&_dims, int _type, void* _data) |
| : MatHeader (std::move(_dims), _type, _data) |
| {} |
|
|
| Mat(Mat const& src, const Rect& roi ) |
| : Mat(src) |
| { |
| rows = roi.height; |
| cols = roi.width; |
| data = ptr(roi.y, roi.x); |
| } |
|
|
| Mat(Mat const& ) = default; |
| Mat(Mat&& ) = default; |
|
|
| Mat& operator=(Mat const& ) = default; |
| Mat& operator=(Mat&& ) = default; |
|
|
| |
| |
| |
| Mat& operator = (const Scalar& s) |
| { |
| constexpr unsigned max_channels = 4; |
| using func_p_t = void (*)(void*, int, Scalar const&); |
| using detail::assign_row; |
| #define TABLE_ENTRY(type) {assign_row<type, 1>, assign_row<type, 2>, assign_row<type, 3>, assign_row<type, 4>} |
| static constexpr func_p_t func_tbl[][max_channels] = { |
| TABLE_ENTRY(uchar), |
| TABLE_ENTRY(schar), |
| TABLE_ENTRY(ushort), |
| TABLE_ENTRY(short), |
| TABLE_ENTRY(int), |
| TABLE_ENTRY(float), |
| TABLE_ENTRY(double) |
| }; |
| #undef TABLE_ENTRY |
|
|
| static_assert(CV_8U == 0 && CV_8S == 1 && CV_16U == 2 && CV_16S == 3 |
| && CV_32S == 4 && CV_32F == 5 && CV_64F == 6, |
| "OCV type ids used as indexes to array, thus exact numbers are important!" |
| ); |
|
|
| const auto depth = static_cast<unsigned int>(this->depth()); |
| GAPI_Assert(depth < sizeof(func_tbl)/sizeof(func_tbl[0])); |
|
|
| if (dims.empty()) |
| { |
| const auto channels = static_cast<unsigned int>(this->channels()); |
| GAPI_Assert(channels <= max_channels); |
|
|
| auto* f = func_tbl[depth][channels - 1]; |
| for (int r = 0; r < rows; ++r) |
| { |
| (*f)(static_cast<void *>(ptr(r)), cols, s ); |
| } |
| } |
| else |
| { |
| auto* f = func_tbl[depth][0]; |
| |
| (*f)(static_cast<void *>(data), static_cast<int>(total()), s); |
| } |
| return *this; |
| } |
|
|
| |
| |
| |
| |
| |
| size_t elemSize() const |
| { |
| return CV_ELEM_SIZE(type()); |
| } |
| |
| |
| |
| |
| |
| int type() const {return CV_MAT_TYPE(flags);} |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| int depth() const {return CV_MAT_DEPTH(flags);} |
|
|
| |
| |
| |
| |
| |
| int channels() const {return dims.empty() ? CV_MAT_CN(flags) : -1;} |
|
|
| |
| |
| |
| |
| |
| void create(int _rows, int _cols, int _type) |
| { |
| create(Size{_cols, _rows}, _type); |
| } |
| |
| |
| |
| |
| void create(Size _size, int _type) |
| { |
| GAPI_Assert(_size.height >= 0 && _size.width >= 0); |
| if (_size != Size{cols, rows} ) |
| { |
| Mat tmp{_size.height, _size.width, _type, nullptr}; |
| tmp.memory.reset(new uchar[ tmp.step * tmp.rows], [](uchar * p){delete[] p;}); |
| tmp.data = tmp.memory.get(); |
|
|
| *this = std::move(tmp); |
| } |
| } |
|
|
| void create(const std::vector<int> &_dims, int _type) |
| { |
| |
| |
| Mat tmp{_dims, _type, nullptr}; |
| |
| const auto sz = std::accumulate(_dims.begin(), _dims.end(), 1, std::multiplies<int>()); |
| tmp.memory.reset(new uchar[CV_ELEM_SIZE(_type)*sz], [](uchar * p){delete[] p;}); |
| tmp.data = tmp.memory.get(); |
| *this = std::move(tmp); |
| } |
|
|
| |
| |
| |
| |
| |
| Mat clone() const |
| { |
| Mat m; |
| copyTo(m); |
| return m; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void copyTo(Mat& dst) const |
| { |
| if (dims.empty()) |
| { |
| dst.create(rows, cols, type()); |
| for (int r = 0; r < rows; ++r) |
| { |
| std::copy_n(ptr(r), detail::default_step(type(),cols), dst.ptr(r)); |
| } |
| } |
| else |
| { |
| dst.create(dims, depth()); |
| std::copy_n(data, total()*elemSize(), data); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| bool empty() const |
| { |
| return data == 0 || total() == 0; |
| } |
|
|
| |
| |
| |
| |
| |
| size_t total() const |
| { |
| return dims.empty() |
| ? (static_cast<std::size_t>(rows) * cols) |
| : std::accumulate(dims.begin(), dims.end(), static_cast<std::size_t>(1), std::multiplies<size_t>()); |
| } |
|
|
| |
| |
| |
| Mat operator()( const Rect& roi ) const |
| { |
| return Mat{*this, roi}; |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| uchar* ptr(int row, int col = 0) |
| { |
| return const_cast<uchar*>(const_cast<const Mat*>(this)->ptr(row,col)); |
| } |
| |
| const uchar* ptr(int row, int col = 0) const |
| { |
| return data + step * row + CV_ELEM_SIZE(type()) * col; |
| } |
|
|
|
|
| private: |
| |
| std::shared_ptr<uchar> memory; |
| }; |
|
|
| } |
| } |
| } |
|
|
| #endif |
|
|