//author: Andrea Salvatore Tambone
//version: 1.2
//date: 01/07/2011

import java.io.*;
import java.text.SimpleDateFormat;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.plugin.filter.*;
import java.util.*;
import javax.swing.*;


public class Specchia_Orizzontalmente implements PlugInFilter {
	ImagePlus imp;

	public int setup(String arg, ImagePlus imp) {
		this.imp = imp;
		return DOES_ALL;
		
	}
	
  
	private void getReport(int state)
	{
        //create data format
 		SimpleDateFormat dt= new SimpleDateFormat(".ddMMyyyyHHmmss");
    //create name of the html page*/
    String filename1 = imp.getOriginalFileInfo().fileName;
	String filename =filename1.concat((String)dt.format(new Date()));
    JFileChooser jfc;
    jfc=new JFileChooser();
   
       try {
        //create File chooser
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);  
        jfc.setAcceptAllFileFilterUsed(false);  
        jfc.setAcceptAllFileFilterUsed(false);
        jfc.setDialogTitle("Save HTML Report");
        int res=jfc.showSaveDialog(null);     
    	   
         //create DataOutputStream
         DataOutputStream dataout = null;
        //Save
        if(res==JFileChooser.APPROVE_OPTION){			
				File f = jfc.getSelectedFile();
				f=new File(f.getAbsolutePath()+System.getProperty("file.separator")+filename+".html");
				dataout = new DataOutputStream(new FileOutputStream(f));
				//create output
				PrintStream out = new PrintStream(dataout);
				out.print(this.createHtmlReport(filename1));
			}
        //Cancel (the first time it'll show an warning message and reply the JFileChooser)
        if(res==JFileChooser.CANCEL_OPTION && state==0){
        JOptionPane.showMessageDialog(null,"Annullando l'operazione le modifiche apportate\n non avranno alcun report di riferimento!","ATTENZIONE!!", JOptionPane.WARNING_MESSAGE);
        this.getReport(1);
        }
			;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
      			System.exit(1);
		}return;
	}
  
  public String getName()
	{
		return "Specchia Orizzontalmente";
	}
	
	public String getDesc()
	{
		return "Specchia Orizzontalmente l'immagine.";
	}
	
	private String createHtmlReport(String filename)
	{
		//date
 		SimpleDateFormat creation= new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    String date=(String)creation.format(new Date());
    
    String html=
        "<html>"+
        "<head>"+
        "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">"+
        "</head>"+
        "<body>"+
        "<h1>Forensics Toolkit - "+this.getName()+" </h1>"+
        "Report generation: "+date+"<BR>"+
        "<BR>"+
        "<strong>File Originale.: "+filename+"<br>"+
        "<hr><h2>Riepilogo:</h2>"+
        "<table border=\"0\">"+
        "	<tr><td>1. <a href=\"#ring0\">Carica Immagine</a>: &nbsp;&nbsp;</td><td><span style=\"font-style: italic;\">Carica un'immagine da file.</span></td><br>"+
        "<tr><td>2. <a href=\"#ring1\"> "+this.getName()+" </a>: &nbsp;&nbsp;</td><td><span style=\"font-style: italic;\">"+this.getDesc()+"</span><br></td>"+
        "</table>"+
        "<br><p>"+
        "<hr><h2>Dettagli:</h2>"+
        "<h3>Catena 1: <a name=\"chain0\">Carica Immagine</a></h3>"+
        "<h4><a name=\"ring0\">1.Carica Immagine</a><br>&nbsp&nbsp<span style=\"font-style: italic;font-weight:normal\">Carica un'immagine da file.</span><br></h4>"+
        "<h4><a name=\"ring1\">2. "+this.getName()+" </a><br>&nbsp&nbsp<span style=\"font-style: italic;font-weight:normal\">"+this.getDesc()+        "</span><br></h4>"+
        "<br>"+
        "</body></html>";
return html;	
}

	public void run(ImageProcessor ip) {
  // clone the Image Processor
	ImageProcessor tmp=ip.duplicate();
  //initialize w and h as width and height of the image 
	int w=ip.getWidth();
	int h=ip.getHeight();
	//per row
		for (int i=0;i<w;i++){
      //per column
			for(int j=0;j<h;j++){
      //replace the pixel with their vertical opposite (the same row)
			ip.putPixel(i, j, tmp.getPixel(w-i-1,j));
			}
		}
  //replace the manipulated Image Processor as the main image
	
  //update
	imp.updateAndDraw();
  //generate report html
  this.getReport(0);

	}

}