import Targa.*;

import java.awt.*;
import java.io.*;
/**
* This performs the `global adjustment' procedure of Herbin et al.
* uses R,G,B values from white reference.
*/


public class Adjust {
	public static void adjust(TargaPicture tp, int whiteR, int whiteG, int whiteB) {
		RGBPoint pt = null;
		double X = java.lang.Math.max(whiteR,java.lang.Math.max(whiteG, whiteB)) ;
		for (int j = 0; j < tp.h; j++) {
			for (int i = 0; i < tp.w; i++) {
				pt = new RGBPoint(tp.getImageData(i+j*tp.w)) ;
				if (pt.r != 0) {
					pt.r = (int)(pt.r * (X/whiteR)) ;
					pt.g = (int)(pt.g * (X/whiteG)) ;
					pt.b = (int)(pt.b * (X/whiteB)) ;
					if (pt.r > 255) pt.r = 255;
					if (pt.g > 255) pt.g = 255;
					if (pt.b > 255) pt.b = 255;

					tp.setImageData(i+j*tp.w,pt.toInt() | 0xFF000000) ;
				}
			}
//System.out.println("\b\b\b\b"+j*100/tp.h);
		}
//    System.out.println("My adjustment");
	}



	public static void meanWhite(TargaPicture tp) {
		int meanR=0,meanG=0,meanB=0,count=0;
		RGBPoint pt = null;
		for (int j = 0; j < tp.h; j++) {
			for (int i = 0; i < tp.w; i++) {
				pt = new RGBPoint(tp.getImageData(i+j*tp.w)) ;
				if (pt.r != 0) {
					meanR += pt.r ;
					meanG += pt.g ;
					meanB += pt.b ;
					count ++ ;
				}
			}
System.out.println("\b\b\b"+j*100/tp.h);
		}
System.out.println("Red " + meanR/count + " Green " + meanG/count + " blue " + meanB/count);
	}



	/** Main
	* Again, this class is designed to be used from the command line.
	* There are three usage options, depending on the first argument
	* If args[0] is `white' then args[1] is the targa file of the white reference image
	*	the RGB values for white reference images are then printed to Std. Out.
	* If args[0] is a directory, then all valid targa files are adjusted by the specified
	* RGB values. These values are obtained from the previous white reference analysis.
	* change in these values requires recompilation, sorry! The adjusted files are saved as xxx.tga3
	* If args[0] is a file, then this file is the only file adjusted. 
	*/
	public static void main(String[] args) {
		TargaPicture tp = null;

		if (args[0].compareTo("white") == 0) {
			try {
				tp = new TargaPicture(args[1]);
				tp.read();
				meanWhite(tp) ;
			} catch (IOException e) {
System.out.println("Error with White analysis:" + e + " ACTION: None") ;
				return ;
			}
		}

		File f = new File(args[0]) ;
		File f2 = new File(args[0]+".tga") ;

		if (f.exists() == true || f2.exists() == true) {
			if (f.isDirectory() != true) {
				try {
					tp = new TargaPicture(args[0]+".tga") ;
					tp.read() ;
				} catch (IOException e) {
System.out.println("Error reading " + args[0] + ".tga - ACTION:EXIT") ;
System.exit(1) ;
      				}
				adjust(tp, 169,185,237) ;   // white ref 1
//				adjust(tp, 242,239,241) ;   // cv swhite ref 1

				TargaView tv = new TargaView(tp);

				try {
					f.renameTo(new File(args[0]+".tga2"));
					tp.write(args[0]+".tga3",false) ;
					System.out.println("Finished Write");
				} catch (IOException e) {
System.out.println("Error writing " + args[0] + ".tga  ACTION: NONE");
				}
			} else { // file is a directory so process all .tga files
				File[] directory = f.listFiles() ; // get list of all files in directory
				for (int i =0; i < directory.length; i++) {
					try {
						tp = new TargaPicture(directory[i].toString()) ;
						tp.read() ;
					} catch (IOException e) {
						System.out.println("Error reading " + directory[i]+" - ACTION:continue") ;
						continue;
					}
					adjust(tp, 169,185,237) ;   // white ref 1
					try {
						directory[i].renameTo(new File(directory[i]+"2"));
						tp.write(directory[i].toString(),false) ;
					} catch (IOException e) {
System.out.println("Error writing "+directory[i].toString()+" ACTION-CONTINUE");
						continue;
					}
				}
			}
		} else {
System.out.println("Cant find file " + args[0] + ". Does it exist?");
		}
	}
}
