This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-imaging.git
commit e7c4dd9e61f181b1d3c7db82ec63c7289f57e644 Author: Gary Gregory <[email protected]> AuthorDate: Sat Jul 11 16:47:09 2026 -0400 Sort test members --- .../commons/imaging/common/AllocatorTest.java | 12 +- .../imaging/common/BinaryFileFunctionsTest.java | 32 ++-- .../imaging/common/BinaryFunctionsTest.java | 32 ++-- .../formats/bmp/PixelParserBitFieldsTest.java | 28 ++-- .../imaging/formats/bmp/PixelParserRgbTest.java | 56 +++---- .../imaging/formats/bmp/PixelParserRleTest.java | 72 ++++----- .../formats/jpeg/exif/ExifRewriterTest.java | 124 +++++++------- .../imaging/formats/rgbe/RgbeParserTest.java | 58 +++---- .../imaging/formats/tiff/TiffClassesTest.java | 136 ++++++++-------- .../commons/imaging/formats/xpm/XpmParserTest.java | 74 ++++----- .../commons/imaging/palette/ColorGroupTest.java | 12 +- .../imaging/palette/ColorSpaceSubsetTest.java | 40 ++--- .../commons/imaging/palette/DitheringTest.java | 164 +++++++++---------- .../imaging/palette/LongestAxisMedianCutTest.java | 178 ++++++++++----------- .../imaging/palette/PaletteFactoryTest.java | 94 +++++------ 15 files changed, 556 insertions(+), 556 deletions(-) diff --git a/src/test/java/org/apache/commons/imaging/common/AllocatorTest.java b/src/test/java/org/apache/commons/imaging/common/AllocatorTest.java index fa2f2bfc..1cf283f8 100644 --- a/src/test/java/org/apache/commons/imaging/common/AllocatorTest.java +++ b/src/test/java/org/apache/commons/imaging/common/AllocatorTest.java @@ -23,15 +23,15 @@ import org.junit.jupiter.api.Test; class AllocatorTest { - /** 107374183 * 40 overflows int to 24, slipping past the byte-cost limit. */ - @Test - void testArrayOverflowIsRejected() { - assertThrows(AllocationRequestException.class, () -> Allocator.array(107374183, Object[]::new, 40)); - } - /** 536870912 * Integer.BYTES overflows int to a negative value. */ @Test void testArrayListOverflowIsRejected() { assertThrows(AllocationRequestException.class, () -> Allocator.arrayList(536870912)); } + + /** 107374183 * 40 overflows int to 24, slipping past the byte-cost limit. */ + @Test + void testArrayOverflowIsRejected() { + assertThrows(AllocationRequestException.class, () -> Allocator.array(107374183, Object[]::new, 40)); + } } diff --git a/src/test/java/org/apache/commons/imaging/common/BinaryFileFunctionsTest.java b/src/test/java/org/apache/commons/imaging/common/BinaryFileFunctionsTest.java index 004edb5b..bf77ee6e 100644 --- a/src/test/java/org/apache/commons/imaging/common/BinaryFileFunctionsTest.java +++ b/src/test/java/org/apache/commons/imaging/common/BinaryFileFunctionsTest.java @@ -27,6 +27,13 @@ class BinaryFileFunctionsTest extends AbstractImagingTest { private static final long TARGET_LONG = 0x01020304_05060708L; + @Test + void testArrayConversions() { + final int[] targets = {0x01020304, 0x05060708}; + final byte[] bytesBE = ByteConversions.toBytes(targets, ByteOrder.BIG_ENDIAN); + assertArrayEquals(targets, ByteConversions.toInts(bytesBE, ByteOrder.BIG_ENDIAN)); + } + @Test void testDoubleToByteConversion() { final byte[] bytesLE = ByteConversions.toBytes(1.0, ByteOrder.LITTLE_ENDIAN); @@ -44,15 +51,6 @@ class BinaryFileFunctionsTest extends AbstractImagingTest { final byte[] bytesBE = ByteConversions.toBytes(1.0f, ByteOrder.BIG_ENDIAN); assertEquals(ByteConversions.toFloat(bytesBE, ByteOrder.BIG_ENDIAN), 1.0f, 0f); } - - @Test - void testLongToByteConversion() { - final byte[] bytesLE = ByteConversions.toBytes(TARGET_LONG, ByteOrder.LITTLE_ENDIAN); - assertEquals(ByteConversions.toLong(bytesLE, ByteOrder.LITTLE_ENDIAN), TARGET_LONG); - - final byte[] bytesBE = ByteConversions.toBytes(TARGET_LONG, ByteOrder.BIG_ENDIAN); - assertEquals(ByteConversions.toLong(bytesBE, ByteOrder.BIG_ENDIAN), TARGET_LONG); - } @Test void testIntToByteConversion() { final int target = 0x01020304; @@ -63,6 +61,15 @@ class BinaryFileFunctionsTest extends AbstractImagingTest { assertEquals(ByteConversions.toInt(bytesBE, ByteOrder.BIG_ENDIAN), target); } + @Test + void testLongToByteConversion() { + final byte[] bytesLE = ByteConversions.toBytes(TARGET_LONG, ByteOrder.LITTLE_ENDIAN); + assertEquals(ByteConversions.toLong(bytesLE, ByteOrder.LITTLE_ENDIAN), TARGET_LONG); + + final byte[] bytesBE = ByteConversions.toBytes(TARGET_LONG, ByteOrder.BIG_ENDIAN); + assertEquals(ByteConversions.toLong(bytesBE, ByteOrder.BIG_ENDIAN), TARGET_LONG); + } + @Test void testShortToByteConversion() { final short target = 0x0102; @@ -72,11 +79,4 @@ class BinaryFileFunctionsTest extends AbstractImagingTest { final byte[] bytesBE = ByteConversions.toBytes(target, ByteOrder.BIG_ENDIAN); assertEquals(ByteConversions.toShort(bytesBE, ByteOrder.BIG_ENDIAN), target); } - - @Test - void testArrayConversions() { - final int[] targets = {0x01020304, 0x05060708}; - final byte[] bytesBE = ByteConversions.toBytes(targets, ByteOrder.BIG_ENDIAN); - assertArrayEquals(targets, ByteConversions.toInts(bytesBE, ByteOrder.BIG_ENDIAN)); - } } diff --git a/src/test/java/org/apache/commons/imaging/common/BinaryFunctionsTest.java b/src/test/java/org/apache/commons/imaging/common/BinaryFunctionsTest.java index c3b93742..23debf9d 100644 --- a/src/test/java/org/apache/commons/imaging/common/BinaryFunctionsTest.java +++ b/src/test/java/org/apache/commons/imaging/common/BinaryFunctionsTest.java @@ -35,11 +35,6 @@ public class BinaryFunctionsTest { assertEquals(0x01020304, BinaryFunctions.charsToQuad((char) 1, (char) 2, (char) 3, (char) 4)); } - @Test - public void testQuadsToByteArray() { - assertArrayEquals(new byte[] { 1, 2, 3, 4 }, BinaryFunctions.quadsToByteArray(0x01020304)); - } - @Test public void testCompareBytes() { final byte[] a = { 1, 2, 3, 4, 5 }; @@ -50,13 +45,15 @@ public class BinaryFunctionsTest { } @Test - public void testStartsWith() { - final byte[] haystack = { 1, 2, 3, 4, 5 }; - final byte[] needle = { 1, 2, 3 }; - final byte[] wrongNeedle = { 1, 2, 4 }; - assertTrue(BinaryFunctions.startsWith(haystack, needle)); - assertFalse(BinaryFunctions.startsWith(haystack, wrongNeedle)); - assertFalse(BinaryFunctions.startsWith(needle, haystack)); + public void testCopyOfRange() { + final byte[] original = { 1, 2, 3, 4, 5 }; + final byte[] copy = BinaryFunctions.copyOfRange(original, 1, 3); + assertArrayEquals(new byte[] { 2, 3, 4 }, copy); + } + + @Test + public void testQuadsToByteArray() { + assertArrayEquals(new byte[] { 1, 2, 3, 4 }, BinaryFunctions.quadsToByteArray(0x01020304)); } @Test @@ -68,9 +65,12 @@ public class BinaryFunctionsTest { } @Test - public void testCopyOfRange() { - final byte[] original = { 1, 2, 3, 4, 5 }; - final byte[] copy = BinaryFunctions.copyOfRange(original, 1, 3); - assertArrayEquals(new byte[] { 2, 3, 4 }, copy); + public void testStartsWith() { + final byte[] haystack = { 1, 2, 3, 4, 5 }; + final byte[] needle = { 1, 2, 3 }; + final byte[] wrongNeedle = { 1, 2, 4 }; + assertTrue(BinaryFunctions.startsWith(haystack, needle)); + assertFalse(BinaryFunctions.startsWith(haystack, wrongNeedle)); + assertFalse(BinaryFunctions.startsWith(needle, haystack)); } } diff --git a/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserBitFieldsTest.java b/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserBitFieldsTest.java index 43cc671c..f5457bc8 100644 --- a/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserBitFieldsTest.java +++ b/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserBitFieldsTest.java @@ -45,33 +45,33 @@ public class PixelParserBitFieldsTest { } @Test - public void testGetNextRgb_32Bit_WithAlpha() throws IOException { - final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 1, 1, 1, 32, 3, 0, 0, 0, 0, 0, 0x00FF0000, // red + public void testGetNextRgb_24Bit() throws IOException { + final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 1, 1, 1, 24, 3, 0, 0, 0, 0, 0, 0x00FF0000, // red 0x0000FF00, // green 0x000000FF, // blue - 0xFF000000, // alpha + 0, // alpha 0, null, 0, 0, 0, 0, 0, 0, 0); - // Data: ARGB = 0x80402010 - // Little endian: 0x10, 0x20, 0x40, 0x80 - final byte[] imageData = { 0x10, 0x20, 0x40, (byte) 0x80 }; + // Data: RGB = 0x112233 + // Little endian: 0x33, 0x22, 0x11 + final byte[] imageData = { 0x33, 0x22, 0x11 }; final PixelParserBitFields parser = new PixelParserBitFields(bhi, null, imageData); final int rgb = parser.getNextRgb(); - assertEquals(0x80402010, rgb); + assertEquals(0xFF112233, rgb); } @Test - public void testGetNextRgb_24Bit() throws IOException { - final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 1, 1, 1, 24, 3, 0, 0, 0, 0, 0, 0x00FF0000, // red + public void testGetNextRgb_32Bit_WithAlpha() throws IOException { + final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 1, 1, 1, 32, 3, 0, 0, 0, 0, 0, 0x00FF0000, // red 0x0000FF00, // green 0x000000FF, // blue - 0, // alpha + 0xFF000000, // alpha 0, null, 0, 0, 0, 0, 0, 0, 0); - // Data: RGB = 0x112233 - // Little endian: 0x33, 0x22, 0x11 - final byte[] imageData = { 0x33, 0x22, 0x11 }; + // Data: ARGB = 0x80402010 + // Little endian: 0x10, 0x20, 0x40, 0x80 + final byte[] imageData = { 0x10, 0x20, 0x40, (byte) 0x80 }; final PixelParserBitFields parser = new PixelParserBitFields(bhi, null, imageData); final int rgb = parser.getNextRgb(); - assertEquals(0xFF112233, rgb); + assertEquals(0x80402010, rgb); } @Test diff --git a/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserRgbTest.java b/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserRgbTest.java index 76409395..5a309823 100644 --- a/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserRgbTest.java +++ b/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserRgbTest.java @@ -32,34 +32,6 @@ public class PixelParserRgbTest { (byte) 0x00, (byte) 0xFF, (byte) 0x00, (byte) 0xFF, // 3: Green }; - @Test - public void testGetNextRgb_1Bit() throws IOException { - final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); - // Data: 0b10100000 -> sample 1, then 0, then 1, then 0... - final byte[] imageData = { (byte) 0xA0 }; - final PixelParserRgb parser = new PixelParserRgb(bhi, COLOR_TABLE, imageData); - assertEquals(0xFFFFFFFF, parser.getNextRgb()); // 1 -> White - assertEquals(0xFF000000, parser.getNextRgb()); // 0 -> Black - } - - @Test - public void testGetNextRgb_4Bit() throws IOException { - final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 2, 1, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); - // Data: 0x23 -> sample 2, then 3 - final byte[] imageData = { 0x23 }; - final PixelParserRgb parser = new PixelParserRgb(bhi, COLOR_TABLE, imageData); - assertEquals(0xFFFF0000, parser.getNextRgb()); // 2 -> Red - assertEquals(0xFF00FF00, parser.getNextRgb()); // 3 -> Green - } - - @Test - public void testGetNextRgb_8Bit() throws IOException { - final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 1, 1, 1, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); - final byte[] imageData = { 2 }; - final PixelParserRgb parser = new PixelParserRgb(bhi, COLOR_TABLE, imageData); - assertEquals(0xFFFF0000, parser.getNextRgb()); // 2 -> Red - } - @Test public void testGetNextRgb_16Bit() throws IOException { final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 1, 1, 1, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); @@ -71,6 +43,16 @@ public class PixelParserRgbTest { assertEquals(0xFF0000F8, parser.getNextRgb()); } + @Test + public void testGetNextRgb_1Bit() throws IOException { + final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); + // Data: 0b10100000 -> sample 1, then 0, then 1, then 0... + final byte[] imageData = { (byte) 0xA0 }; + final PixelParserRgb parser = new PixelParserRgb(bhi, COLOR_TABLE, imageData); + assertEquals(0xFFFFFFFF, parser.getNextRgb()); // 1 -> White + assertEquals(0xFF000000, parser.getNextRgb()); // 0 -> Black + } + @Test public void testGetNextRgb_24Bit() throws IOException { final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 1, 1, 1, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); @@ -89,6 +71,24 @@ public class PixelParserRgbTest { assertEquals(0xFF112233, parser.getNextRgb()); } + @Test + public void testGetNextRgb_4Bit() throws IOException { + final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 2, 1, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); + // Data: 0x23 -> sample 2, then 3 + final byte[] imageData = { 0x23 }; + final PixelParserRgb parser = new PixelParserRgb(bhi, COLOR_TABLE, imageData); + assertEquals(0xFFFF0000, parser.getNextRgb()); // 2 -> Red + assertEquals(0xFF00FF00, parser.getNextRgb()); // 3 -> Green + } + + @Test + public void testGetNextRgb_8Bit() throws IOException { + final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 1, 1, 1, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); + final byte[] imageData = { 2 }; + final PixelParserRgb parser = new PixelParserRgb(bhi, COLOR_TABLE, imageData); + assertEquals(0xFFFF0000, parser.getNextRgb()); // 2 -> Red + } + @Test public void testNewlinePadding() throws IOException { final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 1, 1, 1, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); diff --git a/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserRleTest.java b/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserRleTest.java index 8eaa79a2..e6cd30e8 100644 --- a/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserRleTest.java +++ b/src/test/java/org/apache/commons/imaging/formats/bmp/PixelParserRleTest.java @@ -32,42 +32,6 @@ public class PixelParserRleTest { (byte) 0x00, (byte) 0xFF, (byte) 0x00, (byte) 0xFF, // 3: Green }; - @Test - public void testProcessImage_Rle8() throws IOException { - final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 2, 2, 1, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); - // RLE8 data: - // 02 01 -> Encoded mode: repeat color index 1, 2 times - // 00 00 -> Escape mode: End of line - // 02 02 -> Encoded mode: repeat color index 2, 2 times - // 00 01 -> Escape mode: End of file - final byte[] imageData = { 0x02, 0x01, 0x00, 0x00, 0x02, 0x02, 0x00, 0x01 }; - final PixelParserRle parser = new PixelParserRle(bhi, COLOR_TABLE, imageData); - final ImageBuilder imageBuilder = new ImageBuilder(2, 2, false); - parser.processImage(imageBuilder); - // Row 1 (bottom row in BMP, y=1) - assertEquals(0xFFFFFFFF, imageBuilder.getRgb(0, 1)); - assertEquals(0xFFFFFFFF, imageBuilder.getRgb(1, 1)); - // Row 0 (y=0) - assertEquals(0xFFFF0000, imageBuilder.getRgb(0, 0)); - assertEquals(0xFFFF0000, imageBuilder.getRgb(1, 0)); - } - - @Test - public void testProcessImage_Rle4() throws IOException { - final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 4, 1, 1, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); - // RLE4 data: - // 04 23 -> Encoded mode: repeat colors index 2 and 3, 4 times (2, 3, 2, 3) - // 00 01 -> Escape mode: End of file - final byte[] imageData = { 0x04, 0x23, 0x00, 0x01 }; - final PixelParserRle parser = new PixelParserRle(bhi, COLOR_TABLE, imageData); - final ImageBuilder imageBuilder = new ImageBuilder(4, 1, false); - parser.processImage(imageBuilder); - assertEquals(0xFFFF0000, imageBuilder.getRgb(0, 0)); // 2 - assertEquals(0xFF00FF00, imageBuilder.getRgb(1, 0)); // 3 - assertEquals(0xFFFF0000, imageBuilder.getRgb(2, 0)); // 2 - assertEquals(0xFF00FF00, imageBuilder.getRgb(3, 0)); // 3 - } - @Test public void testProcessImage_AbsoluteMode_Rle8() throws IOException { final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 3, 1, 1, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); @@ -99,4 +63,40 @@ public class PixelParserRleTest { parser.processImage(imageBuilder); assertEquals(0xFFFF0000, imageBuilder.getRgb(1, 2)); } + + @Test + public void testProcessImage_Rle4() throws IOException { + final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 4, 1, 1, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); + // RLE4 data: + // 04 23 -> Encoded mode: repeat colors index 2 and 3, 4 times (2, 3, 2, 3) + // 00 01 -> Escape mode: End of file + final byte[] imageData = { 0x04, 0x23, 0x00, 0x01 }; + final PixelParserRle parser = new PixelParserRle(bhi, COLOR_TABLE, imageData); + final ImageBuilder imageBuilder = new ImageBuilder(4, 1, false); + parser.processImage(imageBuilder); + assertEquals(0xFFFF0000, imageBuilder.getRgb(0, 0)); // 2 + assertEquals(0xFF00FF00, imageBuilder.getRgb(1, 0)); // 3 + assertEquals(0xFFFF0000, imageBuilder.getRgb(2, 0)); // 2 + assertEquals(0xFF00FF00, imageBuilder.getRgb(3, 0)); // 3 + } + + @Test + public void testProcessImage_Rle8() throws IOException { + final BmpHeaderInfo bhi = new BmpHeaderInfo((byte) 0, (byte) 0, 0, 0, 0, 0, 2, 2, 1, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0); + // RLE8 data: + // 02 01 -> Encoded mode: repeat color index 1, 2 times + // 00 00 -> Escape mode: End of line + // 02 02 -> Encoded mode: repeat color index 2, 2 times + // 00 01 -> Escape mode: End of file + final byte[] imageData = { 0x02, 0x01, 0x00, 0x00, 0x02, 0x02, 0x00, 0x01 }; + final PixelParserRle parser = new PixelParserRle(bhi, COLOR_TABLE, imageData); + final ImageBuilder imageBuilder = new ImageBuilder(2, 2, false); + parser.processImage(imageBuilder); + // Row 1 (bottom row in BMP, y=1) + assertEquals(0xFFFFFFFF, imageBuilder.getRgb(0, 1)); + assertEquals(0xFFFFFFFF, imageBuilder.getRgb(1, 1)); + // Row 0 (y=0) + assertEquals(0xFFFF0000, imageBuilder.getRgb(0, 0)); + assertEquals(0xFFFF0000, imageBuilder.getRgb(1, 0)); + } } diff --git a/src/test/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriterTest.java b/src/test/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriterTest.java index a39070b1..2a64bed1 100644 --- a/src/test/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriterTest.java +++ b/src/test/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriterTest.java @@ -41,6 +41,68 @@ import org.junit.jupiter.api.Test; public class ExifRewriterTest extends AbstractExifTest { + @Test + public void testAddExifToImageWithJfif() throws Exception { + // Create a minimal JPEG with SOI and JFIF (APP0) and SOS + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + baos.write(0xFF); + baos.write(0xD8); // SOI + baos.write(0xFF); + baos.write(0xE0); // APP0 (JFIF) + baos.write(0x00); + baos.write(0x10); // Length (16) + baos.write("JFIF".getBytes()); + baos.write(0); // Version + baos.write(new byte[9]); // Rest of JFIF + baos.write(0xFF); + baos.write(0xDA); // SOS + baos.write(0x00); + baos.write(0x0C); // Length + baos.write(new byte[10]); // SOS data + baos.write(0xFF); + baos.write(0xD9); // EOI + final byte[] imageBytes = baos.toByteArray(); + final TiffOutputSet outputSet = new TiffOutputSet(); + outputSet.getOrCreateRootDirectory(); + final ByteArrayOutputStream os = new ByteArrayOutputStream(); + new ExifRewriter().updateExifMetadataLossy(imageBytes, os, outputSet); + final byte[] outBytes = os.toByteArray(); + assertTrue(hasExifData("test.jpg", outBytes)); + // Check that EXIF (APP1) is AFTER JFIF (APP0) + // SOI (2) + JFIF (16+2=18) = 20 + // Next marker at index 20 should be APP1 (0xFFE1) + assertEquals((byte) 0xFF, outBytes[20]); + assertEquals((byte) 0xE1, outBytes[21]); + } + + @Test + public void testAddExifToImageWithoutExif() throws Exception { + // Find a JPEG image without EXIF + final File imageFile = getTestImage(file -> file.getName().toLowerCase().endsWith(".jpg") && !hasExifData(file)); + assertNotNull(imageFile); + final TiffOutputSet outputSet = new TiffOutputSet(); + final TiffOutputDirectory root = outputSet.getOrCreateRootDirectory(); + root.add(ExifTagConstants.EXIF_TAG_EXIF_VERSION, (byte) 1, (byte) 2, (byte) 3, (byte) 4); + final ByteArrayOutputStream os = new ByteArrayOutputStream(); + new ExifRewriter().updateExifMetadataLossy(imageFile, os, outputSet); + final byte[] outBytes = os.toByteArray(); + assertTrue(hasExifData("test.jpg", outBytes)); + } + + @Test + public void testImagingOverflowException() throws Exception { + final File imageFile = getImageWithExifData(); + final TiffOutputSet outputSet = new TiffOutputSet(); + final TiffOutputDirectory root = outputSet.getOrCreateRootDirectory(); + // Add a very large field to exceed 64KB limit of APP1 segment + final byte[] largeData = new byte[70000]; + root.add(ExifTagConstants.EXIF_TAG_MAKER_NOTE, largeData); + final ByteArrayOutputStream os = new ByteArrayOutputStream(); + assertThrows(ImagingOverflowException.class, () -> { + new ExifRewriter().updateExifMetadataLossy(imageFile, os, outputSet); + }); + } + @Test public void testRemoveExifMetadata_ByteArray() throws Exception { final File imageFile = getImageWithExifData(); @@ -158,66 +220,4 @@ public class ExifRewriterTest extends AbstractExifTest { assertTrue(hasExifData("test.jpg", outBytes)); } } - - @Test - public void testAddExifToImageWithoutExif() throws Exception { - // Find a JPEG image without EXIF - final File imageFile = getTestImage(file -> file.getName().toLowerCase().endsWith(".jpg") && !hasExifData(file)); - assertNotNull(imageFile); - final TiffOutputSet outputSet = new TiffOutputSet(); - final TiffOutputDirectory root = outputSet.getOrCreateRootDirectory(); - root.add(ExifTagConstants.EXIF_TAG_EXIF_VERSION, (byte) 1, (byte) 2, (byte) 3, (byte) 4); - final ByteArrayOutputStream os = new ByteArrayOutputStream(); - new ExifRewriter().updateExifMetadataLossy(imageFile, os, outputSet); - final byte[] outBytes = os.toByteArray(); - assertTrue(hasExifData("test.jpg", outBytes)); - } - - @Test - public void testAddExifToImageWithJfif() throws Exception { - // Create a minimal JPEG with SOI and JFIF (APP0) and SOS - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - baos.write(0xFF); - baos.write(0xD8); // SOI - baos.write(0xFF); - baos.write(0xE0); // APP0 (JFIF) - baos.write(0x00); - baos.write(0x10); // Length (16) - baos.write("JFIF".getBytes()); - baos.write(0); // Version - baos.write(new byte[9]); // Rest of JFIF - baos.write(0xFF); - baos.write(0xDA); // SOS - baos.write(0x00); - baos.write(0x0C); // Length - baos.write(new byte[10]); // SOS data - baos.write(0xFF); - baos.write(0xD9); // EOI - final byte[] imageBytes = baos.toByteArray(); - final TiffOutputSet outputSet = new TiffOutputSet(); - outputSet.getOrCreateRootDirectory(); - final ByteArrayOutputStream os = new ByteArrayOutputStream(); - new ExifRewriter().updateExifMetadataLossy(imageBytes, os, outputSet); - final byte[] outBytes = os.toByteArray(); - assertTrue(hasExifData("test.jpg", outBytes)); - // Check that EXIF (APP1) is AFTER JFIF (APP0) - // SOI (2) + JFIF (16+2=18) = 20 - // Next marker at index 20 should be APP1 (0xFFE1) - assertEquals((byte) 0xFF, outBytes[20]); - assertEquals((byte) 0xE1, outBytes[21]); - } - - @Test - public void testImagingOverflowException() throws Exception { - final File imageFile = getImageWithExifData(); - final TiffOutputSet outputSet = new TiffOutputSet(); - final TiffOutputDirectory root = outputSet.getOrCreateRootDirectory(); - // Add a very large field to exceed 64KB limit of APP1 segment - final byte[] largeData = new byte[70000]; - root.add(ExifTagConstants.EXIF_TAG_MAKER_NOTE, largeData); - final ByteArrayOutputStream os = new ByteArrayOutputStream(); - assertThrows(ImagingOverflowException.class, () -> { - new ExifRewriter().updateExifMetadataLossy(imageFile, os, outputSet); - }); - } } diff --git a/src/test/java/org/apache/commons/imaging/formats/rgbe/RgbeParserTest.java b/src/test/java/org/apache/commons/imaging/formats/rgbe/RgbeParserTest.java index 59f23cbc..03fb824d 100644 --- a/src/test/java/org/apache/commons/imaging/formats/rgbe/RgbeParserTest.java +++ b/src/test/java/org/apache/commons/imaging/formats/rgbe/RgbeParserTest.java @@ -37,25 +37,6 @@ import org.junit.jupiter.api.Test; public class RgbeParserTest { - @Test - public void testRgbeImageParserBasics() { - final RgbeImageParser parser = new RgbeImageParser(); - assertEquals("Radiance HDR", parser.getName()); - assertEquals("hdr", parser.getDefaultExtension()); - final String[] extensions = parser.getAcceptedExtensions(); - assertTrue(extensions.length > 0); - final ImageFormat[] types = parser.getAcceptedTypes(); - assertEquals(1, types.length); - assertEquals(ImageFormats.RGBE, types[0]); - assertNotNull(parser.getDefaultParameters()); - } - - @Test - public void testRgbeImagingParameters() { - final RgbeImagingParameters params = new RgbeImagingParameters(); - assertNotNull(params); - } - @Test public void testGetIccProfileBytes() throws IOException { final RgbeImageParser parser = new RgbeImageParser(); @@ -63,16 +44,6 @@ public class RgbeParserTest { assertNull(parser.getIccProfileBytes(ByteSource.array(bytes), new RgbeImagingParameters())); } - @Test - public void testGetMetadata() throws IOException { - final RgbeImageParser parser = new RgbeImageParser(); - final String header = "#?RADIANCE\nFORMAT=32-bit_rle_rgbe\nEXPOSURE=1.0\n\n-Y 1 +X 1\n"; - final byte[] bytes = header.getBytes(StandardCharsets.US_ASCII); - final ImageMetadata metadata = parser.getMetadata(ByteSource.array(bytes), new RgbeImagingParameters()); - assertNotNull(metadata); - assertEquals(2, metadata.getItems().size()); - } - @Test public void testGetImageInfo() throws IOException { final RgbeImageParser parser = new RgbeImageParser(); @@ -95,6 +66,16 @@ public class RgbeParserTest { assertEquals(10, size.height); } + @Test + public void testGetMetadata() throws IOException { + final RgbeImageParser parser = new RgbeImageParser(); + final String header = "#?RADIANCE\nFORMAT=32-bit_rle_rgbe\nEXPOSURE=1.0\n\n-Y 1 +X 1\n"; + final byte[] bytes = header.getBytes(StandardCharsets.US_ASCII); + final ImageMetadata metadata = parser.getMetadata(ByteSource.array(bytes), new RgbeImagingParameters()); + assertNotNull(metadata); + assertEquals(2, metadata.getItems().size()); + } + @Test public void testInvalidHeader() { final RgbeImageParser parser = new RgbeImageParser(); @@ -109,4 +90,23 @@ public class RgbeParserTest { final byte[] bytes = header.getBytes(StandardCharsets.US_ASCII); assertThrows(ImagingException.class, () -> parser.getImageSize(ByteSource.array(bytes), new RgbeImagingParameters())); } + + @Test + public void testRgbeImageParserBasics() { + final RgbeImageParser parser = new RgbeImageParser(); + assertEquals("Radiance HDR", parser.getName()); + assertEquals("hdr", parser.getDefaultExtension()); + final String[] extensions = parser.getAcceptedExtensions(); + assertTrue(extensions.length > 0); + final ImageFormat[] types = parser.getAcceptedTypes(); + assertEquals(1, types.length); + assertEquals(ImageFormats.RGBE, types[0]); + assertNotNull(parser.getDefaultParameters()); + } + + @Test + public void testRgbeImagingParameters() { + final RgbeImagingParameters params = new RgbeImagingParameters(); + assertNotNull(params); + } } diff --git a/src/test/java/org/apache/commons/imaging/formats/tiff/TiffClassesTest.java b/src/test/java/org/apache/commons/imaging/formats/tiff/TiffClassesTest.java index 77ae92b0..45550856 100644 --- a/src/test/java/org/apache/commons/imaging/formats/tiff/TiffClassesTest.java +++ b/src/test/java/org/apache/commons/imaging/formats/tiff/TiffClassesTest.java @@ -35,21 +35,6 @@ import org.junit.jupiter.api.Test; public class TiffClassesTest { - @Test - public void testTiffHeader() { - final ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN; - final int tiffVersion = 42; - final long offsetToFirstIFD = 8; - final boolean bigTiff = false; - final TiffHeader header = new TiffHeader(byteOrder, tiffVersion, offsetToFirstIFD, bigTiff); - assertEquals(byteOrder, header.byteOrder); - assertEquals(tiffVersion, header.tiffVersion); - assertEquals(offsetToFirstIFD, header.offsetToFirstIFD); - assertEquals(bigTiff, header.bigTiff); - assertEquals("TIFF Header", header.getElementDescription()); - assertEquals(0, header.offset); - } - @Test public void testTiffContents() throws ImagingException { final TiffHeader header = new TiffHeader(ByteOrder.LITTLE_ENDIAN, 42, 8, false); @@ -67,49 +52,16 @@ public class TiffClassesTest { } @Test - public void testTiffRasterDataType() { - assertEquals(TiffRasterDataType.INTEGER, TiffRasterDataType.valueOf("INTEGER")); - assertEquals(TiffRasterDataType.FLOAT, TiffRasterDataType.valueOf("FLOAT")); - assertEquals(2, TiffRasterDataType.values().length); - } - - @Test - public void testTiffRasterStatistics() { - final float[] data = { 1.0f, 2.0f, Float.NaN, 3.0f, 4.0f, 5.0f }; - final TiffRasterDataFloat raster = new TiffRasterDataFloat(2, 3, data); - final TiffRasterStatistics stats = new TiffRasterStatistics(raster, 5.0f); - assertEquals(1, stats.getCountOfNulls()); - assertEquals(4, stats.getCountOfSamples()); - assertEquals(5.0f, stats.getExcludedValue()); - assertEquals(4.0f, stats.getMaxValue()); - assertEquals(1.0f, stats.getMinValue()); - assertEquals(2.5f, stats.getMeanValue(), 0.001f); - assertTrue(stats.isAnExcludedValueSet()); - final TiffRasterStatistics statsNoExcluded = new TiffRasterStatistics(raster, Float.NaN); - assertEquals(5, statsNoExcluded.getCountOfSamples()); - assertFalse(statsNoExcluded.isAnExcludedValueSet()); - } - - @Test - public void testTiffRasterStatisticsAllNaN() { - final float[] data = { Float.NaN, Float.NaN }; - final TiffRasterDataFloat raster = new TiffRasterDataFloat(1, 2, data); - final TiffRasterStatistics stats = new TiffRasterStatistics(raster, Float.NaN); - assertEquals(2, stats.getCountOfNulls()); - assertEquals(0, stats.getCountOfSamples()); - assertEquals(0.0f, stats.getMeanValue()); - assertEquals(Float.POSITIVE_INFINITY, stats.getMinValue()); - assertEquals(Float.NEGATIVE_INFINITY, stats.getMaxValue()); - } - - @Test - public void testTiffTags() { - assertNotNull(TiffTags.getTag(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT, TiffTagConstants.TIFF_TAG_COMPRESSION.tag)); - assertEquals(TiffTagConstants.TIFF_TAG_COMPRESSION.name, - TiffTags.getTag(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT, TiffTagConstants.TIFF_TAG_COMPRESSION.tag).name); - // Unknown tag - assertEquals(TiffTagConstants.TIFF_TAG_UNKNOWN.name, TiffTags.getTag(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT, -1).name); - assertNotNull(TiffTags.getTagCount(TiffTagConstants.TIFF_TAG_COMPRESSION.tag)); + public void testTiffDirectory() { + final List<TiffField> entries = new ArrayList<>(); + final TiffDirectory directory = new TiffDirectory(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT, entries, 0, 0, ByteOrder.LITTLE_ENDIAN); + assertEquals(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT, directory.type); + assertEquals(0, directory.offset); + assertEquals(0, directory.getNextDirectoryOffset()); + assertEquals(ByteOrder.LITTLE_ENDIAN, directory.getByteOrder()); + assertNotNull(directory.getElementDescription()); + assertEquals(0, directory.size()); + assertFalse(directory.iterator().hasNext()); } @Test @@ -130,16 +82,18 @@ public class TiffClassesTest { } @Test - public void testTiffDirectory() { - final List<TiffField> entries = new ArrayList<>(); - final TiffDirectory directory = new TiffDirectory(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT, entries, 0, 0, ByteOrder.LITTLE_ENDIAN); - assertEquals(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT, directory.type); - assertEquals(0, directory.offset); - assertEquals(0, directory.getNextDirectoryOffset()); - assertEquals(ByteOrder.LITTLE_ENDIAN, directory.getByteOrder()); - assertNotNull(directory.getElementDescription()); - assertEquals(0, directory.size()); - assertFalse(directory.iterator().hasNext()); + public void testTiffHeader() { + final ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN; + final int tiffVersion = 42; + final long offsetToFirstIFD = 8; + final boolean bigTiff = false; + final TiffHeader header = new TiffHeader(byteOrder, tiffVersion, offsetToFirstIFD, bigTiff); + assertEquals(byteOrder, header.byteOrder); + assertEquals(tiffVersion, header.tiffVersion); + assertEquals(offsetToFirstIFD, header.offsetToFirstIFD); + assertEquals(bigTiff, header.bigTiff); + assertEquals("TIFF Header", header.getElementDescription()); + assertEquals(0, header.offset); } @Test @@ -200,4 +154,50 @@ public class TiffClassesTest { assertNotNull(raster.getData()); assertNotNull(raster.getSimpleStatistics()); } + + @Test + public void testTiffRasterDataType() { + assertEquals(TiffRasterDataType.INTEGER, TiffRasterDataType.valueOf("INTEGER")); + assertEquals(TiffRasterDataType.FLOAT, TiffRasterDataType.valueOf("FLOAT")); + assertEquals(2, TiffRasterDataType.values().length); + } + + @Test + public void testTiffRasterStatistics() { + final float[] data = { 1.0f, 2.0f, Float.NaN, 3.0f, 4.0f, 5.0f }; + final TiffRasterDataFloat raster = new TiffRasterDataFloat(2, 3, data); + final TiffRasterStatistics stats = new TiffRasterStatistics(raster, 5.0f); + assertEquals(1, stats.getCountOfNulls()); + assertEquals(4, stats.getCountOfSamples()); + assertEquals(5.0f, stats.getExcludedValue()); + assertEquals(4.0f, stats.getMaxValue()); + assertEquals(1.0f, stats.getMinValue()); + assertEquals(2.5f, stats.getMeanValue(), 0.001f); + assertTrue(stats.isAnExcludedValueSet()); + final TiffRasterStatistics statsNoExcluded = new TiffRasterStatistics(raster, Float.NaN); + assertEquals(5, statsNoExcluded.getCountOfSamples()); + assertFalse(statsNoExcluded.isAnExcludedValueSet()); + } + + @Test + public void testTiffRasterStatisticsAllNaN() { + final float[] data = { Float.NaN, Float.NaN }; + final TiffRasterDataFloat raster = new TiffRasterDataFloat(1, 2, data); + final TiffRasterStatistics stats = new TiffRasterStatistics(raster, Float.NaN); + assertEquals(2, stats.getCountOfNulls()); + assertEquals(0, stats.getCountOfSamples()); + assertEquals(0.0f, stats.getMeanValue()); + assertEquals(Float.POSITIVE_INFINITY, stats.getMinValue()); + assertEquals(Float.NEGATIVE_INFINITY, stats.getMaxValue()); + } + + @Test + public void testTiffTags() { + assertNotNull(TiffTags.getTag(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT, TiffTagConstants.TIFF_TAG_COMPRESSION.tag)); + assertEquals(TiffTagConstants.TIFF_TAG_COMPRESSION.name, + TiffTags.getTag(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT, TiffTagConstants.TIFF_TAG_COMPRESSION.tag).name); + // Unknown tag + assertEquals(TiffTagConstants.TIFF_TAG_UNKNOWN.name, TiffTags.getTag(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT, -1).name); + assertNotNull(TiffTags.getTagCount(TiffTagConstants.TIFF_TAG_COMPRESSION.tag)); + } } diff --git a/src/test/java/org/apache/commons/imaging/formats/xpm/XpmParserTest.java b/src/test/java/org/apache/commons/imaging/formats/xpm/XpmParserTest.java index 87f04d79..1a9fb1f3 100644 --- a/src/test/java/org/apache/commons/imaging/formats/xpm/XpmParserTest.java +++ b/src/test/java/org/apache/commons/imaging/formats/xpm/XpmParserTest.java @@ -31,30 +31,6 @@ import org.junit.jupiter.api.Test; public class XpmParserTest { - @Test - public void testXpmHeader() throws Exception { - // XpmHeader is private static final, use reflection to test it - final Class<?> headerClass = Class.forName("org.apache.commons.imaging.formats.xpm.XpmImageParser$XpmHeader"); - final Constructor<?> constructor = headerClass.getDeclaredConstructor(int.class, int.class, int.class, int.class, int.class, int.class, boolean.class); - constructor.setAccessible(true); - final Object header = constructor.newInstance(10, 20, 2, 1, 5, 5, true); - final StringWriter sw = new StringWriter(); - final PrintWriter pw = new PrintWriter(sw); - final Method dumpMethod = headerClass.getMethod("dump", PrintWriter.class); - dumpMethod.invoke(header, pw); - pw.flush(); - final String output = sw.toString(); - assertNotNull(output); - // Using contains because exact line endings might vary by OS - assertTrue(output.contains("Width: 10")); - assertTrue(output.contains("Height: 20")); - assertTrue(output.contains("NumColors: 2")); - assertTrue(output.contains("NumCharsPerPixel: 1")); - assertTrue(output.contains("X hotspot: 5")); - assertTrue(output.contains("Y hotspot: 5")); - assertTrue(output.contains("XpmExt: true")); - } - @Test public void testPaletteEntry() throws Exception { final Class<?> entryClass = Class.forName("org.apache.commons.imaging.formats.xpm.XpmImageParser$PaletteEntry"); @@ -104,19 +80,6 @@ public class XpmParserTest { assertEquals(0xFF666666, getBestArgbMethod.invoke(entry)); } - @Test - public void testXpmImagingParameters() { - final XpmImagingParameters params = new XpmImagingParameters(); - assertNotNull(params); - } - - @Test - public void testXpmImageParserBasics() { - final XpmImageParser parser = new XpmImageParser(); - assertEquals("X PixMap", parser.getName()); - assertEquals("xpm", parser.getDefaultExtension()); - } - @Test public void testParseColor() throws Exception { final XpmImageParser parser = new XpmImageParser(); @@ -136,4 +99,41 @@ public class XpmParserTest { // Unknown color name assertEquals(0x00000000, parseColorMethod.invoke(parser, "nonexistent_color_name")); } + + @Test + public void testXpmHeader() throws Exception { + // XpmHeader is private static final, use reflection to test it + final Class<?> headerClass = Class.forName("org.apache.commons.imaging.formats.xpm.XpmImageParser$XpmHeader"); + final Constructor<?> constructor = headerClass.getDeclaredConstructor(int.class, int.class, int.class, int.class, int.class, int.class, boolean.class); + constructor.setAccessible(true); + final Object header = constructor.newInstance(10, 20, 2, 1, 5, 5, true); + final StringWriter sw = new StringWriter(); + final PrintWriter pw = new PrintWriter(sw); + final Method dumpMethod = headerClass.getMethod("dump", PrintWriter.class); + dumpMethod.invoke(header, pw); + pw.flush(); + final String output = sw.toString(); + assertNotNull(output); + // Using contains because exact line endings might vary by OS + assertTrue(output.contains("Width: 10")); + assertTrue(output.contains("Height: 20")); + assertTrue(output.contains("NumColors: 2")); + assertTrue(output.contains("NumCharsPerPixel: 1")); + assertTrue(output.contains("X hotspot: 5")); + assertTrue(output.contains("Y hotspot: 5")); + assertTrue(output.contains("XpmExt: true")); + } + + @Test + public void testXpmImageParserBasics() { + final XpmImageParser parser = new XpmImageParser(); + assertEquals("X PixMap", parser.getName()); + assertEquals("xpm", parser.getDefaultExtension()); + } + + @Test + public void testXpmImagingParameters() { + final XpmImagingParameters params = new XpmImagingParameters(); + assertNotNull(params); + } } diff --git a/src/test/java/org/apache/commons/imaging/palette/ColorGroupTest.java b/src/test/java/org/apache/commons/imaging/palette/ColorGroupTest.java index 58381cae..05828548 100644 --- a/src/test/java/org/apache/commons/imaging/palette/ColorGroupTest.java +++ b/src/test/java/org/apache/commons/imaging/palette/ColorGroupTest.java @@ -59,6 +59,12 @@ public class ColorGroupTest { assertEquals(15 + 30 + 45 + 60, group.diffTotal); } + @Test + public void testConstructorEmpty() { + final List<ColorCount> colorCounts = new ArrayList<>(); + assertThrows(ImagingException.class, () -> new ColorGroup(colorCounts, false)); + } + @Test public void testConstructorWithIgnoreAlpha() throws ImagingException { final List<ColorCount> colorCounts = new ArrayList<>(); @@ -78,12 +84,6 @@ public class ColorGroupTest { assertTrue(group.ignoreAlpha); } - @Test - public void testConstructorEmpty() { - final List<ColorCount> colorCounts = new ArrayList<>(); - assertThrows(ImagingException.class, () -> new ColorGroup(colorCounts, false)); - } - @Test public void testContains() throws ImagingException { final List<ColorCount> colorCounts = new ArrayList<>(); diff --git a/src/test/java/org/apache/commons/imaging/palette/ColorSpaceSubsetTest.java b/src/test/java/org/apache/commons/imaging/palette/ColorSpaceSubsetTest.java index b91b1436..bfbabd26 100644 --- a/src/test/java/org/apache/commons/imaging/palette/ColorSpaceSubsetTest.java +++ b/src/test/java/org/apache/commons/imaging/palette/ColorSpaceSubsetTest.java @@ -78,6 +78,15 @@ public class ColorSpaceSubsetTest { assertFalse(restricted.contains(80, 80, 168)); } + @Test + public void testDump() { + // Just verify it doesn't throw anything + final ColorSpaceSubset subset = new ColorSpaceSubset(100, 8); + subset.rgb = 0x123456; + subset.dump("Test"); + subset.dumpJustRgb("TestJustRgb"); + } + @Test public void testGetArea() { final int[] mins = { 10, 20, 30 }; @@ -87,6 +96,17 @@ public class ColorSpaceSubsetTest { assertEquals(6 * 6 * 6, subset.getArea()); } + @Test + public void testRgbComparator() { + final ColorSpaceSubset subset1 = new ColorSpaceSubset(100, 8); + subset1.rgb = 0x102030; + final ColorSpaceSubset subset2 = new ColorSpaceSubset(100, 8); + subset2.rgb = 0x102040; + assertTrue(ColorSpaceSubset.RGB_COMPARATOR.compare(subset1, subset2) < 0); + assertTrue(ColorSpaceSubset.RGB_COMPARATOR.compare(subset2, subset1) > 0); + assertEquals(0, ColorSpaceSubset.RGB_COMPARATOR.compare(subset1, subset1)); + } + @Test public void testSetAverageRgb() { final int precision = 2; // 2 bits per component. 0, 1, 2, 3 @@ -111,24 +131,4 @@ public class ColorSpaceSubsetTest { // rgb = (96 << 16) | (96 << 8) | 96 assertEquals(0x606060, subset.rgb & 0xffffff); } - - @Test - public void testRgbComparator() { - final ColorSpaceSubset subset1 = new ColorSpaceSubset(100, 8); - subset1.rgb = 0x102030; - final ColorSpaceSubset subset2 = new ColorSpaceSubset(100, 8); - subset2.rgb = 0x102040; - assertTrue(ColorSpaceSubset.RGB_COMPARATOR.compare(subset1, subset2) < 0); - assertTrue(ColorSpaceSubset.RGB_COMPARATOR.compare(subset2, subset1) > 0); - assertEquals(0, ColorSpaceSubset.RGB_COMPARATOR.compare(subset1, subset1)); - } - - @Test - public void testDump() { - // Just verify it doesn't throw anything - final ColorSpaceSubset subset = new ColorSpaceSubset(100, 8); - subset.rgb = 0x123456; - subset.dump("Test"); - subset.dumpJustRgb("TestJustRgb"); - } } diff --git a/src/test/java/org/apache/commons/imaging/palette/DitheringTest.java b/src/test/java/org/apache/commons/imaging/palette/DitheringTest.java index 020c3464..b95beeda 100644 --- a/src/test/java/org/apache/commons/imaging/palette/DitheringTest.java +++ b/src/test/java/org/apache/commons/imaging/palette/DitheringTest.java @@ -32,34 +32,45 @@ import org.junit.jupiter.api.Test; class DitheringTest { @Test - void testApplyFloydSteinbergDitheringWithNonNullOne() throws ImagingException { - final BufferedImage bufferedImage = new BufferedImage(8, 8, 8); - bufferedImage.setRGB(2, 2, 2); - final List<ColorSpaceSubset> linkedList = new LinkedList<>(); - final ColorSpaceSubset colorSpaceSubset = new ColorSpaceSubset(-234, -352); - colorSpaceSubset.rgb = 8; - linkedList.add(colorSpaceSubset); - final QuantizedPalette quantizedPalette = new QuantizedPalette(linkedList, 8); - Dithering.applyFloydSteinbergDithering(bufferedImage, quantizedPalette); - assertEquals(-16777208, bufferedImage.getRGB(0, 0)); - assertEquals(-16777208, bufferedImage.getRGB(1, 1)); - assertEquals(-16777208, bufferedImage.getRGB(2, 1)); - assertEquals(-16777208, bufferedImage.getRGB(2, 2)); - } + void testApplyFloydSteinbergDitheringAlpha() throws ImagingException { + // Image with alpha channel + final BufferedImage image = new BufferedImage(2, 1, BufferedImage.TYPE_INT_ARGB); + // (0,0) is semi-transparent red + image.setRGB(0, 0, 0x80ff0000); + // (1,0) is fully transparent + image.setRGB(1, 0, 0x00000000); + final Palette palette = new Palette() { - @Test - void testApplyFloydSteinbergDitheringWithNonNullTwo() throws ImagingException { - final BufferedImage bufferedImage = new BufferedImage(3, 3, 3); - bufferedImage.setRGB(1, 2, 4); - final List<ColorSpaceSubset> linkedList = new LinkedList<>(); - final ColorSpaceSubset colorSpaceSubset = new ColorSpaceSubset(-234, -352); - linkedList.add(colorSpaceSubset); - final QuantizedPalette quantizedPalette = new QuantizedPalette(linkedList, 3); - Dithering.applyFloydSteinbergDithering(bufferedImage, quantizedPalette); - assertEquals(-1, bufferedImage.getRGB(0, 0)); - assertEquals(-1, bufferedImage.getRGB(1, 1)); - assertEquals(-1, bufferedImage.getRGB(2, 1)); - assertEquals(-1, bufferedImage.getRGB(2, 2)); + @Override + public int getEntry(final int index) { + return index == 0 ? 0xff000000 : 0x00ffffff; // Black or Transparent White + } + + @Override + public int getPaletteIndex(final int rgb) { + final int alpha = rgb >> 24 & 0xff; + return alpha < 128 ? 1 : 0; + } + + @Override + public int length() { + return 2; + } + }; + Dithering.applyFloydSteinbergDithering(image, palette); + // (0,0) alpha is 128 -> index 0 (Black: 0xff000000) + // (1,0) should receive error from (0,0) + // Initial (0,0) ARGB: 128, 255, 0, 0 + // Palette (0,0) ARGB: 255, 0, 0, 0 + // Error A: 128-255 = -127 + // Error R: 255-0 = 255 + // (1,0) was 0, 0, 0, 0 + // New (1,0) A: 0 + (-127 * 7/16) = -55 -> clipped to 0 + // New (1,0) R: 0 + (255 * 7/16) = 111 + // New (1,0) ARGB approx: 0, 111, 0, 0 + // palette.getPaletteIndex(0, 111, 0, 0) -> alpha is 0 < 128 -> index 1 (Transparent White: 0x00ffffff) + assertEquals(0xff000000, image.getRGB(0, 0)); + assertEquals(0x00ffffff, image.getRGB(1, 0)); } @Test @@ -105,61 +116,6 @@ class DitheringTest { assertEquals(0x000000, image.getRGB(1, 0) & 0xffffff); } - @Test - void testApplyFloydSteinbergDitheringSmallImage() throws ImagingException { - // 1x1 image - final BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); - image.setRGB(0, 0, 0x123456); - // SimplePalette uses exact match. - // We must include the alpha channel because TYPE_INT_RGB uses 0xff as alpha in getRGB. - final Palette palette = new SimplePalette(new int[] { 0xff123456 }); - // Should not throw exception - Dithering.applyFloydSteinbergDithering(image, palette); - assertEquals(0x123456, image.getRGB(0, 0) & 0xffffff); - } - - @Test - void testApplyFloydSteinbergDitheringAlpha() throws ImagingException { - // Image with alpha channel - final BufferedImage image = new BufferedImage(2, 1, BufferedImage.TYPE_INT_ARGB); - // (0,0) is semi-transparent red - image.setRGB(0, 0, 0x80ff0000); - // (1,0) is fully transparent - image.setRGB(1, 0, 0x00000000); - final Palette palette = new Palette() { - - @Override - public int getEntry(final int index) { - return index == 0 ? 0xff000000 : 0x00ffffff; // Black or Transparent White - } - - @Override - public int getPaletteIndex(final int rgb) { - final int alpha = rgb >> 24 & 0xff; - return alpha < 128 ? 1 : 0; - } - - @Override - public int length() { - return 2; - } - }; - Dithering.applyFloydSteinbergDithering(image, palette); - // (0,0) alpha is 128 -> index 0 (Black: 0xff000000) - // (1,0) should receive error from (0,0) - // Initial (0,0) ARGB: 128, 255, 0, 0 - // Palette (0,0) ARGB: 255, 0, 0, 0 - // Error A: 128-255 = -127 - // Error R: 255-0 = 255 - // (1,0) was 0, 0, 0, 0 - // New (1,0) A: 0 + (-127 * 7/16) = -55 -> clipped to 0 - // New (1,0) R: 0 + (255 * 7/16) = 111 - // New (1,0) ARGB approx: 0, 111, 0, 0 - // palette.getPaletteIndex(0, 111, 0, 0) -> alpha is 0 < 128 -> index 1 (Transparent White: 0x00ffffff) - assertEquals(0xff000000, image.getRGB(0, 0)); - assertEquals(0x00ffffff, image.getRGB(1, 0)); - } - @Test void testApplyFloydSteinbergDitheringClipping() throws ImagingException { final BufferedImage image = new BufferedImage(2, 1, BufferedImage.TYPE_INT_RGB); @@ -201,4 +157,48 @@ class DitheringTest { assertEquals(0x000000, image.getRGB(0, 0) & 0xffffff); assertEquals(0x000000, image.getRGB(1, 0) & 0xffffff); } + + @Test + void testApplyFloydSteinbergDitheringSmallImage() throws ImagingException { + // 1x1 image + final BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); + image.setRGB(0, 0, 0x123456); + // SimplePalette uses exact match. + // We must include the alpha channel because TYPE_INT_RGB uses 0xff as alpha in getRGB. + final Palette palette = new SimplePalette(new int[] { 0xff123456 }); + // Should not throw exception + Dithering.applyFloydSteinbergDithering(image, palette); + assertEquals(0x123456, image.getRGB(0, 0) & 0xffffff); + } + + @Test + void testApplyFloydSteinbergDitheringWithNonNullOne() throws ImagingException { + final BufferedImage bufferedImage = new BufferedImage(8, 8, 8); + bufferedImage.setRGB(2, 2, 2); + final List<ColorSpaceSubset> linkedList = new LinkedList<>(); + final ColorSpaceSubset colorSpaceSubset = new ColorSpaceSubset(-234, -352); + colorSpaceSubset.rgb = 8; + linkedList.add(colorSpaceSubset); + final QuantizedPalette quantizedPalette = new QuantizedPalette(linkedList, 8); + Dithering.applyFloydSteinbergDithering(bufferedImage, quantizedPalette); + assertEquals(-16777208, bufferedImage.getRGB(0, 0)); + assertEquals(-16777208, bufferedImage.getRGB(1, 1)); + assertEquals(-16777208, bufferedImage.getRGB(2, 1)); + assertEquals(-16777208, bufferedImage.getRGB(2, 2)); + } + + @Test + void testApplyFloydSteinbergDitheringWithNonNullTwo() throws ImagingException { + final BufferedImage bufferedImage = new BufferedImage(3, 3, 3); + bufferedImage.setRGB(1, 2, 4); + final List<ColorSpaceSubset> linkedList = new LinkedList<>(); + final ColorSpaceSubset colorSpaceSubset = new ColorSpaceSubset(-234, -352); + linkedList.add(colorSpaceSubset); + final QuantizedPalette quantizedPalette = new QuantizedPalette(linkedList, 3); + Dithering.applyFloydSteinbergDithering(bufferedImage, quantizedPalette); + assertEquals(-1, bufferedImage.getRGB(0, 0)); + assertEquals(-1, bufferedImage.getRGB(1, 1)); + assertEquals(-1, bufferedImage.getRGB(2, 1)); + assertEquals(-1, bufferedImage.getRGB(2, 2)); + } } diff --git a/src/test/java/org/apache/commons/imaging/palette/LongestAxisMedianCutTest.java b/src/test/java/org/apache/commons/imaging/palette/LongestAxisMedianCutTest.java index a83789f7..4283e34c 100644 --- a/src/test/java/org/apache/commons/imaging/palette/LongestAxisMedianCutTest.java +++ b/src/test/java/org/apache/commons/imaging/palette/LongestAxisMedianCutTest.java @@ -48,67 +48,6 @@ public class LongestAxisMedianCutTest { assertEquals(2, colorGroups.size()); } - @Test - public void testPerformNextMedianCutWithIgnoreAlpha() throws ImagingException { - final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); - final List<ColorGroup> colorGroups = new ArrayList<>(); - final List<ColorCount> colorCounts = new ArrayList<>(); - // Alpha variation: 0 to 200 - // Red variation: 0 to 100 - for (int i = 0; i <= 100; i++) { - // ARGB: i*2, i, 0, 0 - colorCounts.add(new ColorCount(i * 2 << 24 | i << 16)); - } - final ColorGroup root = new ColorGroup(colorCounts, true); - colorGroups.add(root); - // If ignoreAlpha is true, Red should be the longest axis (100) even though Alpha has 200 range - medianCut.performNextMedianCut(colorGroups, true); - assertEquals(2, colorGroups.size()); - final ColorGroup cg1 = colorGroups.get(0); - final ColorGroup cg2 = colorGroups.get(1); - // Both should have same green and blue (0) - assertEquals(0, cg1.minGreen); - assertEquals(0, cg1.maxGreen); - assertEquals(0, cg2.minGreen); - assertEquals(0, cg2.maxGreen); - } - - @Test - public void testPerformNextMedianCutSingleColor() throws ImagingException { - final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); - final List<ColorGroup> colorGroups = new ArrayList<>(); - final List<ColorCount> colorCounts = new ArrayList<>(); - colorCounts.add(new ColorCount(0xff000000)); - final ColorGroup root = new ColorGroup(colorCounts, false); - colorGroups.add(root); - // maxDiff is 0, should return false - final boolean result = medianCut.performNextMedianCut(colorGroups, false); - assertFalse(result); - assertEquals(1, colorGroups.size()); - } - - @Test - public void testPerformNextMedianCutDetailed() throws ImagingException { - final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); - final List<ColorGroup> colorGroups = new ArrayList<>(); - final List<ColorCount> colorCounts = new ArrayList<>(); - // Create 10 pixels of color 1 and 10 pixels of color 2 - final ColorCount cc1 = new ColorCount(0xff000000); // Black - cc1.count = 10; - colorCounts.add(cc1); - final ColorCount cc2 = new ColorCount(0xffff0000); // Red - cc2.count = 10; - colorCounts.add(cc2); - final ColorGroup root = new ColorGroup(colorCounts, false); - colorGroups.add(root); - final boolean result = medianCut.performNextMedianCut(colorGroups, false); - assertTrue(result); - assertEquals(2, colorGroups.size()); - // Each group should have 10 points - assertEquals(10, colorGroups.get(0).totalPoints); - assertEquals(10, colorGroups.get(1).totalPoints); - } - @Test public void testPerformNextMedianCutAllAxes() throws ImagingException { final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); @@ -147,34 +86,6 @@ public class LongestAxisMedianCutTest { } } - @Test - public void testPerformNextMedianCutMultipleGroups() throws ImagingException { - final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); - final List<ColorGroup> colorGroups = new ArrayList<>(); - // Group 1: Range 10 (Red) - final List<ColorCount> counts1 = new ArrayList<>(); - counts1.add(new ColorCount(0xff000000)); - counts1.add(new ColorCount(0xff0a0000)); - colorGroups.add(new ColorGroup(counts1, false)); - // Group 2: Range 20 (Red) - final List<ColorCount> counts2 = new ArrayList<>(); - counts2.add(new ColorCount(0xff000000)); - counts2.add(new ColorCount(0xff140000)); - colorGroups.add(new ColorGroup(counts2, false)); - // Should cut Group 2 because it has larger maxDiff (20 > 10) - assertTrue(medianCut.performNextMedianCut(colorGroups, false)); - assertEquals(3, colorGroups.size()); - // Check that one group still has range 10, and two new groups are created from the range 20 group - boolean foundRange10 = false; - for (final ColorGroup cg : colorGroups) { - if (cg.maxDiff == 10) { - foundRange10 = true; - break; - } - } - assertTrue(foundRange10); - } - @Test public void testPerformNextMedianCutAmbiguousMedian() throws ImagingException { final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); @@ -218,6 +129,56 @@ public class LongestAxisMedianCutTest { assertTrue(found3); } + @Test + public void testPerformNextMedianCutDetailed() throws ImagingException { + final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); + final List<ColorGroup> colorGroups = new ArrayList<>(); + final List<ColorCount> colorCounts = new ArrayList<>(); + // Create 10 pixels of color 1 and 10 pixels of color 2 + final ColorCount cc1 = new ColorCount(0xff000000); // Black + cc1.count = 10; + colorCounts.add(cc1); + final ColorCount cc2 = new ColorCount(0xffff0000); // Red + cc2.count = 10; + colorCounts.add(cc2); + final ColorGroup root = new ColorGroup(colorCounts, false); + colorGroups.add(root); + final boolean result = medianCut.performNextMedianCut(colorGroups, false); + assertTrue(result); + assertEquals(2, colorGroups.size()); + // Each group should have 10 points + assertEquals(10, colorGroups.get(0).totalPoints); + assertEquals(10, colorGroups.get(1).totalPoints); + } + + @Test + public void testPerformNextMedianCutMultipleGroups() throws ImagingException { + final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); + final List<ColorGroup> colorGroups = new ArrayList<>(); + // Group 1: Range 10 (Red) + final List<ColorCount> counts1 = new ArrayList<>(); + counts1.add(new ColorCount(0xff000000)); + counts1.add(new ColorCount(0xff0a0000)); + colorGroups.add(new ColorGroup(counts1, false)); + // Group 2: Range 20 (Red) + final List<ColorCount> counts2 = new ArrayList<>(); + counts2.add(new ColorCount(0xff000000)); + counts2.add(new ColorCount(0xff140000)); + colorGroups.add(new ColorGroup(counts2, false)); + // Should cut Group 2 because it has larger maxDiff (20 > 10) + assertTrue(medianCut.performNextMedianCut(colorGroups, false)); + assertEquals(3, colorGroups.size()); + // Check that one group still has range 10, and two new groups are created from the range 20 group + boolean foundRange10 = false; + for (final ColorGroup cg : colorGroups) { + if (cg.maxDiff == 10) { + foundRange10 = true; + break; + } + } + assertTrue(foundRange10); + } + @Test public void testPerformNextMedianCutOldDiffSmaller() throws ImagingException { final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); @@ -257,4 +218,43 @@ public class LongestAxisMedianCutTest { assertTrue(found4); assertTrue(found6); } + + @Test + public void testPerformNextMedianCutSingleColor() throws ImagingException { + final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); + final List<ColorGroup> colorGroups = new ArrayList<>(); + final List<ColorCount> colorCounts = new ArrayList<>(); + colorCounts.add(new ColorCount(0xff000000)); + final ColorGroup root = new ColorGroup(colorCounts, false); + colorGroups.add(root); + // maxDiff is 0, should return false + final boolean result = medianCut.performNextMedianCut(colorGroups, false); + assertFalse(result); + assertEquals(1, colorGroups.size()); + } + + @Test + public void testPerformNextMedianCutWithIgnoreAlpha() throws ImagingException { + final LongestAxisMedianCut medianCut = new LongestAxisMedianCut(); + final List<ColorGroup> colorGroups = new ArrayList<>(); + final List<ColorCount> colorCounts = new ArrayList<>(); + // Alpha variation: 0 to 200 + // Red variation: 0 to 100 + for (int i = 0; i <= 100; i++) { + // ARGB: i*2, i, 0, 0 + colorCounts.add(new ColorCount(i * 2 << 24 | i << 16)); + } + final ColorGroup root = new ColorGroup(colorCounts, true); + colorGroups.add(root); + // If ignoreAlpha is true, Red should be the longest axis (100) even though Alpha has 200 range + medianCut.performNextMedianCut(colorGroups, true); + assertEquals(2, colorGroups.size()); + final ColorGroup cg1 = colorGroups.get(0); + final ColorGroup cg2 = colorGroups.get(1); + // Both should have same green and blue (0) + assertEquals(0, cg1.minGreen); + assertEquals(0, cg1.maxGreen); + assertEquals(0, cg2.minGreen); + assertEquals(0, cg2.maxGreen); + } } diff --git a/src/test/java/org/apache/commons/imaging/palette/PaletteFactoryTest.java b/src/test/java/org/apache/commons/imaging/palette/PaletteFactoryTest.java index 30cc946d..07a46f90 100644 --- a/src/test/java/org/apache/commons/imaging/palette/PaletteFactoryTest.java +++ b/src/test/java/org/apache/commons/imaging/palette/PaletteFactoryTest.java @@ -29,6 +29,39 @@ import org.junit.jupiter.api.Test; public class PaletteFactoryTest { + @Test + public void testCountTransparentColors() { + final PaletteFactory factory = new PaletteFactory(); + final BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB); + // All transparent, all same color (0x00000000) + assertEquals(1, factory.countTransparentColors(image)); + for (int x = 0; x < 10; x++) { + for (int y = 0; y < 10; y++) { + image.setRGB(x, y, 0x00FFFFFF & (x << 16 | y << 8)); + } + } + // All have alpha 0, but different RGB. + // first = 0x00000000 (at 0,0). next is 0x00000800 (at 0,1). + // Since 0x00000000 != 0x00000800, it should return 2. + assertEquals(2, factory.countTransparentColors(image)); + // Mixed alpha: all opaque + for (int x = 0; x < 10; x++) { + for (int y = 0; y < 10; y++) { + image.setRGB(x, y, 0xFFFFFFFF); + } + } + assertEquals(0, factory.countTransparentColors(image)); + // One pixel transparent + image.setRGB(0, 0, 0x00FFFFFF); + assertEquals(1, factory.countTransparentColors(image)); + final int[] rgbs = { 0x00112233, 0x00112233, 0xFF112233, 0x00445566 }; + assertEquals(2, factory.countTrasparentColors(rgbs)); // 0x00112233 and 0x00445566 are different + final int[] rgbs1 = { 0x00112233, 0x00112233, 0xFF112233 }; + assertEquals(1, factory.countTrasparentColors(rgbs1)); + final int[] rgbs0 = { 0xFF112233, 0xFF445566 }; + assertEquals(0, factory.countTrasparentColors(rgbs0)); + } + @Test public void testHasTransparency() { final PaletteFactory factory = new PaletteFactory(); @@ -68,36 +101,14 @@ public class PaletteFactoryTest { } @Test - public void testCountTransparentColors() { + public void testMakeExactRgbPaletteFancy() { final PaletteFactory factory = new PaletteFactory(); - final BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB); - // All transparent, all same color (0x00000000) - assertEquals(1, factory.countTransparentColors(image)); - for (int x = 0; x < 10; x++) { - for (int y = 0; y < 10; y++) { - image.setRGB(x, y, 0x00FFFFFF & (x << 16 | y << 8)); - } - } - // All have alpha 0, but different RGB. - // first = 0x00000000 (at 0,0). next is 0x00000800 (at 0,1). - // Since 0x00000000 != 0x00000800, it should return 2. - assertEquals(2, factory.countTransparentColors(image)); - // Mixed alpha: all opaque - for (int x = 0; x < 10; x++) { - for (int y = 0; y < 10; y++) { - image.setRGB(x, y, 0xFFFFFFFF); - } - } - assertEquals(0, factory.countTransparentColors(image)); - // One pixel transparent - image.setRGB(0, 0, 0x00FFFFFF); - assertEquals(1, factory.countTransparentColors(image)); - final int[] rgbs = { 0x00112233, 0x00112233, 0xFF112233, 0x00445566 }; - assertEquals(2, factory.countTrasparentColors(rgbs)); // 0x00112233 and 0x00445566 are different - final int[] rgbs1 = { 0x00112233, 0x00112233, 0xFF112233 }; - assertEquals(1, factory.countTrasparentColors(rgbs1)); - final int[] rgbs0 = { 0xFF112233, 0xFF445566 }; - assertEquals(0, factory.countTrasparentColors(rgbs0)); + final BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); + image.setRGB(0, 0, 0xFF0000); + image.setRGB(1, 1, 0x00FF00); + final Palette palette = factory.makeExactRgbPaletteFancy(image); + assertNotNull(palette); + assertEquals(3, palette.length()); // Black + 2 colors } @Test @@ -113,14 +124,15 @@ public class PaletteFactoryTest { } @Test - public void testMakeExactRgbPaletteFancy() { + public void testMakeQuantizedRgbaPalette() throws ImagingException { final PaletteFactory factory = new PaletteFactory(); - final BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); - image.setRGB(0, 0, 0xFF0000); - image.setRGB(1, 1, 0x00FF00); - final Palette palette = factory.makeExactRgbPaletteFancy(image); + final BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB); + for (int i = 0; i < 100; i++) { + image.setRGB(i % 10, i / 10, i << 24 | i << 16 | i << 8 | i); + } + final Palette palette = factory.makeQuantizedRgbaPalette(image, true, 10); assertNotNull(palette); - assertEquals(3, palette.length()); // Black + 2 colors + assertTrue(palette.length() <= 10); } @Test @@ -134,16 +146,4 @@ public class PaletteFactoryTest { assertNotNull(palette); assertTrue(palette.length() <= 10); } - - @Test - public void testMakeQuantizedRgbaPalette() throws ImagingException { - final PaletteFactory factory = new PaletteFactory(); - final BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB); - for (int i = 0; i < 100; i++) { - image.setRGB(i % 10, i / 10, i << 24 | i << 16 | i << 8 | i); - } - final Palette palette = factory.makeQuantizedRgbaPalette(image, true, 10); - assertNotNull(palette); - assertTrue(palette.length() <= 10); - } }
