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 76376e3430804edd16bff99949e40f4795b06f1e Author: Martin Desruisseaux <[email protected]> AuthorDate: Mon Dec 23 16:12:41 2019 +0100 Add an ImageUtilities.createGrayScale(…) factory method. --- .../internal/coverage/j2d/ColorModelFactory.java | 33 +++++++++++++++--- .../sis/internal/coverage/j2d/ImageUtilities.java | 39 ++++++++++++++++++++++ .../sis/coverage/grid/GridCoverage2DTest.java | 12 ++----- 3 files changed, 70 insertions(+), 14 deletions(-) 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 745cf06..15fee68 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 @@ -43,7 +43,7 @@ import org.apache.sis.util.collection.WeakValueHashMap; * * @author Martin Desruisseaux (IRD, Geomatys) * @author Johann Sorel (Geomatys) - * @version 1.0 + * @version 1.1 * @since 1.0 * @module */ @@ -358,10 +358,10 @@ public final class ColorModelFactory { * <p>This methods caches previously created instances using weak references, * because index color model may be big (up to 256 kb).</p> * - * @param ARGB An array of ARGB values. - * @param numBands The number of bands. - * @param visibleBand The band to display. - * @param transparent The transparent pixel, or -1 for auto-detection. + * @param ARGB an array of ARGB values. + * @param numBands the number of bands. + * @param visibleBand the band to display. + * @param transparent the transparent pixel, or -1 for auto-detection. * @return An index color model for the specified array. */ public static IndexColorModel createIndexColorModel(final int[] ARGB, final int numBands, final int visibleBand, int transparent) { @@ -383,6 +383,29 @@ public final class ColorModelFactory { } /** + * Creates a color model for opaque images storing pixels as real numbers. + * The color model can have an arbitrary number of bands, but in current implementation only one band is used. + * + * <p><b>Warning:</b> the use of this color model is very slow. + * It should be used only when no standard color model can be used.</p> + * + * @param dataType the color model type as one of {@code DataBuffer.TYPE_*} constants. + * @param numComponents the number of components. + * @param visibleBand the band to use for computing colors. + * @param minimum the minimal sample value expected. + * @param maximum the maximal sample value expected. + * @return the color space for the given range of values. + * + * @see ImageUtilities#createGrayScale(int, int, int, int, int, double, double) + */ + public static ColorModel createGrayScale(final int dataType, final int numComponents, + final int visibleBand, final double minimum, final double maximum) + { + final ColorSpace cs = createColorSpace(numComponents, visibleBand, minimum, maximum); + return new ComponentColorModel(cs, false, false, Transparency.OPAQUE, dataType); + } + + /** * Returns a color space for images storing pixels as real numbers. The color space can have an arbitrary number of bands, * but in current implementation only one band is used. Current implementation create a gray scale. * 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 327f71f..e9a8495 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 @@ -19,7 +19,9 @@ package org.apache.sis.internal.coverage.j2d; import java.util.Arrays; import java.awt.Rectangle; import java.awt.color.ColorSpace; +import java.awt.image.BufferedImage; import java.awt.image.ColorModel; +import java.awt.image.DataBuffer; import java.awt.image.IndexColorModel; import java.awt.image.PackedColorModel; import java.awt.image.RenderedImage; @@ -47,6 +49,43 @@ public final class ImageUtilities { } /** + * Creates an opaque image with a gray scale color model. The image can have an arbitrary + * number of bands, but in current implementation only one band is used. + * + * <p><b>Warning:</b> displaying this image is very slow, except in a few special cases. + * It should be used only when no standard color model can be used.</p> + * + * @param dataType the color model type as one of {@code DataBuffer.TYPE_*} constants. + * @param width the desired image width. + * @param height the desired image height. + * @param numComponents the number of components. + * @param visibleBand the band to use for computing colors. + * @param minimum the minimal sample value expected. + * @param maximum the maximal sample value expected. + * @return the color space for the given range of values. + */ + public static BufferedImage createGrayScale(final int dataType, final int width, final int height, + final int numComponents, final int visibleBand, final double minimum, final double maximum) + { + switch (dataType) { + case DataBuffer.TYPE_BYTE: { + if (numComponents == 1 && minimum <= 0 && maximum >= 0xFF) { + return new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); + } + break; + } + case DataBuffer.TYPE_USHORT: { + if (numComponents == 1 && minimum <= 0 && maximum >= 0xFFFF) { + return new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY); + } + break; + } + } + final ColorModel cm = ColorModelFactory.createGrayScale(DataBuffer.TYPE_INT, 1, 0, -10, 10); + return new BufferedImage(cm, cm.createCompatibleWritableRaster(width, height), false, null); + } + + /** * Returns the bounds of the given image as a new rectangle. * * @param image the image for which to get the bounds. 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 366b0b8..d3a1d64 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 @@ -17,11 +17,7 @@ package org.apache.sis.coverage.grid; import java.util.Collections; -import java.awt.Transparency; -import java.awt.color.ColorSpace; import java.awt.image.BufferedImage; -import java.awt.image.ColorModel; -import java.awt.image.ComponentColorModel; import java.awt.image.DataBuffer; import java.awt.image.Raster; import java.awt.image.WritableRaster; @@ -31,7 +27,7 @@ import org.opengis.referencing.operation.MathTransform1D; import org.opengis.referencing.datum.PixelInCell; import org.apache.sis.coverage.SampleDimension; import org.apache.sis.geometry.DirectPosition2D; -import org.apache.sis.internal.coverage.j2d.ColorModelFactory; +import org.apache.sis.internal.coverage.j2d.ImageUtilities; import org.apache.sis.measure.NumberRange; import org.apache.sis.measure.Units; import org.apache.sis.referencing.crs.HardCodedCRS; @@ -74,14 +70,12 @@ public final strictfp class GridCoverage2DTest extends TestCase { * Create an image and set values directly as integers. We do not use one of the * BufferedImage.TYPE_* constant because this test uses some negative values. */ - final WritableRaster raster = WritableRaster.createBandedRaster(DataBuffer.TYPE_INT, size, size, 1, null); + final BufferedImage image = ImageUtilities.createGrayScale(DataBuffer.TYPE_INT, size, size, 1, 0, -10, 10); + final WritableRaster raster = image.getRaster(); raster.setSample(0, 0, 0, 2); raster.setSample(1, 0, 0, 5); raster.setSample(0, 1, 0, -5); raster.setSample(1, 1, 0, -10); - final ColorSpace colors = ColorModelFactory.createColorSpace(1, 0, -10, 10); - final ColorModel cm = new ComponentColorModel(colors, false, false, Transparency.OPAQUE, DataBuffer.TYPE_INT); - final BufferedImage image = new BufferedImage(cm, raster, false, null); return new GridCoverage2D(grid, Collections.singleton(sd), image); }
