You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2021/05/16 22:32:33 UTC

[sis] 01/04: Added an internal method for converting `java.awt.image.DataBuffer` enumeration values to `org.apache.sis.util.Numbers` enumeration values.

This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch GeoTIFF
in repository https://gitbox.apache.org/repos/asf/sis.git

commit ebf482e52fc36f99d494cb68f0083cd3b375b0cc
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Tue May 4 01:10:02 2021 +0200

    Added an internal method for converting `java.awt.image.DataBuffer`
    enumeration values to `org.apache.sis.util.Numbers` enumeration values.
---
 .../src/main/java/org/apache/sis/image/DataType.java  |  3 ++-
 .../sis/internal/coverage/j2d/ImageUtilities.java     | 19 +++++++++++++++++++
 .../sis/internal/coverage/j2d/ImageUtilitiesTest.java | 15 +++++++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/image/DataType.java b/core/sis-feature/src/main/java/org/apache/sis/image/DataType.java
index a298022..29a84a7 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/image/DataType.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/image/DataType.java
@@ -60,7 +60,8 @@ public enum DataType {
     SHORT,
 
     /**
-     * Signed 32-bits data.
+     * Signed 32-bits data. Also used for storing unsigned data; the Java2D API such as
+     * {@link java.awt.image.Raster#getSample(int, int, int)} can not distinguish the two cases.
      */
     INT,
 
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 a0a2e21..e270931 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
@@ -33,6 +33,7 @@ import java.awt.image.MultiPixelPackedSampleModel;
 import org.apache.sis.internal.feature.Resources;
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.util.Numerics;
+import org.apache.sis.util.Numbers;
 import org.apache.sis.util.Static;
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.resources.Vocabulary;
@@ -380,6 +381,24 @@ public final class ImageUtilities extends Static {
     }
 
     /**
+     * The values to be returned by {@link #toNumberEnum(int)}.
+     */
+    private static final byte[] NUMBER_ENUMS = {
+        Numbers.BYTE, Numbers.SHORT, Numbers.SHORT, Numbers.INTEGER, Numbers.FLOAT, Numbers.DOUBLE
+    };
+
+    /**
+     * Converts a {@link DataBuffer} enumeration value to {@link Numbers} enumeration value.
+     * This method ignores whether the type is signed or unsigned.
+     *
+     * @param  dataType  the {@link DataBuffer} enumeration value.
+     * @return the {@link Numbers} enumeration value.
+     */
+    public static byte toNumberEnum(final int dataType) {
+        return (dataType >= 0 && dataType < NUMBER_ENUMS.length) ? NUMBER_ENUMS[dataType] : Numbers.OTHER;
+    }
+
+    /**
      * Returns {@code true} if the given data buffer type is an integer type.
      * Returns {@code false} if the type is a floating point type or in case
      * of doubt (e.g. for {@link DataBuffer#TYPE_UNDEFINED}).
diff --git a/core/sis-feature/src/test/java/org/apache/sis/internal/coverage/j2d/ImageUtilitiesTest.java b/core/sis-feature/src/test/java/org/apache/sis/internal/coverage/j2d/ImageUtilitiesTest.java
index 91cecfc..458903b 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/internal/coverage/j2d/ImageUtilitiesTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/internal/coverage/j2d/ImageUtilitiesTest.java
@@ -26,6 +26,7 @@ import java.awt.image.SampleModel;
 import java.awt.image.BandedSampleModel;
 import java.awt.image.SinglePixelPackedSampleModel;
 import org.apache.sis.util.resources.Vocabulary;
+import org.apache.sis.util.Numbers;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -195,6 +196,20 @@ public final strictfp class ImageUtilitiesTest extends TestCase {
     }
 
     /**
+     * Tests {@link ImageUtilities#toNumberEnum(int)}.
+     */
+    @Test
+    public void testToNumberEnum() {
+        assertEquals(Numbers.BYTE,    ImageUtilities.toNumberEnum(DataBuffer.TYPE_BYTE));
+        assertEquals(Numbers.SHORT,   ImageUtilities.toNumberEnum(DataBuffer.TYPE_SHORT));
+        assertEquals(Numbers.SHORT,   ImageUtilities.toNumberEnum(DataBuffer.TYPE_USHORT));
+        assertEquals(Numbers.INTEGER, ImageUtilities.toNumberEnum(DataBuffer.TYPE_INT));
+        assertEquals(Numbers.FLOAT,   ImageUtilities.toNumberEnum(DataBuffer.TYPE_FLOAT));
+        assertEquals(Numbers.DOUBLE,  ImageUtilities.toNumberEnum(DataBuffer.TYPE_DOUBLE));
+        assertEquals(Numbers.OTHER,   ImageUtilities.toNumberEnum(DataBuffer.TYPE_UNDEFINED));
+    }
+
+    /**
      * Tests the {@link ImageUtilities#roundIfAlmostInteger(AffineTransform)} method.
      */
     @Test