KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)InnerShadowEffect.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.image.BufferedImage JavaDoc;
10 import java.awt.image.Raster JavaDoc;
11 import java.awt.image.WritableRaster JavaDoc;
12 import java.util.Arrays JavaDoc;
13
14 /**
15  * InnerShadowEffect
16  *
17  * @author Created by Jasper Potts (Jun 18, 2007)
18  * @version 1.0
19  */

20 class InnerShadowEffect extends ShadowEffect {
21
22     // =================================================================================================================
23
// Effect Methods
24

25     /**
26      * Get the type of this effect, one of UNDER,BLENDED,OVER. UNDER means the result of apply effect should be painted
27      * under the src image. BLENDED means the result of apply sffect contains a modified src image so just it should be
28      * painted. OVER means the result of apply effect should be painted over the src image.
29      *
30      * @return The effect type
31      */

32     Effect.EffectType getEffectType() {
33         return Effect.EffectType.OVER;
34     }
35
36     /**
37      * Apply the effect to the src image generating the result . The result image may or may not contain the source
38      * image depending on what the effect type is.
39      *
40      * @param src The source image for applying the effect to
41      * @param dst The dstination image to paint effect result into. If this is null then a new image will be created
42      * @param w The width of the src image to apply effect to, this allow the src and dst buffers to be bigger than
43      * the area the need effect applied to it
44      * @param h The height of the src image to apply effect to, this allow the src and dst buffers to be bigger than
45      * the area the need effect applied to it
46      * @return The result of appl
47      */

48     BufferedImage JavaDoc applyEffect(BufferedImage JavaDoc src, BufferedImage JavaDoc dst, int w, int h) {
49         // calculate offset
50
double trangleAngle = Math.toRadians(angle - 90);
51         int offsetX = (int) (Math.sin(trangleAngle) * distance);
52         int offsetY = (int) (Math.cos(trangleAngle) * distance);
53         // clac expanded size
54
int tmpOffX = offsetX + size;
55         int tmpOffY = offsetX + size;
56         int tmpW = w + offsetX + size + size;
57         int tmpH = h + offsetX + size;
58         // create tmp buffers
59
int[] lineBuf = getArrayCache().getTmpIntArray(w);
60         byte[] srcAlphaBuf = getArrayCache().getTmpByteArray1(tmpW * tmpH);
61         Arrays.fill(srcAlphaBuf, (byte) 0xFF);
62         byte[] tmpBuf1 = getArrayCache().getTmpByteArray2(tmpW * tmpH);
63         byte[] tmpBuf2 = getArrayCache().getTmpByteArray3(tmpW * tmpH);
64         // extract src image alpha channel and inverse and offset
65
Raster JavaDoc srcRaster = src.getRaster();
66         for (int y = 0; y < h; y++) {
67             int dy = (y + tmpOffY);
68             int offset = dy * tmpW;
69             srcRaster.getDataElements(0, y, w, 1, lineBuf);
70             for (int x = 0; x < w; x++) {
71                 int dx = x + tmpOffX;
72                 srcAlphaBuf[offset + dx] = (byte) ((255 - ((lineBuf[x] & 0xFF000000) >>> 24)) & 0xFF);
73             }
74         }
75         // blur
76
float[] kernel = EffectUtils.createGaussianKernel(size * 2);
77         EffectUtils.blur(srcAlphaBuf, tmpBuf2, tmpW, tmpH, kernel, size * 2); // horizontal pass
78
EffectUtils.blur(tmpBuf2, tmpBuf1, tmpH, tmpW, kernel, size * 2);// vertical pass
79
//rescale
80
float spread = Math.min(1 / (1 - (0.01f * this.spread)), 255);
81         for (int i = 0; i < tmpBuf1.length; i++) {
82             int val = (int) (((int) tmpBuf1[i] & 0xFF) * spread);
83             tmpBuf1[i] = (val > 255) ? (byte) 0xFF : (byte) val;
84         }
85         // create color image with shadow color and greyscale image as alpha
86
if (dst == null) dst = EffectUtils.createCompatibleTranslucentImage(w, h);
87         WritableRaster JavaDoc shadowRaster = dst.getRaster();
88         int red = color.getRed(), green = color.getGreen(), blue = color.getBlue();
89         for (int y = 0; y < h; y++) {
90             int srcY = y + tmpOffY;
91             int offset = srcY * tmpW;
92             int shadowOffset = (srcY - offsetY) * tmpW;
93             for (int x = 0; x < w; x++) {
94                 int srcX = x + tmpOffX;
95                 int origianlAlphaVal = 255 - ((int) srcAlphaBuf[offset + srcX] & 0xFF);
96                 int shadowVal = (int) tmpBuf1[shadowOffset + (srcX - offsetX)] & 0xFF;
97                 int alphaVal = Math.min(origianlAlphaVal, shadowVal);
98                 lineBuf[x] = ((byte) alphaVal & 0xFF) << 24 | red << 16 | green << 8 | blue;
99             }
100             shadowRaster.setDataElements(0, y, w, 1, lineBuf);
101         }
102         return dst;
103     }
104 }
105
Popular Tags