Search in sources :

Example 46 with ImageInputStream

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();
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream)

Example 47 with ImageInputStream

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;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream)

Example 48 with ImageInputStream

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);
        }
    }
}
Also used : IIORegistry(javax.imageio.spi.IIORegistry) ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) Iterator(java.util.Iterator) ImageReader(javax.imageio.ImageReader)

Example 49 with ImageInputStream

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 50 with ImageInputStream

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);
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageInputStream(javax.imageio.stream.ImageInputStream) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) Node(org.w3c.dom.Node) ImageReader(javax.imageio.ImageReader) IIOImage(javax.imageio.IIOImage)

Aggregations

ImageInputStream (javax.imageio.stream.ImageInputStream)60 ImageReader (javax.imageio.ImageReader)32 BufferedImage (java.awt.image.BufferedImage)23 IOException (java.io.IOException)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ImageOutputStream (javax.imageio.stream.ImageOutputStream)9 File (java.io.File)8 InputStream (java.io.InputStream)7 IIOImage (javax.imageio.IIOImage)7 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)7 ImageReadParam (javax.imageio.ImageReadParam)6 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)6 ImageWriter (javax.imageio.ImageWriter)6 ImageWriteParam (javax.imageio.ImageWriteParam)5 IIOMetadata (javax.imageio.metadata.IIOMetadata)5 Iterator (java.util.Iterator)4 IIOException (javax.imageio.IIOException)4 Dimension (java.awt.Dimension)2 Graphics (java.awt.Graphics)2