1. /*
  2. * @(#)ImageFilter.java 1.23 00/02/02
  3. *
  4. * Copyright 1995-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.util.Hashtable;
  12. /**
  13. * This class implements a filter for the set of interface methods that
  14. * are used to deliver data from an ImageProducer to an ImageConsumer.
  15. * It is meant to be used in conjunction with a FilteredImageSource
  16. * object to produce filtered versions of existing images. It is a
  17. * base class that provides the calls needed to implement a "Null filter"
  18. * which has no effect on the data being passed through. Filters should
  19. * subclass this class and override the methods which deal with the
  20. * data that needs to be filtered and modify it as necessary.
  21. *
  22. * @see FilteredImageSource
  23. * @see ImageConsumer
  24. *
  25. * @version 1.23 02/02/00
  26. * @author Jim Graham
  27. */
  28. public class ImageFilter implements ImageConsumer, Cloneable {
  29. /**
  30. * The consumer of the particular image data stream for which this
  31. * instance of the ImageFilter is filtering data. It is not
  32. * initialized during the constructor, but rather during the
  33. * getFilterInstance() method call when the FilteredImageSource
  34. * is creating a unique instance of this object for a particular
  35. * image data stream.
  36. * @see #getFilterInstance
  37. * @see ImageConsumer
  38. */
  39. protected ImageConsumer consumer;
  40. /**
  41. * Returns a unique instance of an ImageFilter object which will
  42. * actually perform the filtering for the specified ImageConsumer.
  43. * The default implementation just clones this object.
  44. * <p>
  45. * Note: This method is intended to be called by the ImageProducer
  46. * of the Image whose pixels are being filtered. Developers using
  47. * this class to filter pixels from an image should avoid calling
  48. * this method directly since that operation could interfere
  49. * with the filtering operation.
  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. }