Search in sources :

Example 21 with ImageWriter

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

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

the class WriterResetTest method main.

public static void main(String[] args) throws IOException {
    ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();
    if (w == null) {
        throw new RuntimeException("No writers available!");
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    w.setOutput(ios);
    BufferedImage img = createTestImage();
    try {
        w.reset();
        w.write(img);
    } catch (IllegalStateException e) {
        System.out.println("Test passed");
    } catch (Throwable e) {
        throw new RuntimeException("Test failed", e);
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage)

Example 23 with ImageWriter

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

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

the class GifTransparencyTest method doTest.

public void doTest() {
    File pwd = new File(".");
    try {
        File f = File.createTempFile("transparency_test_", ".gif", pwd);
        System.out.println("file: " + f.getCanonicalPath());
        ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();
        ImageWriterSpi spi = w.getOriginatingProvider();
        boolean succeed_write = ImageIO.write(src, "gif", f);
        if (!succeed_write) {
            throw new RuntimeException("Test failed: failed to write src.");
        }
        dst = ImageIO.read(f);
        checkResult(src, dst);
    } catch (IOException e) {
        throw new RuntimeException("Test failed.", e);
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) File(java.io.File)

Example 25 with ImageWriter

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

the class IndexingTest method doTest.

public void doTest() {
    ComponentColorModel ccm = createBitmaskColorModel();
    BufferedImage img = createComponentImage(w, h, ccm);
    try {
        ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();
        w.setOutput(ImageIO.createImageOutputStream(new File(fname)));
        w.write(img);
    } catch (Exception e) {
        throw new RuntimeException("Test failed.", e);
    }
    BufferedImage dst = null;
    try {
        dst = ImageIO.read(new File(fname));
    } catch (Exception e) {
        throw new RuntimeException("Test failed.", e);
    }
    compareImages(img, dst);
    System.out.println("Test passed.");
}
Also used : ComponentColorModel(java.awt.image.ComponentColorModel) ImageWriter(javax.imageio.ImageWriter) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

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