From 84c10ed96deb2e6812f1ffa791e583d721a95813 Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Thu, 26 Sep 2024 10:31:27 +0200 Subject: [PATCH] Fully support period == 1 (no subsampling) in subsampleRow even if different arrays --- .../twelvemonkeys/imageio/util/IIOUtil.java | 12 ++++ .../imageio/util/IIOUtilTest.java | 63 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/util/IIOUtil.java b/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/util/IIOUtil.java index c3009826..fded5f04 100644 --- a/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/util/IIOUtil.java +++ b/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/util/IIOUtil.java @@ -232,6 +232,10 @@ public final class IIOUtil { int samplesPerPixel, int bitsPerSample, int samplePeriod) { // Period == 1 is a no-op... if (samplePeriod == 1) { + if (srcRow != destRow) { + System.arraycopy(srcRow, srcPos, destRow, destPos, srcWidth); + } + return; } @@ -275,6 +279,10 @@ public final class IIOUtil { int samplesPerPixel, int bitsPerSample, int samplePeriod) { // Period == 1 is a no-op... if (samplePeriod == 1) { + if (srcRow != destRow) { + System.arraycopy(srcRow, srcPos, destRow, destPos, srcWidth); + } + return; } @@ -297,6 +305,10 @@ public final class IIOUtil { int samplesPerPixel, int bitsPerSample, int samplePeriod) { // Period == 1 is a no-op... if (samplePeriod == 1) { + if (srcRow != destRow) { + System.arraycopy(srcRow, srcPos, destRow, destPos, srcWidth); + } + return; } diff --git a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/IIOUtilTest.java b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/IIOUtilTest.java index 05b9fa64..1406f9c9 100644 --- a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/IIOUtilTest.java +++ b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/IIOUtilTest.java @@ -139,6 +139,69 @@ public class IIOUtilTest { assertArrayEquals(expected, output); } + @Test + public void subsampleRowPeriod1ByteSameArray() { + byte[] inputOutput = {-1, 0, (byte) 0xAA, 0, -1}; + byte[] expected = {-1, 0, (byte) 0xAA, 0, -1}; + + IIOUtil.subsampleRow(inputOutput, 0, inputOutput.length, inputOutput, 0, 1, 8, 1); + + assertArrayEquals(expected, inputOutput); + } + + @Test + public void subsampleRowPeriod1ByteDifferentArray() { + byte[] input = {-1, 0, (byte) 0xAA, 0, -1}; + byte[] output = new byte[input.length]; + byte[] expected = {-1, 0, (byte) 0xAA, 0, -1}; + + IIOUtil.subsampleRow(input, 0, input.length, output, 0, 1, 8, 1); + + assertArrayEquals(expected, output); + } + + @Test + public void subsampleRowPeriod1ShortSameArray() { + short[] inputOutput = {-1, 0, (short) 0xAA77, 0, -1}; + short[] expected = {-1, 0, (short) 0xAA77, 0, -1}; + + IIOUtil.subsampleRow(inputOutput, 0, inputOutput.length, inputOutput, 0, 4, 4, 1); + + assertArrayEquals(expected, inputOutput); + } + + @Test + public void subsampleRowPeriod1ShortDifferentArray() { + short[] input = {-1, 0, (short) 0xAA77, 0, -1}; + short[] output = new short[input.length]; + short[] expected = {-1, 0, (short) 0xAA77, 0, -1}; + + IIOUtil.subsampleRow(input, 0, input.length, output, 0, 1, 16, 1); + + assertArrayEquals(expected, output); + } + + @Test + public void subsampleRowPeriod1IntSameArray() { + int[] inputOutput = {-1, 0, 0xAA997755, 0, -1}; + int[] expected = {-1, 0, 0xAA997755, 0, -1}; + + IIOUtil.subsampleRow(inputOutput, 0, inputOutput.length, inputOutput, 0, 1, 32, 1); + + assertArrayEquals(expected, inputOutput); + } + + @Test + public void subsampleRowPeriod1IntDifferentArray() { + int[] input = {-1, 0, 0xAA997755, 0, -1}; + int[] output = new int[input.length]; + int[] expected = {-1, 0, 0xAA997755, 0, -1}; + + IIOUtil.subsampleRow(input, 0, input.length, output, 0, 4, 8, 1); + + assertArrayEquals(expected, output); + } + private int divCeil(int numerator, int denominator) { return (numerator + denominator - 1) / denominator; }