home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 7.7 KB | 247 lines |
- /*
- * @(#)ConvolveOp.java 1.32 98/03/18
- *
- * Copyright 1997 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt.image;
-
- import java.awt.GraphicsEnvironment;
- import java.awt.color.ICC_Profile;
- import java.awt.geom.Rectangle2D;
- import java.awt.Rectangle;
- import java.awt.geom.Point2D;
-
- /**
- * This class implements a convolution from the source
- * to the destination.
- * Convolution using a convolution kernel is a spatial operation that
- * computes the output pixel from an input pixel by multiplying the kernel
- * with the surround of the input pixel.
- * This allows the output pixel to be affected by the immediate neighborhood
- * in a way that can be mathematically specified with a kernel.
- *<p>
- * This class operates with BufferedImage data that are premultiplied with
- * alpha. If the Source BufferedImage has an alpha component, and the
- * color components are not premultiplied with the alpha component, then
- * the data are premultiplied before being convolved. If the Destination
- * has alpha components which are not premultiplied, then alpha is divided
- * out before storing into the Destination. If the Destination has no
- * alpha component, then the resulting alpha is discarded after first
- * dividing it out of the color components.
- * <p>
- * Rasters are treated as having no alpha channel.
- *<p>
- * Note that the Source and the Destination may not be the same object.
- * @version 10 Feb 1997
- * @see Kernel
- */
- public class ConvolveOp implements BufferedImageOp, RasterOp {
- Kernel kernel;
- int edgeHint;
-
- /**
- * Edge condition constants
- */
-
- /**
- * Pixels at the edge of the destination image are set to zero. This
- * is the default.
- */
-
- public static final int EDGE_ZERO_FILL = 0;
-
- /**
- * Pixels at the edge of the source image are copied to
- * the corresponding pixels in the destination without modification.
- */
- public static final int EDGE_NO_OP = 1;
-
- /**
- * Constructs a ConvolveOp given a Kernel and an edge hint.
- * @see Kernel
- * @see #EDGE_NO_OP
- * @see #EDGE_ZERO_FILL
- */
- public ConvolveOp(Kernel kernel, int edgeHint) {
- this.kernel = kernel;
- this.edgeHint = edgeHint;
- }
-
- /**
- * Constructs a ConvolveOp given a Kernel. The edge hint
- * will be EDGE_ZERO_FILL.
- * @see Kernel
- * @see #EDGE_ZERO_FILL
- */
- public ConvolveOp(Kernel kernel) {
- this.kernel = kernel;
- this.edgeHint = EDGE_ZERO_FILL;
- }
-
- /**
- * Return the edge hint.
- * @see #EDGE_NO_OP
- * @see #EDGE_ZERO_FILL
- */
- public int getEdgeHint() {
- return edgeHint;
- }
-
- /**
- * Return the Kernel.
- */
- public Kernel getKernel() {
- return kernel;
- }
-
- /**
- * Perform a convolution on BufferedImages. Each component of the
- * source image will be convolved.
- * 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.
- * The IllegalArgumentException may be thrown if the source is the
- * same as the destination.
- */
- public BufferedImage filter (BufferedImage src, BufferedImage dst) {
- BufferedImage tmpDst = null;
- ColorModel srcCM = src.getColorModel();
-
- if (src == null) {
- throw new NullPointerException("src image is null");
- }
- if (dst == null) {
- dst = createCompatibleDestImage(src, null);
- }
- else {
- ColorModel dstCM = dst.getColorModel();
- if (! srcCM.equals(dstCM)) {
- tmpDst = dst;
- dst = createCompatibleDestImage(src, null);
- }
- }
-
- GraphicsEnvironment ge =
- GraphicsEnvironment.getLocalGraphicsEnvironment();
- ImagingLib imlib = ge.getImagingLib();
-
- if (imlib.filter(this, src, dst) == null) {
- throw new ImagingOpException ("Unable to convolve src image");
- }
-
- if (tmpDst != null) {
- // It worked so now convert it to the real destination
- ColorConvertOp cop = new ColorConvertOp((ICC_Profile[])null);
- cop.filter(tmpDst, dst);
- }
-
- return dst;
- }
-
- /**
- * Perform a convolution on Rasters. Each channel of the source Raster
- * will be convolved.
- * The source and destination must have the same number of channels.
- * If the destination Raster is null, a new Raster will be created.
- * The IllegalArgumentException may be thrown if the source is
- * the same as the destination.
- */
- public WritableRaster filter (Raster src, WritableRaster dst) {
- if (dst == null) {
- dst = createCompatibleDestRaster(src);
- }
- GraphicsEnvironment ge =
- GraphicsEnvironment.getLocalGraphicsEnvironment();
- ImagingLib imlib = ge.getImagingLib();
-
- if (imlib.filter(this, src, dst) == null) {
- throw new ImagingOpException ("Unable to convolve src image");
- }
-
- return dst;
- }
-
- /**
- * Creates an empty destination image with the correct size and number
- * of channels.
- * @param src Source image for the filter operation
- * @param destCM ColorModel of the destination. Can be null.
- */
- public BufferedImage createCompatibleDestImage(BufferedImage src,
- ColorModel destCM) {
- BufferedImage image;
- if (destCM == null) {
- destCM = src.getColorModel();
- // Not much support for ICM
- if (destCM instanceof IndexColorModel) {
- destCM = ColorModel.getRGBdefault();
- }
- }
-
- int w = src.getWidth();
- int h = src.getHeight();
- image = new BufferedImage (destCM,
- destCM.createCompatibleWritableRaster(w, h),
- destCM.isAlphaPremultiplied());
-
- return image;
- }
-
- /**
- * Creates an empty destination Raster with the correct size and number
- * of channels.
- */
- public WritableRaster createCompatibleDestRaster(Raster src) {
- return src.createCompatibleWritableRaster();
- }
-
- /**
- * Returns the bounding box of the filtered destination image. Since
- * this is not a geometric operation, the bounding box does not
- * change.
- */
- public Rectangle2D getDestBounds(BufferedImage src) {
- return getDestBounds(src.getRaster());
- }
-
- /**
- * Returns the bounding box of the filtered destination Raster. Since
- * this is not a geometric operation, the bounding box does not
- * change.
- */
- public Rectangle2D getDestBounds(Raster src) {
- // return new Rectangle (src.getXOffset(),
- // src.getYOffset(),
- // src.getWidth(), src.getHeight());
- return src.getBounds();
- }
-
- /**
- * Returns the location of the destination point given a
- * point in the source image. 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.
- */
- public Point2D getDestPoint(Point2D srcPt, Point2D dstPt) {
- if (dstPt == null) {
- dstPt = new Point2D.Float();
- }
- dstPt.setLocation(srcPt.getX(), srcPt.getY());
-
- return dstPt;
- }
-
- }
-
-
-