import java.awt.*;
import java.awt.image.*;
import java.io.*;
import Histogram;


class volumeFrame extends Frame
{
	java.awt.Label		label1,label2,label3,status;
	java.awt.Choice		choiceAxis;
	java.awt.Scrollbar	barValue;
	private int		img1[][][], img2[][][], axis;
	int 			section[], p = 0, zoom = 64;
	Image			outputImage1, outputImage2, outputImage3;
	Histogram		h;
	MenuItem		histMenu;
	boolean 		showHistImage = true;
	
	static String OPEN="Open...",
	INTENSITY="intensity.img",
	BINARY="binary.img";
	
	public volumeFrame()
	{
		setLayout(null);
		addNotify();
		setTitle("Volume visualization");
		MenuBar menus = new MenuBar();
		Menu fileMenu = new Menu("File");
		fileMenu.add(new MenuItem(OPEN));
		Menu loadMenu = new Menu("Load");
		loadMenu.add(new MenuItem(INTENSITY));
		loadMenu.add(new MenuItem(BINARY));
		loadMenu.setEnabled(false);
		fileMenu.add(loadMenu);
		histMenu = new MenuItem("Hide histogram equalized image");
		fileMenu.add(histMenu);
		fileMenu.add(new MenuItem("Close"));
		menus.add(fileMenu);
		Menu zoomMenu = new Menu("Zoom");
		zoomMenu.add(new MenuItem("1x"));
		zoomMenu.add(new MenuItem("2x"));
		zoomMenu.add(new MenuItem("3x"));
		zoomMenu.add(new MenuItem("4x"));
		menus.add(zoomMenu);
		setMenuBar(menus);
		choiceAxis = new java.awt.Choice();
		choiceAxis.addItem("X axis   (Y - Z plane)");
		choiceAxis.addItem("Y axis   (X - Z plane)");
		choiceAxis.addItem("Z axis   (X - Y plane)");
		choiceAxis.setBounds(112,65,189,23);
		add(choiceAxis);
		barValue = new java.awt.Scrollbar(Scrollbar.HORIZONTAL,0,0,0,64);
		barValue.setBounds(112,95,189,15);
		add(barValue);
		label1 = new java.awt.Label("Slice image along :");
		label1.setBounds(10,65,112,23);
		add(label1);
		label2 = new java.awt.Label("Slice #");
		label2.setBounds(10,95,42,15);
		add(label2);
		label3 = new java.awt.Label("0");
		label3.setBounds(49,95,43,18);
		add(label3);
		status = new java.awt.Label("Idle");
		status.setBounds(10,120,180,20);
		add(status);
		setBounds(0,0,330,220);
		img1 = new int[64][64][64];
		img2 = new int[64][64][64];
		section = new int[4096];
		h = new Histogram(section, 64, 64);
		show();
		loadImg("intensit.img", "binary.img");
		sliceX(1);
	}
	
	private void loadImg(String filename1, String filename2)
	{
		status.setText("Loading intensity image...");
		try
		{
			FileInputStream in1 = new FileInputStream(filename1);
		
			for(int i=0; i < 64; i++)
			for(int j=0; j < 64; j++)
			for(int k=0; k < 64; k++)
				img1[i][j][k] = in1.read();	
			in1.close();
		}
		catch (IOException e)
        {
            System.out.println( e );
			status.setText("Error reading file");
        }
	
	
		status.setText("Loading binary image...");
		try
		{
			FileInputStream in2 = new FileInputStream(filename2);
		
			for(int i=0; i < 64; i++)
			for(int j=0; j < 64; j++)
			for(int k=0; k < 64; k++)
				img2[i][j][k] = in2.read();			
			in2.close();
		}
		catch (IOException e)
        {
            System.out.println( e );
			status.setText("Error reading file");
        }
		status.setText("Ready to work...");
	}
	

	private void sliceX(int x)
	{
		p = 0;
		for (int b=0; b < 64; b++)
		for (int c=0; c < 64; c++)
			{ section[p] = img1[x][b][c]; p++; }
			
		outputImage1 = createImage(section,64,64);
		if (showHistImage)
		{
			h.update(section);
			section = h.equalize();
			outputImage3 = createImage(section,64,64);
		}
		
		p = 0;
		for (int b=0; b < 64; b++)
		for (int c=0; c < 64; c++)
			{ section[p] = img2[x][b][c]; p++; }
			
		outputImage2 = createImage(section,64,64);
		repaint();
	}
	
	private void sliceY(int y)
	{
		p = 0;
		for (int a=0; a < 64; a++)
		for (int c=0; c < 64; c++)
			{ section[p] = img1[a][y][c]; p++; }
			
		outputImage1 = createImage(section,64,64);
		if (showHistImage)
		{
			h.update(section);
			section = h.equalize();
			outputImage3 = createImage(section,64,64);
		}
		
		p = 0;
		for (int a=0; a < 64; a++)
		for (int c=0; c < 64; c++)
			{ section[p] = img2[a][y][c]; p++; }
			
		outputImage2 = createImage(section,64,64);
		repaint();
	}
	
	private void sliceZ(int z)
	{
		p = 0;
		for (int a=0; a < 64; a++)
		for (int b=0; b < 64; b++)
			{ section[p] = img1[a][b][z]; p++; }
			
		outputImage1 = createImage(section,64,64);
		if (showHistImage)
		{
			h.update(section);
			section = h.equalize();
			outputImage3 = createImage(section,64,64);
		}
		
		p = 0;
		for (int a=0; a < 64; a++)
		for (int b=0; b < 64; b++)
			{ section[p] = img2[a][b][z]; p++; }
			
		outputImage2 = createImage(section,64,64);
		repaint();
	}
			
	
	public boolean handleEvent(Event event)
	{
		if (event.id == Event.WINDOW_DESTROY)
		{
			dispose();
			return true;
		}
		else if (event.target == barValue)
		{
			Integer val = new Integer(barValue.getValue());
			label3.setText(val.toString());
			int image[];
			
			if (axis == 0)
				sliceX(barValue.getValue());
			else if (axis == 1)
				sliceY(barValue.getValue());
			else
				sliceZ(barValue.getValue());
			
			return true;
		}
		else if (event.target == choiceAxis)
		{
			axis = choiceAxis.getSelectedIndex();
		}
		else if (event.id == Event.ACTION_EVENT)
		{
			if (event.target instanceof MenuItem)
			{
				String menuLabel = (String)event.arg;
				
				if (menuLabel.equals("Close"))
				{
					dispose();
					hide();
					return true;
				}
				else if (menuLabel.equals("1x"))
				{
					zoom = 64;
					setBounds(0,0,330,220);
					status.setText("Zoom set to 1X");
				}
				else if (menuLabel.equals("2x"))
				{
					zoom = 128;
					setBounds(0,0,424,280);
					status.setText("Zoom set to 2X");
				}
				else if (menuLabel.equals("3x"))
				{
					zoom = 192;
					setBounds(0,0,620,350);
					status.setText("Zoom set to 3X");
				}
				else if (menuLabel.equals("4x"))
				{
					zoom = 256;
					setBounds(0,0,828,420);
					status.setText("Zoom set to 4X");
				}
				else if (menuLabel.equals("Show histogram equalized image"))
				{
					histMenu.setLabel("Hide histogram equalized image");
					showHistImage = true;
				}
				else if (menuLabel.equals("Hide histogram equalized image"))
				{
					histMenu.setLabel("Show histogram equalized image");
					showHistImage = false;
				}
			}
		}
		return super.handleEvent(event);
	}
	
	
	private Image createImage(int pixels[], int width, int height)
	{
		int newpixels[] = new int[pixels.length];
		for (int k = 0; k < pixels.length; k++)
		{
			int a = pixels[k];
			newpixels[k] = (255 << 24) | (a << 16) | (a << 8) | a;
		}
		Image i = createImage(new MemoryImageSource(width, height, newpixels, 0, width));
		return i;
	}
	
	public void paint(Graphics g)
	{
		if (outputImage1 != null) g.drawImage(outputImage1, 10, 140, zoom, zoom, this);
		else System.err.println("Attempted to display null image.");
		
		if (outputImage2 != null) g.drawImage(outputImage2, zoom+20, 140, zoom, zoom, this);
		else System.err.println("Attempted to display null image.");
		
		if (showHistImage)
			if (outputImage3 != null) g.drawImage(outputImage3, 2*zoom+30, 140, zoom, zoom, this);
			else System.err.println("Attempted to display null image.");
	
	}
}
