sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 02/04: Rename Tile.getAbsoluteRegion() as getRegionOnFinestLevel() and add check against integer overflows.
Date Tue, 18 Feb 2020 20:03:44 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 717c52bd2c524b5ef73f042156c0e53867ec881b
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue Feb 18 12:48:02 2020 +0100

    Rename Tile.getAbsoluteRegion() as getRegionOnFinestLevel() and add check against integer
overflows.
---
 .../apache/sis/internal/referencing/j2d/Tile.java  | 27 +++++++++++++---------
 .../internal/referencing/j2d/TileOrganizer.java    | 15 ++++++------
 .../referencing/provider/DatumShiftGridGroup.java  |  2 +-
 3 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java
b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java
index b2ec2ae..3032253 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java
@@ -324,18 +324,23 @@ public class Tile implements Serializable {
      *
      * @return the region in units relative to the tile having the finest resolution.
      * @throws IOException if an I/O operation was required for fetching the tile size and
that operation failed.
+     * @throws ArithmeticException if the region exceeded the capacity of 32-bits integer
type.
      * @throws IllegalStateException if this tile has been {@linkplain #Tile(Rectangle, AffineTransform)
created
      *         without location} and has not yet been processed by {@link TileOrganizer},
of if this tile does
      *         not have enough information for providing a tile size.
      */
-    public synchronized Rectangle getAbsoluteRegion() throws IOException {
-        final Rectangle region = getRegion();
-        final int sx = xSubsampling;
-        final int sy = ySubsampling;
-        region.x      *= sx;
-        region.y      *= sy;
-        region.width  *= sx;
-        region.height *= sy;
+    public Rectangle getRegionOnFinestLevel() throws IOException {
+        final Rectangle region;
+        final int sx, sy;
+        synchronized (this) {
+            region = getRegion();
+            sx = xSubsampling;
+            sy = ySubsampling;
+        }
+        region.x      = Math.multiplyExact(region.x,      sx);
+        region.y      = Math.multiplyExact(region.y,      sy);
+        region.width  = Math.multiplyExact(region.width,  sx);
+        region.height = Math.multiplyExact(region.height, sy);
         return region;
     }
 
@@ -350,7 +355,7 @@ public class Tile implements Serializable {
      * @param  region  the region to assign to this tile in units of tile having finest resolution.
      * @throws ArithmeticException if {@link #setSubsampling(Dimension)} method has not be
invoked.
      */
-    final void setAbsoluteRegion(final Rectangle region) throws ArithmeticException {
+    final void setRegionOnFinestLevel(final Rectangle region) throws ArithmeticException
{
         assert Thread.holdsLock(this);
         final int sx = xSubsampling;
         final int sy = ySubsampling;
@@ -477,8 +482,8 @@ public class Tile implements Serializable {
      */
     final void translate(final int dx, final int dy) {
         assert Thread.holdsLock(this);
-        x += dx;
-        y += dy;
+        x = Math.addExact(x, dx);
+        y = Math.addExact(y, dy);
         gridToCRS = null;
     }
 
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TileOrganizer.java
b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TileOrganizer.java
index 7d114ea..997306e 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TileOrganizer.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TileOrganizer.java
@@ -230,7 +230,7 @@ public class TileOrganizer {
                         tr.transform(location, location);
                         bounds = new Rectangle(location.x, location.y, 0, 0);
                     }
-                    tile.setAbsoluteRegion(bounds);
+                    tile.setRegionOnFinestLevel(bounds);
                 }
                 if (groupBounds == null) {
                     groupBounds = bounds;
@@ -249,7 +249,7 @@ public class TileOrganizer {
             if (groupBounds != null) {
                 final int dx = xLocation - groupBounds.x;
                 final int dy = yLocation - groupBounds.y;
-                if (dx != 0 || dy != 0) {
+                if ((dx | dy) != 0) {
                     reference.translate(-dx, -dy);
                     groupBounds.translate(dx, dy);
                 }
@@ -283,18 +283,19 @@ public class TileOrganizer {
         private final AffineTransform gridToCRS;
 
         /**
-         * The translation in "absolute units". This is the same units than for tiles at
subsampling (1,1).
+         * The translation in units of the level having finest resolution.
+         * This is the same units than for tiles at subsampling (1,1).
          */
         private final int dx, dy;
 
         /**
-         * Creates a new translated transform. The translation is specified in "absolute
units",
-         * i.e. in the same units than for tiles at subsampling (1,1).
+         * Creates a new translated transform. The translation is specified in unit of the
level
+         * having finest resolution, i.e. in the same units than for tiles at subsampling
(1,1).
          *
          * @param  subsampling  the {@linkplain Tile#getSubsampling() tile subsampling}.
          * @param  reference    the "grid to real world" transform at subsampling (1,1).
-         * @param  dx           the translation along <var>x</var> axis in "absolute
units".
-         * @param  dy           the translation along <var>y</var> axis in "absolute
units".
+         * @param  dx           the translation along <var>x</var> axis in "finest
units".
+         * @param  dy           the translation along <var>y</var> axis in "finest
units".
          */
         Translation(final Dimension subsampling, AffineTransform reference, int dx, int dy)
{
             this.dx = dx / subsampling.width;                           // It is okay to
round toward zero.
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
index df2cefa..b6edac8 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
@@ -79,7 +79,7 @@ final class DatumShiftGridGroup<C extends Quantity<C>, T extends
Quantity<T>> ex
 
         /** Creates a new instance from the given {@link TileOrganizer} result. */
         Region(final Tile tile) throws IOException {
-            final Rectangle r = tile.getAbsoluteRegion();       // In units of the grid having
finest resolution.
+            final Rectangle r = tile.getRegionOnFinestLevel();      // In units of the grid
having finest resolution.
             final Dimension s = tile.getSubsampling();
             xmin = r.getMinX();
             xmax = r.getMaxX();


Mime
View raw message