Push ImageInputStream into DDSReader

Unable to determine buffer length (so as a hack I over allocate buffer and read)

```
byte[] buffer = new byte[width * height * 4];
int len = imageInput.read(buffer);
```

Added test files for all supported formats.
This commit is contained in:
Paul Allen
2024-09-17 16:37:11 +01:00
parent 120d6eba54
commit 0cd056ac54
35 changed files with 118 additions and 69 deletions

1
.gitignore vendored
View File

@@ -16,3 +16,4 @@ profiles.xml
Thumbs.db
.DS_Store
/.metadata/
.run/

View File

@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="DDSImageReader" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="com.twelvemonkeys.imageio.plugins.dds.DDSImageReader" />
<module name="imageio-dds" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.twelvemonkeys.imageio.plugins.dds.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View File

@@ -25,66 +25,61 @@ public final class DDSHeader {
public static DDSHeader read(final ImageInputStream imageInput) throws IOException {
DDSHeader header = new DDSHeader();
imageInput.mark();
imageInput.setByteOrder(ByteOrder.LITTLE_ENDIAN);
try {
imageInput.setByteOrder(ByteOrder.LITTLE_ENDIAN);
// Read MAGIC bytes [0,3]
byte[] magic = new byte[DDS.MAGIC.length];
imageInput.readFully(magic);
if (!Arrays.equals(DDS.MAGIC, magic)) {
throw new IIOException("Unsupported MAGIC bytes.");
}
// DDS_HEADER structure
// https://learn.microsoft.com/en-us/windows/win32/direct3ddds/dds-header
int dwSize = imageInput.readInt(); // [4,7]
if (dwSize != DDS.HEADER_SIZE) {
throw new IIOException("Invalid header size: " + dwSize);
}
// Verify flags
header.flags = imageInput.readInt(); // [8,11]
if (header.getFlag(DDS.FLAG_CAPS
& DDS.FLAG_HEIGHT
& DDS.FLAG_WIDTH
& DDS.FLAG_PIXELFORMAT)) {
throw new IIOException("Required DDS Flag missing in header: " + Integer.toHexString(header.flags));
}
// Read Height & Width
header.height = imageInput.readInt(); // [12,15]
header.width = imageInput.readInt(); // [16,19]
int dwPitchOrLinearSize = imageInput.readInt(); // [20,23]
int dwDepth = imageInput.readInt(); // [24,27]
header.mipmap = imageInput.readInt(); // [28,31]
byte[] dwReserved1 = new byte[11 * 4]; // [32,75]
imageInput.readFully(dwReserved1);
// DDS_PIXELFORMAT structure
int px_dwSize = imageInput.readInt(); // [76,79]
header.pixelFormatFlags = imageInput.readInt(); // [80,83]
header.fourCC = imageInput.readInt(); // [84,87]
header.bitCount = imageInput.readInt(); // [88,91]
header.redMask = imageInput.readInt(); // [92,95]
header.greenMask = imageInput.readInt(); // [96,99]
header.blueMask = imageInput.readInt(); // [100,103]
header.alphaMask = imageInput.readInt(); // [104,107]
int dwCaps = imageInput.readInt();
int dwCaps2 = imageInput.readInt();
int dwCaps3 = imageInput.readInt();
int dwCaps4 = imageInput.readInt();
int dwReserved2 = imageInput.readInt();
} finally {
imageInput.reset();
// Read MAGIC bytes [0,3]
byte[] magic = new byte[DDS.MAGIC.length];
imageInput.readFully(magic);
if (!Arrays.equals(DDS.MAGIC, magic)) {
throw new IIOException("Unsupported MAGIC bytes.");
}
// DDS_HEADER structure
// https://learn.microsoft.com/en-us/windows/win32/direct3ddds/dds-header
int dwSize = imageInput.readInt(); // [4,7]
if (dwSize != DDS.HEADER_SIZE) {
throw new IIOException("Invalid header size: " + dwSize);
}
// Verify flags
header.flags = imageInput.readInt(); // [8,11]
if (header.getFlag(DDS.FLAG_CAPS
& DDS.FLAG_HEIGHT
& DDS.FLAG_WIDTH
& DDS.FLAG_PIXELFORMAT)) {
throw new IIOException("Required DDS Flag missing in header: " + Integer.toHexString(header.flags));
}
// Read Height & Width
header.height = imageInput.readInt(); // [12,15]
header.width = imageInput.readInt(); // [16,19]
int dwPitchOrLinearSize = imageInput.readInt(); // [20,23]
int dwDepth = imageInput.readInt(); // [24,27]
header.mipmap = imageInput.readInt(); // [28,31]
byte[] dwReserved1 = new byte[11 * 4]; // [32,75]
imageInput.readFully(dwReserved1);
// DDS_PIXELFORMAT structure
int px_dwSize = imageInput.readInt(); // [76,79]
header.pixelFormatFlags = imageInput.readInt(); // [80,83]
header.fourCC = imageInput.readInt(); // [84,87]
header.bitCount = imageInput.readInt(); // [88,91]
header.redMask = imageInput.readInt(); // [92,95]
header.greenMask = imageInput.readInt(); // [96,99]
header.blueMask = imageInput.readInt(); // [100,103]
header.alphaMask = imageInput.readInt(); // [104,107]
int dwCaps = imageInput.readInt(); // [108,111]
int dwCaps2 = imageInput.readInt(); // [112,115]
int dwCaps3 = imageInput.readInt(); // [116,119]
int dwCaps4 = imageInput.readInt(); // [120,123]
int dwReserved2 = imageInput.readInt(); // [124,127]
return header;
}

View File

@@ -10,8 +10,10 @@ import javax.imageio.spi.ImageReaderSpi;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public final class DDSImageReader extends ImageReaderBase {
@@ -68,12 +70,8 @@ public final class DDSImageReader extends ImageReaderBase {
BufferedImage destination = getDestination(param, getImageTypes(imageIndex), width, height);
// TODO
byte[] buffer = new byte[width * height * 4];
imageInput.read(buffer);
DDSReader dds = new DDSReader(header);
int[] pixels = dds.read(buffer, 0);
int[] pixels = dds.read(imageInput, 0);
destination.setRGB(0, 0, width, height, pixels, 0, width);
processImageComplete();
@@ -92,11 +90,45 @@ public final class DDSImageReader extends ImageReaderBase {
}
public static void main(final String[] args) throws IOException {
File file = new File("imageio/imageio-dds/src/test/resources/dds/stones.dxt5.dds");
//File file = new File("imageio/imageio-dds/src/test/resources/dds/dxt1-noalpha.dds");
BufferedImage image = ImageIO.read(file);
String parentDir = "imageio/imageio-dds/src/test/resources/dds";
List<File> testFiles = new ArrayList<>();
testFiles.add(new File(parentDir, "dds_A1R5G5B5.dds"));
testFiles.add(new File(parentDir, "dds_A1R5G5B5_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_A4R4G4B4.dds"));
testFiles.add(new File(parentDir, "dds_A4R4G4B4_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_A8B8G8R8.dds"));
testFiles.add(new File(parentDir, "dds_A8B8G8R8_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_A8R8G8B8.dds"));
testFiles.add(new File(parentDir, "dds_A8R8G8B8_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_DXT1.dds"));
testFiles.add(new File(parentDir, "dds_DXT1_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_DXT2.dds"));
testFiles.add(new File(parentDir, "dds_DXT2_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_DXT3.dds"));
testFiles.add(new File(parentDir, "dds_DXT3_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_DXT4.dds"));
testFiles.add(new File(parentDir, "dds_DXT4_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_DXT5.dds"));
testFiles.add(new File(parentDir, "dds_DXT5_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_R5G6B5.dds"));
testFiles.add(new File(parentDir, "dds_R5G6B5_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_R8G8B8.dds"));
testFiles.add(new File(parentDir, "dds_R8G8B8_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_X1R5G5B5.dds"));
testFiles.add(new File(parentDir, "dds_X1R5G5B5_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_X4R4G4B4.dds"));
testFiles.add(new File(parentDir, "dds_X4R4G4B4_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_X8B8G8R8.dds"));
testFiles.add(new File(parentDir, "dds_X8B8G8R8_mipmap.dds"));
testFiles.add(new File(parentDir, "dds_X8R8G8B8.dds"));
testFiles.add(new File(parentDir, "dds_X8R8G8B8_mipmap.dds"));
for (File file : testFiles) {
BufferedImage image = ImageIO.read(file);
showIt(image, file.getName());
}
showIt(image, file.getName());
}
}

View File

@@ -14,6 +14,9 @@
package com.twelvemonkeys.imageio.plugins.dds;
import javax.imageio.stream.ImageInputStream;
import java.io.IOException;
public final class DDSReader {
public static final Order order = new Order(16, 8, 0, 24);
@@ -25,19 +28,22 @@ public final class DDSReader {
this.header = header;
}
public int[] read(byte[] buffer, int mipmapLevel) {
public int[] read(ImageInputStream imageInput, int mipmapLevel) throws IOException {
// header
int width = header.getWidth();
int height = header.getHeight();
int mipmap = header.getMipmap();
byte[] buffer = new byte[width * height * 4];
int len = imageInput.read(buffer);
// type
int type = getType(header);
if (type == 0) return null;
// offset
int offset = 128; // header size
int offset = 0; // header size
if (mipmapLevel > 0 && mipmapLevel < mipmap) {
for (int i = 0; i < mipmapLevel; i++) {
switch (type) {