Compare commits

...

5 Commits

Author SHA1 Message Date
Harald Kuhr ce25d0e349 More clean-up 2024-04-11 20:10:33 +02:00
Harald Kuhr e2cc73f276 Fixed #859, Test clean-up, removed unused class. 2023-11-15 10:46:02 +01:00
Harald Kuhr 3623a7c5dd Further clean-up 2023-11-13 19:35:58 +01:00
Harald Kuhr ee424583c4 Clean-up 2023-11-11 15:03:09 +01:00
Harald Kuhr 1292c95040 Support for WebP in TIFF
Refactored tile reading for delegated formats.
2023-11-10 09:27:40 +01:00
15 changed files with 724 additions and 691 deletions
@@ -88,7 +88,7 @@ public interface JPEG {
// Start of Frame segment markers (SOFn).
/** SOF0: Baseline DCT, Huffman coding. */
int SOF0 = 0xFFC0;
/** SOF0: Extended DCT, Huffman coding. */
/** SOF1: Extended DCT, Huffman coding. */
int SOF1 = 0xFFC1;
/** SOF2: Progressive DCT, Huffman coding. */
int SOF2 = 0xFFC2;
+5
View File
@@ -30,6 +30,11 @@
<artifactId>imageio-jpeg</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-webp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-core</artifactId>
@@ -0,0 +1,99 @@
package com.twelvemonkeys.imageio.plugins.tiff;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.event.IIOReadWarningListener;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.util.Iterator;
import java.util.function.Predicate;
import static com.twelvemonkeys.lang.Validate.notNull;
/**
* DelegateTileDecoder.
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: DelegateTileDecoder.java,v 1.0 09/11/2023 haraldk Exp$
*/
class DelegateTileDecoder extends TileDecoder {
protected final ImageReader delegate;
protected final ImageReadParam param;
private final Predicate<ImageReader> needsRasterConversion;
private final RasterConverter converter;
private Boolean readRasterAndConvert;
DelegateTileDecoder(final IIOReadWarningListener warningListener, final String format, final ImageReadParam originalParam) throws IOException {
this(warningListener, createDelegate(format), originalParam, imageReader -> false, null);
}
DelegateTileDecoder(final IIOReadWarningListener warningListener, final String format, final ImageReadParam originalParam, final Predicate<ImageReader> needsRasterConversion, final RasterConverter converter) throws IOException {
this(warningListener, createDelegate(format), originalParam, needsRasterConversion, converter);
}
private DelegateTileDecoder(final IIOReadWarningListener warningListener, final ImageReader delegate, final ImageReadParam originalParam, final Predicate<ImageReader> needsRasterConversion, final RasterConverter converter) {
super(warningListener);
this.delegate = notNull(delegate, "delegate");
delegate.addIIOReadWarningListener(warningListener);
if (TIFFImageReader.DEBUG) {
System.out.println("tile reading delegate: " + delegate);
}
param = delegate.getDefaultReadParam();
param.setSourceSubsampling(originalParam.getSourceXSubsampling(), originalParam.getSourceYSubsampling(), 0, 0);
this.needsRasterConversion = needsRasterConversion;
this.converter = converter;
}
private static ImageReader createDelegate(String format) throws IOException {
// We'll just use the default (first) reader
// If it's the TwelveMonkeys one, we will be able to read JPEG Lossless etc.
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(format);
if (!readers.hasNext()) {
throw new IIOException("No ImageReader registered for '" + format + "' format");
}
return readers.next();
}
@Override
void decodeTile(final ImageInputStream input, final Rectangle sourceRegion, final Point destinationOffset, final BufferedImage destination) throws IOException {
delegate.setInput(input);
param.setSourceRegion(sourceRegion);
if (readRasterAndConvert == null) {
// All tiles in an image will use the same format, test once and cache result
readRasterAndConvert = needsRasterConversion.test(delegate);
}
if (!readRasterAndConvert) {
// No conversion needed
param.setDestinationOffset(destinationOffset);
param.setDestination(destination);
delegate.read(0, param);
}
else {
// Otherwise, it's likely CMYK or some other interpretation we don't need to convert.
// We'll have to use readAsRaster and later apply color space conversion ourselves
Raster raster = delegate.readRaster(0, param);
converter.convert(raster);
destination.getRaster().setDataElements(destinationOffset.x, destinationOffset.y, raster);
}
}
@Override
public void close() {
delegate.dispose();
}
}
@@ -1,159 +0,0 @@
/*
* 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 of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.twelvemonkeys.imageio.plugins.tiff;
import com.twelvemonkeys.imageio.metadata.jpeg.JPEG;
import com.twelvemonkeys.imageio.metadata.jpeg.JPEGQuality;
import com.twelvemonkeys.imageio.metadata.jpeg.JPEGSegment;
import com.twelvemonkeys.imageio.metadata.jpeg.JPEGSegmentUtil;
import javax.imageio.IIOException;
import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
import javax.imageio.plugins.jpeg.JPEGQTable;
import javax.imageio.stream.ImageInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.*;
/**
* JPEGTables
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: JPEGTables.java,v 1.0 11.05.12 09:13 haraldk Exp$
*/
class JPEGTables {
private static final int DHT_LENGTH = 16;
private static final Map<Integer, List<String>> SEGMENT_IDS = createSegmentIdsMap();
private JPEGQTable[] qTables;
private JPEGHuffmanTable[] dcHTables;
private JPEGHuffmanTable[] acHTables;
private static Map<Integer, List<String>> createSegmentIdsMap() {
Map<Integer, List<String>> segmentIds = new HashMap<Integer, List<String>>();
segmentIds.put(JPEG.DQT, null);
segmentIds.put(JPEG.DHT, null);
return Collections.unmodifiableMap(segmentIds);
}
private final List<JPEGSegment> segments;
public JPEGTables(ImageInputStream input) throws IOException {
segments = JPEGSegmentUtil.readSegments(input, SEGMENT_IDS);
}
public JPEGQTable[] getQTables() throws IOException {
if (qTables == null) {
qTables = JPEGQuality.getQTables(segments);
}
return qTables;
}
private void getHuffmanTables() throws IOException {
if (dcHTables == null || acHTables == null) {
List<JPEGHuffmanTable> dc = new ArrayList<JPEGHuffmanTable>();
List<JPEGHuffmanTable> ac = new ArrayList<JPEGHuffmanTable>();
// JPEG may contain multiple DHT marker segments
for (JPEGSegment segment : segments) {
if (segment.marker() != JPEG.DHT) {
continue;
}
DataInputStream data = new DataInputStream(segment.data());
int read = 0;
// A single DHT marker segment may contain multiple tables
while (read < segment.length()) {
int htInfo = data.read();
read++;
int num = htInfo & 0x0f; // 0-3
int type = htInfo >> 4; // 0 == DC, 1 == AC
if (type > 1) {
throw new IIOException("Bad DHT type: " + type);
}
if (num >= 4) {
throw new IIOException("Bad DHT table index: " + num);
}
else if (type == 0 ? dc.size() > num : ac.size() > num) {
throw new IIOException("Duplicate DHT table index: " + num);
}
// Read lengths as short array
short[] lengths = new short[DHT_LENGTH];
for (int i = 0; i < DHT_LENGTH; i++) {
lengths[i] = (short) data.readUnsignedByte();
}
read += lengths.length;
int sum = 0;
for (short length : lengths) {
sum += length;
}
// Expand table to short array
short[] table = new short[sum];
for (int j = 0; j < sum; j++) {
table[j] = (short) data.readUnsignedByte();
}
JPEGHuffmanTable hTable = new JPEGHuffmanTable(lengths, table);
if (type == 0) {
dc.add(num, hTable);
}
else {
ac.add(num, hTable);
}
read += sum;
}
}
dcHTables = dc.toArray(new JPEGHuffmanTable[dc.size()]);
acHTables = ac.toArray(new JPEGHuffmanTable[ac.size()]);
}
}
public JPEGHuffmanTable[] getDCHuffmanTables() throws IOException {
getHuffmanTables();
return dcHTables;
}
public JPEGHuffmanTable[] getACHuffmanTables() throws IOException {
getHuffmanTables();
return acHTables;
}
}
@@ -0,0 +1,37 @@
package com.twelvemonkeys.imageio.plugins.tiff;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.event.IIOReadWarningListener;
import java.io.IOException;
import java.util.function.Predicate;
/**
* JPEGTileDecoder.
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: JPEGTileDecoder.java,v 1.0 09/11/2023 haraldk Exp$
*/
final class JPEGTileDecoder extends DelegateTileDecoder {
JPEGTileDecoder(final IIOReadWarningListener warningListener, final int compression, final byte[] jpegTables, final int numTiles, final ImageReadParam originalParam, final Predicate<ImageReader> needsConversion, final RasterConverter converter) throws IOException {
super(warningListener, "JPEG", originalParam, needsConversion, converter);
if (jpegTables != null) {
// This initializes the tables and other internal settings for the reader,
// and is actually a feature of JPEG, see "abbreviated streams":
// http://docs.oracle.com/javase/6/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html#abbrev
delegate.setInput(new ByteArrayImageInputStream(jpegTables));
delegate.getStreamMetadata();
}
else if (numTiles > 1) {
// TODO: This is not really a problem as long as we read ALL tiles, but we can't have random access in this case...
if (compression == TIFFExtension.COMPRESSION_JPEG) {
warningListener.warningOccurred(delegate, "Missing JPEGTables for tiled/striped TIFF with compression: 7 (JPEG)");
}
// ...and the JPEG reader might choke on missing tables...
}
}
}
@@ -54,6 +54,8 @@ interface TIFFCustom {
int COMPRESSION_JPEG2000 = 34712;
// TODO: Aperio SVS JPEG2000: 33003 (YCbCr) and 33005 (RGB), see http://openslide.org/formats/aperio/
int COMPRESSION_WEBP = 50001;
// PIXTIFF aka DELL PixTools, see https://community.emc.com/message/515755#515755
/** PIXTIFF proprietary ZIP compression, identical to Deflate/ZLib. */
int COMPRESSION_PIXTIFF_ZIP = 50013;
@@ -0,0 +1,34 @@
package com.twelvemonkeys.imageio.plugins.tiff;
import javax.imageio.event.IIOReadWarningListener;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import static com.twelvemonkeys.lang.Validate.notNull;
/**
* TileDecoder.
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: TileDecoder.java,v 1.0 09/11/2023 haraldk Exp$
*/
abstract class TileDecoder implements AutoCloseable {
protected final IIOReadWarningListener warningListener;
public TileDecoder(IIOReadWarningListener warningListener) {
this.warningListener = notNull(warningListener, "warningListener");
}
abstract void decodeTile(ImageInputStream input, Rectangle sourceRegion, Point destinationOffset, BufferedImage destination) throws IOException;
@Override
public abstract void close();
interface RasterConverter {
void convert(Raster raster) throws IOException;
}
}
@@ -29,28 +29,6 @@
*/
package com.twelvemonkeys.imageio.plugins.tiff;
import static com.twelvemonkeys.imageio.plugins.tiff.TIFFImageMetadataFormat.SUN_NATIVE_IMAGE_METADATA_FORMAT_NAME;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataFormatImpl;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.spi.IIORegistry;
import javax.imageio.stream.ImageInputStream;
import org.junit.Test;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.twelvemonkeys.imageio.metadata.Directory;
import com.twelvemonkeys.imageio.metadata.Entry;
import com.twelvemonkeys.imageio.metadata.tiff.Rational;
@@ -60,6 +38,27 @@ import com.twelvemonkeys.imageio.metadata.tiff.TIFFReader;
import com.twelvemonkeys.imageio.stream.URLImageInputStreamSpi;
import com.twelvemonkeys.lang.StringUtil;
import org.junit.Test;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.imageio.ImageIO;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataFormatImpl;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.spi.IIORegistry;
import javax.imageio.stream.ImageInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static com.twelvemonkeys.imageio.plugins.tiff.TIFFImageMetadataFormat.SUN_NATIVE_IMAGE_METADATA_FORMAT_NAME;
import static org.junit.Assert.*;
/**
* TIFFImageMetadataTest.
*
@@ -305,7 +304,7 @@ public class TIFFImageMetadataTest {
@Test
public void testMergeTreeStandardFormat() throws IOException {
TIFFImageMetadata metadata = (TIFFImageMetadata) createMetadata("/tiff/zackthecat.tif");
TIFFImageMetadata metadata = (TIFFImageMetadata) createMetadata("/tiff/old-style-jpeg-zackthecat.tif");
String standardFormat = IIOMetadataFormatImpl.standardMetadataFormatName;
@@ -94,7 +94,7 @@ public class TIFFImageReaderTest extends ImageReaderAbstractTest<TIFFImageReader
new TestData(getClassLoaderResource("/tiff/ycbcr-cat.tif"), new Dimension(250, 325)), // YCbCr, LZW compressed
new TestData(getClassLoaderResource("/tiff/quad-jpeg.tif"), new Dimension(512, 384)), // YCbCr, JPEG compressed, striped
new TestData(getClassLoaderResource("/tiff/smallliz.tif"), new Dimension(160, 160)), // YCbCr, Old-Style JPEG compressed (full JFIF stream)
new TestData(getClassLoaderResource("/tiff/zackthecat.tif"), new Dimension(234, 213)), // YCbCr, Old-Style JPEG compressed (tables, no JFIF stream)
new TestData(getClassLoaderResource("/tiff/old-style-jpeg-zackthecat.tif"), new Dimension(234, 213)), // YCbCr, Old-Style JPEG compressed (tables, no JFIF stream)
new TestData(getClassLoaderResource("/tiff/test-single-gray-compression-type-2.tiff"), new Dimension(1728, 1146)), // Gray, CCITT type 2 compressed
new TestData(getClassLoaderResource("/tiff/cramps-tile.tif"), new Dimension(800, 607)), // Gray/WhiteIsZero, uncompressed, striped & tiled...
new TestData(getClassLoaderResource("/tiff/lzw-long-strings-sample.tif"), new Dimension(316, 173)), // RGBA, LZW compressed w/predictor
@@ -191,7 +191,9 @@ public class TIFFImageReaderTest extends ImageReaderAbstractTest<TIFFImageReader
new TestData(getClassLoaderResource("/tiff/planar-yuv420-jpeg-uncompressed.tif"), new Dimension(256, 64)), // YCbCr, JPEG coefficients, uncompressed, striped
new TestData(getClassLoaderResource("/tiff/planar-yuv420-jpeg-lzw.tif"), new Dimension(256, 64)), // YCbCr, JPEG coefficients,LZW compressed, striped
new TestData(getClassLoaderResource("/tiff/planar-yuv410-jpeg-uncompressed.tif"), new Dimension(256, 64)), // YCbCr, JPEG coefficients, uncompressed, striped
new TestData(getClassLoaderResource("/tiff/planar-yuv410-jpeg-lzw.tif"), new Dimension(256, 64)) // YCbCr, JPEG coefficients,LZW compressed, striped
new TestData(getClassLoaderResource("/tiff/planar-yuv410-jpeg-lzw.tif"), new Dimension(256, 64)), // YCbCr, JPEG coefficients,LZW compressed, striped
// WebP compressed
new TestData(getClassLoaderResource("/tiff/webp_lossless_rgba_alpha_fully_opaque.tif"), new Dimension(20, 20)) // RGBA, WebP lossless
);
}
@@ -368,7 +370,7 @@ public class TIFFImageReaderTest extends ImageReaderAbstractTest<TIFFImageReader
@Test
public void testReadOldStyleWangMultiStrip2() throws IOException {
TestData testData = new TestData(getClassLoaderResource("/tiff/662260-color.tif"), new Dimension(1600, 1200));
TestData testData = new TestData(getClassLoaderResource("/tiff/old-style-jpeg-662260-color.tif"), new Dimension(1600, 1200));
try (ImageInputStream stream = testData.getInputStream()) {
TIFFImageReader reader = createReader();
@@ -382,7 +384,7 @@ public class TIFFImageReaderTest extends ImageReaderAbstractTest<TIFFImageReader
assertNotNull(image);
assertEquals(testData.getDimension(0), new Dimension(image.getWidth(), image.getHeight()));
verify(warningListener, atLeastOnce()).warningOccurred(eq(reader), and(contains("Old-style JPEG"), contains("tables")));
verify(warningListener, atLeastOnce()).warningOccurred(eq(reader), and(contains("Incorrect StripOffsets/TileOffsets"), contains("SOS marker")));
verify(warningListener, atLeastOnce()).warningOccurred(eq(reader), and(contains("Incorrect StripByteCounts/TileByteCounts"), contains("JPEGInterchangeFormatLength")));
}
}
@@ -418,8 +418,18 @@ final class WebPImageReader extends ImageReaderBase {
}
types.add(rawImageType);
types.add(ImageTypeSpecifiers.createFromBufferedImageType(header.containsALPH ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB));
types.add(ImageTypeSpecifiers.createFromBufferedImageType(header.containsALPH ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_BGR));
if (!header.containsALPH) {
types.add(ImageTypeSpecifiers.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB));
types.add(ImageTypeSpecifiers.createFromBufferedImageType(BufferedImage.TYPE_INT_BGR));
// We can always decode into types with alpha
types.add(ImageTypeSpecifiers.createFromBufferedImageType(BufferedImage.TYPE_4BYTE_ABGR));
}
types.add(ImageTypeSpecifiers.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB));
types.add(ImageTypeSpecifiers.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB_PRE));
types.add(ImageTypeSpecifiers.createFromBufferedImageType(BufferedImage.TYPE_4BYTE_ABGR_PRE));
return types.iterator();
}
+6
View File
@@ -161,6 +161,12 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>imageio-webp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>imageio-core</artifactId>