mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-02 11:05:29 -04:00
TMI-TIFF: Implemented workaround for a bug in ImageReader.getDestination() + added test case for ImageReaderBase.
This commit is contained in:
parent
dd7be5ef11
commit
b8ff4af178
@ -55,6 +55,8 @@ import java.util.Iterator;
|
||||
*/
|
||||
public abstract class ImageReaderBase extends ImageReader {
|
||||
|
||||
private static final Point ORIGIN = new Point(0, 0);
|
||||
|
||||
/**
|
||||
* For convenience. Only set if the input is an {@code ImageInputStream}.
|
||||
* @see #setInput(Object, boolean, boolean)
|
||||
@ -194,50 +196,56 @@ public abstract class ImageReaderBase extends ImageReader {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@code BufferedImage} to which decoded pixel
|
||||
* data should be written.
|
||||
* Returns the {@code BufferedImage} to which decoded pixel data should be written.
|
||||
* <p/>
|
||||
* As {@link javax.imageio.ImageReader#getDestination} but tests if the explicit destination
|
||||
* image (if set) is valid according to the {@code ImageTypeSpecifier}s given in {@code pTypes}
|
||||
* image (if set) is valid according to the {@code ImageTypeSpecifier}s given in {@code types}.
|
||||
*
|
||||
*
|
||||
* @param pParam an {@code ImageReadParam} to be used to get
|
||||
* @param param an {@code ImageReadParam} to be used to get
|
||||
* the destination image or image type, or {@code null}.
|
||||
* @param pTypes an {@code Iterator} of
|
||||
* @param types an {@code Iterator} of
|
||||
* {@code ImageTypeSpecifier}s indicating the legal image
|
||||
* types, with the default first.
|
||||
* @param pWidth the true width of the image or tile begin decoded.
|
||||
* @param pHeight the true width of the image or tile being decoded.
|
||||
* @param width the true width of the image or tile begin decoded.
|
||||
* @param height the true width of the image or tile being decoded.
|
||||
*
|
||||
* @return the {@code BufferedImage} to which decoded pixel
|
||||
* data should be written.
|
||||
*
|
||||
* @exception IIOException if the {@code ImageTypeSpecifier} or {@code BufferedImage}
|
||||
* specified by {@code pParam} does not match any of the legal
|
||||
* ones from {@code pTypes}.
|
||||
* @throws IllegalArgumentException if {@code pTypes}
|
||||
* @exception javax.imageio.IIOException if the {@code ImageTypeSpecifier} or {@code BufferedImage}
|
||||
* specified by {@code param} does not match any of the legal
|
||||
* ones from {@code types}.
|
||||
* @throws IllegalArgumentException if {@code types}
|
||||
* is {@code null} or empty, or if an object not of type
|
||||
* {@code ImageTypeSpecifier} is retrieved from it.
|
||||
* Or, if the resulting image would have a width or height less than 1,
|
||||
* or if the product of {@code pWidth} and {@code pHeight} is greater than
|
||||
* or if the product of {@code width} and {@code height} of the resulting image is greater than
|
||||
* {@code Integer.MAX_VALUE}.
|
||||
*/
|
||||
public static BufferedImage getDestination(final ImageReadParam pParam, final Iterator<ImageTypeSpecifier> pTypes,
|
||||
final int pWidth, final int pHeight) throws IIOException {
|
||||
BufferedImage image = ImageReader.getDestination(pParam, pTypes, pWidth, pHeight);
|
||||
public static BufferedImage getDestination(final ImageReadParam param, final Iterator<ImageTypeSpecifier> types,
|
||||
final int width, final int height) throws IIOException {
|
||||
// Adapted from http://java.net/jira/secure/attachment/29712/TIFFImageReader.java.patch,
|
||||
// to allow reading parts/tiles of huge images.
|
||||
|
||||
if (types == null || !types.hasNext()) {
|
||||
throw new IllegalArgumentException("imageTypes null or empty!");
|
||||
}
|
||||
|
||||
ImageTypeSpecifier imageType = null;
|
||||
|
||||
// If param is non-null, use it
|
||||
if (param != null) {
|
||||
// Try to get the explicit destinaton image
|
||||
BufferedImage dest = param.getDestination();
|
||||
|
||||
if (pParam != null) {
|
||||
BufferedImage dest = pParam.getDestination();
|
||||
if (dest != null) {
|
||||
boolean found = false;
|
||||
|
||||
// NOTE: This is bad, as it relies on implementation details of "super" method...
|
||||
// We know that the iterator has not been touched if explicit destination..
|
||||
while (pTypes.hasNext()) {
|
||||
ImageTypeSpecifier specifier = pTypes.next();
|
||||
int imageType = specifier.getBufferedImageType();
|
||||
while (types.hasNext()) {
|
||||
ImageTypeSpecifier specifier = types.next();
|
||||
int bufferedImageType = specifier.getBufferedImageType();
|
||||
|
||||
if (imageType != 0 && imageType == dest.getType()) {
|
||||
if (bufferedImageType != 0 && bufferedImageType == dest.getType()) {
|
||||
// Known types equal, perfect match
|
||||
found = true;
|
||||
break;
|
||||
@ -256,12 +264,50 @@ public abstract class ImageReaderBase extends ImageReader {
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
throw new IIOException(String.format("Illegal explicit destination image %s", dest));
|
||||
throw new IIOException(String.format("Destination image from ImageReadParam does not match legal imageTypes from reader: %s", dest));
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
// No image, get the image type
|
||||
imageType = param.getDestinationType();
|
||||
}
|
||||
|
||||
// No info from param, use fallback image type
|
||||
if (imageType == null) {
|
||||
imageType = types.next();
|
||||
}
|
||||
else {
|
||||
boolean foundIt = false;
|
||||
|
||||
while (types.hasNext()) {
|
||||
ImageTypeSpecifier type = types.next();
|
||||
|
||||
if (type.equals(imageType)) {
|
||||
foundIt = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!foundIt) {
|
||||
throw new IIOException(String.format("Destination type from ImageReadParam does not match legal imageTypes from reader: %s", imageType));
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
Rectangle srcRegion = new Rectangle(0, 0, 0, 0);
|
||||
Rectangle destRegion = new Rectangle(0, 0, 0, 0);
|
||||
computeRegions(param, width, height, null, srcRegion, destRegion);
|
||||
|
||||
int destWidth = destRegion.x + destRegion.width;
|
||||
int destHeight = destRegion.y + destRegion.height;
|
||||
|
||||
if ((long) destWidth * destHeight > Integer.MAX_VALUE) {
|
||||
throw new IllegalArgumentException(String.format("destination width * height > Integer.MAX_VALUE: %d", (long) destWidth * destHeight));
|
||||
}
|
||||
|
||||
// Create a new image based on the type specifier
|
||||
return imageType.createBufferedImage(destWidth, destHeight);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -311,10 +357,15 @@ public abstract class ImageReaderBase extends ImageReader {
|
||||
*
|
||||
* @param pParam the image read parameter, or {@code null}
|
||||
* @return true if {@code pParam} is non-{@code null} and either its {@code getDestination},
|
||||
* {@code getDestinationType} or {@code getDestinationOffset} returns a non-{@code null} value.
|
||||
* {@code getDestinationType} returns a non-{@code null} value,
|
||||
* or {@code getDestinationOffset} returns a {@link Point} that is not the upper left corner {@code (0, 0)}.
|
||||
*/
|
||||
protected static boolean hasExplicitDestination(final ImageReadParam pParam) {
|
||||
return (pParam != null && (pParam.getDestination() != null || pParam.getDestinationType() != null || pParam.getDestinationOffset() != null));
|
||||
return pParam != null &&
|
||||
(
|
||||
pParam.getDestination() != null || pParam.getDestinationType() != null ||
|
||||
!ORIGIN.equals(pParam.getDestinationOffset())
|
||||
);
|
||||
}
|
||||
|
||||
public static void main(String[] pArgs) throws IOException {
|
||||
|
@ -0,0 +1,268 @@
|
||||
/*
|
||||
* Copyright (c) 2012, 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;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.imageio.IIOException;
|
||||
import javax.imageio.ImageReadParam;
|
||||
import javax.imageio.ImageTypeSpecifier;
|
||||
import java.awt.*;
|
||||
import java.awt.color.ColorSpace;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.DataBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* ImageReaderBaseTest
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haraldk$
|
||||
* @version $Id: ImageReaderBaseTest.java,v 1.0 23.05.12 09:50 haraldk Exp$
|
||||
*/
|
||||
public class ImageReaderBaseTest {
|
||||
|
||||
private static final List<ImageTypeSpecifier> TYPES = Arrays.asList(
|
||||
ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB),
|
||||
ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB)
|
||||
);
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetDestinationZeroWidth() throws IIOException {
|
||||
ImageReaderBase.getDestination(null, TYPES.iterator(), 0, 42);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetDestinationNegativeWidth() throws IIOException {
|
||||
ImageReaderBase.getDestination(null, TYPES.iterator(), -1, 42);
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetDestinationZeroHeight() throws IIOException {
|
||||
ImageReaderBase.getDestination(null, TYPES.iterator(), 42, 0);
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetDestinationNegativeHeight() throws IIOException {
|
||||
ImageReaderBase.getDestination(null, TYPES.iterator(), 42, -1);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetDestinationNullTypes() throws IIOException {
|
||||
ImageReaderBase.getDestination(null, null, 42, 42);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetDestinationNoTypes() throws IIOException {
|
||||
ImageReaderBase.getDestination(null, Collections.<ImageTypeSpecifier>emptyList().iterator(), 42, 42);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDestinationParamSourceRegionWider() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setSourceRegion(new Rectangle(42, 1));
|
||||
BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 3, 3);
|
||||
assertEquals(3, destination.getWidth());
|
||||
assertEquals(1, destination.getHeight());
|
||||
assertEquals(TYPES.get(0).getBufferedImageType(), destination.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDestinationParamSourceRegionTaller() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setSourceRegion(new Rectangle(1, 42));
|
||||
BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 3, 3);
|
||||
assertEquals(1, destination.getWidth());
|
||||
assertEquals(3, destination.getHeight());
|
||||
assertEquals(TYPES.get(0).getBufferedImageType(), destination.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDestinationParamDestinationWider() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestination(new BufferedImage(42, 1, BufferedImage.TYPE_INT_RGB));
|
||||
BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 3, 3);
|
||||
assertEquals(42, destination.getWidth());
|
||||
assertEquals(1, destination.getHeight());
|
||||
assertEquals(BufferedImage.TYPE_INT_RGB, destination.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDestinationParamDestinationTaller() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestination(new BufferedImage(1, 42, BufferedImage.TYPE_INT_ARGB));
|
||||
BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 3, 3);
|
||||
assertEquals(1, destination.getWidth());
|
||||
assertEquals(42, destination.getHeight());
|
||||
assertEquals(BufferedImage.TYPE_INT_ARGB, destination.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDestinationNoParam() throws IIOException {
|
||||
BufferedImage destination = ImageReaderBase.getDestination(null, TYPES.iterator(), 42, 1);
|
||||
assertEquals(BufferedImage.TYPE_INT_RGB, destination.getType());
|
||||
assertEquals(42, destination.getWidth());
|
||||
assertEquals(1, destination.getHeight());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDestinationParamNoDestination() throws IIOException {
|
||||
BufferedImage destination = ImageReaderBase.getDestination(new ImageReadParam(), TYPES.iterator(), 42, 1);
|
||||
assertEquals(BufferedImage.TYPE_INT_RGB, destination.getType());
|
||||
assertEquals(42, destination.getWidth());
|
||||
assertEquals(1, destination.getHeight());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDestinationParamGoodDestination() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestination(new BufferedImage(21, 1, BufferedImage.TYPE_INT_ARGB));
|
||||
BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 42, 1);
|
||||
assertEquals(BufferedImage.TYPE_INT_ARGB, destination.getType());
|
||||
assertEquals(21, destination.getWidth());
|
||||
assertEquals(1, destination.getHeight());
|
||||
}
|
||||
|
||||
@Test(expected = IIOException.class)
|
||||
public void testGetDestinationParamIllegalDestination() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestination(new BufferedImage(21, 1, BufferedImage.TYPE_USHORT_565_RGB));
|
||||
ImageReaderBase.getDestination(param, TYPES.iterator(), 42, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDestinationParamGoodDestinationType() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestinationType(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB));
|
||||
BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 6, 7);
|
||||
assertEquals(BufferedImage.TYPE_INT_ARGB, destination.getType());
|
||||
assertEquals(6, destination.getWidth());
|
||||
assertEquals(7, destination.getHeight());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDestinationParamGoodDestinationTypeAlt() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
// In essence, this is the same as TYPE_INT_ARGB
|
||||
ImageTypeSpecifier type = ImageTypeSpecifier.createPacked(ColorSpace.getInstance(ColorSpace.CS_sRGB), 0xff0000, 0xff00, 0xff, 0xff000000, DataBuffer.TYPE_INT, false);
|
||||
param.setDestinationType(type);
|
||||
BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 6, 7);
|
||||
assertEquals(BufferedImage.TYPE_INT_ARGB, destination.getType());
|
||||
assertEquals(6, destination.getWidth());
|
||||
assertEquals(7, destination.getHeight());
|
||||
}
|
||||
|
||||
@Test(expected = IIOException.class)
|
||||
public void testGetDestinationParamIllegalDestinationType() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestinationType(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_GRAY));
|
||||
ImageReaderBase.getDestination(param, TYPES.iterator(), 6, 7);
|
||||
}
|
||||
|
||||
@Test(expected = IIOException.class)
|
||||
public void testGetDestinationParamIllegalDestinationTypeAlt() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestinationType(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_BGR));
|
||||
ImageReaderBase.getDestination(param, TYPES.iterator(), 6, 7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDestinationSourceExceedsIntegerMax() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setSourceRegion(new Rectangle(42, 7));
|
||||
BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), Integer.MAX_VALUE, 42);// 90 194 313 174 pixels
|
||||
assertEquals(42, destination.getWidth());
|
||||
assertEquals(7, destination.getHeight());
|
||||
assertEquals(TYPES.get(0).getBufferedImageType(), destination.getType());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetDestinationParamDestinationExceedsIntegerMax() throws IIOException {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setSourceRegion(new Rectangle(3 * Short.MAX_VALUE, 2 * Short.MAX_VALUE)); // 6 442 057 734 pixels
|
||||
ImageReaderBase.getDestination(param, TYPES.iterator(), 6 * Short.MAX_VALUE, 4 * Short.MAX_VALUE); // 25 768 230 936 pixels
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetDestinationExceedsIntegerMax() throws IIOException {
|
||||
ImageReaderBase.getDestination(null, TYPES.iterator(), 3 * Short.MAX_VALUE, 2 * Short.MAX_VALUE); // 6 442 057 734 pixels
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasExplicitDestinationNull() {
|
||||
assertFalse(ImageReaderBase.hasExplicitDestination(null));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasExplicitDestinationDefaultParam() {
|
||||
assertFalse(ImageReaderBase.hasExplicitDestination(new ImageReadParam()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasExplicitDestinationParamWithDestination() {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestination(new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY));
|
||||
assertTrue(ImageReaderBase.hasExplicitDestination(param));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasExplicitDestinationParamWithDestinationType() {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestinationType(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB));
|
||||
assertTrue(ImageReaderBase.hasExplicitDestination(param));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasExplicitDestinationParamWithDestinationOffset() {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestinationOffset(new Point(42, 42));
|
||||
assertTrue(ImageReaderBase.hasExplicitDestination(param));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasExplicitDestinationParamWithDestinationOffsetUnspecified() {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
// getDestinationOffset should now return new Point(0, 0)
|
||||
assertFalse(ImageReaderBase.hasExplicitDestination(param));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasExplicitDestinationParamWithDestinationOffsetOrigin() {
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestinationOffset(new Point(0, 0));
|
||||
assertFalse(ImageReaderBase.hasExplicitDestination(param));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user