0N/A/*
2362N/A * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.image;
0N/A
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.awt.color.ICC_Profile;
0N/Aimport java.awt.geom.Rectangle2D;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.geom.Point2D;
0N/Aimport java.awt.RenderingHints;
0N/Aimport sun.awt.image.ImagingLib;
0N/Aimport java.util.Arrays;
0N/A
0N/A/**
0N/A * This class performs an arbitrary linear combination of the bands
0N/A * in a <CODE>Raster</CODE>, using a specified matrix.
0N/A * <p>
0N/A * The width of the matrix must be equal to the number of bands in the
0N/A * source <CODE>Raster</CODE>, optionally plus one. If there is one more
0N/A * column in the matrix than the number of bands, there is an implied 1 at the
0N/A * end of the vector of band samples representing a pixel. The height
0N/A * of the matrix must be equal to the number of bands in the destination.
0N/A * <p>
0N/A * For example, a 3-banded <CODE>Raster</CODE> might have the following
0N/A * transformation applied to each pixel in order to invert the second band of
0N/A * the <CODE>Raster</CODE>.
0N/A * <pre>
0N/A * [ 1.0 0.0 0.0 0.0 ] [ b1 ]
0N/A * [ 0.0 -1.0 0.0 255.0 ] x [ b2 ]
0N/A * [ 0.0 0.0 1.0 0.0 ] [ b3 ]
0N/A * [ 1 ]
0N/A * </pre>
0N/A *
0N/A * <p>
0N/A * Note that the source and destination can be the same object.
0N/A */
0N/Apublic class BandCombineOp implements RasterOp {
0N/A float[][] matrix;
0N/A int nrows = 0;
0N/A int ncols = 0;
0N/A RenderingHints hints;
0N/A
0N/A /**
0N/A * Constructs a <CODE>BandCombineOp</CODE> with the specified matrix.
0N/A * The width of the matrix must be equal to the number of bands in
0N/A * the source <CODE>Raster</CODE>, optionally plus one. If there is one
0N/A * more column in the matrix than the number of bands, there is an implied
0N/A * 1 at the end of the vector of band samples representing a pixel. The
0N/A * height of the matrix must be equal to the number of bands in the
0N/A * destination.
0N/A * <p>
0N/A * The first subscript is the row index and the second
0N/A * is the column index. This operation uses none of the currently
0N/A * defined rendering hints; the <CODE>RenderingHints</CODE> argument can be
0N/A * null.
0N/A *
0N/A * @param matrix The matrix to use for the band combine operation.
0N/A * @param hints The <CODE>RenderingHints</CODE> object for this operation.
0N/A * Not currently used so it can be null.
0N/A */
0N/A public BandCombineOp (float[][] matrix, RenderingHints hints) {
0N/A nrows = matrix.length;
0N/A ncols = matrix[0].length;
0N/A this.matrix = new float[nrows][];
0N/A for (int i=0; i < nrows; i++) {
0N/A /* Arrays.copyOf is forgiving of the source array being
0N/A * too short, but it is also faster than other cloning
0N/A * methods, so we provide our own protection for short
0N/A * matrix rows.
0N/A */
0N/A if (ncols > matrix[i].length) {
0N/A throw new IndexOutOfBoundsException("row "+i+" too short");
0N/A }
0N/A this.matrix[i] = Arrays.copyOf(matrix[i], ncols);
0N/A }
0N/A this.hints = hints;
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of the linear combination matrix.
0N/A *
0N/A * @return The matrix associated with this band combine operation.
0N/A */
0N/A public final float[][] getMatrix() {
0N/A float[][] ret = new float[nrows][];
0N/A for (int i = 0; i < nrows; i++) {
0N/A ret[i] = Arrays.copyOf(matrix[i], ncols);
0N/A }
0N/A return ret;
0N/A }
0N/A
0N/A /**
0N/A * Transforms the <CODE>Raster</CODE> using the matrix specified in the
0N/A * constructor. An <CODE>IllegalArgumentException</CODE> may be thrown if
0N/A * the number of bands in the source or destination is incompatible with
0N/A * the matrix. See the class comments for more details.
0N/A * <p>
0N/A * If the destination is null, it will be created with a number of bands
0N/A * equalling the number of rows in the matrix. No exception is thrown
0N/A * if the operation causes a data overflow.
0N/A *
0N/A * @param src The <CODE>Raster</CODE> to be filtered.
0N/A * @param dst The <CODE>Raster</CODE> in which to store the results
0N/A * of the filter operation.
0N/A *
0N/A * @return The filtered <CODE>Raster</CODE>.
0N/A *
0N/A * @throws IllegalArgumentException If the number of bands in the
0N/A * source or destination is incompatible with the matrix.
0N/A */
0N/A public WritableRaster filter(Raster src, WritableRaster dst) {
0N/A int nBands = src.getNumBands();
0N/A if (ncols != nBands && ncols != (nBands+1)) {
0N/A throw new IllegalArgumentException("Number of columns in the "+
0N/A "matrix ("+ncols+
0N/A ") must be equal to the number"+
0N/A " of bands ([+1]) in src ("+
0N/A nBands+").");
0N/A }
0N/A if (dst == null) {
0N/A dst = createCompatibleDestRaster(src);
0N/A }
0N/A else if (nrows != dst.getNumBands()) {
0N/A throw new IllegalArgumentException("Number of rows in the "+
0N/A "matrix ("+nrows+
0N/A ") must be equal to the number"+
0N/A " of bands ([+1]) in dst ("+
0N/A nBands+").");
0N/A }
0N/A
0N/A if (ImagingLib.filter(this, src, dst) != null) {
0N/A return dst;
0N/A }
0N/A
0N/A int[] pixel = null;
0N/A int[] dstPixel = new int[dst.getNumBands()];
0N/A float accum;
0N/A int sminX = src.getMinX();
0N/A int sY = src.getMinY();
0N/A int dminX = dst.getMinX();
0N/A int dY = dst.getMinY();
0N/A int sX;
0N/A int dX;
0N/A if (ncols == nBands) {
0N/A for (int y=0; y < src.getHeight(); y++, sY++, dY++) {
0N/A dX = dminX;
0N/A sX = sminX;
0N/A for (int x=0; x < src.getWidth(); x++, sX++, dX++) {
0N/A pixel = src.getPixel(sX, sY, pixel);
0N/A for (int r=0; r < nrows; r++) {
0N/A accum = 0.f;
0N/A for (int c=0; c < ncols; c++) {
0N/A accum += matrix[r][c]*pixel[c];
0N/A }
0N/A dstPixel[r] = (int) accum;
0N/A }
0N/A dst.setPixel(dX, dY, dstPixel);
0N/A }
0N/A }
0N/A }
0N/A else {
0N/A // Need to add constant
0N/A for (int y=0; y < src.getHeight(); y++, sY++, dY++) {
0N/A dX = dminX;
0N/A sX = sminX;
0N/A for (int x=0; x < src.getWidth(); x++, sX++, dX++) {
0N/A pixel = src.getPixel(sX, sY, pixel);
0N/A for (int r=0; r < nrows; r++) {
0N/A accum = 0.f;
0N/A for (int c=0; c < nBands; c++) {
0N/A accum += matrix[r][c]*pixel[c];
0N/A }
0N/A dstPixel[r] = (int) (accum+matrix[r][nBands]);
0N/A }
0N/A dst.setPixel(dX, dY, dstPixel);
0N/A }
0N/A }
0N/A }
0N/A
0N/A return dst;
0N/A }
0N/A
0N/A /**
0N/A * Returns the bounding box of the transformed destination. Since
0N/A * this is not a geometric operation, the bounding box is the same for
0N/A * the source and destination.
0N/A * An <CODE>IllegalArgumentException</CODE> may be thrown if the number of
0N/A * bands in the source is incompatible with the matrix. See
0N/A * the class comments for more details.
0N/A *
0N/A * @param src The <CODE>Raster</CODE> to be filtered.
0N/A *
0N/A * @return The <CODE>Rectangle2D</CODE> representing the destination
0N/A * image's bounding box.
0N/A *
0N/A * @throws IllegalArgumentException If the number of bands in the source
0N/A * is incompatible with the matrix.
0N/A */
0N/A public final Rectangle2D getBounds2D (Raster src) {
0N/A return src.getBounds();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Creates a zeroed destination <CODE>Raster</CODE> with the correct size
0N/A * and number of bands.
0N/A * An <CODE>IllegalArgumentException</CODE> may be thrown if the number of
0N/A * bands in the source is incompatible with the matrix. See
0N/A * the class comments for more details.
0N/A *
0N/A * @param src The <CODE>Raster</CODE> to be filtered.
0N/A *
0N/A * @return The zeroed destination <CODE>Raster</CODE>.
0N/A */
0N/A public WritableRaster createCompatibleDestRaster (Raster src) {
0N/A int nBands = src.getNumBands();
0N/A if ((ncols != nBands) && (ncols != (nBands+1))) {
0N/A throw new IllegalArgumentException("Number of columns in the "+
0N/A "matrix ("+ncols+
0N/A ") must be equal to the number"+
0N/A " of bands ([+1]) in src ("+
0N/A nBands+").");
0N/A }
0N/A if (src.getNumBands() == nrows) {
0N/A return src.createCompatibleWritableRaster();
0N/A }
0N/A else {
0N/A throw new IllegalArgumentException("Don't know how to create a "+
0N/A " compatible Raster with "+
0N/A nrows+" bands.");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the location of the corresponding destination point given a
0N/A * point in the source <CODE>Raster</CODE>. If <CODE>dstPt</CODE> is
0N/A * specified, it is used to hold the return value.
0N/A * Since this is not a geometric operation, the point returned
0N/A * is the same as the specified <CODE>srcPt</CODE>.
0N/A *
0N/A * @param srcPt The <code>Point2D</code> that represents the point in
0N/A * the source <code>Raster</code>
0N/A * @param dstPt The <CODE>Point2D</CODE> in which to store the result.
0N/A *
0N/A * @return The <CODE>Point2D</CODE> in the destination image that
0N/A * corresponds to the specified point in the source image.
0N/A */
0N/A public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) {
0N/A if (dstPt == null) {
0N/A dstPt = new Point2D.Float();
0N/A }
0N/A dstPt.setLocation(srcPt.getX(), srcPt.getY());
0N/A
0N/A return dstPt;
0N/A }
0N/A
0N/A /**
0N/A * Returns the rendering hints for this operation.
0N/A *
0N/A * @return The <CODE>RenderingHints</CODE> object associated with this
0N/A * operation. Returns null if no hints have been set.
0N/A */
0N/A public final RenderingHints getRenderingHints() {
0N/A return hints;
0N/A }
0N/A}