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.*; 020import java.awt.image.*; 021import com.jhlabs.math.*; 022 023/** 024 * This filter diffuses an image by moving its pixels in random directions. 025 */ 026public class DiffuseFilter extends TransformFilter { 027 028 private float[] sinTable, cosTable; 029 private float scale = 4; 030 031 public DiffuseFilter() { 032 setEdgeAction(CLAMP); 033 } 034 035 /** 036 * Specifies the scale of the texture. 037 * @param scale the scale of the texture. 038 * @min-value 1 039 * @max-value 100+ 040 * @see #getScale 041 */ 042 public void setScale(float scale) { 043 this.scale = scale; 044 } 045 046 /** 047 * Returns the scale of the texture. 048 * @return the scale of the texture. 049 * @see #setScale 050 */ 051 public float getScale() { 052 return scale; 053 } 054 055 protected void transformInverse(int x, int y, float[] out) { 056 int angle = (int)(Math.random() * 255); 057 float distance = (float)Math.random(); 058 out[0] = x + distance * sinTable[angle]; 059 out[1] = y + distance * cosTable[angle]; 060 } 061 062 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 063 sinTable = new float[256]; 064 cosTable = new float[256]; 065 for (int i = 0; i < 256; i++) { 066 float angle = ImageMath.TWO_PI*i/256f; 067 sinTable[i] = (float)(scale*Math.sin(angle)); 068 cosTable[i] = (float)(scale*Math.cos(angle)); 069 } 070 return super.filter( src, dst ); 071 } 072 073 public String toString() { 074 return "Distort/Diffuse..."; 075 } 076}