Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,218 @@
/* BandCombineOp.java - perform a combination on the bands of a raster
Copyright (C) 2004, 2006 Free Software Foundation
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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 for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.awt.image;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
/**
* Filter Raster pixels by applying a matrix.
*
* BandCombineOp applies a matrix to each pixel to produce new pixel values.
* The width of the matrix must be the same or one more than the number of
* bands in the source Raster. If one more, the pixels in the source are
* assumed to contain an implicit 1.0 at the end.
*
* The rows of the matrix are multiplied by the pixel to produce the values
* for the destination. Therefore the destination Raster must contain the
* same number of bands as the number of rows in the filter matrix.
*
* This Op assumes that samples are integers; floating point sample types will
* be rounded to their nearest integer value during filtering.
*
* @author Jerry Quinn (jlquinn@optonline.net)
*/
public class BandCombineOp implements RasterOp
{
private RenderingHints hints;
private float[][] matrix;
/**
* Construct a BandCombineOp.
*
* @param matrix The matrix to filter pixels with.
* @param hints Rendering hints to apply. Ignored.
* @throws ArrayIndexOutOfBoundsException if the matrix is invalid
*/
public BandCombineOp(float[][] matrix, RenderingHints hints)
{
this.matrix = new float[matrix.length][];
int width = matrix[0].length;
for (int i = 0; i < matrix.length; i++)
{
this.matrix[i] = new float[width + 1];
for (int j = 0; j < width; j++)
this.matrix[i][j] = matrix[i][j];
// The reference implementation pads the array with a trailing zero...
this.matrix[i][width] = 0;
}
this.hints = hints;
}
/**
* Filter Raster pixels through a matrix. Applies the Op matrix to source
* pixes to produce dest pixels. Each row of the matrix is multiplied by the
* src pixel components to produce the dest pixel. If matrix is one more than
* the number of bands in the src, the last element is implicitly multiplied
* by 1, i.e. added to the sum for that dest component. If dest is null, a
* suitable Raster is created. This implementation uses
* createCompatibleDestRaster.
*
* @param src The source Raster.
* @param dest The destination Raster, or null.
* @throws IllegalArgumentException if the destination raster is incompatible
* with the source raster.
* @return The filtered Raster.
* @see java.awt.image.RasterOp#filter(java.awt.image.Raster,
* java.awt.image.WritableRaster)
*/
public WritableRaster filter(Raster src, WritableRaster dest) {
if (dest == null)
dest = createCompatibleDestRaster(src);
else if (dest.getNumBands() != src.getNumBands()
|| dest.getTransferType() != src.getTransferType())
throw new IllegalArgumentException("Destination raster is incompatible with source raster");
// Filter the pixels
int[] spix = new int[matrix[0].length - 1];
int[] spix2 = new int[matrix[0].length - 1];
int[] dpix = new int[matrix.length];
for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
{
// In case matrix rows have implicit translation
spix[spix.length - 1] = 1;
src.getPixel(x, y, spix);
// Do not re-calculate if pixel is identical to the last one
// (ie, blocks of the same colour)
if (!Arrays.equals(spix, spix2))
{
System.arraycopy(spix, 0, spix2, 0, spix.length);
for (int i = 0; i < matrix.length; i++)
{
dpix[i] = 0;
for (int j = 0; j < matrix[0].length - 1; j++)
dpix[i] += spix[j] * (int)matrix[i][j];
}
}
dest.setPixel(x, y, dpix);
}
return dest;
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#getBounds2D(java.awt.image.Raster)
*/
public final Rectangle2D getBounds2D(Raster src)
{
return src.getBounds();
}
/**
* Creates a new WritableRaster that can be used as the destination for this
* Op. The number of bands in the source raster must equal the number of rows
* in the op matrix, which must also be equal to either the number of columns
* or (columns - 1) in the matrix.
*
* @param src The source raster.
* @return A compatible raster.
* @see java.awt.image.RasterOp#createCompatibleDestRaster(java.awt.image.Raster)
* @throws IllegalArgumentException if the raster is incompatible with the
* matrix.
*/
public WritableRaster createCompatibleDestRaster(Raster src)
{
// Destination raster must have same number of bands as source
if (src.getNumBands() != matrix.length)
throw new IllegalArgumentException("Number of rows in matrix specifies an "
+ "incompatible number of bands");
// We use -1 and -2 because we previously padded the rows with a trailing 0
if (src.getNumBands() != matrix[0].length - 1
&& src.getNumBands() != matrix[0].length - 2)
throw new IllegalArgumentException("Incompatible number of bands: "
+ "the number of bands in the raster must equal the number of "
+ "columns in the matrix, optionally minus one");
return src.createCompatibleWritableRaster();
}
/**
* Return corresponding destination point for source point. Because this is
* not a geometric operation, it simply returns a copy of the source.
*
* @param src The source point.
* @param dst The destination point.
* @return dst The destination point.
* @see java.awt.image.RasterOp#getPoint2D(java.awt.geom.Point2D,
*java.awt.geom.Point2D)
*/
public final Point2D getPoint2D(Point2D src, Point2D dst)
{
if (dst == null)
return (Point2D)src.clone();
dst.setLocation(src);
return dst;
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#getRenderingHints()
*/
public final RenderingHints getRenderingHints()
{
return hints;
}
/**
* Return the matrix used in this operation.
*
* @return The matrix used in this operation.
*/
public final float[][] getMatrix()
{
return matrix;
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,380 @@
/* ConvolveOp.java --
Copyright (C) 2004, 2005, 2006, Free Software Foundation -- ConvolveOp
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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 for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.awt.image;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
/**
* Convolution filter.
*
* ConvolveOp convolves the source image with a Kernel to generate a
* destination image. This involves multiplying each pixel and its neighbors
* with elements in the kernel to compute a new pixel.
*
* Each band in a Raster is convolved and copied to the destination Raster.
* For BufferedImages, convolution is applied to all components. Color
* conversion will be applied if needed.
*
* Note that this filter ignores whether the source or destination is alpha
* premultiplied. The reference spec states that data will be premultiplied
* prior to convolving and divided back out afterwards (if needed), but testing
* has shown that this is not the case with their implementation.
*
* @author jlquinn@optonline.net
*/
public class ConvolveOp implements BufferedImageOp, RasterOp
{
/** Edge pixels are set to 0. */
public static final int EDGE_ZERO_FILL = 0;
/** Edge pixels are copied from the source. */
public static final int EDGE_NO_OP = 1;
private Kernel kernel;
private int edge;
private RenderingHints hints;
/**
* Construct a ConvolveOp.
*
* The edge condition specifies that pixels outside the area that can be
* filtered are either set to 0 or copied from the source image.
*
* @param kernel The kernel to convolve with.
* @param edgeCondition Either EDGE_ZERO_FILL or EDGE_NO_OP.
* @param hints Rendering hints for color conversion, or null.
*/
public ConvolveOp(Kernel kernel,
int edgeCondition,
RenderingHints hints)
{
this.kernel = kernel;
edge = edgeCondition;
this.hints = hints;
}
/**
* Construct a ConvolveOp.
*
* The edge condition defaults to EDGE_ZERO_FILL.
*
* @param kernel The kernel to convolve with.
*/
public ConvolveOp(Kernel kernel)
{
this.kernel = kernel;
edge = EDGE_ZERO_FILL;
hints = null;
}
/**
* Converts the source image using the kernel specified in the
* constructor. The resulting image is stored in the destination image if one
* is provided; otherwise a new BufferedImage is created and returned.
*
* The source and destination BufferedImage (if one is supplied) must have
* the same dimensions.
*
* @param src The source image.
* @param dst The destination image.
* @throws IllegalArgumentException if the rasters and/or color spaces are
* incompatible.
* @return The convolved image.
*/
public final BufferedImage filter(BufferedImage src, BufferedImage dst)
{
if (src == dst)
throw new IllegalArgumentException("Source and destination images " +
"cannot be the same.");
if (dst == null)
dst = createCompatibleDestImage(src, src.getColorModel());
// Make sure source image is premultiplied
BufferedImage src1 = src;
// The spec says we should do this, but mauve testing shows that Sun's
// implementation does not check this.
/*
if (!src.isAlphaPremultiplied())
{
src1 = createCompatibleDestImage(src, src.getColorModel());
src.copyData(src1.getRaster());
src1.coerceData(true);
}
*/
BufferedImage dst1 = dst;
if (src1.getColorModel().getColorSpace().getType() != dst.getColorModel().getColorSpace().getType())
dst1 = createCompatibleDestImage(src, src.getColorModel());
filter(src1.getRaster(), dst1.getRaster());
// Since we don't coerceData above, we don't need to divide it back out.
// This is wrong (one mauve test specifically tests converting a non-
// premultiplied image to a premultiplied image, and it shows that Sun
// simply ignores the premultipled flag, contrary to the spec), but we
// mimic it for compatibility.
/*
if (! dst.isAlphaPremultiplied())
dst1.coerceData(false);
*/
// Convert between color models if needed
if (dst1 != dst)
new ColorConvertOp(hints).filter(dst1, dst);
return dst;
}
/**
* Creates an empty BufferedImage with the size equal to the source and the
* correct number of bands. The new image is created with the specified
* ColorModel, or if no ColorModel is supplied, an appropriate one is chosen.
*
* @param src The source image.
* @param dstCM A color model for the destination image (may be null).
* @return The new compatible destination image.
*/
public BufferedImage createCompatibleDestImage(BufferedImage src,
ColorModel dstCM)
{
if (dstCM != null)
return new BufferedImage(dstCM,
src.getRaster().createCompatibleWritableRaster(),
src.isAlphaPremultiplied(), null);
return new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#getRenderingHints()
*/
public final RenderingHints getRenderingHints()
{
return hints;
}
/**
* Get the edge condition for this Op.
*
* @return The edge condition.
*/
public int getEdgeCondition()
{
return edge;
}
/**
* Returns (a clone of) the convolution kernel.
*
* @return The convolution kernel.
*/
public final Kernel getKernel()
{
return (Kernel) kernel.clone();
}
/**
* Converts the source raster using the kernel specified in the constructor.
* The resulting raster is stored in the destination raster if one is
* provided; otherwise a new WritableRaster is created and returned.
*
* If the convolved value for a sample is outside the range of [0-255], it
* will be clipped.
*
* The source and destination raster (if one is supplied) cannot be the same,
* and must also have the same dimensions.
*
* @param src The source raster.
* @param dest The destination raster.
* @throws IllegalArgumentException if the rasters identical.
* @throws ImagingOpException if the convolution is not possible.
* @return The transformed raster.
*/
public final WritableRaster filter(Raster src, WritableRaster dest)
{
if (src == dest)
throw new IllegalArgumentException("src == dest is not allowed.");
if (kernel.getWidth() > src.getWidth()
|| kernel.getHeight() > src.getHeight())
throw new ImagingOpException("The kernel is too large.");
if (dest == null)
dest = createCompatibleDestRaster(src);
else if (src.getNumBands() != dest.getNumBands())
throw new ImagingOpException("src and dest have different band counts.");
// calculate the borders that the op can't reach...
int kWidth = kernel.getWidth();
int kHeight = kernel.getHeight();
int left = kernel.getXOrigin();
int right = Math.max(kWidth - left - 1, 0);
int top = kernel.getYOrigin();
int bottom = Math.max(kHeight - top - 1, 0);
// Calculate max sample values for clipping
int[] maxValue = src.getSampleModel().getSampleSize();
for (int i = 0; i < maxValue.length; i++)
maxValue[i] = (int)Math.pow(2, maxValue[i]) - 1;
// process the region that is reachable...
int regionW = src.width - left - right;
int regionH = src.height - top - bottom;
float[] kvals = kernel.getKernelData(null);
float[] tmp = new float[kWidth * kHeight];
for (int x = 0; x < regionW; x++)
{
for (int y = 0; y < regionH; y++)
{
// FIXME: This needs a much more efficient implementation
for (int b = 0; b < src.getNumBands(); b++)
{
float v = 0;
src.getSamples(x, y, kWidth, kHeight, b, tmp);
for (int i = 0; i < tmp.length; i++)
v += tmp[tmp.length - i - 1] * kvals[i];
// FIXME: in the above line, I've had to reverse the order of
// the samples array to make the tests pass. I haven't worked
// out why this is necessary.
// This clipping is is undocumented, but determined by testing.
if (v > maxValue[b])
v = maxValue[b];
else if (v < 0)
v = 0;
dest.setSample(x + kernel.getXOrigin(), y + kernel.getYOrigin(),
b, v);
}
}
}
// fill in the top border
fillEdge(src, dest, 0, 0, src.width, top, edge);
// fill in the bottom border
fillEdge(src, dest, 0, src.height - bottom, src.width, bottom, edge);
// fill in the left border
fillEdge(src, dest, 0, top, left, regionH, edge);
// fill in the right border
fillEdge(src, dest, src.width - right, top, right, regionH, edge);
return dest;
}
/**
* Fills a range of pixels (typically at the edge of a raster) with either
* zero values (if <code>edgeOp</code> is <code>EDGE_ZERO_FILL</code>) or the
* corresponding pixel values from the source raster (if <code>edgeOp</code>
* is <code>EDGE_NO_OP</code>). This utility method is called by the
* {@link #fillEdge(Raster, WritableRaster, int, int, int, int, int)} method.
*
* @param src the source raster.
* @param dest the destination raster.
* @param x the x-coordinate of the top left pixel in the range.
* @param y the y-coordinate of the top left pixel in the range.
* @param w the width of the pixel range.
* @param h the height of the pixel range.
* @param edgeOp indicates how to determine the values for the range
* (either {@link #EDGE_ZERO_FILL} or {@link #EDGE_NO_OP}).
*/
private void fillEdge(Raster src, WritableRaster dest, int x, int y, int w,
int h, int edgeOp)
{
if (w <= 0)
return;
if (h <= 0)
return;
if (edgeOp == EDGE_ZERO_FILL) // fill region with zeroes
{
float[] zeros = new float[src.getNumBands() * w * h];
dest.setPixels(x, y, w, h, zeros);
}
else // copy pixels from source
{
float[] pixels = new float[src.getNumBands() * w * h];
src.getPixels(x, y, w, h, pixels);
dest.setPixels(x, y, w, h, pixels);
}
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#createCompatibleDestRaster(java.awt.image.Raster)
*/
public WritableRaster createCompatibleDestRaster(Raster src)
{
return src.createCompatibleWritableRaster();
}
/* (non-Javadoc)
* @see java.awt.image.BufferedImageOp#getBounds2D(java.awt.image.BufferedImage)
*/
public final Rectangle2D getBounds2D(BufferedImage src)
{
return src.getRaster().getBounds();
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#getBounds2D(java.awt.image.Raster)
*/
public final Rectangle2D getBounds2D(Raster src)
{
return src.getBounds();
}
/**
* Returns the corresponding destination point for a source point. Because
* this is not a geometric operation, the destination and source points will
* be identical.
*
* @param src The source point.
* @param dst The transformed destination point.
* @return The transformed destination point.
*/
public final Point2D getPoint2D(Point2D src, Point2D dst)
{
if (dst == null) return (Point2D)src.clone();
dst.setLocation(src);
return dst;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,307 @@
/* LookupOp.java -- Filter that converts each pixel using a lookup table.
Copyright (C) 2004 Free Software Foundation
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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 for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.awt.image;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
/**
* LookupOp is a filter that converts each pixel using a lookup table.
*
* For filtering Rasters, the lookup table must have either one component
* that is applied to all bands, or one component for every band in the
* Rasters.
*
* For BufferedImages, the lookup table may apply to both color and alpha
* components. If the lookup table contains one component, or if there are
* the same number of components as color components in the source, the table
* applies to all color components. Otherwise the table applies to all
* components including alpha. Alpha premultiplication is ignored during the
* lookup filtering.
*
* After filtering, if color conversion is necessary, the conversion happens,
* taking alpha premultiplication into account.
*
* @author jlquinn
*/
public class LookupOp implements BufferedImageOp, RasterOp
{
private LookupTable lut;
private RenderingHints hints;
/**
* Construct a new LookupOp using the given LookupTable.
*
* @param lookup LookupTable to use.
* @param hints Rendering hints (can be null).
*/
public LookupOp(LookupTable lookup, RenderingHints hints)
{
lut = lookup;
this.hints = hints;
}
/**
* Converts the source image using the lookup table specified in the
* constructor. The resulting image is stored in the destination image if one
* is provided; otherwise a new BufferedImage is created and returned.
*
* The source image cannot use an IndexColorModel, and the destination image
* (if one is provided) must have the same size.
*
* @param src The source image.
* @param dst The destination image.
* @throws IllegalArgumentException if the rasters and/or color spaces are
* incompatible.
* @throws ArrayIndexOutOfBoundsException if a pixel in the source is not
* contained in the LookupTable.
* @return The convolved image.
*/
public final BufferedImage filter(BufferedImage src, BufferedImage dst)
{
if (src.getColorModel() instanceof IndexColorModel)
throw new IllegalArgumentException("LookupOp.filter: IndexColorModel "
+ "not allowed");
if (lut.getNumComponents() != 1
&& lut.getNumComponents() != src.getColorModel().getNumComponents()
&& lut.getNumComponents() != src.getColorModel().getNumColorComponents())
throw new IllegalArgumentException("LookupOp.filter: Incompatible " +
"lookup table and source image");
if (dst == null)
dst = createCompatibleDestImage(src, null);
else if (src.getHeight() != dst.getHeight() || src.getWidth() != dst.getWidth())
throw new IllegalArgumentException("Source and destination images are " +
"different sizes.");
// Set up for potential colormodel mismatch
BufferedImage tgt;
if (dst.getColorModel().equals(src.getColorModel()))
tgt = dst;
else
tgt = createCompatibleDestImage(src, src.getColorModel());
Raster sr = src.getRaster();
WritableRaster dr = tgt.getRaster();
if (src.getColorModel().hasAlpha() &&
(lut.getNumComponents() == 1 ||
lut.getNumComponents() == src.getColorModel().getNumColorComponents()))
{
// Need to ignore alpha for lookup
int[] dbuf = new int[src.getColorModel().getNumComponents()];
int tmpBands = src.getColorModel().getNumColorComponents();
int[] tmp = new int[tmpBands];
// Filter the pixels
for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
{
// Filter only color components, but also copy alpha
sr.getPixel(x, y, dbuf);
System.arraycopy(dbuf, 0, tmp, 0, tmpBands);
dr.setPixel(x, y, lut.lookupPixel(tmp, dbuf));
/* The reference implementation does not use LookupTable.lookupPixel,
* but rather it seems to copy the table into a native array. The
* effect of this (a probable bug in their implementation) is that
* an out-of-bounds lookup on a ByteLookupTable will *not* throw an
* out of bounds exception, but will instead return random garbage.
* A bad lookup on a ShortLookupTable, however, will throw an
* exception.
*
* Instead of mimicing this behaviour, we always throw an
* ArrayOutofBoundsException by virtue of using
* LookupTable.lookupPixle.
*/
}
}
else
{
// No alpha to ignore
int[] dbuf = new int[src.getColorModel().getNumComponents()];
// Filter the pixels
for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
dr.setPixel(x, y, lut.lookupPixel(sr.getPixel(x, y, dbuf), dbuf));
}
if (tgt != dst)
new ColorConvertOp(hints).filter(tgt, dst);
return dst;
}
/* (non-Javadoc)
* @see java.awt.image.BufferedImageOp#getBounds2D(java.awt.image.BufferedImage)
*/
public final Rectangle2D getBounds2D(BufferedImage src)
{
return src.getRaster().getBounds();
}
/* (non-Javadoc)
* @see java.awt.image.BufferedImageOp#createCompatibleDestImage(java.awt.image.BufferedImage, java.awt.image.ColorModel)
*/
public BufferedImage createCompatibleDestImage(BufferedImage src,
ColorModel dstCM)
{
if (dstCM != null)
return new BufferedImage(dstCM,
src.getRaster().createCompatibleWritableRaster(),
src.isAlphaPremultiplied(), null);
// This is a strange exception, done for compatibility with the reference
// (as demonstrated by a mauve testcase)
int imgType = src.getType();
if (imgType == BufferedImage.TYPE_USHORT_GRAY)
imgType = BufferedImage.TYPE_BYTE_GRAY;
return new BufferedImage(src.getWidth(), src.getHeight(), imgType);
}
/**
* Returns the corresponding destination point for a given source point.
*
* This Op will return the source point unchanged.
*
* @param src The source point.
* @param dst The destination point.
*/
public final Point2D getPoint2D(Point2D src, Point2D dst)
{
if (dst == null)
return (Point2D) src.clone();
dst.setLocation(src);
return dst;
}
/**
* Return the LookupTable for this op.
*
* @return The lookup table.
*/
public final LookupTable getTable()
{
return lut;
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#getRenderingHints()
*/
public final RenderingHints getRenderingHints()
{
return hints;
}
/**
* Filter a raster through a lookup table.
*
* Applies the lookup table for this Rasterop to each pixel of src and
* puts the results in dest. If dest is null, a new Raster is created and
* returned.
*
* @param src The source raster.
* @param dest The destination raster.
* @return The WritableRaster with the filtered pixels.
* @throws IllegalArgumentException if lookup table has more than one
* component but not the same as src and dest.
* @throws ArrayIndexOutOfBoundsException if a pixel in the source is not
* contained in the LookupTable.
*/
public final WritableRaster filter(Raster src, WritableRaster dest)
{
if (dest == null)
// Allocate a raster if needed
dest = createCompatibleDestRaster(src);
else
if (src.getNumBands() != dest.getNumBands())
throw new IllegalArgumentException("Source and destination rasters " +
"are incompatible.");
if (lut.getNumComponents() != 1
&& lut.getNumComponents() != src.getNumBands())
throw new IllegalArgumentException("Lookup table is incompatible with " +
"this raster.");
// Allocate pixel storage.
int[] tmp = new int[src.getNumBands()];
// Filter the pixels
for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
dest.setPixel(x, y, lut.lookupPixel(src.getPixel(x, y, tmp), tmp));
/* The reference implementation does not use LookupTable.lookupPixel,
* but rather it seems to copy the table into a native array. The
* effect of this (a probable bug in their implementation) is that
* an out-of-bounds lookup on a ByteLookupTable will *not* throw an
* out of bounds exception, but will instead return random garbage.
* A bad lookup on a ShortLookupTable, however, will throw an
* exception.
*
* Instead of mimicing this behaviour, we always throw an
* ArrayOutofBoundsException by virtue of using
* LookupTable.lookupPixle.
*/
return dest;
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#getBounds2D(java.awt.image.Raster)
*/
public final Rectangle2D getBounds2D(Raster src)
{
return src.getBounds();
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#createCompatibleDestRaster(java.awt.image.Raster)
*/
public WritableRaster createCompatibleDestRaster(Raster src)
{
return src.createCompatibleWritableRaster();
}
}

View File

@@ -0,0 +1,385 @@
/* Copyright (C) 2004, 2006 Free Software Foundation
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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 for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.awt.image;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
/**
* RescaleOp is a filter that changes each pixel by a scaling factor and offset.
*
* For filtering Rasters, either one scaling factor and offset can be specified,
* which will be applied to all bands; or a scaling factor and offset can be
* specified for each band.
*
* For BufferedImages, the scaling may apply to both color and alpha components.
* If only one scaling factor is provided, or if the number of factors provided
* equals the number of color components, the scaling is performed on all color
* components. Otherwise, the scaling is performed on all components including
* alpha. Alpha premultiplication is ignored.
*
* After filtering, if color conversion is necessary, the conversion happens,
* taking alpha premultiplication into account.
*
* @author Jerry Quinn (jlquinn@optonline.net)
* @author Francis Kung (fkung@redhat.com)
*/
public class RescaleOp implements BufferedImageOp, RasterOp
{
private float[] scale;
private float[] offsets;
private RenderingHints hints = null;
/**
* Create a new RescaleOp object using the given scale factors and offsets.
*
* The length of the arrays must be equal to the number of bands (or number of
* data or color components) of the raster/image that this Op will be used on,
* otherwise an IllegalArgumentException will be thrown when calling the
* filter method.
*
* @param scaleFactors an array of scale factors.
* @param offsets an array of offsets.
* @param hints any rendering hints to use (can be null).
* @throws NullPointerException if the scaleFactors or offsets array is null.
*/
public RescaleOp(float[] scaleFactors,
float[] offsets,
RenderingHints hints)
{
int length = Math.min(scaleFactors.length, offsets.length);
scale = new float[length];
System.arraycopy(scaleFactors, 0, this.scale, 0, length);
this.offsets = new float[length];
System.arraycopy(offsets, 0, this.offsets, 0, length);
this.hints = hints;
}
/**
* Create a new RescaleOp object using the given scale factor and offset.
*
* The same scale factor and offset will be used on all bands/components.
*
* @param scaleFactor the scale factor to use.
* @param offset the offset to use.
* @param hints any rendering hints to use (can be null).
*/
public RescaleOp(float scaleFactor,
float offset,
RenderingHints hints)
{
scale = new float[]{ scaleFactor };
offsets = new float[]{offset};
this.hints = hints;
}
/**
* Returns the scaling factors. This method accepts an optional array, which
* will be used to store the factors if not null (this avoids allocating a
* new array). If this array is too small to hold all the scaling factors,
* the array will be filled and the remaining factors discarded.
*
* @param scaleFactors array to store the scaling factors in (can be null).
* @return an array of scaling factors.
*/
public final float[] getScaleFactors(float[] scaleFactors)
{
if (scaleFactors == null)
scaleFactors = new float[scale.length];
System.arraycopy(scale, 0, scaleFactors, 0, Math.min(scale.length,
scaleFactors.length));
return scaleFactors;
}
/**
* Returns the offsets. This method accepts an optional array, which
* will be used to store the offsets if not null (this avoids allocating a
* new array). If this array is too small to hold all the offsets, the array
* will be filled and the remaining factors discarded.
*
* @param offsets array to store the offsets in (can be null).
* @return an array of offsets.
*/
public final float[] getOffsets(float[] offsets)
{
if (offsets == null)
offsets = new float[this.offsets.length];
System.arraycopy(this.offsets, 0, offsets, 0, Math.min(this.offsets.length,
offsets.length));
return offsets;
}
/**
* Returns the number of scaling factors / offsets.
*
* @return the number of scaling factors / offsets.
*/
public final int getNumFactors()
{
return scale.length;
}
/* (non-Javadoc)
* @see java.awt.image.BufferedImageOp#getRenderingHints()
*/
public final RenderingHints getRenderingHints()
{
return hints;
}
/**
* Converts the source image using the scale factors and offsets specified in
* the constructor. The resulting image is stored in the destination image if
* one is provided; otherwise a new BufferedImage is created and returned.
*
* The source image cannot use an IndexColorModel, and the destination image
* (if one is provided) must have the same size.
*
* If the final value of a sample is beyond the range of the color model, it
* will be clipped to the appropriate maximum / minimum.
*
* @param src The source image.
* @param dst The destination image.
* @throws IllegalArgumentException if the rasters and/or color spaces are
* incompatible.
* @return The rescaled image.
*/
public final BufferedImage filter(BufferedImage src, BufferedImage dst)
{
// Initial checks
if (scale.length != 1
&& scale.length != src.getColorModel().getNumComponents()
&& (scale.length != src.getColorModel().getNumColorComponents()))
throw new IllegalArgumentException("Source image has wrong number of "
+ "bands for these scaling factors.");
if (dst == null)
dst = createCompatibleDestImage(src, null);
else if (src.getHeight() != dst.getHeight()
|| src.getWidth() != dst.getWidth())
throw new IllegalArgumentException("Source and destination images are "
+ "different sizes.");
// Prepare for possible colorspace conversion
BufferedImage dst2 = dst;
if (dst.getColorModel().getColorSpace().getType() != src.getColorModel().getColorSpace().getType())
dst2 = createCompatibleDestImage(src, src.getColorModel());
// Figure out how many bands to scale
int numBands = scale.length;
if (scale.length == 1)
numBands = src.getColorModel().getNumColorComponents();
boolean[] bands = new boolean[numBands];
// this assumes the alpha, if present, is the last band
Arrays.fill(bands, true);
// Perform rescaling
filter(src.getRaster(), dst2.getRaster(), bands);
// Copy alpha band if needed (ie if it exists and wasn't scaled)
// NOTE: This assumes the alpha component is the last band!
if (src.getColorModel().hasAlpha()
&& numBands == src.getColorModel().getNumColorComponents())
{
dst2.getRaster().setSamples(0, 0, src.getWidth(), src.getHeight(),
numBands,
src.getRaster().getSamples(0, 0,
src.getWidth(),
src.getHeight(),
numBands,
(int[]) null));
}
// Perform colorspace conversion if needed
if (dst != dst2)
new ColorConvertOp(hints).filter(dst2, dst);
return dst;
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#filter(java.awt.image.Raster, java.awt.image.WritableRaster)
*/
public final WritableRaster filter(Raster src, WritableRaster dest)
{
// Required sanity checks
if (scale.length != 1 && scale.length != src.numBands)
throw new IllegalArgumentException("Number of rasters is incompatible "
+ "with the number of scaling "
+ "factors provided.");
if (dest == null)
dest = src.createCompatibleWritableRaster();
else if (src.getHeight() != dest.getHeight()
|| src.getWidth() != dest.getWidth())
throw new IllegalArgumentException("Source and destination rasters are "
+ "different sizes.");
else if (src.numBands != dest.numBands)
throw new IllegalArgumentException("Source and destination rasters "
+ "are incompatible.");
// Filter all bands
boolean[] bands = new boolean[src.getNumBands()];
Arrays.fill(bands, true);
return filter(src, dest, bands);
}
/**
* Perform raster-based filtering on a selected number of bands.
*
* The length of the bands array should equal the number of bands; a true
* element indicates filtering should happen on the corresponding band, while
* a false element will skip the band.
*
* The rasters are assumed to be compatible and non-null.
*
* @param src the source raster.
* @param dest the destination raster.
* @param bands an array indicating which bands to filter.
* @throws NullPointerException if any parameter is null.
* @throws ArrayIndexOutOfBoundsException if the bands array is too small.
* @return the destination raster.
*/
private WritableRaster filter(Raster src, WritableRaster dest, boolean[] bands)
{
int[] values = new int[src.getHeight() * src.getWidth()];
float scaleFactor, offset;
// Find max sample value, to be used for clipping later
int[] maxValue = src.getSampleModel().getSampleSize();
for (int i = 0; i < maxValue.length; i++)
maxValue[i] = (int)Math.pow(2, maxValue[i]) - 1;
// TODO: can this be optimized further?
// Filter all samples of all requested bands
for (int band = 0; band < bands.length; band++)
if (bands[band])
{
values = src.getSamples(src.getMinX(), src.getMinY(), src.getWidth(),
src.getHeight(), band, values);
if (scale.length == 1)
{
scaleFactor = scale[0];
offset = offsets[0];
}
else
{
scaleFactor = scale[band];
offset = offsets[band];
}
for (int i = 0; i < values.length; i++)
{
values[i] = (int) (values[i] * scaleFactor + offset);
// Clip if needed
if (values[i] < 0)
values[i] = 0;
if (values[i] > maxValue[band])
values[i] = maxValue[band];
}
dest.setSamples(dest.getMinX(), dest.getMinY(), dest.getWidth(),
dest.getHeight(), band, values);
}
return dest;
}
/*
* (non-Javadoc)
*
* @see java.awt.image.BufferedImageOp#createCompatibleDestImage(java.awt.image.BufferedImage,
* java.awt.image.ColorModel)
*/
public BufferedImage createCompatibleDestImage(BufferedImage src,
ColorModel dstCM)
{
if (dstCM == null)
return new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
return new BufferedImage(dstCM,
src.getRaster().createCompatibleWritableRaster(),
src.isAlphaPremultiplied(), null);
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#createCompatibleDestRaster(java.awt.image.Raster)
*/
public WritableRaster createCompatibleDestRaster(Raster src)
{
return src.createCompatibleWritableRaster();
}
/* (non-Javadoc)
* @see java.awt.image.BufferedImageOp#getBounds2D(java.awt.image.BufferedImage)
*/
public final Rectangle2D getBounds2D(BufferedImage src)
{
return src.getRaster().getBounds();
}
/* (non-Javadoc)
* @see java.awt.image.RasterOp#getBounds2D(java.awt.image.Raster)
*/
public final Rectangle2D getBounds2D(Raster src)
{
return src.getBounds();
}
/* (non-Javadoc)
* @see java.awt.image.BufferedImageOp#getPoint2D(java.awt.geom.Point2D, java.awt.geom.Point2D)
*/
public final Point2D getPoint2D(Point2D src, Point2D dst)
{
if (dst == null)
dst = (Point2D) src.clone();
else
dst.setLocation(src);
return dst;
}
}