1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

Merge pull request #12674 from dmatveev:gapi_upd270918

* Update G-API code base to 27-Sep-18

Changes mostly improve standalone build support

* G-API code base update 28-09-2018

* Windows/Documentation warnings should be fixed
* Fixed stability issues in Fluid backend
* Fixed precompiled headers issues in G-API source files

* G-API code base update 28-09-18 EOD

* Fixed several static analysis issues
* Fixed issues found when G-API is built in a standalone mode
This commit is contained in:
Dmitry Matveev
2018-09-28 18:42:09 +03:00
committed by Alexander Alekhin
parent 66fdddc339
commit 2c6ab65476
64 changed files with 981 additions and 223 deletions
@@ -14,10 +14,12 @@
#include <cstdint> // uint8_t
#include <opencv2/gapi/opencv_includes.hpp>
#include <opencv2/gapi/own/mat.hpp>
#include <opencv2/gapi/gmat.hpp>
#include "opencv2/gapi/util/optional.hpp"
#include "opencv2/gapi/own/scalar.hpp"
#include "opencv2/gapi/own/mat.hpp"
namespace cv {
namespace gapi {
@@ -25,9 +27,10 @@ namespace fluid {
struct Border
{
#if 1
#if !defined(GAPI_STANDALONE)
// This constructor is required to support existing kernels which are part of G-API
Border(int _type, cv::Scalar _val) : type(_type), value(to_own(_val)) {};
#endif
#endif // !defined(GAPI_STANDALONE)
Border(int _type, cv::gapi::own::Scalar _val) : type(_type), value(_val) {};
int type;
cv::gapi::own::Scalar value;
@@ -45,7 +48,7 @@ public:
View() = default;
const uint8_t* InLineB(int index) const; // -(w-1)/2...0...+(w-1)/2 for Filters
template<typename T> const T* InLine(int i) const
template<typename T> const inline T* InLine(int i) const
{
const uint8_t* ptr = this->InLineB(i);
return reinterpret_cast<const T*>(ptr);
@@ -56,7 +59,7 @@ public:
int length() const;
int y() const;
GMatDesc meta() const;
const GMatDesc& meta() const;
class GAPI_EXPORTS Priv; // internal use only
Priv& priv(); // internal use only
@@ -84,10 +87,10 @@ public:
int wlpi,
BorderOpt border);
// Constructor for in/out buffers (for tests)
Buffer(const cv::Mat &data, bool is_input);
Buffer(const cv::gapi::own::Mat &data, bool is_input);
uint8_t* OutLineB(int index = 0);
template<typename T> T* OutLine(int index = 0)
template<typename T> inline T* OutLine(int index = 0)
{
uint8_t* ptr = this->OutLineB(index);
return reinterpret_cast<T*>(ptr);
@@ -100,7 +103,7 @@ public:
int length() const;
int lpi() const; // LPI for WRITER
GMatDesc meta() const;
const GMatDesc& meta() const;
View mkView(int lineConsumption, int borderSize, BorderOpt border, bool ownStorage);
@@ -102,22 +102,30 @@ template<> struct fluid_get_in<cv::GMat>
{
static const cv::gapi::fluid::View& get(const cv::GArgs &in_args, int idx)
{
return in_args[idx].get<cv::gapi::fluid::View>();
return in_args[idx].unsafe_get<cv::gapi::fluid::View>();
}
};
template<> struct fluid_get_in<cv::GScalar>
{
// FIXME: change to return by reference when moved to own::Scalar
#if !defined(GAPI_STANDALONE)
static const cv::Scalar get(const cv::GArgs &in_args, int idx)
{
return cv::gapi::own::to_ocv(in_args[idx].get<cv::gapi::own::Scalar>());
return cv::gapi::own::to_ocv(in_args[idx].unsafe_get<cv::gapi::own::Scalar>());
}
#else
static const cv::gapi::own::Scalar get(const cv::GArgs &in_args, int idx)
{
return in_args[idx].get<cv::gapi::own::Scalar>();
}
#endif // !defined(GAPI_STANDALONE)
};
template<class T> struct fluid_get_in
{
static T get(const cv::GArgs &in_args, int idx)
static const T& get(const cv::GArgs &in_args, int idx)
{
return in_args[idx].get<T>();
return in_args[idx].unsafe_get<T>();
}
};
+30 -4
View File
@@ -56,16 +56,26 @@ public:
{
}
template<typename T> T& get()
template<typename T> inline T& get()
{
return util::any_cast<typename std::remove_reference<T>::type>(value);
}
template<typename T> const T& get() const
template<typename T> inline const T& get() const
{
return util::any_cast<typename std::remove_reference<T>::type>(value);
}
template<typename T> inline T& unsafe_get()
{
return util::unsafe_any_cast<typename std::remove_reference<T>::type>(value);
}
template<typename T> inline const T& unsafe_get() const
{
return util::unsafe_any_cast<typename std::remove_reference<T>::type>(value);
}
detail::ArgKind kind = detail::ArgKind::OPAQUE;
protected:
@@ -76,10 +86,26 @@ using GArgs = std::vector<GArg>;
// FIXME: Express as M<GProtoArg...>::type
// FIXME: Move to a separate file!
using GRunArg = util::variant<cv::Mat, cv::gapi::own::Mat, cv::Scalar, cv::gapi::own::Scalar, cv::detail::VectorRef>;
using GRunArg = util::variant<
#if !defined(GAPI_STANDALONE)
cv::Mat,
cv::Scalar,
#endif // !defined(GAPI_STANDALONE)
cv::gapi::own::Mat,
cv::gapi::own::Scalar,
cv::detail::VectorRef
>;
using GRunArgs = std::vector<GRunArg>;
using GRunArgP = util::variant<cv::Mat*, cv::gapi::own::Mat*, cv::Scalar*, cv::gapi::own::Scalar*, cv::detail::VectorRef>;
using GRunArgP = util::variant<
#if !defined(GAPI_STANDALONE)
cv::Mat*,
cv::Scalar*,
#endif // !defined(GAPI_STANDALONE)
cv::gapi::own::Mat*,
cv::gapi::own::Scalar*,
cv::detail::VectorRef
>;
using GRunArgsP = std::vector<GRunArgP>;
@@ -10,9 +10,8 @@
#include <vector>
#include "opencv2/gapi/opencv_includes.hpp"
#include "opencv2/gapi/own/assert.hpp"
#include "opencv2/core/mat.hpp"
#include "opencv2/gapi/garg.hpp"
namespace cv {
@@ -36,13 +35,14 @@ public:
GCompiled();
void operator() (GRunArgs &&ins, GRunArgsP &&outs); // Generic arg-to-arg
#if !defined(GAPI_STANDALONE)
void operator() (cv::Mat in, cv::Mat &out); // Unary overload
void operator() (cv::Mat in, cv::Scalar &out); // Unary overload (scalar)
void operator() (cv::Mat in1, cv::Mat in2, cv::Mat &out); // Binary overload
void operator() (cv::Mat in1, cv::Mat in2, cv::Scalar &out); // Binary overload (scalar)
void operator() (const std::vector<cv::Mat> &ins, // Compatibility overload
const std::vector<cv::Mat> &outs);
#endif // !defined(GAPI_STANDALONE)
Priv& priv();
explicit operator bool () const; // Check if GCompiled is runnable or empty
@@ -58,8 +58,12 @@ public:
// Various versions of apply(): ////////////////////////////////////////////
// 1. Generic apply()
void apply(GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args = {}); // Arg-to-arg overload
void apply(const std::vector<cv::gapi::own::Mat>& ins, // Compatibility overload
const std::vector<cv::gapi::own::Mat>& outs,
GCompileArgs &&args = {});
// 2. Syntax sugar and compatibility overloads
#if !defined(GAPI_STANDALONE)
void apply(cv::Mat in, cv::Mat &out, GCompileArgs &&args = {}); // Unary overload
void apply(cv::Mat in, cv::Scalar &out, GCompileArgs &&args = {}); // Unary overload (scalar)
void apply(cv::Mat in1, cv::Mat in2, cv::Mat &out, GCompileArgs &&args = {}); // Binary overload
@@ -67,7 +71,7 @@ public:
void apply(const std::vector<cv::Mat>& ins, // Compatibility overload
const std::vector<cv::Mat>& outs,
GCompileArgs &&args = {});
#endif // !defined(GAPI_STANDALONE)
// Various versions of compile(): //////////////////////////////////////////
// 1. Generic compile() - requires metas to be passed as vector
GCompiled compile(GMetaArgs &&in_metas, GCompileArgs &&args = {});
+9 -7
View File
@@ -65,12 +65,17 @@ struct GAPI_EXPORTS GMatDesc
desc.size += delta;
return desc;
}
#if !defined(GAPI_STANDALONE)
GMatDesc withSizeDelta(cv::Size delta) const
{
return withSizeDelta(to_own(delta));
}
GMatDesc withSize(cv::Size sz) const
{
return withSize(to_own(sz));
}
#endif // !defined(GAPI_STANDALONE)
// Meta combinator: return a new GMatDesc which differs in size by delta
// (all other fields are taken unchanged from this GMatDesc)
//
@@ -87,11 +92,6 @@ struct GAPI_EXPORTS GMatDesc
return desc;
}
GMatDesc withSize(cv::Size sz) const
{
return withSize(to_own(sz));
}
// Meta combinator: return a new GMatDesc with specified data depth.
// (all other fields are taken unchanged from this GMatDesc)
GMatDesc withDepth(int ddepth) const
@@ -116,12 +116,14 @@ struct GAPI_EXPORTS GMatDesc
static inline GMatDesc empty_gmat_desc() { return GMatDesc{-1,-1,{-1,-1}}; }
#if !defined(GAPI_STANDALONE)
class Mat;
GAPI_EXPORTS GMatDesc descr_of(const cv::Mat &mat);
#endif // !defined(GAPI_STANDALONE)
namespace gapi { namespace own {
class Mat;
CV_EXPORTS GMatDesc descr_of(const Mat &mat);
GAPI_EXPORTS GMatDesc descr_of(const Mat &mat);
}}//gapi::own
std::ostream& operator<<(std::ostream& os, const cv::GMatDesc &desc);
@@ -14,9 +14,7 @@
#include <opencv2/gapi/gcommon.hpp> // GShape
#include <opencv2/gapi/util/optional.hpp>
#include "opencv2/gapi/own/scalar.hpp"
#include <opencv2/core/types.hpp>
// TODO GAPI_EXPORTS or so
namespace cv
{
// Forward declaration; GNode and GOrigin are an internal
@@ -30,7 +28,9 @@ public:
GScalar(); // Empty constructor
explicit GScalar(const cv::gapi::own::Scalar& s); // Constant value constructor from cv::gapi::own::Scalar
explicit GScalar(cv::gapi::own::Scalar&& s); // Constant value move-constructor from cv::gapi::own::Scalar
#if !defined(GAPI_STANDALONE)
explicit GScalar(const cv::Scalar& s); // Constant value constructor from cv::Scalar
#endif // !defined(GAPI_STANDALONE)
GScalar(double v0); // Constant value constructor from double
GScalar(const GNode &n, std::size_t out); // Operation result constructor
@@ -59,7 +59,10 @@ struct GScalarDesc
static inline GScalarDesc empty_scalar_desc() { return GScalarDesc(); }
GAPI_EXPORTS GScalarDesc descr_of(const cv::gapi::own::Scalar &scalar);
#if !defined(GAPI_STANDALONE)
GAPI_EXPORTS GScalarDesc descr_of(const cv::Scalar &scalar);
#endif // !defined(GAPI_STANDALONE)
std::ostream& operator<<(std::ostream& os, const cv::GScalarDesc &desc);
@@ -77,9 +77,11 @@ namespace detail
// Resolve a Host type back to its associated G-Type.
// FIXME: Probably it can be avoided
template<typename T> struct GTypeOf;
#if !defined(GAPI_STANDALONE)
template<> struct GTypeOf<cv::Mat> { using type = cv::GMat; };
template<> struct GTypeOf<cv::gapi::own::Mat> { using type = cv::GMat; };
template<> struct GTypeOf<cv::Scalar> { using type = cv::GScalar; };
#endif // !defined(GAPI_STANDALONE)
template<> struct GTypeOf<cv::gapi::own::Mat> { using type = cv::GMat; };
template<> struct GTypeOf<cv::gapi::own::Scalar> { using type = cv::GScalar; };
template<typename U> struct GTypeOf<std::vector<U> > { using type = cv::GArray<U>; };
template<class T> using g_type_of_t = typename GTypeOf<T>::type;
+2 -1
View File
@@ -7,6 +7,7 @@
#ifndef OPENCV_GAPI_GTYPED_HPP
#define OPENCV_GAPI_GTYPED_HPP
#if !defined(GAPI_STANDALONE)
#include <vector>
@@ -182,5 +183,5 @@ public:
};
} // namespace cv
#endif // !defined(GAPI_STANDALONE)
#endif // OPENCV_GAPI_GTYPED_HPP
@@ -1,4 +1,5 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
@@ -8,9 +9,13 @@
#ifndef OPENCV_GAPI_OPENCV_INCLUDES_HPP
#define OPENCV_GAPI_OPENCV_INCLUDES_HPP
#include <opencv2/core/mat.hpp>
#include <opencv2/core/cvdef.h>
#include <opencv2/core/types.hpp>
#include <opencv2/core/base.hpp>
#if !defined(GAPI_STANDALONE)
# include <opencv2/core/mat.hpp>
# include <opencv2/core/cvdef.h>
# include <opencv2/core/types.hpp>
# include <opencv2/core/base.hpp>
#else // Without OpenCV
# include <opencv2/gapi/own/cvdefs.hpp>
#endif // !defined(GAPI_STANDALONE)
#endif // OPENCV_GAPI_OPENCV_INCLUDES_HPP
@@ -8,8 +8,9 @@
#ifndef OPENCV_GAPI_OWN_CONVERT_HPP
#define OPENCV_GAPI_OWN_CONVERT_HPP
#include <opencv2/core/types.hpp>
#include <opencv2/core/mat.hpp>
#if !defined(GAPI_STANDALONE)
#include <opencv2/gapi/opencv_includes.hpp>
#include <opencv2/gapi/own/types.hpp>
#include <opencv2/gapi/own/mat.hpp>
#include "opencv2/gapi/own/scalar.hpp"
@@ -44,4 +45,6 @@ namespace own
} // namespace gapi
} // namespace cv
#endif // !defined(GAPI_STANDALONE)
#endif // OPENCV_GAPI_OWN_CONVERT_HPP
@@ -0,0 +1,146 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018 Intel Corporation
#ifndef OPENCV_GAPI_CV_DEFS_HPP
#define OPENCV_GAPI_CV_DEFS_HPP
#if defined(GAPI_STANDALONE)
// Simulate OpenCV definitions taken from various
// OpenCV interface headers if G-API is built in a
// standalone mode.
// interface.h:
typedef unsigned char uchar;
typedef char schar;
typedef unsigned short ushort;
#define CV_CN_MAX 512
#define CV_CN_SHIFT 3
#define CV_DEPTH_MAX (1 << CV_CN_SHIFT)
#define CV_8U 0
#define CV_8S 1
#define CV_16U 2
#define CV_16S 3
#define CV_32S 4
#define CV_32F 5
#define CV_64F 6
#define CV_USRTYPE1 7
#define CV_MAT_DEPTH_MASK (CV_DEPTH_MAX - 1)
#define CV_MAT_DEPTH(flags) ((flags) & CV_MAT_DEPTH_MASK)
#define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))
#define CV_MAKE_TYPE CV_MAKETYPE
#define CV_8UC1 CV_MAKETYPE(CV_8U,1)
#define CV_8UC2 CV_MAKETYPE(CV_8U,2)
#define CV_8UC3 CV_MAKETYPE(CV_8U,3)
#define CV_8UC4 CV_MAKETYPE(CV_8U,4)
#define CV_8UC(n) CV_MAKETYPE(CV_8U,(n))
#define CV_8SC1 CV_MAKETYPE(CV_8S,1)
#define CV_8SC2 CV_MAKETYPE(CV_8S,2)
#define CV_8SC3 CV_MAKETYPE(CV_8S,3)
#define CV_8SC4 CV_MAKETYPE(CV_8S,4)
#define CV_8SC(n) CV_MAKETYPE(CV_8S,(n))
#define CV_16UC1 CV_MAKETYPE(CV_16U,1)
#define CV_16UC2 CV_MAKETYPE(CV_16U,2)
#define CV_16UC3 CV_MAKETYPE(CV_16U,3)
#define CV_16UC4 CV_MAKETYPE(CV_16U,4)
#define CV_16UC(n) CV_MAKETYPE(CV_16U,(n))
#define CV_16SC1 CV_MAKETYPE(CV_16S,1)
#define CV_16SC2 CV_MAKETYPE(CV_16S,2)
#define CV_16SC3 CV_MAKETYPE(CV_16S,3)
#define CV_16SC4 CV_MAKETYPE(CV_16S,4)
#define CV_16SC(n) CV_MAKETYPE(CV_16S,(n))
#define CV_32SC1 CV_MAKETYPE(CV_32S,1)
#define CV_32SC2 CV_MAKETYPE(CV_32S,2)
#define CV_32SC3 CV_MAKETYPE(CV_32S,3)
#define CV_32SC4 CV_MAKETYPE(CV_32S,4)
#define CV_32SC(n) CV_MAKETYPE(CV_32S,(n))
#define CV_32FC1 CV_MAKETYPE(CV_32F,1)
#define CV_32FC2 CV_MAKETYPE(CV_32F,2)
#define CV_32FC3 CV_MAKETYPE(CV_32F,3)
#define CV_32FC4 CV_MAKETYPE(CV_32F,4)
#define CV_32FC(n) CV_MAKETYPE(CV_32F,(n))
#define CV_64FC1 CV_MAKETYPE(CV_64F,1)
#define CV_64FC2 CV_MAKETYPE(CV_64F,2)
#define CV_64FC3 CV_MAKETYPE(CV_64F,3)
#define CV_64FC4 CV_MAKETYPE(CV_64F,4)
#define CV_64FC(n) CV_MAKETYPE(CV_64F,(n))
// cvdef.h:
#define CV_MAT_CN_MASK ((CV_CN_MAX - 1) << CV_CN_SHIFT)
#define CV_MAT_CN(flags) ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
#define CV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
#define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
#define CV_MAT_CONT_FLAG_SHIFT 14
#define CV_MAT_CONT_FLAG (1 << CV_MAT_CONT_FLAG_SHIFT)
#define CV_IS_MAT_CONT(flags) ((flags) & CV_MAT_CONT_FLAG)
#define CV_IS_CONT_MAT CV_IS_MAT_CONT
#define CV_SUBMAT_FLAG_SHIFT 15
#define CV_SUBMAT_FLAG (1 << CV_SUBMAT_FLAG_SHIFT)
#define CV_IS_SUBMAT(flags) ((flags) & CV_MAT_SUBMAT_FLAG)
///** Size of each channel item,
// 0x8442211 = 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
//#define CV_ELEM_SIZE1(type) \
// ((((sizeof(size_t)<<28)|0x8442211) >> CV_MAT_DEPTH(type)*4) & 15)
#define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
/** 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */
#define CV_ELEM_SIZE(type) \
(CV_MAT_CN(type) << ((((sizeof(size_t)/4+1)*16384|0x3a50) >> CV_MAT_DEPTH(type)*2) & 3))
// base.h:
namespace cv
{
enum BorderTypes {
BORDER_CONSTANT = 0, //!< `iiiiii|abcdefgh|iiiiiii` with some specified `i`
BORDER_REPLICATE = 1, //!< `aaaaaa|abcdefgh|hhhhhhh`
BORDER_REFLECT = 2, //!< `fedcba|abcdefgh|hgfedcb`
BORDER_WRAP = 3, //!< `cdefgh|abcdefgh|abcdefg`
BORDER_REFLECT_101 = 4, //!< `gfedcb|abcdefgh|gfedcba`
BORDER_TRANSPARENT = 5, //!< `uvwxyz|abcdefgh|ijklmno`
BORDER_REFLECT101 = BORDER_REFLECT_101, //!< same as BORDER_REFLECT_101
BORDER_DEFAULT = BORDER_REFLECT_101, //!< same as BORDER_REFLECT_101
BORDER_ISOLATED = 16 //!< do not look outside of ROI
};
// imgproc.hpp:
enum InterpolationFlags{
INTER_NEAREST = 0,
INTER_LINEAR = 1,
INTER_CUBIC = 2,
INTER_AREA = 3,
INTER_LANCZOS4 = 4,
INTER_LINEAR_EXACT = 5,
INTER_MAX = 7,
};
} // namespace cv
static inline int cvFloor( double value )
{
int i = (int)value;
return i - (i > value);
}
#endif // defined(GAPI_STANDALONE)
#endif // OPENCV_GAPI_CV_DEFS_HPP
+153 -6
View File
@@ -5,15 +5,34 @@
// Copyright (C) 2018 Intel Corporation
#ifndef INCLUDE_OPENCV2_GAPI_OWN_MAT_HPP
#define INCLUDE_OPENCV2_GAPI_OWN_MAT_HPP
#ifndef OPENCV_GAPI_OWN_MAT_HPP
#define OPENCV_GAPI_OWN_MAT_HPP
#include "opencv2/core/cvdef.h"
#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> //std::shared_ptr
#include <cstring> //std::memcpy
#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;
@@ -81,12 +100,66 @@ namespace cv { namespace gapi { namespace own {
: MatHeader (_rows, _cols, _type, _data, _step)
{}
Mat(Mat const& src, const Rect& roi )
: Mat(src)
{
rows = roi.height;
cols = roi.width;
data = ptr(roi.y, roi.x);
}
Mat(Mat const& src) = default;
Mat(Mat&& src) = default;
Mat& operator=(Mat const& src) = default;
Mat& operator=(Mat&& src) = default;
/** @brief Sets all or some of the array elements to the specified value.
@param s Assigned scalar converted to the actual array type.
*/
Mat& operator = (const Scalar& s)
{
static constexpr unsigned max_channels = 4; //Scalar can't fit more than 4
GAPI_Assert(static_cast<unsigned int>(channels()) <= max_channels);
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!"
);
GAPI_Assert(static_cast<unsigned int>(depth()) < sizeof(func_tbl)/sizeof(func_tbl[0]));
for (int r = 0; r < rows; ++r)
{
auto* f = func_tbl[depth()][channels() -1];
(*f)(static_cast<void *>(ptr(r)), cols, s );
}
return *this;
}
/** @brief Returns the matrix element size in bytes.
The method returns the matrix element size in bytes. For example, if the matrix type is CV_16SC3 ,
the method returns 3\*sizeof(short) or 6.
*/
size_t elemSize() const
{
return CV_ELEM_SIZE(type());
}
/** @brief Returns the type of a matrix element.
The method returns a matrix element type. This is an identifier compatible with the CvMat type
@@ -115,13 +188,22 @@ namespace cv { namespace gapi { namespace own {
*/
int channels() const {return CV_MAT_CN(flags);}
/**
@param _rows New number of rows.
@param _cols New number of columns.
@param _type New matrix type.
*/
void create(int _rows, int _cols, int _type)
{
create({_cols, _rows}, _type);
}
/** @overload
@param _size Alternative new matrix size specification: Size(cols, rows)
@param _type New matrix type.
*/
void create(cv::gapi::own::Size _size, int _type)
void create(Size _size, int _type)
{
if (_size != cv::gapi::own::Size{cols, rows} )
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;});
@@ -130,6 +212,71 @@ namespace cv { namespace gapi { namespace own {
*this = std::move(tmp);
}
}
/** @brief Copies the matrix to another one.
The method copies the matrix data to another matrix. Before copying the data, the method invokes :
@code
m.create(this->size(), this->type());
@endcode
so that the destination matrix is reallocated if needed. While m.copyTo(m); works flawlessly, the
function does not handle the case of a partial overlap between the source and the destination
matrices.
*/
void copyTo(Mat& dst) const
{
dst.create(rows, cols, type());
for (int r = 0; r < rows; ++r)
{
std::memcpy(dst.ptr(r), ptr(r), detail::default_step(type(),cols));
}
}
/** @brief Returns true if the array has no elements.
The method returns true if Mat::total() is 0 or if Mat::data is NULL. Because of pop_back() and
resize() methods `M.total() == 0` does not imply that `M.data == NULL`.
*/
bool empty() const;
/** @brief Returns the total number of array elements.
The method returns the number of array elements (a number of pixels if the array represents an
image).
*/
size_t total() const
{
return static_cast<size_t>(rows * cols);
}
/** @overload
@param roi Extracted submatrix specified as a rectangle.
*/
Mat operator()( const Rect& roi ) const
{
return Mat{*this, roi};
}
/** @brief Returns a pointer to the specified matrix row.
The methods return `uchar*` or typed pointer to the specified matrix row. See the sample in
Mat::isContinuous to know how to use these methods.
@param row Index along the dimension 0
@param col Index along the dimension 1
*/
uchar* ptr(int row, int col = 0)
{
return const_cast<uchar*>(const_cast<const Mat*>(this)->ptr(row,col));
}
/** @overload */
const uchar* ptr(int row, int col = 0) const
{
return data + step * row + CV_ELEM_SIZE(type()) * col;
}
private:
//actual memory allocated for storage, or nullptr if object is non owning view to over memory
std::shared_ptr<uchar> memory;
@@ -139,4 +286,4 @@ namespace cv { namespace gapi { namespace own {
} //namespace gapi
} //namespace cv
#endif /* INCLUDE_OPENCV2_GAPI_OWN_MAT_HPP */
#endif /* OPENCV_GAPI_OWN_MAT_HPP */
@@ -0,0 +1,88 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018 Intel Corporation
#ifndef OPENCV_GAPI_OWN_SATURATE_HPP
#define OPENCV_GAPI_OWN_SATURATE_HPP
#include <limits>
#include <type_traits>
#include <opencv2/gapi/own/assert.hpp>
namespace cv { namespace gapi { namespace own {
//-----------------------------
//
// Numeric cast with saturation
//
//-----------------------------
template<typename DST, typename SRC>
static inline DST saturate(SRC x)
{
// only integral types please!
GAPI_DbgAssert(std::is_integral<DST>::value &&
std::is_integral<SRC>::value);
if (std::is_same<DST, SRC>::value)
return static_cast<DST>(x);
if (sizeof(DST) > sizeof(SRC))
return static_cast<DST>(x);
// compiler must recognize this saturation,
// so compile saturate<s16>(a + b) with adds
// instruction (e.g.: _mm_adds_epi16 if x86)
return x < std::numeric_limits<DST>::min()?
std::numeric_limits<DST>::min():
x > std::numeric_limits<DST>::max()?
std::numeric_limits<DST>::max():
static_cast<DST>(x);
}
// Note, that OpenCV rounds differently:
// - like std::round() for add, subtract
// - like std::rint() for multiply, divide
template<typename DST, typename SRC, typename R>
static inline DST saturate(SRC x, R round)
{
if (std::is_floating_point<DST>::value)
{
return static_cast<DST>(x);
}
else if (std::is_integral<SRC>::value)
{
GAPI_DbgAssert(std::is_integral<DST>::value &&
std::is_integral<SRC>::value);
return saturate<DST>(x);
}
else
{
GAPI_DbgAssert(std::is_integral<DST>::value &&
std::is_floating_point<SRC>::value);
#ifdef _WIN32
// Suppress warning about convering x to floating-point
// Note that x is already floating-point at this point
#pragma warning(disable: 4244)
#endif
int ix = static_cast<int>(round(x));
#ifdef _WIN32
#pragma warning(default: 4244)
#endif
return saturate<DST>(ix);
}
}
// explicit suffix 'd' for double type
inline double ceild(double x) { return std::ceil(x); }
inline double floord(double x) { return std::floor(x); }
inline double roundd(double x) { return std::round(x); }
inline double rintd(double x) { return std::rint(x); }
} //namespace own
} //namespace gapi
} //namespace cv
#endif /* OPENCV_GAPI_OWN_SATURATE_HPP */
@@ -8,6 +8,8 @@
#ifndef OPENCV_GAPI_GAPI_OWN_SCALAR_HPP
#define OPENCV_GAPI_GAPI_OWN_SCALAR_HPP
#include <opencv2/gapi/own/exports.hpp>
namespace cv
{
namespace gapi
@@ -15,7 +17,7 @@ namespace gapi
namespace own
{
class CV_EXPORTS Scalar
class GAPI_EXPORTS Scalar
{
public:
Scalar() = default;
@@ -9,9 +9,7 @@
#define OPENCV_GAPI_TYPES_HPP
#include <algorithm> // std::max, std::min
#include "opencv2/core/base.hpp" //for CV_DbgAssert
#include "opencv2/gapi/own/assert.hpp"
#include <ostream>
namespace cv
{
@@ -35,7 +33,7 @@ class Rect
public:
Rect() = default;
Rect(int _x, int _y, int _width, int _height) : x(_x), y(_y), width(_width), height(_height) {};
#if 1
#if !defined(GAPI_STANDALONE)
Rect(const cv::Rect& other) : x(other.x), y(other.y), width(other.width), height(other.height) {};
inline Rect& operator=(const cv::Rect& other)
{
@@ -45,7 +43,7 @@ public:
height = other.height;
return *this;
}
#endif
#endif // !defined(GAPI_STANDALONE)
int x = 0; //!< x coordinate of the top-left corner
int y = 0; //!< y coordinate of the top-left corner
@@ -92,7 +90,7 @@ class Size
public:
Size() = default;
Size(int _width, int _height) : width(_width), height(_height) {};
#if 1
#if !defined(GAPI_STANDALONE)
Size(const cv::Size& other) : width(other.width), height(other.height) {};
inline Size& operator=(const cv::Size& rhs)
{
@@ -100,7 +98,7 @@ public:
height = rhs.height;
return *this;
}
#endif
#endif // !defined(GAPI_STANDALONE)
int width = 0;
int height = 0;
@@ -97,6 +97,12 @@ namespace util
template<class value_t>
friend const value_t* any_cast(const any* operand);
template<class value_t>
friend value_t& unsafe_any_cast(any& operand);
template<class value_t>
friend const value_t& unsafe_any_cast(const any& operand);
friend void swap(any & lhs, any& rhs)
{
swap(lhs.hldr, rhs.hldr);
@@ -148,6 +154,27 @@ namespace util
throw_error(bad_any_cast());
}
template<class value_t>
inline value_t& unsafe_any_cast(any& operand)
{
#ifdef DEBUG
return any_cast<value_t>(operand);
#else
return static_cast<any::holder_impl<typename std::decay<value_t>::type> *>(operand.hldr.get())->v;
#endif
}
template<class value_t>
inline const value_t& unsafe_any_cast(const any& operand)
{
#ifdef DEBUG
return any_cast<value_t>(operand);
#else
return static_cast<any::holder_impl<typename std::decay<value_t>::type> *>(operand.hldr.get())->v;
#endif
}
} // namespace util
} // namespace cv