001/* 002Copyright 2006 Jerry Huxtable 003 004Licensed under the Apache License, Version 2.0 (the "License"); 005you may not use this file except in compliance with the License. 006You may obtain a copy of the License at 007 008 http://www.apache.org/licenses/LICENSE-2.0 009 010Unless required by applicable law or agreed to in writing, software 011distributed under the License is distributed on an "AS IS" BASIS, 012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013See the License for the specific language governing permissions and 014limitations under the License. 015*/ 016 017package com.jhlabs.image; 018 019import java.awt.image.*; 020 021/** 022 * A filter which adds Gaussian blur to an image, producing a glowing effect. 023 * @author Jerry Huxtable 024 */ 025public class GlowFilter extends GaussianFilter { 026 027 private float amount = 0.5f; 028 029 public GlowFilter() { 030 radius = 2; 031 } 032 033 /** 034 * Set the amount of glow. 035 * @param amount the amount 036 * @min-value 0 037 * @max-value 1 038 * @see #getAmount 039 */ 040 public void setAmount( float amount ) { 041 this.amount = amount; 042 } 043 044 /** 045 * Get the amount of glow. 046 * @return the amount 047 * @see #setAmount 048 */ 049 public float getAmount() { 050 return amount; 051 } 052 053 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 054 int width = src.getWidth(); 055 int height = src.getHeight(); 056 057 if ( dst == null ) 058 dst = createCompatibleDestImage( src, null ); 059 060 int[] inPixels = new int[width*height]; 061 int[] outPixels = new int[width*height]; 062 src.getRGB( 0, 0, width, height, inPixels, 0, width ); 063 064 if ( radius > 0 ) { 065 convolveAndTranspose(kernel, inPixels, outPixels, width, height, alpha, alpha && premultiplyAlpha, false, CLAMP_EDGES); 066 convolveAndTranspose(kernel, outPixels, inPixels, height, width, alpha, false, alpha && premultiplyAlpha, CLAMP_EDGES); 067 } 068 069 src.getRGB( 0, 0, width, height, outPixels, 0, width ); 070 071 float a = 4*amount; 072 073 int index = 0; 074 for ( int y = 0; y < height; y++ ) { 075 for ( int x = 0; x < width; x++ ) { 076 int rgb1 = outPixels[index]; 077 int r1 = (rgb1 >> 16) & 0xff; 078 int g1 = (rgb1 >> 8) & 0xff; 079 int b1 = rgb1 & 0xff; 080 081 int rgb2 = inPixels[index]; 082 int r2 = (rgb2 >> 16) & 0xff; 083 int g2 = (rgb2 >> 8) & 0xff; 084 int b2 = rgb2 & 0xff; 085 086 r1 = PixelUtils.clamp( (int)(r1 + a * r2) ); 087 g1 = PixelUtils.clamp( (int)(g1 + a * g2) ); 088 b1 = PixelUtils.clamp( (int)(b1 + a * b2) ); 089 090 inPixels[index] = (rgb1 & 0xff000000) | (r1 << 16) | (g1 << 8) | b1; 091 index++; 092 } 093 } 094 095 dst.setRGB( 0, 0, width, height, inPixels, 0, width ); 096 return dst; 097 } 098 099 public String toString() { 100 return "Blur/Glow..."; 101 } 102}