Search in sources :

Example 16 with ImageWriter

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

the class RLECompressionTest method doTest.

public void doTest(int type) throws IOException {
    BufferedImage src = getTestImage(type);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
    writer.setOutput(ios);
    ImageWriteParam wparam = writer.getDefaultWriteParam();
    wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    switch(type) {
        case TEST_RLE8:
            wparam.setCompressionType("BI_RLE8");
            break;
        case TEST_RLE4:
            wparam.setCompressionType("BI_RLE4");
            break;
        default:
            throw new IllegalArgumentException("Wrong test type: " + type);
    }
    writer.write(null, new IIOImage(src, null, null), wparam);
    ios.close();
    baos.close();
    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    BufferedImage dst = ImageIO.read(bais);
    checkResult(src, dst);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 17 with ImageWriter

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

the class WriteProgressListenerTest method doTest.

public void doTest() {
    try {
        System.out.println("Progress test for " + compression_type);
        BufferedImage bi = new BufferedImage(20, 300, BufferedImage.TYPE_INT_RGB);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
        Iterator iter = ImageIO.getImageWritersByFormatName(format);
        if (!iter.hasNext()) {
            throw new RuntimeException("No available writer for " + format);
        }
        ImageWriter writer = (ImageWriter) iter.next();
        writer.setOutput(ios);
        writer.addIIOWriteProgressListener(listener);
        IIOImage iio_img = new IIOImage(bi, null, null);
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionType(compression_type);
        writer.write(null, iio_img, param);
        if (!listener.isTestPassed) {
            throw new RuntimeException("Test for " + compression_type + " does not finish correctly!");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 18 with ImageWriter

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

the class ReaderListenersTest method doTest.

private static void doTest(String compression) {
    try {
        BufferedImage img = createTestImage();
        ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName("bmp").next();
        if (iw == null) {
            throw new RuntimeException("No writers for bmp format." + " Test failed.");
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        iw.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = iw.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionType(compression);
        iw.write(null, new IIOImage(img, null, null), param);
        baos.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName("bmp").next();
        if (ir == null) {
            throw new RuntimeException("No readers for bmp format." + " Test failed.");
        }
        IIOReadUpdateAdapter updateAdapter = new IIOReadUpdateAdapter();
        IIOReadProgressAdapter progressAdapter = new IIOReadProgressAdapter();
        ir.addIIOReadProgressListener(progressAdapter);
        ir.addIIOReadUpdateListener(updateAdapter);
        ir.setInput(ImageIO.createImageInputStream(bais));
        BufferedImage dst = ir.read(0);
        progressAdapter.checkResults();
        if (!updateAdapter.isImageUpdateUsed) {
            throw new RuntimeException("imageUpdate was not used." + " Test failed.");
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 19 with ImageWriter

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

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

the class PrepareWriteSequenceTest method main.

public static void main(String[] args) throws IOException {
    String format = "GIF";
    ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
    ImageWriteParam param = writer.getDefaultWriteParam();
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(param);
    boolean gotException = false;
    try {
        writer.prepareWriteSequence(streamMetadata);
    } catch (IllegalStateException e) {
        gotException = true;
        System.out.println("Test passed.");
        e.printStackTrace(System.out);
    }
    if (!gotException) {
        throw new RuntimeException("Test failed.");
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam)

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