1. /*
  2. * @(#)ImageFilter.java 1.31 04/07/16
  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.util.Hashtable;
  9. /**
  10. * This class implements a filter for the set of interface methods that
  11. * are used to deliver data from an ImageProducer to an ImageConsumer.
  12. * It is meant to be used in conjunction with a FilteredImageSource
  13. * object to produce filtered versions of existing images. It is a
  14. * base class that provides the calls needed to implement a "Null filter"
  15. * which has no effect on the data being passed through. Filters should
  16. * subclass this class and override the methods which deal with the
  17. * data that needs to be filtered and modify it as necessary.
  18. *
  19. * @see FilteredImageSource
  20. * @see ImageConsumer
  21. *
  22. * @version 1.31 07/16/04
  23. * @author Jim Graham
  24. */
  25. public class ImageFilter implements ImageConsumer, Cloneable {
  26. /**
  27. * The consumer of the particular image data stream for which this
  28. * instance of the ImageFilter is filtering data. It is not
  29. * initialized during the constructor, but rather during the
  30. * getFilterInstance() method call when the FilteredImageSource
  31. * is creating a unique instance of this object for a particular
  32. * image data stream.
  33. * @see #getFilterInstance
  34. * @see ImageConsumer
  35. */
  36. protected ImageConsumer consumer;
  37. /**
  38. * Returns a unique instance of an ImageFilter object which will
  39. * actually perform the filtering for the specified ImageConsumer.
  40. * The default implementation just clones this object.
  41. * <p>
  42. * Note: This method is intended to be called by the ImageProducer
  43. * of the Image whose pixels are being filtered. Developers using
  44. * this class to filter pixels from an image should avoid calling
  45. * this method directly since that operation could interfere
  46. * with the filtering operation.
  47. * @param ic the specified <code>ImageConsumer</code>
  48. * @return an <code>ImageFilter</code> used to perform the
  49. * filtering for the specified <code>ImageConsumer</code>.
  50. */
  51. public ImageFilter getFilterInstance(ImageConsumer ic) {
  52. ImageFilter instance = (ImageFilter) clone();
  53. instance.consumer = ic;
  54. return instance;
  55. }
  56. /**
  57. * Filters the information provided in the setDimensions method
  58. * of the ImageConsumer interface.
  59. * <p>
  60. * Note: This method is intended to be called by the ImageProducer
  61. * of the Image whose pixels are being filtered. Developers using
  62. * this class to filter pixels from an image should avoid calling
  63. * this method directly since that operation could interfere
  64. * with the filtering operation.
  65. * @see ImageConsumer#setDimensions
  66. */
  67. public void setDimensions(int width, int height) {
  68. consumer.setDimensions(width, height);
  69. }
  70. /**
  71. * Passes the properties from the source object along after adding a
  72. * property indicating the stream of filters it has been run through.
  73. * <p>
  74. * Note: This method is intended to be called by the ImageProducer
  75. * of the Image whose pixels are being filtered. Developers using
  76. * this class to filter pixels from an image should avoid calling
  77. * this method directly since that operation could interfere
  78. * with the filtering operation.
  79. *
  80. * @param props the properties from the source object
  81. * @exception NullPointerException if <code>props</code> is null
  82. */
  83. public void setProperties(Hashtable<?,?> props) {
  84. Hashtable<Object,Object> p = (Hashtable<Object,Object>)props.clone();
  85. Object o = p.get("filters");
  86. if (o == null) {
  87. p.put("filters", toString());
  88. } else if (o instanceof String) {
  89. p.put("filters", ((String) o)+toString());
  90. }
  91. consumer.setProperties(p);
  92. }
  93. /**
  94. * Filter the information provided in the setColorModel method
  95. * of the ImageConsumer interface.
  96. * <p>
  97. * Note: This method is intended to be called by the ImageProducer
  98. * of the Image whose pixels are being filtered. Developers using
  99. * this class to filter pixels from an image should avoid calling
  100. * this method directly since that operation could interfere
  101. * with the filtering operation.
  102. * @see ImageConsumer#setColorModel
  103. */
  104. public void setColorModel(ColorModel model) {
  105. consumer.setColorModel(model);
  106. }
  107. /**
  108. * Filters the information provided in the setHints method
  109. * of the ImageConsumer interface.
  110. * <p>
  111. * Note: This method is intended to be called by the ImageProducer
  112. * of the Image whose pixels are being filtered. Developers using
  113. * this class to filter pixels from an image should avoid calling
  114. * this method directly since that operation could interfere
  115. * with the filtering operation.
  116. * @see ImageConsumer#setHints
  117. */
  118. public void setHints(int hints) {
  119. consumer.setHints(hints);
  120. }
  121. /**
  122. * Filters the information provided in the setPixels method of the
  123. * ImageConsumer interface which takes an array of bytes.
  124. * <p>
  125. * Note: This method is intended to be called by the ImageProducer
  126. * of the Image whose pixels are being filtered. Developers using
  127. * this class to filter pixels from an image should avoid calling
  128. * this method directly since that operation could interfere
  129. * with the filtering operation.
  130. * @see ImageConsumer#setPixels
  131. */
  132. public void setPixels(int x, int y, int w, int h,
  133. ColorModel model, byte pixels[], int off,
  134. int scansize) {
  135. consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  136. }
  137. /**
  138. * Filters the information provided in the setPixels method of the
  139. * ImageConsumer interface which takes an array of integers.
  140. * <p>
  141. * Note: This method is intended to be called by the ImageProducer
  142. * of the Image whose pixels are being filtered. Developers using
  143. * this class to filter pixels from an image should avoid calling
  144. * this method directly since that operation could interfere
  145. * with the filtering operation.
  146. * @see ImageConsumer#setPixels
  147. */
  148. public void setPixels(int x, int y, int w, int h,
  149. ColorModel model, int pixels[], int off,
  150. int scansize) {
  151. consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  152. }
  153. /**
  154. * Filters the information provided in the imageComplete method of
  155. * the ImageConsumer interface.
  156. * <p>
  157. * Note: This method is intended to be called by the ImageProducer
  158. * of the Image whose pixels are being filtered. Developers using
  159. * this class to filter pixels from an image should avoid calling
  160. * this method directly since that operation could interfere
  161. * with the filtering operation.
  162. * @see ImageConsumer#imageComplete
  163. */
  164. public void imageComplete(int status) {
  165. consumer.imageComplete(status);
  166. }
  167. /**
  168. * Responds to a request for a TopDownLeftRight (TDLR) ordered resend
  169. * of the pixel data from an <code>ImageConsumer</code>.
  170. * When an <code>ImageConsumer</code> being fed
  171. * by an instance of this <code>ImageFilter</code>
  172. * requests a resend of the data in TDLR order,
  173. * the <code>FilteredImageSource</code>
  174. * invokes this method of the <code>ImageFilter</code>.
  175. *
  176. * <p>
  177. *
  178. * An <code>ImageFilter</code> subclass might override this method or not,
  179. * depending on if and how it can send data in TDLR order.
  180. * Three possibilities exist:
  181. *
  182. * <ul>
  183. * <li>
  184. * Do not override this method.
  185. * This makes the subclass use the default implementation,
  186. * which is to
  187. * forward the request
  188. * to the indicated <code>ImageProducer</code>
  189. * using this filter as the requesting <code>ImageConsumer</code>.
  190. * This behavior
  191. * is appropriate if the filter can determine
  192. * that it will forward the pixels
  193. * in TDLR order if its upstream producer object
  194. * sends them in TDLR order.
  195. *
  196. * <li>
  197. * Override the method to simply send the data.
  198. * This is appropriate if the filter can handle the request itself —
  199. * for example,
  200. * if the generated pixels have been saved in some sort of buffer.
  201. *
  202. * <li>
  203. * Override the method to do nothing.
  204. * This is appropriate
  205. * if the filter cannot produce filtered data in TDLR order.
  206. * </ul>
  207. *
  208. * @see ImageProducer#requestTopDownLeftRightResend
  209. * @param ip the ImageProducer that is feeding this instance of
  210. * the filter - also the ImageProducer that the request should be
  211. * forwarded to if necessary
  212. * @exception NullPointerException if <code>ip</code> is null
  213. */
  214. public void resendTopDownLeftRight(ImageProducer ip) {
  215. ip.requestTopDownLeftRightResend(this);
  216. }
  217. /**
  218. * Clones this object.
  219. */
  220. public Object clone() {
  221. try {
  222. return super.clone();
  223. } catch (CloneNotSupportedException e) {
  224. // this shouldn't happen, since we are Cloneable
  225. throw new InternalError();
  226. }
  227. }
  228. }