Search in sources :

Example 21 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class JPEGsNotAcceleratedTest method writeTestImage.

public static void writeTestImage(String fileName) {
    BufferedImage bi = new BufferedImage(TEST_W, TEST_H, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.setColor(new Color(testRGB));
    g.fillRect(0, 0, TEST_W, TEST_H);
    try {
        System.err.printf("Writing %s\n", fileName);
        if (lowCompression) {
            ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix("jpeg").next();
            if (iw == null) {
                throw new RuntimeException("No available image writer for " + "jpeg " + " Test failed.");
            }
            File file = new File(fileName);
            ImageOutputStream ios = ImageIO.createImageOutputStream(file);
            iw.setOutput(ios);
            ImageWriteParam param = iw.getDefaultWriteParam();
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            param.setCompressionQuality(1);
            IIOImage iioImg = new IIOImage(bi, null, null);
            iw.write(null, iioImg, param);
        } else {
            ImageIO.write(bi, "jpeg", new File(fileName));
        }
    } catch (IOException e) {
        System.err.println("Error " + e + " when writing file: " + fileName);
        throw new RuntimeException(e);
    }
}
Also used : Graphics(java.awt.Graphics) Color(java.awt.Color) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 22 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream 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 23 with ImageOutputStream

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

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class CanWriteSequence method test.

private static void test(final ImageWriter writer) throws Exception {
    final File file = File.createTempFile("temp", ".img");
    file.deleteOnExit();
    final FileOutputStream fos = new FileOutputStream(file);
    final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
    writer.setOutput(ios);
    final IIOMetadata data = writer.getDefaultStreamMetadata(null);
    if (writer.canWriteSequence()) {
        writer.prepareWriteSequence(data);
    } else {
        try {
            writer.prepareWriteSequence(data);
            throw new RuntimeException("UnsupportedOperationException was not thrown");
        } catch (final UnsupportedOperationException ignored) {
        // expected
        }
    }
    writer.dispose();
    ios.close();
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 25 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class WriteAfterAbort method test.

private void test(final ImageWriter writer) throws IOException {
    // Image initialization
    final BufferedImage imageWrite = new BufferedImage(WIDTH, HEIGHT, TYPE_BYTE_BINARY);
    final Graphics2D g = imageWrite.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, WIDTH, HEIGHT);
    g.dispose();
    // File initialization
    final File file = File.createTempFile("temp", ".img");
    file.deleteOnExit();
    final FileOutputStream fos = new SkipWriteOnAbortOutputStream(file);
    final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
    writer.setOutput(ios);
    writer.addIIOWriteProgressListener(this);
    // This write will be aborted, and file will not be touched
    writer.write(imageWrite);
    if (!isStartedCalled) {
        throw new RuntimeException("Started should be called");
    }
    if (!isProgressCalled) {
        throw new RuntimeException("Progress should be called");
    }
    if (!isAbortCalled) {
        throw new RuntimeException("Abort should be called");
    }
    if (isCompleteCalled) {
        throw new RuntimeException("Complete should not be called");
    }
    // Flush aborted data
    ios.flush();
    // This write should be completed successfully and the file should
    // contain correct image data.
    abortFlag = false;
    isAbortCalled = false;
    isCompleteCalled = false;
    isProgressCalled = false;
    isStartedCalled = false;
    writer.write(imageWrite);
    if (!isStartedCalled) {
        throw new RuntimeException("Started should be called");
    }
    if (!isProgressCalled) {
        throw new RuntimeException("Progress should be called");
    }
    if (isAbortCalled) {
        throw new RuntimeException("Abort should not be called");
    }
    if (!isCompleteCalled) {
        throw new RuntimeException("Complete should be called");
    }
    writer.dispose();
    ios.close();
    // Validates content of the file.
    final BufferedImage imageRead = ImageIO.read(file);
    for (int x = 0; x < WIDTH; ++x) {
        for (int y = 0; y < HEIGHT; ++y) {
            if (imageRead.getRGB(x, y) != imageWrite.getRGB(x, y)) {
                throw new RuntimeException("Test failed.");
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) Graphics2D(java.awt.Graphics2D)

Aggregations

ImageOutputStream (javax.imageio.stream.ImageOutputStream)64 ImageWriter (javax.imageio.ImageWriter)41 BufferedImage (java.awt.image.BufferedImage)37 ByteArrayOutputStream (java.io.ByteArrayOutputStream)36 IIOImage (javax.imageio.IIOImage)33 IOException (java.io.IOException)26 ImageWriteParam (javax.imageio.ImageWriteParam)26 File (java.io.File)24 IIOMetadata (javax.imageio.metadata.IIOMetadata)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)12 Iterator (java.util.Iterator)9 ImageInputStream (javax.imageio.stream.ImageInputStream)9 ImageReader (javax.imageio.ImageReader)7 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)7 Graphics2D (java.awt.Graphics2D)5 FileOutputStream (java.io.FileOutputStream)5 Color (java.awt.Color)4 Graphics (java.awt.Graphics)4 OutputStream (java.io.OutputStream)4