1. /*
  2. * @(#)ImageFilter.java 1.21 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.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.21 11/29/01
  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. */
  42. public ImageFilter getFilterInstance(ImageConsumer ic) {
  43. ImageFilter instance = (ImageFilter) clone();
  44. instance.consumer = ic;
  45. return instance;
  46. }
  47. /**
  48. * Filters the information provided in the setDimensions method
  49. * of the ImageConsumer interface.
  50. * @see ImageConsumer#setDimensions
  51. */
  52. public void setDimensions(int width, int height) {
  53. consumer.setDimensions(width, height);
  54. }
  55. /**
  56. * Passes the properties from the source object along after adding a
  57. * property indicating the stream of filters it has been run through.
  58. */
  59. public void setProperties(Hashtable props) {
  60. props = (Hashtable) props.clone();
  61. Object o = props.get("filters");
  62. if (o == null) {
  63. props.put("filters", toString());
  64. } else if (o instanceof String) {
  65. props.put("filters", ((String) o)+toString());
  66. }
  67. consumer.setProperties(props);
  68. }
  69. /**
  70. * Filter the information provided in the setColorModel method
  71. * of the ImageConsumer interface.
  72. * @see ImageConsumer#setColorModel
  73. */
  74. public void setColorModel(ColorModel model) {
  75. consumer.setColorModel(model);
  76. }
  77. /**
  78. * Filters the information provided in the setHints method
  79. * of the ImageConsumer interface.
  80. * @see ImageConsumer#setHints
  81. */
  82. public void setHints(int hints) {
  83. consumer.setHints(hints);
  84. }
  85. /**
  86. * Filters the information provided in the setPixels method of the
  87. * ImageConsumer interface which takes an array of bytes.
  88. * @see ImageConsumer#setPixels
  89. */
  90. public void setPixels(int x, int y, int w, int h,
  91. ColorModel model, byte pixels[], int off,
  92. int scansize) {
  93. consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  94. }
  95. /**
  96. * Filters the information provided in the setPixels method of the
  97. * ImageConsumer interface which takes an array of integers.
  98. * @see ImageConsumer#setPixels
  99. */
  100. public void setPixels(int x, int y, int w, int h,
  101. ColorModel model, int pixels[], int off,
  102. int scansize) {
  103. consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  104. }
  105. /**
  106. * Filters the information provided in the imageComplete method of
  107. * the ImageConsumer interface.
  108. * @see ImageConsumer#imageComplete
  109. */
  110. public void imageComplete(int status) {
  111. consumer.imageComplete(status);
  112. }
  113. /**
  114. * Responds to a request for a TopDownLeftRight (TDLR) ordered resend
  115. * of the pixel data from an ImageConsumer.
  116. * The ImageFilter can respond to this request in one of three ways.
  117. * <ol>
  118. * <li>If the filter can determine that it will forward the pixels in
  119. * TDLR order if its upstream producer object sends them
  120. * in TDLR order, then the request is automatically forwarded by
  121. * default to the indicated ImageProducer using this filter as the
  122. * requesting ImageConsumer, so no override is necessary.
  123. * <li>If the filter can resend the pixels in the right order on its
  124. * own (presumably because the generated pixels have been saved in
  125. * some sort of buffer), then it can override this method and
  126. * simply resend the pixels in TDLR order as specified in the
  127. * ImageProducer API. <li>If the filter simply returns from this
  128. * method then the request will be ignored and no resend will
  129. * occur. </ol>
  130. * @see ImageProducer#requestTopDownLeftRightResend
  131. * @param ip The ImageProducer that is feeding this instance of
  132. * the filter - also the ImageProducer that the request should be
  133. * forwarded to if necessary.
  134. */
  135. public void resendTopDownLeftRight(ImageProducer ip) {
  136. ip.requestTopDownLeftRightResend(this);
  137. }
  138. /**
  139. * Clones this object.
  140. */
  141. public Object clone() {
  142. try {
  143. return super.clone();
  144. } catch (CloneNotSupportedException e) {
  145. // this shouldn't happen, since we are Cloneable
  146. throw new InternalError();
  147. }
  148. }
  149. }