[opencv] 215/251: core: empty() for Rect/Size templates

Nobuhiro Iwamatsu iwamatsu at moszumanska.debian.org
Sun Aug 27 23:27:44 UTC 2017


This is an automated email from the git hooks/post-receive script.

iwamatsu pushed a commit to annotated tag 3.3.0
in repository opencv.

commit 321c0ec533184db305828bca5a6450c8948a770f
Author: Alexander Alekhin <alexander.alekhin at intel.com>
Date:   Tue Aug 1 17:19:18 2017 +0300

    core: empty() for Rect/Size templates
    
    Check for empty objects via .area() is not a good practice due overflows
---
 modules/core/include/opencv2/core/types.hpp      | 21 +++++++++++++++++++--
 modules/core/misc/java/src/java/core+Rect.java   |  4 ++++
 modules/core/misc/java/src/java/core+Rect2d.java |  4 ++++
 modules/core/misc/java/src/java/core+Size.java   |  4 ++++
 4 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/modules/core/include/opencv2/core/types.hpp b/modules/core/include/opencv2/core/types.hpp
index ffac2ee..61cedc3 100644
--- a/modules/core/include/opencv2/core/types.hpp
+++ b/modules/core/include/opencv2/core/types.hpp
@@ -301,6 +301,8 @@ public:
     Size_& operator = (const Size_& sz);
     //! the area (width*height)
     _Tp area() const;
+    //! true if empty
+    bool empty() const;
 
     //! conversion of another data type.
     template<typename _Tp2> operator Size_<_Tp2>() const;
@@ -400,6 +402,8 @@ public:
     Size_<_Tp> size() const;
     //! area (width*height) of the rectangle
     _Tp area() const;
+    //! true if empty
+    bool empty() const;
 
     //! conversion to another data type
     template<typename _Tp2> operator Rect_<_Tp2>() const;
@@ -1599,6 +1603,13 @@ _Tp Size_<_Tp>::area() const
     return result;
 }
 
+template<typename _Tp> inline
+bool Size_<_Tp>::empty() const
+{
+    return width <= 0 || height <= 0;
+}
+
+
 template<typename _Tp> static inline
 Size_<_Tp>& operator *= (Size_<_Tp>& a, _Tp b)
 {
@@ -1741,6 +1752,12 @@ _Tp Rect_<_Tp>::area() const
     return result;
 }
 
+template<typename _Tp> inline
+bool Rect_<_Tp>::empty() const
+{
+    return width <= 0 || height <= 0;
+}
+
 template<typename _Tp> template<typename _Tp2> inline
 Rect_<_Tp>::operator Rect_<_Tp2>() const
 {
@@ -1803,10 +1820,10 @@ Rect_<_Tp>& operator &= ( Rect_<_Tp>& a, const Rect_<_Tp>& b )
 template<typename _Tp> static inline
 Rect_<_Tp>& operator |= ( Rect_<_Tp>& a, const Rect_<_Tp>& b )
 {
-    if (!a.area()) {
+    if (a.empty()) {
         a = b;
     }
-    else if (b.area()) {
+    else if (!b.empty()) {
         _Tp x1 = std::min(a.x, b.x);
         _Tp y1 = std::min(a.y, b.y);
         a.width = std::max(a.x + a.width, b.x + b.width) - x1;
diff --git a/modules/core/misc/java/src/java/core+Rect.java b/modules/core/misc/java/src/java/core+Rect.java
index 8f3fad7..c68e818 100644
--- a/modules/core/misc/java/src/java/core+Rect.java
+++ b/modules/core/misc/java/src/java/core+Rect.java
@@ -65,6 +65,10 @@ public class Rect {
         return width * height;
     }
 
+    public boolean empty() {
+        return width <= 0 || height <= 0;
+    }
+
     public boolean contains(Point p) {
         return x <= p.x && p.x < x + width && y <= p.y && p.y < y + height;
     }
diff --git a/modules/core/misc/java/src/java/core+Rect2d.java b/modules/core/misc/java/src/java/core+Rect2d.java
index cb83a97..4c27869 100644
--- a/modules/core/misc/java/src/java/core+Rect2d.java
+++ b/modules/core/misc/java/src/java/core+Rect2d.java
@@ -65,6 +65,10 @@ public class Rect2d {
         return width * height;
     }
 
+    public boolean empty() {
+        return width <= 0 || height <= 0;
+    }
+
     public boolean contains(Point p) {
         return x <= p.x && p.x < x + width && y <= p.y && p.y < y + height;
     }
diff --git a/modules/core/misc/java/src/java/core+Size.java b/modules/core/misc/java/src/java/core+Size.java
index dcc5742..f7d69f3 100644
--- a/modules/core/misc/java/src/java/core+Size.java
+++ b/modules/core/misc/java/src/java/core+Size.java
@@ -37,6 +37,10 @@ public class Size {
         return width * height;
     }
 
+    public boolean empty() {
+        return width <= 0 || height <= 0;
+    }
+
     public Size clone() {
         return new Size(width, height);
     }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/opencv.git



More information about the debian-science-commits mailing list