Search in sources :

Example 31 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata in project jdk8u_jdk by JetBrains.

the class RasterWithMinXTest method main.

public static void main(String[] args) {
    String format = "jpeg";
    // Set output file.
    ImageOutputStream output = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());
    // Create image.
    BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_3BYTE_BGR);
    // Populate image.
    int[] rgbArray = new int[256];
    for (int i = 0; i < 256; i++) {
        Arrays.fill(rgbArray, i);
        bi.setRGB(0, i, 256, 1, rgbArray, 0, 256);
    }
    // create translated raster in order to get non-zero minX and minY
    WritableRaster r = (WritableRaster) bi.getRaster().createTranslatedChild(64, 64);
    Iterator i = ImageIO.getImageWritersByFormatName(format);
    ImageWriter iw = null;
    while (i.hasNext() && iw == null) {
        Object o = i.next();
        if (o instanceof com.sun.imageio.plugins.jpeg.JPEGImageWriter) {
            iw = (ImageWriter) o;
        }
    }
    if (iw == null) {
        throw new RuntimeException("No available image writer");
    }
    ImageWriteParam iwp = iw.getDefaultWriteParam();
    IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
    IIOImage img = new IIOImage(r, null, metadata);
    iw.setOutput(output);
    try {
        iw.write(img);
    } catch (RasterFormatException e) {
        e.printStackTrace();
        throw new RuntimeException("RasterException occurs. Test Failed!");
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Unexpected Exception");
    }
    // test case of theImageWriteParam with non-null sourceRegion
    iwp.setSourceRegion(new Rectangle(32, 32, 192, 192));
    metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
    try {
        iw.write(metadata, img, iwp);
    } catch (RasterFormatException e) {
        e.printStackTrace();
        throw new RuntimeException("SetSourceRegion causes the RasterException. Test Failed!");
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Unexpected Exception");
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) Rectangle(java.awt.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) RasterFormatException(java.awt.image.RasterFormatException) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) WritableRaster(java.awt.image.WritableRaster) Iterator(java.util.Iterator) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) RasterFormatException(java.awt.image.RasterFormatException)

Example 32 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata 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)

Example 33 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata in project jdk8u_jdk by JetBrains.

the class MergeStdCommentTest method main.

public static void main(String[] args) throws Exception {
    String format = "javax_imageio_1.0";
    BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
    IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
    DOMImplementationRegistry registry;
    registry = DOMImplementationRegistry.newInstance();
    DOMImplementation impl = registry.getDOMImplementation("XML 3.0");
    Document doc = impl.createDocument(null, format, null);
    Element root, text, entry;
    root = doc.getDocumentElement();
    root.appendChild(text = doc.createElement("Text"));
    text.appendChild(entry = doc.createElement("TextEntry"));
    // keyword isn't #REQUIRED by the standard metadata format.
    // However, it is required by the PNG format, so we include it here.
    entry.setAttribute("keyword", "Comment");
    entry.setAttribute("value", "Some demo comment");
    meta.mergeTree(format, root);
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) Element(org.w3c.dom.Element) ImageWriter(javax.imageio.ImageWriter) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) DOMImplementation(org.w3c.dom.DOMImplementation) Document(org.w3c.dom.Document) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 34 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata in project jdk8u_jdk by JetBrains.

the class BMPPluginTest method test.

public boolean test() throws IIOException, IOException {
    ir.reset();
    iw.reset();
    String[] suffixes = iw.getOriginatingProvider().getFileSuffixes();
    IIOMetadata md = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    System.out.println("Image type " + img.getType());
    ImageWriterSpi spi = iw.getOriginatingProvider();
    boolean bCanEncode = spi.canEncodeImage(img);
    System.out.println("Can encode image? " + (bCanEncode ? "YES" : "NO"));
    if (!bCanEncode) {
        return true;
    }
    IIOImage iio_img = new IIOImage(img, null, md);
    String fname = "test" + img.getType() + "." + suffixes[0];
    iw.setOutput(ImageIO.createImageOutputStream(new FileOutputStream(new File(fname))));
    System.out.print("write image ... ");
    iw.write(iio_img);
    System.out.println("OK");
    System.out.print("read image ... ");
    byte[] ba_image = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);
    ir.setInput(ImageIO.createImageInputStream(new FileInputStream(new File(fname))));
    BufferedImage res = ir.read(0);
    System.out.println("OK");
    System.out.print("compare images ... ");
    boolean r = compare(img, res);
    System.out.println(r ? "OK" : "FAILED");
    return r;
}
Also used : ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) FileInputStream(java.io.FileInputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) File(java.io.File)

Example 35 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata in project jdk8u_jdk by JetBrains.

the class UshortOutOfMemoryTest method testGetAsTree.

public void testGetAsTree() {
    ImageWriteParam p = w.getDefaultWriteParam();
    IIOMetadata m = w.getDefaultImageMetadata(ImageTypeSpecifier.createFromBufferedImageType(type), p);
    String format = m.getNativeMetadataFormatName();
    System.out.println("native format: " + format);
    int count = 0;
    try {
        while (count < 100) {
            System.out.println(" test " + count++);
            m.getAsTree(format);
        }
    } catch (OutOfMemoryError e) {
        System.gc();
        throw new RuntimeException("Test failed. Number of performed operations: " + count, e);
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriteParam(javax.imageio.ImageWriteParam)

Aggregations

IIOMetadata (javax.imageio.metadata.IIOMetadata)37 BufferedImage (java.awt.image.BufferedImage)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)20 ImageWriter (javax.imageio.ImageWriter)18 IIOImage (javax.imageio.IIOImage)17 ImageOutputStream (javax.imageio.stream.ImageOutputStream)15 ImageWriteParam (javax.imageio.ImageWriteParam)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 ImageReader (javax.imageio.ImageReader)9 File (java.io.File)8 IOException (java.io.IOException)7 Node (org.w3c.dom.Node)7 Rectangle (java.awt.Rectangle)5 ImageInputStream (javax.imageio.stream.ImageInputStream)5 FileOutputStream (java.io.FileOutputStream)4 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)4 Graphics (java.awt.Graphics)3 Point (java.awt.Point)3 ColorModel (java.awt.image.ColorModel)3