/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.
*/
/**
* This class performs a pixel-by-pixel rescaling of the data in the
* source image by multiplying the sample values for each pixel by a scale
* factor and then adding an offset. The scaled sample values are clipped
* <p>
* The pseudo code for the rescaling operation is as follows:
* <pre>
*for each pixel from Source object {
* dstElement = (srcElement*scaleFactor) + offset
* }
*}
* </pre>
* <p>
* For Rasters, rescaling operates on bands. The number of
* sets of scaling constants may be one, in which case the same constants
* are applied to all bands, or it must equal the number of Source
* Raster bands.
* <p>
* For BufferedImages, rescaling operates on color and alpha components.
* The number of sets of scaling constants may be one, in which case the
* same constants are applied to all color (but not alpha) components.
* Otherwise, the number of sets of scaling constants may
* equal the number of Source color components, in which case no
* rescaling of the alpha component (if present) is performed.
* If neither of these cases apply, the number of sets of scaling constants
* must equal the number of Source color components plus alpha components,
* in which case all color and alpha components are rescaled.
* <p>
* BufferedImage sources with premultiplied alpha data are treated in the same
* manner as non-premultiplied images for purposes of rescaling. That is,
* the rescaling is done per band on the raw data of the BufferedImage source
* without regard to whether the data is premultiplied. If a color conversion
* is required to the destination ColorModel, the premultiplied state of
* both source and destination will be taken into account for this step.
* <p>
* Images with an IndexColorModel cannot be rescaled.
* <p>
* If a RenderingHints object is specified in the constructor, the
* color rendering hint and the dithering hint may be used when color
* conversion is required.
* <p>
* Note that in-place operation is allowed (i.e. the source and destination can
* be the same object).
* @see java.awt.RenderingHints#KEY_COLOR_RENDERING
* @see java.awt.RenderingHints#KEY_DITHERING
*/
float[] scaleFactors;
float[] offsets;
private int srcNbits;
private int dstNbits;
/**
* Constructs a new RescaleOp with the desired scale factors
* and offsets. The length of the scaleFactor and offset arrays
* must meet the restrictions stated in the class comments above.
* The RenderingHints argument may be null.
* @param scaleFactors the specified scale factors
* @param offsets the specified offsets
* @param hints the specified <code>RenderingHints</code>, or
* <code>null</code>
*/
this.scaleFactors = new float[length];
for (int i=0; i < length; i++) {
this.scaleFactors[i] = scaleFactors[i];
}
}
/**
* Constructs a new RescaleOp with the desired scale factor
* and offset. The scaleFactor and offset will be applied to
* all bands in a source Raster and to all color (but not alpha)
* components in a BufferedImage.
* The RenderingHints argument may be null.
* @param scaleFactor the specified scale factor
* @param offset the specified offset
* @param hints the specified <code>RenderingHints</code>, or
* <code>null</code>
*/
length = 1;
this.scaleFactors = new float[1];
this.offsets = new float[1];
}
/**
* Returns the scale factors in the given array. The array is also
* returned for convenience. If scaleFactors is null, a new array
* will be allocated.
* @param scaleFactors the array to contain the scale factors of
* this <code>RescaleOp</code>
* @return the scale factors of this <code>RescaleOp</code>.
*/
if (scaleFactors == null) {
return (float[]) this.scaleFactors.clone();
}
return scaleFactors;
}
/**
* Returns the offsets in the given array. The array is also returned
* for convenience. If offsets is null, a new array
* will be allocated.
* @param offsets the array to contain the offsets of
* this <code>RescaleOp</code>
* @return the offsets of this <code>RescaleOp</code>.
*/
}
return offsets;
}
/**
* Returns the number of scaling factors and offsets used in this
* RescaleOp.
* @return the number of scaling factors and offsets of this
* <code>RescaleOp</code>.
*/
final public int getNumFactors() {
return length;
}
/**
* Creates a ByteLookupTable to implement the rescale.
* The table may have either a SHORT or BYTE input.
* @param nElems Number of elements the table is to have.
* This will generally be 256 for byte and
* 65536 for short.
*/
float off[],
int nBands,
int nElems) {
for (int i=0; i<nElems; i++) {
if (val < 0) {
val = 0;
} else {
val = 255;
}
}
bandLutData[i] = (byte)val;
}
}
}
/**
* Creates a ShortLookupTable to implement the rescale.
* The table may have either a SHORT or BYTE input.
* @param nElems Number of elements the table is to have.
* This will generally be 256 for byte and
* 65536 for short.
*/
float off[],
int nBands,
int nElems) {
for (int i=0; i<nElems; i++) {
if (val < 0) {
val = 0;
} else {
val = 65535;
}
}
bandLutData[i] = (short)val;
}
}
}
/**
* Determines if the rescale can be performed as a lookup.
* The dst must be a byte or short type.
* The src must be less than 16 bits.
* All source band sizes must be the same and all dst band sizes
* must be the same.
*/
//
// Check that the src datatype is either a BYTE or SHORT
//
return false;
}
//
// Check dst sample sizes. All must be 8 or 16 bits.
//
return false;
}
return false;
}
}
//
// Check src sample sizes. All must be the same size
//
if (srcNbits > 16) {
return false;
}
return false;
}
}
return true;
}
/**
* Rescales the source BufferedImage.
* If the color model in the source image is not the same as that
* in the destination image, the pixels will be converted
* in the destination. If the destination image is null,
* a BufferedImage will be created with the source ColorModel.
* An IllegalArgumentException may be thrown if the number of
* restrictions stated in the class comments above, or if the
* source image has an IndexColorModel.
* @param src the <code>BufferedImage</code> to be filtered
* @param dst the destination for the filtering operation
* or <code>null</code>
* @return the filtered <code>BufferedImage</code>.
* @throws IllegalArgumentException if the <code>ColorModel</code>
* of <code>src</code> is an <code>IndexColorModel</code>,
* or if the number of scaling factors and offsets in this
* <code>RescaleOp</code> do not meet the requirements
* stated in the class comments.
*/
if (srcCM instanceof IndexColorModel) {
throw new
IllegalArgumentException("Rescaling cannot be "+
"performed on an indexed image");
}
{
throw new IllegalArgumentException("Number of scaling constants "+
"does not equal the number of"+
" components");
}
boolean needToConvert = false;
// Include alpha
}
}
else {
throw new
") not equal to dst width ("+
}
throw new
") not equal to dst height ("+
}
needToConvert = true;
}
}
//
// Try to use a native BI rescale operation first
//
//
// Native BI rescale failed - convert to rasters
//
bands[i] = i;
}
bands);
}
}
bands[i] = i;
}
bands);
}
}
//
// Call the raster filter method
//
}
if (needToConvert) {
// ColorModels are not the same
}
return origDst;
}
/**
* Rescales the pixel data in the source Raster.
* If the destination Raster is null, a new Raster will be created.
* The source and destination must have the same number of bands.
* Otherwise, an IllegalArgumentException is thrown.
* meet the restrictions stated in the class comments above.
* Otherwise, an IllegalArgumentException is thrown.
* @param src the <code>Raster</code> to be filtered
* @param dst the destination for the filtering operation
* or <code>null</code>
* @return the filtered <code>WritableRaster</code>.
* @throws IllegalArgumentException if <code>src</code> and
* <code>dst</code> do not have the same number of bands,
* or if the number of scaling factors and offsets in this
* <code>RescaleOp</code> do not meet the requirements
* stated in the class comments.
*/
int step = 0;
int tidx = 0;
// Create a new destination Raster, if needed
}
throw new
IllegalArgumentException("Width or height of Rasters do not "+
"match");
}
// Make sure that the number of bands are equal
throw new IllegalArgumentException("Number of bands in src "
+ numBands
+ " does not equal number of bands in dest "
+ dst.getNumBands());
}
// Make sure that the arrays match
throw new IllegalArgumentException("Number of scaling constants "+
"does not equal the number of"+
" of bands in the src raster");
}
//
// Try for a native raster rescale first
//
return dst;
}
//
// Native raster rescale failed.
// Try to see if a lookup operation can be used
//
if (dstNgray == 256) {
} else {
}
} else {
//
// Fall back to the slow code
//
if (length > 1) {
step = 1;
}
int sX;
int dX;
//
// Determine bits per band to determine maxval for clamps.
// The min is assumed to be zero.
// REMIND: This must change if we ever support signed data types.
//
int nbits;
for (int z=0; z<numBands; z++) {
}
int val;
// Get data for all bands at this x,y position
tidx = 0;
// Clamp
if (val < 0) {
val = 0;
} else {
}
}
}
// Put it back for all bands
}
}
}
return dst;
}
/**
* Returns the bounding box of the rescaled destination image. Since
* this is not a geometric operation, the bounding box does not
* change.
*/
}
/**
* Returns the bounding box of the rescaled destination Raster. Since
* this is not a geometric operation, the bounding box does not
* change.
* @param src the rescaled destination <code>Raster</code>
* @return the bounds of the specified <code>Raster</code>.
*/
}
/**
* Creates a zeroed destination image with the correct size and number of
* bands.
* @param src Source image for the filter operation.
* @param destCM ColorModel of the destination. If null, the
* ColorModel of the source will be used.
* @return the zeroed-destination image.
*/
ColorModel destCM) {
null);
}
else {
}
return image;
}
/**
* Creates a zeroed-destination <code>Raster</code> with the correct
* size and number of bands, given this source.
* @param src the source <code>Raster</code>
* @return the zeroed-destination <code>Raster</code>.
*/
}
/**
* Returns the location of the destination point given a
* point in the source. If dstPt is non-null, it will
* be used to hold the return value. Since this is not a geometric
* operation, the srcPt will equal the dstPt.
* @param srcPt a point in the source image
* @param dstPt the destination point or <code>null</code>
* @return the location of the destination point.
*/
}
return dstPt;
}
/**
* Returns the rendering hints for this op.
* @return the rendering hints of this <code>RescaleOp</code>.
*/
return hints;
}
}