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

  1. /*
  2.  * @(#)FilteredImageSource.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.awt.Image;
  26. import java.awt.image.ImageFilter;
  27. import java.awt.image.ImageConsumer;
  28. import java.awt.image.ImageProducer;
  29. import java.util.Hashtable;
  30. import java.awt.image.ColorModel;
  31.  
  32. /**
  33.  * This class is an implementation of the ImageProducer interface which
  34.  * takes an existing image and a filter object and uses them to produce
  35.  * image data for a new filtered version of the original image.
  36.  * Here is an example which filters an image by swapping the red and
  37.  * blue compents:
  38.  * <pre>
  39.  * 
  40.  *    Image src = getImage("doc:///demo/images/duke/T1.gif");
  41.  *    ImageFilter colorfilter = new RedBlueSwapFilter();
  42.  *    Image img = createImage(new FilteredImageSource(src.getSource(),
  43.  *                            colorfilter));
  44.  * 
  45.  * </pre>
  46.  *
  47.  * @see ImageProducer
  48.  *
  49.  * @version    1.17 11/23/96
  50.  * @author     Jim Graham
  51.  */
  52. public class FilteredImageSource implements ImageProducer {
  53.     ImageProducer src;
  54.     ImageFilter filter;
  55.  
  56.     /**
  57.      * Constructs an ImageProducer object from an existing ImageProducer
  58.      * and a filter object.
  59.      * @see ImageFilter
  60.      * @see java.awt.Component#createImage
  61.      */
  62.     public FilteredImageSource(ImageProducer orig, ImageFilter imgf) {
  63.     src = orig;
  64.     filter = imgf;
  65.     }
  66.  
  67.     private Hashtable proxies;
  68.  
  69.     /**
  70.      * Adds an ImageConsumer to the list of consumers interested in
  71.      * data for this image.
  72.      * @see ImageConsumer
  73.      */
  74.     public synchronized void addConsumer(ImageConsumer ic) {
  75.     if (proxies == null) {
  76.         proxies = new Hashtable();
  77.     }
  78.     if (!proxies.containsKey(ic)) {
  79.         ImageFilter imgf = filter.getFilterInstance(ic);
  80.         proxies.put(ic, imgf);
  81.         src.addConsumer(imgf);
  82.     }
  83.     }
  84.  
  85.     /**
  86.      * Determines whether an ImageConsumer is on the list of consumers 
  87.      * currently interested in data for this image.
  88.      * @return true if the ImageConsumer is on the list; false otherwise
  89.      * @see ImageConsumer
  90.      */
  91.     public synchronized boolean isConsumer(ImageConsumer ic) {
  92.     return (proxies != null && proxies.containsKey(ic));
  93.     }
  94.  
  95.     /**
  96.      * Removes an ImageConsumer from the list of consumers interested in
  97.      * data for this image.
  98.      * @see ImageConsumer
  99.      */
  100.     public synchronized void removeConsumer(ImageConsumer ic) {
  101.     if (proxies != null) {
  102.         ImageFilter imgf = (ImageFilter) proxies.get(ic);
  103.         if (imgf != null) {
  104.         src.removeConsumer(imgf);
  105.         proxies.remove(ic);
  106.         if (proxies.isEmpty()) {
  107.             proxies = null;
  108.         }
  109.         }
  110.     }
  111.     }
  112.  
  113.     /**
  114.      * Adds an ImageConsumer to the list of consumers interested in
  115.      * data for this image, and immediately starts delivery of the
  116.      * image data through the ImageConsumer interface.
  117.      * @see ImageConsumer
  118.      */
  119.     public void startProduction(ImageConsumer ic) {
  120.     if (proxies == null) {
  121.         proxies = new Hashtable();
  122.     }
  123.     ImageFilter imgf = (ImageFilter) proxies.get(ic);
  124.     if (imgf == null) {
  125.         imgf = filter.getFilterInstance(ic);
  126.         proxies.put(ic, imgf);
  127.     }
  128.     src.startProduction(imgf);
  129.     }
  130.  
  131.     /**
  132.      * Requests that a given ImageConsumer have the image data delivered
  133.      * one more time in top-down, left-right order.  The request is
  134.      * handed to the ImageFilter for further processing, since the
  135.      * ability to preserve the pixel ordering depends on the filter.
  136.      * @see ImageConsumer
  137.      */
  138.     public void requestTopDownLeftRightResend(ImageConsumer ic) {
  139.     if (proxies != null) {
  140.         ImageFilter imgf = (ImageFilter) proxies.get(ic);
  141.         if (imgf != null) {
  142.         imgf.resendTopDownLeftRight(src);
  143.         }
  144.     }
  145.     }
  146. }
  147.