📄 affinetransformop.java
字号:
try { g.setComposite(AlphaComposite.Src); g.drawImage(dst, 0, 0, null); } finally { g.dispose(); } } return origDst; } /** * Transforms the source <CODE>Raster</CODE> and stores the results in * the destination <CODE>Raster</CODE>. This operation performs the * transform band by band. * <p> * If the destination <CODE>Raster</CODE> is null, a new * <CODE>Raster</CODE> is created. * An <CODE>IllegalArgumentException</CODE> may be thrown if the source is * the same as the destination or if the number of bands in * the source is not equal to the number of bands in the * destination. * <p> * The coordinates of the rectangle returned by * <code>getBounds2D(Raster)</code> * are not necessarily the same as the coordinates of the * <code>WritableRaster</code> returned by this method. If the * upper-left corner coordinates of rectangle are negative then * this part of the rectangle is not drawn. If the coordinates * of the rectangle are positive then the filtered image is drawn at * that position in the destination <code>Raster</code>. * <p> * @param src The <CODE>Raster</CODE> to transform. * @param dst The <CODE>Raster</CODE> in which to store the results of the * transformation. * * @return The transformed <CODE>Raster</CODE>. * * @throws ImagingOpException if the raster cannot be transformed * because of a data-processing error that might be * caused by an invalid image format, tile format, or * image-processing operation, or any other unsupported * operation. */ public final WritableRaster filter(Raster src, WritableRaster dst) { if (src == null) { throw new NullPointerException("src image is null"); } if (dst == null) { dst = createCompatibleDestRaster(src); } if (src == dst) { throw new IllegalArgumentException("src image cannot be the "+ "same as the dst image"); } if (src.getNumBands() != dst.getNumBands()) { throw new IllegalArgumentException("Number of src bands ("+ src.getNumBands()+ ") does not match number of "+ " dst bands ("+ dst.getNumBands()+")"); } if (ImagingLib.filter(this, src, dst) == null) { throw new ImagingOpException ("Unable to transform src image"); } return dst; } /** * Returns the bounding box of the transformed destination. The * rectangle returned is the actual bounding box of the * transformed points. The coordinates of the upper-left corner * of the returned rectangle might not be (0, 0). * * @param src The <CODE>BufferedImage</CODE> to be transformed. * * @return The <CODE>Rectangle2D</CODE> representing the destination's * bounding box. */ public final Rectangle2D getBounds2D (BufferedImage src) { return getBounds2D(src.getRaster()); } /** * Returns the bounding box of the transformed destination. The * rectangle returned will be the actual bounding box of the * transformed points. The coordinates of the upper-left corner * of the returned rectangle might not be (0, 0). * * @param src The <CODE>Raster</CODE> to be transformed. * * @return The <CODE>Rectangle2D</CODE> representing the destination's * bounding box. */ public final Rectangle2D getBounds2D (Raster src) { int w = src.getWidth(); int h = src.getHeight(); // Get the bounding box of the src and transform the corners float[] pts = {0, 0, w, 0, w, h, 0, h}; xform.transform(pts, 0, pts, 0, 4); // Get the min, max of the dst float fmaxX = pts[0]; float fmaxY = pts[1]; float fminX = pts[0]; float fminY = pts[1]; int maxX; int maxY; for (int i=2; i < 8; i+=2) { if (pts[i] > fmaxX) { fmaxX = pts[i]; } else if (pts[i] < fminX) { fminX = pts[i]; } if (pts[i+1] > fmaxY) { fmaxY = pts[i+1]; } else if (pts[i+1] < fminY) { fminY = pts[i+1]; } } return new Rectangle2D.Float(fminX, fminY, fmaxX-fminX, fmaxY-fminY); } /** * Creates a zeroed destination image with the correct size and number of * bands. A <CODE>RasterFormatException</CODE> may be thrown if the * transformed width or height is equal to 0. * <p> * If <CODE>destCM</CODE> is null, * an appropriate <CODE>ColorModel</CODE> is used; this * <CODE>ColorModel</CODE> may have * an alpha channel even if the source <CODE>ColorModel</CODE> is opaque. * * @param src The <CODE>BufferedImage</CODE> to be transformed. * @param destCM <CODE>ColorModel</CODE> of the destination. If null, * an appropriate <CODE>ColorModel</CODE> is used. * * @return The zeroed destination image. */ public BufferedImage createCompatibleDestImage (BufferedImage src, ColorModel destCM) { BufferedImage image; Rectangle r = getBounds2D(src).getBounds(); // If r.x (or r.y) is < 0, then we want to only create an image // that is in the positive range. // If r.x (or r.y) is > 0, then we need to create an image that // includes the translation. int w = r.x + r.width; int h = r.y + r.height; if (w <= 0) { throw new RasterFormatException("Transformed width ("+w+ ") is less than or equal to 0."); } if (h <= 0) { throw new RasterFormatException("Transformed height ("+h+ ") is less than or equal to 0."); } if (destCM == null) { ColorModel cm = src.getColorModel(); if (interpolationType != TYPE_NEAREST_NEIGHBOR && (cm instanceof IndexColorModel || cm.getTransparency() == Transparency.OPAQUE)) { image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); } else { image = new BufferedImage(cm, src.getRaster().createCompatibleWritableRaster(w,h), cm.isAlphaPremultiplied(), null); } } else { image = new BufferedImage(destCM, destCM.createCompatibleWritableRaster(w,h), destCM.isAlphaPremultiplied(), null); } return image; } /** * Creates a zeroed destination <CODE>Raster</CODE> with the correct size * and number of bands. A <CODE>RasterFormatException</CODE> may be thrown * if the transformed width or height is equal to 0. * * @param src The <CODE>Raster</CODE> to be transformed. * * @return The zeroed destination <CODE>Raster</CODE>. */ public WritableRaster createCompatibleDestRaster (Raster src) { Rectangle2D r = getBounds2D(src); return src.createCompatibleWritableRaster((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight()); } /** * Returns the location of the corresponding destination point given a * point in the source. If <CODE>dstPt</CODE> is specified, it * is used to hold the return value. * * @param srcPt The <code>Point2D</code> that represents the source * point. * @param dstPt The <CODE>Point2D</CODE> in which to store the result. * * @return The <CODE>Point2D</CODE> in the destination that corresponds to * the specified point in the source. */ public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) { return xform.transform (srcPt, dstPt); } /** * Returns the affine transform used by this transform operation. * * @return The <CODE>AffineTransform</CODE> associated with this op. */ public final AffineTransform getTransform() { return (AffineTransform) xform.clone(); } /** * Returns the rendering hints used by this transform operation. * * @return The <CODE>RenderingHints</CODE> object associated with this op. */ public final RenderingHints getRenderingHints() { if (hints == null) { Object val; switch(interpolationType) { case TYPE_NEAREST_NEIGHBOR: val = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR; break; case TYPE_BILINEAR: val = RenderingHints.VALUE_INTERPOLATION_BILINEAR; break; case TYPE_BICUBIC: val = RenderingHints.VALUE_INTERPOLATION_BICUBIC; break; default: // Should never get here throw new InternalError("Unknown interpolation type "+ interpolationType); } hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION, val); } return hints; } // We need to be able to invert the transform if we want to // transform the image. If the determinant of the matrix is 0, // then we can't invert the transform. void validateTransform(AffineTransform xform) { if (Math.abs(xform.getDeterminant()) <= Double.MIN_VALUE) { throw new ImagingOpException("Unable to invert transform "+xform); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -