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 d38193b First version of a netCDF reader capable to make an image
from a variable where one of the variable dimensions is the set of bands.
d38193b is described below
commit d38193bd1d6fa3f638dfc2425ef402b3f1af3d6d
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Fri Mar 22 00:17:29 2019 +0100
First version of a netCDF reader capable to make an image from a variable
where one of the variable dimensions is the set of bands.
---
.../apache/sis/coverage/grid/ImageRenderer.java | 17 +++-
.../java/org/apache/sis/internal/jdk9/JDK9.java | 16 ++++
.../org/apache/sis/internal/netcdf/Raster.java | 15 +++-
.../apache/sis/internal/netcdf/RasterResource.java | 14 ++--
.../sis/internal/storage/AbstractGridResource.java | 12 +--
.../internal/storage/AbstractGridResourceTest.java | 93 ++++++++++++++++++++++
.../apache/sis/test/suite/StorageTestSuite.java | 1 +
7 files changed, 153 insertions(+), 15 deletions(-)
diff --git
a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
index 2d6b4a9..7263498 100644
---
a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
+++
b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
@@ -118,7 +118,7 @@ public class ImageRenderer {
*
* @see java.awt.image.ComponentSampleModel#pixelStride
*/
- private final int pixelStride;
+ private int pixelStride;
/**
* Number of data elements between a given sample and the corresponding
sample in the same column of the next line.
@@ -127,7 +127,7 @@ public class ImageRenderer {
*
* @see java.awt.image.ComponentSampleModel#scanlineStride
*/
- private final int scanlineStride;
+ private int scanlineStride;
/**
* The sample dimensions, to be used for defining the bands.
@@ -343,6 +343,19 @@ public class ImageRenderer {
}
/**
+ * Applies a subsampling between pixels. Invoking this method multiplies
the <cite>pixel stride</cite>
+ * by the given amount. This method is cumulative: invoking it many times
is equivalent to invoking it
+ * once with the product of all argument values.
+ *
+ * @param subsampling the subsampling between pixels in each row.
+ */
+ public void subsampleX(final int subsampling) {
+ ArgumentChecks.ensureStrictlyPositive("subsampling", subsampling);
+ scanlineStride = Math.multiplyExact(scanlineStride, subsampling);
+ pixelStride *= subsampling; // If above operation did not fail,
then this operation can not fail.
+ }
+
+ /**
* Creates a raster with the data specified by the last call to a {@code
setData(…)} method.
* The raster upper-left corner is located at the position given by {@link
#getLocation()}.
*
diff --git
a/core/sis-utility/src/main/java/org/apache/sis/internal/jdk9/JDK9.java
b/core/sis-utility/src/main/java/org/apache/sis/internal/jdk9/JDK9.java
index d05a107..90344b0 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/jdk9/JDK9.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/jdk9/JDK9.java
@@ -94,4 +94,20 @@ public final class JDK9 {
if (b instanceof DoubleBuffer) return ((DoubleBuffer) b).slice();
throw new IllegalArgumentException();
}
+
+ /**
+ * Place holder for {@code Buffer.duplicate()}.
+ *
+ * @param b the buffer to duplicate.
+ * @return the duplicate buffer.
+ */
+ public static Buffer duplicate(Buffer b) {
+ if (b instanceof ByteBuffer) return ((ByteBuffer) b).duplicate();
+ if (b instanceof ShortBuffer) return ((ShortBuffer) b).duplicate();
+ if (b instanceof IntBuffer) return ((IntBuffer) b).duplicate();
+ if (b instanceof LongBuffer) return ((LongBuffer) b).duplicate();
+ if (b instanceof FloatBuffer) return ((FloatBuffer) b).duplicate();
+ if (b instanceof DoubleBuffer) return ((DoubleBuffer) b).duplicate();
+ throw new IllegalArgumentException();
+ }
}
diff --git
a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Raster.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Raster.java
index e65ded4..cf29b44 100644
---
a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Raster.java
+++
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Raster.java
@@ -43,6 +43,11 @@ final class Raster extends GridCoverage {
private final DataBuffer data;
/**
+ * Increment to apply on index for moving to the next pixel in the same
band.
+ */
+ private final int pixelStride;
+
+ /**
* Name to display in error messages. Not to be used for processing.
*/
private final String label;
@@ -50,10 +55,13 @@ final class Raster extends GridCoverage {
/**
* Creates a new raster from the given resource.
*/
- Raster(final GridGeometry domain, final List<SampleDimension> range, final
DataBuffer data, final int bandStride, final String label) {
+ Raster(final GridGeometry domain, final List<SampleDimension> range, final
DataBuffer data, final int pixelStride,
+ final String label)
+ {
super(domain, range);
- this.data = data;
- this.label = label;
+ this.data = data;
+ this.label = label;
+ this.pixelStride = pixelStride;
}
/**
@@ -65,6 +73,7 @@ final class Raster extends GridCoverage {
try {
final ImageRenderer renderer = new ImageRenderer(this, target);
renderer.setData(data);
+ renderer.subsampleX(pixelStride);
return renderer.image();
} catch (IllegalArgumentException | ArithmeticException |
RasterFormatException e) {
throw new
CannotEvaluateException(Resources.format(Resources.Keys.CanNotRender_2, label,
e), e);
diff --git
a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/RasterResource.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/RasterResource.java
index 6403def..bd8135d 100644
---
a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/RasterResource.java
+++
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/RasterResource.java
@@ -46,6 +46,7 @@ import org.apache.sis.measure.NumberRange;
import org.apache.sis.util.Numbers;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.resources.Vocabulary;
+import org.apache.sis.internal.jdk9.JDK9;
/**
@@ -499,11 +500,14 @@ public final class RasterResource extends
AbstractGridResource implements Resour
if (bandDimension == 0) {
/*
* This block is executed only if the band dimension
is first, in which case we have interleaved
- * values like (band0, band1) for each pixel. Those
values are read only once in above block.
- * This block sets the offset of the first value to
read. The pixel stride is not specified here;
- * it will be specified later, at
java.awt.image.SampleModel construction time.
+ * values like (band0, band1) for each pixel. Those
values were read once for all in above block.
+ * This block sets the offset of the first value to
read, together with the buffer limit in order
+ * to ensure that all buffers have the same number of
remaining elements. The pixel stride is not
+ * specified here; it will be specified later, at
java.awt.image.SampleModel construction time.
*/
- values.position(rangeIndices.getSubsampledIndex(i));
+ if (i != 0) values = JDK9.duplicate(values);
+ final int p = rangeIndices.getSubsampledIndex(i);
+ values.position(p).limit(values.capacity() -
bands.length + p + 1);
}
final int p = rangeIndices.getTargetIndex(i);
sampleValues[p] = values;
@@ -526,7 +530,7 @@ public final class RasterResource extends
AbstractGridResource implements Resour
if (imageBuffer == null) {
throw new
DataStoreContentException(Errors.getResources(getLocale()).getString(Errors.Keys.UnsupportedType_1,
dataType.name()));
}
- return new Raster(domain, UnmodifiableArrayList.wrap(bands),
imageBuffer, rangeIndices.getBandStride(), first.getStandardName());
+ return new Raster(domain, UnmodifiableArrayList.wrap(bands),
imageBuffer, rangeIndices.getPixelStride(), first.getStandardName());
}
/**
diff --git
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
index defb835..39817aa 100644
---
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
+++
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
@@ -118,7 +118,7 @@ public abstract class AbstractGridResource extends
AbstractResource implements G
final int r = range[i];
if (r < 0 || r >= numSampleDimensions) {
throw new
IllegalArgumentException(Resources.forLocale(getLocale()).getString(
- Resources.Keys.InvalidSampleDimensionIndex_2, r,
numSampleDimensions - 1));
+ Resources.Keys.InvalidSampleDimensionIndex_2,
numSampleDimensions - 1, r));
}
packed[i] = (((long) r) << Integer.SIZE) | i;
}
@@ -241,13 +241,15 @@ public abstract class AbstractGridResource extends
AbstractResource implements G
}
/**
- * Returns the increment to apply on index for moving to the next
pixel in the same band.
- * If the {@code insert} methods have never been invoked, then this
method returns the number of bands.
+ * Returns the increment to apply on index for moving to the same band
of the next pixel.
+ * If the {@code insert} methods have never been invoked, then this
method returns 1.
*
* @return the increment to apply on index for moving to the next
pixel in the same band.
+ *
+ * @see java.awt.image.PixelInterleavedSampleModel#getPixelStride()
*/
- public int getBandStride() {
- return (1 + last - first) / interval;
+ public int getPixelStride() {
+ return (last - first) / interval + 1;
}
/**
diff --git
a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/AbstractGridResourceTest.java
b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/AbstractGridResourceTest.java
new file mode 100644
index 0000000..f3ad8f6
--- /dev/null
+++
b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/AbstractGridResourceTest.java
@@ -0,0 +1,93 @@
+/*
+ * 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.internal.storage;
+
+import java.util.List;
+import org.opengis.util.GenericName;
+import org.apache.sis.coverage.SampleDimension;
+import org.apache.sis.coverage.grid.GridExtent;
+import org.apache.sis.coverage.grid.GridGeometry;
+import org.apache.sis.coverage.grid.GridCoverage;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests {@link AbstractGridResource} and {@link
AbstractGridResource.RangeArgument}.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @version 1.0
+ * @since 1.0
+ * @module
+ */
+public final strictfp class AbstractGridResourceTest extends TestCase {
+ /**
+ * A resource performing no operation.
+ */
+ private final AbstractGridResource resource = new
AbstractGridResource((AbstractGridResource) null) {
+ @Override public GenericName getIdentifier() {throw
new UnsupportedOperationException();}
+ @Override public GridGeometry getGridGeometry() {throw
new UnsupportedOperationException();}
+ @Override public List<SampleDimension> getSampleDimensions() {throw
new UnsupportedOperationException();}
+ @Override public GridCoverage read(GridGeometry d, int... r) {throw
new UnsupportedOperationException();}
+ };
+
+ /**
+ * Tests {@link AbstractGridResource.RangeArgument} for data organized in
a banded sample model.
+ * This is the state when no {@code insert} method is invoked.
+ */
+ @Test
+ public void testRangeArgumentForBandedModel() {
+ final AbstractGridResource.RangeArgument r =
resource.validateRangeArgument(7, new int[] {4, 6, 2});
+ assertEquals("numBands", 3, r.getNumBands());
+ assertEquals("first", 4, r.getFirstSpecified());
+ assertEquals("source", 2, r.getSourceIndex(0)); //
Expect sorted source indices: {2, 4, 6}.
+ assertEquals("source", 4, r.getSourceIndex(1));
+ assertEquals("source", 6, r.getSourceIndex(2));
+ assertEquals("target", 2, r.getTargetIndex(0)); //
Expect original positions of sorted indices: {2, 0, 1}.
+ assertEquals("target", 0, r.getTargetIndex(1));
+ assertEquals("target", 1, r.getTargetIndex(2));
+ assertEquals("subsampled", 2, r.getSubsampledIndex(0)); //
Expect equivalent to getSourceIndex(i).
+ assertEquals("subsampled", 4, r.getSubsampledIndex(1));
+ assertEquals("subsampled", 6, r.getSubsampledIndex(2));
+ assertEquals("pixelStride", 1, r.getPixelStride());
+ }
+
+ /**
+ * Tests {@link AbstractGridResource.RangeArgument} for data organized in
an interleaved sample model.
+ * This is the state when the {@code insert} methods are invoked.
+ */
+ @Test
+ public void testRangeArgumentForInterleavedModel() {
+ final AbstractGridResource.RangeArgument r =
resource.validateRangeArgument(7, new int[] {4, 6, 2});
+ assertEquals(3, r.insertBandDimension(new GridExtent(360, 180),
2).getDimension());
+ assertArrayEquals(new int[] {3, 1, 2}, r.insertSubsampling(new int[]
{3, 1}, 2));
+ assertEquals("numBands", 3, r.getNumBands());
+ assertEquals("first", 4, r.getFirstSpecified());
+ assertEquals("source", 2, r.getSourceIndex(0)); //
Expect sorted source indices: {2, 4, 6}.
+ assertEquals("source", 4, r.getSourceIndex(1));
+ assertEquals("source", 6, r.getSourceIndex(2));
+ assertEquals("target", 2, r.getTargetIndex(0)); //
Expect original positions of sorted indices: {2, 0, 1}.
+ assertEquals("target", 0, r.getTargetIndex(1));
+ assertEquals("target", 1, r.getTargetIndex(2));
+ assertEquals("subsampled", 0, r.getSubsampledIndex(0)); //
Expect source indices divided by 2 minus 1.
+ assertEquals("subsampled", 1, r.getSubsampledIndex(1));
+ assertEquals("subsampled", 2, r.getSubsampledIndex(2));
+ assertEquals("pixelStride", 3, r.getPixelStride());
+ }
+}
diff --git
a/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
b/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
index 5f6a1c3..da50e25 100644
---
a/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
+++
b/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
@@ -39,6 +39,7 @@ import org.junit.BeforeClass;
org.apache.sis.internal.storage.io.HyperRectangleReaderTest.class,
org.apache.sis.internal.storage.io.RewindableLineReaderTest.class,
org.apache.sis.internal.storage.MetadataBuilderTest.class,
+ org.apache.sis.internal.storage.AbstractGridResourceTest.class,
org.apache.sis.storage.FeatureNamingTest.class,
org.apache.sis.storage.ProbeResultTest.class,
org.apache.sis.storage.StorageConnectorTest.class,