Search in sources :

Example 46 with IIOMetadata

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

the class CompressionModeTest method doTest.

private static void doTest(int mode) {
    String fileFormat = "bmp";
    try {
        ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix(fileFormat).next();
        if (iw == null) {
            throw new RuntimeException("No available image writer for " + fileFormat + " Test failed.");
        }
        File file = new File("image." + fileFormat);
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        iw.setOutput(ios);
        BufferedImage bimg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics g = bimg.getGraphics();
        g.setColor(Color.green);
        g.fillRect(0, 0, 100, 100);
        ImageWriteParam param = iw.getDefaultWriteParam();
        param.setCompressionMode(mode);
        IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bimg), param);
        IIOImage iioImg = new IIOImage(bimg, null, meta);
        iw.write(null, iioImg, param);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed.");
    }
}
Also used : Graphics(java.awt.Graphics) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Example 47 with IIOMetadata

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

the class AnimationTest method main.

public static void main(String[] args) {
    try {
        AnimationTest t = new AnimationTest();
        t.initFrame();
        ImageWriter w = t.initWriter();
        ImageWriteParam p = w.getDefaultWriteParam();
        IIOMetadata streamMetadata = w.getDefaultStreamMetadata(p);
        w.prepareWriteSequence(streamMetadata);
        for (int i = 0; i < 50; i++) {
            BufferedImage f = t.createNextFrame();
            ImageTypeSpecifier type = new ImageTypeSpecifier(f);
            IIOMetadata m = w.getDefaultImageMetadata(type, p);
            w.writeToSequence(new IIOImage(f, null, m), p);
        }
        w.endWriteSequence();
        t.checkAnimation();
    } catch (Exception e) {
        throw new RuntimeException("Test failed.", e);
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IOException(java.io.IOException) IIOImage(javax.imageio.IIOImage)

Example 48 with IIOMetadata

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

the class EmptyInputBmpMetadataTest method main.

public static void main(String[] args) {
    boolean isPassed = false;
    ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName(fmt).next();
    if (ir == null) {
        throw new RuntimeException("No available reader for " + fmt);
    }
    IIOMetadata meta = null;
    try {
        meta = ir.getImageMetadata(0);
    } catch (IllegalStateException e) {
        System.out.println("Correct exception was thrown. Test passed.");
        isPassed = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (!isPassed) {
        throw new RuntimeException("The IllegalStateException was not thrown." + "Test failed.");
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader)

Example 49 with IIOMetadata

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

the class NoExtraBytesTest method checkColorDepth.

private void checkColorDepth() {
    IIOMetadata dst = iio_dst.getMetadata();
    Node data = dst.getAsTree("javax_imageio_bmp_1.0");
    Node n = data.getFirstChild();
    while (n != null && !("BitsPerPixel".equals(n.getNodeName()))) {
        System.out.println("Node " + n.getNodeName());
        n = n.getNextSibling();
    }
    if (n == null) {
        throw new RuntimeException("No BitsPerSample node!");
    }
    int bpp = 0;
    String value = n.getNodeValue();
    System.out.println("value = " + value);
    try {
        bpp = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        throw new RuntimeException("Wrong bpp value: " + value, e);
    }
    if (bpp != this.expectedColorDepth) {
        throw new RuntimeException("Wrong color depth: " + bpp + " (should be " + this.expectedColorDepth + ")");
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) Node(org.w3c.dom.Node)

Example 50 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)

Aggregations

IIOMetadata (javax.imageio.metadata.IIOMetadata)81 BufferedImage (java.awt.image.BufferedImage)36 ImageWriter (javax.imageio.ImageWriter)35 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)32 IIOImage (javax.imageio.IIOImage)30 ImageOutputStream (javax.imageio.stream.ImageOutputStream)30 File (java.io.File)27 ImageWriteParam (javax.imageio.ImageWriteParam)22 Node (org.w3c.dom.Node)22 IOException (java.io.IOException)20 ImageReader (javax.imageio.ImageReader)19 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)17 Rectangle (java.awt.Rectangle)13 NodeList (org.w3c.dom.NodeList)13 Point (java.awt.Point)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ImageInputStream (javax.imageio.stream.ImageInputStream)10 Test (org.junit.Test)9 ColorModel (java.awt.image.ColorModel)7