Path: blob/master/src/java.desktop/share/classes/javax/imageio/IIOImage.java
41152 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}, or a {@code Raster}. Reader40* methods that return an {@code IIOImage} will always return a41* {@code BufferedImage} using the {@code RenderedImage}42* reference. Writer methods that accept an {@code IIOImage}43* will always accept a {@code RenderedImage}, and may optionally44* accept a {@code Raster}.45*46* <p> Exactly one of {@code getRenderedImage} and47* {@code getRaster} will return a non-{@code null} 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} being referenced.63*/64protected RenderedImage image;6566/**67* The {@code Raster} being referenced.68*/69protected Raster raster;7071/**72* A {@code List} of {@code BufferedImage} thumbnails,73* or {@code null}. Non-{@code BufferedImage} objects74* must not be stored in this {@code List}.75*/76protected List<? extends BufferedImage> thumbnails = null;7778/**79* An {@code IIOMetadata} object containing metadata80* associated with the image.81*/82protected IIOMetadata metadata;8384/**85* Constructs an {@code IIOImage} containing a86* {@code RenderedImage}, and thumbnails and metadata87* associated with it.88*89* <p> All parameters are stored by reference.90*91* <p> The {@code thumbnails} argument must either be92* {@code null} or contain only {@code BufferedImage}93* objects.94*95* @param image a {@code RenderedImage}.96* @param thumbnails a {@code List} of {@code BufferedImage}s,97* or {@code null}.98* @param metadata an {@code IIOMetadata} object, or99* {@code null}.100*101* @exception IllegalArgumentException if {@code image} is102* {@code null}.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} containing a118* {@code Raster}, and thumbnails and metadata119* associated with it.120*121* <p> All parameters are stored by reference.122*123* @param raster a {@code Raster}.124* @param thumbnails a {@code List} of {@code BufferedImage}s,125* or {@code null}.126* @param metadata an {@code IIOMetadata} object, or127* {@code null}.128*129* @exception IllegalArgumentException if {@code raster} is130* {@code null}.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}, or146* {@code null} if only a {@code Raster} is available.147*148* @return a {@code RenderedImage}, or {@code null}.149*150* @see #setRenderedImage151*/152public RenderedImage getRenderedImage() {153synchronized(this) {154return image;155}156}157158/**159* Sets the current {@code RenderedImage}. The value is160* stored by reference. Any existing {@code Raster} is161* discarded.162*163* @param image a {@code RenderedImage}.164*165* @exception IllegalArgumentException if {@code image} is166* {@code null}.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} if this {@code IIOImage} stores182* a {@code Raster} rather than a {@code RenderedImage}.183*184* @return {@code true} if a {@code Raster} is185* available.186*/187public boolean hasRaster() {188synchronized(this) {189return (raster != null);190}191}192193/**194* Returns the currently set {@code Raster}, or195* {@code null} if only a {@code RenderedImage} is196* available.197*198* @return a {@code Raster}, or {@code null}.199*200* @see #setRaster201*/202public Raster getRaster() {203synchronized(this) {204return raster;205}206}207208/**209* Sets the current {@code Raster}. The value is210* stored by reference. Any existing {@code RenderedImage} is211* discarded.212*213* @param raster a {@code Raster}.214*215* @exception IllegalArgumentException if {@code raster} is216* {@code null}.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}.233*234* @return the number of thumbnails, as an {@code int}.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}.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} 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} of thumbnail265* {@code BufferedImage}s, or {@code null} if none is266* set. A live reference is returned.267*268* @return the current {@code List} of269* {@code BufferedImage} thumbnails, or {@code null}.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} of280* {@code BufferedImage}s, or to {@code null}. The281* reference to the previous {@code List} is discarded.282*283* <p> The {@code thumbnails} argument must either be284* {@code null} or contain only {@code BufferedImage}285* objects.286*287* @param thumbnails a {@code List} of288* {@code BufferedImage} thumbnails, or {@code null}.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}299* object, or {@code null} is none is set.300*301* @return an {@code IIOMetadata} object, or {@code null}.302*303* @see #setMetadata304*/305public IIOMetadata getMetadata() {306return metadata;307}308309/**310* Sets the {@code IIOMetadata} to a new object, or311* {@code null}.312*313* @param metadata an {@code IIOMetadata} object, or314* {@code null}.315*316* @see #getMetadata317*/318public void setMetadata(IIOMetadata metadata) {319this.metadata = metadata;320}321}322323324