1. /*
  2. * @(#)ImageFilter.java 1.26 03/01/23
  3. *
  4. * Copyright 2003 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.26 01/23/03
  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. public void setProperties(Hashtable props) {
  81. props = (Hashtable) props.clone();
  82. Object o = props.get("filters");
  83. if (o == null) {
  84. props.put("filters", toString());
  85. } else if (o instanceof String) {
  86. props.put("filters", ((String) o)+toString());
  87. }
  88. consumer.setProperties(props);
  89. }
  90. /**
  91. * Filter the information provided in the setColorModel method
  92. * of the ImageConsumer interface.
  93. * <p>
  94. * Note: This method is intended to be called by the ImageProducer
  95. * of the Image whose pixels are being filtered. Developers using
  96. * this class to filter pixels from an image should avoid calling
  97. * this method directly since that operation could interfere
  98. * with the filtering operation.
  99. * @see ImageConsumer#setColorModel
  100. */
  101. public void setColorModel(ColorModel model) {
  102. consumer.setColorModel(model);
  103. }
  104. /**
  105. * Filters the information provided in the setHints method
  106. * of the ImageConsumer interface.
  107. * <p>
  108. * Note: This method is intended to be called by the ImageProducer
  109. * of the Image whose pixels are being filtered. Developers using
  110. * this class to filter pixels from an image should avoid calling
  111. * this method directly since that operation could interfere
  112. * with the filtering operation.
  113. * @see ImageConsumer#setHints
  114. */
  115. public void setHints(int hints) {
  116. consumer.setHints(hints);
  117. }
  118. /**
  119. * Filters the information provided in the setPixels method of the
  120. * ImageConsumer interface which takes an array of bytes.
  121. * <p>
  122. * Note: This method is intended to be called by the ImageProducer
  123. * of the Image whose pixels are being filtered. Developers using
  124. * this class to filter pixels from an image should avoid calling
  125. * this method directly since that operation could interfere
  126. * with the filtering operation.
  127. * @see ImageConsumer#setPixels
  128. */
  129. public void setPixels(int x, int y, int w, int h,
  130. ColorModel model, byte pixels[], int off,
  131. int scansize) {
  132. consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  133. }
  134. /**
  135. * Filters the information provided in the setPixels method of the
  136. * ImageConsumer interface which takes an array of integers.
  137. * <p>
  138. * Note: This method is intended to be called by the ImageProducer
  139. * of the Image whose pixels are being filtered. Developers using
  140. * this class to filter pixels from an image should avoid calling
  141. * this method directly since that operation could interfere
  142. * with the filtering operation.
  143. * @see ImageConsumer#setPixels
  144. */
  145. public void setPixels(int x, int y, int w, int h,
  146. ColorModel model, int pixels[], int off,
  147. int scansize) {
  148. consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  149. }
  150. /**
  151. * Filters the information provided in the imageComplete method of
  152. * the ImageConsumer interface.
  153. * <p>
  154. * Note: This method is intended to be called by the ImageProducer
  155. * of the Image whose pixels are being filtered. Developers using
  156. * this class to filter pixels from an image should avoid calling
  157. * this method directly since that operation could interfere
  158. * with the filtering operation.
  159. * @see ImageConsumer#imageComplete
  160. */
  161. public void imageComplete(int status) {
  162. consumer.imageComplete(status);
  163. }
  164. /**
  165. * Responds to a request for a TopDownLeftRight (TDLR) ordered resend
  166. * of the pixel data from an ImageConsumer.
  167. * The ImageFilter can respond to this request in one of three ways.
  168. * <ol>
  169. * <li>If the filter can determine that it will forward the pixels in
  170. * TDLR order if its upstream producer object sends them
  171. * in TDLR order, then the request is automatically forwarded by
  172. * default to the indicated ImageProducer using this filter as the
  173. * requesting ImageConsumer, so no override is necessary.
  174. * <li>If the filter can resend the pixels in the right order on its
  175. * own (presumably because the generated pixels have been saved in
  176. * some sort of buffer), then it can override this method and
  177. * simply resend the pixels in TDLR order as specified in the
  178. * ImageProducer API. <li>If the filter simply returns from this
  179. * method then the request will be ignored and no resend will
  180. * occur. </ol>
  181. * @see ImageProducer#requestTopDownLeftRightResend
  182. * @param ip The ImageProducer that is feeding this instance of
  183. * the filter - also the ImageProducer that the request should be
  184. * forwarded to if necessary.
  185. */
  186. public void resendTopDownLeftRight(ImageProducer ip) {
  187. ip.requestTopDownLeftRightResend(this);
  188. }
  189. /**
  190. * Clones this object.
  191. */
  192. public Object clone() {
  193. try {
  194. return super.clone();
  195. } catch (CloneNotSupportedException e) {
  196. // this shouldn't happen, since we are Cloneable
  197. throw new InternalError();
  198. }
  199. }
  200. }