home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / ImageFilter.java < prev    next >
Text File  |  1997-05-20  |  6KB  |  178 lines

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