Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/imageio/spi/ImageWriterSpi.java
38830 views
/*1* Copyright (c) 1999, 2013, 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</code>s.35* For more information on service provider classes, see the class comment36* for the <code>IIORegistry</code> class.37*38* <p> Each <code>ImageWriterSpi</code> provides several types of information39* about the <code>ImageWriter</code> 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>, <code>getDescription</code>,44* and <code>getVersion</code> 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>, <code>getFileSuffixes</code>, and53* <code>getMIMEType</code> methods. These methods may be used to54* identify candidate <code>ImageWriter</code>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</code>s59* are likely to be able to parse a particular data stream is provided60* by the <code>canEncodeImage</code> method. This methods allows the61* service provider to inspect the actual image contents.62*63* <p> Finally, an instance of the <code>ImageWriter</code> class64* associated with this service provider may be obtained by calling65* the <code>createWriterInstance</code> 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</code>, to be returned from80* <code>getOutputTypes</code>.81* @deprecated Instead of using this field, directly create82* the equivalent array <code>{ ImageOutputStream.class }</code>.83*/84@Deprecated85public static final Class[] STANDARD_OUTPUT_TYPE =86{ ImageOutputStream.class };8788/**89* An array of <code>Class</code> objects to be returned from90* <code>getOutputTypes</code>, initially <code>null</code>.91*/92protected Class[] outputTypes = null;9394/**95* An array of strings to be returned from96* <code>getImageReaderSpiNames</code>, initially97* <code>null</code>.98*/99protected String[] readerSpiNames = null;100101/**102* The <code>Class</code> of the writer, initially103* <code>null</code>.104*/105private Class writerClass = null;106107/**108* Constructs a blank <code>ImageWriterSpi</code>. 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</code> with a given118* set of values.119*120* @param vendorName the vendor name, as a non-<code>null</code>121* <code>String</code>.122* @param version a version identifier, as a non-<code>null</code>123* <code>String</code>.124* @param names a non-<code>null</code> array of125* <code>String</code>s indicating the format names. At least one126* entry must be present.127* @param suffixes an array of <code>String</code>s indicating the128* common file suffixes. If no suffixes are defined,129* <code>null</code> should be supplied. An array of length 0130* will be normalized to <code>null</code>.131* @param MIMETypes an array of <code>String</code>s indicating132* the format's MIME types. If no suffixes are defined,133* <code>null</code> should be supplied. An array of length 0134* will be normalized to <code>null</code>.135* @param writerClassName the fully-qualified name of the136* associated <code>ImageWriterSpi</code> class, as a137* non-<code>null</code> <code>String</code>.138* @param outputTypes an array of <code>Class</code> objects of139* length at least 1 indicating the legal output types.140* @param readerSpiNames an array <code>String</code>s of length141* at least 1 naming the classes of all associated142* <code>ImageReader</code>s, or <code>null</code>. An array of143* length 0 is normalized to <code>null</code>.144* @param supportsStandardStreamMetadataFormat a145* <code>boolean</code> that indicates whether a stream metadata146* object can use trees described by the standard metadata format.147* @param nativeStreamMetadataFormatName a148* <code>String</code>, or <code>null</code>, to be returned from149* <code>getNativeStreamMetadataFormatName</code>.150* @param nativeStreamMetadataFormatClassName a151* <code>String</code>, or <code>null</code>, to be used to instantiate152* a metadata format object to be returned from153* <code>getNativeStreamMetadataFormat</code>.154* @param extraStreamMetadataFormatNames an array of155* <code>String</code>s, or <code>null</code>, to be returned from156* <code>getExtraStreamMetadataFormatNames</code>. An array of length157* 0 is normalized to <code>null</code>.158* @param extraStreamMetadataFormatClassNames an array of159* <code>String</code>s, or <code>null</code>, to be used to instantiate160* a metadata format object to be returned from161* <code>getStreamMetadataFormat</code>. An array of length162* 0 is normalized to <code>null</code>.163* @param supportsStandardImageMetadataFormat a164* <code>boolean</code> that indicates whether an image metadata165* object can use trees described by the standard metadata format.166* @param nativeImageMetadataFormatName a167* <code>String</code>, or <code>null</code>, to be returned from168* <code>getNativeImageMetadataFormatName</code>.169* @param nativeImageMetadataFormatClassName a170* <code>String</code>, or <code>null</code>, to be used to instantiate171* a metadata format object to be returned from172* <code>getNativeImageMetadataFormat</code>.173* @param extraImageMetadataFormatNames an array of174* <code>String</code>s to be returned from175* <code>getExtraImageMetadataFormatNames</code>. An array of length 0176* is normalized to <code>null</code>.177* @param extraImageMetadataFormatClassNames an array of178* <code>String</code>s, or <code>null</code>, to be used to instantiate179* a metadata format object to be returned from180* <code>getImageMetadataFormat</code>. An array of length181* 0 is normalized to <code>null</code>.182*183* @exception IllegalArgumentException if <code>vendorName</code>184* is <code>null</code>.185* @exception IllegalArgumentException if <code>version</code>186* is <code>null</code>.187* @exception IllegalArgumentException if <code>names</code>188* is <code>null</code> or has length 0.189* @exception IllegalArgumentException if <code>writerClassName</code>190* is <code>null</code>.191* @exception IllegalArgumentException if <code>outputTypes</code>192* is <code>null</code> 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 = (String[])readerSpiNames.clone();241}242}243244/**245* Returns <code>true</code> if the format that this writer246* outputs preserves pixel data bit-accurately. The default247* implementation returns <code>true</code>.248*249* @return <code>true</code> if the format preserves full pixel250* accuracy.251*/252public boolean isFormatLossless() {253return true;254}255256/**257* Returns an array of <code>Class</code> objects indicating what258* types of objects may be used as arguments to the writer's259* <code>setOutput</code> method.260*261* <p> For most writers, which only output to an262* <code>ImageOutputStream</code>, a single-element array263* containing <code>ImageOutputStream.class</code> should be264* returned.265*266* @return a non-<code>null</code> array of267* <code>Class</code>objects of length at least 1.268*/269public Class[] getOutputTypes() {270return (Class[])outputTypes.clone();271}272273/**274* Returns <code>true</code> if the <code>ImageWriter</code>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</code> and278* <code>ColorModel</code>) is described by an279* <code>ImageTypeSpecifier</code> object.280*281* <p> A return value of <code>true</code> 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</code> if handed an RGB <code>BufferedImage</code>.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</code>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</code> specifying the301* layout of the image to be written.302*303* @return <code>true</code> if this writer is likely to be able304* to encode images with the given layout.305*306* @exception IllegalArgumentException if <code>type</code>307* is <code>null</code>.308*/309public abstract boolean canEncodeImage(ImageTypeSpecifier type);310311/**312* Returns <code>true</code> if the <code>ImageWriter</code>313* implementation associated with this service provider is able to314* encode the given <code>RenderedImage</code> instance. Note315* that this includes instances of316* <code>java.awt.image.BufferedImage</code>.317*318* <p> See the discussion for319* <code>canEncodeImage(ImageTypeSpecifier)</code> for information320* on the semantics of this method.321*322* @param im an instance of <code>RenderedImage</code> to be encoded.323*324* @return <code>true</code> if this writer is likely to be able325* to encode this image.326*327* @exception IllegalArgumentException if <code>im</code>328* is <code>null</code>.329*/330public boolean canEncodeImage(RenderedImage im) {331return canEncodeImage(ImageTypeSpecifier.createFromRenderedImage(im));332}333334/**335* Returns an instance of the <code>ImageWriter</code>336* implementation associated with this service provider.337* The returned object will initially be in an initial state as if338* its <code>reset</code> method had been called.339*340* <p> The default implementation simply returns341* <code>createWriterInstance(null)</code>.342*343* @return an <code>ImageWriter</code> 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</code>355* implementation associated with this service provider.356* The returned object will initially be in an initial state357* as if its <code>reset</code> method had been called.358*359* <p> An <code>Object</code> 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)</code>.365*366* @param extension a plug-in specific extension object, which may367* be <code>null</code>.368*369* @return an <code>ImageWriter</code> instance.370*371* @exception IOException if the attempt to instantiate372* the writer fails.373* @exception IllegalArgumentException if the374* <code>ImageWriter</code>'s constructor throws an375* <code>IllegalArgumentException</code> to indicate that the376* extension object is unsuitable.377*/378public abstract ImageWriter createWriterInstance(Object extension)379throws IOException;380381/**382* Returns <code>true</code> if the <code>ImageWriter</code> object383* passed in is an instance of the <code>ImageWriter</code>384* associated with this service provider.385*386* @param writer an <code>ImageWriter</code> instance.387*388* @return <code>true</code> if <code>writer</code> is recognized389*390* @exception IllegalArgumentException if <code>writer</code> is391* <code>null</code>.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</code>s containing all the403* fully qualified names of all the <code>ImageReaderSpi</code>404* classes that can understand the internal metadata405* representation used by the <code>ImageWriter</code> associated406* with this service provider, or <code>null</code> if there are407* no such <code>ImageReaders</code> specified. If a408* non-<code>null</code> 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</code> returned by414* <code>ImageIO.getImageReader(ImageWriter)</code>.415*416* <p> This mechanism may be used to obtain417* <code>ImageReaders</code> that will generated non-pixel418* meta-data (see <code>IIOExtraDataInfo</code>) in a structure419* understood by an <code>ImageWriter</code>. By reading the420* image and obtaining this data from one of the421* <code>ImageReaders</code> obtained with this method and passing422* it on to the <code>ImageWriter</code>, 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</code>s of length at least 1429* containing names of <code>ImageReaderSpi</code>s, or430* <code>null</code>.431*432* @see javax.imageio.ImageIO#getImageReader(ImageWriter)433* @see ImageReaderSpi#getImageWriterSpiNames()434*/435public String[] getImageReaderSpiNames() {436return readerSpiNames == null ?437null : (String[])readerSpiNames.clone();438}439}440441442