sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/06: Clarify MASK_KEY value with multi-banded images.
Date Mon, 29 Jun 2020 17:31:17 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 2c9d4409b920f953571521035e167eca2e194232
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Sun Jun 28 15:17:46 2020 +0200

    Clarify MASK_KEY value with multi-banded images.
---
 .../main/java/org/apache/sis/image/MaskImage.java   | 21 +++++++++++++--------
 .../main/java/org/apache/sis/image/PlanarImage.java |  6 +++++-
 .../sis/internal/coverage/j2d/ImageUtilities.java   |  2 +-
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/image/MaskImage.java b/core/sis-feature/src/main/java/org/apache/sis/image/MaskImage.java
index 17e0366..0b00c71 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/image/MaskImage.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/image/MaskImage.java
@@ -49,7 +49,7 @@ final class MaskImage extends SourceAlignedImage {
      */
     MaskImage(final ResampledImage image) {
         super(image, ColorModelFactory.createIndexColorModel(new int[] {0, -1},
-                image.getSampleModel().getNumBands(), ImageUtilities.getVisibleBand(image),
0));
+                1, ImageUtilities.getVisibleBand(image), 0));
     }
 
     /**
@@ -82,14 +82,16 @@ final class MaskImage extends SourceAlignedImage {
     @Override
     protected Raster computeTile(final int tileX, final int tileY, WritableRaster tile) throws
TransformException {
         final Raster source = getSource().getTile(tileX, tileY);
-        if (tile == null) {
-            tile = createTile(tileX, tileY);
-        }
+        /*
+         * Create a new tile unconditionally, without checking if a we can recycle a previous
tile,
+         * because we need a tile will all sample values initialized to zero. It should not
happen
+         * often that there is a tile to recycle anyway.
+         */
+        tile = createTile(tileX, tileY);
         final int tileMinX = tile.getMinX();
         final int tileMinY = tile.getMinY();
         final int tileMaxX = Math.addExact(tileMinX, tile.getWidth());
         final int tileMaxY = Math.addExact(tileMinY, tile.getHeight());
-        final int[] pixel  = new int[tile.getNumBands()];
         float[] values = null;
         /*
          * Following algorithm is inefficient; it would be much faster to read or write directly
in the arrays.
@@ -98,10 +100,13 @@ final class MaskImage extends SourceAlignedImage {
         for (int y=tileMinY; y<tileMaxY; y++) {
             for (int x=tileMinX; x<tileMaxX; x++) {
                 values = source.getPixel(x, y, values);
-                for (int i=0; i<pixel.length; i++) {
-                    pixel[i] = Float.isNaN(values[i]) ? 1 : 0;
+                for (int i=0; i<values.length; i++) {
+                    if (Float.isNaN(values[i])) {
+                        tile.setSample(x, y, 0, 1);
+                        break;
+                    }
                 }
-                tile.setPixel(x, y, pixel);
+                // Otherwise leave the value to 0.
             }
         }
         return tile;
diff --git a/core/sis-feature/src/main/java/org/apache/sis/image/PlanarImage.java b/core/sis-feature/src/main/java/org/apache/sis/image/PlanarImage.java
index 2646114..0be29e9 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/image/PlanarImage.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/image/PlanarImage.java
@@ -173,12 +173,16 @@ public abstract class PlanarImage implements RenderedImage {
 
     /**
      * Key of property providing a mask for missing values. Values should be instances of
{@link RenderedImage}
-     * with binary sample values and a color model of {@link java.awt.Transparency#BITMASK}
type.
+     * with a single band, binary sample values and a color model of {@link java.awt.Transparency#BITMASK}
type.
      * The binary values 0 and 1 are alpha values: 0 for fully transparent pixels and 1 for
fully opaque pixels.
      * For every pixel (<var>x</var>,<var>y</var>) in this image,
the pixel at the same coordinates in the mask
      * is either fully transparent (sample value 0) if the sample value in this image is
valid, or fully opaque
      * (sample value 1) if the sample value in this image is invalid ({@link Float#NaN}).
      *
+     * <p>If this {@code PlanarImage} has more than one band, then the value for this
property is the overlay of
+     * masks of each band: pixels are 0 when sample values are valid in all bands, and 1
when sample value is
+     * invalid in at least one band.</p>
+     *
      * <p>Note that it is usually not necessary to use masks explicitly in Apache SIS
because missing values
      * are represented by {@link Float#NaN}. This property is provided for algorithms that
can not work with
      * NaN values.</p>
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ImageUtilities.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ImageUtilities.java
index b6cb98f..11b3332 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ImageUtilities.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ImageUtilities.java
@@ -20,8 +20,8 @@ import java.util.Arrays;
 import java.awt.Rectangle;
 import java.awt.color.ColorSpace;
 import java.awt.geom.AffineTransform;
-import java.awt.image.ColorModel;
 import java.awt.image.DataBuffer;
+import java.awt.image.ColorModel;
 import java.awt.image.IndexColorModel;
 import java.awt.image.PackedColorModel;
 import java.awt.image.RenderedImage;


Mime
View raw message