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

// Directional-Distance Filter
public class DDF extends vectorFilter
{
	int indent;

	// Create object and initialize data
	public DDF(int size)
	{
		masksize = size;
		maskIndex = new int[masksize*masksize];
		array = new double[masksize*masksize];
		indent = (int) Math.floor(masksize/2);
	}

	// Runs the algorithm
	public void performEffect()
	{
		int counter;	
		int newPixels[] = new int [width*height];
		double temp_array[] = new double [masksize*masksize];

		// 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;

				// Calculate Index for mask
				counter = 0;
				for (int i=-indent;i<=indent;i++)
				{
					for (int j=-indent;j<=indent;j++)
					{
						maskIndex[counter] = (y+i)*width+(x+j);
						counter++;
					}
				}

				// Distance from pixel to all others in mask
				calc_dist();
				
				// Place sum of distances in temp array
				for (int i=0; i<masksize*masksize; i++)
				{
					temp_array[i]=array[i];
				}
				
				// Angle from pixel to all others in mask
				calc_angle();
				
				// Calculate sum of dist mult by sum of angles
				for (int i=0; i<masksize*masksize; i++)
				{
					array[i]=array[i]*temp_array[i];
				}
				
				// Sort the new data array				
				b_sort();
				
				// Output smallest pixel value in array
				newPixels[pointOffset] = pixels[maskIndex[0]];
			}
		}
		this.pixels = newPixels;
	}
}
