author | tvaleev |
Thu, 04 Oct 2018 12:40:55 -0700 | |
changeset 52248 | 2e330da7cbf4 |
parent 48289 | 054fecf0c1d2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48289
054fecf0c1d2
8188083: NullPointerExcpn-java.awt.image.FilteredImageSource.startProduction JDK-8079607
pnarayanan
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.awt.image; |
|
27 |
||
28 |
import java.awt.Image; |
|
29 |
import java.awt.image.ImageFilter; |
|
30 |
import java.awt.image.ImageConsumer; |
|
31 |
import java.awt.image.ImageProducer; |
|
32 |
import java.util.Hashtable; |
|
33 |
import java.awt.image.ColorModel; |
|
34 |
||
35 |
/** |
|
36 |
* This class is an implementation of the ImageProducer interface which |
|
37 |
* takes an existing image and a filter object and uses them to produce |
|
48289
054fecf0c1d2
8188083: NullPointerExcpn-java.awt.image.FilteredImageSource.startProduction JDK-8079607
pnarayanan
parents:
47216
diff
changeset
|
38 |
* image data for a new filtered version of the original image. Furthermore, |
054fecf0c1d2
8188083: NullPointerExcpn-java.awt.image.FilteredImageSource.startProduction JDK-8079607
pnarayanan
parents:
47216
diff
changeset
|
39 |
* {@code FilteredImageSource} is safe for use by multiple threads. |
2 | 40 |
* Here is an example which filters an image by swapping the red and |
26749 | 41 |
* blue components: |
2 | 42 |
* <pre> |
43 |
* |
|
44 |
* Image src = getImage("doc:///demo/images/duke/T1.gif"); |
|
45 |
* ImageFilter colorfilter = new RedBlueSwapFilter(); |
|
46 |
* Image img = createImage(new FilteredImageSource(src.getSource(), |
|
47 |
* colorfilter)); |
|
48 |
* |
|
49 |
* </pre> |
|
50 |
* |
|
51 |
* @see ImageProducer |
|
52 |
* |
|
53 |
* @author Jim Graham |
|
54 |
*/ |
|
55 |
public class FilteredImageSource implements ImageProducer { |
|
56 |
ImageProducer src; |
|
57 |
ImageFilter filter; |
|
58 |
||
59 |
/** |
|
60 |
* Constructs an ImageProducer object from an existing ImageProducer |
|
61 |
* and a filter object. |
|
35667 | 62 |
* @param orig the specified {@code ImageProducer} |
63 |
* @param imgf the specified {@code ImageFilter} |
|
2 | 64 |
* @see ImageFilter |
65 |
* @see java.awt.Component#createImage |
|
66 |
*/ |
|
67 |
public FilteredImageSource(ImageProducer orig, ImageFilter imgf) { |
|
68 |
src = orig; |
|
69 |
filter = imgf; |
|
70 |
} |
|
71 |
||
24549
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
5506
diff
changeset
|
72 |
private Hashtable<ImageConsumer, ImageFilter> proxies; |
2 | 73 |
|
74 |
/** |
|
35667 | 75 |
* Adds the specified {@code ImageConsumer} |
2 | 76 |
* to the list of consumers interested in data for the filtered image. |
35667 | 77 |
* An instance of the original {@code ImageFilter} |
2 | 78 |
* is created |
35667 | 79 |
* (using the filter's {@code getFilterInstance} method) |
2 | 80 |
* to manipulate the image data |
35667 | 81 |
* for the specified {@code ImageConsumer}. |
2 | 82 |
* The newly created filter instance |
35667 | 83 |
* is then passed to the {@code addConsumer} method |
84 |
* of the original {@code ImageProducer}. |
|
2 | 85 |
* |
86 |
* <p> |
|
87 |
* This method is public as a side effect |
|
88 |
* of this class implementing |
|
35667 | 89 |
* the {@code ImageProducer} interface. |
2 | 90 |
* It should not be called from user code, |
91 |
* and its behavior if called from user code is unspecified. |
|
92 |
* |
|
93 |
* @param ic the consumer for the filtered image |
|
94 |
* @see ImageConsumer |
|
95 |
*/ |
|
96 |
public synchronized void addConsumer(ImageConsumer ic) { |
|
97 |
if (proxies == null) { |
|
24549
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
5506
diff
changeset
|
98 |
proxies = new Hashtable<>(); |
2 | 99 |
} |
100 |
if (!proxies.containsKey(ic)) { |
|
101 |
ImageFilter imgf = filter.getFilterInstance(ic); |
|
102 |
proxies.put(ic, imgf); |
|
103 |
src.addConsumer(imgf); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Determines whether an ImageConsumer is on the list of consumers |
|
109 |
* currently interested in data for this image. |
|
110 |
* |
|
111 |
* <p> |
|
112 |
* This method is public as a side effect |
|
113 |
* of this class implementing |
|
35667 | 114 |
* the {@code ImageProducer} interface. |
2 | 115 |
* It should not be called from user code, |
116 |
* and its behavior if called from user code is unspecified. |
|
117 |
* |
|
35667 | 118 |
* @param ic the specified {@code ImageConsumer} |
2 | 119 |
* @return true if the ImageConsumer is on the list; false otherwise |
120 |
* @see ImageConsumer |
|
121 |
*/ |
|
122 |
public synchronized boolean isConsumer(ImageConsumer ic) { |
|
123 |
return (proxies != null && proxies.containsKey(ic)); |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Removes an ImageConsumer from the list of consumers interested in |
|
128 |
* data for this image. |
|
129 |
* |
|
130 |
* <p> |
|
131 |
* This method is public as a side effect |
|
132 |
* of this class implementing |
|
35667 | 133 |
* the {@code ImageProducer} interface. |
2 | 134 |
* It should not be called from user code, |
135 |
* and its behavior if called from user code is unspecified. |
|
136 |
* |
|
137 |
* @see ImageConsumer |
|
138 |
*/ |
|
139 |
public synchronized void removeConsumer(ImageConsumer ic) { |
|
140 |
if (proxies != null) { |
|
24549
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
5506
diff
changeset
|
141 |
ImageFilter imgf = proxies.get(ic); |
2 | 142 |
if (imgf != null) { |
143 |
src.removeConsumer(imgf); |
|
144 |
proxies.remove(ic); |
|
145 |
if (proxies.isEmpty()) { |
|
146 |
proxies = null; |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Starts production of the filtered image. |
|
35667 | 154 |
* If the specified {@code ImageConsumer} |
2 | 155 |
* isn't already a consumer of the filtered image, |
35667 | 156 |
* an instance of the original {@code ImageFilter} |
2 | 157 |
* is created |
35667 | 158 |
* (using the filter's {@code getFilterInstance} method) |
2 | 159 |
* to manipulate the image data |
35667 | 160 |
* for the {@code ImageConsumer}. |
161 |
* The filter instance for the {@code ImageConsumer} |
|
162 |
* is then passed to the {@code startProduction} method |
|
163 |
* of the original {@code ImageProducer}. |
|
2 | 164 |
* |
165 |
* <p> |
|
166 |
* This method is public as a side effect |
|
167 |
* of this class implementing |
|
35667 | 168 |
* the {@code ImageProducer} interface. |
2 | 169 |
* It should not be called from user code, |
170 |
* and its behavior if called from user code is unspecified. |
|
171 |
* |
|
172 |
* @param ic the consumer for the filtered image |
|
173 |
* @see ImageConsumer |
|
174 |
*/ |
|
48289
054fecf0c1d2
8188083: NullPointerExcpn-java.awt.image.FilteredImageSource.startProduction JDK-8079607
pnarayanan
parents:
47216
diff
changeset
|
175 |
public synchronized void startProduction(ImageConsumer ic) { |
2 | 176 |
if (proxies == null) { |
24549
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
5506
diff
changeset
|
177 |
proxies = new Hashtable<>(); |
2 | 178 |
} |
24549
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
5506
diff
changeset
|
179 |
ImageFilter imgf = proxies.get(ic); |
2 | 180 |
if (imgf == null) { |
181 |
imgf = filter.getFilterInstance(ic); |
|
182 |
proxies.put(ic, imgf); |
|
183 |
} |
|
184 |
src.startProduction(imgf); |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Requests that a given ImageConsumer have the image data delivered |
|
189 |
* one more time in top-down, left-right order. The request is |
|
190 |
* handed to the ImageFilter for further processing, since the |
|
191 |
* ability to preserve the pixel ordering depends on the filter. |
|
192 |
* |
|
193 |
* <p> |
|
194 |
* This method is public as a side effect |
|
195 |
* of this class implementing |
|
35667 | 196 |
* the {@code ImageProducer} interface. |
2 | 197 |
* It should not be called from user code, |
198 |
* and its behavior if called from user code is unspecified. |
|
199 |
* |
|
200 |
* @see ImageConsumer |
|
201 |
*/ |
|
48289
054fecf0c1d2
8188083: NullPointerExcpn-java.awt.image.FilteredImageSource.startProduction JDK-8079607
pnarayanan
parents:
47216
diff
changeset
|
202 |
public synchronized void requestTopDownLeftRightResend(ImageConsumer ic) { |
2 | 203 |
if (proxies != null) { |
24549
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
5506
diff
changeset
|
204 |
ImageFilter imgf = proxies.get(ic); |
2 | 205 |
if (imgf != null) { |
206 |
imgf.resendTopDownLeftRight(src); |
|
207 |
} |
|
208 |
} |
|
209 |
} |
|
210 |
} |