Search in sources :

Example 11 with ImageWriter

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

the class SetOutput method main.

public static void main(String[] args) throws IOException {
    ImageWriter iw = new PNGImageWriter(null);
    File f = File.createTempFile("imageio", "tmp");
    ImageOutputStream ios = ImageIO.createImageOutputStream(f);
    try {
        iw.setOutput(ios);
    } catch (NullPointerException npe) {
        f.delete();
        throw new RuntimeException("Got NullPointerException!");
    }
    f.delete();
}
Also used : PNGImageWriter(com.sun.imageio.plugins.png.PNGImageWriter) PNGImageWriter(com.sun.imageio.plugins.png.PNGImageWriter) ImageWriter(javax.imageio.ImageWriter) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 12 with ImageWriter

use of javax.imageio.ImageWriter 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 13 with ImageWriter

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

the class BMPWriteParamTest method main.

public static void main(String[] args) {
    ImageWriter iw = null;
    Iterator writers = ImageIO.getImageWritersByFormatName(format);
    if (!writers.hasNext()) {
        throw new RuntimeException("No available Image writer for " + format);
    }
    iw = (ImageWriter) writers.next();
    try {
        BufferedImage img = createTestImage();
        BufferedImage bmp_res = getWriteResult(img, "BMP");
        BufferedImage png_res = getWriteResult(img, "PNG");
        compare(bmp_res, png_res);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Unexpected exception: " + ex);
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) Iterator(java.util.Iterator) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException)

Example 14 with ImageWriter

use of javax.imageio.ImageWriter 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 15 with ImageWriter

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

the class RleEncodingTest method doTest.

private static void doTest(BufferedImage src, String compressionType, int compressionMode) throws IOException {
    ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix("bmp").next();
    if (iw == null) {
        throw new RuntimeException("No available writer. Test failed.");
    }
    IIOImage iioImg = new IIOImage(src, null, null);
    ImageWriteParam param = iw.getDefaultWriteParam();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    iw.setOutput(ios);
    System.out.println("Compression Type is " + compressionType);
    System.out.println("Compression Mode is " + compressionMode);
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType(compressionType);
    if (compressionMode != ImageWriteParam.MODE_EXPLICIT) {
        param.setCompressionMode(compressionMode);
    }
    try {
        iw.write(null, iioImg, param);
    } catch (IOException e) {
        int bpp = src.getColorModel().getPixelSize();
        if (compressionMode == ImageWriteParam.MODE_EXPLICIT) {
            if ((compressionType.equals("BI_RLE4") && bpp != 4) || (compressionType.equals("BI_RLE8") && bpp != 8)) {
                System.out.println("Can not encode " + bpp + "bpp image as" + compressionType);
                return;
            } else {
                throw new RuntimeException("Unable to encode " + bpp + "bpp image as " + compressionType + ". Test failed");
            }
        }
    }
    baos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    BufferedImage dst = ImageIO.read(iis);
    int w = src.getWidth();
    int h = src.getHeight();
    Object dstPixel = dst.getRaster().getDataElements(w / 2, h / 2, null);
    Object srcPixel = src.getRaster().getDataElements(w / 2, h / 2, null);
    if ((src.getColorModel().getRed(srcPixel) != dst.getColorModel().getRed(dstPixel)) || (src.getColorModel().getGreen(srcPixel) != dst.getColorModel().getGreen(dstPixel)) || (src.getColorModel().getBlue(srcPixel) != dst.getColorModel().getBlue(dstPixel)) || (src.getColorModel().getAlpha(srcPixel) != dst.getColorModel().getAlpha(dstPixel))) {
        showPixel(src, w / 2, h / 2);
        showPixel(dst, w / 2, h / 2);
        throw new RuntimeException("Colors are different: " + Integer.toHexString(src.getColorModel().getRGB(srcPixel)) + " and " + Integer.toHexString(dst.getColorModel().getRGB(dstPixel)));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Aggregations

ImageWriter (javax.imageio.ImageWriter)71 BufferedImage (java.awt.image.BufferedImage)45 ImageOutputStream (javax.imageio.stream.ImageOutputStream)41 IIOImage (javax.imageio.IIOImage)36 ImageWriteParam (javax.imageio.ImageWriteParam)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 IOException (java.io.IOException)27 Iterator (java.util.Iterator)21 File (java.io.File)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)19 IIOMetadata (javax.imageio.metadata.IIOMetadata)19 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ImageReader (javax.imageio.ImageReader)13 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)8 Graphics2D (java.awt.Graphics2D)6 ImageInputStream (javax.imageio.stream.ImageInputStream)6 Rectangle (java.awt.Rectangle)5 InputStream (java.io.InputStream)5 ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)5 Color (java.awt.Color)4