//This Code is written by Siwat Sirichai
import java.io.File;
import java.util.Arrays;

public class DesaturateIt{
	public static void main(String [] args){
		int [][][] rgb = Java101ImageUtil.getRGBArrayFromFile();
		if(rgb==null){return;}
		int[][][] gray = grayscale(rgb);
		int[][][] sepia = sepia(rgb);
		// fill code to show all three images
		// - original, grayscale, sepia
		//Array Structure
		// [X][Y][COLOR]
		int[][][][] images = new int[3][][][]; //We want to draw 3 images, 
		images[0]=rgb; //the default image
		images[1]=gray; //gray image
		images[2]=sepia; //sepiaed image
		Java101ImageUtil.showViewer(images,"show all"); //Call ImageUtil to draw the image
	}

	public static int [][][] grayscale(int [][][] rgb) {
		// fill code
		int[][][] grayscale = new int [rgb.length][rgb[0].length][3];
		for(int i=0;i<grayscale.length;i++) for(int j=0;j<grayscale[0].length;j++) { //Loop through each Pixel
			int modifiedColor = (int)Math.ceil((rgb[i][j][0]+rgb[i][j][1]+rgb[i][j][2])/3.0); //Grayscale is the average of all color in each pixel
			for(int k = 0;k<3;k++)grayscale[i][j][k] = modifiedColor; //All color in pixel == Avg Color
		}
		return grayscale;
	}

	public static int [][][] sepia(int [][][] rgb) {
		// fill code
		int[][][] sepia = new int [rgb.length][rgb[0].length][3];
		for(int i=0;i<sepia.length;i++) for(int j=0;j<sepia[0].length;j++) {
			int modifiedColor = (int)Math.ceil((rgb[i][j][0]+rgb[i][j][1]+rgb[i][j][2])/3.0);
			//Like Grayscale, just adjust the color ratio
			sepia[i][j][0] = (int)(modifiedColor*0.8745);
			sepia[i][j][1] = (int)(modifiedColor*0.6352);
			sepia[i][j][2] = (int)(modifiedColor*0.4941);
		}
		return sepia;
	}
}