1. /*
  2. * @(#)ConvolveOp.java 1.40 01/11/29
  3. *
  4. * Copyright 2002 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. * @see Kernel
  69. * @see #EDGE_NO_OP
  70. * @see #EDGE_ZERO_FILL
  71. * @see java.awt.RenderingHints
  72. */
  73. public ConvolveOp(Kernel kernel, int edgeCondition, RenderingHints hints) {
  74. this.kernel = kernel;
  75. this.edgeHint = edgeCondition;
  76. this.hints = hints;
  77. }
  78. /**
  79. * Constructs a ConvolveOp given a Kernel. The edge condition
  80. * will be EDGE_ZERO_FILL.
  81. * @see Kernel
  82. * @see #EDGE_ZERO_FILL
  83. */
  84. public ConvolveOp(Kernel kernel) {
  85. this.kernel = kernel;
  86. this.edgeHint = EDGE_ZERO_FILL;
  87. }
  88. /**
  89. * Returns the edge condition.
  90. * @see #EDGE_NO_OP
  91. * @see #EDGE_ZERO_FILL
  92. */
  93. public int getEdgeCondition() {
  94. return edgeHint;
  95. }
  96. /**
  97. * Returns the Kernel.
  98. */
  99. public final Kernel getKernel() {
  100. return (Kernel) kernel.clone();
  101. }
  102. /**
  103. * Performs a convolution on BufferedImages. Each component of the
  104. * source image will be convolved (including the alpha component, if
  105. * present).
  106. * If the color model in the source image is not the same as that
  107. * in the destination image, the pixels will be converted
  108. * in the destination. If the destination image is null,
  109. * a BufferedImage will be created with the source ColorModel.
  110. * The IllegalArgumentException may be thrown if the source is the
  111. * same as the destination.
  112. * @param src the source <code>BufferedImage</code> to filter
  113. * @param dst the destination <code>BufferedImage</code> for the
  114. * filtered <code>src</code>
  115. * @return the filtered <code>BufferedImage</code>
  116. * @throws NullPointerException if <code>src</code> is <code>null</code>
  117. * @throws IllegalArgumentException if <code>src</code> equals
  118. * <code>dst</code>
  119. * @throws ImagingOpException if <code>src</code> cannot be filtered
  120. */
  121. public final BufferedImage filter (BufferedImage src, BufferedImage dst) {
  122. if (src == null) {
  123. throw new NullPointerException("src image is null");
  124. }
  125. if (src == dst) {
  126. throw new IllegalArgumentException("src image cannot be the "+
  127. "same as the dst image");
  128. }
  129. boolean needToConvert = false;
  130. ColorModel srcCM = src.getColorModel();
  131. ColorModel dstCM;
  132. BufferedImage origDst = dst;
  133. // Can't convolve an IndexColorModel. Need to expand it
  134. if (srcCM instanceof IndexColorModel) {
  135. IndexColorModel icm = (IndexColorModel) srcCM;
  136. src = icm.convertToIntDiscrete(src.getRaster(), false);
  137. srcCM = src.getColorModel();
  138. }
  139. if (dst == null) {
  140. dst = createCompatibleDestImage(src, null);
  141. dstCM = srcCM;
  142. origDst = dst;
  143. }
  144. else {
  145. dstCM = dst.getColorModel();
  146. if (srcCM.getColorSpace().getType() !=
  147. dstCM.getColorSpace().getType())
  148. {
  149. needToConvert = true;
  150. dst = createCompatibleDestImage(src, null);
  151. dstCM = dst.getColorModel();
  152. }
  153. else if (dstCM instanceof IndexColorModel) {
  154. dst = createCompatibleDestImage(src, null);
  155. dstCM = dst.getColorModel();
  156. }
  157. }
  158. if (ImagingLib.filter(this, src, dst) == null) {
  159. throw new ImagingOpException ("Unable to convolve src image");
  160. }
  161. if (needToConvert) {
  162. ColorConvertOp ccop = new ColorConvertOp(hints);
  163. ccop.filter(dst, origDst);
  164. }
  165. else if (origDst != dst) {
  166. java.awt.Graphics2D g = origDst.createGraphics();
  167. g.drawImage(dst, 0, 0, null);
  168. }
  169. return origDst;
  170. }
  171. /**
  172. * Performs a convolution on Rasters. Each band of the source Raster
  173. * will be convolved.
  174. * The source and destination must have the same number of bands.
  175. * If the destination Raster is null, a new Raster will be created.
  176. * The IllegalArgumentException may be thrown if the source is
  177. * the same as the destination.
  178. * @param src the source <code>Raster</code> to filter
  179. * @param dst the destination <code>WritableRaster</code> for the
  180. * filtered <code>src</code>
  181. * @return the filtered <code>WritableRaster</code>
  182. * @throws NullPointerException if <code>src</code> is <code>null</code>
  183. * @throws ImagingOpException if <code>src</code> and <code>dst</code>
  184. * do not have the same number of bands
  185. * @throws ImagingOpException if <code>src</code> cannot be filtered
  186. * @throws IllegalArgumentException if <code>src</code> equals
  187. * <code>dst</code>
  188. */
  189. public final WritableRaster filter (Raster src, WritableRaster dst) {
  190. if (dst == null) {
  191. dst = createCompatibleDestRaster(src);
  192. }
  193. else if (src == dst) {
  194. throw new IllegalArgumentException("src image cannot be the "+
  195. "same as the dst image");
  196. }
  197. else if (src.getNumBands() != dst.getNumBands()) {
  198. throw new ImagingOpException("Different number of bands in src "+
  199. " and dst Rasters");
  200. }
  201. if (ImagingLib.filter(this, src, dst) == null) {
  202. throw new ImagingOpException ("Unable to convolve src image");
  203. }
  204. return dst;
  205. }
  206. /**
  207. * Creates a zeroed destination image with the correct size and number
  208. * of bands. If destCM is null, an appropriate ColorModel will be used.
  209. * @param src Source image for the filter operation.
  210. * @param destCM ColorModel of the destination. Can be null.
  211. */
  212. public BufferedImage createCompatibleDestImage(BufferedImage src,
  213. ColorModel destCM) {
  214. BufferedImage image;
  215. if (destCM == null) {
  216. destCM = src.getColorModel();
  217. // Not much support for ICM
  218. if (destCM instanceof IndexColorModel) {
  219. destCM = ColorModel.getRGBdefault();
  220. }
  221. }
  222. int w = src.getWidth();
  223. int h = src.getHeight();
  224. image = new BufferedImage (destCM,
  225. destCM.createCompatibleWritableRaster(w, h),
  226. destCM.isAlphaPremultiplied(), null);
  227. return image;
  228. }
  229. /**
  230. * Creates a zeroed destination Raster with the correct size and number
  231. * of bands, given this source.
  232. */
  233. public WritableRaster createCompatibleDestRaster(Raster src) {
  234. return src.createCompatibleWritableRaster();
  235. }
  236. /**
  237. * Returns the bounding box of the filtered destination image. Since
  238. * this is not a geometric operation, the bounding box does not
  239. * change.
  240. */
  241. public final Rectangle2D getBounds2D(BufferedImage src) {
  242. return getBounds2D(src.getRaster());
  243. }
  244. /**
  245. * Returns the bounding box of the filtered destination Raster. Since
  246. * this is not a geometric operation, the bounding box does not
  247. * change.
  248. */
  249. public final Rectangle2D getBounds2D(Raster src) {
  250. return src.getBounds();
  251. }
  252. /**
  253. * Returns the location of the destination point given a
  254. * point in the source. If dstPt is non-null, it will
  255. * be used to hold the return value. Since this is not a geometric
  256. * operation, the srcPt will equal the dstPt.
  257. */
  258. public final Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
  259. if (dstPt == null) {
  260. dstPt = new Point2D.Float();
  261. }
  262. dstPt.setLocation(srcPt.getX(), srcPt.getY());
  263. return dstPt;
  264. }
  265. /**
  266. * Returns the rendering hints for this op.
  267. */
  268. public final RenderingHints getRenderingHints() {
  269. return hints;
  270. }
  271. }