sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 02/02: Add checks against empty area of interest during pixel iteration.
Date Fri, 11 Sep 2020 17:51:23 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 894384d200c61a24cc42a3508704284b6de2d174
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Sep 11 14:57:50 2020 +0200

    Add checks against empty area of interest during pixel iteration.
---
 .../main/java/org/apache/sis/image/PixelIterator.java  | 14 +++++++++-----
 .../java/org/apache/sis/image/DefaultIteratorTest.java | 18 ++++++++++++++++--
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/image/PixelIterator.java b/core/sis-feature/src/main/java/org/apache/sis/image/PixelIterator.java
index cf2e8c1..c9577d1 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/image/PixelIterator.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/image/PixelIterator.java
@@ -139,11 +139,11 @@ public abstract class PixelIterator {
         tileHeight      = data.getHeight();
         tileGridXOffset = data.getMinX();
         tileGridYOffset = data.getMinY();
+        bounds          = intersection(tileGridXOffset, tileGridYOffset, tileWidth, tileHeight,
subArea, window);
         tileLowerX      = 0;                    // In this case only one raster: tile index
is fixed to 0.
         tileLowerY      = 0;
-        tileUpperX      = 1;
-        tileUpperY      = 1;
-        bounds          = intersection(tileGridXOffset, tileGridYOffset, tileWidth, tileHeight,
subArea, window);
+        tileUpperX      = (bounds.width  == 0) ? 0 : 1;
+        tileUpperY      = (bounds.height == 0) ? 0 : 1;
         lowerX          = bounds.x;
         lowerY          = bounds.y;
         upperX          = Math.addExact(lowerX, bounds.width);
@@ -198,8 +198,8 @@ public abstract class PixelIterator {
         Rectangle bounds = new Rectangle(x, y, width, height);
         if (subArea != null) {
             bounds = bounds.intersection(subArea);
-            if (bounds.width  < 0) bounds.width  = 0;
-            if (bounds.height < 0) bounds.height = 0;
+            if (bounds.width  < 0) {bounds.x = x; bounds.width  = 0;}
+            if (bounds.height < 0) {bounds.y = y; bounds.height = 0;}
         }
         return bounds;
     }
@@ -245,8 +245,12 @@ public abstract class PixelIterator {
          *
          * @param  subArea  region where to iterator, or {@code null} for iterating over
all image domain.
          * @return {@code this} for method call chaining.
+         * @throws IllegalArgumentException if the given rectangle is empty.
          */
         public Builder setRegionOfInterest(final Rectangle subArea) {
+            if (subArea != null && subArea.isEmpty()) {
+                throw new IllegalArgumentException(Resources.format(Resources.Keys.EmptyTileOrImageRegion));
+            }
             this.subArea = subArea;
             return this;
         }
diff --git a/core/sis-feature/src/test/java/org/apache/sis/image/DefaultIteratorTest.java
b/core/sis-feature/src/test/java/org/apache/sis/image/DefaultIteratorTest.java
index 012302e..5cfcb0f 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/image/DefaultIteratorTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/image/DefaultIteratorTest.java
@@ -45,7 +45,7 @@ import static org.junit.Assert.*;
  *
  * @author  Rémi Maréchal (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
  * @since   1.0
  * @module
  */
@@ -425,7 +425,7 @@ public strictfp class DefaultIteratorTest extends TestCase {
      * Iterates over all values returned by the current {@link #iterator} and compares with
expected values.
      *
      * @param  verifyIndices  whether to verify also iterator {@code getPosition()} return
values.
-     *                        This is usually {@code true} if an only if the iterator cover
the full raster area.
+     *                        This is usually {@code true} if and only if the iterator covers
the full raster area.
      *
      * @see #verifyIterationAfterMove(int, int)
      * @see #verifyWindow(Dimension)
@@ -1276,4 +1276,18 @@ public strictfp class DefaultIteratorTest extends TestCase {
         isWritable = true;
         testOnImageSubArea();
     }
+
+    /**
+     * Tests iterator on an area.
+     */
+    @Test
+    public void testEmpty() {
+        tileWidth  = width  = 3;
+        tileHeight = height = 2;
+        numBands            = 1;
+        final Rectangle subArea = new Rectangle(5, 1, 3, 2);    // No intersection with image
bounds.
+        createPixelIterator(createImage(subArea), subArea);
+        assertEquals(0, expected.length);
+        verifyIteration(true);
+    }
 }


Mime
View raw message