Search in sources :

Example 1 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.

the class BooleanAttributes method test.

public static void test(String mimeType, boolean useStreamMeta, String metaXml, String... boolXpaths) throws Exception {
    BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    ImageWriteParam param = null;
    IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
    IIOMetadata imageMeta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
    Source src = new StreamSource(new StringReader(metaXml));
    DOMResult dst = new DOMResult();
    transform(src, dst);
    Document doc = (Document) dst.getNode();
    Element node = doc.getDocumentElement();
    String metaFormat = node.getNodeName();
    // Verify that the default metadata gets formatted correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, false);
    meta.mergeTree(metaFormat, node);
    // Verify that the merged metadata gets formatte correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
    iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
    iw.dispose();
    ios.close();
    ImageReader ir = ImageIO.getImageReader(iw);
    byte[] bytes = os.toByteArray();
    if (bytes.length == 0)
        throw new AssertionError("Zero length image file");
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    if (useStreamMeta)
        meta = ir.getStreamMetadata();
    else
        meta = ir.getImageMetadata(0);
    // Verify again after writing and re-reading the image
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) StreamSource(javax.xml.transform.stream.StreamSource) Element(org.w3c.dom.Element) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageWriter(javax.imageio.ImageWriter) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) Document(org.w3c.dom.Document) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream)

Example 2 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.

the class ImageReaderGetDestination method main.

public static void main(String[] argv) {
    Vector imageTypes = new Vector();
    boolean gotIAE1 = false;
    boolean gotIAE2 = false;
    boolean gotIAE3 = false;
    boolean gotIAE4 = false;
    try {
        DummyImageReaderImpl.getDestination(null, null, 5, 10);
    } catch (IllegalArgumentException iae) {
        gotIAE1 = true;
    } catch (Throwable ee) {
        System.out.println("Unexpected exception 1:");
        ee.printStackTrace();
    }
    if (!gotIAE1) {
        throw new RuntimeException("Failed to get IAE #1!");
    }
    try {
        DummyImageReaderImpl.getDestination(null, imageTypes.iterator(), 5, 10);
    } catch (IllegalArgumentException iae) {
        gotIAE2 = true;
    } catch (Throwable ee) {
        System.out.println("Unexpected exception 2:");
        ee.printStackTrace();
    }
    if (!gotIAE2) {
        throw new RuntimeException("Failed to get IAE #2!");
    }
    imageTypes.add("abc");
    try {
        DummyImageReaderImpl.getDestination(null, imageTypes.iterator(), 5, 10);
    } catch (IllegalArgumentException iae) {
        gotIAE3 = true;
    } catch (Throwable ee) {
        System.out.println("Unexpected exception 3:");
        ee.printStackTrace();
    }
    if (!gotIAE3) {
        throw new RuntimeException("Failed to get IAE #3!");
    }
    imageTypes.clear();
    ImageTypeSpecifier its = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
    imageTypes.add(its);
    try {
        DummyImageReaderImpl.getDestination(null, imageTypes.iterator(), Integer.MAX_VALUE, Integer.MAX_VALUE);
    } catch (IllegalArgumentException iae) {
        gotIAE4 = true;
    } catch (Throwable ee) {
        System.out.println("Unexpected exception 4: ");
        ee.printStackTrace();
    }
    if (!gotIAE4) {
        throw new RuntimeException("Failed to get IAE #4!");
    }
}
Also used : Vector(java.util.Vector) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 3 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.

the class ImageTypeSpecifierBitsPerBand method main.

public static void main(String[] args) {
    int biType = BufferedImage.TYPE_USHORT_565_RGB;
    ImageTypeSpecifier type = ImageTypeSpecifier.createFromBufferedImageType(biType);
    int b0 = type.getBitsPerBand(0);
    int b1 = type.getBitsPerBand(1);
    int b2 = type.getBitsPerBand(2);
    if (b0 != 5 || b1 != 6 || b2 != 5) {
        throw new RuntimeException("Got incorrect bits per band value!");
    }
    boolean gotIAE = false;
    try {
        int b3 = type.getBitsPerBand(3);
    } catch (IllegalArgumentException e) {
        gotIAE = true;
    }
    if (!gotIAE) {
        throw new RuntimeException("Failed to get IllegalArgumentException for band == 3!");
    }
}
Also used : ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 4 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.

the class BMPWriteParamTest method getWriteResult.

private static BufferedImage getWriteResult(BufferedImage img, String format) throws IOException {
    ImageWriter iw = null;
    Iterator writers = ImageIO.getImageWritersByFormatName(format);
    while (writers.hasNext()) {
        iw = (ImageWriter) writers.next();
        System.out.println(format + " -> " + iw.toString());
    }
    if (iw == null) {
        throw new RuntimeException("No available Image writer for " + format);
    }
    ImageWriteParam param = iw.getDefaultWriteParam();
    param.setSourceRegion(new Rectangle(10, 10, 31, 31));
    param.setSourceSubsampling(3, 3, 0, 0);
    IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOImage iio_img = new IIOImage(img, null, meta);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    iw.setOutput(ios);
    iw.write(meta, iio_img, param);
    ios.flush();
    byte[] ba_image = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);
    ImageReader ir = null;
    Iterator readers = ImageIO.getImageReadersByFormatName(format);
    while (readers.hasNext()) {
        ir = (ImageReader) readers.next();
        System.out.println(format + " -> " + ir.toString());
    }
    if (ir == null) {
        throw new RuntimeException("No available Image reader for " + format);
    }
    ir.setInput(ImageIO.createImageInputStream(bais));
    BufferedImage res = ir.read(0);
    return res;
}
Also used : ImageWriter(javax.imageio.ImageWriter) Rectangle(java.awt.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) Iterator(java.util.Iterator) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 5 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.

the class LogicalScreenDimensionTest method main.

public static void main(String[] args) throws IOException {
    String format = "GIF";
    ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
    if (writer == null) {
        throw new RuntimeException("No available writers for " + format);
    }
    BufferedImage img = createTestImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);
    ImageWriteParam p = writer.getDefaultWriteParam();
    ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(img);
    IIOMetadata inImageMetadata = writer.getDefaultImageMetadata(type, p);
    IIOMetadata inStreamMetadata = writer.getDefaultStreamMetadata(p);
    // write and read image
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);
    writer.write(inStreamMetadata, new IIOImage(img, null, inImageMetadata), p);
    ios.flush();
    ios.close();
    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    ImageReader reader = ImageIO.getImageReader(writer);
    reader.setInput(iis);
    IIOMetadata outStreamMetadata = reader.getStreamMetadata();
    GIFStreamMetadata gifStreamMetadata = (GIFStreamMetadata) outStreamMetadata;
    if (gifStreamMetadata.logicalScreenWidth != img.getWidth() || gifStreamMetadata.logicalScreenHeight != img.getHeight()) {
        throw new RuntimeException("Test failed due to wrong logical screen dimension.");
    }
}
Also used : GIFStreamMetadata(com.sun.imageio.plugins.gif.GIFStreamMetadata) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Aggregations

ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)91 BufferedImage (java.awt.image.BufferedImage)36 IIOMetadata (javax.imageio.metadata.IIOMetadata)33 ImageWriter (javax.imageio.ImageWriter)30 IIOImage (javax.imageio.IIOImage)24 IOException (java.io.IOException)23 ImageOutputStream (javax.imageio.stream.ImageOutputStream)22 ColorModel (java.awt.image.ColorModel)21 ImageReader (javax.imageio.ImageReader)20 ImageWriteParam (javax.imageio.ImageWriteParam)19 SampleModel (java.awt.image.SampleModel)18 ImageReadParam (javax.imageio.ImageReadParam)16 Rectangle (java.awt.Rectangle)14 File (java.io.File)13 Iterator (java.util.Iterator)12 ColorSpace (java.awt.color.ColorSpace)11 IndexColorModel (java.awt.image.IndexColorModel)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Point (java.awt.Point)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9