sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 03/03: Workaround for https://bugs.openjdk.java.net/browse/JDK-8166038 If BufferedImage can not be used, fallback on ReshapedImage.
Date Thu, 25 Jun 2020 14:23:24 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 b594b665d11977620799fe74667f20837efe317a
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Thu Jun 25 16:19:31 2020 +0200

    Workaround for https://bugs.openjdk.java.net/browse/JDK-8166038
    If BufferedImage can not be used, fallback on ReshapedImage.
---
 .../apache/sis/coverage/grid/GridCoverage2D.java   | 35 +++++++++++++++-------
 .../sis/coverage/grid/GridCoverage2DTest.java      | 25 +++++++++++++---
 2 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage2D.java
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage2D.java
index 84960a1..a281172 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage2D.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage2D.java
@@ -618,12 +618,16 @@ public class GridCoverage2D extends GridCoverage {
              * upper-left point is inside the image.
              */
             if (data instanceof BufferedImage) {
-                // BufferedImage origin should be (0, 0), but for consistency with image
API, we consider it variable.
-                final long ix = data.getMinX();
-                final long iy = data.getMinY();
+                BufferedImage result = (BufferedImage) data;
+                /*
+                 * BufferedImage origin should be (0, 0). But for consistency with image
API,
+                 * we consider it as variable.
+                 */
+                final long ix = result.getMinX();
+                final long iy = result.getMinY();
                 if (xmin >= ix && ymin >= iy) {
-                    final int width  = data.getWidth();
-                    final int height = data.getHeight();
+                    final int width  = result.getWidth();
+                    final int height = result.getHeight();
                     /*
                      * Result of `ix + width` requires at most 33 bits for any `ix` value
(same for y axis).
                      * Subtractions by `xmin` and `ymin` never overflow if `ix` and `iy`
are zero or positive,
@@ -633,10 +637,19 @@ public class GridCoverage2D extends GridCoverage {
                      */
                     final int nx = toIntExact(min(xmax, ix + width  - 1) - xmin + 1);
                     final int ny = toIntExact(min(ymax, iy + height - 1) - ymin + 1);
-                    if ((xmin | ymin) == 0 && nx == width && ny == height)
{
-                        return data;
+                    if ((xmin | ymin) != 0 || nx != width || ny != height) {
+                        result = result.getSubimage(toIntExact(xmin), toIntExact(ymin), nx,
ny);
+                    }
+                    /*
+                     * Workaround for https://bugs.openjdk.java.net/browse/JDK-8166038
+                     * If BufferedImage can not be used, fallback on ReshapedImage
+                     * at the cost of returning an image larger than necessary.
+                     *
+                     * See also GridCoverage2DTest.PENDING_JDK_FIX
+                     */
+                    if (result.getTileGridXOffset() == ix && result.getTileGridYOffset()
== iy) {
+                        return result;
                     }
-                    return ((BufferedImage) data).getSubimage(toIntExact(xmin), toIntExact(ymin),
nx, ny);
                 }
             }
             /*
@@ -649,9 +662,9 @@ public class GridCoverage2D extends GridCoverage {
              * with Raster.createChild(…), but that would force us to invoke RenderedImage.getTile(…)
which
              * may force data loading earlier than desired.
              */
-            final ReshapedImage r = new ReshapedImage(data, xmin, ymin, xmax, ymax);
-            String error; assert (error = r.verify()) == null : error;
-            return r.isIdentity() ? data : r;
+            final ReshapedImage result = new ReshapedImage(data, xmin, ymin, xmax, ymax);
+            String error; assert (error = result.verify()) == null : error;
+            return result.isIdentity() ? data : result;
         } catch (ArithmeticException e) {
             throw new CannotEvaluateException(e.getMessage(), e);
         }
diff --git a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverage2DTest.java
b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverage2DTest.java
index e94fe90..35bd7be 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverage2DTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverage2DTest.java
@@ -56,6 +56,13 @@ import static org.apache.sis.test.FeatureAssert.*;
  */
 public strictfp class GridCoverage2DTest extends TestCase {
     /**
+     * Disables tests that are pending <a href="https://bugs.openjdk.java.net/browse/JDK-8166038">JDK-8166038</a>
fix.
+     * If {@link BufferedImage} can not be used, fallback on {@link ReshapedImage} at the
cost of an image larger than
+     * necessary. In such case, the tests need to specify the sub-region of pixels to verify.
+     */
+    private static final boolean PENDING_JDK_FIX = false;
+
+    /**
      * Width and height of the grid tested in this class.
      */
     protected static final int GRID_SIZE = 2;
@@ -232,8 +239,13 @@ public strictfp class GridCoverage2DTest extends TestCase {
          */
         final GridExtent singleRow = new GridExtent(GRID_SIZE, 1).translate(0, 1);
         result = coverage.render(singleRow);
-        assertInstanceOf("render", BufferedImage.class, result);
-        assertPixelsEqual(coverage.render(null), new Rectangle(0, 1, GRID_SIZE, 1), result,
null);
+        if (PENDING_JDK_FIX) {
+            assertInstanceOf("render", BufferedImage.class, result);
+            assertPixelsEqual(coverage.render(null), new Rectangle(0, 1, GRID_SIZE, 1), result,
null);
+        } else {
+            assertPixelsEqual(coverage.render(null), new Rectangle(0, 1, GRID_SIZE, 1),
+                                             result, new Rectangle(0, 0, GRID_SIZE, 1));
+        }
         /*
          * Column extraction:
          *   - Expected size (1,2) is verified by `assertPixelsEqual(…)`.
@@ -243,8 +255,13 @@ public strictfp class GridCoverage2DTest extends TestCase {
          */
         final GridExtent singleCol = new GridExtent(1, GRID_SIZE).translate(1, 0);
         result = coverage.render(singleCol);
-        assertInstanceOf("render", BufferedImage.class, result);
-        assertPixelsEqual(coverage.render(null), new Rectangle(1, 0, 1, GRID_SIZE), result,
null);
+        if (PENDING_JDK_FIX) {
+            assertInstanceOf("render", BufferedImage.class, result);
+            assertPixelsEqual(coverage.render(null), new Rectangle(1, 0, 1, GRID_SIZE), result,
null);
+        } else {
+            assertPixelsEqual(coverage.render(null), new Rectangle(1, 0, 1, GRID_SIZE),
+                                             result, new Rectangle(0, 0, 1, GRID_SIZE));
+        }
     }
 
     /**


Mime
View raw message