1173N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
1173N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1173N/A *
1173N/A * This code is free software; you can redistribute it and/or modify it
1173N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1173N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1173N/A *
1173N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1173N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1173N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1173N/A * version 2 for more details (a copy is included in the LICENSE file that
1173N/A * accompanied this code).
1173N/A *
1173N/A * You should have received a copy of the GNU General Public License version
1173N/A * 2 along with this work; if not, write to the Free Software Foundation,
1173N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1173N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1173N/A */
1173N/Apackage javax.swing.plaf.nimbus;
1173N/A
1173N/Aimport java.awt.image.BufferedImage;
1173N/Aimport java.awt.image.Raster;
1173N/Aimport java.awt.image.WritableRaster;
1173N/Aimport java.util.Arrays;
1173N/A
1173N/A/**
1173N/A * InnerShadowEffect - This effect currently only works with ARGB type buffered
1173N/A * images.
1173N/A *
1173N/A * @author Created by Jasper Potts (Jun 18, 2007)
1173N/A */
1173N/Aclass InnerShadowEffect extends ShadowEffect {
1173N/A
1173N/A // =================================================================================================================
1173N/A // Effect Methods
1173N/A
1173N/A /**
1173N/A * Get the type of this effect, one of UNDER,BLENDED,OVER. UNDER means the result of apply effect should be painted
1173N/A * under the src image. BLENDED means the result of apply sffect contains a modified src image so just it should be
1173N/A * painted. OVER means the result of apply effect should be painted over the src image.
1173N/A *
1173N/A * @return The effect type
1173N/A */
1173N/A Effect.EffectType getEffectType() {
1173N/A return Effect.EffectType.OVER;
1173N/A }
1173N/A
1173N/A /**
1173N/A * Apply the effect to the src image generating the result . The result image may or may not contain the source
1173N/A * image depending on what the effect type is.
1173N/A *
1173N/A * @param src The source image for applying the effect to
1173N/A * @param dst The dstination image to paint effect result into. If this is null then a new image will be created
1173N/A * @param w The width of the src image to apply effect to, this allow the src and dst buffers to be bigger than
1173N/A * the area the need effect applied to it
1173N/A * @param h The height of the src image to apply effect to, this allow the src and dst buffers to be bigger than
1173N/A * the area the need effect applied to it
1173N/A * @return Image with the result of the effect
1173N/A */
1173N/A BufferedImage applyEffect(BufferedImage src, BufferedImage dst, int w, int h) {
1173N/A if (src == null || src.getType() != BufferedImage.TYPE_INT_ARGB){
1173N/A throw new IllegalArgumentException("Effect only works with " +
1173N/A "source images of type BufferedImage.TYPE_INT_ARGB.");
1173N/A }
1173N/A if (dst != null && dst.getType() != BufferedImage.TYPE_INT_ARGB){
1173N/A throw new IllegalArgumentException("Effect only works with " +
1173N/A "destination images of type BufferedImage.TYPE_INT_ARGB.");
1173N/A }
1173N/A // calculate offset
1173N/A double trangleAngle = Math.toRadians(angle - 90);
1173N/A int offsetX = (int) (Math.sin(trangleAngle) * distance);
1173N/A int offsetY = (int) (Math.cos(trangleAngle) * distance);
1173N/A // clac expanded size
1173N/A int tmpOffX = offsetX + size;
1173N/A int tmpOffY = offsetX + size;
1173N/A int tmpW = w + offsetX + size + size;
1173N/A int tmpH = h + offsetX + size;
1173N/A // create tmp buffers
1173N/A int[] lineBuf = getArrayCache().getTmpIntArray(w);
1173N/A byte[] srcAlphaBuf = getArrayCache().getTmpByteArray1(tmpW * tmpH);
1173N/A Arrays.fill(srcAlphaBuf, (byte) 0xFF);
1173N/A byte[] tmpBuf1 = getArrayCache().getTmpByteArray2(tmpW * tmpH);
1173N/A byte[] tmpBuf2 = getArrayCache().getTmpByteArray3(tmpW * tmpH);
1173N/A // extract src image alpha channel and inverse and offset
1173N/A Raster srcRaster = src.getRaster();
1173N/A for (int y = 0; y < h; y++) {
1173N/A int dy = (y + tmpOffY);
1173N/A int offset = dy * tmpW;
1173N/A srcRaster.getDataElements(0, y, w, 1, lineBuf);
1173N/A for (int x = 0; x < w; x++) {
1173N/A int dx = x + tmpOffX;
1173N/A srcAlphaBuf[offset + dx] = (byte) ((255 - ((lineBuf[x] & 0xFF000000) >>> 24)) & 0xFF);
1173N/A }
1173N/A }
1173N/A // blur
1173N/A float[] kernel = EffectUtils.createGaussianKernel(size * 2);
1173N/A EffectUtils.blur(srcAlphaBuf, tmpBuf2, tmpW, tmpH, kernel, size * 2); // horizontal pass
1173N/A EffectUtils.blur(tmpBuf2, tmpBuf1, tmpH, tmpW, kernel, size * 2);// vertical pass
1173N/A //rescale
1173N/A float spread = Math.min(1 / (1 - (0.01f * this.spread)), 255);
1173N/A for (int i = 0; i < tmpBuf1.length; i++) {
1173N/A int val = (int) (((int) tmpBuf1[i] & 0xFF) * spread);
1173N/A tmpBuf1[i] = (val > 255) ? (byte) 0xFF : (byte) val;
1173N/A }
1173N/A // create color image with shadow color and greyscale image as alpha
1173N/A if (dst == null) dst = new BufferedImage(w, h,
1173N/A BufferedImage.TYPE_INT_ARGB);
1173N/A WritableRaster shadowRaster = dst.getRaster();
1173N/A int red = color.getRed(), green = color.getGreen(), blue = color.getBlue();
1173N/A for (int y = 0; y < h; y++) {
1173N/A int srcY = y + tmpOffY;
1173N/A int offset = srcY * tmpW;
1173N/A int shadowOffset = (srcY - offsetY) * tmpW;
1173N/A for (int x = 0; x < w; x++) {
1173N/A int srcX = x + tmpOffX;
1173N/A int origianlAlphaVal = 255 - ((int) srcAlphaBuf[offset + srcX] & 0xFF);
1173N/A int shadowVal = (int) tmpBuf1[shadowOffset + (srcX - offsetX)] & 0xFF;
1173N/A int alphaVal = Math.min(origianlAlphaVal, shadowVal);
1173N/A lineBuf[x] = ((byte) alphaVal & 0xFF) << 24 | red << 16 | green << 8 | blue;
1173N/A }
1173N/A shadowRaster.setDataElements(0, y, w, 1, lineBuf);
1173N/A }
1173N/A return dst;
1173N/A }
1173N/A}