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
The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
new b1dc326 Add test for ComputedImage.
b1dc326 is described below
commit b1dc326f46959ca2cf4991238c3614093b36200d
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Sat Jan 4 12:52:00 2020 +0100
Add test for ComputedImage.
---
.../java/org/apache/sis/image/ComputedImage.java | 1 +
.../org/apache/sis/image/ComputedImageTest.java | 116 +++++++++++++++++++++
.../apache/sis/test/suite/FeatureTestSuite.java | 1 +
3 files changed, 118 insertions(+)
diff --git
a/core/sis-feature/src/main/java/org/apache/sis/image/ComputedImage.java
b/core/sis-feature/src/main/java/org/apache/sis/image/ComputedImage.java
index e74a71e..8f728f2 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/image/ComputedImage.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/image/ComputedImage.java
@@ -275,6 +275,7 @@ public abstract class ComputedImage extends PlanarImage {
throw (ImagingOpException) new
ImagingOpException(Resources.format(
Resources.Keys.CanNotComputeTile_2, tileX,
tileY)).initCause(cause);
}
+ reference.addTile(key);
}
} finally {
handler.putAndUnlock(tile); // Must be invoked even if an
exception occurred.
diff --git
a/core/sis-feature/src/test/java/org/apache/sis/image/ComputedImageTest.java
b/core/sis-feature/src/test/java/org/apache/sis/image/ComputedImageTest.java
new file mode 100644
index 0000000..2c6e613
--- /dev/null
+++ b/core/sis-feature/src/test/java/org/apache/sis/image/ComputedImageTest.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.image;
+
+import java.awt.Rectangle;
+import java.awt.image.Raster;
+import java.awt.image.WritableRaster;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static org.apache.sis.test.FeatureAssert.assertValuesEqual;
+
+
+/**
+ * Tests {@link ComputedImage}.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @version 1.1
+ * @since 1.1
+ * @module
+ */
+@DependsOn(PlanarImageTest.class)
+public final strictfp class ComputedImageTest extends TestCase {
+ /**
+ * Size of tiles in this test. The width should be different than the
height
+ * for increasing the chances to detect errors in index calculations.
+ */
+ private static final int TILE_WIDTH = 3, TILE_HEIGHT = 2;
+
+ /**
+ * Creates an image to test. The {@link ComputedImage} tiles are simply
sub-regions of a {@link BufferedImage}.
+ */
+ private static ComputedImage createImage() {
+ final BufferedImage source = new BufferedImage(TILE_WIDTH * 2,
TILE_HEIGHT * 4, BufferedImage.TYPE_USHORT_GRAY);
+ final WritableRaster raster = source.getRaster();
+ for (int y=raster.getHeight(); --y >= 0;) {
+ for (int x=raster.getWidth(); --x >= 0;) {
+ raster.setSample(x, y, 0, 10*y + x);
+ }
+ }
+ return new
ComputedImage(source.getSampleModel().createCompatibleSampleModel(TILE_WIDTH,
TILE_HEIGHT), source) {
+ @Override public ColorModel getColorModel() {return
getSource(0).getColorModel();}
+ @Override public int getWidth() {return
getSource(0).getWidth();}
+ @Override public int getHeight() {return
getSource(0).getHeight();}
+ @Override protected Raster computeTile(final int tileX, final int
tileY) {
+ final int tw = getTileWidth();
+ final int th = getTileHeight();
+ return getSource(0).getData(new Rectangle(tileX * tw, tileY *
th, tw, th));
+ }
+ };
+ }
+
+ /**
+ * Verifies that tiles returned by {@link ComputedImage#getTile(int, int)}
are cached.
+ */
+ @Test
+ public void testTileCaching() {
+ final ComputedImage image = createImage();
+ assertNull(image.verify());
+ Raster tile10 = image.getTile(1, 0);
+ Raster tile02 = image.getTile(0, 2);
+ assertNotSame(tile10, tile02);
+ assertValuesEqual(tile10, 0, new int[][] {
+ { 3, 4, 5},
+ {13, 14, 15}
+ });
+ assertValuesEqual(tile02, 0, new int[][] {
+ {40, 41, 42},
+ {50, 51, 52}
+ });
+ /*
+ * Verify that computed tiles are cached.
+ */
+ assertSame(tile10, image.getTile(1, 0));
+ assertSame(tile02, image.getTile(0, 2));
+ /*
+ * Verify that call to ComputedImage.Cleaner.dispose() remove tiles
from the cache.
+ */
+ image.dispose();
+ assertNotSame(tile10, tile10 = image.getTile(1, 0));
+ assertNotSame(tile02, tile02 = image.getTile(0, 2));
+ assertSame (tile10, image.getTile(1, 0)); // New tiles
are now cached.
+ assertSame (tile02, image.getTile(0, 2));
+ /*
+ * Should be last because this test will trig the computation of all
tiles.
+ */
+ assertValuesEqual(image.getData(), 0, new int[][] {
+ { 0, 1, 2, 3, 4, 5},
+ {10, 11, 12, 13, 14, 15},
+ {20, 21, 22, 23, 24, 25},
+ {30, 31, 32, 33, 34, 35},
+ {40, 41, 42, 43, 44, 45},
+ {50, 51, 52, 53, 54, 55},
+ {60, 61, 62, 63, 64, 65},
+ {70, 71, 72, 73, 74, 75}
+ });
+ }
+}
diff --git
a/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
b/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
index 4e6ff8a..3990ea2 100644
---
a/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
+++
b/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
@@ -77,6 +77,7 @@ import org.junit.runners.Suite;
org.apache.sis.internal.coverage.j2d.ImageUtilitiesTest.class,
org.apache.sis.internal.coverage.j2d.ScaledColorSpaceTest.class,
org.apache.sis.image.PlanarImageTest.class,
+ org.apache.sis.image.ComputedImageTest.class,
org.apache.sis.image.DefaultIteratorTest.class,
org.apache.sis.image.LinearIteratorTest.class,
org.apache.sis.coverage.CategoryTest.class,