package filter;

import GUI.FileChooser;
import GUI.internalFrame;
import GUI.mainWindow;
import ij.*;
import ij.gui.NewImage;
import ij.process.ImageProcessor;

public class Average {

	ImagePlus imp, imp3;
	ImageProcessor ip, nip, nip2;
	final int scale = 100;
	int numberResult;
	boolean draw;
	
	public Average(ImagePlus imp, ImageProcessor ip, int numberResult, boolean draw) {
		this.imp = imp;
		this.ip = ip;
		this.numberResult = numberResult;
		this.draw = draw;
	}
	
	//filter 3x3 s maticou 1 1 1,1 1 1, 1 1 1
	public void run1(){
		
		//ImagePlus imp2 = NewImage.createImage ("Threshold", ip.getWidth(), ip.getHeight(), 5, 8, NewImage.FILL_WHITE);
		//nip = imp2.getProcessor();
//		System.out.println(((float)1/ (float)6));
		for (int i = 0; i < ip.getWidth(); i++) 
			for (int j = 0; j < ip.getHeight(); j++) {
				if ((i == 0) && (j == 0)) {
					//System.out.println((ip.getPixelValue(i, j) + ip.getPixelValue(i+1,j) + ip.getPixelValue(i, j+1) + ip.getPixelValue(i+1, j+1)) * (1/4));
					ip.putPixelValue(i, j, (ip.getPixelValue(i, j) + ip.getPixelValue(i+1,j) + ip.getPixelValue(i, j+1) + ip.getPixelValue(i+1, j+1)) * 0.25 );
				} else if ((i>0)&& (i<ip.getWidth()-1) &&(j == 0)) 
					ip.putPixelValue(i, j, (ip.getPixelValue(i, j) + ip.getPixelValue(i+1,j) + ip.getPixelValue(i, j+1) + ip.getPixelValue(i+1, j+1) + ip.getPixelValue(i-1, j) + ip.getPixelValue(i-1, j+1)) * ((double)1/(double)6) );
				else if ((i == ip.getWidth()-1) && (j==0))
					ip.putPixelValue(i, j, (ip.getPixelValue(i, j) + ip.getPixelValue(i-1,j) + ip.getPixelValue(i, j+1) + ip.getPixelValue(i-1, j+1) ) * 0.25 );
				else if ((i == 0) && (j>0) && (j<ip.getHeight()-1))
					ip.putPixelValue(i, j, (ip.getPixelValue(i, j) + ip.getPixelValue(i+1,j) + ip.getPixelValue(i+1, j-1) + ip.getPixelValue(i+1, j+1) + ip.getPixelValue(i, j-1) + ip.getPixelValue(i, j+1)) * ((double)1/(double)6) );
				else if ((i>0) && (i<ip.getWidth()-1)&& (j>0) && (j<ip.getHeight()-1)) {
					ip.putPixelValue(i, j, (ip.getPixelValue(i, j) + ip.getPixelValue(i+1,j) + ip.getPixelValue(i+1, j-1) + ip.getPixelValue(i+1, j+1) + ip.getPixelValue(i, j-1) + ip.getPixelValue(i, j+1) + ip.getPixelValue(i-1, j) + ip.getPixelValue(i-1, j+1) + ip.getPixelValue(i-1, j-1)) * ((double)1/(double)9) );
					//System.out.println((ip.getPixelValue(i, j) + ip.getPixelValue(i+1,j) + ip.getPixelValue(i+1, j-1) + ip.getPixelValue(i+1, j+1) + ip.getPixelValue(i, j-1) + ip.getPixelValue(i, j+1) + ip.getPixelValue(i-1, j) + ip.getPixelValue(i-1, j+1) + ip.getPixelValue(i-1, j-1)) * (1/9));
				} else if ((i==ip.getWidth()-1)&& (j>0) && (j<ip.getHeight()-1))
					ip.putPixelValue(i, j, (ip.getPixelValue(i, j) + ip.getPixelValue(i-1,j) + ip.getPixelValue(i-1, j-1) + ip.getPixelValue(i-1, j+1) + ip.getPixelValue(i, j-1) + ip.getPixelValue(i, j+1)) * ((double)1/(double)6) );
				else if ((i==0) && (j==ip.getHeight()-1))
					ip.putPixelValue(i, j, (ip.getPixelValue(i, j) + ip.getPixelValue(i+1,j) + ip.getPixelValue(i, j-1) + ip.getPixelValue(i+1, j-1) ) * 0.25 );
				else if ((i>0) && (i<ip.getWidth()-1)&& (j==ip.getHeight()-1))
					ip.putPixelValue(i, j, (ip.getPixelValue(i, j) + ip.getPixelValue(i+1,j) + ip.getPixelValue(i, j-1) + ip.getPixelValue(i+1, j-1) + ip.getPixelValue(i-1, j) + ip.getPixelValue(i-1, j-1)) * ((double)1/(double)6) );
				else if ((i==ip.getWidth()-1)&& (j==ip.getHeight()-1))
					ip.putPixelValue(i, j, (ip.getPixelValue(i, j) + ip.getPixelValue(i-1,j) + ip.getPixelValue(i-1, j-1) + ip.getPixelValue(i, j-1)) * 0.25 );
				
			}
		
		if (draw) {
		
			mainWindow.intFrameArrayList.add(new internalFrame("Average Filter", imp, FileChooser.number, numberResult, 1, true));
			try {
				//mainWindow.intFrame[FileChooser.number].setSelected(true);
				((internalFrame)(mainWindow.intFrameArrayList.get(FileChooser.number))).setSelected(true);
			} catch(Exception e){
				System.out.println("Chyba");
			}	
			++FileChooser.number;		
		}
		
	}
	
	//filter 5x5 s maticou 1 1 1 1 1, 1 1 1 1 1, 1 1 1 1 1, 1 1 1 1 1, 1 1 1 1 1
	public void run2(){
		
		ImagePlus imp2 = NewImage.createImage ("Threshold", ip.getWidth() + 4, ip.getHeight() + 4, 5, 8, NewImage.FILL_WHITE);
		nip = imp2.getProcessor();
		
		for (int i = 0; i < ip.getWidth(); i++) 
			for (int j = 0; j < ip.getHeight(); j++) {
				if ( !( ((i == 0) || (i == 1) || (i == ip.getWidth() -1 ) || (i == ip.getWidth() -2 )) ||
					((j == 0) || (j == 1) || (j == ip.getHeight() -1 ) || (j == ip.getHeight() -2 )) ) )
/*					nip.putPixelValue(i, j, 255);
				else */
					nip.putPixelValue(i, j, ip.getPixelValue(i, j));
			}
		
		for (int i = 0; i < ip.getWidth(); i++) 
			for (int j = 0; j < ip.getHeight(); j++) {
				if ((i > 1) && (i < ip.getWidth() -2) && (j > 1) && (j < ip.getHeight() -2)) {
					double newValue = ( nip.getPixelValue(i, j) + nip.getPixelValue(i-1, j)+ nip.getPixelValue(i-1, j-1) + nip.getPixelValue(i, j+1) +
					+ nip.getPixelValue(i, j) + nip.getPixelValue(i, j-1) + nip.getPixelValue(i-1, j+1) +
					+ nip.getPixelValue(i+1, j) + nip.getPixelValue(i+1, j-1) + nip.getPixelValue(i+1, j+1) +
					+ nip.getPixelValue(i-2, j) + nip.getPixelValue(i+2, j) + nip.getPixelValue(i-2, j-1) +
					+ nip.getPixelValue(i+2, j-1) + nip.getPixelValue(i-2, j+1) + nip.getPixelValue(i+2, j+1) +
					+ nip.getPixelValue(i-2, j-2) + nip.getPixelValue(i-1, j-2) + nip.getPixelValue(i, j-2) +
					+ nip.getPixelValue(i+1, j-2) + nip.getPixelValue(i+2, j-2) + nip.getPixelValue(i+2, j+2) +
					+ nip.getPixelValue(i+1, j+2) + nip.getPixelValue(i, j+2) + nip.getPixelValue(i-1, j+2) +
					+ nip.getPixelValue(i-2, j+2)) / 25;
					//System.out.println(newValue);
					//if (newValue > 120) newValue = 255;
					//else newValue = 0;
					nip.putPixelValue(i, j, newValue);
				}
			}
		
		imp3 = NewImage.createImage ("Threshold", ip.getWidth(), ip.getHeight(), 5, 8, NewImage.FILL_WHITE);
		nip2 = imp3.getProcessor();
		for (int i = 0; i < ip.getWidth(); i++) 
			for (int j = 0; j < ip.getHeight(); j++) {
				if ((i > 1) && (i < ip.getWidth() -2) && (j > 1) && (j < ip.getHeight() -2)) {
					nip2.putPixelValue(i, j, nip.getPixelValue(i, j));
				}
			}		
		
		if (draw) {
			
			mainWindow.intFrameArrayList.add(new internalFrame("Average Filter", imp3, FileChooser.number, numberResult, 1, true));
			try {
				//mainWindow.intFrame[FileChooser.number].setSelected(true);
				((internalFrame)(mainWindow.intFrameArrayList.get(FileChooser.number))).setSelected(true);
			} catch(Exception e){
				System.out.println("Chyba");
			}	
			++FileChooser.number;		
		}
		
	}	
	
	public void run3(){
		
		ImagePlus imp2 = NewImage.createImage ("Threshold", ip.getWidth() + 4, ip.getHeight() + 4, 5, 8, NewImage.FILL_WHITE);
		nip = imp2.getProcessor();
		
		for (int i = 0; i < ip.getWidth(); i++) 
			for (int j = 0; j < ip.getHeight(); j++) {
				if  ( ((i == 0) || (i == 1) || (i == 2) || (i == ip.getWidth() -1 ) || (i == ip.getWidth() -2 ) || (i == ip.getWidth() -3 )) ||
					((j == 0) || (j == 1) || (j == 2) || (j == ip.getHeight() -1 ) || (j == ip.getHeight() -2 ) || (j == ip.getHeight() -3 )) )
					nip.putPixelValue(i, j, 255);
				else 
					nip.putPixelValue(i, j, ip.getPixelValue(i, j));
			}
		
		for (int i = 0; i < ip.getWidth(); i++) 
			for (int j = 0; j < ip.getHeight(); j++) {
				if ((i > 1) && (i < ip.getWidth() -3) && (j > 1) && (j < ip.getHeight() -3)) {
					double newValue = ( nip.getPixelValue(i, j) + nip.getPixelValue(i-1, j)+ nip.getPixelValue(i-1, j-1) + nip.getPixelValue(i, j+1) +
							+ nip.getPixelValue(i, j) + nip.getPixelValue(i, j-1) + nip.getPixelValue(i-1, j+1) +
							+ nip.getPixelValue(i+1, j) + nip.getPixelValue(i+1, j-1) + nip.getPixelValue(i+1, j+1) +
							+ nip.getPixelValue(i-2, j) + nip.getPixelValue(i+2, j) + nip.getPixelValue(i-2, j-1) +
							+ nip.getPixelValue(i+2, j-1) + nip.getPixelValue(i-2, j+1) + nip.getPixelValue(i+2, j+1) +
							+ nip.getPixelValue(i-2, j-2) + nip.getPixelValue(i-1, j-2) + nip.getPixelValue(i, j-2) +
							+ nip.getPixelValue(i+1, j-2) + nip.getPixelValue(i+2, j-2) + nip.getPixelValue(i+2, j+2) +
							+ nip.getPixelValue(i+1, j+2) + nip.getPixelValue(i, j+2) + nip.getPixelValue(i-1, j+2) +
							+ nip.getPixelValue(i-2, j+2) +
							+ nip.getPixelValue(i-3, j-3) + nip.getPixelValue(i-2, j-3) + nip.getPixelValue(i-1, j-3) +
							+ nip.getPixelValue(i, j-3) + nip.getPixelValue(i+1, j-3) + nip.getPixelValue(i+2, j-3) +
							+ nip.getPixelValue(i+3, j-3) + nip.getPixelValue(i-3, j-2) + nip.getPixelValue(i+3, j-2) +
							+ nip.getPixelValue(i-3, j-1) + nip.getPixelValue(i+3, j-1) + nip.getPixelValue(i-3, j) +
							+ nip.getPixelValue(i+3, j) + nip.getPixelValue(i-3, j+1) + nip.getPixelValue(i+3, j+1) +
							+ nip.getPixelValue(i-3, j+2) + nip.getPixelValue(i+3, j+2) + nip.getPixelValue(i-3, j+3) +
							+ nip.getPixelValue(i-2, j+3) + nip.getPixelValue(i-1, j+3) + nip.getPixelValue(i, j+3) +
							+ nip.getPixelValue(i+1, j+3) + nip.getPixelValue(i+2, j+3) + nip.getPixelValue(i+3, j+3) 
							) / 49;			
					nip.putPixelValue(i, j, newValue);
				}
			}
		
		imp3 = NewImage.createImage ("Threshold", ip.getWidth(), ip.getHeight(), 5, 8, NewImage.FILL_WHITE);
		nip2 = imp3.getProcessor();
		for (int i = 0; i < ip.getWidth(); i++) 
			for (int j = 0; j < ip.getHeight(); j++) {
				if ((i > 2) && (i < ip.getWidth() -3) && (j > 2) && (j < ip.getHeight() -3)) {
					nip2.putPixelValue(i, j, nip.getPixelValue(i, j));
				}
			}		
		
		if (draw) {
		
			mainWindow.intFrameArrayList.add(new internalFrame("Average Filter", imp3, FileChooser.number, numberResult, 1, true));
			try {
				//mainWindow.intFrame[FileChooser.number].setSelected(true);
				((internalFrame)(mainWindow.intFrameArrayList.get(FileChooser.number))).setSelected(true);
			} catch(Exception e){
				System.out.println("Chyba");
			}	
			++FileChooser.number;		
		}
		
	}	
	
	public ImagePlus getImg(){
		return imp3;
	}
	
	public ImageProcessor getIp(){
		return nip2;
	}	
	
	
	protected void finalize() throws Throwable {
	    try {
	        //close();        // close open files
	    } finally {
	        super.finalize();
	        System.out.println("average has been finalized.");
	    }
	}	
	
}
