mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-05 04:25:29 -04:00
#280 Support for bitsPerSample == 6, 10, 12, 14 & 24
This commit is contained in:
parent
ab13084f44
commit
e0b9bdef7e
@ -29,7 +29,6 @@
|
||||
package com.twelvemonkeys.imageio.util;
|
||||
|
||||
import com.twelvemonkeys.imageio.color.DiscreteAlphaIndexColorModel;
|
||||
import com.twelvemonkeys.lang.Validate;
|
||||
|
||||
import javax.imageio.ImageTypeSpecifier;
|
||||
import java.awt.color.ColorSpace;
|
||||
@ -84,7 +83,7 @@ public final class ImageTypeSpecifiers {
|
||||
final boolean isAlphaPremultiplied) {
|
||||
// As the ComponentColorModel is broken for 32 bit unsigned int, we'll use our own version
|
||||
if (dataType == DataBuffer.TYPE_INT) {
|
||||
return new UInt32ImageTypeSpecifier(colorSpace, bandOffsets, hasAlpha, isAlphaPremultiplied);
|
||||
return UInt32ImageTypeSpecifier.createInterleaved(colorSpace, bandOffsets, hasAlpha, isAlphaPremultiplied);
|
||||
}
|
||||
|
||||
// ...or fall back to default for anything else
|
||||
@ -95,6 +94,12 @@ public final class ImageTypeSpecifiers {
|
||||
final int[] bankIndices, final int[] bandOffsets,
|
||||
final int dataType,
|
||||
final boolean hasAlpha, final boolean isAlphaPremultiplied) {
|
||||
// As the ComponentColorModel is broken for 32 bit unsigned int, we'll use our own version
|
||||
if (dataType == DataBuffer.TYPE_INT) {
|
||||
return UInt32ImageTypeSpecifier.createBanded(colorSpace, bankIndices, bandOffsets, hasAlpha, isAlphaPremultiplied);
|
||||
}
|
||||
|
||||
// ...or fall back to default for anything else
|
||||
return ImageTypeSpecifier.createBanded(colorSpace, bankIndices, bandOffsets, dataType, hasAlpha, isAlphaPremultiplied);
|
||||
}
|
||||
|
||||
@ -105,7 +110,7 @@ public final class ImageTypeSpecifiers {
|
||||
}
|
||||
else if (bits == 32 && dataType == DataBuffer.TYPE_INT) {
|
||||
// As the ComponentColorModel is broken for 32 bit unsigned int, we'll use our own version
|
||||
return new UInt32ImageTypeSpecifier(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] {0}, false, false);
|
||||
return UInt32ImageTypeSpecifier.createInterleaved(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] {0}, false, false);
|
||||
}
|
||||
|
||||
// NOTE: The isSigned boolean is stored but *not used for anything* in the Grayscale ImageTypeSpecifier...
|
||||
@ -119,7 +124,7 @@ public final class ImageTypeSpecifiers {
|
||||
}
|
||||
else if (bits == 32 && dataType == DataBuffer.TYPE_INT) {
|
||||
// As the ComponentColorModel is broken for 32 bit unsigned int, we'll use our own version
|
||||
return new UInt32ImageTypeSpecifier(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] {0, 1}, true, isAlphaPremultiplied);
|
||||
return UInt32ImageTypeSpecifier.createInterleaved(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] {0, 1}, true, isAlphaPremultiplied);
|
||||
}
|
||||
|
||||
// NOTE: The isSigned boolean is stored but *not used for anything* in the Grayscale ImageTypeSpecifier...
|
||||
@ -166,7 +171,7 @@ public final class ImageTypeSpecifiers {
|
||||
}
|
||||
|
||||
public static ImageTypeSpecifier createFromIndexColorModel(final IndexColorModel pColorModel) {
|
||||
return new IndexedImageTypeSpecifier(pColorModel);
|
||||
return IndexedImageTypeSpecifier.createFromIndexColorModel(pColorModel);
|
||||
}
|
||||
|
||||
public static ImageTypeSpecifier createDiscreteAlphaIndexedFromIndexColorModel(final IndexColorModel pColorModel) {
|
||||
|
@ -15,24 +15,27 @@ import static com.twelvemonkeys.lang.Validate.notNull;
|
||||
* @author last modified by $Author: haraldk$
|
||||
* @version $Id: IndexedImageTypeSpecifier.java,v 1.0 May 19, 2008 11:04:28 AM haraldk Exp$
|
||||
*/
|
||||
final class IndexedImageTypeSpecifier extends ImageTypeSpecifier {
|
||||
IndexedImageTypeSpecifier(final IndexColorModel pColorModel) {
|
||||
// For some reason, we need a sample model
|
||||
super(notNull(pColorModel, "colorModel"), pColorModel.createCompatibleSampleModel(1, 1));
|
||||
}
|
||||
final class IndexedImageTypeSpecifier {
|
||||
private IndexedImageTypeSpecifier() {}
|
||||
|
||||
@Override
|
||||
public final BufferedImage createBufferedImage(final int pWidth, final int pHeight) {
|
||||
try {
|
||||
// This is a fix for the super-method, that first creates a sample model, and then
|
||||
// creates a raster from it, using Raster.createWritableRaster. The problem with
|
||||
// that approach, is that it always creates a TYPE_CUSTOM BufferedImage for indexed images.
|
||||
WritableRaster raster = colorModel.createCompatibleWritableRaster(pWidth, pHeight);
|
||||
return new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), new Hashtable());
|
||||
}
|
||||
catch (NegativeArraySizeException e) {
|
||||
// Exception most likely thrown from a DataBuffer constructor
|
||||
throw new IllegalArgumentException("Array size > Integer.MAX_VALUE!");
|
||||
}
|
||||
static ImageTypeSpecifier createFromIndexColorModel(final IndexColorModel pColorModel) {
|
||||
// For some reason, we need a sample model
|
||||
return new ImageTypeSpecifier(notNull(pColorModel, "colorModel"), pColorModel.createCompatibleSampleModel(1, 1)) {
|
||||
|
||||
@Override
|
||||
public final BufferedImage createBufferedImage(final int pWidth, final int pHeight) {
|
||||
try {
|
||||
// This is a fix for the super-method, that first creates a sample model, and then
|
||||
// creates a raster from it, using Raster.createWritableRaster. The problem with
|
||||
// that approach, is that it always creates a TYPE_CUSTOM BufferedImage for indexed images.
|
||||
WritableRaster raster = colorModel.createCompatibleWritableRaster(pWidth, pHeight);
|
||||
return new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), new Hashtable());
|
||||
}
|
||||
catch (NegativeArraySizeException e) {
|
||||
// Exception most likely thrown from a DataBuffer constructor
|
||||
throw new IllegalArgumentException("Array size > Integer.MAX_VALUE!");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -32,8 +32,10 @@ import com.twelvemonkeys.imageio.color.UInt32ColorModel;
|
||||
|
||||
import javax.imageio.ImageTypeSpecifier;
|
||||
import java.awt.color.ColorSpace;
|
||||
import java.awt.image.BandedSampleModel;
|
||||
import java.awt.image.DataBuffer;
|
||||
import java.awt.image.PixelInterleavedSampleModel;
|
||||
import java.awt.image.SampleModel;
|
||||
|
||||
/**
|
||||
* ImageTypeSpecifier for interleaved 32 bit unsigned integral samples.
|
||||
@ -43,10 +45,12 @@ import java.awt.image.PixelInterleavedSampleModel;
|
||||
* @author last modified by $Author: haraldk$
|
||||
* @version $Id: UInt32ImageTypeSpecifier.java,v 1.0 24.01.11 17.51 haraldk Exp$
|
||||
*/
|
||||
final class UInt32ImageTypeSpecifier extends ImageTypeSpecifier {
|
||||
UInt32ImageTypeSpecifier(final ColorSpace cs, int[] bandOffsets, final boolean hasAlpha, final boolean isAlphaPremultiplied) {
|
||||
super(
|
||||
new UInt32ColorModel(cs, hasAlpha, isAlphaPremultiplied),
|
||||
final class UInt32ImageTypeSpecifier {
|
||||
private UInt32ImageTypeSpecifier() {}
|
||||
|
||||
static ImageTypeSpecifier createInterleaved(final ColorSpace cs, final int[] bandOffsets, final boolean hasAlpha, final boolean isAlphaPremultiplied) {
|
||||
return create(
|
||||
cs, hasAlpha, isAlphaPremultiplied,
|
||||
new PixelInterleavedSampleModel(
|
||||
DataBuffer.TYPE_INT, 1, 1,
|
||||
cs.getNumComponents() + (hasAlpha ? 1 : 0),
|
||||
@ -55,4 +59,18 @@ final class UInt32ImageTypeSpecifier extends ImageTypeSpecifier {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
static ImageTypeSpecifier createBanded(final ColorSpace cs, final int[] bandIndices, final int[] bandOffsets, final boolean hasAlpha, final boolean isAlphaPremultiplied) {
|
||||
return create(
|
||||
cs, hasAlpha, isAlphaPremultiplied,
|
||||
new BandedSampleModel(
|
||||
DataBuffer.TYPE_INT, 1, 1, 1,
|
||||
bandIndices, bandOffsets
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private static ImageTypeSpecifier create(final ColorSpace cs, final boolean hasAlpha, final boolean isAlphaPremultiplied, final SampleModel sampleModel) {
|
||||
return new ImageTypeSpecifier(new UInt32ColorModel(cs, hasAlpha, isAlphaPremultiplied), sampleModel);
|
||||
}
|
||||
}
|
||||
|
@ -182,24 +182,24 @@ public class ImageTypeSpecifiersTest {
|
||||
public void testCreateInterleaved32() {
|
||||
// 32 bits/sample
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(GRAY, new int[] {0}, false, false),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0}, false, false),
|
||||
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0}, DataBuffer.TYPE_INT, false, false)
|
||||
);
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(GRAY, new int[] {0, 1}, true, false),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0, 1}, true, false),
|
||||
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0, 1}, DataBuffer.TYPE_INT, true, false)
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(sRGB, new int[] {0, 1, 2}, false, false),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2}, false, false),
|
||||
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2}, DataBuffer.TYPE_INT, false, false)
|
||||
);
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(sRGB, new int[] {0, 1, 2, 3}, true, false),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, true, false),
|
||||
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_INT, true, false)
|
||||
);
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(sRGB, new int[] {0, 1, 2, 3}, true, true),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, true, true),
|
||||
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_INT, true, true)
|
||||
);
|
||||
}
|
||||
@ -303,15 +303,15 @@ public class ImageTypeSpecifiersTest {
|
||||
@Test
|
||||
public void testCreateBanded32() {
|
||||
assertEquals(
|
||||
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_INT, false, false),
|
||||
UInt32ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, false, false),
|
||||
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_INT, false, false)
|
||||
);
|
||||
assertEquals(
|
||||
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_INT, true, false),
|
||||
UInt32ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, true, false),
|
||||
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_INT, true, false)
|
||||
);
|
||||
assertEquals(
|
||||
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_INT, true, true),
|
||||
UInt32ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, true, true),
|
||||
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_INT, true, true)
|
||||
);
|
||||
}
|
||||
@ -383,11 +383,11 @@ public class ImageTypeSpecifiersTest {
|
||||
@Test
|
||||
public void testCreateGrayscale32() {
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(GRAY, new int[] {0}, false, false),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0}, false, false),
|
||||
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT)
|
||||
);
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(GRAY, new int[] {0}, false, false),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0}, false, false),
|
||||
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT)
|
||||
);
|
||||
}
|
||||
@ -446,19 +446,19 @@ public class ImageTypeSpecifiersTest {
|
||||
@Test
|
||||
public void testCreateGrayscaleAlpha32() {
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(GRAY, new int[] {0, 1}, true, false),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0, 1}, true, false),
|
||||
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT, false)
|
||||
);
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(GRAY, new int[] {0, 1}, true, false),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0, 1}, true, false),
|
||||
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT, false)
|
||||
);
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(GRAY, new int[] {0, 1}, true, true),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0, 1}, true, true),
|
||||
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT, true)
|
||||
);
|
||||
assertEquals(
|
||||
new UInt32ImageTypeSpecifier(GRAY, new int[] {0, 1}, true, true),
|
||||
UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0, 1}, true, true),
|
||||
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT, true)
|
||||
);
|
||||
}
|
||||
@ -573,7 +573,7 @@ public class ImageTypeSpecifiersTest {
|
||||
for (int bits = 1; bits <= 8; bits <<= 1) {
|
||||
int[] colors = createIntLut(1 << bits);
|
||||
assertEquals(
|
||||
new IndexedImageTypeSpecifier(new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE)),
|
||||
IndexedImageTypeSpecifier.createFromIndexColorModel(new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE)),
|
||||
ImageTypeSpecifiers.createIndexed(colors, false, -1, bits, DataBuffer.TYPE_BYTE)
|
||||
);
|
||||
}
|
||||
@ -583,7 +583,7 @@ public class ImageTypeSpecifiersTest {
|
||||
public void testCreateIndexedIntArray16() {
|
||||
int[] colors = createIntLut(1 << 16);
|
||||
assertEquals(
|
||||
new IndexedImageTypeSpecifier(new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT)),
|
||||
IndexedImageTypeSpecifier.createFromIndexColorModel(new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT)),
|
||||
ImageTypeSpecifiers.createIndexed(colors, false, -1, 16, DataBuffer.TYPE_USHORT)
|
||||
);
|
||||
|
||||
@ -595,7 +595,7 @@ public class ImageTypeSpecifiersTest {
|
||||
int[] colors = createIntLut(1 << bits);
|
||||
IndexColorModel colorModel = new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE);
|
||||
assertEquals(
|
||||
new IndexedImageTypeSpecifier(colorModel),
|
||||
IndexedImageTypeSpecifier.createFromIndexColorModel(colorModel),
|
||||
ImageTypeSpecifiers.createFromIndexColorModel(colorModel)
|
||||
);
|
||||
}
|
||||
@ -606,7 +606,7 @@ public class ImageTypeSpecifiersTest {
|
||||
int[] colors = createIntLut(1 << 16);
|
||||
IndexColorModel colorModel = new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT);
|
||||
assertEquals(
|
||||
new IndexedImageTypeSpecifier(colorModel),
|
||||
IndexedImageTypeSpecifier.createFromIndexColorModel(colorModel),
|
||||
ImageTypeSpecifiers.createFromIndexColorModel(colorModel)
|
||||
);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.twelvemonkeys.imageio.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.imageio.ImageTypeSpecifier;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.DataBuffer;
|
||||
import java.awt.image.IndexColorModel;
|
||||
@ -20,9 +21,9 @@ public class IndexedImageTypeSpecifierTest {
|
||||
public void testEquals() {
|
||||
IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE);
|
||||
|
||||
IndexedImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
|
||||
IndexedImageTypeSpecifier other = new IndexedImageTypeSpecifier(cm);
|
||||
IndexedImageTypeSpecifier different = new IndexedImageTypeSpecifier(new IndexColorModel(2, 2, new int[]{0xff00ff, 0x00, 0xff00ff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE));
|
||||
ImageTypeSpecifier spec = IndexedImageTypeSpecifier.createFromIndexColorModel(cm);
|
||||
ImageTypeSpecifier other = IndexedImageTypeSpecifier.createFromIndexColorModel(cm);
|
||||
ImageTypeSpecifier different = IndexedImageTypeSpecifier.createFromIndexColorModel(new IndexColorModel(2, 2, new int[]{0xff00ff, 0x00, 0xff00ff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE));
|
||||
|
||||
assertEquals(spec, other);
|
||||
assertEquals(other, spec);
|
||||
@ -41,9 +42,9 @@ public class IndexedImageTypeSpecifierTest {
|
||||
public void testHashCode() {
|
||||
IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE);
|
||||
|
||||
IndexedImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
|
||||
IndexedImageTypeSpecifier other = new IndexedImageTypeSpecifier(cm);
|
||||
IndexedImageTypeSpecifier different = new IndexedImageTypeSpecifier(new IndexColorModel(2, 2, new int[]{0xff00ff, 0x00, 0xff00ff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE));
|
||||
ImageTypeSpecifier spec = IndexedImageTypeSpecifier.createFromIndexColorModel(cm);
|
||||
ImageTypeSpecifier other = IndexedImageTypeSpecifier.createFromIndexColorModel(cm);
|
||||
ImageTypeSpecifier different = IndexedImageTypeSpecifier.createFromIndexColorModel(new IndexColorModel(2, 2, new int[]{0xff00ff, 0x00, 0xff00ff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE));
|
||||
|
||||
// TODO: There is still a problem that IndexColorModel does not override hashCode,
|
||||
// so any model with the same number of bits, transparency, and transfer type will have same hash
|
||||
@ -53,13 +54,13 @@ public class IndexedImageTypeSpecifierTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateNull() {
|
||||
new IndexedImageTypeSpecifier(null);
|
||||
IndexedImageTypeSpecifier.createFromIndexColorModel(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateBufferedImageBinary() {
|
||||
IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE);
|
||||
IndexedImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
|
||||
ImageTypeSpecifier spec = IndexedImageTypeSpecifier.createFromIndexColorModel(cm);
|
||||
|
||||
BufferedImage image = spec.createBufferedImage(2, 2);
|
||||
|
||||
@ -71,7 +72,7 @@ public class IndexedImageTypeSpecifierTest {
|
||||
@Test
|
||||
public void testCreateBufferedImageIndexed() {
|
||||
IndexColorModel cm = new IndexColorModel(8, 256, new int[256], 0, false, -1, DataBuffer.TYPE_BYTE);
|
||||
IndexedImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
|
||||
ImageTypeSpecifier spec = IndexedImageTypeSpecifier.createFromIndexColorModel(cm);
|
||||
|
||||
BufferedImage image = spec.createBufferedImage(2, 2);
|
||||
|
||||
|
@ -19,7 +19,7 @@ public class UInt32ImageTypeSpecifierTest {
|
||||
|
||||
@Test
|
||||
public void testGray() {
|
||||
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(GRAY, new int [] {0}, false, false);
|
||||
ImageTypeSpecifier spec = UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int [] {0}, false, false);
|
||||
|
||||
assertEquals(1, spec.getNumBands());
|
||||
assertEquals(1, spec.getNumComponents());
|
||||
@ -38,7 +38,7 @@ public class UInt32ImageTypeSpecifierTest {
|
||||
|
||||
@Test
|
||||
public void testGrayAlpha() {
|
||||
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(GRAY, new int [] {0, 1}, true, false);
|
||||
ImageTypeSpecifier spec = UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int [] {0, 1}, true, false);
|
||||
assertEquals(2, spec.getNumBands());
|
||||
assertEquals(2, spec.getNumComponents());
|
||||
assertEquals(32, spec.getBitsPerBand(0));
|
||||
@ -57,7 +57,7 @@ public class UInt32ImageTypeSpecifierTest {
|
||||
|
||||
@Test
|
||||
public void testRGB() {
|
||||
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2}, false, false);
|
||||
ImageTypeSpecifier spec = UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2}, false, false);
|
||||
|
||||
assertEquals(3, spec.getNumBands());
|
||||
assertEquals(3, spec.getNumComponents());
|
||||
@ -78,7 +78,7 @@ public class UInt32ImageTypeSpecifierTest {
|
||||
|
||||
@Test
|
||||
public void testRGBAlpha() {
|
||||
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2, 3}, true, false);
|
||||
ImageTypeSpecifier spec = UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2, 3}, true, false);
|
||||
assertEquals(4, spec.getNumBands());
|
||||
assertEquals(4, spec.getNumComponents());
|
||||
assertEquals(32, spec.getBitsPerBand(0));
|
||||
@ -99,7 +99,7 @@ public class UInt32ImageTypeSpecifierTest {
|
||||
|
||||
@Test
|
||||
public void testRGBAlphaPre() {
|
||||
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2, 3}, true, true);
|
||||
ImageTypeSpecifier spec = UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2, 3}, true, true);
|
||||
assertEquals(4, spec.getNumBands());
|
||||
assertEquals(4, spec.getNumComponents());
|
||||
assertEquals(32, spec.getBitsPerBand(0));
|
||||
@ -120,7 +120,7 @@ public class UInt32ImageTypeSpecifierTest {
|
||||
|
||||
@Test
|
||||
public void testCMYK() {
|
||||
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(CMYK, new int [] {0, 1, 2, 3}, false, false);
|
||||
ImageTypeSpecifier spec = UInt32ImageTypeSpecifier.createInterleaved(CMYK, new int [] {0, 1, 2, 3}, false, false);
|
||||
|
||||
assertEquals(4, spec.getNumBands());
|
||||
assertEquals(4, spec.getNumComponents());
|
||||
@ -142,7 +142,7 @@ public class UInt32ImageTypeSpecifierTest {
|
||||
|
||||
@Test
|
||||
public void testCMYKAlpha() {
|
||||
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(CMYK, new int [] {0, 1, 2, 3, 4}, true, false);
|
||||
ImageTypeSpecifier spec = UInt32ImageTypeSpecifier.createInterleaved(CMYK, new int [] {0, 1, 2, 3, 4}, true, false);
|
||||
assertEquals(5, spec.getNumBands());
|
||||
assertEquals(5, spec.getNumComponents());
|
||||
assertEquals(32, spec.getBitsPerBand(0));
|
||||
@ -165,9 +165,9 @@ public class UInt32ImageTypeSpecifierTest {
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2}, false, false);
|
||||
ImageTypeSpecifier other = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2}, false, false);
|
||||
ImageTypeSpecifier different = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2, 3}, true, false);
|
||||
ImageTypeSpecifier spec = UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2}, false, false);
|
||||
ImageTypeSpecifier other = UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2}, false, false);
|
||||
ImageTypeSpecifier different = UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2, 3}, true, false);
|
||||
// Equivalent, but broken, not equal
|
||||
ImageTypeSpecifier broken =
|
||||
ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2}, DataBuffer.TYPE_INT, false, false);
|
||||
@ -185,9 +185,9 @@ public class UInt32ImageTypeSpecifierTest {
|
||||
|
||||
@Test
|
||||
public void testHashCode() {
|
||||
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2}, false, false);
|
||||
ImageTypeSpecifier other = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2}, false, false);
|
||||
ImageTypeSpecifier different = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2, 3}, true, false);
|
||||
ImageTypeSpecifier spec = UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2}, false, false);
|
||||
ImageTypeSpecifier other = UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2}, false, false);
|
||||
ImageTypeSpecifier different = UInt32ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2, 3}, true, false);
|
||||
// Equivalent, but broken, not equal
|
||||
ImageTypeSpecifier broken =
|
||||
ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2}, DataBuffer.TYPE_INT, false, false);
|
||||
|
@ -0,0 +1,221 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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 "TwelveMonkeys" 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 OWNER 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.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.imageio.plugins.tiff;
|
||||
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import static com.twelvemonkeys.lang.Validate.notNull;
|
||||
|
||||
/**
|
||||
* BitPaddingStream.
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: harald.kuhr$
|
||||
* @version $Id: Foo.java,v 1.0 15/11/2016 harald.kuhr Exp$
|
||||
*/
|
||||
final class BitPaddingStream extends FilterInputStream {
|
||||
// Bit masks 0 - 32 bits
|
||||
private static final int[] MASK = {
|
||||
0x0,
|
||||
0x1, 0x3, 0x7, 0xf,
|
||||
0x1f, 0x3f, 0x7f, 0xff,
|
||||
0x1ff, 0x3ff, 0x7ff, 0xfff,
|
||||
0x1fff, 0x3fff, 0x7fff, 0xffff,
|
||||
0x1ffff, 0x3ffff, 0x7ffff, 0xfffff,
|
||||
0x1fffff, 0x3fffff, 0x7fffff, 0xffffff,
|
||||
0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff,
|
||||
0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff
|
||||
};
|
||||
|
||||
private final int bitsPerSample;
|
||||
|
||||
private final byte[] inputBuffer;
|
||||
private final ByteBuffer buffer;
|
||||
private int componentSize;
|
||||
|
||||
BitPaddingStream(final InputStream stream, int samplesPerPixel, final int bitsPerSample, final int colsInTile, final ByteOrder byteOrder) {
|
||||
super(notNull(stream, "stream"));
|
||||
|
||||
this.bitsPerSample = bitsPerSample;
|
||||
|
||||
notNull(byteOrder, "byteOrder");
|
||||
|
||||
switch (bitsPerSample) {
|
||||
case 2:
|
||||
case 4:
|
||||
case 6:
|
||||
// Byte
|
||||
componentSize = 1;
|
||||
break;
|
||||
case 10:
|
||||
case 12:
|
||||
case 14:
|
||||
// Short
|
||||
componentSize = 2;
|
||||
break;
|
||||
case 18:
|
||||
case 20:
|
||||
case 22:
|
||||
case 24:
|
||||
case 26:
|
||||
case 28:
|
||||
case 30:
|
||||
// Int
|
||||
componentSize = 4;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unsupported BitsPerSample value: " + bitsPerSample);
|
||||
}
|
||||
|
||||
int rowByteLength = (samplesPerPixel * bitsPerSample * colsInTile + 7) / 8;
|
||||
inputBuffer = new byte[rowByteLength];
|
||||
|
||||
int rowLength = samplesPerPixel * colsInTile * componentSize;
|
||||
buffer = ByteBuffer.allocate(rowLength);
|
||||
buffer.order(byteOrder);
|
||||
buffer.position(buffer.limit()); // Make sure we start by filling the buffer
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if (!buffer.hasRemaining()) {
|
||||
if (!fillBuffer()) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return buffer.get() & 0xff;
|
||||
}
|
||||
|
||||
private boolean readFully(final byte[] bytes) throws IOException {
|
||||
int rest = bytes.length;
|
||||
|
||||
while (rest > 0) {
|
||||
int read = in.read(bytes, bytes.length - rest, rest);
|
||||
|
||||
if (read == -1) {
|
||||
// NOTE: If we did a partial read here, we are in trouble...
|
||||
// Most likely an EOFException will happen up-stream
|
||||
return false;
|
||||
}
|
||||
|
||||
rest -= read;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(final byte[] b, final int off, final int len) throws IOException {
|
||||
if (!buffer.hasRemaining()) {
|
||||
if (!fillBuffer()) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int length = Math.min(len, buffer.remaining());
|
||||
buffer.get(b, off, length);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long skip(final long n) throws IOException {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!buffer.hasRemaining()) {
|
||||
if (!fillBuffer()) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int length = (int) Math.min(n, buffer.remaining());
|
||||
buffer.position(buffer.position() + length);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
private boolean fillBuffer() throws IOException {
|
||||
if (!readFully(inputBuffer)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer.clear();
|
||||
padBits(buffer, componentSize, bitsPerSample, inputBuffer);
|
||||
buffer.rewind();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void padBits(final ByteBuffer buffer, final int componentSize, final int bitsPerSample, final byte[] samples) {
|
||||
int offset = 0;
|
||||
int remainingBits = 0;
|
||||
int temp = 0;
|
||||
|
||||
while (true) {
|
||||
int value = temp & MASK[remainingBits];
|
||||
|
||||
// Read smallest number of bytes > bits
|
||||
while (remainingBits < bitsPerSample) {
|
||||
if (offset >= samples.length) {
|
||||
// End of data
|
||||
return;
|
||||
}
|
||||
|
||||
temp = samples[offset++] & 0xff;
|
||||
value = value << 8 | temp;
|
||||
remainingBits += 8;
|
||||
}
|
||||
|
||||
remainingBits -= bitsPerSample;
|
||||
value = (value >> remainingBits) & MASK[bitsPerSample];
|
||||
|
||||
switch (componentSize) {
|
||||
case 1:
|
||||
buffer.put((byte) value);
|
||||
break;
|
||||
case 2:
|
||||
buffer.putShort((short) value);
|
||||
break;
|
||||
case 4:
|
||||
buffer.putInt(value);
|
||||
break;
|
||||
default:
|
||||
// Guarded in constructor
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -45,7 +45,6 @@ import com.twelvemonkeys.imageio.metadata.psd.PSDReader;
|
||||
import com.twelvemonkeys.imageio.metadata.xmp.XMPReader;
|
||||
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
|
||||
import com.twelvemonkeys.imageio.stream.SubImageInputStream;
|
||||
import com.twelvemonkeys.imageio.util.IIOUtil;
|
||||
import com.twelvemonkeys.imageio.util.ImageTypeSpecifiers;
|
||||
import com.twelvemonkeys.imageio.util.ProgressListenerBase;
|
||||
import com.twelvemonkeys.io.FastByteArrayOutputStream;
|
||||
@ -62,7 +61,6 @@ import javax.imageio.metadata.IIOMetadataNode;
|
||||
import javax.imageio.plugins.jpeg.JPEGImageReadParam;
|
||||
import javax.imageio.spi.IIORegistry;
|
||||
import javax.imageio.spi.ImageReaderSpi;
|
||||
import javax.imageio.spi.ServiceRegistry;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.awt.*;
|
||||
import java.awt.color.CMMException;
|
||||
@ -70,8 +68,6 @@ import java.awt.color.ColorSpace;
|
||||
import java.awt.color.ICC_Profile;
|
||||
import java.awt.image.*;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@ -378,6 +374,10 @@ public final class TIFFImageReader extends ImageReaderBase {
|
||||
else if (bitsPerSample == 8 || bitsPerSample == 16 || bitsPerSample == 32) {
|
||||
return ImageTypeSpecifiers.createInterleaved(cs, new int[] {0}, dataType, false, false);
|
||||
}
|
||||
else if (bitsPerSample % 2 == 0) {
|
||||
ColorModel colorModel = new ComponentColorModel(cs, new int[] {bitsPerSample}, false, false, Transparency.OPAQUE, dataType);
|
||||
return new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(1, 1));
|
||||
}
|
||||
|
||||
throw new IIOException(String.format("Unsupported BitsPerSample for Bi-level/Gray TIFF (expected 1, 2, 4, 8, 16 or 32): %d", bitsPerSample));
|
||||
|
||||
@ -442,6 +442,14 @@ public final class TIFFImageReader extends ImageReaderBase {
|
||||
return ImageTypeSpecifiers.createBanded(cs, new int[] {0, 1, 2}, new int[] {0, 0, 0}, dataType, false, false);
|
||||
}
|
||||
}
|
||||
else if (bitsPerSample > 8 && bitsPerSample % 2 == 0) {
|
||||
// TODO: Support variable bits/sample?
|
||||
ColorModel colorModel = new ComponentColorModel(cs, new int[] {bitsPerSample, bitsPerSample, bitsPerSample}, false, false, Transparency.OPAQUE, dataType);
|
||||
SampleModel sampleModel = planarConfiguration == TIFFBaseline.PLANARCONFIG_CHUNKY
|
||||
? colorModel.createCompatibleSampleModel(1, 1)
|
||||
: new BandedSampleModel(dataType, 1, 1, 3, new int[]{0, 1, 2}, new int[]{0, 0, 0});
|
||||
return new ImageTypeSpecifier(colorModel, sampleModel);
|
||||
}
|
||||
case 4:
|
||||
if (bitsPerSample == 8 || bitsPerSample == 16 || bitsPerSample == 32) {
|
||||
switch (planarConfiguration) {
|
||||
@ -892,6 +900,10 @@ public final class TIFFImageReader extends ImageReaderBase {
|
||||
|
||||
// General uncompressed/compressed reading
|
||||
int bands = planarConfiguration == TIFFExtension.PLANARCONFIG_PLANAR ? rawType.getNumBands() : 1;
|
||||
int bitsPerSample = getBitsPerSample();
|
||||
boolean needsBitPadding = bitsPerSample > 16 && bitsPerSample % 16 != 0 || bitsPerSample > 8 && bitsPerSample % 8 != 0 || bitsPerSample == 6;
|
||||
boolean needsAdapter = compression != TIFFBaseline.COMPRESSION_NONE
|
||||
|| interpretation == TIFFExtension.PHOTOMETRIC_YCBCR || needsBitPadding;
|
||||
|
||||
for (int y = 0; y < tilesDown; y++) {
|
||||
int col = 0;
|
||||
@ -906,7 +918,7 @@ public final class TIFFImageReader extends ImageReaderBase {
|
||||
imageInput.seek(stripTileOffsets[i]);
|
||||
|
||||
DataInput input;
|
||||
if (compression == TIFFBaseline.COMPRESSION_NONE && interpretation != TIFFExtension.PHOTOMETRIC_YCBCR) {
|
||||
if (!needsAdapter) {
|
||||
// No need for transformation, fast forward
|
||||
input = imageInput;
|
||||
}
|
||||
@ -916,7 +928,7 @@ public final class TIFFImageReader extends ImageReaderBase {
|
||||
: createStreamAdapter(imageInput);
|
||||
|
||||
adapter = createDecompressorStream(compression, stripTileWidth, numBands, adapter);
|
||||
adapter = createUnpredictorStream(predictor, stripTileWidth, numBands, getBitsPerSample(), adapter, imageInput.getByteOrder());
|
||||
adapter = createUnpredictorStream(predictor, stripTileWidth, numBands, bitsPerSample, adapter, imageInput.getByteOrder());
|
||||
|
||||
if (interpretation == TIFFExtension.PHOTOMETRIC_YCBCR && rowRaster.getTransferType() == DataBuffer.TYPE_BYTE) {
|
||||
adapter = new YCbCrUpsamplerStream(adapter, yCbCrSubsampling, yCbCrPos, colsInTile);
|
||||
@ -929,6 +941,11 @@ public final class TIFFImageReader extends ImageReaderBase {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
if (needsBitPadding) {
|
||||
// We'll pad "odd" bitsPerSample streams to the smallest data type (byte/short/int) larger than the input
|
||||
adapter = new BitPaddingStream(adapter, numBands, bitsPerSample, colsInTile, imageInput.getByteOrder());
|
||||
}
|
||||
|
||||
// According to the spec, short/long/etc should follow order of containing stream
|
||||
input = imageInput.getByteOrder() == ByteOrder.BIG_ENDIAN
|
||||
? new DataInputStream(adapter)
|
||||
@ -1300,8 +1317,6 @@ public final class TIFFImageReader extends ImageReaderBase {
|
||||
|
||||
break;
|
||||
|
||||
// Additionally, the specification defines these values as part of the TIFF extensions:
|
||||
|
||||
// Known, but unsupported compression types
|
||||
case TIFFCustom.COMPRESSION_NEXT:
|
||||
case TIFFCustom.COMPRESSION_CCITTRLEW:
|
||||
|
@ -0,0 +1,186 @@
|
||||
package com.twelvemonkeys.imageio.plugins.tiff;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* BitPaddingStreamTest.
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: harald.kuhr$
|
||||
* @version $Id: BitPaddingStreamTest.java,v 1.0 16/11/2016 harald.kuhr Exp$
|
||||
*/
|
||||
public class BitPaddingStreamTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateNullStream() {
|
||||
new BitPaddingStream(null, 1, 12, 4, ByteOrder.BIG_ENDIAN);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateBadBits() {
|
||||
new BitPaddingStream(new ByteArrayInputStream(new byte[6]), 1, 7, 4, ByteOrder.BIG_ENDIAN);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateBadBitsLarge() {
|
||||
new BitPaddingStream(new ByteArrayInputStream(new byte[6]), 1, 37, 4, ByteOrder.BIG_ENDIAN);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateNullByteOrder() {
|
||||
new BitPaddingStream(new ByteArrayInputStream(new byte[6]), 1, 12, 4, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRead() throws IOException {
|
||||
byte[] bytes = {(byte) 0xff, (byte) 0xf0, 0x00, 0x66, 0x67, (byte) 0x89};
|
||||
|
||||
BitPaddingStream stream = new BitPaddingStream(new ByteArrayInputStream(bytes), 1, 12, 4, ByteOrder.BIG_ENDIAN);
|
||||
assertEquals(0x0f, stream.read());
|
||||
assertEquals(0xff, stream.read());
|
||||
|
||||
assertEquals(0x00, stream.read());
|
||||
assertEquals(0x00, stream.read());
|
||||
|
||||
assertEquals(0x06, stream.read());
|
||||
assertEquals(0x66, stream.read());
|
||||
|
||||
assertEquals(0x07, stream.read());
|
||||
assertEquals(0x89, stream.read());
|
||||
|
||||
assertEquals(-1, stream.read());
|
||||
}
|
||||
|
||||
// TODO: Test read 10, 14, etc bits....
|
||||
|
||||
@Test
|
||||
public void testReadLittleEndian() throws IOException {
|
||||
byte[] bytes = {(byte) 0xff, (byte) 0xf0, 0x00, 0x66, 0x67, (byte) 0x89};
|
||||
|
||||
BitPaddingStream stream = new BitPaddingStream(new ByteArrayInputStream(bytes), 1, 12, 4, ByteOrder.LITTLE_ENDIAN);
|
||||
assertEquals(0xff, stream.read());
|
||||
assertEquals(0x0f, stream.read());
|
||||
|
||||
assertEquals(0x00, stream.read());
|
||||
assertEquals(0x00, stream.read());
|
||||
|
||||
assertEquals(0x66, stream.read());
|
||||
assertEquals(0x06, stream.read());
|
||||
|
||||
assertEquals(0x89, stream.read());
|
||||
assertEquals(0x07, stream.read());
|
||||
|
||||
assertEquals(-1, stream.read());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRead3Components() throws IOException {
|
||||
byte[] bytes = {(byte) 0xff, (byte) 0xf0, 0x00, 0x66, 0x60};
|
||||
|
||||
BitPaddingStream stream = new BitPaddingStream(new ByteArrayInputStream(bytes), 3, 12, 1, ByteOrder.BIG_ENDIAN);
|
||||
assertEquals(0x0f, stream.read());
|
||||
assertEquals(0xff, stream.read());
|
||||
|
||||
assertEquals(0x00, stream.read());
|
||||
assertEquals(0x00, stream.read());
|
||||
|
||||
assertEquals(0x06, stream.read());
|
||||
assertEquals(0x66, stream.read());
|
||||
|
||||
assertEquals(-1, stream.read());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadArray() throws IOException {
|
||||
byte[] bytes = {(byte) 0xff, (byte) 0xf0, 0x00, 0x66, 0x67, (byte) 0x89};
|
||||
|
||||
BitPaddingStream stream = new BitPaddingStream(new ByteArrayInputStream(bytes), 1, 12, 4, ByteOrder.BIG_ENDIAN);
|
||||
|
||||
byte[] result = new byte[8];
|
||||
new DataInputStream(stream).readFully(result);
|
||||
|
||||
assertArrayEquals(new byte[] {0x0f, (byte) 0xff, 0x00, 0x00, 0x06, 0x66, 0x07, (byte) 0x89}, result);
|
||||
|
||||
assertEquals(-1, stream.read());
|
||||
assertEquals(-1, stream.read(new byte[4]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadArrayLittleEndian() throws IOException {
|
||||
byte[] bytes = {(byte) 0xff, (byte) 0xf0, 0x00, 0x66, 0x67, (byte) 0x89};
|
||||
|
||||
BitPaddingStream stream = new BitPaddingStream(new ByteArrayInputStream(bytes), 1, 12, 4, ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
byte[] result = new byte[8];
|
||||
new DataInputStream(stream).readFully(result);
|
||||
|
||||
assertArrayEquals(new byte[] {(byte) 0xff, 0x0f, 0x00, 0x00, 0x66, 0x06, (byte) 0x89, 0x07}, result);
|
||||
|
||||
assertEquals(-1, stream.read());
|
||||
assertEquals(-1, stream.read(new byte[4]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadArray2Components() throws IOException {
|
||||
byte[] bytes = {(byte) 0xff, (byte) 0xf0, 0x00, 0x66, 0x67, (byte) 0x89};
|
||||
|
||||
BitPaddingStream stream = new BitPaddingStream(new ByteArrayInputStream(bytes), 2, 12, 2, ByteOrder.BIG_ENDIAN);
|
||||
|
||||
byte[] result = new byte[8];
|
||||
new DataInputStream(stream).readFully(result);
|
||||
|
||||
assertArrayEquals(new byte[] {0x0f, (byte) 0xff, 0x00, 0x00, 0x06, 0x66, 0x07, (byte) 0x89}, result);
|
||||
|
||||
assertEquals(-1, stream.read());
|
||||
assertEquals(-1, stream.read(new byte[4]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadArray3Components() throws IOException {
|
||||
byte[] bytes = {(byte) 0xff, (byte) 0xf0, 0x00, 0x66, 0x6f};
|
||||
|
||||
BitPaddingStream stream = new BitPaddingStream(new ByteArrayInputStream(bytes), 3, 12, 1, ByteOrder.BIG_ENDIAN);
|
||||
|
||||
byte[] result = new byte[6];
|
||||
new DataInputStream(stream).readFully(result);
|
||||
|
||||
assertArrayEquals(new byte[] {0x0f, (byte) 0xff, 0x00, 0x00, 0x06, 0x66}, result);
|
||||
|
||||
assertEquals(-1, stream.read());
|
||||
assertEquals(-1, stream.read(new byte[4]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadArray4Components() throws IOException {
|
||||
byte[] bytes = {(byte) 0xff, (byte) 0xf0, 0x00, 0x66, 0x67, (byte) 0x89};
|
||||
|
||||
BitPaddingStream stream = new BitPaddingStream(new ByteArrayInputStream(bytes), 4, 12, 1, ByteOrder.BIG_ENDIAN);
|
||||
|
||||
byte[] result = new byte[8];
|
||||
new DataInputStream(stream).readFully(result);
|
||||
|
||||
assertArrayEquals(new byte[] {0x0f, (byte) 0xff, 0x00, 0x00, 0x06, 0x66, 0x07, (byte) 0x89}, result);
|
||||
|
||||
assertEquals(-1, stream.read());
|
||||
assertEquals(-1, stream.read(new byte[4]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkip() throws IOException {
|
||||
byte[] bytes = {(byte) 0xff, (byte) 0xf0, 0x00, 0x66, 0x67, (byte) 0x89};
|
||||
|
||||
BitPaddingStream stream = new BitPaddingStream(new ByteArrayInputStream(bytes), 1, 12, 4, ByteOrder.BIG_ENDIAN);
|
||||
|
||||
assertEquals(4, stream.skip(4)); // Normal skip
|
||||
assertEquals(0x06, stream.read()); // Verify position after skip
|
||||
assertEquals(3, stream.skip(4)); // Partial skip
|
||||
assertEquals(-1, stream.read()); // Verify position (EOF)
|
||||
}
|
||||
}
|
@ -29,6 +29,7 @@ package com.twelvemonkeys.imageio.plugins.tiff;/*
|
||||
import com.twelvemonkeys.imageio.util.ImageReaderAbstractTest;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.imageio.IIOException;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReadParam;
|
||||
import javax.imageio.ImageReader;
|
||||
@ -44,6 +45,7 @@ import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.internal.matchers.StringContains.containsString;
|
||||
import static org.mockito.Matchers.contains;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
@ -109,8 +111,13 @@ public class TIFFImageReaderTest extends ImageReaderAbstractTest<TIFFImageReader
|
||||
// Gray
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-minisblack-02.tif"), new Dimension(73, 43)), // Gray 2 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-minisblack-04.tif"), new Dimension(73, 43)), // Gray 4 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-minisblack-06.tif"), new Dimension(73, 43)), // Gray 6 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-minisblack-08.tif"), new Dimension(73, 43)), // Gray 8 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-minisblack-10.tif"), new Dimension(73, 43)), // Gray 10 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-minisblack-12.tif"), new Dimension(73, 43)), // Gray 12 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-minisblack-14.tif"), new Dimension(73, 43)), // Gray 14 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-minisblack-16.tif"), new Dimension(73, 43)), // Gray 16 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-minisblack-24.tif"), new Dimension(73, 43)), // Gray 24 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-minisblack-32.tif"), new Dimension(73, 43)), // Gray 32 bit/sample
|
||||
// Palette
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-palette-02.tif"), new Dimension(73, 43)), // Palette 2 bit/sample
|
||||
@ -119,12 +126,21 @@ public class TIFFImageReaderTest extends ImageReaderAbstractTest<TIFFImageReader
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-palette-16.tif"), new Dimension(73, 43)), // Palette 16 bit/sample
|
||||
// RGB Interleaved (PlanarConfiguration: 1)
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-contig-08.tif"), new Dimension(73, 43)), // RGB 8 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-contig-10.tif"), new Dimension(73, 43)), // RGB 10 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-contig-12.tif"), new Dimension(73, 43)), // RGB 12 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-contig-14.tif"), new Dimension(73, 43)), // RGB 14 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-contig-16.tif"), new Dimension(73, 43)), // RGB 16 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-contig-24.tif"), new Dimension(73, 43)), // RGB 24 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-contig-32.tif"), new Dimension(73, 43)), // RGB 32 bit/sample
|
||||
// RGB Planar (PlanarConfiguration: 2)
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-planar-08.tif"), new Dimension(73, 43)), // RGB 8 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-planar-10.tif"), new Dimension(73, 43)), // RGB 10 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-planar-12.tif"), new Dimension(73, 43)), // RGB 12 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-planar-14.tif"), new Dimension(73, 43)), // RGB 14 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-planar-16.tif"), new Dimension(73, 43)), // RGB 16 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-planar-32.tif"), new Dimension(73, 43)), // RGB 32 bit FP samples!
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-planar-24.tif"), new Dimension(73, 43)), // RGB 24 bit/sample
|
||||
// RGB Interleaved Floating point..!? We can read this one, but the samples are not normalized, so colors are way too bright...
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-planar-32.tif"), new Dimension(73, 43)), // RGB 32 bit FP samples (!)
|
||||
// Separated (CMYK) Interleaved (PlanarConfiguration: 1)
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-separated-contig-08.tif"), new Dimension(73, 43)), // CMYK 8 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-separated-contig-16.tif"), new Dimension(73, 43)), // CMYK 16 bit/sample
|
||||
@ -134,6 +150,16 @@ public class TIFFImageReaderTest extends ImageReaderAbstractTest<TIFFImageReader
|
||||
);
|
||||
}
|
||||
|
||||
protected List<TestData> getUnsupportedTestData() {
|
||||
return Arrays.asList(
|
||||
// RGB Interleaved (PlanarConfiguration: 1)
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-contig-02.tif"), new Dimension(73, 43)), // RGB 2 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-contig-04.tif"), new Dimension(73, 43)), // RGB 4 bit/sample
|
||||
// RGB Planar (PlanarConfiguration: 2)
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-planar-02.tif"), new Dimension(73, 43)), // RGB 2 bit/sample
|
||||
new TestData(getClassLoaderResource("/tiff/depth/flower-rgb-planar-04.tif"), new Dimension(73, 43)) // RGB 4 bit/sample
|
||||
);
|
||||
}
|
||||
@Override
|
||||
protected ImageReaderSpi createProvider() {
|
||||
return SPI;
|
||||
@ -512,4 +538,26 @@ public class TIFFImageReaderTest extends ImageReaderAbstractTest<TIFFImageReader
|
||||
|
||||
assertSubsampledImageDataEquals("Subsampled image data does not match expected", image, subsampled, param);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUnsupported() {
|
||||
ImageReader reader = createReader();
|
||||
|
||||
for (TestData data : getUnsupportedTestData()) {
|
||||
reader.setInput(data.getInputStream());
|
||||
|
||||
for (int i = 0; i < data.getImageCount(); i++) {
|
||||
try {
|
||||
reader.read(i);
|
||||
fail("Sample should be moved from unsupported to normal test case");
|
||||
}
|
||||
catch (IIOException e) {
|
||||
assertThat(e.getMessage().toLowerCase(), containsString("unsupported"));
|
||||
}
|
||||
catch (Exception e) {
|
||||
failBecause(String.format("Image %s index %s could not be read: %s", data.getInput(), i, e), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user