Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/imageio/ImageReadParam.java
38829 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;2627import java.awt.Dimension;28import java.awt.image.BufferedImage;2930/**31* A class describing how a stream is to be decoded. Instances of32* this class or its subclasses are used to supply prescriptive33* "how-to" information to instances of <code>ImageReader</code>.34*35* <p> An image encoded as part of a file or stream may be thought of36* extending out in multiple dimensions: the spatial dimensions of37* width and height, a number of bands, and a number of progressive38* decoding passes. This class allows a contiguous (hyper)rectangular39* subarea of the image in all of these dimensions to be selected for40* decoding. Additionally, the spatial dimensions may be subsampled41* discontinuously. Finally, color and format conversions may be42* specified by controlling the <code>ColorModel</code> and43* <code>SampleModel</code> of the destination image, either by44* providing a <code>BufferedImage</code> or by using an45* <code>ImageTypeSpecifier</code>.46*47* <p> An <code>ImageReadParam</code> object is used to specify how an48* image, or a set of images, will be converted on input from49* a stream in the context of the Java Image I/O framework. A plug-in for a50* specific image format will return instances of51* <code>ImageReadParam</code> from the52* <code>getDefaultReadParam</code> method of its53* <code>ImageReader</code> implementation.54*55* <p> The state maintained by an instance of56* <code>ImageReadParam</code> is independent of any particular image57* being decoded. When actual decoding takes place, the values set in58* the read param are combined with the actual properties of the image59* being decoded from the stream and the destination60* <code>BufferedImage</code> that will receive the decoded pixel61* data. For example, the source region set using62* <code>setSourceRegion</code> will first be intersected with the63* actual valid source area. The result will be translated by the64* value returned by <code>getDestinationOffset</code>, and the65* resulting rectangle intersected with the actual valid destination66* area to yield the destination area that will be written.67*68* <p> The parameters specified by an <code>ImageReadParam</code> are69* applied to an image as follows. First, if a rendering size has70* been set by <code>setSourceRenderSize</code>, the entire decoded71* image is rendered at the size given by72* <code>getSourceRenderSize</code>. Otherwise, the image has its73* natural size given by <code>ImageReader.getWidth</code> and74* <code>ImageReader.getHeight</code>.75*76* <p> Next, the image is clipped against the source region77* specified by <code>getSourceXOffset</code>, <code>getSourceYOffset</code>,78* <code>getSourceWidth</code>, and <code>getSourceHeight</code>.79*80* <p> The resulting region is then subsampled according to the81* factors given in {@link IIOParam#setSourceSubsampling82* IIOParam.setSourceSubsampling}. The first pixel,83* the number of pixels per row, and the number of rows all depend84* on the subsampling settings.85* Call the minimum X and Y coordinates of the resulting rectangle86* (<code>minX</code>, <code>minY</code>), its width <code>w</code>87* and its height <code>h</code>.88*89* <p> This rectangle is offset by90* (<code>getDestinationOffset().x</code>,91* <code>getDestinationOffset().y</code>) and clipped against the92* destination bounds. If no destination image has been set, the93* destination is defined to have a width of94* <code>getDestinationOffset().x</code> + <code>w</code>, and a95* height of <code>getDestinationOffset().y</code> + <code>h</code> so96* that all pixels of the source region may be written to the97* destination.98*99* <p> Pixels that land, after subsampling, within the destination100* image, and that are written in one of the progressive passes101* specified by <code>getSourceMinProgressivePass</code> and102* <code>getSourceNumProgressivePasses</code> are passed along to the103* next step.104*105* <p> Finally, the source samples of each pixel are mapped into106* destination bands according to the algorithm described in the107* comment for <code>setDestinationBands</code>.108*109* <p> Plug-in writers may extend the functionality of110* <code>ImageReadParam</code> by providing a subclass that implements111* additional, plug-in specific interfaces. It is up to the plug-in112* to document what interfaces are available and how they are to be113* used. Readers will silently ignore any extended features of an114* <code>ImageReadParam</code> subclass of which they are not aware.115* Also, they may ignore any optional features that they normally116* disable when creating their own <code>ImageReadParam</code>117* instances via <code>getDefaultReadParam</code>.118*119* <p> Note that unless a query method exists for a capability, it must120* be supported by all <code>ImageReader</code> implementations121* (<i>e.g.</i> source render size is optional, but subsampling must be122* supported).123*124*125* @see ImageReader126* @see ImageWriter127* @see ImageWriteParam128*/129public class ImageReadParam extends IIOParam {130131/**132* <code>true</code> if this <code>ImageReadParam</code> allows133* the source rendering dimensions to be set. By default, the134* value is <code>false</code>. Subclasses must set this value135* manually.136*137* <p> <code>ImageReader</code>s that do not support setting of138* the source render size should set this value to139* <code>false</code>.140*/141protected boolean canSetSourceRenderSize = false;142143/**144* The desired rendering width and height of the source, if145* <code>canSetSourceRenderSize</code> is <code>true</code>, or146* <code>null</code>.147*148* <p> <code>ImageReader</code>s that do not support setting of149* the source render size may ignore this value.150*/151protected Dimension sourceRenderSize = null;152153/**154* The current destination <code>BufferedImage</code>, or155* <code>null</code> if none has been set. By default, the value156* is <code>null</code>.157*/158protected BufferedImage destination = null;159160/**161* The set of destination bands to be used, as an array of162* <code>int</code>s. By default, the value is <code>null</code>,163* indicating all destination bands should be written in order.164*/165protected int[] destinationBands = null;166167/**168* The minimum index of a progressive pass to read from the169* source. By default, the value is set to 0, which indicates170* that passes starting with the first available pass should be171* decoded.172*173* <p> Subclasses should ensure that this value is174* non-negative.175*/176protected int minProgressivePass = 0;177178/**179* The maximum number of progressive passes to read from the180* source. By default, the value is set to181* <code>Integer.MAX_VALUE</code>, which indicates that passes up182* to and including the last available pass should be decoded.183*184* <p> Subclasses should ensure that this value is positive.185* Additionally, if the value is not186* <code>Integer.MAX_VALUE</code>, then <code>minProgressivePass +187* numProgressivePasses - 1</code> should not exceed188* <code>Integer.MAX_VALUE</code>.189*/190protected int numProgressivePasses = Integer.MAX_VALUE;191192/**193* Constructs an <code>ImageReadParam</code>.194*/195public ImageReadParam() {}196197// Comment inherited198public void setDestinationType(ImageTypeSpecifier destinationType) {199super.setDestinationType(destinationType);200setDestination(null);201}202203/**204* Supplies a <code>BufferedImage</code> to be used as the205* destination for decoded pixel data. The currently set image206* will be written to by the <code>read</code>,207* <code>readAll</code>, and <code>readRaster</code> methods, and208* a reference to it will be returned by those methods.209*210* <p> Pixel data from the aforementioned methods will be written211* starting at the offset specified by212* <code>getDestinationOffset</code>.213*214* <p> If <code>destination</code> is <code>null</code>, a215* newly-created <code>BufferedImage</code> will be returned by216* those methods.217*218* <p> At the time of reading, the image is checked to verify that219* its <code>ColorModel</code> and <code>SampleModel</code>220* correspond to one of the <code>ImageTypeSpecifier</code>s221* returned from the <code>ImageReader</code>'s222* <code>getImageTypes</code> method. If it does not, the reader223* will throw an <code>IIOException</code>.224*225* @param destination the BufferedImage to be written to, or226* <code>null</code>.227*228* @see #getDestination229*/230public void setDestination(BufferedImage destination) {231this.destination = destination;232}233234/**235* Returns the <code>BufferedImage</code> currently set by the236* <code>setDestination</code> method, or <code>null</code>237* if none is set.238*239* @return the BufferedImage to be written to.240*241* @see #setDestination242*/243public BufferedImage getDestination() {244return destination;245}246247/**248* Sets the indices of the destination bands where data249* will be placed. Duplicate indices are not allowed.250*251* <p> A <code>null</code> value indicates that all destination252* bands will be used.253*254* <p> Choosing a destination band subset will not affect the255* number of bands in the output image of a read if no destination256* image is specified; the created destination image will still257* have the same number of bands as if this method had never been258* called. If a different number of bands in the destination259* image is desired, an image must be supplied using the260* <code>ImageReadParam.setDestination</code> method.261*262* <p> At the time of reading or writing, an263* <code>IllegalArgumentException</code> will be thrown by the264* reader or writer if a value larger than the largest destination265* band index has been specified, or if the number of source bands266* and destination bands to be used differ. The267* <code>ImageReader.checkReadParamBandSettings</code> method may268* be used to automate this test.269*270* @param destinationBands an array of integer band indices to be271* used.272*273* @exception IllegalArgumentException if <code>destinationBands</code>274* contains a negative or duplicate value.275*276* @see #getDestinationBands277* @see #getSourceBands278* @see ImageReader#checkReadParamBandSettings279*/280public void setDestinationBands(int[] destinationBands) {281if (destinationBands == null) {282this.destinationBands = null;283} else {284int numBands = destinationBands.length;285for (int i = 0; i < numBands; i++) {286int band = destinationBands[i];287if (band < 0) {288throw new IllegalArgumentException("Band value < 0!");289}290for (int j = i + 1; j < numBands; j++) {291if (band == destinationBands[j]) {292throw new IllegalArgumentException("Duplicate band value!");293}294}295}296this.destinationBands = (int[])destinationBands.clone();297}298}299300/**301* Returns the set of band indices where data will be placed.302* If no value has been set, <code>null</code> is returned to303* indicate that all destination bands will be used.304*305* @return the indices of the destination bands to be used,306* or <code>null</code>.307*308* @see #setDestinationBands309*/310public int[] getDestinationBands() {311if (destinationBands == null) {312return null;313} else {314return (int[])(destinationBands.clone());315}316}317318/**319* Returns <code>true</code> if this reader allows the source320* image to be rendered at an arbitrary size as part of the321* decoding process, by means of the322* <code>setSourceRenderSize</code> method. If this method323* returns <code>false</code>, calls to324* <code>setSourceRenderSize</code> will throw an325* <code>UnsupportedOperationException</code>.326*327* @return <code>true</code> if setting source rendering size is328* supported.329*330* @see #setSourceRenderSize331*/332public boolean canSetSourceRenderSize() {333return canSetSourceRenderSize;334}335336/**337* If the image is able to be rendered at an arbitrary size, sets338* the source width and height to the supplied values. Note that339* the values returned from the <code>getWidth</code> and340* <code>getHeight</code> methods on <code>ImageReader</code> are341* not affected by this method; they will continue to return the342* default size for the image. Similarly, if the image is also343* tiled the tile width and height are given in terms of the default344* size.345*346* <p> Typically, the width and height should be chosen such that347* the ratio of width to height closely approximates the aspect348* ratio of the image, as returned from349* <code>ImageReader.getAspectRatio</code>.350*351* <p> If this plug-in does not allow the rendering size to be352* set, an <code>UnsupportedOperationException</code> will be353* thrown.354*355* <p> To remove the render size setting, pass in a value of356* <code>null</code> for <code>size</code>.357*358* @param size a <code>Dimension</code> indicating the desired359* width and height.360*361* @exception IllegalArgumentException if either the width or the362* height is negative or 0.363* @exception UnsupportedOperationException if image resizing364* is not supported by this plug-in.365*366* @see #getSourceRenderSize367* @see ImageReader#getWidth368* @see ImageReader#getHeight369* @see ImageReader#getAspectRatio370*/371public void setSourceRenderSize(Dimension size)372throws UnsupportedOperationException {373if (!canSetSourceRenderSize()) {374throw new UnsupportedOperationException375("Can't set source render size!");376}377378if (size == null) {379this.sourceRenderSize = null;380} else {381if (size.width <= 0 || size.height <= 0) {382throw new IllegalArgumentException("width or height <= 0!");383}384this.sourceRenderSize = (Dimension)size.clone();385}386}387388/**389* Returns the width and height of the source image as it390* will be rendered during decoding, if they have been set via the391* <code>setSourceRenderSize</code> method. A392* <code>null</code>value indicates that no setting has been made.393*394* @return the rendered width and height of the source image395* as a <code>Dimension</code>.396*397* @see #setSourceRenderSize398*/399public Dimension getSourceRenderSize() {400return (sourceRenderSize == null) ?401null : (Dimension)sourceRenderSize.clone();402}403404/**405* Sets the range of progressive passes that will be decoded.406* Passes outside of this range will be ignored.407*408* <p> A progressive pass is a re-encoding of the entire image,409* generally at progressively higher effective resolutions, but410* requiring greater transmission bandwidth. The most common use411* of progressive encoding is found in the JPEG format, where412* successive passes include more detailed representations of the413* high-frequency image content.414*415* <p> The actual number of passes to be decoded is determined416* during decoding, based on the number of actual passes available417* in the stream. Thus if <code>minPass + numPasses - 1</code> is418* larger than the index of the last available passes, decoding419* will end with that pass.420*421* <p> A value of <code>numPasses</code> of422* <code>Integer.MAX_VALUE</code> indicates that all passes from423* <code>minPass</code> forward should be read. Otherwise, the424* index of the last pass (<i>i.e.</i>, <code>minPass + numPasses425* - 1</code>) must not exceed <code>Integer.MAX_VALUE</code>.426*427* <p> There is no <code>unsetSourceProgressivePasses</code>428* method; the same effect may be obtained by calling429* <code>setSourceProgressivePasses(0, Integer.MAX_VALUE)</code>.430*431* @param minPass the index of the first pass to be decoded.432* @param numPasses the maximum number of passes to be decoded.433*434* @exception IllegalArgumentException if <code>minPass</code> is435* negative, <code>numPasses</code> is negative or 0, or436* <code>numPasses</code> is smaller than437* <code>Integer.MAX_VALUE</code> but <code>minPass +438* numPasses - 1</code> is greater than439* <code>INTEGER.MAX_VALUE</code>.440*441* @see #getSourceMinProgressivePass442* @see #getSourceMaxProgressivePass443*/444public void setSourceProgressivePasses(int minPass, int numPasses) {445if (minPass < 0) {446throw new IllegalArgumentException("minPass < 0!");447}448if (numPasses <= 0) {449throw new IllegalArgumentException("numPasses <= 0!");450}451if ((numPasses != Integer.MAX_VALUE) &&452(((minPass + numPasses - 1) & 0x80000000) != 0)) {453throw new IllegalArgumentException454("minPass + numPasses - 1 > INTEGER.MAX_VALUE!");455}456457this.minProgressivePass = minPass;458this.numProgressivePasses = numPasses;459}460461/**462* Returns the index of the first progressive pass that will be463* decoded. If no value has been set, 0 will be returned (which is464* the correct value).465*466* @return the index of the first pass that will be decoded.467*468* @see #setSourceProgressivePasses469* @see #getSourceNumProgressivePasses470*/471public int getSourceMinProgressivePass() {472return minProgressivePass;473}474475/**476* If <code>getSourceNumProgressivePasses</code> is equal to477* <code>Integer.MAX_VALUE</code>, returns478* <code>Integer.MAX_VALUE</code>. Otherwise, returns479* <code>getSourceMinProgressivePass() +480* getSourceNumProgressivePasses() - 1</code>.481*482* @return the index of the last pass to be read, or483* <code>Integer.MAX_VALUE</code>.484*/485public int getSourceMaxProgressivePass() {486if (numProgressivePasses == Integer.MAX_VALUE) {487return Integer.MAX_VALUE;488} else {489return minProgressivePass + numProgressivePasses - 1;490}491}492493/**494* Returns the number of the progressive passes that will be495* decoded. If no value has been set,496* <code>Integer.MAX_VALUE</code> will be returned (which is the497* correct value).498*499* @return the number of the passes that will be decoded.500*501* @see #setSourceProgressivePasses502* @see #getSourceMinProgressivePass503*/504public int getSourceNumProgressivePasses() {505return numProgressivePasses;506}507}508509510