1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dishevelled.iconbundle.impl;
25
26 import java.awt.Color;
27 import java.awt.Image;
28
29 import java.awt.image.Raster;
30 import java.awt.image.RasterOp;
31 import java.awt.image.RescaleOp;
32 import java.awt.image.BufferedImage;
33 import java.awt.image.WritableRaster;
34
35 import java.net.URL;
36
37 import org.apache.batik.transcoder.TranscoderInput;
38 import org.apache.batik.transcoder.TranscoderOutput;
39 import org.apache.batik.transcoder.TranscoderException;
40
41 import org.apache.batik.transcoder.image.ImageTranscoder;
42
43 /***
44 * IconBundle static utility class.
45 *
46 * @author Michael Heuer
47 * @version $Revision: 404 $ $Date: 2008-01-02 13:19:59 -0600 (Wed, 02 Jan 2008) $
48 */
49 public final class IconBundleUtils
50 {
51 /*** Intensity factor for red. */
52 private static final float RED_FACTOR = 0.30f;
53
54 /*** Intensity factor for green. */
55 private static final float GREEN_FACTOR = 0.59f;
56
57 /*** Intensity factor for blue. */
58 private static final float BLUE_FACTOR = 0.11f;
59
60 /*** Pixel array size. */
61 private static final int PIXEL_ARRAY_SIZE = 4;
62
63 /*** Medium grey color component. */
64 private static final int MEDIUM_GREY = 127;
65
66 /*** Disabled pattern factor. */
67 private static final float DISABLED_PATTERN_FACTOR = 0.7f;
68
69 /***
70 * RescaleOp for saturating active images.
71 */
72 private static final RescaleOp ACTIVE_OP = new RescaleOp(new float[] { 0.8f, 0.8f, 0.8f, 1.0f },
73 new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
74 null);
75
76 /***
77 * RescaleOp for saturating mouseover images.
78 */
79 private static final RescaleOp MOUSEOVER_OP = new RescaleOp(new float[] { 1.2f, 1.2f, 1.2f, 1.0f },
80 new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
81 null);
82
83 /***
84 * RescaleOp for creating translucent dragging images.
85 */
86 private static final RescaleOp DRAGGING_OP = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, 0.2f },
87 new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
88 null);
89
90 /***
91 * RescaleOp for desaturating disabled images.
92 */
93 private static final RescaleOp DISABLED_SATURATION_OP = new RescaleOp(new float[] { 0.8f, 0.8f, 0.8f, 1.0f },
94 new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
95 null);
96
97 /***
98 * RasterOp for creating the pattern overlay for disabled images.
99 */
100 private static final RasterOp DISABLED_PATTERN_OP = new AbstractRasterOp()
101 {
102
103 /*** {@inheritDoc} */
104 public WritableRaster filter(final Raster src, final WritableRaster dest)
105 {
106 float intensity;
107 float[] pixel = new float[PIXEL_ARRAY_SIZE];
108
109 for (int x = 0, w = src.getWidth(); x < w; x++)
110 {
111 for (int y = 0, h = src.getHeight(); y < h; y++)
112 {
113 pixel = src.getPixel(x, y, pixel);
114 intensity = calculateIntensity(pixel[0], pixel[1], pixel[2]);
115
116 if (((x + y) % 2) == 0)
117 {
118 pixel[0] = (intensity / 2) + MEDIUM_GREY;
119 pixel[1] = (intensity / 2) + MEDIUM_GREY;
120 pixel[2] = (intensity / 2) + MEDIUM_GREY;
121 }
122 else
123 {
124 pixel[0] *= DISABLED_PATTERN_FACTOR;
125 pixel[1] *= DISABLED_PATTERN_FACTOR;
126 pixel[2] *= DISABLED_PATTERN_FACTOR;
127 }
128 dest.setPixel(x, y, pixel);
129 }
130 }
131 return dest;
132 }
133 };
134
135
136 /***
137 * Private constructor.
138 */
139 private IconBundleUtils()
140 {
141
142 }
143
144
145 /***
146 * Calculate a measure of intensity for the specified RGB values.
147 *
148 * @param r red value
149 * @param g green value
150 * @param b blue value
151 * @return a measure of intensity for the specified RGB values
152 */
153 private static float calculateIntensity(final float r, final float g, final float b)
154 {
155 return (float) (r * RED_FACTOR + g * GREEN_FACTOR + b * BLUE_FACTOR);
156 }
157
158 /***
159 * Read the specified SVG URL and render it to a BufferedImage
160 * of the specified width and height.
161 *
162 * @param url url
163 * @param width width
164 * @param height height
165 * @return the specified SVG URL rendered to a BufferedImage of the
166 * specified width and height
167 */
168 public static BufferedImage readSVG(final URL url, final int width, final int height)
169 {
170 BufferedImageTranscoder transcoder = new BufferedImageTranscoder();
171 transcoder.addTranscodingHint(ImageTranscoder.KEY_WIDTH, new Float(width));
172 transcoder.addTranscodingHint(ImageTranscoder.KEY_HEIGHT, new Float(height));
173
174 try
175 {
176 transcoder.transcode(new TranscoderInput(url.toString()), new TranscoderOutput());
177 }
178 catch (TranscoderException e)
179 {
180
181 }
182
183 BufferedImage image = transcoder.getImage();
184 transcoder = null;
185
186 return image;
187 }
188
189 /***
190 * Make the specified source image active.
191 *
192 * @param src source image
193 * @return filtered source image
194 */
195 public static Image makeActive(final BufferedImage src)
196 {
197 BufferedImage dest = new BufferedImage(src.getColorModel(),
198 src.copyData(null),
199 src.isAlphaPremultiplied(),
200 null);
201 ACTIVE_OP.filter(src, dest);
202 return dest;
203 }
204
205 /***
206 * Make the specified source image disabled.
207 *
208 * @param src source image
209 * @return filtered source image
210 */
211 public static Image makeDisabled(final BufferedImage src)
212 {
213 BufferedImage dest = new BufferedImage(src.getColorModel(),
214 src.copyData(null),
215 src.isAlphaPremultiplied(),
216 null);
217 DISABLED_SATURATION_OP.filter(src, dest);
218 DISABLED_PATTERN_OP.filter(src.getRaster(), dest.getRaster());
219 return dest;
220 }
221
222 /***
223 * Make the specified source image dragging.
224 *
225 * @param src source image
226 * @return filtered source image
227 */
228 public static Image makeDragging(final BufferedImage src)
229 {
230 BufferedImage dest = new BufferedImage(src.getColorModel(),
231 src.copyData(null),
232 src.isAlphaPremultiplied(),
233 null);
234 DRAGGING_OP.filter(src, dest);
235 return dest;
236 }
237
238 /***
239 * Make the specified source image mouseover.
240 *
241 * @param src source image
242 * @return filtered source image
243 */
244 public static Image makeMouseover(final BufferedImage src)
245 {
246 BufferedImage dest = new BufferedImage(src.getColorModel(),
247 src.copyData(null),
248 src.isAlphaPremultiplied(),
249 null);
250 MOUSEOVER_OP.filter(src, dest);
251 return dest;
252 }
253
254 /***
255 * Make the specified source image selected.
256 *
257 * @param src source image
258 * @param selectionColor selection color
259 * @return filtered source image
260 */
261 public static Image makeSelected(final BufferedImage src, final Color selectionColor)
262 {
263 BufferedImage dest = new BufferedImage(src.getColorModel(),
264 src.copyData(null),
265 src.isAlphaPremultiplied(),
266 null);
267 RasterOp selectionOp = new SelectionRasterOp(selectionColor);
268 selectionOp.filter(src.getRaster(), dest.getRaster());
269 selectionOp = null;
270 return dest;
271 }
272 }