Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/imageio/IIOImage.java
38829 views
/*1* Copyright (c) 2000, 2004, 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;2627import java.awt.image.BufferedImage;28import java.awt.image.Raster;29import java.awt.image.RenderedImage;30import java.util.List;31import javax.imageio.metadata.IIOMetadata;3233/**34* A simple container class to aggregate an image, a set of35* thumbnail (preview) images, and an object representing metadata36* associated with the image.37*38* <p> The image data may take the form of either a39* <code>RenderedImage</code>, or a <code>Raster</code>. Reader40* methods that return an <code>IIOImage</code> will always return a41* <code>BufferedImage</code> using the <code>RenderedImage</code>42* reference. Writer methods that accept an <code>IIOImage</code>43* will always accept a <code>RenderedImage</code>, and may optionally44* accept a <code>Raster</code>.45*46* <p> Exactly one of <code>getRenderedImage</code> and47* <code>getRaster</code> will return a non-<code>null</code> value.48* Subclasses are responsible for ensuring this behavior.49*50* @see ImageReader#readAll(int, ImageReadParam)51* @see ImageReader#readAll(java.util.Iterator)52* @see ImageWriter#write(javax.imageio.metadata.IIOMetadata,53* IIOImage, ImageWriteParam)54* @see ImageWriter#write(IIOImage)55* @see ImageWriter#writeToSequence(IIOImage, ImageWriteParam)56* @see ImageWriter#writeInsert(int, IIOImage, ImageWriteParam)57*58*/59public class IIOImage {6061/**62* The <code>RenderedImage</code> being referenced.63*/64protected RenderedImage image;6566/**67* The <code>Raster</code> being referenced.68*/69protected Raster raster;7071/**72* A <code>List</code> of <code>BufferedImage</code> thumbnails,73* or <code>null</code>. Non-<code>BufferedImage</code> objects74* must not be stored in this <code>List</code>.75*/76protected List<? extends BufferedImage> thumbnails = null;7778/**79* An <code>IIOMetadata</code> object containing metadata80* associated with the image.81*/82protected IIOMetadata metadata;8384/**85* Constructs an <code>IIOImage</code> containing a86* <code>RenderedImage</code>, and thumbnails and metadata87* associated with it.88*89* <p> All parameters are stored by reference.90*91* <p> The <code>thumbnails</code> argument must either be92* <code>null</code> or contain only <code>BufferedImage</code>93* objects.94*95* @param image a <code>RenderedImage</code>.96* @param thumbnails a <code>List</code> of <code>BufferedImage</code>s,97* or <code>null</code>.98* @param metadata an <code>IIOMetadata</code> object, or99* <code>null</code>.100*101* @exception IllegalArgumentException if <code>image</code> is102* <code>null</code>.103*/104public IIOImage(RenderedImage image,105List<? extends BufferedImage> thumbnails,106IIOMetadata metadata) {107if (image == null) {108throw new IllegalArgumentException("image == null!");109}110this.image = image;111this.raster = null;112this.thumbnails = thumbnails;113this.metadata = metadata;114}115116/**117* Constructs an <code>IIOImage</code> containing a118* <code>Raster</code>, and thumbnails and metadata119* associated with it.120*121* <p> All parameters are stored by reference.122*123* @param raster a <code>Raster</code>.124* @param thumbnails a <code>List</code> of <code>BufferedImage</code>s,125* or <code>null</code>.126* @param metadata an <code>IIOMetadata</code> object, or127* <code>null</code>.128*129* @exception IllegalArgumentException if <code>raster</code> is130* <code>null</code>.131*/132public IIOImage(Raster raster,133List<? extends BufferedImage> thumbnails,134IIOMetadata metadata) {135if (raster == null) {136throw new IllegalArgumentException("raster == null!");137}138this.raster = raster;139this.image = null;140this.thumbnails = thumbnails;141this.metadata = metadata;142}143144/**145* Returns the currently set <code>RenderedImage</code>, or146* <code>null</code> if only a <code>Raster</code> is available.147*148* @return a <code>RenderedImage</code>, or <code>null</code>.149*150* @see #setRenderedImage151*/152public RenderedImage getRenderedImage() {153synchronized(this) {154return image;155}156}157158/**159* Sets the current <code>RenderedImage</code>. The value is160* stored by reference. Any existing <code>Raster</code> is161* discarded.162*163* @param image a <code>RenderedImage</code>.164*165* @exception IllegalArgumentException if <code>image</code> is166* <code>null</code>.167*168* @see #getRenderedImage169*/170public void setRenderedImage(RenderedImage image) {171synchronized(this) {172if (image == null) {173throw new IllegalArgumentException("image == null!");174}175this.image = image;176this.raster = null;177}178}179180/**181* Returns <code>true</code> if this <code>IIOImage</code> stores182* a <code>Raster</code> rather than a <code>RenderedImage</code>.183*184* @return <code>true</code> if a <code>Raster</code> is185* available.186*/187public boolean hasRaster() {188synchronized(this) {189return (raster != null);190}191}192193/**194* Returns the currently set <code>Raster</code>, or195* <code>null</code> if only a <code>RenderedImage</code> is196* available.197*198* @return a <code>Raster</code>, or <code>null</code>.199*200* @see #setRaster201*/202public Raster getRaster() {203synchronized(this) {204return raster;205}206}207208/**209* Sets the current <code>Raster</code>. The value is210* stored by reference. Any existing <code>RenderedImage</code> is211* discarded.212*213* @param raster a <code>Raster</code>.214*215* @exception IllegalArgumentException if <code>raster</code> is216* <code>null</code>.217*218* @see #getRaster219*/220public void setRaster(Raster raster) {221synchronized(this) {222if (raster == null) {223throw new IllegalArgumentException("raster == null!");224}225this.raster = raster;226this.image = null;227}228}229230/**231* Returns the number of thumbnails stored in this232* <code>IIOImage</code>.233*234* @return the number of thumbnails, as an <code>int</code>.235*/236public int getNumThumbnails() {237return thumbnails == null ? 0 : thumbnails.size();238}239240/**241* Returns a thumbnail associated with the main image.242*243* @param index the index of the desired thumbnail image.244*245* @return a thumbnail image, as a <code>BufferedImage</code>.246*247* @exception IndexOutOfBoundsException if the supplied index is248* negative or larger than the largest valid index.249* @exception ClassCastException if a250* non-<code>BufferedImage</code> object is encountered in the251* list of thumbnails at the given index.252*253* @see #getThumbnails254* @see #setThumbnails255*/256public BufferedImage getThumbnail(int index) {257if (thumbnails == null) {258throw new IndexOutOfBoundsException("No thumbnails available!");259}260return (BufferedImage)thumbnails.get(index);261}262263/**264* Returns the current <code>List</code> of thumbnail265* <code>BufferedImage</code>s, or <code>null</code> if none is266* set. A live reference is returned.267*268* @return the current <code>List</code> of269* <code>BufferedImage</code> thumbnails, or <code>null</code>.270*271* @see #getThumbnail(int)272* @see #setThumbnails273*/274public List<? extends BufferedImage> getThumbnails() {275return thumbnails;276}277278/**279* Sets the list of thumbnails to a new <code>List</code> of280* <code>BufferedImage</code>s, or to <code>null</code>. The281* reference to the previous <code>List</code> is discarded.282*283* <p> The <code>thumbnails</code> argument must either be284* <code>null</code> or contain only <code>BufferedImage</code>285* objects.286*287* @param thumbnails a <code>List</code> of288* <code>BufferedImage</code> thumbnails, or <code>null</code>.289*290* @see #getThumbnail(int)291* @see #getThumbnails292*/293public void setThumbnails(List<? extends BufferedImage> thumbnails) {294this.thumbnails = thumbnails;295}296297/**298* Returns a reference to the current <code>IIOMetadata</code>299* object, or <code>null</code> is none is set.300*301* @return an <code>IIOMetadata</code> object, or <code>null</code>.302*303* @see #setMetadata304*/305public IIOMetadata getMetadata() {306return metadata;307}308309/**310* Sets the <code>IIOMetadata</code> to a new object, or311* <code>null</code>.312*313* @param metadata an <code>IIOMetadata</code> object, or314* <code>null</code>.315*316* @see #getMetadata317*/318public void setMetadata(IIOMetadata metadata) {319this.metadata = metadata;320}321}322323324