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

// Sharpen filter
public class sharpfilt extends EffectFilter
{
	int masksize;
	int[] mask;
	
	// Create object and initialize data
	public sharpfilt(int size, int[] input)
	{
		masksize = size;
		mask = input;
	}
	
	// Runs algorithm
	public void performEffect()
	{
		int elements = masksize*masksize;
		int sumr,sumg,sumb;
		int avgr,avgg,avgb;
		int newPixels[] = new int[width*height];	
		int indent = (int) Math.floor(masksize/2);

		newPixels = pixels;
		
		// Moves through the input array pixel by pixel
		for (int y=indent;y<height-indent;y++)
		{
			for (int x=indent;x<width-indent;x++)
			{
				// Location of centre of mask
				int pointOffset = y*width+x;

				sumr = 0;
				sumg = 0;
				sumb = 0;

				// Runs through mask and gives centre pixel large weight
				// Gives all other pixels negative weight
				for (int m=-indent;m<=indent;m++)
				{
					for (int n=-indent;n<=indent;n++)
					{
						if (((m+indent)*masksize + (n+indent)) == ((elements-1)/2))
						{
							sumr += (elements-1)*(((pixels[pointOffset+m*width+n])>>16)&0xff);
							sumg += (elements-1)*(((pixels[pointOffset+m*width+n])>>8)&0xff);
							sumb += (elements-1)*((pixels[pointOffset+m*width+n])&0xff);
						}
						else
						{
							sumr += -(((pixels[pointOffset+m*width+n])>>16)&0xff);
							sumg += -(((pixels[pointOffset+m*width+n])>>8)&0xff);
							sumb += -((pixels[pointOffset+m*width+n])&0xff);
						}
					}
				}
				
				
				// Normailze values by adding up the values of the mask
				avgr = Math.round(sumr/elements) + (((pixels[pointOffset])>>16)&0xff);
				avgg = Math.round(sumg/elements) + (((pixels[pointOffset])>>8)&0xff);
				avgb = Math.round(sumb/elements) + ((pixels[pointOffset])&0xff);
				
				if (avgr > 255) avgr = 255;
				if (avgr < 0) avgr = 0;
				if (avgg > 255) avgg = 255;
				if (avgg < 0) avgg = 0;
				if (avgb > 255) avgb = 255;
				if (avgb < 0) avgb = 0;
					
				newPixels[pointOffset] = (255<<24) | avgr<<16 | avgg <<8 | avgb;
			}
		}
				
		this.pixels = newPixels;
	}
}
