#ifndef FILTER
#define FILTER

#include <iostream.h>
#include <fstream.h>
#include "Image.h"
#include <stdlib.h>

//*************************Base Class Filter******************
  	
class Filter{
 
 
 
 public:
 	
  	virtual void manipulateImage()=0;
   	~Filter(){}
  
  
  };

//***********************Derived Class CourseGrain************** 
	


class CourseGrain:public Filter{
	
  public:
  
	CourseGrain( Image & Picture); //constructor takes an image
	
	CourseGrain();
	
	~CourseGrain();	
	
	virtual void manipulateImage();
		
	float TellData(int s,int t);
	
	int TellNewXRes(){return NewXRes;}  //This returns private:NewXRes

	int TellNewYRes(){return NewYRes;}  //This returns private:NewYRes



  private:


  	int X1;		//Lower left x-coordinate selected
	int X2;		//upper right y-coordinate selected
	int Y1;		//lower left y-coordinate selected
	int Y2;		//upper right y-coordinate selected
	int NewXRes;	//number of new rows in X-direction (X-resolution)
	int NewYRes;	//number of new columns in Y-direction (Y-resolution)
	int Width;	//Width of the original image
	int Height;	//Height of the original image
	int **new_image_matrix;	//2-D array to store averages for modified image
	int Incx;	//Number of pixles each grain in the x- direction
	int Incy;	//Number of pixles each grain in the y- direction
	int rows;	//Number of rows in the new image
	int columns;	//Number of columns in the new image
	unsigned int deltax;    //selected length in x
	unsigned int deltay;	//selected length in y
	};
	
	
	
	
	float CourseGrain::TellData(int s,int t){
		return new_image_matrix[s][t];
		}
	
	CourseGrain::CourseGrain( Image & Picture){
	
	Width=Picture.Tellcolumns();		//Width of your Orginal image(n)
	Height=Picture.Tellrows();		//Height of your original image(m)
	
	cout<<"Enter the Lower left point x-Coordinate"<<endl;
	cin>>X1;

	cout<<"Enter the Lower left point y-Coordinate"<<endl;
	cin>>Y1;

	cout<<"Enter the upper right point x-Coordinate"<<endl;
	cin>>X2;

	cout<<"Enter the upper right point y-coordinate"<<endl;
	cin>>Y2;

	cout<<"Enter the new x-resolution "<<endl;
	cin>>NewXRes;

	cout<<"Enter the new y-resolution "<<endl;
	cin>>NewYRes;

		
	
	//allocating memory for averaged data
	
	new_image_matrix=new int*[NewYRes];
		for (int i=0; i<NewYRes; i++){
		new_image_matrix[i]=new int[NewXRes];}
  	  
	  
	deltax= X2-X1+1;  //selected width
	deltay= Y1-Y2+1;  //selected height

	 
	Incx = (int)((float)deltax/((float)NewXRes) + 0.25) ;
  	Incy = (int)((float)deltay/((float)NewYRes) + 0.25) ;
  	 
	 
	rows=(int)((int)((float)Height/(float)NewYRes) * NewYRes+0.5);
  	columns= (int)((int)((float)Width/(float)NewXRes) *NewXRes+0.5);
	 
	 
	 
  	int s=0;   			//index used below
  	int t=0;			//index used below
	
	
	
	//the following will perform the coursegrain by assigning 
	// average avlues of the selected grain size (resolution)
	//and assigning them to new_image_matrix
	for (int j=Y2 ; j<= Y2+Incy*(NewYRes-1) && j< Y1 && j< Height; j+=Incy ,s++){

		t=0;
		for (int i=X1 ; i<= X1+Incx*(NewXRes-1) && i<X2 && i<Width ; i+= Incx,t++){
			
			float Average=0.0;
			float N=0.0;
			float sum=0.0;

	    		for (int k=j; k<=j+Incy-1 && k<Height; k++){
				for (int l=i; l<=i+Incx-1 && l<Width; l++){
				sum = sum + Picture.Tellall(k,l);
				N=N+1.0;
				}
			}
			Average=sum/N;
			new_image_matrix[s][t]=(int)(Average+0.5);

			}
  		}
	
	}
		
	
	
void CourseGrain::manipulateImage(){
	
	
	//allocating the new vieable image which zooms a selected area
	
	unsigned char new_image_matrix_zoom[rows][columns];
	
	
	
	//the following is some test info 
	
	cout<<"rows are"<<rows<<endl;
	cout<<"columns are"<<columns<<endl;
	cout<<"width is"<<Width<<endl;
	cout<<"height is"<<Height<<endl;
	cout<<"Incx is"<< Incx<<endl;
	cout<<"Incy is"<< Incy<<endl;
	
	//The following are some numbers needed later in the loops
	//I have done the calculations now so they won't have
	//to be redone every time (thanks to the Judges)
	//so the following don't have much physical meaning they just
	//add to the efficiency of the program
	
	int rowsbyNewYRes=(int)((float)rows/(float)NewYRes+0.5);
	
	int columnsbyNewXRes=(int)((float)columns/(float)NewXRes+0.5);
	
	int HeightbyNewYRes=(int)((float)Height/(float)NewYRes+0.5);
	
	int WidthbyNewXRes=(int)((float)Width/(float)NewXRes+0.5);
 	
	int s=0;		//index used below
 	int t=0;		//index used below
	
	
   //the following algorithm will assign the averaged values
   //into a matrix which will store them for an output image
   
   	
    for (int k=0; k<rows ; k+=rowsbyNewYRes,s++){
    	t=0;
    	
    	for (int l=0; l<columns; l+=columnsbyNewXRes,t++){	
   
    	   for (int i=k; i<k+HeightbyNewYRes && i<rows; i++){
    	  
    		for (int j=l ; j<l+WidthbyNewXRes && j<columns;j++){
    		
    			new_image_matrix_zoom[i][j] = (char)new_image_matrix[s][t];
    				}
    			}
    		}
    	}
 

	//this will create a file called p.out which is later displayed by xview
   
    	ofstream si(("p.out"),ios::binary|ios::out|ios::app|ios::trunc);
		
		si<<"P5 \n"<<columns<<" "<<rows<<endl<<"255 \n";
		
		si.write( (char*)&new_image_matrix_zoom, sizeof(new_image_matrix_zoom));
		
		si.close();
    
		system("xv p.out&");		//load image with xv
    
	
	

 
 	//the following will create a file data.dat which will store the individual 
 	//average values needed for calculating the thickness of mask required 
 	//to equalize x-ray beam. 

 	ofstream fout("data.dat");
 	for (int b=0; b<NewYRes;b++){
		for (int d=0; d<NewXRes; d++){
			fout<<new_image_matrix[b][d]<<" ";
			}
			fout<<endl;
		}		
		
		fout.close();  
	}  
	
CourseGrain::CourseGrain(){
	
		
  	X1=0;		
	X2=0;		
	Y1=0;		
	Y2=0;		
	NewXRes=0;	
	NewYRes=0;	
	Width=0;	
	Height=0;	
	new_image_matrix=0;	
	Incx=0;
	Incy=0;
	rows=0;
	columns=0;
	deltax=0;
	deltay=0;
	
	}

CourseGrain::~CourseGrain(){
		
		for (int y=0;y<NewYRes;y++){
		delete[] new_image_matrix[y];
		delete[] new_image_matrix;
		
		}
	}


//==================Derived Class TakeNegative=================

//The following class will create a negative of the image by subtracting the pixle value 
//from 255 which is the highest grescale value

class TakeNegative:public Filter{

   public:
	
	TakeNegative(Image & MyImage);   //constructor
	TakeNegative();			 //default constructor
	
	virtual void manipulateImage(); //derived from Filter
	
	~TakeNegative();  		//destructor
	
	
   private:
   	
	
	
	int W;	//Width of the original image
	int H;	//Height of the original image
	
	};
	
	
	
	
	
TakeNegative::TakeNegative(){
	
		W=0;
		H=0;
	}


TakeNegative::TakeNegative(Image & MyImage){
		
	W=MyImage.Tellcolumns();		//Width of your Orginal image(n)
	H=MyImage.Tellrows();
		
		
	//allocating memory for the viewable negative image
	
	
	unsigned char negative_image_matrix[H][W];	

		
	for (int i=0; i<H; i++){
		for (int j=0; j<W; j++){
			negative_image_matrix[i][j]=(char)(255-MyImage.Tellall(i,j));
			}
		}
	
	
	//output to n.out the info regarding the nagative image
	
	
    	ofstream ni(("n.out"),ios::binary|ios::out|ios::app|ios::trunc);
		ni<<"P5 \n"<<W<<" "<<H<<endl<<"255 \n";
		ni.write( (char*)&negative_image_matrix, sizeof(negative_image_matrix));
		ni.close();
    
    	}
    	



TakeNegative::~TakeNegative(){}			
	
		
	
	
	void TakeNegative::manipulateImage(){	//just an attempt at using inheritance

    	system("xv n.out&");		//load image with xv
	


	}
	
	
	
	//==================Derived Class BlacknWhite=================

//The following class will create a Black(0) and White(255) of the image by assigning
 //to the pixle either the value 0 or 255 which are the lowest highest grescale value
//depending on the luminosity asigned by the user at run time.



class BlacknWhite:public Filter{


   public:
	
	BlacknWhite(Image & MyImage);   //constructor
	BlacknWhite();			 //default constructor
	~BlacknWhite();  		//destructor

	virtual void manipulateImage(); //derived from Filter
	
	
	
   private:
   	
	
	
	int W;	//Width of the original image
	int H;	//Height of the original image
	int luminosity;	    //userdefined constrast threshold

	};
	
	
	
	
	
BlacknWhite::BlacknWhite(){
	
		W=0;
		H=0;
		luminosity=0;
	}


BlacknWhite::BlacknWhite(Image & MyImage){
		
	W=MyImage.Tellcolumns();		//Width of your Orginal image(n)
	H=MyImage.Tellrows();
	cout<<"What luminosity threshold do you want for this image? "<<endl;
	cin>>luminosity;
		
	//allocating memory for the viewable BlacknWhite image
	
	
	unsigned char BlacknWhite_image_matrix[H][W];	

		
	for (int i=0; i<H; i++){
		for (int j=0; j<W; j++){
			
			if (MyImage.Tellall(i,j)<luminosity){
			BlacknWhite_image_matrix[i][j]=(char) 0;}
			else{
			BlacknWhite_image_matrix[i][j]=(char) 255;}

			
			
			}
		}
	
	
	//output to bw.out the info regarding the BlacknWhite image
	
	
    	ofstream bwi(("bw.out"),ios::binary|ios::out|ios::app|ios::trunc);
		bwi<<"P5 \n"<<W<<" "<<H<<endl<<"255 \n";
		bwi.write( (char*)&BlacknWhite_image_matrix, sizeof(BlacknWhite_image_matrix));
		bwi.close();
    
    	}
    	



BlacknWhite::~BlacknWhite(){}
	
		
	
	
	void BlacknWhite::manipulateImage(){	//just another attempt at using inheritance

    	system("xv bw.out&");		//load image with xv
		}
	
#endif
