package main;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintStream;

import javax.imageio.ImageIO;

import preprocessing.Lanczos;
import preprocessing.Parser;
import preprocessing.Util;

public class HoG_Dalal {

	public static void main(String[] args) throws Exception{ 

		/*Settings*/

		Runtime rt = Runtime.getRuntime();

		Parser parser = new Parser();

		String image_files = args[0];

		int fixed_height = Integer.parseInt(args[1]);

		assert(fixed_height > 0);

		String out_path = args[2];

		String option= args[3];

		int ncx = Integer.parseInt(args[4]);

		int ncy = Integer.parseInt(args[5]);

		int nbx = Integer.parseInt(args[6]);

		int nby = Integer.parseInt(args[7]);

		int nox = Integer.parseInt(args[8]);

		int noy = Integer.parseInt(args[9]);

		int nbins = Integer.parseInt(args[10]);
		
		int S = Integer.parseInt(args[11]);
				
		int wtscale = Integer.parseInt(args[12]);
		
		double maxvalue = Double.parseDouble(args[13]);
		
		//int epsilon = Integer.parseInt(args[14]);
		double epsilon = Double.parseDouble(args[14]);
		
		int fullcirc = Integer.parseInt(args[15]);

		String proc =args[16];

		String norm = args[17];

		assert(fixed_height % (ncy*noy) == 0);

		LineNumberReader file = parser.Open_File (image_files);

		/*Image resizing. If the new height is negative the region is not resized.*/
		BufferedImage image_resized = null;			

		int image_number = 0;

		do {
			String[] image_name = new String[1];
			
			BufferedImage image = Util.Get_Image (file, image_name);

			if (image == null) { break; }

			/*Resizing by Lanczos interpolation.*/
			Lanczos lanczos = new Lanczos();
			image_resized = lanczos.lanczos(image, fixed_height, ncx*nox);
			int new_width = image_resized.getWidth();

			if (image_resized.getHeight() != fixed_height) {
				continue;
			}
			assert(image_resized.getHeight() == fixed_height);
			
			assert((new_width % (ncx*nox)) == 0); //-> Isso que mata o 12x1

			/*./dump_rhog -W 61,18 -C 61,6 -N 1,3 -B 9 -G 61,18 -S 0 --wtscale 2
			--maxvalue 0.2 --epsilon 1 --fullcirc 0 -v 3 --proc rgb_sqrt --norm
			l2hys -s 1 pos/list.txt train_pos.RHOG*/
	
			try {
				/*Writing resized image.*/
				ImageIO.write(image_resized, "png", new File(out_path + String.format("%06d", image_number) + ".png"));
			} catch (IOException e) {
				e.printStackTrace();
			}

			FileOutputStream file_out = new FileOutputStream(out_path + String.format("%06d", image_number) + ".txt"); 

			PrintStream out = new PrintStream(file_out);

			out.println(out_path + String.format("%06d", image_number) + ".png");


			int cell_width = (int) Math.floor(new_width/ncx);
			int cell_height = (int) Math.floor(fixed_height/ncy);

			assert((cell_width % nox) == 0);
			assert((cell_height % noy) == 0);

			Process pr1 = rt.exec("./dump_rhog" +
					" -W " + new_width + "," + fixed_height +
					" -C " + cell_width + "," + cell_height +
					" -N " + nbx   + "," + nby +
					" -B " + nbins +
					" -G " + cell_width/nox + "," + cell_height/noy +
					" -S " + S + 
					" --wtscale " + wtscale +
					" --maxvalue " + maxvalue +
					" --epsilon " + epsilon +
					" --fullcirc " + fullcirc +
					" -v 3" + 
					" --proc " + proc +
					" --norm " + norm + " " +
					out_path + String.format("%06d",image_number) + ".txt " +
					out_path + String.format("%06d",image_number) + ".hog");


			int exitVal1 = pr1.waitFor();

			Process pr2 = rt.exec("./dump4svmlearn " + 
					option + 
					" " + 
					out_path + String.format("%06d", image_number) + ".hog" +
					" " + 
					out_path + String.format("%06d", image_number) + ".blt" +
			        " -f SVMLight");

			int exitVal2 = pr2.waitFor();

			if ( (exitVal1 != 0) || (exitVal2 != 0) ) {
				System.out.println("Error executing command\n");
				System.err.println("Error executing command\n");
				System.exit(1);
			}    


			out.close();			

			FileOutputStream info = new FileOutputStream(out_path + String.format("%06d", image_number) + ".info.txt"); 

			PrintStream Info = new PrintStream(info);

			Info.println("./dump_rhog" +
					" -W " + new_width + "," + fixed_height +
					" -C " + cell_width + "," + cell_height +
					" -N " + nbx   + "," + nby +
					" -B " + nbins +
					" -G " + cell_width/nox + "," + cell_height/noy +
					" -S " + S + 
					" --wtscale " + wtscale +
					" --maxvalue " + maxvalue +
					" --epsilon " + epsilon +
					" --fullcirc " + fullcirc +
					" -v 3" + 
					" --proc " + proc +
					" --norm " + norm + " " +
					out_path + String.format("%06d",image_number) + ".txt " +
					out_path + String.format("%06d",image_number) + ".hog");

			Info.close();

			image_number++;			

		} while (true);


		/*End-resizing*/
	}
}
