Add source region and subsample support + license.txt

This commit is contained in:
Harald Kuhr 2024-09-25 17:47:57 +02:00
parent 3c01071452
commit aab7b6f7f5
7 changed files with 185 additions and 213 deletions

View File

@ -1391,6 +1391,8 @@ public abstract class ImageReaderAbstractTest<T extends ImageReader> {
public void testSetDestination() throws IOException { public void testSetDestination() throws IOException {
ImageReader reader = createReader(); ImageReader reader = createReader();
TestData data = getTestData().get(0); TestData data = getTestData().get(0);
Dimension size = data.getDimension(0);
reader.setInput(data.getInputStream()); reader.setInput(data.getInputStream());
ImageReadParam param = reader.getDefaultReadParam(); ImageReadParam param = reader.getDefaultReadParam();
@ -1398,7 +1400,7 @@ public abstract class ImageReaderAbstractTest<T extends ImageReader> {
while (types.hasNext()) { while (types.hasNext()) {
ImageTypeSpecifier type = types.next(); ImageTypeSpecifier type = types.next();
BufferedImage destination = type.createBufferedImage(50, 50); BufferedImage destination = type.createBufferedImage(size.width, size.height);
param.setDestination(destination); param.setDestination(destination);
BufferedImage result = null; BufferedImage result = null;
@ -1448,13 +1450,15 @@ public abstract class ImageReaderAbstractTest<T extends ImageReader> {
public void testSetDestinationIllegal() throws IOException { public void testSetDestinationIllegal() throws IOException {
final ImageReader reader = createReader(); final ImageReader reader = createReader();
TestData data = getTestData().get(0); TestData data = getTestData().get(0);
Dimension size = data.getDimension(0);
reader.setInput(data.getInputStream()); reader.setInput(data.getInputStream());
List<ImageTypeSpecifier> illegalTypes = createIllegalTypes(reader.getImageTypes(0)); List<ImageTypeSpecifier> illegalTypes = createIllegalTypes(reader.getImageTypes(0));
ImageReadParam param = reader.getDefaultReadParam(); ImageReadParam param = reader.getDefaultReadParam();
for (ImageTypeSpecifier illegalType : illegalTypes) { for (ImageTypeSpecifier illegalType : illegalTypes) {
BufferedImage destination = illegalType.createBufferedImage(50, 50); BufferedImage destination = illegalType.createBufferedImage(size.width, size.height);
param.setDestination(destination); param.setDestination(destination);
try { try {
@ -1755,11 +1759,12 @@ public abstract class ImageReaderAbstractTest<T extends ImageReader> {
ImageReader reader = createReader(); ImageReader reader = createReader();
for (TestData testData : getTestDataForAffineTransformOpCompatibility()) { for (TestData testData : getTestDataForAffineTransformOpCompatibility()) {
Dimension size = testData.getDimension(0);
try (ImageInputStream input = testData.getInputStream()) { try (ImageInputStream input = testData.getInputStream()) {
reader.setInput(input); reader.setInput(input);
ImageReadParam param = reader.getDefaultReadParam(); ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(new Rectangle(min(reader.getWidth(0), 64), min(reader.getHeight(0), 64))); param.setSourceRegion(new Rectangle(size.width, size.height));
BufferedImage originalImage = reader.read(0, param); BufferedImage originalImage = reader.read(0, param);

52
imageio/imageio-dds/license.txt Executable file
View File

@ -0,0 +1,52 @@
Copyright (c) 2024, Harald Kuhr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Parts of this software is based on DDSReader by Kenji Sasaki:
The MIT License (MIT)
Copyright (c) 2015 Kenji Sasaki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -23,14 +23,14 @@ final class DDSHeader {
private int blueMask; private int blueMask;
private int alphaMask; private int alphaMask;
public static DDSHeader read(final ImageInputStream imageInput) throws IOException { static DDSHeader read(final ImageInputStream imageInput) throws IOException {
DDSHeader header = new DDSHeader(); DDSHeader header = new DDSHeader();
// Read MAGIC bytes [0,3] // Read MAGIC bytes [0,3]
byte[] magic = new byte[DDS.MAGIC.length]; byte[] magic = new byte[DDS.MAGIC.length];
imageInput.readFully(magic); imageInput.readFully(magic);
if (!Arrays.equals(DDS.MAGIC, magic)) { if (!Arrays.equals(DDS.MAGIC, magic)) {
throw new IIOException(String.format("Not a DDS file. Expected DDS magic %08x, read %08x", new BigInteger(1, DDS.MAGIC), new BigInteger(1, magic))); throw new IIOException(String.format("Not a DDS file. Expected DDS magic 0x%08x', read 0x%08x", new BigInteger(DDS.MAGIC), new BigInteger(magic)));
} }
// DDS_HEADER structure // DDS_HEADER structure
@ -42,11 +42,11 @@ final class DDSHeader {
// Verify flags // Verify flags
header.flags = imageInput.readInt(); // [8,11] header.flags = imageInput.readInt(); // [8,11]
if (header.getFlag(DDS.FLAG_CAPS if (!header.getFlag(DDS.FLAG_CAPS
& DDS.FLAG_HEIGHT | DDS.FLAG_HEIGHT
& DDS.FLAG_WIDTH | DDS.FLAG_WIDTH
& DDS.FLAG_PIXELFORMAT)) { | DDS.FLAG_PIXELFORMAT)) {
throw new IIOException("Required DDS Flag missing in header: " + Integer.toHexString(header.flags)); throw new IIOException("Required DDS Flag missing in header: " + Integer.toBinaryString(header.flags));
} }
// Read Height & Width // Read Height & Width
@ -55,7 +55,9 @@ final class DDSHeader {
int dwPitchOrLinearSize = imageInput.readInt(); // [20,23] int dwPitchOrLinearSize = imageInput.readInt(); // [20,23]
int dwDepth = imageInput.readInt(); // [24,27] int dwDepth = imageInput.readInt(); // [24,27]
header.mipMapCount = imageInput.readInt(); // [28,31]
// 0 = (unused) or 1 = (1 level), but still one 'base' image
header.mipMapCount = Math.max(1, imageInput.readInt()); // [28,31]
// build dimensions list // build dimensions list
header.addDimensions(dwWidth, dwHeight); header.addDimensions(dwWidth, dwHeight);
@ -85,11 +87,11 @@ final class DDSHeader {
} }
private void addDimensions(int width, int height) { private void addDimensions(int width, int height) {
dimensions = new Dimension[getMipMapCount()]; dimensions = new Dimension[mipMapCount];
int w = width; int w = width;
int h = height; int h = height;
for (int i = 0; i < getMipMapCount(); i++) { for (int i = 0; i < mipMapCount; i++) {
dimensions[i] = new Dimension(w, h); dimensions[i] = new Dimension(w, h);
w /= 2; w /= 2;
h /= 2; h /= 2;
@ -100,50 +102,45 @@ final class DDSHeader {
return (flags & mask) != 0; return (flags & mask) != 0;
} }
public int getWidth(int imageIndex) { int getWidth(int imageIndex) {
int lim = dimensions[imageIndex].width; int lim = dimensions[imageIndex].width;
return (lim <= 0) ? 1 : lim; return (lim <= 0) ? 1 : lim;
} }
public int getHeight(int imageIndex) { int getHeight(int imageIndex) {
int lim = dimensions[imageIndex].height; int lim = dimensions[imageIndex].height;
return (lim <= 0) ? 1 : lim; return (lim <= 0) ? 1 : lim;
} }
public int getMipMapCount() { int getMipMapCount() {
// 0 = (unused) or 1 = (1 level), but still only one 'base' image return mipMapCount;
return (mipMapCount == 0) ? 1 : mipMapCount;
} }
public int getAlphaMask() { int getBitCount() {
return alphaMask;
}
public int getBitCount() {
return bitCount; return bitCount;
} }
public int getBlueMask() { int getFourCC() {
return blueMask;
}
public int getFlags() {
return flags;
}
public int getFourCC() {
return fourCC; return fourCC;
} }
public int getGreenMask() { int getPixelFormatFlags() {
return greenMask;
}
public int getPixelFormatFlags() {
return pixelFormatFlags; return pixelFormatFlags;
} }
public int getRedMask() { int getRedMask() {
return redMask; return redMask;
} }
int getGreenMask() {
return greenMask;
}
int getBlueMask() {
return blueMask;
}
int getAlphaMask() {
return alphaMask;
}
} }

View File

@ -1,5 +1,7 @@
package com.twelvemonkeys.imageio.plugins.dds; package com.twelvemonkeys.imageio.plugins.dds;
import static com.twelvemonkeys.imageio.util.IIOUtil.subsampleRow;
import com.twelvemonkeys.imageio.ImageReaderBase; import com.twelvemonkeys.imageio.ImageReaderBase;
import com.twelvemonkeys.imageio.util.ImageTypeSpecifiers; import com.twelvemonkeys.imageio.util.ImageTypeSpecifiers;
@ -7,14 +9,14 @@ import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam; import javax.imageio.ImageReadParam;
import javax.imageio.ImageTypeSpecifier; import javax.imageio.ImageTypeSpecifier;
import javax.imageio.spi.ImageReaderSpi; import javax.imageio.spi.ImageReaderSpi;
import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
public final class DDSImageReader extends ImageReaderBase { public final class DDSImageReader extends ImageReaderBase {
@ -47,6 +49,7 @@ public final class DDSImageReader extends ImageReaderBase {
@Override @Override
public int getNumImages(final boolean allowSearch) throws IOException { public int getNumImages(final boolean allowSearch) throws IOException {
assertInput();
readHeader(); readHeader();
return header.getMipMapCount(); return header.getMipMapCount();
@ -79,12 +82,28 @@ public final class DDSImageReader extends ImageReaderBase {
int height = getHeight(imageIndex); int height = getHeight(imageIndex);
BufferedImage destination = getDestination(param, getImageTypes(imageIndex), width, height); BufferedImage destination = getDestination(param, getImageTypes(imageIndex), width, height);
destination.setRGB(0, 0, width, height, pixels, 0, width);
// TODO: break read into raster line and add progress and abort checks Rectangle srcRegion = new Rectangle();
processImageProgress(100f); Rectangle destRegion = new Rectangle();
computeRegions(param, width, height, destination, srcRegion, destRegion);
int srcXStep = param != null ? param.getSourceXSubsampling() : 1;
int srcYStep = param != null ? param.getSourceYSubsampling() : 1;
int srcMaxY = srcRegion.y + srcRegion.height;
for (int srcY = srcRegion.y, destY = destRegion.y; srcY < srcMaxY; srcY += srcYStep, destY++) {
int offset = width * srcY + srcRegion.x;
subsampleRow(pixels, offset, width, pixels, offset, 1, 32, srcXStep);
destination.setRGB(destRegion.x, destY, destRegion.width, 1, pixels, offset, width);
if (abortRequested()) { if (abortRequested()) {
processReadAborted(); processReadAborted();
break;
}
processImageProgress(100f * srcY / srcRegion.height);
} }
processImageComplete(); processImageComplete();
@ -104,45 +123,10 @@ public final class DDSImageReader extends ImageReaderBase {
} }
public static void main(final String[] args) throws IOException { public static void main(final String[] args) throws IOException {
for (String arg : args) {
String parentDir = "imageio/imageio-dds/src/test/resources/dds"; File file = new File(arg);
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); BufferedImage image = ImageIO.read(file);
showIt(image, file.getName()); showIt(image, file.getName());
} }
} }
} }

View File

@ -20,16 +20,15 @@ import java.io.IOException;
final class DDSReader { final class DDSReader {
public static final Order order = new Order(16, 8, 0, 24); static final Order ARGB_ORDER = new Order(16, 8, 0, 24);
private final DDSHeader header; private final DDSHeader header;
DDSReader(DDSHeader header) {
public DDSReader(DDSHeader header) {
this.header = header; this.header = header;
} }
public int[] read(ImageInputStream imageInput, int imageIndex) throws IOException { int[] read(ImageInputStream imageInput, int imageIndex) throws IOException {
// type // type
DDSType type = getType(); DDSType type = getType();
@ -87,7 +86,7 @@ final class DDSReader {
if ((flags & 0x04) != 0) { if ((flags & 0x04) != 0) {
// DXT // DXT
int type = header.getFourCC(); int type = header.getFourCC();
return DDSType.parse(type); return DDSType.valueOf(type);
} else if ((flags & 0x40) != 0) { } else if ((flags & 0x40) != 0) {
// RGB // RGB
int bitCount = header.getBitCount(); int bitCount = header.getBitCount();
@ -316,7 +315,7 @@ final class DDSReader {
int g = BIT5[(rgba & A1R5G5B5_MASKS[1]) >> 5]; int g = BIT5[(rgba & A1R5G5B5_MASKS[1]) >> 5];
int b = BIT5[(rgba & A1R5G5B5_MASKS[2])]; int b = BIT5[(rgba & A1R5G5B5_MASKS[2])];
int a = 255 * ((rgba & A1R5G5B5_MASKS[3]) >> 15); int a = 255 * ((rgba & A1R5G5B5_MASKS[3]) >> 15);
pixels[i] = (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); pixels[i] = (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
return pixels; return pixels;
} }
@ -331,7 +330,7 @@ final class DDSReader {
int g = BIT5[(rgba & X1R5G5B5_MASKS[1]) >> 5]; int g = BIT5[(rgba & X1R5G5B5_MASKS[1]) >> 5];
int b = BIT5[(rgba & X1R5G5B5_MASKS[2])]; int b = BIT5[(rgba & X1R5G5B5_MASKS[2])];
int a = 255; int a = 255;
pixels[i] = (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); pixels[i] = (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
return pixels; return pixels;
} }
@ -346,7 +345,7 @@ final class DDSReader {
int g = 17 * ((rgba & A4R4G4B4_MASKS[1]) >> 4); int g = 17 * ((rgba & A4R4G4B4_MASKS[1]) >> 4);
int b = 17 * ((rgba & A4R4G4B4_MASKS[2])); int b = 17 * ((rgba & A4R4G4B4_MASKS[2]));
int a = 17 * ((rgba & A4R4G4B4_MASKS[3]) >> 12); int a = 17 * ((rgba & A4R4G4B4_MASKS[3]) >> 12);
pixels[i] = (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); pixels[i] = (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
return pixels; return pixels;
} }
@ -361,7 +360,7 @@ final class DDSReader {
int g = 17 * ((rgba & A4R4G4B4_MASKS[1]) >> 4); int g = 17 * ((rgba & A4R4G4B4_MASKS[1]) >> 4);
int b = 17 * ((rgba & A4R4G4B4_MASKS[2])); int b = 17 * ((rgba & A4R4G4B4_MASKS[2]));
int a = 255; int a = 255;
pixels[i] = (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); pixels[i] = (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
return pixels; return pixels;
} }
@ -376,7 +375,7 @@ final class DDSReader {
int g = BIT6[((rgba & R5G6B5_MASKS[1]) >> 5)]; int g = BIT6[((rgba & R5G6B5_MASKS[1]) >> 5)];
int b = BIT5[((rgba & R5G6B5_MASKS[2]))]; int b = BIT5[((rgba & R5G6B5_MASKS[2]))];
int a = 255; int a = 255;
pixels[i] = (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); pixels[i] = (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
return pixels; return pixels;
} }
@ -389,7 +388,7 @@ final class DDSReader {
int g = buffer[index++] & 0xFF; int g = buffer[index++] & 0xFF;
int r = buffer[index++] & 0xFF; int r = buffer[index++] & 0xFF;
int a = 255; int a = 255;
pixels[i] = (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); pixels[i] = (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
return pixels; return pixels;
} }
@ -402,7 +401,7 @@ final class DDSReader {
int g = buffer[index++] & 0xFF; int g = buffer[index++] & 0xFF;
int b = buffer[index++] & 0xFF; int b = buffer[index++] & 0xFF;
int a = buffer[index++] & 0xFF; int a = buffer[index++] & 0xFF;
pixels[i] = (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); pixels[i] = (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
return pixels; return pixels;
} }
@ -416,7 +415,7 @@ final class DDSReader {
int b = buffer[index++] & 0xFF; int b = buffer[index++] & 0xFF;
int a = 255; int a = 255;
index++; index++;
pixels[i] = (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); pixels[i] = (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
return pixels; return pixels;
} }
@ -429,7 +428,7 @@ final class DDSReader {
int g = buffer[index++] & 0xFF; int g = buffer[index++] & 0xFF;
int r = buffer[index++] & 0xFF; int r = buffer[index++] & 0xFF;
int a = buffer[index++] & 0xFF; int a = buffer[index++] & 0xFF;
pixels[i] = (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); pixels[i] = (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
return pixels; return pixels;
} }
@ -443,7 +442,7 @@ final class DDSReader {
int r = buffer[index++] & 0xFF; int r = buffer[index++] & 0xFF;
int a = 255; int a = 255;
index++; index++;
pixels[i] = (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); pixels[i] = (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
return pixels; return pixels;
} }
@ -467,7 +466,7 @@ final class DDSReader {
int r = (2 * BIT5[(c0 & 0xFC00) >> 11] + BIT5[(c1 & 0xFC00) >> 11]) / 3; int r = (2 * BIT5[(c0 & 0xFC00) >> 11] + BIT5[(c1 & 0xFC00) >> 11]) / 3;
int g = (2 * BIT6[(c0 & 0x07E0) >> 5] + BIT6[(c1 & 0x07E0) >> 5]) / 3; int g = (2 * BIT6[(c0 & 0x07E0) >> 5] + BIT6[(c1 & 0x07E0) >> 5]) / 3;
int b = (2 * BIT5[c0 & 0x001F] + BIT5[c1 & 0x001F]) / 3; int b = (2 * BIT5[c0 & 0x001F] + BIT5[c1 & 0x001F]) / 3;
return (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); return (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
private static int getDXTColor1_1(int c0, int c1, int a) { private static int getDXTColor1_1(int c0, int c1, int a) {
@ -475,14 +474,14 @@ final class DDSReader {
int r = (BIT5[(c0 & 0xFC00) >> 11] + BIT5[(c1 & 0xFC00) >> 11]) / 2; int r = (BIT5[(c0 & 0xFC00) >> 11] + BIT5[(c1 & 0xFC00) >> 11]) / 2;
int g = (BIT6[(c0 & 0x07E0) >> 5] + BIT6[(c1 & 0x07E0) >> 5]) / 2; int g = (BIT6[(c0 & 0x07E0) >> 5] + BIT6[(c1 & 0x07E0) >> 5]) / 2;
int b = (BIT5[c0 & 0x001F] + BIT5[c1 & 0x001F]) / 2; int b = (BIT5[c0 & 0x001F] + BIT5[c1 & 0x001F]) / 2;
return (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); return (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
private static int getDXTColor1(int c, int a) { private static int getDXTColor1(int c, int a) {
int r = BIT5[(c & 0xFC00) >> 11]; int r = BIT5[(c & 0xFC00) >> 11];
int g = BIT6[(c & 0x07E0) >> 5]; int g = BIT6[(c & 0x07E0) >> 5];
int b = BIT5[(c & 0x001F)]; int b = BIT5[(c & 0x001F)];
return (a << order.alphaShift) | (r << order.redShift) | (g << order.greenShift) | (b << order.blueShift); return (a << ARGB_ORDER.alphaShift) | (r << ARGB_ORDER.redShift) | (g << ARGB_ORDER.greenShift) | (b << ARGB_ORDER.blueShift);
} }
private static int getDXT5Alpha(int a0, int a1, int t) { private static int getDXT5Alpha(int a0, int a1, int t) {

View File

@ -1,7 +1,5 @@
package com.twelvemonkeys.imageio.plugins.dds; package com.twelvemonkeys.imageio.plugins.dds;
import javax.imageio.IIOException;
enum DDSType { enum DDSType {
DXT1(0x31545844), DXT1(0x31545844),
@ -30,12 +28,13 @@ enum DDSType {
return value; return value;
} }
public static DDSType parse(int type) throws IIOException { public static DDSType valueOf(int value) {
for (DDSType t : DDSType.values()) { for (DDSType type : DDSType.values()) {
if (type == t.value()) { if (value == type.value()) {
return t; return type;
} }
} }
throw new IIOException("Unknown type: " + Integer.toHexString(type));
throw new IllegalArgumentException(String.format("Unknown type: 0x%08x", value));
} }
} }

View File

@ -1,13 +1,9 @@
import com.twelvemonkeys.imageio.plugins.dds.DDSImageReader; import com.twelvemonkeys.imageio.plugins.dds.DDSImageReader;
import com.twelvemonkeys.imageio.plugins.dds.DDSImageReaderSpi; import com.twelvemonkeys.imageio.plugins.dds.DDSImageReaderSpi;
import com.twelvemonkeys.imageio.util.ImageReaderAbstractTest; import com.twelvemonkeys.imageio.util.ImageReaderAbstractTest;
import org.junit.Ignore;
import org.junit.Test;
import javax.imageio.spi.ImageReaderSpi; import javax.imageio.spi.ImageReaderSpi;
import java.awt.Dimension; import java.awt.Dimension;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -20,41 +16,48 @@ public class DDSImageTeaderTest extends ImageReaderAbstractTest<DDSImageReader>
@Override @Override
protected List<TestData> getTestData() { protected List<TestData> getTestData() {
Dimension dim = new Dimension(256, 256); Dimension dim256 = new Dimension(256, 256);
Dimension dim128 = new Dimension(128, 128);
Dimension dim64 = new Dimension(64, 64);
Dimension dim32 = new Dimension(32, 32);
Dimension dim16 = new Dimension(16, 16);
Dimension dim8 = new Dimension(8, 8);
Dimension dim4 = new Dimension(4, 4);
Dimension dim2 = new Dimension(2, 2);
Dimension dim1 = new Dimension(1, 1);
List<TestData> testData = new ArrayList<>(); return Arrays.asList(
testData.add(new TestData(getClassLoaderResource("/dds/dds_A1R5G5B5.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_A1R5G5B5.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_A1R5G5B5_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_A1R5G5B5_mipmap.dds"), dim256, dim128, dim64, dim32, dim16, dim8, dim4, dim2, dim1),
testData.add(new TestData(getClassLoaderResource("/dds/dds_A4R4G4B4.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_A4R4G4B4.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_A4R4G4B4_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_A4R4G4B4_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_A8B8G8R8.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_A8B8G8R8.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_A8B8G8R8_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_A8B8G8R8_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_A8R8G8B8.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_A8R8G8B8.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_A8R8G8B8_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_A8R8G8B8_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_DXT1.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_DXT1.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_DXT1_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_DXT1_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_DXT2.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_DXT2.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_DXT2_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_DXT2_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_DXT3.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_DXT3.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_DXT3_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_DXT3_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_DXT4.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_DXT4.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_DXT4_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_DXT4_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_DXT5.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_DXT5.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_DXT5_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_DXT5_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_R5G6B5.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_R5G6B5.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_R5G6B5_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_R5G6B5_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_R8G8B8.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_R8G8B8.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_R8G8B8_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_R8G8B8_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_X1R5G5B5.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_X1R5G5B5.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_X1R5G5B5_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_X1R5G5B5_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_X4R4G4B4.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_X4R4G4B4.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_X4R4G4B4_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_X4R4G4B4_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_X8B8G8R8.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_X8B8G8R8.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_X8B8G8R8_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_X8B8G8R8_mipmap.dds"), dim256, dim128, dim64),
testData.add(new TestData(getClassLoaderResource("/dds/dds_X8R8G8B8.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_X8R8G8B8.dds"), dim256),
testData.add(new TestData(getClassLoaderResource("/dds/dds_X8R8G8B8_mipmap.dds"), dim)); new TestData(getClassLoaderResource("/dds/dds_X8R8G8B8_mipmap.dds"), dim256, dim128, dim64)
);
return testData;
} }
@Override @Override
@ -64,78 +67,11 @@ public class DDSImageTeaderTest extends ImageReaderAbstractTest<DDSImageReader>
@Override @Override
protected List<String> getSuffixes() { protected List<String> getSuffixes() {
return Arrays.asList("dds"); return Collections.singletonList("dds");
} }
@Override @Override
protected List<String> getMIMETypes() { protected List<String> getMIMETypes() {
return Collections.singletonList("image/vnd-ms.dds"); return Collections.singletonList("image/vnd-ms.dds");
} }
/* ************************************************************************************************************* *
* IGNORE Broken Tests...
* ************************************************************************************************************* */
@Ignore("Known issue: currently not supported in DDS")
@Test
@Override
public void testGetNumImagesNoInput() throws IOException {
super.testGetNumImagesNoInput();
}
@Ignore("Known issue: currently not supported in DDS")
@Test
@Override
public void testAffineTransformOpCompatibility() throws IOException {
super.testAffineTransformOpCompatibility();
}
@Ignore("Known issue: currently not supported in DDS")
@Test
@Override
public void testReadWithSourceRegionParam() throws IOException {
super.testReadWithSourceRegionParam();
}
@Ignore("Known issue: currently not supported in DDS")
@Test
@Override
public void testReadWithSourceRegionParamEqualImage() throws IOException {
super.testReadWithSourceRegionParamEqualImage();
}
@Ignore("Known issue: currently not supported in DDS")
@Test
@Override
public void testReadWithSubsampleAndSourceRegionParam() throws IOException {
super.testReadWithSubsampleAndSourceRegionParam();
}
@Ignore("Known issue: currently not supported in DDS")
@Test
@Override
public void testReadWithSubsampleParamDimensions() throws IOException {
super.testReadWithSubsampleParamDimensions();
}
@Ignore("Known issue: currently not supported in DDS")
@Test
@Override
public void testReadWithSubsampleParamPixels() throws IOException {
super.testReadWithSubsampleParamPixels();
}
@Ignore("Known issue: currently not supported in DDS")
@Test
@Override
public void testSetDestination() throws IOException {
super.testSetDestination();
}
@Ignore("Known issue: currently not supported in DDS")
@Test
@Override
public void testSetDestinationIllegal() throws IOException {
super.testSetDestinationIllegal();
}
} }