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

View File

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

View File

@ -364,7 +364,11 @@ abstract class DIBHeader {
public String getBMPVersion() { public String getBMPVersion() {
// This is to be compatible with the native metadata of the original com.sun....BMPMetadata // 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()) { for (TestData data : getTestData()) {
if (data.getInput().toString().contains("pal8offs")) { if (data.getInput().toString().contains("pal8offs")) {
// Skip: Contains extra bogus PaletteEntry nodes
continue; continue;
} }
@ -358,6 +359,7 @@ public class BMPImageReaderTest extends ImageReaderAbstractTest<BMPImageReader>
System.err.println("WARNING: Reading " + data + " caused exception: " + e.getMessage()); System.err.println("WARNING: Reading " + data + " caused exception: " + e.getMessage());
continue; continue;
} }
IIOMetadata jreMetadata = jreReader.getImageMetadata(0); IIOMetadata jreMetadata = jreReader.getImageMetadata(0);
assertTrue(metadata.isStandardMetadataFormatSupported()); assertTrue(metadata.isStandardMetadataFormatSupported());
@ -370,6 +372,7 @@ public class BMPImageReaderTest extends ImageReaderAbstractTest<BMPImageReader>
String absolutePath = data.toString(); String absolutePath = data.toString();
String localPath = absolutePath.substring(absolutePath.lastIndexOf("test-classes") + 12); String localPath = absolutePath.substring(absolutePath.lastIndexOf("test-classes") + 12);
// TODO: blauesglas_16_bitmask444 fails BMP Version for 11+
Node expectedTree = jreMetadata.getAsTree(format); Node expectedTree = jreMetadata.getAsTree(format);
Node actualTree = metadata.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) { private boolean excludeEqualValueTest(final Node expected) {
if (expected.getLocalName().equals("ImageSize")) { if (expected.getLocalName().equals("ImageSize")) {
// JRE metadata returns 0, even if known in reader... // 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 // This class only supports DataBuffer.TYPE_INT, cast is safe
int[] ipixel = (int[]) pixel; int[] ipixel = (int[]) pixel;
for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) { 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(); int numColorComponents = getNumColorComponents();

View File

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

View File

@ -30,14 +30,14 @@
package com.twelvemonkeys.imageio.util; 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 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 * IndexedImageTypeSpecifier
* *
@ -45,27 +45,24 @@ import static com.twelvemonkeys.lang.Validate.notNull;
* @author last modified by $Author: haraldk$ * @author last modified by $Author: haraldk$
* @version $Id: IndexedImageTypeSpecifier.java,v 1.0 May 19, 2008 11:04:28 AM haraldk Exp$ * @version $Id: IndexedImageTypeSpecifier.java,v 1.0 May 19, 2008 11:04:28 AM haraldk Exp$
*/ */
final class IndexedImageTypeSpecifier { final class IndexedImageTypeSpecifier extends ImageTypeSpecifier {
private IndexedImageTypeSpecifier() {} 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) { @Override
// For some reason, we need a sample model public final BufferedImage createBufferedImage(final int pWidth, final int pHeight) {
return new ImageTypeSpecifier(notNull(pColorModel, "colorModel"), pColorModel.createCompatibleSampleModel(1, 1)) { try {
// This is a fix for the super-method, that first creates a sample model, and then
@Override // creates a raster from it, using Raster.createWritableRaster. The problem with
public final BufferedImage createBufferedImage(final int pWidth, final int pHeight) { // that approach, is that it always creates a TYPE_CUSTOM BufferedImage for indexed images.
try { WritableRaster raster = colorModel.createCompatibleWritableRaster(pWidth, pHeight);
// This is a fix for the super-method, that first creates a sample model, and then return new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
// 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. catch (NegativeArraySizeException e) {
WritableRaster raster = colorModel.createCompatibleWritableRaster(pWidth, pHeight); // Exception most likely thrown from a DataBuffer constructor
return new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), new Hashtable()); throw new IllegalArgumentException("Array size > Integer.MAX_VALUE!");
} }
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; package com.twelvemonkeys.imageio.util;
import com.twelvemonkeys.imageio.color.UInt32ColorModel;
import javax.imageio.ImageTypeSpecifier;
import java.awt.color.ColorSpace; import java.awt.color.ColorSpace;
import java.awt.image.BandedSampleModel; import java.awt.image.BandedSampleModel;
import java.awt.image.DataBuffer; import java.awt.image.DataBuffer;
import java.awt.image.PixelInterleavedSampleModel; import java.awt.image.PixelInterleavedSampleModel;
import java.awt.image.SampleModel; import java.awt.image.SampleModel;
import javax.imageio.ImageTypeSpecifier;
import com.twelvemonkeys.imageio.color.UInt32ColorModel;
/** /**
* ImageTypeSpecifier for interleaved 32 bit unsigned integral samples. * ImageTypeSpecifier for interleaved 32 bit unsigned integral samples.
* *
@ -47,11 +48,13 @@ import java.awt.image.SampleModel;
* @author last modified by $Author: haraldk$ * @author last modified by $Author: haraldk$
* @version $Id: UInt32ImageTypeSpecifier.java,v 1.0 24.01.11 17.51 haraldk Exp$ * @version $Id: UInt32ImageTypeSpecifier.java,v 1.0 24.01.11 17.51 haraldk Exp$
*/ */
final class UInt32ImageTypeSpecifier { final class UInt32ImageTypeSpecifier extends ImageTypeSpecifier {
private UInt32ImageTypeSpecifier() {} 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) { static ImageTypeSpecifier createInterleaved(final ColorSpace cs, final int[] bandOffsets, final boolean hasAlpha, final boolean isAlphaPremultiplied) {
return create( return new UInt32ImageTypeSpecifier(
cs, hasAlpha, isAlphaPremultiplied, cs, hasAlpha, isAlphaPremultiplied,
new PixelInterleavedSampleModel( new PixelInterleavedSampleModel(
DataBuffer.TYPE_INT, 1, 1, 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) { 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, cs, hasAlpha, isAlphaPremultiplied,
new BandedSampleModel( new BandedSampleModel(
DataBuffer.TYPE_INT, 1, 1, 1, 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) { @Override
return new ImageTypeSpecifier(new UInt32ColorModel(cs, hasAlpha, isAlphaPremultiplied), sampleModel); 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; package com.twelvemonkeys.imageio.util;
import com.twelvemonkeys.lang.Validate; import static org.junit.Assert.assertEquals;
import org.junit.Test;
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 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 { public class ImageTypeSpecifiersTest {
@ -541,8 +547,7 @@ public class ImageTypeSpecifiersTest {
} }
@Test @Test
public void testCreatePackedGrayscale1() { public void testCreatePackedGrayscale1BPP() {
// TODO: Fails on Java 11, because IndexColorModel now has an overloaded equals that actually tests the color entries
assertEquals( assertEquals(
ImageTypeSpecifier.createGrayscale(1, DataBuffer.TYPE_BYTE, false), ImageTypeSpecifier.createGrayscale(1, DataBuffer.TYPE_BYTE, false),
ImageTypeSpecifiers.createPackedGrayscale(GRAY, 1, DataBuffer.TYPE_BYTE) ImageTypeSpecifiers.createPackedGrayscale(GRAY, 1, DataBuffer.TYPE_BYTE)
@ -550,8 +555,8 @@ public class ImageTypeSpecifiersTest {
} }
@Test @Test
public void testCreatePackedGrayscale2() { public void testCreatePackedGrayscale2BPP() {
// TODO: Fails on Java 11, because IndexColorModel now has an overloaded equals that actually tests the color entries // TODO: Fails on Java 11+, because IndexColorModel now has an overloaded equals that actually tests the color entries
assertEquals( assertEquals(
ImageTypeSpecifier.createGrayscale(2, DataBuffer.TYPE_BYTE, false), ImageTypeSpecifier.createGrayscale(2, DataBuffer.TYPE_BYTE, false),
ImageTypeSpecifiers.createPackedGrayscale(GRAY, 2, DataBuffer.TYPE_BYTE) ImageTypeSpecifiers.createPackedGrayscale(GRAY, 2, DataBuffer.TYPE_BYTE)
@ -559,8 +564,8 @@ public class ImageTypeSpecifiersTest {
} }
@Test @Test
public void testCreatePackedGrayscale4() throws Exception { public void testCreatePackedGrayscale4BPP() {
// TODO: Fails on Java 11, because IndexColorModel now has an overloaded equals that actually tests the color entries // TODO: Fails on Java 11+, because IndexColorModel now has an overloaded equals that actually tests the color entries
assertEquals( assertEquals(
ImageTypeSpecifier.createGrayscale(4, DataBuffer.TYPE_BYTE, false), ImageTypeSpecifier.createGrayscale(4, DataBuffer.TYPE_BYTE, false),
ImageTypeSpecifiers.createPackedGrayscale(GRAY, 4, DataBuffer.TYPE_BYTE) ImageTypeSpecifiers.createPackedGrayscale(GRAY, 4, DataBuffer.TYPE_BYTE)
@ -653,7 +658,7 @@ public class ImageTypeSpecifiersTest {
for (int bits = 1; bits <= 8; bits <<= 1) { for (int bits = 1; bits <= 8; bits <<= 1) {
int[] colors = createIntLut(1 << bits); int[] colors = createIntLut(1 << bits);
assertEquals( 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) ImageTypeSpecifiers.createIndexed(colors, false, -1, bits, DataBuffer.TYPE_BYTE)
); );
} }
@ -663,7 +668,7 @@ public class ImageTypeSpecifiersTest {
public void testCreateIndexedIntArray16() { public void testCreateIndexedIntArray16() {
int[] colors = createIntLut(1 << 16); int[] colors = createIntLut(1 << 16);
assertEquals( 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) ImageTypeSpecifiers.createIndexed(colors, false, -1, 16, DataBuffer.TYPE_USHORT)
); );
@ -675,7 +680,7 @@ public class ImageTypeSpecifiersTest {
int[] colors = createIntLut(1 << bits); int[] colors = createIntLut(1 << bits);
IndexColorModel colorModel = new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE); IndexColorModel colorModel = new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE);
assertEquals( assertEquals(
IndexedImageTypeSpecifier.createFromIndexColorModel(colorModel), new IndexedImageTypeSpecifier(colorModel),
ImageTypeSpecifiers.createFromIndexColorModel(colorModel) ImageTypeSpecifiers.createFromIndexColorModel(colorModel)
); );
} }
@ -686,7 +691,7 @@ public class ImageTypeSpecifiersTest {
int[] colors = createIntLut(1 << 16); int[] colors = createIntLut(1 << 16);
IndexColorModel colorModel = new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT); IndexColorModel colorModel = new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT);
assertEquals( assertEquals(
IndexedImageTypeSpecifier.createFromIndexColorModel(colorModel), new IndexedImageTypeSpecifier(colorModel),
ImageTypeSpecifiers.createFromIndexColorModel(colorModel) ImageTypeSpecifiers.createFromIndexColorModel(colorModel)
); );
} }

View File

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

View File

@ -30,26 +30,16 @@
package com.twelvemonkeys.imageio.plugins.jpeg; package com.twelvemonkeys.imageio.plugins.jpeg;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream; import static org.junit.Assert.assertEquals;
import com.twelvemonkeys.imageio.util.IIOUtil; import static org.junit.Assert.assertNotNull;
import com.twelvemonkeys.imageio.util.ImageWriterAbstractTest; 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.*;
import java.awt.color.ColorSpace; import java.awt.color.ColorSpace;
import java.awt.color.ICC_Profile; import java.awt.color.ICC_Profile;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel; import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer; import java.awt.image.DataBuffer;
import java.awt.image.RenderedImage; import java.awt.image.RenderedImage;
@ -61,10 +51,29 @@ import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import javax.imageio.IIOImage;
import static org.junit.Assert.assertNotNull; import javax.imageio.ImageIO;
import static org.mockito.Mockito.mock; import javax.imageio.ImageReadParam;
import static org.mockito.Mockito.when; 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 * JPEGImageWriterTest
@ -85,13 +94,16 @@ public class JPEGImageWriterTest extends ImageWriterAbstractTest<JPEGImageWriter
@Override @Override
protected List<? extends RenderedImage> getTestData() { 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( return Arrays.asList(
new BufferedImage(320, 200, BufferedImage.TYPE_3BYTE_BGR), new BufferedImage(320, 200, BufferedImage.TYPE_3BYTE_BGR),
new BufferedImage(32, 20, BufferedImage.TYPE_INT_RGB), new BufferedImage(32, 20, BufferedImage.TYPE_INT_RGB),
new BufferedImage(32, 20, BufferedImage.TYPE_INT_BGR), new BufferedImage(32, 20, BufferedImage.TYPE_INT_BGR),
new BufferedImage(32, 20, BufferedImage.TYPE_INT_ARGB), // Java 11+ no longer supports RGBA JPEG
new BufferedImage(32, 20, BufferedImage.TYPE_4BYTE_ABGR), // new BufferedImage(32, 20, BufferedImage.TYPE_INT_ARGB),
new BufferedImage(32, 20, BufferedImage.TYPE_BYTE_GRAY) // 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)
); );
} }