package noise;

import java.util.ArrayList;

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

public class RegionRemoval_ {
			
		ImagePlus imp, imp2;
		ImageProcessor ip, nip;
		int numberResult;
		boolean draw;
			
		public RegionRemoval_(ImagePlus imp, ImageProcessor ip, int numberResult, boolean draw) {
			this.imp = imp;
			this.ip = ip;
			this.numberResult = numberResult;
			this.draw = draw;
		}	

		public void run(){
			
			imp2 = NewImage.createImage ("Threshold", ip.getWidth(), ip.getHeight(), 1, 8, NewImage.FILL_WHITE);
			nip = imp2.getProcessor();
			ArrayList white_i, white_j;
			white_i = new ArrayList();
			white_j = new ArrayList();
			
			for (int i = 0; i < ip.getWidth(); i++) {
				for (int j = 0; j < ip.getHeight(); j++) {
					if (ip.getPixelValue(i, j) == 0)
						if  ( ((ip.getPixelValue(i-1, j-1) ==0) || (ip.getPixelValue(i-1, j) ==0) || (ip.getPixelValue(i-1, j+1) ==0) ||
								(ip.getPixelValue(i, j-1) ==0) || (ip.getPixelValue(i, j+1) ==0) ||
								(ip.getPixelValue(i+1, j-1) ==0) || (ip.getPixelValue(i+1, j) ==0) || (ip.getPixelValue(i+1, j+1) ==0)) &&
						 ( (ip.getPixelValue(i-2, j-2) !=0) && (ip.getPixelValue(i-1, j-2) !=0) && (ip.getPixelValue(i, j-2) !=0) &&
						   (ip.getPixelValue(i+1, j-2) !=0) && (ip.getPixelValue(i+2, j-2) !=0) &&
						   (ip.getPixelValue(i-2, j-1) !=0) && (ip.getPixelValue(i+2, j-1) !=0) && (ip.getPixelValue(i-2, j) !=0) &&
						   (ip.getPixelValue(i+2, j) !=0) && (ip.getPixelValue(i-2, j+1) !=0) &&
						   (ip.getPixelValue(i+2, j+1) !=0) && (ip.getPixelValue(i-2, j+2) !=0) && (ip.getPixelValue(i-1, j+2) !=0) &&								
						   (ip.getPixelValue(i, j+2) !=0) && (ip.getPixelValue(i+1, j+2) !=0) && (ip.getPixelValue(i+2, j+2) !=0)) ) {
							white_i.add(i);
							white_j.add(j);
						} else nip.putPixelValue(i, j, 0);
							
				}
			}
			
			for (int i = 0; i < white_i.size(); i++) {
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-1, Integer.parseInt(white_j.get(i).toString())-1, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-1, Integer.parseInt(white_j.get(i).toString()), 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-1, Integer.parseInt(white_j.get(i).toString())+1, 255);
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString()), Integer.parseInt(white_j.get(i).toString())-1, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString()), Integer.parseInt(white_j.get(i).toString())+1, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+1, Integer.parseInt(white_j.get(i).toString())-1, 255);
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+1, Integer.parseInt(white_j.get(i).toString()), 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+1, Integer.parseInt(white_j.get(i).toString())+1, 255);
			}
			
			for (int i = 0; i < ip.getWidth(); i++) {
				for (int j = 0; j < ip.getHeight(); j++) {
					if (ip.getPixelValue(i, j) == 0)
						if  ( ((ip.getPixelValue(i-1, j-1) ==0) || (ip.getPixelValue(i-1, j) ==0) || (ip.getPixelValue(i-1, j+1) ==0) ||
								(ip.getPixelValue(i, j-1) ==0) || (ip.getPixelValue(i, j+1) ==0) ||
								(ip.getPixelValue(i+1, j-1) ==0) || (ip.getPixelValue(i+1, j) ==0) || (ip.getPixelValue(i+1, j+1) ==0)) &&
						 ( (ip.getPixelValue(i-2, j-2) ==0) || (ip.getPixelValue(i-1, j-2) ==0) || (ip.getPixelValue(i, j-2) ==0) ||
						   (ip.getPixelValue(i+1, j-2) ==0) || (ip.getPixelValue(i+2, j-2) ==0) ||
						   (ip.getPixelValue(i-2, j-1) ==0) || (ip.getPixelValue(i+2, j-1) ==0) || (ip.getPixelValue(i-2, j) ==0) ||
						   (ip.getPixelValue(i+2, j) ==0) || (ip.getPixelValue(i-2, j+1) ==0) ||
						   (ip.getPixelValue(i+2, j+1) ==0) || (ip.getPixelValue(i-2, j+2) ==0) || (ip.getPixelValue(i-1, j+2) ==0) ||								
						   (ip.getPixelValue(i, j+2) ==0) || (ip.getPixelValue(i+1, j+2) ==0) || (ip.getPixelValue(i+2, j+2) ==0)) &&
						   
						( (ip.getPixelValue(i-3, j-3) !=0) && (ip.getPixelValue(i-2, j-3) !=0) && (ip.getPixelValue(i-1, j-3) !=0) &&
							(ip.getPixelValue(i, j-3) !=0) && (ip.getPixelValue(i+1, j-3) !=0) &&
							(ip.getPixelValue(i+2, j-3) !=0) && (ip.getPixelValue(i+3, j-3) !=0) && (ip.getPixelValue(i-3, j+3) !=0) &&
							(ip.getPixelValue(i-2, j+3) !=0) && (ip.getPixelValue(i-1, j+3) !=0) &&
							(ip.getPixelValue(i, j+3) !=0) && (ip.getPixelValue(i+1, j+3) !=0) && (ip.getPixelValue(i+2, j+3) !=0) &&								
							(ip.getPixelValue(i+3, j+3) !=0) && (ip.getPixelValue(i-3, j-2) !=0) && (ip.getPixelValue(i+3, j-2) !=0) &&
							(ip.getPixelValue(i-3, j-1) !=0) && (ip.getPixelValue(i+3, j-1) !=0) &&
							(ip.getPixelValue(i-3, j) !=0) && (ip.getPixelValue(i+3, j) !=0) &&
							(ip.getPixelValue(i-3, j+1) !=0) && (ip.getPixelValue(i+3, j+1) !=0) &&
							(ip.getPixelValue(i-3, j+2) !=0) && (ip.getPixelValue(i+3, j+2) !=0) 
							) ) {
							white_i.add(i);
							white_j.add(j);
						} else nip.putPixelValue(i, j, 0);
							
				}
			}
			
			for (int i = 0; i < white_i.size(); i++) {
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-1, Integer.parseInt(white_j.get(i).toString())-1, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-1, Integer.parseInt(white_j.get(i).toString()), 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-1, Integer.parseInt(white_j.get(i).toString())+1, 255);
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString()), Integer.parseInt(white_j.get(i).toString())-1, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString()), Integer.parseInt(white_j.get(i).toString())+1, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+1, Integer.parseInt(white_j.get(i).toString())-1, 255);
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+1, Integer.parseInt(white_j.get(i).toString()), 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+1, Integer.parseInt(white_j.get(i).toString())+1, 255);
				
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-2, Integer.parseInt(white_j.get(i).toString())-2, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-1, Integer.parseInt(white_j.get(i).toString())-2, 255);
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString()), Integer.parseInt(white_j.get(i).toString())-2, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+1, Integer.parseInt(white_j.get(i).toString())-2, 255);
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+2, Integer.parseInt(white_j.get(i).toString())-2, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-2, Integer.parseInt(white_j.get(i).toString())-1, 255);
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+2, Integer.parseInt(white_j.get(i).toString())-1, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-2, Integer.parseInt(white_j.get(i).toString()), 255);
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+2, Integer.parseInt(white_j.get(i).toString()), 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-2, Integer.parseInt(white_j.get(i).toString())+1, 255);
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+2, Integer.parseInt(white_j.get(i).toString())+1, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-2, Integer.parseInt(white_j.get(i).toString())+2, 255);
				 
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())-1, Integer.parseInt(white_j.get(i).toString())+2, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString()), Integer.parseInt(white_j.get(i).toString())+2, 255);
				nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+1, Integer.parseInt(white_j.get(i).toString())+2, 255);nip.putPixelValue(Integer.parseInt(white_i.get(i).toString())+2, Integer.parseInt(white_j.get(i).toString())+2, 255);
		
			}			
			
			if (draw) {
				
				mainWindow.intFrameArrayList.add(new internalFrame("Isolated Pixels Removal", imp2, 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 imp2;
		}
		
		public ImageProcessor getIp(){
			return nip;
		}	

		protected void finalize() throws Throwable {
		    try {
		        //close();        // close open files
		    } finally {
		        super.finalize();
		        System.out.println("region has been finalized.");
		    }
		}		
		
	}

