| 1 | // Copyright 2005 Huxtable.com. All rights reserved. |
| 2 | // For more information, see <http://www.jhlabs.com/ip/blurring.html>. |
| 3 | package com.jhlabs.image; |
| 4 | |
| 5 | import java.awt.Rectangle; |
| 6 | import java.awt.RenderingHints; |
| 7 | import java.awt.geom.Point2D; |
| 8 | import java.awt.geom.Rectangle2D; |
| 9 | import java.awt.image.BufferedImage; |
| 10 | import java.awt.image.BufferedImageOp; |
| 11 | import java.awt.image.ColorModel; |
| 12 | |
| 13 | /** |
| 14 | * A convenience class which implements those methods of BufferedImageOp which are rarely changed. |
| 15 | */ |
| 16 | public abstract class AbstractBufferedImageOp implements BufferedImageOp |
| 17 | { |
| 18 | /** |
| 19 | * A convenience method for setting ARGB pixels in an image. This tries to avoid the |
| 20 | * performance penalty of BufferedImage.setRGB unmanaging the image. |
| 21 | */ |
| 22 | public void setRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) { |
| 23 | int type = image.getType(); |
| 24 | |
| 25 | if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) { |
| 26 | image.getRaster().setDataElements(x, y, width, height, pixels); |
| 27 | } else { |
| 28 | image.setRGB(x, y, width, height, pixels, 0, width); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public Rectangle2D getBounds2D(BufferedImage src) { |
| 33 | return new Rectangle(0, 0, src.getWidth(), src.getHeight()); |
| 34 | } |
| 35 | |
| 36 | public Point2D getPoint2D(Point2D srcPt, Point2D dstPt) { |
| 37 | if (dstPt == null) { |
| 38 | dstPt = new Point2D.Double(); |
| 39 | } |
| 40 | dstPt.setLocation(srcPt.getX(), srcPt.getY()); |
| 41 | return dstPt; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * A convenience method for getting ARGB pixels from an image. This tries to avoid the |
| 46 | * performance penalty of BufferedImage.getRGB unmanaging the image. |
| 47 | */ |
| 48 | public int[] getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) { |
| 49 | int type = image.getType(); |
| 50 | |
| 51 | if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) { |
| 52 | return (int[]) image.getRaster().getDataElements(x, y, width, height, pixels); |
| 53 | } |
| 54 | return image.getRGB(x, y, width, height, pixels, 0, width); |
| 55 | } |
| 56 | |
| 57 | public RenderingHints getRenderingHints() { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) { |
| 62 | if (dstCM == null) { |
| 63 | dstCM = src.getColorModel(); |
| 64 | } |
| 65 | return new BufferedImage( |
| 66 | dstCM, |
| 67 | dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), |
| 68 | dstCM.isAlphaPremultiplied(), |
| 69 | null); |
| 70 | } |
| 71 | } |