Search in sources :

Example 21 with ImageWriteParam

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

Example 22 with ImageWriteParam

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

the class Frame method doTest.

public void doTest() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writer.reset();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);
    ImageWriteParam wparam = prepareWriteParam();
    IIOMetadata streamMetadata = prepareStreamMetadata(wparam);
    writer.prepareWriteSequence(streamMetadata);
    for (int i = 0; i < frames.length; i++) {
        BufferedImage src = frames[i].getImage();
        IIOMetadata imageMetadata = prepareImageMetadata(i, src, wparam);
        IIOImage img = new IIOImage(src, null, imageMetadata);
        writer.writeToSequence(img, wparam);
    }
    writer.endWriteSequence();
    ios.flush();
    ios.close();
    if (doSave) {
        File f = File.createTempFile("wr_test_", "." + format, new File("."));
        System.out.println("Save to file: " + f.getCanonicalPath());
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(baos.toByteArray());
        fos.flush();
        fos.close();
    }
    // read result
    reader.reset();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    reader.setInput(ImageIO.createImageInputStream(bais));
    int minIndex = reader.getMinIndex();
    int numImages = reader.getNumImages(true);
    for (int i = 0; i < numImages; i++) {
        BufferedImage dst = reader.read(i + minIndex);
        frames[i].checkResult(dst);
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 23 with ImageWriteParam

use of javax.imageio.ImageWriteParam 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 24 with ImageWriteParam

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

the class WriterReuseTest method doTest.

public static void doTest(boolean writeSequence) throws IOException {
    String format = "GIF";
    ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
    if (writer == null) {
        throw new RuntimeException("No writer available for " + format);
    }
    BufferedImage img = createTestImage();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);
    WriterReuseTest t = new WriterReuseTest();
    writer.addIIOWriteProgressListener(t);
    ImageWriteParam param = writer.getDefaultWriteParam();
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(param);
    IIOImage iioImg = new IIOImage(img, null, null);
    if (writeSequence) {
        writer.prepareWriteSequence(streamMetadata);
        writer.writeToSequence(iioImg, param);
    } else {
        writer.write(img);
    }
    if (!t.isWritingAborted || t.isWritingCompleted) {
        throw new RuntimeException("Test failed.");
    }
    t.reset();
    // next attempt after abort
    ImageOutputStream ios2 = ImageIO.createImageOutputStream(new ByteArrayOutputStream());
    writer.setOutput(ios2);
    if (writeSequence) {
        writer.writeToSequence(iioImg, param);
    } else {
        writer.write(img);
    }
    if (t.isWritingAborted || !t.isWritingCompleted) {
        throw new RuntimeException("Test failed.");
    }
    System.out.println("Test passed.");
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) 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 25 with ImageWriteParam

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

Aggregations

ImageWriteParam (javax.imageio.ImageWriteParam)48 IIOImage (javax.imageio.IIOImage)32 ImageWriter (javax.imageio.ImageWriter)32 BufferedImage (java.awt.image.BufferedImage)27 ImageOutputStream (javax.imageio.stream.ImageOutputStream)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 IIOMetadata (javax.imageio.metadata.IIOMetadata)14 File (java.io.File)13 IOException (java.io.IOException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Iterator (java.util.Iterator)10 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)10 Rectangle (java.awt.Rectangle)6 ImageReader (javax.imageio.ImageReader)5 ImageInputStream (javax.imageio.stream.ImageInputStream)5 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)5 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)4 Graphics2D (java.awt.Graphics2D)3 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)3 Color (java.awt.Color)2