#292 Now builds on Java 8, 11 and 15.

This commit is contained in:
Harald Kuhr 2020-12-02 22:08:18 +01:00
parent c7d2f422b8
commit 73044bea58
11 changed files with 196 additions and 140 deletions

View File

@ -30,7 +30,7 @@
package com.twelvemonkeys.lang;
import org.junit.Test;
import static org.junit.Assert.*;
import java.awt.*;
import java.sql.Timestamp;
@ -41,7 +41,7 @@ import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* StringUtilTestCase
@ -76,24 +76,24 @@ public class StringUtilTest {
assertNull(StringUtil.valueOf(null));
}
@SuppressWarnings("ConstantConditions")
@Test
public void testToUpperCase() {
String str = StringUtil.toUpperCase(TEST_STRING);
assertNotNull(str);
assertEquals(TEST_STRING.toUpperCase(), str);
str = StringUtil.toUpperCase(null);
assertNull(str);
assertNull(StringUtil.toUpperCase(null));
}
@SuppressWarnings("ConstantConditions")
@Test
public void testToLowerCase() {
String str = StringUtil.toLowerCase(TEST_STRING);
assertNotNull(str);
assertEquals(TEST_STRING.toLowerCase(), str);
str = StringUtil.toLowerCase(null);
assertNull(str);
assertNull(StringUtil.toLowerCase(null));
}
@Test
@ -113,6 +113,7 @@ public class StringUtilTest {
assertFalse(StringUtil.isEmpty(new String[]{WHITESPACE_STRING, TEST_STRING}));
}
@SuppressWarnings("ConstantConditions")
@Test
public void testContains() {
assertTrue(StringUtil.contains(TEST_STRING, TEST_STRING));
@ -145,6 +146,7 @@ public class StringUtilTest {
assertFalse(StringUtil.containsIgnoreCase(null, null));
}
@SuppressWarnings("ConstantConditions")
@Test
public void testContainsChar() {
for (int i = 0; i < TEST_STRING.length(); i++) {
@ -466,7 +468,7 @@ public class StringUtilTest {
assertEquals(TEST_STRING, StringUtil.ltrim(TEST_STRING));
assertEquals(TEST_STRING, StringUtil.ltrim(" " + TEST_STRING));
assertEquals(TEST_STRING, StringUtil.ltrim(WHITESPACE_STRING + TEST_STRING));
assertFalse(TEST_STRING.equals(StringUtil.ltrim(TEST_STRING + WHITESPACE_STRING)));
assertNotEquals(TEST_STRING, StringUtil.ltrim(TEST_STRING + WHITESPACE_STRING));
// TODO: Test is not complete
}
@ -475,7 +477,7 @@ public class StringUtilTest {
assertEquals(TEST_STRING, StringUtil.rtrim(TEST_STRING));
assertEquals(TEST_STRING, StringUtil.rtrim(TEST_STRING + " "));
assertEquals(TEST_STRING, StringUtil.rtrim(TEST_STRING + WHITESPACE_STRING));
assertFalse(TEST_STRING.equals(StringUtil.rtrim(WHITESPACE_STRING + TEST_STRING)));
assertNotEquals(TEST_STRING, StringUtil.rtrim(WHITESPACE_STRING + TEST_STRING));
// TODO: Test is not complete
}
@ -516,7 +518,7 @@ public class StringUtilTest {
public void testCaptialize() {
assertNull(StringUtil.capitalize(null));
assertEquals(TEST_STRING.toUpperCase(), StringUtil.capitalize(TEST_STRING.toUpperCase()));
assertTrue(StringUtil.capitalize("abc").charAt(0) == 'A');
assertEquals('A', StringUtil.capitalize("abc").charAt(0));
}
@Test
@ -552,13 +554,13 @@ public class StringUtilTest {
public void testToDateWithFormatString() {
Calendar cal = new GregorianCalendar();
cal.clear();
cal.set(1976, 2, 16); // Month is 0-based
cal.set(1976, Calendar.MARCH, 16); // Month is 0-based
Date date = StringUtil.toDate("16.03.1976", "dd.MM.yyyy");
assertNotNull(date);
assertEquals(cal.getTime(), date);
cal.clear();
cal.set(2004, 4, 13, 23, 51, 3);
cal.set(2004, Calendar.MAY, 13, 23, 51, 3);
date = StringUtil.toDate("2004-5-13 23:51 (03)", "yyyy-MM-dd hh:mm (ss)");
assertNotNull(date);
assertEquals(cal.getTime(), date);
@ -576,15 +578,15 @@ public class StringUtilTest {
public void testToDateWithFormat() {
Calendar cal = new GregorianCalendar();
cal.clear();
cal.set(1976, 2, 16); // Month is 0-based
cal.set(1976, Calendar.MARCH, 16); // Month is 0-based
Date date = StringUtil.toDate("16.03.1976", new SimpleDateFormat("dd.MM.yyyy"));
assertNotNull(date);
assertEquals(cal.getTime(), date);
cal.clear();
cal.set(2004, 4, 13, 23, 51);
date = StringUtil.toDate("13.5.04 23:51",
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, new Locale("no", "NO")));
cal.set(2004, Calendar.MAY, 13, 23, 51);
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, new Locale("no", "NO"));
date = StringUtil.toDate(format.format(cal.getTime()), format);
assertNotNull(date);
assertEquals(cal.getTime(), date);
@ -601,10 +603,9 @@ public class StringUtilTest {
public void testToTimestamp() {
Calendar cal = new GregorianCalendar();
cal.clear();
cal.set(1976, 2, 16, 21, 28, 4); // Month is 0-based
Date date = StringUtil.toTimestamp("1976-03-16 21:28:04");
cal.set(1976, Calendar.MARCH, 16, 21, 28, 4); // Month is 0-based
Timestamp date = StringUtil.toTimestamp("1976-03-16 21:28:04");
assertNotNull(date);
assertTrue(date instanceof Timestamp);
assertEquals(cal.getTime(), date);
}
@ -821,7 +822,7 @@ public class StringUtilTest {
assertTrue(StringUtil.isNumber("12345"));
assertTrue(StringUtil.isNumber(TEST_INTEGER.toString()));
assertTrue(StringUtil.isNumber("1234567890123456789012345678901234567890"));
assertTrue(StringUtil.isNumber(String.valueOf(Long.MAX_VALUE) + String.valueOf(Long.MAX_VALUE)));
assertTrue(StringUtil.isNumber(String.valueOf(Long.MAX_VALUE) + Long.MAX_VALUE));
assertFalse(StringUtil.isNumber("abc"));
assertFalse(StringUtil.isNumber(TEST_STRING));
}
@ -831,7 +832,7 @@ public class StringUtilTest {
assertTrue(StringUtil.isNumber("-12345"));
assertTrue(StringUtil.isNumber('-' + TEST_INTEGER.toString()));
assertTrue(StringUtil.isNumber("-1234567890123456789012345678901234567890"));
assertTrue(StringUtil.isNumber('-' + String.valueOf(Long.MAX_VALUE) + String.valueOf(Long.MAX_VALUE)));
assertTrue(StringUtil.isNumber('-' + String.valueOf(Long.MAX_VALUE) + Long.MAX_VALUE));
assertFalse(StringUtil.isNumber("-abc"));
assertFalse(StringUtil.isNumber('-' + TEST_STRING));
}

View File

@ -37,7 +37,7 @@ import java.io.DataInput;
import java.io.File;
import java.io.IOException;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import javax.imageio.IIOException;
@ -205,7 +205,7 @@ public final class BMPImageReader extends ImageReaderBase {
checkBounds(pImageIndex);
// TODO: Better implementation, include INT_RGB types for 3BYTE_BGR and 4BYTE_ABGR for INT_ARGB
return Arrays.asList(getRawImageType(pImageIndex)).iterator();
return Collections.singletonList(getRawImageType(pImageIndex)).iterator();
}
@Override
@ -410,6 +410,13 @@ public final class BMPImageReader extends ImageReaderBase {
private ImageReader initReaderDelegate(int compression) throws IOException {
ImageReader reader = getImageReaderDelegate(compression);
reader.reset();
// Install listener
ListenerDelegator listenerDelegator = new ListenerDelegator();
reader.addIIOReadWarningListener(listenerDelegator);
reader.addIIOReadProgressListener(listenerDelegator);
reader.addIIOReadUpdateListener(listenerDelegator);
imageInput.seek(pixelOffset);
reader.setInput(new SubImageInputStream(imageInput, header.getImageSize()));
@ -450,12 +457,6 @@ public final class BMPImageReader extends ImageReaderBase {
ImageReader reader = readers.next();
// Install listener
ListenerDelegator listenerDelegator = new ListenerDelegator();
reader.addIIOReadWarningListener(listenerDelegator);
reader.addIIOReadProgressListener(listenerDelegator);
reader.addIIOReadUpdateListener(listenerDelegator);
// Cache for later use
switch (compression) {
case DIB.COMPRESSION_JPEG:
@ -633,7 +634,8 @@ public final class BMPImageReader extends ImageReaderBase {
return new BMPMetadata(header, colors);
}
public static void main(String[] args) throws IOException {
@SuppressWarnings("ConstantConditions")
public static void main(String[] args) {
BMPImageReaderSpi provider = new BMPImageReaderSpi();
BMPImageReader reader = new BMPImageReader(provider);
@ -686,7 +688,7 @@ public final class BMPImageReader extends ImageReaderBase {
}
}
@SuppressWarnings({"unchecked", "UnusedDeclaration"})
@SuppressWarnings({ "unchecked", "UnusedDeclaration", "SameParameterValue" })
static <T extends Throwable> void throwAs(final Class<T> pType, final Throwable pThrowable) throws T {
throw (T) pThrowable;
}

View File

@ -364,7 +364,11 @@ abstract class DIBHeader {
public String getBMPVersion() {
// This is to be compatible with the native metadata of the original com.sun....BMPMetadata
return compression == DIB.COMPRESSION_BITFIELDS ? "BMP v. 3.x NT" : "BMP v. 3.x";
return size > DIB.BITMAP_INFO_HEADER_SIZE
? "BMP V2/V3 INFO"
: compression == DIB.COMPRESSION_BITFIELDS || compression == DIB.COMPRESSION_ALPHA_BITFIELDS
? "BMP v. 3.x NT"
: "BMP v. 3.x";
}
}

View File

@ -342,6 +342,7 @@ public class BMPImageReaderTest extends ImageReaderAbstractTest<BMPImageReader>
for (TestData data : getTestData()) {
if (data.getInput().toString().contains("pal8offs")) {
// Skip: Contains extra bogus PaletteEntry nodes
continue;
}
@ -358,6 +359,7 @@ public class BMPImageReaderTest extends ImageReaderAbstractTest<BMPImageReader>
System.err.println("WARNING: Reading " + data + " caused exception: " + e.getMessage());
continue;
}
IIOMetadata jreMetadata = jreReader.getImageMetadata(0);
assertTrue(metadata.isStandardMetadataFormatSupported());
@ -370,6 +372,7 @@ public class BMPImageReaderTest extends ImageReaderAbstractTest<BMPImageReader>
String absolutePath = data.toString();
String localPath = absolutePath.substring(absolutePath.lastIndexOf("test-classes") + 12);
// TODO: blauesglas_16_bitmask444 fails BMP Version for 11+
Node expectedTree = jreMetadata.getAsTree(format);
Node actualTree = metadata.getAsTree(format);
@ -428,6 +431,7 @@ public class BMPImageReaderTest extends ImageReaderAbstractTest<BMPImageReader>
}
}
@SuppressWarnings("RedundantIfStatement")
private boolean excludeEqualValueTest(final Node expected) {
if (expected.getLocalName().equals("ImageSize")) {
// JRE metadata returns 0, even if known in reader...

View File

@ -61,7 +61,7 @@ public final class UInt32ColorModel extends ComponentColorModel {
// This class only supports DataBuffer.TYPE_INT, cast is safe
int[] ipixel = (int[]) pixel;
for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
normComponents[nc] = ((float) (ipixel[c] & 0xffffffffl)) / ((float) ((1l << getComponentSize(c)) - 1));
normComponents[nc] = ((float) (ipixel[c] & 0xFFFFFFFFL)) / ((float) ((1L << getComponentSize(c)) - 1));
}
int numColorComponents = getNumColorComponents();

View File

@ -30,15 +30,22 @@
package com.twelvemonkeys.imageio.util;
import com.twelvemonkeys.imageio.color.DiscreteAlphaIndexColorModel;
import javax.imageio.ImageTypeSpecifier;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import static com.twelvemonkeys.lang.Validate.isTrue;
import static com.twelvemonkeys.lang.Validate.notNull;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DirectColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.MultiPixelPackedSampleModel;
import java.awt.image.SampleModel;
import javax.imageio.ImageTypeSpecifier;
import com.twelvemonkeys.imageio.color.DiscreteAlphaIndexColorModel;
/**
* Factory class for creating {@code ImageTypeSpecifier}s.
* Fixes some subtle bugs in {@code ImageTypeSpecifier}'s factory methods, but
@ -169,21 +176,36 @@ public final class ImageTypeSpecifiers {
int numEntries = 1 << bits;
byte[] r = new byte[numEntries];
byte[] g = new byte[numEntries];
byte[] b = new byte[numEntries];
ColorModel colorModel;
// Scale array values according to color profile..
for (int i = 0; i < numEntries; i++) {
float[] gray = new float[]{i / (float) (numEntries - 1)};
float[] rgb = colorSpace.toRGB(gray);
if (ColorSpace.getInstance(ColorSpace.CS_GRAY).equals(colorSpace)) {
// For default gray, use linear response
byte[] gray = new byte[numEntries];
r[i] = (byte) (rgb[0] * 255);
g[i] = (byte) (rgb[1] * 255);
b[i] = (byte) (rgb[2] * 255);
for (int i = 0; i < numEntries; i++) {
gray[i] = (byte) ((i * 255) / (numEntries - 1));
}
colorModel = new IndexColorModel(bits, numEntries, gray, gray, gray);
}
else {
byte[] r = new byte[numEntries];
byte[] g = new byte[numEntries];
byte[] b = new byte[numEntries];
// Scale array values according to color profile..
for (int i = 0; i < numEntries; i++) {
float[] gray = new float[] { i / (float) (numEntries - 1) };
float[] rgb = colorSpace.toRGB(gray);
r[i] = (byte) Math.round(rgb[0] * 255);
g[i] = (byte) Math.round(rgb[1] * 255);
b[i] = (byte) Math.round(rgb[2] * 255);
}
colorModel = new IndexColorModel(bits, numEntries, r, g, b);
}
ColorModel colorModel = new IndexColorModel(bits, numEntries, r, g, b);
SampleModel sampleModel = new MultiPixelPackedSampleModel(dataType, 1, 1, bits);
return new ImageTypeSpecifier(colorModel, sampleModel);
@ -201,7 +223,7 @@ public final class ImageTypeSpecifiers {
}
public static ImageTypeSpecifier createFromIndexColorModel(final IndexColorModel pColorModel) {
return IndexedImageTypeSpecifier.createFromIndexColorModel(pColorModel);
return new IndexedImageTypeSpecifier(pColorModel);
}
public static ImageTypeSpecifier createDiscreteAlphaIndexedFromIndexColorModel(final IndexColorModel pColorModel) {

View File

@ -30,14 +30,14 @@
package com.twelvemonkeys.imageio.util;
import javax.imageio.ImageTypeSpecifier;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.util.Hashtable;
import static com.twelvemonkeys.lang.Validate.notNull;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import javax.imageio.ImageTypeSpecifier;
/**
* IndexedImageTypeSpecifier
*
@ -45,27 +45,24 @@ 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 {
private IndexedImageTypeSpecifier() {}
final class IndexedImageTypeSpecifier extends ImageTypeSpecifier {
IndexedImageTypeSpecifier(final ColorModel colorModel) {
// For some reason, we need a sample model, even though we won't use it
super(notNull(colorModel, "colorModel"), colorModel.createCompatibleSampleModel(1, 1));
}
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!");
}
}
};
@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(), null);
}
catch (NegativeArraySizeException e) {
// Exception most likely thrown from a DataBuffer constructor
throw new IllegalArgumentException("Array size > Integer.MAX_VALUE!");
}
}
}

View File

@ -30,15 +30,16 @@
package com.twelvemonkeys.imageio.util;
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;
import javax.imageio.ImageTypeSpecifier;
import com.twelvemonkeys.imageio.color.UInt32ColorModel;
/**
* ImageTypeSpecifier for interleaved 32 bit unsigned integral samples.
*
@ -47,11 +48,13 @@ import java.awt.image.SampleModel;
* @author last modified by $Author: haraldk$
* @version $Id: UInt32ImageTypeSpecifier.java,v 1.0 24.01.11 17.51 haraldk Exp$
*/
final class UInt32ImageTypeSpecifier {
private UInt32ImageTypeSpecifier() {}
final class UInt32ImageTypeSpecifier extends ImageTypeSpecifier {
private UInt32ImageTypeSpecifier(final ColorSpace cs, final boolean hasAlpha, final boolean isAlphaPremultiplied, final SampleModel sampleModel) {
super(new UInt32ColorModel(cs, hasAlpha, isAlphaPremultiplied), sampleModel);
}
static ImageTypeSpecifier createInterleaved(final ColorSpace cs, final int[] bandOffsets, final boolean hasAlpha, final boolean isAlphaPremultiplied) {
return create(
return new UInt32ImageTypeSpecifier(
cs, hasAlpha, isAlphaPremultiplied,
new PixelInterleavedSampleModel(
DataBuffer.TYPE_INT, 1, 1,
@ -63,7 +66,7 @@ final class UInt32ImageTypeSpecifier {
}
static ImageTypeSpecifier createBanded(final ColorSpace cs, final int[] bandIndices, final int[] bandOffsets, final boolean hasAlpha, final boolean isAlphaPremultiplied) {
return create(
return new UInt32ImageTypeSpecifier(
cs, hasAlpha, isAlphaPremultiplied,
new BandedSampleModel(
DataBuffer.TYPE_INT, 1, 1, 1,
@ -72,7 +75,13 @@ final class UInt32ImageTypeSpecifier {
);
}
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);
@Override
public boolean equals(final Object other) {
if (!(other instanceof UInt32ImageTypeSpecifier)) {
return false;
}
UInt32ImageTypeSpecifier that = (UInt32ImageTypeSpecifier) other;
return colorModel.equals(that.colorModel) && sampleModel.equals(that.sampleModel);
}
}

View File

@ -30,14 +30,20 @@
package com.twelvemonkeys.imageio.util;
import com.twelvemonkeys.lang.Validate;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DirectColorModel;
import java.awt.image.IndexColorModel;
import javax.imageio.ImageTypeSpecifier;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.twelvemonkeys.lang.Validate;
public class ImageTypeSpecifiersTest {
@ -541,8 +547,7 @@ public class ImageTypeSpecifiersTest {
}
@Test
public void testCreatePackedGrayscale1() {
// TODO: Fails on Java 11, because IndexColorModel now has an overloaded equals that actually tests the color entries
public void testCreatePackedGrayscale1BPP() {
assertEquals(
ImageTypeSpecifier.createGrayscale(1, DataBuffer.TYPE_BYTE, false),
ImageTypeSpecifiers.createPackedGrayscale(GRAY, 1, DataBuffer.TYPE_BYTE)
@ -550,8 +555,8 @@ public class ImageTypeSpecifiersTest {
}
@Test
public void testCreatePackedGrayscale2() {
// TODO: Fails on Java 11, because IndexColorModel now has an overloaded equals that actually tests the color entries
public void testCreatePackedGrayscale2BPP() {
// TODO: Fails on Java 11+, because IndexColorModel now has an overloaded equals that actually tests the color entries
assertEquals(
ImageTypeSpecifier.createGrayscale(2, DataBuffer.TYPE_BYTE, false),
ImageTypeSpecifiers.createPackedGrayscale(GRAY, 2, DataBuffer.TYPE_BYTE)
@ -559,8 +564,8 @@ public class ImageTypeSpecifiersTest {
}
@Test
public void testCreatePackedGrayscale4() throws Exception {
// TODO: Fails on Java 11, because IndexColorModel now has an overloaded equals that actually tests the color entries
public void testCreatePackedGrayscale4BPP() {
// TODO: Fails on Java 11+, because IndexColorModel now has an overloaded equals that actually tests the color entries
assertEquals(
ImageTypeSpecifier.createGrayscale(4, DataBuffer.TYPE_BYTE, false),
ImageTypeSpecifiers.createPackedGrayscale(GRAY, 4, DataBuffer.TYPE_BYTE)
@ -653,7 +658,7 @@ public class ImageTypeSpecifiersTest {
for (int bits = 1; bits <= 8; bits <<= 1) {
int[] colors = createIntLut(1 << bits);
assertEquals(
IndexedImageTypeSpecifier.createFromIndexColorModel(new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE)),
new IndexedImageTypeSpecifier(new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE)),
ImageTypeSpecifiers.createIndexed(colors, false, -1, bits, DataBuffer.TYPE_BYTE)
);
}
@ -663,7 +668,7 @@ public class ImageTypeSpecifiersTest {
public void testCreateIndexedIntArray16() {
int[] colors = createIntLut(1 << 16);
assertEquals(
IndexedImageTypeSpecifier.createFromIndexColorModel(new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT)),
new IndexedImageTypeSpecifier(new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT)),
ImageTypeSpecifiers.createIndexed(colors, false, -1, 16, DataBuffer.TYPE_USHORT)
);
@ -675,7 +680,7 @@ public class ImageTypeSpecifiersTest {
int[] colors = createIntLut(1 << bits);
IndexColorModel colorModel = new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE);
assertEquals(
IndexedImageTypeSpecifier.createFromIndexColorModel(colorModel),
new IndexedImageTypeSpecifier(colorModel),
ImageTypeSpecifiers.createFromIndexColorModel(colorModel)
);
}
@ -686,7 +691,7 @@ public class ImageTypeSpecifiersTest {
int[] colors = createIntLut(1 << 16);
IndexColorModel colorModel = new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT);
assertEquals(
IndexedImageTypeSpecifier.createFromIndexColorModel(colorModel),
new IndexedImageTypeSpecifier(colorModel),
ImageTypeSpecifiers.createFromIndexColorModel(colorModel)
);
}

View File

@ -30,14 +30,17 @@
package com.twelvemonkeys.imageio.util;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import javax.imageio.ImageTypeSpecifier;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel;
import static org.junit.Assert.*;
import javax.imageio.ImageTypeSpecifier;
import org.junit.Test;
/**
* IndexedImageTypeSpecifierTestCase
@ -51,46 +54,43 @@ public class IndexedImageTypeSpecifierTest {
public void testEquals() {
IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 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));
ImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
ImageTypeSpecifier other = new IndexedImageTypeSpecifier(cm);
ImageTypeSpecifier different = new IndexedImageTypeSpecifier(new IndexColorModel(2, 2, new int[]{0xff00ff, 0x00, 0xff00ff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE));
assertEquals(spec, other);
assertEquals(other, spec);
assertEquals(spec.hashCode(), other.hashCode());
assertTrue(spec.equals(other));
assertTrue(other.equals(spec));
// TODO: There is still a problem that IndexColorModel does not override equals,
// so any model with the same number of bits, transparency, and transfer type will be treated as equal
assertFalse(other.equals(different));
assertNotEquals(other, different);
}
@Test
public void testHashCode() {
IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 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));
ImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
ImageTypeSpecifier other = new IndexedImageTypeSpecifier(cm);
ImageTypeSpecifier different = new IndexedImageTypeSpecifier(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
assertEquals(spec.hashCode(), other.hashCode());
assertFalse(spec.hashCode() == different.hashCode());
assertNotEquals(spec.hashCode(), different.hashCode());
}
@Test(expected = IllegalArgumentException.class)
public void testCreateNull() {
IndexedImageTypeSpecifier.createFromIndexColorModel(null);
new IndexedImageTypeSpecifier(null);
}
@Test
public void testCreateBufferedImageBinary() {
IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE);
ImageTypeSpecifier spec = IndexedImageTypeSpecifier.createFromIndexColorModel(cm);
ImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
BufferedImage image = spec.createBufferedImage(2, 2);
@ -102,7 +102,7 @@ public class IndexedImageTypeSpecifierTest {
@Test
public void testCreateBufferedImageIndexed() {
IndexColorModel cm = new IndexColorModel(8, 256, new int[256], 0, false, -1, DataBuffer.TYPE_BYTE);
ImageTypeSpecifier spec = IndexedImageTypeSpecifier.createFromIndexColorModel(cm);
ImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
BufferedImage image = spec.createBufferedImage(2, 2);

View File

@ -30,26 +30,16 @@
package com.twelvemonkeys.imageio.plugins.jpeg;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
import com.twelvemonkeys.imageio.util.IIOUtil;
import com.twelvemonkeys.imageio.util.ImageWriterAbstractTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.w3c.dom.NodeList;
import javax.imageio.*;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataFormatImpl;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_Profile;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.RenderedImage;
@ -61,10 +51,29 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataFormatImpl;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import org.junit.Test;
import org.w3c.dom.NodeList;
import com.twelvemonkeys.imageio.color.ColorSpaces;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
import com.twelvemonkeys.imageio.util.IIOUtil;
import com.twelvemonkeys.imageio.util.ImageWriterAbstractTest;
/**
* JPEGImageWriterTest
@ -85,13 +94,16 @@ public class JPEGImageWriterTest extends ImageWriterAbstractTest<JPEGImageWriter
@Override
protected List<? extends RenderedImage> getTestData() {
ColorModel cmyk = new ComponentColorModel(ColorSpaces.getColorSpace(ColorSpaces.CS_GENERIC_CMYK), false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
return Arrays.asList(
new BufferedImage(320, 200, BufferedImage.TYPE_3BYTE_BGR),
new BufferedImage(32, 20, BufferedImage.TYPE_INT_RGB),
new BufferedImage(32, 20, BufferedImage.TYPE_INT_BGR),
new BufferedImage(32, 20, BufferedImage.TYPE_INT_ARGB),
new BufferedImage(32, 20, BufferedImage.TYPE_4BYTE_ABGR),
new BufferedImage(32, 20, BufferedImage.TYPE_BYTE_GRAY)
// Java 11+ no longer supports RGBA JPEG
// new BufferedImage(32, 20, BufferedImage.TYPE_INT_ARGB),
// new BufferedImage(32, 20, BufferedImage.TYPE_4BYTE_ABGR),
new BufferedImage(32, 20, BufferedImage.TYPE_BYTE_GRAY),
new BufferedImage(cmyk, cmyk.createCompatibleWritableRaster(32, 20), cmyk.isAlphaPremultiplied(), null)
);
}