Path: blob/master/src/java.desktop/share/classes/javax/imageio/spi/ImageWriterSpi.java
41155 views
/*1* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.imageio.spi;2627import java.awt.image.RenderedImage;28import java.io.IOException;29import javax.imageio.ImageTypeSpecifier;30import javax.imageio.ImageWriter;31import javax.imageio.stream.ImageOutputStream;3233/**34* The service provider interface (SPI) for {@code ImageWriter}s.35* For more information on service provider classes, see the class comment36* for the {@code IIORegistry} class.37*38* <p> Each {@code ImageWriterSpi} provides several types of information39* about the {@code ImageWriter} class with which it is associated.40*41* <p> The name of the vendor who defined the SPI class and a42* brief description of the class are available via the43* {@code getVendorName}, {@code getDescription},44* and {@code getVersion} methods.45* These methods may be internationalized to provide locale-specific46* output. These methods are intended mainly to provide short,47* human-writable information that might be used to organize a pop-up48* menu or other list.49*50* <p> Lists of format names, file suffixes, and MIME types associated51* with the service may be obtained by means of the52* {@code getFormatNames}, {@code getFileSuffixes}, and53* {@code getMIMEType} methods. These methods may be used to54* identify candidate {@code ImageWriter}s for writing a55* particular file or stream based on manual format selection, file56* naming, or MIME associations.57*58* <p> A more reliable way to determine which {@code ImageWriter}s59* are likely to be able to parse a particular data stream is provided60* by the {@code canEncodeImage} method. This methods allows the61* service provider to inspect the actual image contents.62*63* <p> Finally, an instance of the {@code ImageWriter} class64* associated with this service provider may be obtained by calling65* the {@code createWriterInstance} method. Any heavyweight66* initialization, such as the loading of native libraries or creation67* of large tables, should be deferred at least until the first68* invocation of this method.69*70* @see IIORegistry71* @see javax.imageio.ImageTypeSpecifier72* @see javax.imageio.ImageWriter73*74*/75public abstract class ImageWriterSpi extends ImageReaderWriterSpi {7677/**78* A single-element array, initially containing79* {@code ImageOutputStream.class}, to be returned from80* {@code getOutputTypes}.81* @deprecated Instead of using this field, directly create82* the equivalent array {@code { ImageOutputStream.class }}.83*/84@Deprecated85public static final Class<?>[] STANDARD_OUTPUT_TYPE =86{ ImageOutputStream.class };8788/**89* An array of {@code Class} objects to be returned from90* {@code getOutputTypes}, initially {@code null}.91*/92protected Class<?>[] outputTypes = null;9394/**95* An array of strings to be returned from96* {@code getImageReaderSpiNames}, initially97* {@code null}.98*/99protected String[] readerSpiNames = null;100101/**102* The {@code Class} of the writer, initially103* {@code null}.104*/105private Class<?> writerClass = null;106107/**108* Constructs a blank {@code ImageWriterSpi}. It is up to109* the subclass to initialize instance variables and/or override110* method implementations in order to provide working versions of111* all methods.112*/113protected ImageWriterSpi() {114}115116/**117* Constructs an {@code ImageWriterSpi} with a given118* set of values.119*120* @param vendorName the vendor name, as a non-{@code null}121* {@code String}.122* @param version a version identifier, as a non-{@code null}123* {@code String}.124* @param names a non-{@code null} array of125* {@code String}s indicating the format names. At least one126* entry must be present.127* @param suffixes an array of {@code String}s indicating the128* common file suffixes. If no suffixes are defined,129* {@code null} should be supplied. An array of length 0130* will be normalized to {@code null}.131* @param MIMETypes an array of {@code String}s indicating132* the format's MIME types. If no suffixes are defined,133* {@code null} should be supplied. An array of length 0134* will be normalized to {@code null}.135* @param writerClassName the fully-qualified name of the136* associated {@code ImageWriterSpi} class, as a137* non-{@code null String}.138* @param outputTypes an array of {@code Class} objects of139* length at least 1 indicating the legal output types.140* @param readerSpiNames an array {@code String}s of length141* at least 1 naming the classes of all associated142* {@code ImageReader}s, or {@code null}. An array of143* length 0 is normalized to {@code null}.144* @param supportsStandardStreamMetadataFormat a145* {@code boolean} that indicates whether a stream metadata146* object can use trees described by the standard metadata format.147* @param nativeStreamMetadataFormatName a148* {@code String}, or {@code null}, to be returned from149* {@code getNativeStreamMetadataFormatName}.150* @param nativeStreamMetadataFormatClassName a151* {@code String}, or {@code null}, to be used to instantiate152* a metadata format object to be returned from153* {@code getNativeStreamMetadataFormat}.154* @param extraStreamMetadataFormatNames an array of155* {@code String}s, or {@code null}, to be returned from156* {@code getExtraStreamMetadataFormatNames}. An array of length157* 0 is normalized to {@code null}.158* @param extraStreamMetadataFormatClassNames an array of159* {@code String}s, or {@code null}, to be used to instantiate160* a metadata format object to be returned from161* {@code getStreamMetadataFormat}. An array of length162* 0 is normalized to {@code null}.163* @param supportsStandardImageMetadataFormat a164* {@code boolean} that indicates whether an image metadata165* object can use trees described by the standard metadata format.166* @param nativeImageMetadataFormatName a167* {@code String}, or {@code null}, to be returned from168* {@code getNativeImageMetadataFormatName}.169* @param nativeImageMetadataFormatClassName a170* {@code String}, or {@code null}, to be used to instantiate171* a metadata format object to be returned from172* {@code getNativeImageMetadataFormat}.173* @param extraImageMetadataFormatNames an array of174* {@code String}s to be returned from175* {@code getExtraImageMetadataFormatNames}. An array of length 0176* is normalized to {@code null}.177* @param extraImageMetadataFormatClassNames an array of178* {@code String}s, or {@code null}, to be used to instantiate179* a metadata format object to be returned from180* {@code getImageMetadataFormat}. An array of length181* 0 is normalized to {@code null}.182*183* @exception IllegalArgumentException if {@code vendorName}184* is {@code null}.185* @exception IllegalArgumentException if {@code version}186* is {@code null}.187* @exception IllegalArgumentException if {@code names}188* is {@code null} or has length 0.189* @exception IllegalArgumentException if {@code writerClassName}190* is {@code null}.191* @exception IllegalArgumentException if {@code outputTypes}192* is {@code null} or has length 0.193*/194public ImageWriterSpi(String vendorName,195String version,196String[] names,197String[] suffixes,198String[] MIMETypes,199String writerClassName,200Class<?>[] outputTypes,201String[] readerSpiNames,202boolean supportsStandardStreamMetadataFormat,203String nativeStreamMetadataFormatName,204String nativeStreamMetadataFormatClassName,205String[] extraStreamMetadataFormatNames,206String[] extraStreamMetadataFormatClassNames,207boolean supportsStandardImageMetadataFormat,208String nativeImageMetadataFormatName,209String nativeImageMetadataFormatClassName,210String[] extraImageMetadataFormatNames,211String[] extraImageMetadataFormatClassNames) {212super(vendorName, version,213names, suffixes, MIMETypes, writerClassName,214supportsStandardStreamMetadataFormat,215nativeStreamMetadataFormatName,216nativeStreamMetadataFormatClassName,217extraStreamMetadataFormatNames,218extraStreamMetadataFormatClassNames,219supportsStandardImageMetadataFormat,220nativeImageMetadataFormatName,221nativeImageMetadataFormatClassName,222extraImageMetadataFormatNames,223extraImageMetadataFormatClassNames);224225if (outputTypes == null) {226throw new IllegalArgumentException227("outputTypes == null!");228}229if (outputTypes.length == 0) {230throw new IllegalArgumentException231("outputTypes.length == 0!");232}233234this.outputTypes = (outputTypes == STANDARD_OUTPUT_TYPE) ?235new Class<?>[] { ImageOutputStream.class } :236outputTypes.clone();237238// If length == 0, leave it null239if (readerSpiNames != null && readerSpiNames.length > 0) {240this.readerSpiNames = readerSpiNames.clone();241}242}243244/**245* Returns {@code true} if the format that this writer246* outputs preserves pixel data bit-accurately. The default247* implementation returns {@code true}.248*249* @return {@code true} if the format preserves full pixel250* accuracy.251*/252public boolean isFormatLossless() {253return true;254}255256/**257* Returns an array of {@code Class} objects indicating what258* types of objects may be used as arguments to the writer's259* {@code setOutput} method.260*261* <p> For most writers, which only output to an262* {@code ImageOutputStream}, a single-element array263* containing {@code ImageOutputStream.class} should be264* returned.265*266* @return a non-{@code null} array of267* {@code Class} objects of length at least 1.268*/269public Class<?>[] getOutputTypes() {270return outputTypes.clone();271}272273/**274* Returns {@code true} if the {@code ImageWriter}275* implementation associated with this service provider is able to276* encode an image with the given layout. The layout277* (<i>i.e.</i>, the image's {@code SampleModel} and278* {@code ColorModel}) is described by an279* {@code ImageTypeSpecifier} object.280*281* <p> A return value of {@code true} is not an absolute282* guarantee of successful encoding; the encoding process may still283* produce errors due to factors such as I/O errors, inconsistent284* or malformed data structures, etc. The intent is that a285* reasonable inspection of the basic structure of the image be286* performed in order to determine if it is within the scope of287* the encoding format. For example, a service provider for a288* format that can only encode greyscale would return289* {@code false} if handed an RGB {@code BufferedImage}.290* Similarly, a service provider for a format that can encode291* 8-bit RGB imagery might refuse to encode an image with an292* associated alpha channel.293*294* <p> Different {@code ImageWriter}s, and thus service295* providers, may choose to be more or less strict. For example,296* they might accept an image with premultiplied alpha even though297* it will have to be divided out of each pixel, at some loss of298* precision, in order to be stored.299*300* @param type an {@code ImageTypeSpecifier} specifying the301* layout of the image to be written.302*303* @return {@code true} if this writer is likely to be able304* to encode images with the given layout.305*306* @exception IllegalArgumentException if {@code type}307* is {@code null}.308*/309public abstract boolean canEncodeImage(ImageTypeSpecifier type);310311/**312* Returns {@code true} if the {@code ImageWriter}313* implementation associated with this service provider is able to314* encode the given {@code RenderedImage} instance. Note315* that this includes instances of316* {@code java.awt.image.BufferedImage}.317*318* <p> See the discussion for319* {@code canEncodeImage(ImageTypeSpecifier)} for information320* on the semantics of this method.321*322* @param im an instance of {@code RenderedImage} to be encoded.323*324* @return {@code true} if this writer is likely to be able325* to encode this image.326*327* @exception IllegalArgumentException if {@code im}328* is {@code null}.329*/330public boolean canEncodeImage(RenderedImage im) {331return canEncodeImage(ImageTypeSpecifier.createFromRenderedImage(im));332}333334/**335* Returns an instance of the {@code ImageWriter}336* implementation associated with this service provider.337* The returned object will initially be in an initial state as if338* its {@code reset} method had been called.339*340* <p> The default implementation simply returns341* {@code createWriterInstance(null)}.342*343* @return an {@code ImageWriter} instance.344*345* @exception IOException if an error occurs during loading,346* or initialization of the writer class, or during instantiation347* or initialization of the writer object.348*/349public ImageWriter createWriterInstance() throws IOException {350return createWriterInstance(null);351}352353/**354* Returns an instance of the {@code ImageWriter}355* implementation associated with this service provider.356* The returned object will initially be in an initial state357* as if its {@code reset} method had been called.358*359* <p> An {@code Object} may be supplied to the plug-in at360* construction time. The nature of the object is entirely361* plug-in specific.362*363* <p> Typically, a plug-in will implement this method using code364* such as {@code return new MyImageWriter(this)}.365*366* @param extension a plug-in specific extension object, which may367* be {@code null}.368*369* @return an {@code ImageWriter} instance.370*371* @exception IOException if the attempt to instantiate372* the writer fails.373* @exception IllegalArgumentException if the374* {@code ImageWriter}'s constructor throws an375* {@code IllegalArgumentException} to indicate that the376* extension object is unsuitable.377*/378public abstract ImageWriter createWriterInstance(Object extension)379throws IOException;380381/**382* Returns {@code true} if the {@code ImageWriter} object383* passed in is an instance of the {@code ImageWriter}384* associated with this service provider.385*386* @param writer an {@code ImageWriter} instance.387*388* @return {@code true} if {@code writer} is recognized389*390* @exception IllegalArgumentException if {@code writer} is391* {@code null}.392*/393public boolean isOwnWriter(ImageWriter writer) {394if (writer == null) {395throw new IllegalArgumentException("writer == null!");396}397String name = writer.getClass().getName();398return name.equals(pluginClassName);399}400401/**402* Returns an array of {@code String}s containing all the403* fully qualified names of all the {@code ImageReaderSpi}404* classes that can understand the internal metadata405* representation used by the {@code ImageWriter} associated406* with this service provider, or {@code null} if there are407* no such {@code ImageReaders} specified. If a408* non-{@code null} value is returned, it must have non-zero409* length.410*411* <p> The first item in the array must be the name of the service412* provider for the "preferred" reader, as it will be used to413* instantiate the {@code ImageReader} returned by414* {@code ImageIO.getImageReader(ImageWriter)}.415*416* <p> This mechanism may be used to obtain417* {@code ImageReaders} that will generated non-pixel418* meta-data (see {@code IIOExtraDataInfo}) in a structure419* understood by an {@code ImageWriter}. By reading the420* image and obtaining this data from one of the421* {@code ImageReaders} obtained with this method and passing422* it on to the {@code ImageWriter}, a client program can423* read an image, modify it in some way, and write it back out424* preserving all meta-data, without having to understand anything425* about the internal structure of the meta-data, or even about426* the image format.427*428* @return an array of {@code String}s of length at least 1429* containing names of {@code ImageReaderSpi}s, or430* {@code null}.431*432* @see javax.imageio.ImageIO#getImageReader(ImageWriter)433* @see ImageReaderSpi#getImageWriterSpiNames()434*/435public String[] getImageReaderSpiNames() {436return readerSpiNames == null ?437null : readerSpiNames.clone();438}439}440441442