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 * InnerShadowEffect - This effect currently only works with ARGB type buffered 34 * images. 35 * 36 * @author Created by Jasper Potts (Jun 18, 2007) 37 */ 38 class InnerShadowEffect 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 */ getEffectType()50 Effect.EffectType getEffectType() { 51 return Effect.EffectType.OVER; 52 } 53 54 /** 55 * Apply the effect to the src image generating the result . The result image may or may not contain the source 56 * image depending on what the effect type is. 57 * 58 * @param src The source image for applying the effect to 59 * @param dst The dstination image to paint effect result into. If this is null then a new image will be created 60 * @param w The width of the src image to apply effect to, this allow the src and dst buffers to be bigger than 61 * the area the need effect applied to it 62 * @param h The height of the src image to apply effect to, this allow the src and dst buffers to be bigger than 63 * the area the need effect applied to it 64 * @return Image with the result of the effect 65 */ applyEffect(BufferedImage src, BufferedImage dst, int w, int h)66 BufferedImage applyEffect(BufferedImage src, BufferedImage dst, int w, int h) { 67 if (src == null || src.getType() != BufferedImage.TYPE_INT_ARGB){ 68 throw new IllegalArgumentException("Effect only works with " + 69 "source images of type BufferedImage.TYPE_INT_ARGB."); 70 } 71 if (dst != null && dst.getType() != BufferedImage.TYPE_INT_ARGB){ 72 throw new IllegalArgumentException("Effect only works with " + 73 "destination images of type BufferedImage.TYPE_INT_ARGB."); 74 } 75 // calculate offset 76 double trangleAngle = Math.toRadians(angle - 90); 77 int offsetX = (int) (Math.sin(trangleAngle) * distance); 78 int offsetY = (int) (Math.cos(trangleAngle) * distance); 79 // clac expanded size 80 int tmpOffX = offsetX + size; 81 int tmpOffY = offsetX + size; 82 int tmpW = w + offsetX + size + size; 83 int tmpH = h + offsetX + size; 84 // create tmp buffers 85 int[] lineBuf = getArrayCache().getTmpIntArray(w); 86 byte[] srcAlphaBuf = getArrayCache().getTmpByteArray1(tmpW * tmpH); 87 Arrays.fill(srcAlphaBuf, (byte) 0xFF); 88 byte[] tmpBuf1 = getArrayCache().getTmpByteArray2(tmpW * tmpH); 89 byte[] tmpBuf2 = getArrayCache().getTmpByteArray3(tmpW * tmpH); 90 // extract src image alpha channel and inverse and offset 91 Raster srcRaster = src.getRaster(); 92 for (int y = 0; y < h; y++) { 93 int dy = (y + tmpOffY); 94 int offset = dy * tmpW; 95 srcRaster.getDataElements(0, y, w, 1, lineBuf); 96 for (int x = 0; x < w; x++) { 97 int dx = x + tmpOffX; 98 srcAlphaBuf[offset + dx] = (byte) ((255 - ((lineBuf[x] & 0xFF000000) >>> 24)) & 0xFF); 99 } 100 } 101 // blur 102 float[] kernel = EffectUtils.createGaussianKernel(size * 2); 103 EffectUtils.blur(srcAlphaBuf, tmpBuf2, tmpW, tmpH, kernel, size * 2); // horizontal pass 104 EffectUtils.blur(tmpBuf2, tmpBuf1, tmpH, tmpW, kernel, size * 2);// vertical pass 105 //rescale 106 float spread = Math.min(1 / (1 - (0.01f * this.spread)), 255); 107 for (int i = 0; i < tmpBuf1.length; i++) { 108 int val = (int) (((int) tmpBuf1[i] & 0xFF) * spread); 109 tmpBuf1[i] = (val > 255) ? (byte) 0xFF : (byte) val; 110 } 111 // create color image with shadow color and greyscale image as alpha 112 if (dst == null) dst = new BufferedImage(w, h, 113 BufferedImage.TYPE_INT_ARGB); 114 WritableRaster shadowRaster = dst.getRaster(); 115 int red = color.getRed(), green = color.getGreen(), blue = color.getBlue(); 116 for (int y = 0; y < h; y++) { 117 int srcY = y + tmpOffY; 118 int offset = srcY * tmpW; 119 int shadowOffset = (srcY - offsetY) * tmpW; 120 for (int x = 0; x < w; x++) { 121 int srcX = x + tmpOffX; 122 int origianlAlphaVal = 255 - ((int) srcAlphaBuf[offset + srcX] & 0xFF); 123 int shadowVal = (int) tmpBuf1[shadowOffset + (srcX - offsetX)] & 0xFF; 124 int alphaVal = Math.min(origianlAlphaVal, shadowVal); 125 lineBuf[x] = ((byte) alphaVal & 0xFF) << 24 | red << 16 | green << 8 | blue; 126 } 127 shadowRaster.setDataElements(0, y, w, 1, lineBuf); 128 } 129 return dst; 130 } 131 } 132