Search in sources :

Example 31 with ImageWriter

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

the class JpegWriterLeakTest method main.

public static void main(String[] args) {
    final ReferenceQueue<ImageWriter> queue = new ReferenceQueue<>();
    final ArrayList<Reference<? extends ImageWriter>> refs = new ArrayList<>();
    int count = 2;
    do {
        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
        final WeakReference<? extends ImageWriter> ref = new WeakReference<>(writer, queue);
        refs.add(ref);
        try {
            final ImageOutputStream os = ImageIO.createImageOutputStream(new ByteArrayOutputStream());
            writer.setOutput(os);
            writer.write(getImage());
        // NB: dispose() or reset() workarounds the problem.
        } catch (IOException e) {
        } finally {
            writer = null;
        }
        count--;
    } while (count > 0);
    System.out.println("Wait for GC...");
    final long testTimeOut = 60000L;
    final long startTime = System.currentTimeMillis();
    while (!refs.isEmpty()) {
        // check for the test timeout
        final long now = System.currentTimeMillis();
        if (now - startTime > testTimeOut) {
            System.out.println();
            throw new RuntimeException("Test FAILED.");
        }
        System.gc();
        try {
            System.out.print(".");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        ;
        Reference<? extends ImageWriter> r = queue.poll();
        if (r != null) {
            System.out.println("Got reference: " + r);
            refs.remove(r);
        }
    }
    System.out.println("Test PASSED.");
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) ImageWriter(javax.imageio.ImageWriter) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WeakReference(java.lang.ref.WeakReference) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 32 with ImageWriter

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

the class MergeTreeTest method main.

public static void main(String[] args) throws IOException {
    ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next();
    ImageTypeSpecifier type = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
    ImageOutputStream ios = ImageIO.createImageOutputStream(new File("MergeTreeTest.jpeg"));
    iw.setOutput(ios);
    IIOMetadata meta = iw.getDefaultImageMetadata(type, null);
    boolean isFailed = false;
    String[] fmts = meta.getMetadataFormatNames();
    for (int i = 0; i < fmts.length; i++) {
        System.out.print("Format: " + fmts[i] + " ... ");
        Node root = meta.getAsTree(fmts[i]);
        try {
            meta.mergeTree(fmts[i], root);
        } catch (NullPointerException e) {
            throw new RuntimeException("Test failed for format " + fmts[i], e);
        }
        System.out.println("PASSED");
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) Node(org.w3c.dom.Node) ImageWriter(javax.imageio.ImageWriter) File(java.io.File) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 33 with ImageWriter

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

the class ShortHistogramTest method writeImageWithHist.

protected File writeImageWithHist(BufferedImage bi) throws IOException {
    File f = File.createTempFile("hist_", ".png", new File("."));
    ImageWriter writer = ImageIO.getImageWritersByFormatName("PNG").next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(f);
    writer.setOutput(ios);
    ImageWriteParam param = writer.getDefaultWriteParam();
    ImageTypeSpecifier type = new ImageTypeSpecifier(bi);
    IIOMetadata imgMetadata = writer.getDefaultImageMetadata(type, param);
    /* add hIST node to image metadata */
    imgMetadata = upgradeMetadata(imgMetadata, bi);
    IIOImage iio_img = new IIOImage(bi, // no thumbnails
    null, imgMetadata);
    writer.write(iio_img);
    ios.flush();
    ios.close();
    return f;
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Example 34 with ImageWriter

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

the class PngDitDepthTest method main.

public static void main(String[] args) throws IIOInvalidTreeException {
    // getting the writer for the png format
    Iterator iter = ImageIO.getImageWritersByFormatName("png");
    ImageWriter writer = (ImageWriter) iter.next();
    // creating a color model
    ColorModel colorModel = ColorModel.getRGBdefault();
    // creating a sample model
    SampleModel sampleModel = colorModel.createCompatibleSampleModel(640, 480);
    // creating a default metadata object
    IIOMetadata metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(colorModel, sampleModel), null);
    String formatName = metaData.getNativeMetadataFormatName();
    // first call
    Node metaDataNode = metaData.getAsTree(formatName);
    try {
        metaData.setFromTree(formatName, metaDataNode);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    // second call (bitdepht is already set to an invalid value)
    metaDataNode = metaData.getAsTree(formatName);
    metaData.setFromTree(formatName, metaDataNode);
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOInvalidTreeException(javax.imageio.metadata.IIOInvalidTreeException)

Example 35 with ImageWriter

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

the class WriteAfterAbort method main.

public static void main(final String[] args) throws IOException {
    final IIORegistry registry = IIORegistry.getDefaultInstance();
    final Iterator<ImageWriterSpi> iter = registry.getServiceProviders(ImageWriterSpi.class, provider -> true, true);
    // Validates all supported ImageWriters
    while (iter.hasNext()) {
        final WriteAfterAbort writeAfterAbort = new WriteAfterAbort();
        final ImageWriter writer = iter.next().createWriterInstance();
        System.out.println("ImageWriter = " + writer);
        writeAfterAbort.test(writer);
    }
    System.out.println("Test passed");
}
Also used : IIORegistry(javax.imageio.spi.IIORegistry) ImageWriter(javax.imageio.ImageWriter) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

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