KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > image > FilteredImageSource


1 /*
2  * @(#)FilteredImageSource.java 1.28 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.image;
9
10 import java.awt.Image JavaDoc;
11 import java.awt.image.ImageFilter JavaDoc;
12 import java.awt.image.ImageConsumer JavaDoc;
13 import java.awt.image.ImageProducer JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.awt.image.ColorModel JavaDoc;
16
17 /**
18  * This class is an implementation of the ImageProducer interface which
19  * takes an existing image and a filter object and uses them to produce
20  * image data for a new filtered version of the original image.
21  * Here is an example which filters an image by swapping the red and
22  * blue compents:
23  * <pre>
24  *
25  * Image src = getImage("doc:///demo/images/duke/T1.gif");
26  * ImageFilter colorfilter = new RedBlueSwapFilter();
27  * Image img = createImage(new FilteredImageSource(src.getSource(),
28  * colorfilter));
29  *
30  * </pre>
31  *
32  * @see ImageProducer
33  *
34  * @version 1.28 12/19/03
35  * @author Jim Graham
36  */

37 public class FilteredImageSource implements ImageProducer JavaDoc {
38     ImageProducer JavaDoc src;
39     ImageFilter JavaDoc filter;
40
41     /**
42      * Constructs an ImageProducer object from an existing ImageProducer
43      * and a filter object.
44      * @param orig the specified <code>ImageProducer</code>
45      * @param imgf the specified <code>ImageFilter</code>
46      * @see ImageFilter
47      * @see java.awt.Component#createImage
48      */

49     public FilteredImageSource(ImageProducer JavaDoc orig, ImageFilter JavaDoc imgf) {
50     src = orig;
51     filter = imgf;
52     }
53
54     private Hashtable JavaDoc proxies;
55
56     /**
57      * Adds the specified <code>ImageConsumer</code>
58      * to the list of consumers interested in data for the filtered image.
59      * An instance of the original <code>ImageFilter</code>
60      * is created
61      * (using the filter's <code>getFilterInstance</code> method)
62      * to manipulate the image data
63      * for the specified <code>ImageConsumer</code>.
64      * The newly created filter instance
65      * is then passed to the <code>addConsumer</code> method
66      * of the original <code>ImageProducer</code>.
67      *
68      * <p>
69      * This method is public as a side effect
70      * of this class implementing
71      * the <code>ImageProducer</code> interface.
72      * It should not be called from user code,
73      * and its behavior if called from user code is unspecified.
74      *
75      * @param ic the consumer for the filtered image
76      * @see ImageConsumer
77      */

78     public synchronized void addConsumer(ImageConsumer JavaDoc ic) {
79     if (proxies == null) {
80         proxies = new Hashtable JavaDoc();
81     }
82     if (!proxies.containsKey(ic)) {
83         ImageFilter JavaDoc imgf = filter.getFilterInstance(ic);
84         proxies.put(ic, imgf);
85         src.addConsumer(imgf);
86     }
87     }
88
89     /**
90      * Determines whether an ImageConsumer is on the list of consumers
91      * currently interested in data for this image.
92      *
93      * <p>
94      * This method is public as a side effect
95      * of this class implementing
96      * the <code>ImageProducer</code> interface.
97      * It should not be called from user code,
98      * and its behavior if called from user code is unspecified.
99      *
100      * @param ic the specified <code>ImageConsumer</code>
101      * @return true if the ImageConsumer is on the list; false otherwise
102      * @see ImageConsumer
103      */

104     public synchronized boolean isConsumer(ImageConsumer JavaDoc ic) {
105     return (proxies != null && proxies.containsKey(ic));
106     }
107
108     /**
109      * Removes an ImageConsumer from the list of consumers interested in
110      * data for this image.
111      *
112      * <p>
113      * This method is public as a side effect
114      * of this class implementing
115      * the <code>ImageProducer</code> interface.
116      * It should not be called from user code,
117      * and its behavior if called from user code is unspecified.
118      *
119      * @see ImageConsumer
120      */

121     public synchronized void removeConsumer(ImageConsumer JavaDoc ic) {
122     if (proxies != null) {
123         ImageFilter JavaDoc imgf = (ImageFilter JavaDoc) proxies.get(ic);
124         if (imgf != null) {
125         src.removeConsumer(imgf);
126         proxies.remove(ic);
127         if (proxies.isEmpty()) {
128             proxies = null;
129         }
130         }
131     }
132     }
133
134     /**
135      * Starts production of the filtered image.
136      * If the specified <code>ImageConsumer</code>
137      * isn't already a consumer of the filtered image,
138      * an instance of the original <code>ImageFilter</code>
139      * is created
140      * (using the filter's <code>getFilterInstance</code> method)
141      * to manipulate the image data
142      * for the <code>ImageConsumer</code>.
143      * The filter instance for the <code>ImageConsumer</code>
144      * is then passed to the <code>startProduction</code> method
145      * of the original <code>ImageProducer</code>.
146      *
147      * <p>
148      * This method is public as a side effect
149      * of this class implementing
150      * the <code>ImageProducer</code> interface.
151      * It should not be called from user code,
152      * and its behavior if called from user code is unspecified.
153      *
154      * @param ic the consumer for the filtered image
155      * @see ImageConsumer
156      */

157     public void startProduction(ImageConsumer JavaDoc ic) {
158     if (proxies == null) {
159         proxies = new Hashtable JavaDoc();
160     }
161     ImageFilter JavaDoc imgf = (ImageFilter JavaDoc) proxies.get(ic);
162     if (imgf == null) {
163         imgf = filter.getFilterInstance(ic);
164         proxies.put(ic, imgf);
165     }
166     src.startProduction(imgf);
167     }
168
169     /**
170      * Requests that a given ImageConsumer have the image data delivered
171      * one more time in top-down, left-right order. The request is
172      * handed to the ImageFilter for further processing, since the
173      * ability to preserve the pixel ordering depends on the filter.
174      *
175      * <p>
176      * This method is public as a side effect
177      * of this class implementing
178      * the <code>ImageProducer</code> interface.
179      * It should not be called from user code,
180      * and its behavior if called from user code is unspecified.
181      *
182      * @see ImageConsumer
183      */

184     public void requestTopDownLeftRightResend(ImageConsumer JavaDoc ic) {
185     if (proxies != null) {
186         ImageFilter JavaDoc imgf = (ImageFilter JavaDoc) proxies.get(ic);
187         if (imgf != null) {
188         imgf.resendTopDownLeftRight(src);
189         }
190     }
191     }
192 }
193
Popular Tags