KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ImageFilter.java 1.31 04/07/16
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.util.Hashtable JavaDoc;
11
12 /**
13  * This class implements a filter for the set of interface methods that
14  * are used to deliver data from an ImageProducer to an ImageConsumer.
15  * It is meant to be used in conjunction with a FilteredImageSource
16  * object to produce filtered versions of existing images. It is a
17  * base class that provides the calls needed to implement a "Null filter"
18  * which has no effect on the data being passed through. Filters should
19  * subclass this class and override the methods which deal with the
20  * data that needs to be filtered and modify it as necessary.
21  *
22  * @see FilteredImageSource
23  * @see ImageConsumer
24  *
25  * @version 1.31 07/16/04
26  * @author Jim Graham
27  */

28 public class ImageFilter implements ImageConsumer JavaDoc, Cloneable JavaDoc {
29     /**
30      * The consumer of the particular image data stream for which this
31      * instance of the ImageFilter is filtering data. It is not
32      * initialized during the constructor, but rather during the
33      * getFilterInstance() method call when the FilteredImageSource
34      * is creating a unique instance of this object for a particular
35      * image data stream.
36      * @see #getFilterInstance
37      * @see ImageConsumer
38      */

39     protected ImageConsumer JavaDoc consumer;
40
41     /**
42      * Returns a unique instance of an ImageFilter object which will
43      * actually perform the filtering for the specified ImageConsumer.
44      * The default implementation just clones this object.
45      * <p>
46      * Note: This method is intended to be called by the ImageProducer
47      * of the Image whose pixels are being filtered. Developers using
48      * this class to filter pixels from an image should avoid calling
49      * this method directly since that operation could interfere
50      * with the filtering operation.
51      * @param ic the specified <code>ImageConsumer</code>
52      * @return an <code>ImageFilter</code> used to perform the
53      * filtering for the specified <code>ImageConsumer</code>.
54      */

55     public ImageFilter JavaDoc getFilterInstance(ImageConsumer JavaDoc ic) {
56     ImageFilter JavaDoc instance = (ImageFilter JavaDoc) clone();
57     instance.consumer = ic;
58     return instance;
59     }
60
61     /**
62      * Filters the information provided in the setDimensions method
63      * of the ImageConsumer interface.
64      * <p>
65      * Note: This method is intended to be called by the ImageProducer
66      * of the Image whose pixels are being filtered. Developers using
67      * this class to filter pixels from an image should avoid calling
68      * this method directly since that operation could interfere
69      * with the filtering operation.
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      * <p>
80      * Note: This method is intended to be called by the ImageProducer
81      * of the Image whose pixels are being filtered. Developers using
82      * this class to filter pixels from an image should avoid calling
83      * this method directly since that operation could interfere
84      * with the filtering operation.
85      *
86      * @param props the properties from the source object
87      * @exception NullPointerException if <code>props</code> is null
88      */

89     public void setProperties(Hashtable JavaDoc<?,?> props) {
90     Hashtable JavaDoc<Object JavaDoc,Object JavaDoc> p = (Hashtable JavaDoc<Object JavaDoc,Object JavaDoc>)props.clone();
91     Object JavaDoc o = p.get("filters");
92     if (o == null) {
93         p.put("filters", toString());
94     } else if (o instanceof String JavaDoc) {
95         p.put("filters", ((String JavaDoc) o)+toString());
96     }
97     consumer.setProperties(p);
98     }
99
100     /**
101      * Filter the information provided in the setColorModel method
102      * of the ImageConsumer interface.
103      * <p>
104      * Note: This method is intended to be called by the ImageProducer
105      * of the Image whose pixels are being filtered. Developers using
106      * this class to filter pixels from an image should avoid calling
107      * this method directly since that operation could interfere
108      * with the filtering operation.
109      * @see ImageConsumer#setColorModel
110      */

111     public void setColorModel(ColorModel JavaDoc model) {
112     consumer.setColorModel(model);
113     }
114
115     /**
116      * Filters the information provided in the setHints method
117      * of the ImageConsumer interface.
118      * <p>
119      * Note: This method is intended to be called by the ImageProducer
120      * of the Image whose pixels are being filtered. Developers using
121      * this class to filter pixels from an image should avoid calling
122      * this method directly since that operation could interfere
123      * with the filtering operation.
124      * @see ImageConsumer#setHints
125      */

126     public void setHints(int hints) {
127     consumer.setHints(hints);
128     }
129
130     /**
131      * Filters the information provided in the setPixels method of the
132      * ImageConsumer interface which takes an array of bytes.
133      * <p>
134      * Note: This method is intended to be called by the ImageProducer
135      * of the Image whose pixels are being filtered. Developers using
136      * this class to filter pixels from an image should avoid calling
137      * this method directly since that operation could interfere
138      * with the filtering operation.
139      * @see ImageConsumer#setPixels
140      */

141     public void setPixels(int x, int y, int w, int h,
142               ColorModel JavaDoc model, byte pixels[], int off,
143               int scansize) {
144     consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
145     }
146
147     /**
148      * Filters the information provided in the setPixels method of the
149      * ImageConsumer interface which takes an array of integers.
150      * <p>
151      * Note: This method is intended to be called by the ImageProducer
152      * of the Image whose pixels are being filtered. Developers using
153      * this class to filter pixels from an image should avoid calling
154      * this method directly since that operation could interfere
155      * with the filtering operation.
156      * @see ImageConsumer#setPixels
157      */

158     public void setPixels(int x, int y, int w, int h,
159               ColorModel JavaDoc model, int pixels[], int off,
160               int scansize) {
161     consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
162     }
163
164     /**
165      * Filters the information provided in the imageComplete method of
166      * the ImageConsumer interface.
167      * <p>
168      * Note: This method is intended to be called by the ImageProducer
169      * of the Image whose pixels are being filtered. Developers using
170      * this class to filter pixels from an image should avoid calling
171      * this method directly since that operation could interfere
172      * with the filtering operation.
173      * @see ImageConsumer#imageComplete
174      */

175     public void imageComplete(int status) {
176     consumer.imageComplete(status);
177     }
178
179     /**
180      * Responds to a request for a TopDownLeftRight (TDLR) ordered resend
181      * of the pixel data from an <code>ImageConsumer</code>.
182      * When an <code>ImageConsumer</code> being fed
183      * by an instance of this <code>ImageFilter</code>
184      * requests a resend of the data in TDLR order,
185      * the <code>FilteredImageSource</code>
186      * invokes this method of the <code>ImageFilter</code>.
187      *
188      * <p>
189      *
190      * An <code>ImageFilter</code> subclass might override this method or not,
191      * depending on if and how it can send data in TDLR order.
192      * Three possibilities exist:
193      *
194      * <ul>
195      * <li>
196      * Do not override this method.
197      * This makes the subclass use the default implementation,
198      * which is to
199      * forward the request
200      * to the indicated <code>ImageProducer</code>
201      * using this filter as the requesting <code>ImageConsumer</code>.
202      * This behavior
203      * is appropriate if the filter can determine
204      * that it will forward the pixels
205      * in TDLR order if its upstream producer object
206      * sends them in TDLR order.
207      *
208      * <li>
209      * Override the method to simply send the data.
210      * This is appropriate if the filter can handle the request itself &#151;
211      * for example,
212      * if the generated pixels have been saved in some sort of buffer.
213      *
214      * <li>
215      * Override the method to do nothing.
216      * This is appropriate
217      * if the filter cannot produce filtered data in TDLR order.
218      * </ul>
219      *
220      * @see ImageProducer#requestTopDownLeftRightResend
221      * @param ip the ImageProducer that is feeding this instance of
222      * the filter - also the ImageProducer that the request should be
223      * forwarded to if necessary
224      * @exception NullPointerException if <code>ip</code> is null
225      */

226     public void resendTopDownLeftRight(ImageProducer JavaDoc ip) {
227     ip.requestTopDownLeftRightResend(this);
228     }
229     
230     /**
231      * Clones this object.
232      */

233     public Object JavaDoc clone() {
234     try {
235         return super.clone();
236     } catch (CloneNotSupportedException JavaDoc e) {
237         // this shouldn't happen, since we are Cloneable
238
throw new InternalError JavaDoc();
239     }
240     }
241 }
242
Popular Tags