001/* ImageFilter.java -- Java class for filtering images
002   Copyright (C) 1999 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.awt.image;
040
041import java.util.Hashtable;
042
043/**
044 * The <code>ImageFilter</code> class is a base class which can be
045 * extended to provide different types of filters for an image.  By
046 * default this class does nothing to an image passing through it.
047 *
048 * @author C. Brian Jones (cbj@gnu.org)
049 */
050public class ImageFilter implements ImageConsumer, Cloneable
051{
052  /**
053   * The consumer this filter is filtering an image data stream for.
054   * It is initialized in the method <code>getFilterInstance</code>.
055   */
056  protected ImageConsumer consumer = null;
057
058  /**
059   * The <code>ImageConsumer</code> can use this method to request
060   * the pixels be delivered in top-down, left-right order.
061   * <br>
062   * The filter can respond in three different ways.
063   * <ul>
064   *   <li>The default behavior is to forward the request to the
065   *       <code>ImageProducer</code>
066   *       using the method <code>requestTopDownLeftRightResend</code>
067   *       and using the filter as the consumer.</li>
068   *   <li>The filter has the pixels and can retransmit them in the
069   *       top-down, left-right order.</li>
070   *   <li>The filter can do nothing when this method is called.</li>
071   * </ul>
072   */
073  public void resendTopDownLeftRight(ImageProducer ip)
074  {
075    ip.requestTopDownLeftRightResend(this);
076  }
077
078  /**
079   * By default, returns a shallow copy of the object created by
080   * <code>Object.clone()</code>
081   *
082   * @see java.lang.Object#clone ()
083   */
084  public Object clone()
085  {
086    try
087      {
088        return super.clone();
089      }
090    catch (CloneNotSupportedException e)
091      {
092        // This should never happen as this class implements the
093        // Cloneable interface.
094        throw new InternalError ();
095      }
096  }
097
098  /**
099   * This is the only method which can set the
100   * <code>ImageConsumer</code> for this filter.  By default a clone
101   * of this filter with the appropriate consumer set is returned.
102   *
103   * @see #clone ()
104   */
105  public ImageFilter getFilterInstance(ImageConsumer ic)
106  {
107    ImageFilter f = (ImageFilter)clone();
108    f.consumer = ic;
109    return f;
110  }
111
112  /**
113   * An <code>ImageProducer</code> indicates the size of the image
114   * being produced using this method.  A filter can override this
115   * method to intercept these calls from the producer in order to
116   * change either the width or the height before in turn calling
117   * the consumer's <code>setDimensions</code> method.
118   *
119   * @param width the width of the image
120   * @param height the height of the image
121   */
122  public void setDimensions(int width, int height)
123  {
124    consumer.setDimensions(width, height);
125  }
126
127  /**
128   * An <code>ImageProducer</code> can set a list of properties
129   * associated with this image by using this method.
130   *
131   * @param props the list of properties associated with this image
132   */
133  public void setProperties(Hashtable<?,?> props)
134  {
135    Hashtable copy = (Hashtable) props.clone();
136    Object o = copy.get("filters");
137    if (o == null)
138      copy.put("filters", toString());
139    else if (o instanceof String)
140      copy.put("filters", ((String) o) + toString());
141
142    consumer.setProperties(copy);
143  }
144
145  /**
146   * Override this method to process calls to this method from the
147   * <code>ImageProducer</code>.  By default the <code>setColorModel</code>
148   * method of the consumer is called with the specified <code>model</code>.
149   *
150   * @param model the color model to be used most often by setPixels
151   *
152   * @see ColorModel
153   */
154  public void setColorModel(ColorModel model)
155  {
156    consumer.setColorModel(model);
157  }
158
159  /**
160   * The <code>ImageProducer</code> should call this method with a
161   * bit mask of hints from any of <code>RANDOMPIXELORDER</code>,
162   * <code>TOPDOWNLEFTRIGHT</code>, <code>COMPLETESCANLINES</code>,
163   * <code>SINGLEPASS</code>, <code>SINGLEFRAME</code> from the
164   * <code>ImageConsumer</code> interface.
165   *
166   * @param flags a bit mask of hints
167   * @see ImageConsumer
168   */
169  public void setHints(int flags)
170  {
171    consumer.setHints(flags);
172  }
173
174  /**
175   * This function delivers a rectangle of pixels where any
176   * pixel(m,n) is stored in the array as a <code>byte</code> at
177   * index (n * scansize + m + offset).
178   *
179   * @param x the x coordinate of the rectangle
180   * @param y the y coordinate of the rectangle
181   * @param w the width of the rectangle
182   * @param h the height of the rectangle
183   * @param model the <code>ColorModel</code> used to translate the pixels
184   * @param pixels the array of pixel values
185   * @param offset the index of the first pixels in the <code>pixels</code> array
186   * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
187   */
188  public void setPixels(int x, int y, int w, int h,
189                        ColorModel model, byte[] pixels, int offset,
190                        int scansize)
191  {
192    consumer.setPixels(x, y, w, h, model, pixels, offset, scansize);
193  }
194
195  /**
196   * This function delivers a rectangle of pixels where any
197   * pixel(m,n) is stored in the array as an <code>int</code> at
198   * index (n * scansize + m + offset).
199   *
200   * @param x the x coordinate of the rectangle
201   * @param y the y coordinate of the rectangle
202   * @param w the width of the rectangle
203   * @param h the height of the rectangle
204   * @param model the <code>ColorModel</code> used to translate the pixels
205   * @param pixels the array of pixel values
206   * @param offset the index of the first pixels in the <code>pixels</code> array
207   * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
208   */
209  public void setPixels(int x, int y, int w, int h,
210                        ColorModel model, int[] pixels, int offset,
211                        int scansize)
212  {
213    consumer.setPixels(x, y, w, h, model, pixels, offset, scansize);
214  }
215
216  /**
217   * The <code>ImageProducer</code> calls this method to indicate a
218   * single frame or the entire image is complete.  The method is
219   * also used to indicate an error in loading or producing the
220   * image.
221   */
222  public void imageComplete(int status)
223  {
224    consumer.imageComplete(status);
225  }
226}