import java.awt.*;
import java.awt.image.*;
import java.io.*;

// Exponential Noise Generator
public class ex_noise extends noiseFilter
{
	double variance;
	double[] prob = new double[SIZE];
	double[] Prob = new double[SIZE];
	
	// Create object and initialize data
	public ex_noise(float var)
	{
		variance = var*500;
	}
		
	// Runs the algorithm
	public void performEffect()
	{
		double stdDev;		
		int finalnoiseR = 0;
		int finalnoiseG = 0;
		int finalnoiseB = 0;
		int pointOffset = 0;
		int[] newPixels = new int[width*height];
		
		stdDev = Math.sqrt(variance);
		
		// Initializes and sets probability array (Exponential)
		prob[0] = 1/stdDev;
		Prob[0] = 0;
		
		for (int i=1; i<SIZE; i++)
		{
			prob[i] = Math.exp(-i/stdDev)/stdDev;
			Prob[i] = Prob[i-1] + ((prob[i-1] + prob[i])/2);
		}

		// Moves through the input array pixel by pixel
		for(int i=0;i<height;i++)
		{
			for(int j=0;j<width;j++)
			{
				// Location of current pixel
				pointOffset = i*width+j;

				// Finds the exponential value
				calc_array(Prob);

				// Creates exponential noise
				finalnoiseR = (int)(((pixels[pointOffset] >> 16)&0xff) + Mid
				- stdDev);
				finalnoiseG = (int)(((pixels[pointOffset] >> 8)&0xff) + Mid
				- stdDev);
				finalnoiseB = (int)(((pixels[pointOffset])&0xff) + Mid
				- stdDev);

				if (finalnoiseR > 255) finalnoiseR = 255;
				if (finalnoiseG > 255) finalnoiseG = 255;
				if (finalnoiseB > 255) finalnoiseB = 255;
				if (finalnoiseR < 0) finalnoiseR = 0;
				if (finalnoiseG < 0) finalnoiseG = 0;
				if (finalnoiseB < 0) finalnoiseB = 0;
					
				newPixels[pointOffset] = (255<<24)|(finalnoiseR<<16)|(finalnoiseG<<8)|(finalnoiseB);
			}
		}
		this.pixels = newPixels;
	}
}
