Skip to content

Commit

Permalink
Add source region and subsample support + license.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldk committed Sep 25, 2024
1 parent 3c01071 commit aab7b6f
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1391,14 +1391,16 @@ public void testGetTypeSpecifiers() throws IOException {
public void testSetDestination() throws IOException {
ImageReader reader = createReader();
TestData data = getTestData().get(0);
Dimension size = data.getDimension(0);

reader.setInput(data.getInputStream());

ImageReadParam param = reader.getDefaultReadParam();
Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0);
while (types.hasNext()) {
ImageTypeSpecifier type = types.next();

BufferedImage destination = type.createBufferedImage(50, 50);
BufferedImage destination = type.createBufferedImage(size.width, size.height);
param.setDestination(destination);

BufferedImage result = null;
Expand Down Expand Up @@ -1448,13 +1450,15 @@ public void testSetDestinationRaw() throws IOException {
public void testSetDestinationIllegal() throws IOException {
final ImageReader reader = createReader();
TestData data = getTestData().get(0);
Dimension size = data.getDimension(0);

reader.setInput(data.getInputStream());

List<ImageTypeSpecifier> illegalTypes = createIllegalTypes(reader.getImageTypes(0));

ImageReadParam param = reader.getDefaultReadParam();
for (ImageTypeSpecifier illegalType : illegalTypes) {
BufferedImage destination = illegalType.createBufferedImage(50, 50);
BufferedImage destination = illegalType.createBufferedImage(size.width, size.height);
param.setDestination(destination);

try {
Expand Down Expand Up @@ -1755,11 +1759,12 @@ public void testAffineTransformOpCompatibility() throws IOException {
ImageReader reader = createReader();

for (TestData testData : getTestDataForAffineTransformOpCompatibility()) {
Dimension size = testData.getDimension(0);
try (ImageInputStream input = testData.getInputStream()) {
reader.setInput(input);

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);

Expand Down
52 changes: 52 additions & 0 deletions imageio/imageio-dds/license.txt
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ final class DDSHeader {
private int blueMask;
private int alphaMask;

public static DDSHeader read(final ImageInputStream imageInput) throws IOException {
static DDSHeader read(final ImageInputStream imageInput) throws IOException {
DDSHeader header = new DDSHeader();

// Read MAGIC bytes [0,3]
byte[] magic = new byte[DDS.MAGIC.length];
imageInput.readFully(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
Expand All @@ -42,11 +42,11 @@ public static DDSHeader read(final ImageInputStream imageInput) throws IOExcepti

// 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));
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.toBinaryString(header.flags));
}

// Read Height & Width
Expand All @@ -55,7 +55,9 @@ public static DDSHeader read(final ImageInputStream imageInput) throws IOExcepti

int dwPitchOrLinearSize = imageInput.readInt(); // [20,23]
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
header.addDimensions(dwWidth, dwHeight);
Expand Down Expand Up @@ -85,11 +87,11 @@ public static DDSHeader read(final ImageInputStream imageInput) throws IOExcepti
}

private void addDimensions(int width, int height) {
dimensions = new Dimension[getMipMapCount()];
dimensions = new Dimension[mipMapCount];

int w = width;
int h = height;
for (int i = 0; i < getMipMapCount(); i++) {
for (int i = 0; i < mipMapCount; i++) {
dimensions[i] = new Dimension(w, h);
w /= 2;
h /= 2;
Expand All @@ -100,50 +102,45 @@ private boolean getFlag(int mask) {
return (flags & mask) != 0;
}

public int getWidth(int imageIndex) {
int getWidth(int imageIndex) {
int lim = dimensions[imageIndex].width;
return (lim <= 0) ? 1 : lim;
}

public int getHeight(int imageIndex) {
int getHeight(int imageIndex) {
int lim = dimensions[imageIndex].height;
return (lim <= 0) ? 1 : lim;
}

public int getMipMapCount() {
// 0 = (unused) or 1 = (1 level), but still only one 'base' image
return (mipMapCount == 0) ? 1 : mipMapCount;
}

public int getAlphaMask() {
return alphaMask;
int getMipMapCount() {
return mipMapCount;
}

public int getBitCount() {
int getBitCount() {
return bitCount;
}

public int getBlueMask() {
return blueMask;
int getFourCC() {
return fourCC;
}

public int getFlags() {
return flags;
int getPixelFormatFlags() {
return pixelFormatFlags;
}

public int getFourCC() {
return fourCC;
int getRedMask() {
return redMask;
}

public int getGreenMask() {
int getGreenMask() {
return greenMask;
}

public int getPixelFormatFlags() {
return pixelFormatFlags;
int getBlueMask() {
return blueMask;
}

public int getRedMask() {
return redMask;
int getAlphaMask() {
return alphaMask;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.twelvemonkeys.imageio.plugins.dds;

import static com.twelvemonkeys.imageio.util.IIOUtil.subsampleRow;

import com.twelvemonkeys.imageio.ImageReaderBase;
import com.twelvemonkeys.imageio.util.ImageTypeSpecifiers;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.spi.ImageReaderSpi;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public final class DDSImageReader extends ImageReaderBase {

Expand Down Expand Up @@ -47,6 +49,7 @@ public int getHeight(int imageIndex) throws IOException {

@Override
public int getNumImages(final boolean allowSearch) throws IOException {
assertInput();
readHeader();

return header.getMipMapCount();
Expand Down Expand Up @@ -79,12 +82,28 @@ public BufferedImage read(int imageIndex, ImageReadParam param) throws IOExcepti
int height = getHeight(imageIndex);

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
processImageProgress(100f);
if (abortRequested()) {
processReadAborted();
Rectangle srcRegion = new Rectangle();
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()) {
processReadAborted();
break;
}

processImageProgress(100f * srcY / srcRegion.height);
}

processImageComplete();
Expand All @@ -104,45 +123,10 @@ private void readHeader() throws IOException {
}

public static void main(final String[] args) throws IOException {

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) {
for (String arg : args) {
File file = new File(arg);
BufferedImage image = ImageIO.read(file);
showIt(image, file.getName());
}

}
}
Loading

0 comments on commit aab7b6f

Please sign in to comment.