home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / ImageFilter.java < prev    next >
Text File  |  1996-05-03  |  6KB  |  175 lines

  1. /*
  2.  * @(#)ImageFilter.java    1.16 96/03/21 Jim Graham
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt.image;
  21.  
  22. import java.util.Hashtable;
  23.  
  24. /**
  25.  * This class implements a filter for the set of interface methods that
  26.  * are used to deliver data from an ImageProducer to an ImageConsumer.
  27.  * It is meant to be used in conjunction with a FilteredImageSource
  28.  * object to produce filtered versions of existing images.  It is a
  29.  * base class that provides the calls needed to implement a "Null filter"
  30.  * which has no effect on the data being passed through.  Filters should
  31.  * subclass this class and override the methods which deal with the
  32.  * data that needs to be filtered and modify it as necessary.
  33.  *
  34.  * @see FilteredImageSource
  35.  * @see ImageConsumer
  36.  *
  37.  * @version    1.16 21 Mar 1996
  38.  * @author     Jim Graham
  39.  */
  40. public class ImageFilter implements ImageConsumer, Cloneable {
  41.     /**
  42.      * The consumer of the particular image data stream for which this
  43.      * instance of the ImageFilter is filtering data.  It is not
  44.      * initialized during the constructor, but rather during the
  45.      * getFilterInstance() method call when the FilteredImageSource
  46.      * is creating a unique instance of this object for a particular
  47.      * image data stream.
  48.      * @see #getFilterInstance
  49.      * @see ImageConsumer
  50.      */
  51.     protected ImageConsumer consumer;
  52.  
  53.     /**
  54.      * Returns a unique instance of an ImageFilter object which will
  55.      * actually perform the filtering for the specified ImageConsumer.
  56.      * The default implementation just clones this object.
  57.      */
  58.     public ImageFilter getFilterInstance(ImageConsumer ic) {
  59.     ImageFilter instance = (ImageFilter) clone();
  60.     instance.consumer = ic;
  61.     return instance;
  62.     }
  63.  
  64.     /**
  65.      * Filters the information provided in the setDimensions method
  66.      * of the ImageConsumer interface.
  67.      * @see ImageConsumer#setDimensions
  68.      */
  69.     public void setDimensions(int width, int height) {
  70.     consumer.setDimensions(width, height);
  71.     }
  72.  
  73.     /**
  74.      * Passes the properties from the source object along after adding a
  75.      * property indicating the stream of filters it has been run through.
  76.      */
  77.     public void setProperties(Hashtable props) {
  78.     props = (Hashtable) props.clone();
  79.     Object o = props.get("filters");
  80.     if (o == null) {
  81.         props.put("filters", toString());
  82.     } else if (o instanceof String) {
  83.         props.put("filters", ((String) o)+toString());
  84.     }
  85.     consumer.setProperties(props);
  86.     }
  87.  
  88.     /**
  89.      * Filter the information provided in the setColorModel method
  90.      * of the ImageConsumer interface.
  91.      * @see ImageConsumer#setColorModel
  92.      */
  93.     public void setColorModel(ColorModel model) {
  94.     consumer.setColorModel(model);
  95.     }
  96.  
  97.     /**
  98.      * Filters the information provided in the setHints method
  99.      * of the ImageConsumer interface.
  100.      * @see ImageConsumer#setHints
  101.      */
  102.     public void setHints(int hints) {
  103.     consumer.setHints(hints);
  104.     }
  105.  
  106.     /**
  107.      * Filters the information provided in the setPixels method of the
  108.      * ImageConsumer interface which takes an array of bytes.
  109.      * @see ImageConsumer#setPixels
  110.      */
  111.     public void setPixels(int x, int y, int w, int h,
  112.               ColorModel model, byte pixels[], int off,
  113.               int scansize) {
  114.     consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  115.     }
  116.  
  117.     /**
  118.      * Filters the information provided in the setPixels method of the
  119.      * ImageConsumer interface which takes an array of integers.
  120.      * @see ImageConsumer#setPixels
  121.      */
  122.     public void setPixels(int x, int y, int w, int h,
  123.               ColorModel model, int pixels[], int off,
  124.               int scansize) {
  125.     consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  126.     }
  127.  
  128.     /**
  129.      * Filters the information provided in the imageComplete method of
  130.      * the ImageConsumer interface.
  131.      * @see ImageConsumer#imageComplete
  132.      */
  133.     public void imageComplete(int status) {
  134.     consumer.imageComplete(status);
  135.     }
  136.  
  137.     /**
  138.      * Responds to a request for a TopDownLeftRight (TDLR) ordered resend
  139.      * of the pixel data from an ImageConsumer.
  140.      * The ImageFilter can respond to this request in one of three ways.
  141.      * <ol>
  142.      * <li>If the filter can determine that it will forward the pixels in
  143.      * TDLR order if its upstream producer object sends them
  144.      * in TDLR order, then the request is automatically forwarded by
  145.      * default to the indicated ImageProducer using this filter as the
  146.      * requesting ImageConsumer, so no override is necessary.
  147.      * <li>If the filter can resend the pixels in the right order on its
  148.      * own (presumably because the generated pixels have been saved in
  149.      * some sort of buffer), then it can override this method and
  150.      * simply resend the pixels in TDLR order as specified in the
  151.      * ImageProducer API.  <li>If the filter simply returns from this
  152.      * method then the request will be ignored and no resend will
  153.      * occur.  </ol> 
  154.      * @see ImageProducer#requestTopDownLeftRightResend
  155.      * @param ip The ImageProducer that is feeding this instance of
  156.      * the filter - also the ImageProducer that the request should be
  157.      * forwarded to if necessary.
  158.      */
  159.     public void resendTopDownLeftRight(ImageProducer ip) {
  160.     ip.requestTopDownLeftRightResend(this);
  161.     }
  162.     
  163.     /**
  164.      * Clones this object.
  165.      */
  166.     public Object clone() { 
  167.     try { 
  168.         return super.clone();
  169.     } catch (CloneNotSupportedException e) { 
  170.         // this shouldn't happen, since we are Cloneable
  171.         throw new InternalError();
  172.     }
  173.     }
  174. }
  175.