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