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 021public class ErodeAlphaFilter extends PointFilter { 022 023 private float threshold; 024 private float softness = 0; 025 protected float radius = 5; 026 private float lowerThreshold; 027 private float upperThreshold; 028 029 public ErodeAlphaFilter() { 030 this( 3, 0.75f, 0 ); 031 } 032 033 public ErodeAlphaFilter( float radius, float threshold, float softness ) { 034 this.radius = radius; 035 this.threshold = threshold; 036 this.softness = softness; 037 } 038 039 public void setRadius(float radius) { 040 this.radius = radius; 041 } 042 043 public float getRadius() { 044 return radius; 045 } 046 047 public void setThreshold(float threshold) { 048 this.threshold = threshold; 049 } 050 051 public float getThreshold() { 052 return threshold; 053 } 054 055 public void setSoftness(float softness) { 056 this.softness = softness; 057 } 058 059 public float getSoftness() { 060 return softness; 061 } 062 063 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 064 dst = new GaussianFilter( (int)radius ).filter( src, null ); 065 lowerThreshold = 255*(threshold - softness*0.5f); 066 upperThreshold = 255*(threshold + softness*0.5f); 067 return super.filter(dst, dst); 068 } 069 070 public int filterRGB(int x, int y, int rgb) { 071 int a = (rgb >> 24) & 0xff; 072 int r = (rgb >> 16) & 0xff; 073 int g = (rgb >> 8) & 0xff; 074 int b = rgb & 0xff; 075 if ( a == 255 ) 076 return 0xffffffff; 077 float f = ImageMath.smoothStep(lowerThreshold, upperThreshold, (float)a); 078 a = (int)(f * 255); 079 if ( a < 0 ) 080 a = 0; 081 else if ( a > 255 ) 082 a = 255; 083 return (a << 24) | 0xffffff; 084 } 085 086 public String toString() { 087 return "Alpha/Erode..."; 088 } 089}