From dede5eabb58b3fd0aeebdf21817aafe5f75afb9e Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Thu, 19 Sep 2024 19:03:33 +0100 Subject: [PATCH] Use Enum DDSType instead of int values. Pass imageIndex into mipmap. --- .../imageio/plugins/dds/DDSImageReader.java | 5 +- .../imageio/plugins/dds/DDSReader.java | 50 +++++++------------ .../imageio/plugins/dds/DDSType.java | 41 +++++++++++++++ 3 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSType.java diff --git a/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSImageReader.java b/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSImageReader.java index 9e29da5a..b29886a6 100644 --- a/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSImageReader.java +++ b/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSImageReader.java @@ -49,9 +49,6 @@ public final class DDSImageReader extends ImageReaderBase { checkBounds(imageIndex); readHeader(); - // TODO change based on format DXT1 4bpp / DXT1-nonalpha - - return ImageTypeSpecifiers.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB); } @@ -68,7 +65,7 @@ public final class DDSImageReader extends ImageReaderBase { processImageStarted(imageIndex); DDSReader dds = new DDSReader(header); - int[] pixels = dds.read(imageInput, 0); + int[] pixels = dds.read(imageInput, imageIndex); int width = getWidth(imageIndex); int height = getHeight(imageIndex); diff --git a/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSReader.java b/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSReader.java index 9d45ef47..4450aa12 100644 --- a/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSReader.java +++ b/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSReader.java @@ -41,7 +41,7 @@ public final class DDSReader { } // type - int type = getType(); + DDSType type = getType(); // length int len = getLength(type, width, height); @@ -99,12 +99,13 @@ public final class DDSReader { } } - private int getType() throws IIOException { + private DDSType getType() throws IIOException { int flags = header.getPixelFormatFlags(); if ((flags & 0x04) != 0) { // DXT - return header.getFourCC(); + int type = header.getFourCC(); + return DDSType.parse(type); } else if ((flags & 0x40) != 0) { // RGB int bitCount = header.getBitCount(); @@ -115,42 +116,42 @@ public final class DDSReader { if (bitCount == 16) { if (redMask == A1R5G5B5_MASKS[0] && greenMask == A1R5G5B5_MASKS[1] && blueMask == A1R5G5B5_MASKS[2] && alphaMask == A1R5G5B5_MASKS[3]) { // A1R5G5B5 - return A1R5G5B5; + return DDSType.A1R5G5B5; } else if (redMask == X1R5G5B5_MASKS[0] && greenMask == X1R5G5B5_MASKS[1] && blueMask == X1R5G5B5_MASKS[2] && alphaMask == X1R5G5B5_MASKS[3]) { // X1R5G5B5 - return X1R5G5B5; + return DDSType.X1R5G5B5; } else if (redMask == A4R4G4B4_MASKS[0] && greenMask == A4R4G4B4_MASKS[1] && blueMask == A4R4G4B4_MASKS[2] && alphaMask == A4R4G4B4_MASKS[3]) { // A4R4G4B4 - return A4R4G4B4; + return DDSType.A4R4G4B4; } else if (redMask == X4R4G4B4_MASKS[0] && greenMask == X4R4G4B4_MASKS[1] && blueMask == X4R4G4B4_MASKS[2] && alphaMask == X4R4G4B4_MASKS[3]) { // X4R4G4B4 - return X4R4G4B4; + return DDSType.X4R4G4B4; } else if (redMask == R5G6B5_MASKS[0] && greenMask == R5G6B5_MASKS[1] && blueMask == R5G6B5_MASKS[2] && alphaMask == R5G6B5_MASKS[3]) { // R5G6B5 - return R5G6B5; + return DDSType.R5G6B5; } else { throw new IIOException("Unsupported 16bit RGB image."); } } else if (bitCount == 24) { if (redMask == R8G8B8_MASKS[0] && greenMask == R8G8B8_MASKS[1] && blueMask == R8G8B8_MASKS[2] && alphaMask == R8G8B8_MASKS[3]) { // R8G8B8 - return R8G8B8; + return DDSType.R8G8B8; } else { throw new IIOException("Unsupported 24bit RGB image."); } } else if (bitCount == 32) { if (redMask == A8B8G8R8_MASKS[0] && greenMask == A8B8G8R8_MASKS[1] && blueMask == A8B8G8R8_MASKS[2] && alphaMask == A8B8G8R8_MASKS[3]) { // A8B8G8R8 - return A8B8G8R8; + return DDSType.A8B8G8R8; } else if (redMask == X8B8G8R8_MASKS[0] && greenMask == X8B8G8R8_MASKS[1] && blueMask == X8B8G8R8_MASKS[2] && alphaMask == X8B8G8R8_MASKS[3]) { // X8B8G8R8 - return X8B8G8R8; + return DDSType.X8B8G8R8; } else if (redMask == A8R8G8B8_MASKS[0] && greenMask == A8R8G8B8_MASKS[1] && blueMask == A8R8G8B8_MASKS[2] && alphaMask == A8R8G8B8_MASKS[3]) { // A8R8G8B8 - return A8R8G8B8; + return DDSType.A8R8G8B8; } else if (redMask == X8R8G8B8_MASKS[0] && greenMask == X8R8G8B8_MASKS[1] && blueMask == X8R8G8B8_MASKS[2] && alphaMask == X8R8G8B8_MASKS[3]) { // X8R8G8B8 - return X8R8G8B8; + return DDSType.X8R8G8B8; } else { throw new IIOException("Unsupported 32bit RGB image."); } @@ -162,7 +163,7 @@ public final class DDSReader { } } - private static int getLength(int type, int width, int height) throws IIOException { + private static int getLength(DDSType type, int width, int height) throws IIOException { switch (type) { case DXT1: return 8 * ((width + 3) / 4) * ((height + 3) / 4); @@ -181,9 +182,9 @@ public final class DDSReader { case X8B8G8R8: case A8R8G8B8: case X8R8G8B8: - return (type & 0xFF) * width * height; + return (type.value() & 0xFF) * width * height; default: - throw new IIOException("Unknown type: " + Integer.toHexString(type)); + throw new IIOException("Unknown type: " + Integer.toHexString(type.value())); } } @@ -539,23 +540,6 @@ public final class DDSReader { return 0; } - // Image Type - private static final int DXT1 = (0x31545844); - private static final int DXT2 = (0x32545844); - private static final int DXT3 = (0x33545844); - private static final int DXT4 = (0x34545844); - private static final int DXT5 = (0x35545844); - private static final int A1R5G5B5 = ((1 << 16) | 2); - private static final int X1R5G5B5 = ((2 << 16) | 2); - private static final int A4R4G4B4 = ((3 << 16) | 2); - private static final int X4R4G4B4 = ((4 << 16) | 2); - private static final int R5G6B5 = ((5 << 16) | 2); - private static final int R8G8B8 = ((1 << 16) | 3); - private static final int A8B8G8R8 = ((1 << 16) | 4); - private static final int X8B8G8R8 = ((2 << 16) | 4); - private static final int A8R8G8B8 = ((3 << 16) | 4); - private static final int X8R8G8B8 = ((4 << 16) | 4); - // RGBA Masks private static final int[] A1R5G5B5_MASKS = {0x7C00, 0x03E0, 0x001F, 0x8000}; private static final int[] X1R5G5B5_MASKS = {0x7C00, 0x03E0, 0x001F, 0x0000}; diff --git a/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSType.java b/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSType.java new file mode 100644 index 00000000..dbc138aa --- /dev/null +++ b/imageio/imageio-dds/src/main/java/com/twelvemonkeys/imageio/plugins/dds/DDSType.java @@ -0,0 +1,41 @@ +package com.twelvemonkeys.imageio.plugins.dds; + +import javax.imageio.IIOException; + +public enum DDSType { + + DXT1(0x31545844), + DXT2(0x32545844), + DXT3(0x33545844), + DXT4(0x34545844), + DXT5(0x35545844), + A1R5G5B5((1 << 16) | 2), + X1R5G5B5((2 << 16) | 2), + A4R4G4B4((3 << 16) | 2), + X4R4G4B4((4 << 16) | 2), + R5G6B5((5 << 16) | 2), + R8G8B8((1 << 16) | 3), + A8B8G8R8((1 << 16) | 4), + X8B8G8R8((2 << 16) | 4), + A8R8G8B8((3 << 16) | 4), + X8R8G8B8((4 << 16) | 4); + + private final int value; + + DDSType(int value) { + this.value = value; + } + + public int value() { + return value; + } + + public static DDSType parse(int type) throws IIOException { + for (DDSType t : DDSType.values()) { + if (type == t.value()) { + return t; + } + } + throw new IIOException("Unknown type: " + Integer.toHexString(type)); + } +}