use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class WBMPImageReaderSpi method canDecodeInput.
public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
return false;
}
ImageInputStream stream = (ImageInputStream) source;
stream.mark();
try {
// TypeField
int type = stream.readByte();
int fixHeaderField = stream.readByte();
// check WBMP "header"
if (type != 0 || fixHeaderField != 0) {
// while WBMP reader does not support ext WBMP headers
return false;
}
int width = ReaderUtil.readMultiByteInteger(stream);
int height = ReaderUtil.readMultiByteInteger(stream);
// check image dimension
if (width <= 0 || height <= 0) {
return false;
}
long dataLength = stream.length();
if (dataLength == -1) {
// let's reject images with dimension above the limit.
return (width < MAX_WBMP_WIDTH) && (height < MAX_WBMP_HEIGHT);
}
dataLength -= stream.getStreamPosition();
long scanSize = (width / 8) + ((width % 8) == 0 ? 0 : 1);
return (dataLength == scanSize * height);
} finally {
stream.reset();
}
}
use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class JPEGImageReaderSpi method canDecodeInput.
public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
return false;
}
ImageInputStream iis = (ImageInputStream) source;
iis.mark();
// If the first two bytes are a JPEG SOI marker, it's probably
// a JPEG file. If they aren't, it definitely isn't a JPEG file.
int byte1 = iis.read();
int byte2 = iis.read();
iis.reset();
if ((byte1 == 0xFF) && (byte2 == JPEG.SOI)) {
return true;
}
return false;
}
use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class GetNumImages method main.
public static void main(String[] args) throws Exception {
IIORegistry registry = IIORegistry.getDefaultInstance();
// test ImageReader.getNumImages() for all available ImageReaders,
// with no source set
Iterator readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
while (readerspis.hasNext()) {
boolean caughtEx = false;
ImageReaderSpi readerspi = (ImageReaderSpi) readerspis.next();
ImageReader reader = readerspi.createReaderInstance();
try {
reader.getNumImages(false);
} catch (IllegalStateException ise) {
// caught exception, everything's okay
caughtEx = true;
}
if (!caughtEx) {
throw new RuntimeException("Test failed: exception was not " + "thrown for null input: " + reader);
}
}
// test ImageReader.getNumImages() for all available ImageReaders,
// with source set, seekForwardOnly and allowSearch both true
readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
while (readerspis.hasNext()) {
boolean caughtEx = false;
ImageReaderSpi readerspi = (ImageReaderSpi) readerspis.next();
ImageReader reader = readerspi.createReaderInstance();
byte[] barr = new byte[100];
ByteArrayInputStream bais = new ByteArrayInputStream(barr);
ImageInputStream iis = ImageIO.createImageInputStream(bais);
try {
reader.setInput(iis, true);
reader.getNumImages(true);
} catch (IllegalStateException ise) {
// caught exception, everything's okay
caughtEx = true;
}
if (!caughtEx) {
throw new RuntimeException("Test failed: exception was not " + "thrown when allowSearch and " + "seekForwardOnly are both true: " + reader);
}
}
}
use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class EmbeddedFormatTest method doTest.
public void doTest(String compression, int bi_type) throws IOException {
System.out.println("Test " + compression + " on " + getImageTypeName(bi_type));
BufferedImage src = createTestImage(bi_type);
writer.reset();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
ImageWriteParam wparam = prepareWriteParam(compression);
writer.write(null, new IIOImage(src, null, null), wparam);
ios.flush();
ios.close();
// read result
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ImageInputStream iis = ImageIO.createImageInputStream(bais);
reader.reset();
reader.setInput(iis);
BufferedImage dst = reader.read(0);
checkResult(dst);
}
use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class ITXtTest method readFrom.
private static ITXtTest readFrom(File f) {
try {
ImageInputStream iis = ImageIO.createImageInputStream(f);
ImageReader r = ImageIO.getImageReaders(iis).next();
r.setInput(iis);
IIOImage dst = r.readAll(0, null);
// look for iTXt node
IIOMetadata m = dst.getMetadata();
Node root = m.getAsTree(m.getNativeMetadataFormatName());
Node n = root.getFirstChild();
while (n != null && !"iTXt".equals(n.getNodeName())) {
n = n.getNextSibling();
}
if (n == null) {
throw new RuntimeException("No iTXt node!");
}
ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode) n);
return t;
} catch (Throwable e) {
throw new RuntimeException("Reading test failed.", e);
}
}
Aggregations