0N/A/*
2362N/A * Copyright (c) 1999, 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.imageio.spi;
0N/A
0N/Aimport java.awt.image.RenderedImage;
0N/Aimport java.io.IOException;
0N/Aimport javax.imageio.ImageTypeSpecifier;
0N/Aimport javax.imageio.ImageWriter;
0N/Aimport javax.imageio.stream.ImageOutputStream;
0N/A
0N/A/**
0N/A * The service provider interface (SPI) for <code>ImageWriter</code>s.
0N/A * For more information on service provider classes, see the class comment
0N/A * for the <code>IIORegistry</code> class.
0N/A *
0N/A * <p> Each <code>ImageWriterSpi</code> provides several types of information
0N/A * about the <code>ImageWriter</code> class with which it is associated.
0N/A *
0N/A * <p> The name of the vendor who defined the SPI class and a
0N/A * brief description of the class are available via the
0N/A * <code>getVendorName</code>, <code>getDescription</code>,
0N/A * and <code>getVersion</code> methods.
0N/A * These methods may be internationalized to provide locale-specific
0N/A * output. These methods are intended mainly to provide short,
0N/A * human-writable information that might be used to organize a pop-up
0N/A * menu or other list.
0N/A *
0N/A * <p> Lists of format names, file suffixes, and MIME types associated
0N/A * with the service may be obtained by means of the
0N/A * <code>getFormatNames</code>, <code>getFileSuffixes</code>, and
0N/A * <code>getMIMEType</code> methods. These methods may be used to
0N/A * identify candidate <code>ImageWriter</code>s for writing a
0N/A * particular file or stream based on manual format selection, file
0N/A * naming, or MIME associations.
0N/A *
0N/A * <p> A more reliable way to determine which <code>ImageWriter</code>s
0N/A * are likely to be able to parse a particular data stream is provided
0N/A * by the <code>canEncodeImage</code> method. This methods allows the
0N/A * service provider to inspect the actual image contents.
0N/A *
0N/A * <p> Finally, an instance of the <code>ImageWriter</code> class
0N/A * associated with this service provider may be obtained by calling
0N/A * the <code>createWriterInstance</code> method. Any heavyweight
0N/A * initialization, such as the loading of native libraries or creation
0N/A * of large tables, should be deferred at least until the first
0N/A * invocation of this method.
0N/A *
0N/A * @see IIORegistry
0N/A * @see javax.imageio.ImageTypeSpecifier
0N/A * @see javax.imageio.ImageWriter
0N/A *
0N/A */
0N/Apublic abstract class ImageWriterSpi extends ImageReaderWriterSpi {
0N/A
0N/A /**
0N/A * A single-element array, initially containing
1500N/A * <code>ImageOutputStream.class</code>, to be returned from
1500N/A * <code>getOutputTypes</code>.
1500N/A * @deprecated Instead of using this field, directly create
1500N/A * the equivalent array <code>{ ImageOutputStream.class }<code>.
0N/A */
1500N/A @Deprecated
0N/A public static final Class[] STANDARD_OUTPUT_TYPE =
0N/A { ImageOutputStream.class };
0N/A
0N/A /**
0N/A * An array of <code>Class</code> objects to be returned from
0N/A * <code>getOutputTypes</code>, initially <code>null</code>.
0N/A */
0N/A protected Class[] outputTypes = null;
0N/A
0N/A /**
0N/A * An array of strings to be returned from
0N/A * <code>getImageReaderSpiNames</code>, initially
0N/A * <code>null</code>.
0N/A */
0N/A protected String[] readerSpiNames = null;
0N/A
0N/A /**
0N/A * The <code>Class</code> of the writer, initially
0N/A * <code>null</code>.
0N/A */
0N/A private Class writerClass = null;
0N/A
0N/A /**
0N/A * Constructs a blank <code>ImageWriterSpi</code>. It is up to
0N/A * the subclass to initialize instance variables and/or override
0N/A * method implementations in order to provide working versions of
0N/A * all methods.
0N/A */
0N/A protected ImageWriterSpi() {
0N/A }
0N/A
0N/A /**
0N/A * Constructs an <code>ImageWriterSpi</code> with a given
0N/A * set of values.
0N/A *
0N/A * @param vendorName the vendor name, as a non-<code>null</code>
0N/A * <code>String</code>.
0N/A * @param version a version identifier, as a non-<code>null</code>
0N/A * <code>String</code>.
0N/A * @param names a non-<code>null</code> array of
0N/A * <code>String</code>s indicating the format names. At least one
0N/A * entry must be present.
0N/A * @param suffixes an array of <code>String</code>s indicating the
0N/A * common file suffixes. If no suffixes are defined,
0N/A * <code>null</code> should be supplied. An array of length 0
0N/A * will be normalized to <code>null</code>.
0N/A * @param MIMETypes an array of <code>String</code>s indicating
0N/A * the format's MIME types. If no suffixes are defined,
0N/A * <code>null</code> should be supplied. An array of length 0
0N/A * will be normalized to <code>null</code>.
0N/A * @param writerClassName the fully-qualified name of the
0N/A * associated <code>ImageWriterSpi</code> class, as a
0N/A * non-<code>null</code> <code>String</code>.
0N/A * @param outputTypes an array of <code>Class</code> objects of
0N/A * length at least 1 indicating the legal output types.
0N/A * @param readerSpiNames an array <code>String</code>s of length
0N/A * at least 1 naming the classes of all associated
0N/A * <code>ImageReader</code>s, or <code>null</code>. An array of
0N/A * length 0 is normalized to <code>null</code>.
0N/A * @param supportsStandardStreamMetadataFormat a
0N/A * <code>boolean</code> that indicates whether a stream metadata
0N/A * object can use trees described by the standard metadata format.
0N/A * @param nativeStreamMetadataFormatName a
0N/A * <code>String</code>, or <code>null</code>, to be returned from
0N/A * <code>getNativeStreamMetadataFormatName</code>.
0N/A * @param nativeStreamMetadataFormatClassName a
0N/A * <code>String</code>, or <code>null</code>, to be used to instantiate
0N/A * a metadata format object to be returned from
0N/A * <code>getNativeStreamMetadataFormat</code>.
0N/A * @param extraStreamMetadataFormatNames an array of
0N/A * <code>String</code>s, or <code>null</code>, to be returned from
0N/A * <code>getExtraStreamMetadataFormatNames</code>. An array of length
0N/A * 0 is normalized to <code>null</code>.
0N/A * @param extraStreamMetadataFormatClassNames an array of
0N/A * <code>String</code>s, or <code>null</code>, to be used to instantiate
0N/A * a metadata format object to be returned from
0N/A * <code>getStreamMetadataFormat</code>. An array of length
0N/A * 0 is normalized to <code>null</code>.
0N/A * @param supportsStandardImageMetadataFormat a
0N/A * <code>boolean</code> that indicates whether an image metadata
0N/A * object can use trees described by the standard metadata format.
0N/A * @param nativeImageMetadataFormatName a
0N/A * <code>String</code>, or <code>null</code>, to be returned from
0N/A * <code>getNativeImageMetadataFormatName</code>.
0N/A * @param nativeImageMetadataFormatClassName a
0N/A * <code>String</code>, or <code>null</code>, to be used to instantiate
0N/A * a metadata format object to be returned from
0N/A * <code>getNativeImageMetadataFormat</code>.
0N/A * @param extraImageMetadataFormatNames an array of
0N/A * <code>String</code>s to be returned from
0N/A * <code>getExtraImageMetadataFormatNames</code>. An array of length 0
0N/A * is normalized to <code>null</code>.
0N/A * @param extraImageMetadataFormatClassNames an array of
0N/A * <code>String</code>s, or <code>null</code>, to be used to instantiate
0N/A * a metadata format object to be returned from
0N/A * <code>getImageMetadataFormat</code>. An array of length
0N/A * 0 is normalized to <code>null</code>.
0N/A *
0N/A * @exception IllegalArgumentException if <code>vendorName</code>
0N/A * is <code>null</code>.
0N/A * @exception IllegalArgumentException if <code>version</code>
0N/A * is <code>null</code>.
0N/A * @exception IllegalArgumentException if <code>names</code>
0N/A * is <code>null</code> or has length 0.
0N/A * @exception IllegalArgumentException if <code>writerClassName</code>
0N/A * is <code>null</code>.
0N/A * @exception IllegalArgumentException if <code>outputTypes</code>
0N/A * is <code>null</code> or has length 0.
0N/A */
0N/A public ImageWriterSpi(String vendorName,
0N/A String version,
0N/A String[] names,
0N/A String[] suffixes,
0N/A String[] MIMETypes,
0N/A String writerClassName,
0N/A Class[] outputTypes,
0N/A String[] readerSpiNames,
0N/A boolean supportsStandardStreamMetadataFormat,
0N/A String nativeStreamMetadataFormatName,
0N/A String nativeStreamMetadataFormatClassName,
0N/A String[] extraStreamMetadataFormatNames,
0N/A String[] extraStreamMetadataFormatClassNames,
0N/A boolean supportsStandardImageMetadataFormat,
0N/A String nativeImageMetadataFormatName,
0N/A String nativeImageMetadataFormatClassName,
0N/A String[] extraImageMetadataFormatNames,
0N/A String[] extraImageMetadataFormatClassNames) {
0N/A super(vendorName, version,
0N/A names, suffixes, MIMETypes, writerClassName,
0N/A supportsStandardStreamMetadataFormat,
0N/A nativeStreamMetadataFormatName,
0N/A nativeStreamMetadataFormatClassName,
0N/A extraStreamMetadataFormatNames,
0N/A extraStreamMetadataFormatClassNames,
0N/A supportsStandardImageMetadataFormat,
0N/A nativeImageMetadataFormatName,
0N/A nativeImageMetadataFormatClassName,
0N/A extraImageMetadataFormatNames,
0N/A extraImageMetadataFormatClassNames);
0N/A
0N/A if (outputTypes == null) {
0N/A throw new IllegalArgumentException
0N/A ("outputTypes == null!");
0N/A }
0N/A if (outputTypes.length == 0) {
0N/A throw new IllegalArgumentException
0N/A ("outputTypes.length == 0!");
0N/A }
1500N/A
1500N/A this.outputTypes = (outputTypes == STANDARD_OUTPUT_TYPE) ?
1500N/A new Class<?>[] { ImageOutputStream.class } :
1500N/A outputTypes.clone();
1500N/A
0N/A // If length == 0, leave it null
0N/A if (readerSpiNames != null && readerSpiNames.length > 0) {
0N/A this.readerSpiNames = (String[])readerSpiNames.clone();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns <code>true</code> if the format that this writer
0N/A * outputs preserves pixel data bit-accurately. The default
0N/A * implementation returns <code>true</code>.
0N/A *
0N/A * @return <code>true</code> if the format preserves full pixel
0N/A * accuracy.
0N/A */
0N/A public boolean isFormatLossless() {
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of <code>Class</code> objects indicating what
0N/A * types of objects may be used as arguments to the writer's
0N/A * <code>setOutput</code> method.
0N/A *
0N/A * <p> For most writers, which only output to an
0N/A * <code>ImageOutputStream</code>, a single-element array
0N/A * containing <code>ImageOutputStream.class</code> should be
0N/A * returned.
0N/A *
0N/A * @return a non-<code>null</code> array of
0N/A * <code>Class</code>objects of length at least 1.
0N/A */
0N/A public Class[] getOutputTypes() {
0N/A return (Class[])outputTypes.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns <code>true</code> if the <code>ImageWriter</code>
0N/A * implementation associated with this service provider is able to
0N/A * encode an image with the given layout. The layout
0N/A * (<i>i.e.</i>, the image's <code>SampleModel</code> and
0N/A * <code>ColorModel</code>) is described by an
0N/A * <code>ImageTypeSpecifier</code> object.
0N/A *
0N/A * <p> A return value of <code>true</code> is not an absolute
0N/A * guarantee of successful encoding; the encoding process may still
0N/A * produce errors due to factors such as I/O errors, inconsistent
0N/A * or malformed data structures, etc. The intent is that a
0N/A * reasonable inspection of the basic structure of the image be
0N/A * performed in order to determine if it is within the scope of
0N/A * the encoding format. For example, a service provider for a
0N/A * format that can only encode greyscale would return
0N/A * <code>false</code> if handed an RGB <code>BufferedImage</code>.
0N/A * Similarly, a service provider for a format that can encode
0N/A * 8-bit RGB imagery might refuse to encode an image with an
0N/A * associated alpha channel.
0N/A *
0N/A * <p> Different <code>ImageWriter</code>s, and thus service
0N/A * providers, may choose to be more or less strict. For example,
0N/A * they might accept an image with premultiplied alpha even though
0N/A * it will have to be divided out of each pixel, at some loss of
0N/A * precision, in order to be stored.
0N/A *
0N/A * @param type an <code>ImageTypeSpecifier</code> specifying the
0N/A * layout of the image to be written.
0N/A *
0N/A * @return <code>true</code> if this writer is likely to be able
0N/A * to encode images with the given layout.
0N/A *
0N/A * @exception IllegalArgumentException if <code>type</code>
0N/A * is <code>null</code>.
0N/A */
0N/A public abstract boolean canEncodeImage(ImageTypeSpecifier type);
0N/A
0N/A /**
0N/A * Returns <code>true</code> if the <code>ImageWriter</code>
0N/A * implementation associated with this service provider is able to
0N/A * encode the given <code>RenderedImage</code> instance. Note
0N/A * that this includes instances of
0N/A * <code>java.awt.image.BufferedImage</code>.
0N/A *
0N/A * <p> See the discussion for
0N/A * <code>canEncodeImage(ImageTypeSpecifier)</code> for information
0N/A * on the semantics of this method.
0N/A *
0N/A * @param im an instance of <code>RenderedImage</code> to be encoded.
0N/A *
0N/A * @return <code>true</code> if this writer is likely to be able
0N/A * to encode this image.
0N/A *
0N/A * @exception IllegalArgumentException if <code>im</code>
0N/A * is <code>null</code>.
0N/A */
0N/A public boolean canEncodeImage(RenderedImage im) {
0N/A return canEncodeImage(ImageTypeSpecifier.createFromRenderedImage(im));
0N/A }
0N/A
0N/A /**
0N/A * Returns an instance of the <code>ImageWriter</code>
0N/A * implementation associated with this service provider.
0N/A * The returned object will initially be in an initial state as if
0N/A * its <code>reset</code> method had been called.
0N/A *
0N/A * <p> The default implementation simply returns
0N/A * <code>createWriterInstance(null)</code>.
0N/A *
0N/A * @return an <code>ImageWriter</code> instance.
0N/A *
0N/A * @exception IOException if an error occurs during loading,
0N/A * or initialization of the writer class, or during instantiation
0N/A * or initialization of the writer object.
0N/A */
0N/A public ImageWriter createWriterInstance() throws IOException {
0N/A return createWriterInstance(null);
0N/A }
0N/A
0N/A /**
0N/A * Returns an instance of the <code>ImageWriter</code>
0N/A * implementation associated with this service provider.
0N/A * The returned object will initially be in an initial state
0N/A * as if its <code>reset</code> method had been called.
0N/A *
0N/A * <p> An <code>Object</code> may be supplied to the plug-in at
0N/A * construction time. The nature of the object is entirely
0N/A * plug-in specific.
0N/A *
0N/A * <p> Typically, a plug-in will implement this method using code
0N/A * such as <code>return new MyImageWriter(this)</code>.
0N/A *
0N/A * @param extension a plug-in specific extension object, which may
0N/A * be <code>null</code>.
0N/A *
0N/A * @return an <code>ImageWriter</code> instance.
0N/A *
0N/A * @exception IOException if the attempt to instantiate
0N/A * the writer fails.
0N/A * @exception IllegalArgumentException if the
0N/A * <code>ImageWriter</code>'s constructor throws an
0N/A * <code>IllegalArgumentException</code> to indicate that the
0N/A * extension object is unsuitable.
0N/A */
0N/A public abstract ImageWriter createWriterInstance(Object extension)
0N/A throws IOException;
0N/A
0N/A /**
0N/A * Returns <code>true</code> if the <code>ImageWriter</code> object
0N/A * passed in is an instance of the <code>ImageWriter</code>
0N/A * associated with this service provider.
0N/A *
0N/A * @param writer an <code>ImageWriter</code> instance.
0N/A *
0N/A * @return <code>true</code> if <code>writer</code> is recognized
0N/A *
0N/A * @exception IllegalArgumentException if <code>writer</code> is
0N/A * <code>null</code>.
0N/A */
0N/A public boolean isOwnWriter(ImageWriter writer) {
0N/A if (writer == null) {
0N/A throw new IllegalArgumentException("writer == null!");
0N/A }
0N/A String name = writer.getClass().getName();
0N/A return name.equals(pluginClassName);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of <code>String</code>s containing all the
0N/A * fully qualified names of all the <code>ImageReaderSpi</code>
0N/A * classes that can understand the internal metadata
0N/A * representation used by the <code>ImageWriter</code> associated
0N/A * with this service provider, or <code>null</code> if there are
0N/A * no such <code>ImageReaders</code> specified. If a
0N/A * non-<code>null</code> value is returned, it must have non-zero
0N/A * length.
0N/A *
0N/A * <p> The first item in the array must be the name of the service
0N/A * provider for the "preferred" reader, as it will be used to
0N/A * instantiate the <code>ImageReader</code> returned by
0N/A * <code>ImageIO.getImageReader(ImageWriter)</code>.
0N/A *
0N/A * <p> This mechanism may be used to obtain
0N/A * <code>ImageReaders</code> that will generated non-pixel
0N/A * meta-data (see <code>IIOExtraDataInfo</code>) in a structure
0N/A * understood by an <code>ImageWriter</code>. By reading the
0N/A * image and obtaining this data from one of the
0N/A * <code>ImageReaders</code> obtained with this method and passing
0N/A * it on to the <code>ImageWriter</code>, a client program can
0N/A * read an image, modify it in some way, and write it back out
0N/A * preserving all meta-data, without having to understand anything
0N/A * about the internal structure of the meta-data, or even about
0N/A * the image format.
0N/A *
0N/A * @return an array of <code>String</code>s of length at least 1
0N/A * containing names of <code>ImageReaderSpi</code>s, or
0N/A * <code>null</code>.
0N/A *
0N/A * @see javax.imageio.ImageIO#getImageReader(ImageWriter)
0N/A * @see ImageReaderSpi#getImageWriterSpiNames()
0N/A */
0N/A public String[] getImageReaderSpiNames() {
0N/A return readerSpiNames == null ?
0N/A null : (String[])readerSpiNames.clone();
0N/A }
0N/A}