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

// Impulsive Noise Generator
public class imp_noise extends noiseFilter
{
	double variance;
	
	// Create object and initialize data
	public imp_noise(float var)
	{
		variance = var*10;
	}
		
	// Runs the algorithm
	public void performEffect()
	{	
		int Range = 255;
		int finalnoiseR = 0;
		int finalnoiseG = 0;
		int finalnoiseB = 0;
		int pointOffset = 0;
		int[] newPixels = new int[width*height];
		
		// 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;
				
				// Creates impulsive noise based on variance and a
				// random number
				if ((Math.random()*1000) < variance)
				{
					finalnoiseR = (int)Math.round(Math.random()*Range);
					finalnoiseG = (int)Math.round(Math.random()*Range);
					finalnoiseB = (int)Math.round(Math.random()*Range);
				}
				else
				{
					finalnoiseR = (int)((pixels[pointOffset] >> 16)&0xff);
					finalnoiseG = (int)((pixels[pointOffset] >> 8)&0xff);
					finalnoiseB = (int)((pixels[pointOffset])&0xff);
				}

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