Search in sources :

Example 1 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi 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 2 with ImageWriterSpi

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

Example 3 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi in project jdk8u_jdk by JetBrains.

the class OutputImageTests method initIIOWriteFormats.

private static void initIIOWriteFormats() {
    List spis = new ArrayList();
    List shortNames = new ArrayList();
    ImageIO.scanForPlugins();
    IIORegistry registry = IIORegistry.getDefaultInstance();
    java.util.Iterator writerspis = registry.getServiceProviders(ImageWriterSpi.class, false);
    while (writerspis.hasNext()) {
        // REMIND: there could be more than one non-core plugin for
        // a particular format, as is the case for JPEG2000 in the JAI
        // IIO Tools package, so we should support that somehow
        ImageWriterSpi spi = (ImageWriterSpi) writerspis.next();
        String klass = spi.getClass().getName();
        String format = spi.getFormatNames()[0].toLowerCase();
        String suffix = spi.getFileSuffixes()[0].toLowerCase();
        if (suffix == null || suffix.equals("")) {
            suffix = format;
        }
        String shortName;
        if (klass.startsWith("com.sun.imageio.plugins")) {
            shortName = "core-" + suffix;
        } else {
            shortName = "ext-" + suffix;
        }
        spis.add(spi);
        shortNames.add(shortName);
    }
    imageioWriterSpis = new ImageWriterSpi[spis.size()];
    imageioWriterSpis = (ImageWriterSpi[]) spis.toArray(imageioWriterSpis);
    imageioWriteFormatShortNames = new String[shortNames.size()];
    imageioWriteFormatShortNames = (String[]) shortNames.toArray(imageioWriteFormatShortNames);
}
Also used : IIORegistry(javax.imageio.spi.IIORegistry) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

Example 4 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi in project jdk8u_jdk by JetBrains.

the class DataTransferer method imageToStandardBytesImpl.

protected byte[] imageToStandardBytesImpl(RenderedImage renderedImage, String mimeType) throws IOException {
    Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType);
    ImageTypeSpecifier typeSpecifier = new ImageTypeSpecifier(renderedImage);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOException ioe = null;
    while (writerIterator.hasNext()) {
        ImageWriter imageWriter = (ImageWriter) writerIterator.next();
        ImageWriterSpi writerSpi = imageWriter.getOriginatingProvider();
        if (!writerSpi.canEncodeImage(typeSpecifier)) {
            continue;
        }
        try {
            ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(baos);
            try {
                imageWriter.setOutput(imageOutputStream);
                imageWriter.write(renderedImage);
                imageOutputStream.flush();
            } finally {
                imageOutputStream.close();
            }
        } catch (IOException e) {
            imageWriter.dispose();
            baos.reset();
            ioe = e;
            continue;
        }
        imageWriter.dispose();
        baos.close();
        return baos.toByteArray();
    }
    baos.close();
    if (ioe == null) {
        ioe = new IOException("Registered service providers failed to encode " + renderedImage + " to " + mimeType);
    }
    throw ioe;
}
Also used : ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 5 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi in project jdk8u_jdk by JetBrains.

the class ImageWriter method setOutput.

/**
     * Sets the destination to the given
     * <code>ImageOutputStream</code> or other <code>Object</code>.
     * The destination is assumed to be ready to accept data, and will
     * not be closed at the end of each write. This allows distributed
     * imaging applications to transmit a series of images over a
     * single network connection.  If <code>output</code> is
     * <code>null</code>, any currently set output will be removed.
     *
     * <p> If <code>output</code> is an
     * <code>ImageOutputStream</code>, calls to the
     * <code>write</code>, <code>writeToSequence</code>, and
     * <code>prepareWriteEmpty</code>/<code>endWriteEmpty</code>
     * methods will preserve the existing contents of the stream.
     * Other write methods, such as <code>writeInsert</code>,
     * <code>replaceStreamMetadata</code>,
     * <code>replaceImageMetadata</code>, <code>replacePixels</code>,
     * <code>prepareInsertEmpty</code>/<code>endInsertEmpty</code>,
     * and <code>endWriteSequence</code>, require the full contents
     * of the stream to be readable and writable, and may alter any
     * portion of the stream.
     *
     * <p> Use of a general <code>Object</code> other than an
     * <code>ImageOutputStream</code> is intended for writers that
     * interact directly with an output device or imaging protocol.
     * The set of legal classes is advertised by the writer's service
     * provider's <code>getOutputTypes</code> method; most writers
     * will return a single-element array containing only
     * <code>ImageOutputStream.class</code> to indicate that they
     * accept only an <code>ImageOutputStream</code>.
     *
     * <p> The default implementation sets the <code>output</code>
     * instance variable to the value of <code>output</code> after
     * checking <code>output</code> against the set of classes
     * advertised by the originating provider, if there is one.
     *
     * @param output the <code>ImageOutputStream</code> or other
     * <code>Object</code> to use for future writing.
     *
     * @exception IllegalArgumentException if <code>output</code> is
     * not an instance of one of the classes returned by the
     * originating service provider's <code>getOutputTypes</code>
     * method.
     *
     * @see #getOutput
     */
public void setOutput(Object output) {
    if (output != null) {
        ImageWriterSpi provider = getOriginatingProvider();
        if (provider != null) {
            Class[] classes = provider.getOutputTypes();
            boolean found = false;
            for (int i = 0; i < classes.length; i++) {
                if (classes[i].isInstance(output)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new IllegalArgumentException("Illegal output type!");
            }
        }
    }
    this.output = output;
}
Also used : ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

Aggregations

ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)28 ImageWriter (javax.imageio.ImageWriter)13 File (java.io.File)9 IIOImage (javax.imageio.IIOImage)8 ImageReaderSpi (javax.imageio.spi.ImageReaderSpi)7 Test (org.junit.Test)7 Iterator (java.util.Iterator)6 ImageWriteParam (javax.imageio.ImageWriteParam)6 IIORegistry (javax.imageio.spi.IIORegistry)6 ImageOutputStream (javax.imageio.stream.ImageOutputStream)5 IOException (java.io.IOException)4 BufferedImage (java.awt.image.BufferedImage)3 FileOutputStream (java.io.FileOutputStream)3 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)3 Ignore (org.junit.Ignore)3 ImageOutputStreamAdapter (it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter)2 OutputStream (java.io.OutputStream)2 ImageReader (javax.imageio.ImageReader)2 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)2 ImageReaderWriterSpi (javax.imageio.spi.ImageReaderWriterSpi)2