2898 lines · 116.6 KB · Java
RawDownload
/* * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.imageio; import java.awt.Point;import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.awt.image.Raster;import java.awt.image.RenderedImage;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.MissingResourceException;import java.util.ResourceBundle;import java.util.Set;import javax.imageio.spi.ImageReaderSpi;import javax.imageio.event.IIOReadWarningListener;import javax.imageio.event.IIOReadProgressListener;import javax.imageio.event.IIOReadUpdateListener;import javax.imageio.metadata.IIOMetadata;import javax.imageio.metadata.IIOMetadataFormatImpl;import javax.imageio.stream.ImageInputStream; /** * An abstract superclass for parsing and decoding of images.  This * class must be subclassed by classes that read in images in the * context of the Java Image I/O framework. * * <p> <code>ImageReader</code> objects are normally instantiated by * the service provider interface (SPI) class for the specific format. * Service provider classes (e.g., instances of * <code>ImageReaderSpi</code>) are registered with the * <code>IIORegistry</code>, which uses them for format recognition * and presentation of available format readers and writers. * * <p> When an input source is set (using the <code>setInput</code> * method), it may be marked as "seek forward only".  This setting * means that images contained within the input source will only be * read in order, possibly allowing the reader to avoid caching * portions of the input containing data associated with images that * have been read previously. * * @see ImageWriter * @see javax.imageio.spi.IIORegistry * @see javax.imageio.spi.ImageReaderSpi * */public abstract class ImageReader {     /**     * The <code>ImageReaderSpi</code> that instantiated this object,     * or <code>null</code> if its identity is not known or none     * exists.  By default it is initialized to <code>null</code>.     */    protected ImageReaderSpi originatingProvider;     /**     * The <code>ImageInputStream</code> or other     * <code>Object</code> by <code>setInput</code> and retrieved     * by <code>getInput</code>.  By default it is initialized to     * <code>null</code>.     */    protected Object input = null;     /**     * <code>true</code> if the current input source has been marked     * as allowing only forward seeking by <code>setInput</code>.  By     * default, the value is <code>false</code>.     *     * @see #minIndex     * @see #setInput     */    protected boolean seekForwardOnly = false;     /**     * <code>true</code> if the current input source has been marked     * as allowing metadata to be ignored by <code>setInput</code>.     * By default, the value is <code>false</code>.     *     * @see #setInput     */    protected boolean ignoreMetadata = false;     /**     * The smallest valid index for reading, initially 0.  When     * <code>seekForwardOnly</code> is <code>true</code>, various methods     * may throw an <code>IndexOutOfBoundsException</code> on an     * attempt to access data associate with an image having a lower     * index.     *     * @see #seekForwardOnly     * @see #setInput     */    protected int minIndex = 0;     /**     * An array of <code>Locale</code>s which may be used to localize     * warning messages, or <code>null</code> if localization is not     * supported.     */    protected Locale[] availableLocales = null;     /**     * The current <code>Locale</code> to be used for localization, or     * <code>null</code> if none has been set.     */    protected Locale locale = null;     /**     * A <code>List</code> of currently registered     * <code>IIOReadWarningListener</code>s, initialized by default to     * <code>null</code>, which is synonymous with an empty     * <code>List</code>.     */    protected List<IIOReadWarningListener> warningListeners = null;     /**     * A <code>List</code> of the <code>Locale</code>s associated with     * each currently registered <code>IIOReadWarningListener</code>,     * initialized by default to <code>null</code>, which is     * synonymous with an empty <code>List</code>.     */    protected List<Locale> warningLocales = null;     /**     * A <code>List</code> of currently registered     * <code>IIOReadProgressListener</code>s, initialized by default     * to <code>null</code>, which is synonymous with an empty     * <code>List</code>.     */    protected List<IIOReadProgressListener> progressListeners = null;     /**     * A <code>List</code> of currently registered     * <code>IIOReadUpdateListener</code>s, initialized by default to     * <code>null</code>, which is synonymous with an empty     * <code>List</code>.     */    protected List<IIOReadUpdateListener> updateListeners = null;     /**     * If <code>true</code>, the current read operation should be     * aborted.     */    private boolean abortFlag = false;     /**     * Constructs an <code>ImageReader</code> and sets its     * <code>originatingProvider</code> field to the supplied value.     *     * <p> Subclasses that make use of extensions should provide a     * constructor with signature <code>(ImageReaderSpi,     * Object)</code> in order to retrieve the extension object.  If     * the extension object is unsuitable, an     * <code>IllegalArgumentException</code> should be thrown.     *     * @param originatingProvider the <code>ImageReaderSpi</code> that is     * invoking this constructor, or <code>null</code>.     */    protected ImageReader(ImageReaderSpi originatingProvider) {        this.originatingProvider = originatingProvider;    }     /**     * Returns a <code>String</code> identifying the format of the     * input source.     *     * <p> The default implementation returns     * <code>originatingProvider.getFormatNames()[0]</code>.     * Implementations that may not have an originating service     * provider, or which desire a different naming policy should     * override this method.     *     * @exception IOException if an error occurs reading the     * information from the input source.     *     * @return the format name, as a <code>String</code>.     */    public String getFormatName() throws IOException {        return originatingProvider.getFormatNames()[0];    }     /**     * Returns the <code>ImageReaderSpi</code> that was passed in on     * the constructor.  Note that this value may be <code>null</code>.     *     * @return an <code>ImageReaderSpi</code>, or <code>null</code>.     *     * @see ImageReaderSpi     */    public ImageReaderSpi getOriginatingProvider() {        return originatingProvider;    }     /**     * Sets the input source to use to the given     * <code>ImageInputStream</code> or other <code>Object</code>.     * The input source must be set before any of the query or read     * methods are used.  If <code>input</code> is <code>null</code>,     * any currently set input source will be removed.  In any case,     * the value of <code>minIndex</code> will be initialized to 0.     *     * <p> The <code>seekForwardOnly</code> parameter controls whether     * the value returned by <code>getMinIndex</code> will be     * increased as each image (or thumbnail, or image metadata) is     * read.  If <code>seekForwardOnly</code> is true, then a call to     * <code>read(index)</code> will throw an     * <code>IndexOutOfBoundsException</code> if <code>index &lt     * this.minIndex</code>; otherwise, the value of     * <code>minIndex</code> will be set to <code>index</code>.  If     * <code>seekForwardOnly</code> is <code>false</code>, the value of     * <code>minIndex</code> will remain 0 regardless of any read     * operations.     *     * <p> The <code>ignoreMetadata</code> parameter, if set to     * <code>true</code>, allows the reader to disregard any metadata     * encountered during the read.  Subsequent calls to the     * <code>getStreamMetadata</code> and     * <code>getImageMetadata</code> methods may return     * <code>null</code>, and an <code>IIOImage</code> returned from     * <code>readAll</code> may return <code>null</code> from their     * <code>getMetadata</code> method.  Setting this parameter may     * allow the reader to work more efficiently.  The reader may     * choose to disregard this setting and return metadata normally.     *     * <p> Subclasses should take care to remove any cached     * information based on the previous stream, such as header     * information or partially decoded image data.     *     * <p> Use of a general <code>Object</code> other than an     * <code>ImageInputStream</code> is intended for readers that     * interact directly with a capture device or imaging protocol.     * The set of legal classes is advertised by the reader's service     * provider's <code>getInputTypes</code> method; most readers     * will return a single-element array containing only     * <code>ImageInputStream.class</code> to indicate that they     * accept only an <code>ImageInputStream</code>.     *     * <p> The default implementation checks the <code>input</code>     * argument against the list returned by     * <code>originatingProvider.getInputTypes()</code> and fails     * if the argument is not an instance of one of the classes     * in the list.  If the originating provider is set to     * <code>null</code>, the input is accepted only if it is an     * <code>ImageInputStream</code>.     *     * @param input the <code>ImageInputStream</code> or other     * <code>Object</code> to use for future decoding.     * @param seekForwardOnly if <code>true</code>, images and metadata     * may only be read in ascending order from this input source.     * @param ignoreMetadata if <code>true</code>, metadata     * may be ignored during reads.     *     * @exception IllegalArgumentException if <code>input</code> is     * not an instance of one of the classes returned by the     * originating service provider's <code>getInputTypes</code>     * method, or is not an <code>ImageInputStream</code>.     *     * @see ImageInputStream     * @see #getInput     * @see javax.imageio.spi.ImageReaderSpi#getInputTypes     */    public void setInput(Object input,                         boolean seekForwardOnly,                         boolean ignoreMetadata) {        if (input != null) {            boolean found = false;            if (originatingProvider != null) {                Class[] classes = originatingProvider.getInputTypes();                for (int i = 0; i < classes.length; i++) {                    if (classes[i].isInstance(input)) {                        found = true;                        break;                    }                }            } else {                if (input instanceof ImageInputStream) {                    found = true;                }            }            if (!found) {                throw new IllegalArgumentException("Incorrect input type!");            }             this.seekForwardOnly = seekForwardOnly;            this.ignoreMetadata = ignoreMetadata;            this.minIndex = 0;        }         this.input = input;    }     /**     * Sets the input source to use to the given     * <code>ImageInputStream</code> or other <code>Object</code>.     * The input source must be set before any of the query or read     * methods are used.  If <code>input</code> is <code>null</code>,     * any currently set input source will be removed.  In any case,     * the value of <code>minIndex</code> will be initialized to 0.     *     * <p> The <code>seekForwardOnly</code> parameter controls whether     * the value returned by <code>getMinIndex</code> will be     * increased as each image (or thumbnail, or image metadata) is     * read.  If <code>seekForwardOnly</code> is true, then a call to     * <code>read(index)</code> will throw an     * <code>IndexOutOfBoundsException</code> if <code>index &lt     * this.minIndex</code>; otherwise, the value of     * <code>minIndex</code> will be set to <code>index</code>.  If     * <code>seekForwardOnly</code> is <code>false</code>, the value of     * <code>minIndex</code> will remain 0 regardless of any read     * operations.     *     * <p> This method is equivalent to <code>setInput(input,     * seekForwardOnly, false)</code>.     *     * @param input the <code>ImageInputStream</code> or other     * <code>Object</code> to use for future decoding.     * @param seekForwardOnly if <code>true</code>, images and metadata     * may only be read in ascending order from this input source.     *     * @exception IllegalArgumentException if <code>input</code> is     * not an instance of one of the classes returned by the     * originating service provider's <code>getInputTypes</code>     * method, or is not an <code>ImageInputStream</code>.     *     * @see #getInput     */    public void setInput(Object input,                         boolean seekForwardOnly) {        setInput(input, seekForwardOnly, false);    }     /**     * Sets the input source to use to the given     * <code>ImageInputStream</code> or other <code>Object</code>.     * The input source must be set before any of the query or read     * methods are used.  If <code>input</code> is <code>null</code>,     * any currently set input source will be removed.  In any case,     * the value of <code>minIndex</code> will be initialized to 0.     *     * <p> This method is equivalent to <code>setInput(input, false,     * false)</code>.     *     * @param input the <code>ImageInputStream</code> or other     * <code>Object</code> to use for future decoding.     *     * @exception IllegalArgumentException if <code>input</code> is     * not an instance of one of the classes returned by the     * originating service provider's <code>getInputTypes</code>     * method, or is not an <code>ImageInputStream</code>.     *     * @see #getInput     */    public void setInput(Object input) {        setInput(input, false, false);    }     /**     * Returns the <code>ImageInputStream</code> or other     * <code>Object</code> previously set as the input source.  If the     * input source has not been set, <code>null</code> is returned.     *     * @return the <code>Object</code> that will be used for future     * decoding, or <code>null</code>.     *     * @see ImageInputStream     * @see #setInput     */    public Object getInput() {        return input;    }     /**     * Returns <code>true</code> if the current input source has been     * marked as seek forward only by passing <code>true</code> as the     * <code>seekForwardOnly</code> argument to the     * <code>setInput</code> method.     *     * @return <code>true</code> if the input source is seek forward     * only.     *     * @see #setInput     */    public boolean isSeekForwardOnly() {        return seekForwardOnly;    }     /**     * Returns <code>true</code> if the current input source has been     * marked as allowing metadata to be ignored by passing     * <code>true</code> as the <code>ignoreMetadata</code> argument     * to the <code>setInput</code> method.     *     * @return <code>true</code> if the metadata may be ignored.     *     * @see #setInput     */    public boolean isIgnoringMetadata() {        return ignoreMetadata;    }     /**     * Returns the lowest valid index for reading an image, thumbnail,     * or image metadata.  If <code>seekForwardOnly()</code> is     * <code>false</code>, this value will typically remain 0,     * indicating that random access is possible.  Otherwise, it will     * contain the value of the most recently accessed index, and     * increase in a monotonic fashion.     *     * @return the minimum legal index for reading.     */    public int getMinIndex() {        return minIndex;    }     // Localization     /**     * Returns an array of <code>Locale</code>s that may be used to     * localize warning listeners and compression settings.  A return     * value of <code>null</code> indicates that localization is not     * supported.     *     * <p> The default implementation returns a clone of the     * <code>availableLocales</code> instance variable if it is     * non-<code>null</code>, or else returns <code>null</code>.     *     * @return an array of <code>Locale</code>s that may be used as     * arguments to <code>setLocale</code>, or <code>null</code>.     */    public Locale[] getAvailableLocales() {        if (availableLocales == null) {            return null;        } else {            return (Locale[])availableLocales.clone();        }    }     /**     * Sets the current <code>Locale</code> of this     * <code>ImageReader</code> to the given value.  A value of     * <code>null</code> removes any previous setting, and indicates     * that the reader should localize as it sees fit.     *     * @param locale the desired <code>Locale</code>, or     * <code>null</code>.     *     * @exception IllegalArgumentException if <code>locale</code> is     * non-<code>null</code> but is not one of the values returned by     * <code>getAvailableLocales</code>.     *     * @see #getLocale     */    public void setLocale(Locale locale) {        if (locale != null) {            Locale[] locales = getAvailableLocales();            boolean found = false;            if (locales != null) {                for (int i = 0; i < locales.length; i++) {                    if (locale.equals(locales[i])) {                        found = true;                        break;                    }                }            }            if (!found) {                throw new IllegalArgumentException("Invalid locale!");            }        }        this.locale = locale;    }     /**     * Returns the currently set <code>Locale</code>, or     * <code>null</code> if none has been set.

Lines 1–500 of 2898.