1. /*
  2. * @(#)ConvolveOp.java 1.46 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.image;
  8. import java.awt.color.ICC_Profile;
  9. import java.awt.geom.Rectangle2D;
  10. import java.awt.Rectangle;
  11. import java.awt.RenderingHints;
  12. import java.awt.geom.Point2D;
  13. import sun.awt.image.ImagingLib;
  14. /**
  15. * This class implements a convolution from the source
  16. * to the destination.
  17. * Convolution using a convolution kernel is a spatial operation that
  18. * computes the output pixel from an input pixel by multiplying the kernel
  19. * with the surround of the input pixel.
  20. * This allows the output pixel to be affected by the immediate neighborhood
  21. * in a way that can be mathematically specified with a kernel.
  22. *<p>
  23. * This class operates with BufferedImage data in which color components are
  24. * premultiplied with the alpha component. If the Source BufferedImage has
  25. * an alpha component, and the color components are not premultiplied with
  26. * the alpha component, then the data are premultiplied before being
  27. * convolved. If the Destination has color components which are not
  28. * premultiplied, then alpha is divided out before storing into the
  29. * Destination (if alpha is 0, the color components are set to 0). If the
  30. * Destination has no alpha component, then the resulting alpha is discarded
  31. * after first dividing it out of the color components.
  32. * <p>
  33. * Rasters are treated as having no alpha channel. If the above treatment
  34. * of the alpha channel in BufferedImages is not desired, it may be avoided
  35. * by getting the Raster of a source BufferedImage and using the filter method
  36. * of this class which works with Rasters.
  37. * <p>
  38. * If a RenderingHints object is specified in the constructor, the
  39. * color rendering hint and the dithering hint may be used when color
  40. * conversion is required.
  41. *<p>
  42. * Note that the Source and the Destination may not be the same object.
  43. * @version 10 Feb 1997
  44. * @see Kernel
  45. * @see java.awt.RenderingHints#KEY_COLOR_RENDERING
  46. * @see java.awt.RenderingHints#KEY_DITHERING
  47. */
  48. public class ConvolveOp implements BufferedImageOp, RasterOp {
  49. Kernel kernel;
  50. int edgeHint;
  51. RenderingHints hints;
  52. /**
  53. * Edge condition constants.
  54. */
  55. /**
  56. * Pixels at the edge of the destination image are set to zero. This
  57. * is the default.
  58. */
  59. public static final int EDGE_ZERO_FILL = 0;
  60. /**
  61. * Pixels at the edge of the source image are copied to
  62. * the corresponding pixels in the destination without modification.
  63. */
  64. public static final int EDGE_NO_OP = 1;
  65. /**
  66. * Constructs a ConvolveOp given a Kernel, an edge condition, and a
  67. * RenderingHints object (which may be null).
  68. * @param kernel the specified <code>Kernel</code>
  69. * @param edgeCondition the specified edge condition
  70. * @param hints the specified <code>RenderingHints</code> object
  71. * @see Kernel
  72. * @see #EDGE_NO_OP
  73. * @see #EDGE_ZERO_FILL
  74. * @see java.awt.RenderingHints
  75. */
  76. public ConvolveOp(Kernel kernel, int edgeCondition, RenderingHints hints) {
  77. this.kernel = kernel;
  78. this.edgeHint = edgeCondition;
  79. this.hints = hints;
  80. }
  81. /**
  82. * Constructs a ConvolveOp given a Kernel. The edge condition
  83. * will be EDGE_ZERO_FILL.
  84. * @param kernel the specified <code>Kernel</code>
  85. * @see Kernel
  86. * @see #EDGE_ZERO_FILL
  87. */
  88. public ConvolveOp(Kernel kernel) {
  89. this.kernel = kernel;
  90. this.edgeHint = EDGE_ZERO_FILL;
  91. }
  92. /**
  93. * Returns the edge condition.
  94. * @return the edge condition of this <code>ConvolveOp</code>.
  95. * @see #EDGE_NO_OP
  96. * @see #EDGE_ZERO_FILL
  97. */
  98. public int getEdgeCondition() {
  99. return edgeHint;
  100. }
  101. /**
  102. * Returns the Kernel.
  103. * @return the <code>Kernel</code> of this <code>ConvolveOp</code>.
  104. */
  105. public final Kernel getKernel() {
  106. return (Kernel) kernel.clone();
  107. }
  108. /**
  109. * Performs a convolution on BufferedImages. Each component of the
  110. * source image will be convolved (including the alpha component, if
  111. * present).
  112. * If the color model in the source image is not the same as that
  113. * in the destination image, the pixels will be converted
  114. * in the destination. If the destination image is null,
  115. * a BufferedImage will be created with the source ColorModel.
  116. * The IllegalArgumentException may be thrown if the source is the
  117. * same as the destination.
  118. * @param src the source <code>BufferedImage</code> to filter
  119. * @param dst the destination <code>BufferedImage</code> for the
  120. * filtered <code>src</code>
  121. * @return the filtered <code>BufferedImage</code>
  122. * @throws NullPointerException if <code>src</code> is <code>null</code>
  123. * @throws IllegalArgumentException if <code>src</code> equals
  124. * <code>dst</code>
  125. * @throws ImagingOpException if <code>src</code> cannot be filtered
  126. */
  127. public final BufferedImage filter (BufferedImage src, BufferedImage dst) {
  128. if (src == null) {
  129. throw new NullPointerException("src image is null");
  130. }
  131. if (src == dst) {
  132. throw new IllegalArgumentException("src image cannot be the "+
  133. "same as the dst image");
  134. }
  135. boolean needToConvert = false;
  136. ColorModel srcCM = src.getColorModel();
  137. ColorModel dstCM;
  138. BufferedImage origDst = dst;
  139. // Can't convolve an IndexColorModel. Need to expand it
  140. if (srcCM instanceof IndexColorModel) {
  141. IndexColorModel icm = (IndexColorModel) srcCM;
  142. src = icm.convertToIntDiscrete(src.getRaster(), false);
  143. srcCM = src.getColorModel();
  144. }
  145. if (dst == null) {
  146. dst = createCompatibleDestImage(src, null);
  147. dstCM = srcCM;
  148. origDst = dst;
  149. }
  150. else {
  151. dstCM = dst.getColorModel();
  152. if (srcCM.getColorSpace().getType() !=
  153. dstCM.getColorSpace().getType())
  154. {
  155. needToConvert = true;
  156. dst = createCompatibleDestImage(src, null);
  157. dstCM = dst.getColorModel();
  158. }
  159. else if (dstCM instanceof IndexColorModel) {
  160. dst = createCompatibleDestImage(src, null);
  161. dstCM = dst.getColorModel();
  162. }
  163. }
  164. if (ImagingLib.filter(this, src, dst) == null) {
  165. throw new ImagingOpException ("Unable to convolve src image");
  166. }
  167. if (needToConvert) {
  168. ColorConvertOp ccop = new ColorConvertOp(hints);
  169. ccop.filter(dst, origDst);
  170. }
  171. else if (origDst != dst) {
  172. java.awt.Graphics2D g = origDst.createGraphics();
  173. try {
  174. g.drawImage(dst, 0, 0, null);
  175. } finally {
  176. g.dispose();
  177. }
  178. }
  179. return origDst;
  180. }
  181. /**
  182. * Performs a convolution on Rasters. Each band of the source Raster
  183. * will be convolved.
  184. * The source and destination must have the same number of bands.
  185. * If the destination Raster is null, a new Raster will be created.
  186. * The IllegalArgumentException may be thrown if the source is
  187. * the same as the destination.
  188. * @param src the source <code>Raster</code> to filter
  189. * @param dst the destination <code>WritableRaster</code> for the
  190. * filtered <code>src</code>
  191. * @return the filtered <code>WritableRaster</code>
  192. * @throws NullPointerException if <code>src</code> is <code>null</code>
  193. * @throws ImagingOpException if <code>src</code> and <code>dst</code>
  194. * do not have the same number of bands
  195. * @throws ImagingOpException if <code>src</code> cannot be filtered
  196. * @throws IllegalArgumentException if <code>src</code> equals
  197. * <code>dst</code>
  198. */
  199. public final WritableRaster filter (Raster src, WritableRaster dst) {
  200. if (dst == null) {
  201. dst = createCompatibleDestRaster(src);
  202. }
  203. else if (src == dst) {
  204. throw new IllegalArgumentException("src image cannot be the "+
  205. "same as the dst image");
  206. }
  207. else if (src.getNumBands() != dst.getNumBands()) {
  208. throw new ImagingOpException("Different number of bands in src "+
  209. " and dst Rasters");
  210. }
  211. if (ImagingLib.filter(this, src, dst) == null) {
  212. throw new ImagingOpException ("Unable to convolve src image");
  213. }
  214. return dst;
  215. }
  216. /**
  217. * Creates a zeroed destination image with the correct size and number
  218. * of bands. If destCM is null, an appropriate ColorModel will be used.
  219. * @param src Source image for the filter operation.
  220. * @param destCM ColorModel of the destination. Can be null.
  221. * @return a destination <code>BufferedImage</code> with the correct
  222. * size and number of bands.
  223. */
  224. public BufferedImage createCompatibleDestImage(BufferedImage src,
  225. ColorModel destCM) {
  226. BufferedImage image;
  227. if (destCM == null) {
  228. destCM = src.getColorModel();
  229. // Not much support for ICM
  230. if (destCM instanceof IndexColorModel) {
  231. destCM = ColorModel.getRGBdefault();
  232. }
  233. }
  234. int w = src.getWidth();
  235. int h = src.getHeight();
  236. image = new BufferedImage (destCM,
  237. destCM.createCompatibleWritableRaster(w, h),
  238. destCM.isAlphaPremultiplied(), null);
  239. return image;
  240. }
  241. /**
  242. * Creates a zeroed destination Raster with the correct size and number
  243. * of bands, given this source.
  244. */
  245. public WritableRaster createCompatibleDestRaster(Raster src) {
  246. return src.createCompatibleWritableRaster();
  247. }
  248. /**
  249. * Returns the bounding box of the filtered destination image. Since
  250. * this is not a geometric operation, the bounding box does not
  251. * change.
  252. */
  253. public final Rectangle2D getBounds2D(BufferedImage src) {
  254. return getBounds2D(src.getRaster());
  255. }
  256. /**
  257. * Returns the bounding box of the filtered destination Raster. Since
  258. * this is not a geometric operation, the bounding box does not
  259. * change.
  260. */
  261. public final Rectangle2D getBounds2D(Raster src) {
  262. return src.getBounds();
  263. }
  264. /**
  265. * Returns the location of the destination point given a
  266. * point in the source. If dstPt is non-null, it will
  267. * be used to hold the return value. Since this is not a geometric
  268. * operation, the srcPt will equal the dstPt.
  269. */
  270. public final Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
  271. if (dstPt == null) {
  272. dstPt = new Point2D.Float();
  273. }
  274. dstPt.setLocation(srcPt.getX(), srcPt.getY());
  275. return dstPt;
  276. }
  277. /**
  278. * Returns the rendering hints for this op.
  279. */
  280. public final RenderingHints getRenderingHints() {
  281. return hints;
  282. }
  283. }