package yi.iu.ui;

import yi.iu.ui.*;
import java.beans.*;
import java.awt.*;
import java.awt.image.*;

/**
 * This Frame is used to display the segmentation result of an image
 * When the mouse moved, the segmentation and RGB information are displayed;
 * when the mouse button pressed, only the segment you selected are displayed
 */
public class SegFrame extends yi.iu.ui.DisplayFrame
{
    int[] seg= null;
    boolean VERBOSE=true;

	public SegFrame()
	{
	    super();
	}

	public SegFrame(int[] data, int w, int h) {
	    super(data,w,h);
	}

	public SegFrame(int[] data, int w, int h, int[] _seg)
	{
	    this(data,w,h);
	    seg=_seg;
	}

   	public SegFrame(String title, int[] data, int w, int h, int[] _seg)
	{
	    super(title, data,w,h);
	    seg=_seg;
	}

	public void pressInfo(int x, int y)
	{
		if (seg==null) return;
		int label=seg[y*width+x];

		// mask the image and only the segment with number of blob counter is remained
		int[] display= new int[width*height];
		int area=0;
		for (int i=0; i<seg.length; i++) {
		    if (seg[i]==label) {display[i]=imageData[i];area++;}
		    else display[i]=0xff000000;
		}
        //if (VERBOSE) System.err.println(area);

		//display the masked image
	    Image im = createImage(new MemoryImageSource(width, height, display, 0, width));
		try {
		    imageViewer.setImage(im);
		}
		catch (Exception e) {
		    System.err.println("Can't set image!");
		}
		imageViewer.repaint();
	}

	public void displayInfo(int x, int y)
	{
		int rgb= imageData[y*width+x];
		int r=rgb>>16&255,g=rgb>>8&255,b=rgb&255;
		infLabel.setText("("+x+","+y+"): "+seg[y*width+x]+"-RGB("+r+","+g+","+b+")");
	}

    public void mouseExit()
    {
        //display the masked image
	    Image im = createImage(new MemoryImageSource(width, height, imageData, 0, width));
		try {
		    imageViewer.setImage(im);
		}
		catch (Exception e) {
		    System.err.println("Can't set image!");
		}
		imageViewer.repaint();
    }
}