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

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