KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > nimbus > EffectUtils


1 /*
2  * @(#)EffectUtils.java 1.2 07/12/12
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.nimbus;
8
9 import java.awt.AlphaComposite JavaDoc;
10 import java.awt.Graphics2D JavaDoc;
11 import java.awt.Transparency JavaDoc;
12 import java.awt.GraphicsConfiguration JavaDoc;
13 import java.awt.GraphicsEnvironment JavaDoc;
14 import java.awt.image.BufferedImage JavaDoc;
15 import java.awt.image.Raster JavaDoc;
16 import java.awt.image.WritableRaster JavaDoc;
17 import java.awt.image.ColorModel JavaDoc;
18
19 /**
20  * EffectUtils
21  *
22  * @author Created by Jasper Potts (Jun 18, 2007)
23  * @version 1.0
24  */

25 class EffectUtils {
26
27     /**
28      * Clear a transparent image to 100% transparent
29      *
30      * @param img The image to clear
31      */

32     static void clearImage(BufferedImage JavaDoc img) {
33         Graphics2D JavaDoc g2 = img.createGraphics();
34         g2.setComposite(AlphaComposite.Clear);
35         g2.fillRect(0, 0, img.getWidth(), img.getHeight());
36         g2.dispose();
37     }
38
39     // =================================================================================================================
40
// Blur
41

42     /**
43      * Apply Gaussian Blur to Image
44      *
45      * @param src The image tp
46      * @param dst The destination image to draw blured src image into, null if you want a new one created
47      * @param radius The blur kernel radius
48      * @return The blured image
49      */

50     static BufferedImage JavaDoc gaussianBlur(BufferedImage JavaDoc src, BufferedImage JavaDoc dst, int radius) {
51         int width = src.getWidth();
52         int height = src.getHeight();
53         if (dst == null || dst.getWidth() != width || dst.getHeight() != height || src.getType() != dst.getType()) {
54             dst = createColorModelCompatibleImage(src);
55         }
56         float[] kernel = createGaussianKernel(radius);
57         if (src.getType() == BufferedImage.TYPE_INT_ARGB) {
58             int[] srcPixels = new int[width * height];
59             int[] dstPixels = new int[width * height];
60             getPixels(src, 0, 0, width, height, srcPixels);
61             // horizontal pass
62
blur(srcPixels, dstPixels, width, height, kernel, radius);
63             // vertical pass
64
//noinspection SuspiciousNameCombination
65
blur(dstPixels, srcPixels, height, width, kernel, radius);
66             // the result is now stored in srcPixels due to the 2nd pass
67
setPixels(dst, 0, 0, width, height, srcPixels);
68         } else if (src.getType() == BufferedImage.TYPE_BYTE_GRAY) {
69             byte[] srcPixels = new byte[width * height];
70             byte[] dstPixels = new byte[width * height];
71             getPixels(src, 0, 0, width, height, srcPixels);
72             // horizontal pass
73
blur(srcPixels, dstPixels, width, height, kernel, radius);
74             // vertical pass
75
//noinspection SuspiciousNameCombination
76
blur(dstPixels, srcPixels, height, width, kernel, radius);
77             // the result is now stored in srcPixels due to the 2nd pass
78
setPixels(dst, 0, 0, width, height, srcPixels);
79         } else {
80             throw new IllegalArgumentException JavaDoc("EffectUtils.gaussianBlur() src image is not a supported type, type=[" +
81                     src.getType() + "]");
82         }
83         return dst;
84     }
85
86     /**
87      * <p>Blurs the source pixels into the destination pixels. The force of the blur is specified by the radius which
88      * must be greater than 0.</p> <p>The source and destination pixels arrays are expected to be in the INT_ARGB
89      * format.</p> <p>After this method is executed, dstPixels contains a transposed and filtered copy of
90      * srcPixels.</p>
91      *
92      * @param srcPixels the source pixels
93      * @param dstPixels the destination pixels
94      * @param width the width of the source picture
95      * @param height the height of the source picture
96      * @param kernel the kernel of the blur effect
97      * @param radius the radius of the blur effect
98      */

99     private static void blur(int[] srcPixels, int[] dstPixels,
100                              int width, int height,
101                              float[] kernel, int radius) {
102         float a;
103         float r;
104         float g;
105         float b;
106
107         int ca;
108         int cr;
109         int cg;
110         int cb;
111
112         for (int y = 0; y < height; y++) {
113             int index = y;
114             int offset = y * width;
115
116             for (int x = 0; x < width; x++) {
117                 a = r = g = b = 0.0f;
118
119                 for (int i = -radius; i <= radius; i++) {
120                     int subOffset = x + i;
121                     if (subOffset < 0 || subOffset >= width) {
122                         subOffset = (x + width) % width;
123                     }
124
125                     int pixel = srcPixels[offset + subOffset];
126                     float blurFactor = kernel[radius + i];
127
128                     a += blurFactor * ((pixel >> 24) & 0xFF);
129                     r += blurFactor * ((pixel >> 16) & 0xFF);
130                     g += blurFactor * ((pixel >> 8) & 0xFF);
131                     b += blurFactor * ((pixel) & 0xFF);
132                 }
133
134                 ca = (int) (a + 0.5f);
135                 cr = (int) (r + 0.5f);
136                 cg = (int) (g + 0.5f);
137                 cb = (int) (b + 0.5f);
138
139                 dstPixels[index] = ((ca > 255 ? 255 : ca) << 24) |
140                         ((cr > 255 ? 255 : cr) << 16) |
141                         ((cg > 255 ? 255 : cg) << 8) |
142                         (cb > 255 ? 255 : cb);
143                 index += height;
144             }
145         }
146     }
147
148     /**
149      * <p>Blurs the source pixels into the destination pixels. The force of the blur is specified by the radius which
150      * must be greater than 0.</p> <p>The source and destination pixels arrays are expected to be in the BYTE_GREY
151      * format.</p> <p>After this method is executed, dstPixels contains a transposed and filtered copy of
152      * srcPixels.</p>
153      *
154      * @param srcPixels the source pixels
155      * @param dstPixels the destination pixels
156      * @param width the width of the source picture
157      * @param height the height of the source picture
158      * @param kernel the kernel of the blur effect
159      * @param radius the radius of the blur effect
160      */

161     static void blur(byte[] srcPixels, byte[] dstPixels,
162                             int width, int height,
163                             float[] kernel, int radius) {
164         float p;
165         int cp;
166         for (int y = 0; y < height; y++) {
167             int index = y;
168             int offset = y * width;
169             for (int x = 0; x < width; x++) {
170                 p = 0.0f;
171                 for (int i = -radius; i <= radius; i++) {
172                     int subOffset = x + i;
173 // if (subOffset < 0) subOffset = 0;
174
// if (subOffset >= width) subOffset = width-1;
175
if (subOffset < 0 || subOffset >= width) {
176                         subOffset = (x + width) % width;
177                     }
178                     int pixel = srcPixels[offset + subOffset] & 0xFF;
179                     float blurFactor = kernel[radius + i];
180                     p += blurFactor * pixel;
181                 }
182                 cp = (int) (p + 0.5f);
183                 dstPixels[index] = (byte) (cp > 255 ? 255 : cp);
184                 index += height;
185             }
186         }
187     }
188
189     static float[] createGaussianKernel(int radius) {
190         if (radius < 1) {
191             throw new IllegalArgumentException JavaDoc("Radius must be >= 1");
192         }
193
194         float[] data = new float[radius * 2 + 1];
195
196         float sigma = radius / 3.0f;
197         float twoSigmaSquare = 2.0f * sigma * sigma;
198         float sigmaRoot = (float) Math.sqrt(twoSigmaSquare * Math.PI);
199         float total = 0.0f;
200
201         for (int i = -radius; i <= radius; i++) {
202             float distance = i * i;
203             int index = i + radius;
204             data[index] = (float) Math.exp(-distance / twoSigmaSquare) / sigmaRoot;
205             total += data[index];
206         }
207
208         for (int i = 0; i < data.length; i++) {
209             data[i] /= total;
210         }
211
212         return data;
213     }
214
215     // =================================================================================================================
216
// Get/Set Pixels helper methods
217

218     /**
219      * <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from
220      * a rectangular area defined by a location and two dimensions. Calling this method on an image of type different
221      * from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the
222      * image.</p>
223      *
224      * @param img the source image
225      * @param x the x location at which to start grabbing pixels
226      * @param y the y location at which to start grabbing pixels
227      * @param w the width of the rectangle of pixels to grab
228      * @param h the height of the rectangle of pixels to grab
229      * @param pixels a pre-allocated array of pixels of size w*h; can be null
230      * @return <code>pixels</code> if non-null, a new array of integers otherwise
231      * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
232      */

233     static byte[] getPixels(BufferedImage JavaDoc img,
234                                    int x, int y, int w, int h, byte[] pixels) {
235         if (w == 0 || h == 0) {
236             return new byte[0];
237         }
238
239         if (pixels == null) {
240             pixels = new byte[w * h];
241         } else if (pixels.length < w * h) {
242             throw new IllegalArgumentException JavaDoc("pixels array must have a length >= w*h");
243         }
244
245         int imageType = img.getType();
246         if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
247             Raster JavaDoc raster = img.getRaster();
248             return (byte[]) raster.getDataElements(x, y, w, h, pixels);
249         } else {
250             throw new IllegalArgumentException JavaDoc("Only type BYTE_GRAY is supported");
251         }
252     }
253
254     /**
255      * <p>Writes a rectangular area of pixels in the destination <code>BufferedImage</code>. Calling this method on an
256      * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code>
257      * will unmanage the image.</p>
258      *
259      * @param img the destination image
260      * @param x the x location at which to start storing pixels
261      * @param y the y location at which to start storing pixels
262      * @param w the width of the rectangle of pixels to store
263      * @param h the height of the rectangle of pixels to store
264      * @param pixels an array of pixels, stored as integers
265      * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
266      */

267     static void setPixels(BufferedImage JavaDoc img,
268                                  int x, int y, int w, int h, byte[] pixels) {
269         if (pixels == null || w == 0 || h == 0) {
270             return;
271         } else if (pixels.length < w * h) {
272             throw new IllegalArgumentException JavaDoc("pixels array must have a length >= w*h");
273         }
274         int imageType = img.getType();
275         if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
276             WritableRaster JavaDoc raster = img.getRaster();
277             raster.setDataElements(x, y, w, h, pixels);
278         } else {
279             throw new IllegalArgumentException JavaDoc("Only type BYTE_GRAY is supported");
280         }
281     }
282
283     /**
284      * <p>Returns an array of pixels, stored as integers, from a
285      * <code>BufferedImage</code>. The pixels are grabbed from a rectangular
286      * area defined by a location and two dimensions. Calling this method on
287      * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
288      * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
289      *
290      * @param img the source image
291      * @param x the x location at which to start grabbing pixels
292      * @param y the y location at which to start grabbing pixels
293      * @param w the width of the rectangle of pixels to grab
294      * @param h the height of the rectangle of pixels to grab
295      * @param pixels a pre-allocated array of pixels of size w*h; can be null
296      * @return <code>pixels</code> if non-null, a new array of integers
297      * otherwise
298      * @throws IllegalArgumentException is <code>pixels</code> is non-null and
299      * of length &lt; w*h
300      */

301     public static int[] getPixels(BufferedImage JavaDoc img,
302                                   int x, int y, int w, int h, int[] pixels) {
303         if (w == 0 || h == 0) {
304             return new int[0];
305         }
306
307         if (pixels == null) {
308             pixels = new int[w * h];
309         } else if (pixels.length < w * h) {
310             throw new IllegalArgumentException JavaDoc("pixels array must have a length" +
311                                                " >= w*h");
312         }
313
314         int imageType = img.getType();
315         if (imageType == BufferedImage.TYPE_INT_ARGB ||
316             imageType == BufferedImage.TYPE_INT_RGB) {
317             Raster JavaDoc raster = img.getRaster();
318             return (int[]) raster.getDataElements(x, y, w, h, pixels);
319         }
320
321         // Unmanages the image
322
return img.getRGB(x, y, w, h, pixels, 0, w);
323     }
324
325     /**
326      * <p>Writes a rectangular area of pixels in the destination
327      * <code>BufferedImage</code>. Calling this method on
328      * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
329      * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
330      *
331      * @param img the destination image
332      * @param x the x location at which to start storing pixels
333      * @param y the y location at which to start storing pixels
334      * @param w the width of the rectangle of pixels to store
335      * @param h the height of the rectangle of pixels to store
336      * @param pixels an array of pixels, stored as integers
337      * @throws IllegalArgumentException is <code>pixels</code> is non-null and
338      * of length &lt; w*h
339      */

340     public static void setPixels(BufferedImage JavaDoc img,
341                                  int x, int y, int w, int h, int[] pixels) {
342         if (pixels == null || w == 0 || h == 0) {
343             return;
344         } else if (pixels.length < w * h) {
345             throw new IllegalArgumentException JavaDoc("pixels array must have a length" +
346                                                " >= w*h");
347         }
348
349         int imageType = img.getType();
350         if (imageType == BufferedImage.TYPE_INT_ARGB ||
351             imageType == BufferedImage.TYPE_INT_RGB) {
352             WritableRaster JavaDoc raster = img.getRaster();
353             raster.setDataElements(x, y, w, h, pixels);
354         } else {
355             // Unmanages the image
356
img.setRGB(x, y, w, h, pixels, 0, w);
357         }
358     }
359
360     /**
361      * <p>Returns a new <code>BufferedImage</code> using the same color model
362      * as the image passed as a parameter. The returned image is only compatible
363      * with the image passed as a parameter. This does not mean the returned
364      * image is compatible with the hardware.</p>
365      *
366      * @param image the reference image from which the color model of the new
367      * image is obtained
368      * @return a new <code>BufferedImage</code>, compatible with the color model
369      * of <code>image</code>
370      */

371     public static BufferedImage JavaDoc createColorModelCompatibleImage(BufferedImage JavaDoc image) {
372         ColorModel JavaDoc cm = image.getColorModel();
373         return new BufferedImage JavaDoc(cm,
374             cm.createCompatibleWritableRaster(image.getWidth(),
375                                               image.getHeight()),
376             cm.isAlphaPremultiplied(), null);
377     }
378
379     /**
380      * <p>Returns a new translucent compatible image of the specified width and
381      * height. That is, the returned <code>BufferedImage</code> is compatible with
382      * the graphics hardware. If the method is called in a headless
383      * environment, then the returned BufferedImage will be compatible with
384      * the source image.</p>
385      *
386      * @param width the width of the new image
387      * @param height the height of the new image
388      * @return a new translucent compatible <code>BufferedImage</code> of the
389      * specified width and height
390      */

391     public static BufferedImage JavaDoc createCompatibleTranslucentImage(int width,
392                                                                  int height) {
393         return isHeadless() ?
394                 new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_INT_ARGB) :
395                 getGraphicsConfiguration().createCompatibleImage(width, height,
396                                                    Transparency.TRANSLUCENT);
397     }
398
399     private static boolean isHeadless() {
400         return GraphicsEnvironment.isHeadless();
401     }
402
403     // Returns the graphics configuration for the primary screen
404
private static GraphicsConfiguration JavaDoc getGraphicsConfiguration() {
405         return GraphicsEnvironment.getLocalGraphicsEnvironment().
406                     getDefaultScreenDevice().getDefaultConfiguration();
407     }
408
409 }
410
Popular Tags