Source for java.awt.image.ImageFilter

   1: /* ImageFilter.java -- Java class for filtering images
   2:    Copyright (C) 1999 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.awt.image;
  40: 
  41: import java.util.Hashtable;
  42: 
  43: /**
  44:  * The <code>ImageFilter</code> class is a base class which can be
  45:  * extended to provide different types of filters for an image.  By
  46:  * default this class does nothing to an image passing through it.
  47:  *
  48:  * @author C. Brian Jones (cbj@gnu.org)
  49:  */
  50: public class ImageFilter implements ImageConsumer, Cloneable
  51: {
  52:   /**
  53:    * The consumer this filter is filtering an image data stream for.
  54:    * It is initialized in the method <code>getFilterInstance</code>.
  55:    */
  56:   protected ImageConsumer consumer = null;
  57: 
  58:   /**
  59:    * The <code>ImageConsumer</code> can use this method to request
  60:    * the pixels be delivered in top-down, left-right order.
  61:    * <br>
  62:    * The filter can respond in three different ways.
  63:    * <ul>
  64:    *   <li>The default behavior is to forward the request to the
  65:    *       <code>ImageProducer</code>
  66:    *       using the method <code>requestTopDownLeftRightResend</code>
  67:    *       and using the filter as the consumer.</li>
  68:    *   <li>The filter has the pixels and can retransmit them in the
  69:    *       top-down, left-right order.</li>
  70:    *   <li>The filter can do nothing when this method is called.</li>
  71:    * </ul>
  72:    */
  73:   public void resendTopDownLeftRight(ImageProducer ip)
  74:   {
  75:     ip.requestTopDownLeftRightResend(this);
  76:   }
  77: 
  78:   /**
  79:    * By default, returns a shallow copy of the object created by
  80:    * <code>Object.clone()</code>
  81:    *
  82:    * @see java.lang.Object#clone ()
  83:    */
  84:   public Object clone()
  85:   {
  86:     try
  87:       {
  88:         return super.clone();
  89:       }
  90:     catch (CloneNotSupportedException e)
  91:       {
  92:         // This should never happen as this class implements the
  93:         // Cloneable interface.
  94:         throw new InternalError ();
  95:       }
  96:   }
  97: 
  98:   /**
  99:    * 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: }