Search in sources :

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

Example 8 with ImageTypeSpecifier

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

the class RGBImageTest method doTest.

protected void doTest() throws IOException {
    BufferedImage biSrc = getSrc();
    ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);
    ImageWriteParam writeParam = writer.getDefaultWriteParam();
    IIOMetadata imageMetadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(biSrc), writeParam);
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(writeParam);
    IIOImage iioImg = new IIOImage(biSrc, null, imageMetadata);
    writer.write(streamMetadata, iioImg, writeParam);
    ios.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    ImageReader reader = ImageIO.getImageReader(writer);
    reader.setInput(iis);
    BufferedImage dst = reader.read(0);
    // do test
    int x = dx / 2;
    int y = height / 2;
    for (int i = 0; i < usedColors.length; i++) {
        int dstRgb = dst.getRGB(x, y);
        System.out.println("dstColor: " + Integer.toHexString(dstRgb));
        int srcRgb = usedColors[i].getRGB();
        System.out.println("srcColor: " + Integer.toHexString(srcRgb));
        if (dstRgb != srcRgb) {
            throw new RuntimeException("wrong color " + i + ": " + Integer.toHexString(dstRgb));
        }
        x += dx;
    }
}
Also used : 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)

Example 9 with ImageTypeSpecifier

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

the class WriteMetadataTest method main.

public static void main(String[] args) {
    ImageWriter w = ImageIO.getImageWritersByFormatName(format).next();
    if (w == null) {
        throw new RuntimeException("No available writers for format " + format);
    }
    ImageWriteParam p = w.getDefaultWriteParam();
    ImageTypeSpecifier t = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
    IIOMetadata m = w.getDefaultImageMetadata(t, p);
    System.out.println("Default image metadata is " + m);
    testWritableMetadata(m);
    IIOMetadata sm = w.getDefaultStreamMetadata(p);
    System.out.println("Default stream metadata is " + sm);
    testWritableMetadata(sm);
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 10 with ImageTypeSpecifier

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

the class CanEncodeIndexed method main.

public static void main(String[] args) {
    BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_BYTE_INDEXED);
    ImageTypeSpecifier spec = ImageTypeSpecifier.createFromRenderedImage(img);
    Iterator writers = ImageIO.getImageWriters(spec, "jpeg");
    if (!writers.hasNext()) {
        throw new RuntimeException("Test failed: " + "no JPEG writer found for " + "image with IndexColorModel");
    }
}
Also used : Iterator(java.util.Iterator) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Aggregations

ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)45 BufferedImage (java.awt.image.BufferedImage)25 IIOMetadata (javax.imageio.metadata.IIOMetadata)20 ImageWriter (javax.imageio.ImageWriter)18 IIOImage (javax.imageio.IIOImage)13 ImageOutputStream (javax.imageio.stream.ImageOutputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ImageReader (javax.imageio.ImageReader)11 ImageWriteParam (javax.imageio.ImageWriteParam)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 Iterator (java.util.Iterator)9 ColorModel (java.awt.image.ColorModel)7 IOException (java.io.IOException)7 SampleModel (java.awt.image.SampleModel)6 File (java.io.File)6 ImageInputStream (javax.imageio.stream.ImageInputStream)6 Rectangle (java.awt.Rectangle)5 ImageReadParam (javax.imageio.ImageReadParam)5 IndexColorModel (java.awt.image.IndexColorModel)4 Node (org.w3c.dom.Node)4