1 7 package com.sun.java.swing.plaf.nimbus; 8 9 import java.awt.image.BufferedImage ; 10 import java.awt.image.Raster ; 11 import java.awt.image.WritableRaster ; 12 import java.util.Arrays ; 13 14 20 class InnerShadowEffect extends ShadowEffect { 21 22 25 32 Effect.EffectType getEffectType() { 33 return Effect.EffectType.OVER; 34 } 35 36 48 BufferedImage applyEffect(BufferedImage src, BufferedImage dst, int w, int h) { 49 double trangleAngle = Math.toRadians(angle - 90); 51 int offsetX = (int) (Math.sin(trangleAngle) * distance); 52 int offsetY = (int) (Math.cos(trangleAngle) * distance); 53 int tmpOffX = offsetX + size; 55 int tmpOffY = offsetX + size; 56 int tmpW = w + offsetX + size + size; 57 int tmpH = h + offsetX + size; 58 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 Raster 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 float[] kernel = EffectUtils.createGaussianKernel(size * 2); 77 EffectUtils.blur(srcAlphaBuf, tmpBuf2, tmpW, tmpH, kernel, size * 2); EffectUtils.blur(tmpBuf2, tmpBuf1, tmpH, tmpW, kernel, size * 2); 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 if (dst == null) dst = EffectUtils.createCompatibleTranslucentImage(w, h); 87 WritableRaster 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 |