mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-10-05 13:17:12 -04:00
Deprecate for BufferedImageInputStream, now using buffered streams directly in all readers.
This commit is contained in:
@@ -30,37 +30,6 @@
|
||||
|
||||
package com.twelvemonkeys.imageio.plugins.jpeg;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.color.ColorSpace;
|
||||
import java.awt.color.ICC_ColorSpace;
|
||||
import java.awt.color.ICC_Profile;
|
||||
import java.awt.image.*;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.SequenceInputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.imageio.IIOException;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReadParam;
|
||||
import javax.imageio.ImageReader;
|
||||
import javax.imageio.ImageTypeSpecifier;
|
||||
import javax.imageio.event.IIOReadUpdateListener;
|
||||
import javax.imageio.event.IIOReadWarningListener;
|
||||
import javax.imageio.metadata.IIOMetadata;
|
||||
import javax.imageio.metadata.IIOMetadataFormatImpl;
|
||||
import javax.imageio.metadata.IIOMetadataNode;
|
||||
import javax.imageio.spi.ImageReaderSpi;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
|
||||
import com.twelvemonkeys.imageio.ImageReaderBase;
|
||||
import com.twelvemonkeys.imageio.color.ColorSpaces;
|
||||
import com.twelvemonkeys.imageio.color.YCbCrConverter;
|
||||
@@ -72,7 +41,6 @@ import com.twelvemonkeys.imageio.metadata.jpeg.JPEGSegment;
|
||||
import com.twelvemonkeys.imageio.metadata.jpeg.JPEGSegmentUtil;
|
||||
import com.twelvemonkeys.imageio.metadata.tiff.TIFF;
|
||||
import com.twelvemonkeys.imageio.metadata.tiff.TIFFReader;
|
||||
import com.twelvemonkeys.imageio.stream.BufferedImageInputStream;
|
||||
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
|
||||
import com.twelvemonkeys.imageio.stream.SubImageInputStream;
|
||||
import com.twelvemonkeys.imageio.util.ImageTypeSpecifiers;
|
||||
@@ -80,6 +48,23 @@ import com.twelvemonkeys.imageio.util.ProgressListenerBase;
|
||||
import com.twelvemonkeys.lang.Validate;
|
||||
import com.twelvemonkeys.xml.XMLSerializer;
|
||||
|
||||
import javax.imageio.*;
|
||||
import javax.imageio.event.IIOReadUpdateListener;
|
||||
import javax.imageio.event.IIOReadWarningListener;
|
||||
import javax.imageio.metadata.IIOMetadata;
|
||||
import javax.imageio.metadata.IIOMetadataFormatImpl;
|
||||
import javax.imageio.metadata.IIOMetadataNode;
|
||||
import javax.imageio.spi.ImageReaderSpi;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.awt.*;
|
||||
import java.awt.color.ColorSpace;
|
||||
import java.awt.color.ICC_ColorSpace;
|
||||
import java.awt.color.ICC_Profile;
|
||||
import java.awt.image.*;
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A JPEG {@code ImageReader} implementation based on the JRE {@code JPEGImageReader},
|
||||
* that adds support and properly handles cases where the JRE version throws exceptions.
|
||||
@@ -140,7 +125,7 @@ public final class JPEGImageReader extends ImageReaderBase {
|
||||
private List<Segment> segments;
|
||||
|
||||
private int currentStreamIndex = 0;
|
||||
private List<Long> streamOffsets = new ArrayList<>();
|
||||
private final List<Long> streamOffsets = new ArrayList<>();
|
||||
|
||||
protected JPEGImageReader(final ImageReaderSpi provider, final ImageReader delegate) {
|
||||
super(provider);
|
||||
@@ -197,10 +182,10 @@ public final class JPEGImageReader extends ImageReaderBase {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (IIOException ignore) {
|
||||
catch (IIOException e) {
|
||||
// May happen if no SOF is found, in case we'll just fall through
|
||||
if (DEBUG) {
|
||||
ignore.printStackTrace();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -747,26 +732,26 @@ public final class JPEGImageReader extends ImageReaderBase {
|
||||
long lastKnownSOIOffset = streamOffsets.get(streamOffsets.size() - 1);
|
||||
imageInput.seek(lastKnownSOIOffset);
|
||||
|
||||
try (ImageInputStream stream = new BufferedImageInputStream(imageInput)) { // Extreme (10s -> 50ms) speedup if imageInput is FileIIS
|
||||
try {
|
||||
for (int i = streamOffsets.size() - 1; i < imageIndex; i++) {
|
||||
long start = 0;
|
||||
|
||||
if (DEBUG) {
|
||||
start = System.currentTimeMillis();
|
||||
System.out.println(String.format("Start seeking for image index %d", i + 1));
|
||||
System.out.printf("Start seeking for image index %d%n", i + 1);
|
||||
}
|
||||
|
||||
// Need to skip over segments, as they may contain JPEG markers (eg. JFXX or EXIF thumbnail)
|
||||
JPEGSegmentUtil.readSegments(stream, Collections.<Integer, List<String>>emptyMap());
|
||||
JPEGSegmentUtil.readSegments(imageInput, Collections.<Integer, List<String>>emptyMap());
|
||||
|
||||
// Now, search for EOI and following SOI...
|
||||
int marker;
|
||||
while ((marker = stream.read()) != -1) {
|
||||
if (marker == 0xFF && (0xFF00 | stream.readUnsignedByte()) == JPEG.EOI) {
|
||||
while ((marker = imageInput.read()) != -1) {
|
||||
if (marker == 0xFF && (0xFF00 | imageInput.readUnsignedByte()) == JPEG.EOI) {
|
||||
// Found EOI, now the SOI should be nearby...
|
||||
while ((marker = stream.read()) != -1) {
|
||||
if (marker == 0xFF && (0xFF00 | stream.readUnsignedByte()) == JPEG.SOI) {
|
||||
long nextSOIOffset = stream.getStreamPosition() - 2;
|
||||
while ((marker = imageInput.read()) != -1) {
|
||||
if (marker == 0xFF && (0xFF00 | imageInput.readUnsignedByte()) == JPEG.SOI) {
|
||||
long nextSOIOffset = imageInput.getStreamPosition() - 2;
|
||||
imageInput.seek(nextSOIOffset);
|
||||
streamOffsets.add(nextSOIOffset);
|
||||
|
||||
@@ -780,10 +765,9 @@ public final class JPEGImageReader extends ImageReaderBase {
|
||||
}
|
||||
|
||||
if (DEBUG) {
|
||||
System.out.println(String.format("Seek in %d ms", System.currentTimeMillis() - start));
|
||||
System.out.printf("Seek in %d ms%n", System.currentTimeMillis() - start);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (EOFException eof) {
|
||||
IndexOutOfBoundsException ioobe = new IndexOutOfBoundsException("Image index " + imageIndex + " not found in stream");
|
||||
@@ -843,9 +827,9 @@ public final class JPEGImageReader extends ImageReaderBase {
|
||||
|
||||
return JPEGSegmentUtil.readSegments(imageInput, JPEGSegmentUtil.ALL_SEGMENTS);
|
||||
}
|
||||
catch (IIOException | IllegalArgumentException ignore) {
|
||||
catch (IIOException | IllegalArgumentException e) {
|
||||
if (DEBUG) {
|
||||
ignore.printStackTrace();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
@@ -1392,7 +1376,7 @@ public final class JPEGImageReader extends ImageReaderBase {
|
||||
final String arg = args[argIdx];
|
||||
|
||||
if (arg.charAt(0) == '-') {
|
||||
if (arg.equals("-s") || arg.equals("--subsample") && args.length > argIdx) {
|
||||
if (arg.equals("-s") || arg.equals("--subsample") && args.length > argIdx + 1) {
|
||||
String[] sub = args[++argIdx].split(",");
|
||||
|
||||
try {
|
||||
@@ -1411,7 +1395,7 @@ public final class JPEGImageReader extends ImageReaderBase {
|
||||
System.err.println("Bad sub sampling (x,y): '" + args[argIdx] + "'");
|
||||
}
|
||||
}
|
||||
else if (arg.equals("-r") || arg.equals("--roi") && args.length > argIdx) {
|
||||
else if (arg.equals("-r") || arg.equals("--roi") && args.length > argIdx + 1) {
|
||||
String[] region = args[++argIdx].split(",");
|
||||
|
||||
try {
|
||||
|
@@ -31,8 +31,6 @@
|
||||
|
||||
package com.twelvemonkeys.imageio.plugins.jpeg;
|
||||
|
||||
import com.twelvemonkeys.imageio.stream.BufferedImageInputStream;
|
||||
|
||||
import javax.imageio.IIOException;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.awt.*;
|
||||
@@ -77,7 +75,7 @@ final class JPEGLosslessDecoderWrapper {
|
||||
* @throws IOException is thrown if the decoder failed or a conversion is not supported
|
||||
*/
|
||||
BufferedImage readImage(final List<Segment> segments, final ImageInputStream input) throws IOException {
|
||||
JPEGLosslessDecoder decoder = new JPEGLosslessDecoder(segments, createBufferedInput(input), listenerDelegate);
|
||||
JPEGLosslessDecoder decoder = new JPEGLosslessDecoder(segments, input, listenerDelegate);
|
||||
|
||||
// TODO: Allow 10/12/14 bit (using a ComponentColorModel with correct bits, as in TIFF)
|
||||
// TODO: Rewrite this to pass a pre-allocated buffer of correct type (byte/short)/correct bands
|
||||
@@ -111,10 +109,6 @@ final class JPEGLosslessDecoderWrapper {
|
||||
throw new IIOException("JPEG Lossless with " + decoder.getPrecision() + " bit precision and " + decoder.getNumComponents() + " component(s) not supported");
|
||||
}
|
||||
|
||||
private ImageInputStream createBufferedInput(final ImageInputStream input) throws IOException {
|
||||
return input instanceof BufferedImageInputStream ? input : new BufferedImageInputStream(input);
|
||||
}
|
||||
|
||||
Raster readRaster(final List<Segment> segments, final ImageInputStream input) throws IOException {
|
||||
// TODO: Can perhaps be implemented faster
|
||||
return readImage(segments, input).getRaster();
|
||||
|
@@ -1415,7 +1415,7 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTest<JPEGImageReader
|
||||
assertNotNull(unknown.getUserObject()); // All unknowns must have user object (data array)
|
||||
}
|
||||
}
|
||||
catch (IIOException e) {
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail(String.format("Reading metadata failed for %s image %s: %s", testData, i, e.getMessage()));
|
||||
}
|
||||
|
@@ -187,7 +187,7 @@ public class JPEGSegmentImageInputStreamTest {
|
||||
assertEquals(2, iis.read(buffer, 0, buffer.length));
|
||||
assertEquals(2, iis.getStreamPosition());
|
||||
|
||||
iis.seek(2000); // Just a random postion beyond EOF
|
||||
iis.seek(2000); // Just a random position beyond EOF
|
||||
assertEquals(2000, iis.getStreamPosition());
|
||||
|
||||
// So far, so good (but stream position is now really beyond EOF)...
|
||||
|
Reference in New Issue
Block a user