package java.awt.image;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
public class RescaleOp implements BufferedImageOp, RasterOp
{
private float[] scale;
private float[] offsets;
private RenderingHints hints = null;
public RescaleOp(float[] scaleFactors,
float[] offsets,
RenderingHints hints)
{
this.scale = scaleFactors;
this.offsets = offsets;
this.hints = hints;
}
public RescaleOp(float scaleFactor,
float offset,
RenderingHints hints)
{
scale = new float[]{ scaleFactor };
offsets = new float[]{offset};
this.hints = hints;
}
public final float[] getScaleFactors(float[] scaleFactors)
{
if (scaleFactors == null)
scaleFactors = new float[scale.length];
System.arraycopy(scale, 0, scaleFactors, 0, scale.length);
return scaleFactors;
}
public final float[] getOffsets(float[] offsets)
{
if (offsets == null)
offsets = new float[this.offsets.length];
System.arraycopy(this.offsets, 0, offsets, 0, this.offsets.length);
return offsets;
}
public final int getNumFactors()
{
return scale.length;
}
public RenderingHints getRenderingHints()
{
return hints;
}
public final BufferedImage filter(BufferedImage src, BufferedImage dst)
{
if (scale.length != offsets.length)
throw new IllegalArgumentException();
ColorModel scm = src.getColorModel();
if (dst == null) dst = createCompatibleDestImage(src, null);
WritableRaster wsrc = src.getRaster();
WritableRaster wdst = dst.getRaster();
if (scale.length == 1 || scale.length == scm.getNumColorComponents())
{
int[] subbands = new int[scm.getNumColorComponents()];
for (int i=0; i < subbands.length; i++) subbands[i] = i;
wsrc =
wsrc.createWritableChild(wsrc.minX, wsrc.minY, wsrc.width, wsrc.height,
wsrc.minX, wsrc.minY, subbands);
}
filter(wsrc, wdst);
return dst;
}
public final WritableRaster filter(Raster src, WritableRaster dest)
{
if (dest == null) dest = src.createCompatibleWritableRaster();
if (src.numBands != dest.numBands || scale.length != offsets.length)
throw new IllegalArgumentException();
if (scale.length != 1 && scale.length != src.numBands)
throw new IllegalArgumentException();
float[] lscale = scale;
float[] loff = offsets;
if (scale.length == 1)
{
lscale = new float[src.numBands];
Arrays.fill(lscale, scale[0]);
loff = new float[src.numBands];
Arrays.fill(loff, offsets[0]);
}
float[] pixel = new float[src.numBands];
for (int y = src.minY; y < src.height + src.minY; y++)
for (int x = src.minX; x < src.width + src.minX; x++)
{
src.getPixel(x, y, pixel);
for (int b = 0; b < src.numBands; b++)
pixel[b] = pixel[b] * lscale[b] + loff[b];
dest.setPixel(x, y, pixel);
}
return dest;
}
public BufferedImage createCompatibleDestImage(BufferedImage src,
ColorModel dstCM)
{
if (dstCM == null) dstCM = src.getColorModel();
WritableRaster wr = src.getRaster().createCompatibleWritableRaster();
BufferedImage image
= new BufferedImage(dstCM, wr, src.isPremultiplied, null);
return image;
}
public WritableRaster createCompatibleDestRaster(Raster src)
{
return src.createCompatibleWritableRaster();
}
public final Rectangle2D getBounds2D(BufferedImage src)
{
return src.getRaster().getBounds();
}
public final Rectangle2D getBounds2D(Raster src)
{
return src.getBounds();
}
public final Point2D getPoint2D(Point2D src, Point2D dst) {
if (dst == null) dst = (Point2D) src.clone();
else dst.setLocation(src);
return dst;
}
}