sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: ConvertedGridCoverage should provide a non-null ColorModel.
Date Mon, 06 Jan 2020 12:05:00 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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 6f551c4  ConvertedGridCoverage should provide a non-null ColorModel.
6f551c4 is described below

commit 6f551c466901f64d19c54091bb4c2fb8742d1ba5
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Mon Jan 6 13:04:18 2020 +0100

    ConvertedGridCoverage should provide a non-null ColorModel.
---
 .../java/org/apache/sis/coverage/grid/GridCoverage.java    | 13 +++++++++++++
 .../java/org/apache/sis/coverage/grid/GridCoverage2D.java  |  5 ++++-
 .../sis/internal/coverage/j2d/BandedSampleConverter.java   | 14 ++++++++++++--
 .../sis/internal/coverage/j2d/ColorModelFactory.java       |  2 +-
 .../sis/internal/coverage/j2d/ConvertedGridCoverage.java   | 10 +++++++++-
 .../internal/coverage/j2d/BandedSampleConverterTest.java   |  2 +-
 6 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage.java
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage.java
index 40c0596..dd885b9 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage.java
@@ -20,6 +20,7 @@ import java.util.List;
 import java.util.Collection;
 import java.util.Locale;
 import java.util.Objects;
+import java.awt.image.ColorModel;
 import java.awt.image.RenderedImage;
 import org.opengis.geometry.DirectPosition;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
@@ -30,6 +31,7 @@ import org.opengis.referencing.operation.NoninvertibleTransformException;
 import org.apache.sis.internal.util.UnmodifiableArrayList;
 import org.apache.sis.coverage.SampleDimension;
 import org.apache.sis.coverage.SubspaceNotSpecifiedException;
+import org.apache.sis.internal.coverage.j2d.ColorModelFactory;
 import org.apache.sis.internal.coverage.j2d.ConvertedGridCoverage;
 import org.apache.sis.util.collection.DefaultTreeTable;
 import org.apache.sis.util.collection.TableColumn;
@@ -150,6 +152,17 @@ public abstract class GridCoverage {
     }
 
     /**
+     * Creates a color model for the expected range of sample values.
+     *
+     * @param  visibleBand  the band to be made visible (usually 0). All other bands (if
any) will be ignored.
+     * @param  dataType     the color model type as one {@link java.awt.image.DataBuffer}
constants.
+     * @return proposed color model, or {@code null} if none.
+     */
+    protected final ColorModel createColorModel(final int visibleBand, final int dataType)
{
+        return ColorModelFactory.createColorModel(sampleDimensions, visibleBand, dataType,
ColorModelFactory.GRAYSCALE);
+    }
+
+    /**
      * Returns the converted or package view, or {@code null} if not yet computed.
      * It is caller responsibility to ensure that this method is invoked in a synchronized
block.
      */
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 5c1fb4b..a23df3e 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
@@ -29,6 +29,7 @@ import java.io.UncheckedIOException;
 import java.awt.image.BufferedImage;
 import java.awt.image.RenderedImage;
 import java.awt.image.SampleModel;
+import java.awt.image.ColorModel;
 import org.opengis.geometry.Envelope;
 import org.opengis.metadata.spatial.DimensionNameType;
 import org.opengis.util.NameFactory;
@@ -59,6 +60,7 @@ import static java.lang.Math.subtractExact;
 import static java.lang.Math.toIntExact;
 
 // Branch-specific imports
+
 import org.opengis.coverage.CannotEvaluateException;
 import org.opengis.coverage.PointOutsideCoverageException;
 
@@ -146,7 +148,8 @@ public class GridCoverage2D extends GridCoverage {
     {
         super(source.gridGeometry, range);
         final int dataType = ConvertedGridCoverage.getDataType(range, isConverted);
-        data           = new BandedSampleConverter(source.data, null, dataType, converters);
+        final ColorModel colorModel = createColorModel(ConvertedGridCoverage.VISIBLE_BAND,
dataType);
+        data           = new BandedSampleConverter(source.data, null, dataType, colorModel,
converters);
         gridToImageX   = source.gridToImageX;
         gridToImageY   = source.gridToImageY;
         xDimension     = source.xDimension;
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BandedSampleConverter.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BandedSampleConverter.java
index c488436..a7cdca4 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BandedSampleConverter.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BandedSampleConverter.java
@@ -59,6 +59,11 @@ public final class BandedSampleConverter extends ComputedImage {
     private final MathTransform1D[] converters;
 
     /**
+     * The color model for the expected range of values. May be {@code null}.
+     */
+    private final ColorModel colorModel;
+
+    /**
      * Creates a new image of the given data type which will compute values using the given
converters.
      * The number of bands is the length of the {@code converters} array, which must be greater
than 0
      * and not greater than the number of bands in the source image.
@@ -66,12 +71,15 @@ public final class BandedSampleConverter extends ComputedImage {
      * @param  source      the image for which to convert sample values.
      * @param  layout      object to use for computing tile size, or {@code null} for the
default.
      * @param  targetType  the type of this image resulting from conversion of given image.
+     * @param  colorModel  the color model for from the expected range of values, or {@code
null}.
      * @param  converters  the transfer functions to apply on each band of the source image.
      */
     public BandedSampleConverter(final RenderedImage source, final ImageLayout layout,
-                                 final int targetType, final MathTransform1D... converters)
+                                 final int targetType, final ColorModel colorModel,
+                                 final MathTransform1D... converters)
     {
         super(createSampleModel(targetType, converters.length, layout, source), source);
+        this.colorModel = colorModel;
         this.converters = converters;
     }
 
@@ -94,12 +102,14 @@ public final class BandedSampleConverter extends ComputedImage {
 
     /**
      * Returns the color model associated with all rasters of this image.
+     * If the sample values of this image are floating point numbers, then
+     * a gray scale color model is computed from the expected range of values.
      *
      * @return the color model of this image, or {@code null} if none.
      */
     @Override
     public ColorModel getColorModel() {
-        return null;
+        return colorModel;
     }
 
     /**
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelFactory.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelFactory.java
index 39a332c..5291e61 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelFactory.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelFactory.java
@@ -304,7 +304,7 @@ public final class ColorModelFactory {
      * Returned instances of {@link ColorModel} are shared among all callers in the running
virtual machine.
      *
      * @param  bands        the sample dimensions for which to create a color model.
-     * @param  visibleBand  the band to be made visible (usually 0). All other bands, if
any will be ignored.
+     * @param  visibleBand  the band to be made visible (usually 0). All other bands (if
any) will be ignored.
      * @param  type         the color model type. One of {@link DataBuffer#TYPE_BYTE}, {@link
DataBuffer#TYPE_USHORT},
      *                      {@link DataBuffer#TYPE_INT}, {@link DataBuffer#TYPE_FLOAT} or
{@link DataBuffer#TYPE_DOUBLE}.
      * @param  colors       the colors to use for each category. The function may return
{@code null}, which means transparent.
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ConvertedGridCoverage.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ConvertedGridCoverage.java
index 5629cfd..64efd29 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ConvertedGridCoverage.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ConvertedGridCoverage.java
@@ -20,6 +20,7 @@ import java.util.List;
 import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.Optional;
+import java.awt.image.ColorModel;
 import java.awt.image.DataBuffer;
 import java.awt.image.RenderedImage;
 import org.opengis.geometry.DirectPosition;
@@ -54,6 +55,12 @@ import org.apache.sis.measure.NumberRange;
  */
 public final class ConvertedGridCoverage extends GridCoverage {
     /**
+     * The band to make visible when the image is shown on screen.
+     * All other bands, if any, will be computed their result ignored at display time.
+     */
+    public static final int VISIBLE_BAND = 0;
+
+    /**
      * The coverage containing source values.
      * Sample values will be converted from that coverage using the {@link #converters}.
      */
@@ -210,7 +217,8 @@ public final class ConvertedGridCoverage extends GridCoverage {
          * That image should never be null. But if an implementation wants to do so, respect
that.
          */
         if (image != null) {
-            image = new BandedSampleConverter(image, null, dataType, converters);
+            final ColorModel colorModel = createColorModel(VISIBLE_BAND, dataType);
+            image = new BandedSampleConverter(image, null, dataType, colorModel, converters);
         }
         return image;
     }
diff --git a/core/sis-feature/src/test/java/org/apache/sis/internal/coverage/j2d/BandedSampleConverterTest.java
b/core/sis-feature/src/test/java/org/apache/sis/internal/coverage/j2d/BandedSampleConverterTest.java
index c2be7db..f443450 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/internal/coverage/j2d/BandedSampleConverterTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/internal/coverage/j2d/BandedSampleConverterTest.java
@@ -70,7 +70,7 @@ public final strictfp class BandedSampleConverterTest extends TestCase {
                 random.nextInt(20) - 10);       // minTileY
         source.validate();
         source.initializeAllTiles(0);
-        image = new BandedSampleConverter(source, null, targetType,
+        image = new BandedSampleConverter(source, null, targetType, null,
                 (MathTransform1D) MathTransforms.linear(scale, 0));
     }
 


Mime
View raw message