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.*; 020import java.util.*; 021 022/** 023 * A filter which "dissolves" an image by thresholding the alpha channel with random numbers. 024 */ 025public class DissolveFilter extends PointFilter { 026 027 private float density = 1; 028 private float softness = 0; 029 private float minDensity, maxDensity; 030 private Random randomNumbers; 031 032 public DissolveFilter() { 033 } 034 035 /** 036 * Set the density of the image in the range 0..1. 037 * @param density the density 038 * @min-value 0 039 * @max-value 1 040 * @see #getDensity 041 */ 042 public void setDensity( float density ) { 043 this.density = density; 044 } 045 046 /** 047 * Get the density of the image. 048 * @return the density 049 * @see #setDensity 050 */ 051 public float getDensity() { 052 return density; 053 } 054 055 /** 056 * Set the softness of the dissolve in the range 0..1. 057 * @param softness the softness 058 * @min-value 0 059 * @max-value 1 060 * @see #getSoftness 061 */ 062 public void setSoftness( float softness ) { 063 this.softness = softness; 064 } 065 066 /** 067 * Get the softness of the dissolve. 068 * @return the softness 069 * @see #setSoftness 070 */ 071 public float getSoftness() { 072 return softness; 073 } 074 075 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 076 float d = (1-density) * (1+softness); 077 minDensity = d-softness; 078 maxDensity = d; 079 randomNumbers = new Random( 0 ); 080 return super.filter( src, dst ); 081 } 082 083 public int filterRGB(int x, int y, int rgb) { 084 int a = (rgb >> 24) & 0xff; 085 float v = randomNumbers.nextFloat(); 086 float f = ImageMath.smoothStep( minDensity, maxDensity, v ); 087 return ((int)(a * f) << 24) | rgb & 0x00ffffff; 088 } 089 090 public String toString() { 091 return "Stylize/Dissolve..."; 092 } 093}