1 /*
2  * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
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
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
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  *
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.
24  */
25 package javax.swing.plaf.nimbus;
26 
27 import java.awt.image.BufferedImage;
28 import java.awt.image.Raster;
29 import java.awt.image.WritableRaster;
30 import java.util.Arrays;
31 
32 /**
33  * DropShadowEffect - This effect currently only works with ARGB type buffered
34  * images.
35  *
36  * @author Created by Jasper Potts (Jun 18, 2007)
37  */
38 class DropShadowEffect extends ShadowEffect {
39 
40     // =================================================================================================================
41     // Effect Methods
42 
43     /**
44      * Get the type of this effect, one of UNDER,BLENDED,OVER. UNDER means the result of apply effect should be painted
45      * under the src image. BLENDED means the result of apply sffect contains a modified src image so just it should be
46      * painted. OVER means the result of apply effect should be painted over the src image.
47      *
48      * @return The effect type
49      */
50     @Override
getEffectType()51     EffectType getEffectType() {
52         return EffectType.UNDER;
53     }
54 
55     /**
56      * Apply the effect to the src image generating the result . The result image may or may not contain the source
57      * image depending on what the effect type is.
58      *
59      * @param src The source image for applying the effect to
60      * @param dst The destination image to paint effect result into. If this is null then a new image will be created
61      * @param w   The width of the src image to apply effect to, this allow the src and dst buffers to be bigger than
62      *            the area the need effect applied to it
63      * @param h   The height of the src image to apply effect to, this allow the src and dst buffers to be bigger than
64      *            the area the need effect applied to it
65      * @return Image with the result of the effect
66      */
67     @Override
applyEffect(BufferedImage src, BufferedImage dst, int w, int h)68     BufferedImage applyEffect(BufferedImage src, BufferedImage dst, int w, int h) {
69         if (src == null || src.getType() != BufferedImage.TYPE_INT_ARGB){
70             throw new IllegalArgumentException("Effect only works with " +
71                     "source images of type BufferedImage.TYPE_INT_ARGB.");
72         }
73         if (dst != null && dst.getType() != BufferedImage.TYPE_INT_ARGB){
74             throw new IllegalArgumentException("Effect only works with " +
75                     "destination images of type BufferedImage.TYPE_INT_ARGB.");
76         }
77         // calculate offset
78         double trangleAngle = Math.toRadians(angle - 90);
79         int offsetX = (int) (Math.sin(trangleAngle) * distance);
80         int offsetY = (int) (Math.cos(trangleAngle) * distance);
81         // clac expanded size
82         int tmpOffX = offsetX + size;
83         int tmpOffY = offsetX + size;
84         int tmpW = w + offsetX + size + size;
85         int tmpH = h + offsetX + size;
86         // create tmp buffers
87         int[] lineBuf = getArrayCache().getTmpIntArray(w);
88         byte[] tmpBuf1 = getArrayCache().getTmpByteArray1(tmpW * tmpH);
89         Arrays.fill(tmpBuf1, (byte) 0x00);
90         byte[] tmpBuf2 = getArrayCache().getTmpByteArray2(tmpW * tmpH);
91         // extract src image alpha channel and inverse and offset
92         Raster srcRaster = src.getRaster();
93         for (int y = 0; y < h; y++) {
94             int dy = (y + tmpOffY);
95             int offset = dy * tmpW;
96             srcRaster.getDataElements(0, y, w, 1, lineBuf);
97             for (int x = 0; x < w; x++) {
98                 int dx = x + tmpOffX;
99                 tmpBuf1[offset + dx] = (byte) ((lineBuf[x] & 0xFF000000) >>> 24);
100             }
101         }
102         // blur
103         float[] kernel = EffectUtils.createGaussianKernel(size);
104         EffectUtils.blur(tmpBuf1, tmpBuf2, tmpW, tmpH, kernel, size); // horizontal pass
105         EffectUtils.blur(tmpBuf2, tmpBuf1, tmpH, tmpW, kernel, size);// vertical pass
106         //rescale
107         float spread = Math.min(1 / (1 - (0.01f * this.spread)), 255);
108         for (int i = 0; i < tmpBuf1.length; i++) {
109             int val = (int) (((int) tmpBuf1[i] & 0xFF) * spread);
110             tmpBuf1[i] = (val > 255) ? (byte) 0xFF : (byte) val;
111         }
112         // create color image with shadow color and greyscale image as alpha
113         if (dst == null) dst = new BufferedImage(w, h,
114                 BufferedImage.TYPE_INT_ARGB);
115         WritableRaster shadowRaster = dst.getRaster();
116         int red = color.getRed(), green = color.getGreen(), blue = color.getBlue();
117         for (int y = 0; y < h; y++) {
118             int srcY = y + tmpOffY;
119             int shadowOffset = (srcY - offsetY) * tmpW;
120             for (int x = 0; x < w; x++) {
121                 int srcX = x + tmpOffX;
122                 lineBuf[x] = tmpBuf1[shadowOffset + (srcX - offsetX)] << 24 | red << 16 | green << 8 | blue;
123             }
124             shadowRaster.setDataElements(0, y, w, 1, lineBuf);
125         }
126         return dst;
127     }
128 }
129