package org.cemte.jacek.mpo;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.image.*;
import java.io.IOException;

import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.protocol.*;

/**
 * 
 * @author jwr
 *
 */
public class ImageFactory extends Panel{


    int[] data={100,20,30,10,200,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,10,200,100,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,10,200,100,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,0,10,200,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,40,200,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,40,200,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,200,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,110,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30,
                100,20,30,10,100,10,30,100,20,30,10,200,10,30,100,20,30,10,200,10,30};

	/**
	 * 
	 * @param w
	 * @param h
	 * @return
	 */
	private Image createSImage(int w, int h){ 
        int pix[] = new int[w * h]; //tablica warto�ci pr�bek 
        int index = 0; 
        //generacja przyk�adowego obrazu 
        for (int y = 0; y < h; y++) { 
            int red = (y * 255) / (h - 1); 
            for (int x = 0; x < w; x++) { 
                int blue = (x * 255) / (w - 1); 
                pix[index++] = (255 << 24) | (red << 16) | blue; 
            }
        } 
        Image img = createImage(new MemoryImageSource(w, h, pix, 0, w)); 
        
        //tworzony jest obraz w RGB o szeroko�ci w, wysoko�ci h, 
        //na podstawie tablicy pr�bek pix, bez przesuni�cia w tej tablicy z w elementami w linii 
        return img; 
    }//createSImage()


    public static Image create1CImage(int w, int h, int [] data){
        IndexColorModel cm = colorLUT(0);
        Image img = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, cm, data, 0, w));
        return img;
    }//create1CImage()

    public static Image create3CImage(int w, int h, int [] data){
        Image img = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, data, 0, w));
        return img;
    }//createSImage()
	
	public Image getImage(int w,int h){
		//return (createSImage(w,h));
        return (create1CImage(21,20,data));
	}//getImage();
	
	public Image getCapture(){
		Image img;
		CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
		Format[] cfmts = deviceInfo.getFormats();
		VideoFormat fmt = (VideoFormat)cfmts[0];
		try{
			DataSource src = Manager.createDataSource(deviceInfo.getLocator());
			FormatControl fmtc = ((CaptureDevice)src).getFormatControls()[0];
			fmtc.setFormat(fmt);
			Player player = Manager.createRealizedPlayer(src);
			player.start();
			Thread.sleep(3000);
			FrameGrabbingControl frameGrabber = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
			Buffer buf = frameGrabber.grabFrame();
			BufferToImage btoi = new BufferToImage(fmt);
			img = btoi.createImage(buf);
			player.close();
			player.deallocate();
		}catch(Exception e){
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			img= toolkit.getImage(ClassLoader.getSystemResource("icons/err.jpg"));
			System.out.println("error ");
		}
		return img;
	}//getCapture()
	
	public BufferedImage getScreenCapture() throws   AWTException, IOException {
		BufferedImage bi;
		try{
     		Thread.currentThread().sleep(2000);
		}catch (Exception e){}
		bi = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
		return bi;
	}//getScreenCapture()
	
	public static void testRobot() {
	        
	        try {
	            
	            Robot robot = new Robot();
	            // Creates the delay of 5 sec so that you can open notepad before
	            // Robot start writting
	            
	            //Runtime.getRuntime().exec("notepad.exe");
                Runtime.getRuntime().exec("open /Applications/TextEdit.app");
	            robot.delay(5000);
	            robot.keyPress(KeyEvent.VK_M);
	            robot.keyPress(KeyEvent.VK_E);
	            robot.keyPress(KeyEvent.VK_T);
	            robot.keyPress(KeyEvent.VK_O);
	            robot.keyPress(KeyEvent.VK_D);
	            robot.keyPress(KeyEvent.VK_Y);
	            robot.keyPress(KeyEvent.VK_SPACE);
	            robot.delay(500);
	            robot.keyPress(KeyEvent.VK_P);
	            robot.keyPress(KeyEvent.VK_R);
	            robot.keyPress(KeyEvent.VK_Z);
	            robot.keyPress(KeyEvent.VK_E);
	            robot.keyPress(KeyEvent.VK_T);
	            robot.keyPress(KeyEvent.VK_W);
	            robot.keyPress(KeyEvent.VK_A);
	            robot.keyPress(KeyEvent.VK_R);
	            robot.keyPress(KeyEvent.VK_Z);
	            robot.keyPress(KeyEvent.VK_A);
	            robot.keyPress(KeyEvent.VK_N);
	            robot.keyPress(KeyEvent.VK_I);
	            robot.keyPress(KeyEvent.VK_A);
	            robot.keyPress(KeyEvent.VK_SPACE);
	            robot.delay(500);
	            robot.keyPress(KeyEvent.VK_O);
	            robot.keyPress(KeyEvent.VK_B);
	            robot.keyPress(KeyEvent.VK_R);
	            robot.keyPress(KeyEvent.VK_A);
	            robot.keyPress(KeyEvent.VK_Z);
	            robot.keyPress(KeyEvent.VK_O);
	            robot.keyPress(KeyEvent.VK_W);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	   
	}//testRobot()

    /**
    * Build color lookup tables
    * (IndexColorModel) colorLUT - konstrukcja tabel kolorowania
    * @param (int) PC_table - wska�nik do tablicy wyboru schematu kolorowania
    */
    public static IndexColorModel colorLUT(int PC_table){
            int bity = 8;
            int size = 256;
            byte r[] = new byte[256];
            byte g[] = new byte[256];
            byte b[] = new byte[256];
            Integer maxColor = new Integer(255);
            byte maxC = maxColor.byteValue();
            //CLUTstale = {"skala szarosci", "tecza", "spektrum", "blackbody"};
           
            int t=0;

            switch(PC_table){
                case 1:
                        for (int bb = 0; bb < size; bb++){
                            Integer l = new Integer(255-bb);
                            r[bb]=g[bb]=b[bb]= l.byteValue();
                        }
                        break;
                case 2:
                        for (int bb = 0; bb < size; bb++){
                            Integer l = new Integer(bb);
                            r[bb]= l.byteValue();
                            Integer gTMP= new Integer(maxC-r[bb]);
                            g[bb]= gTMP.byteValue();
                            b[bb]= l.byteValue();
                        }
                        break;
                case 3:
                            t=0;
                            for (int bb = 0; bb < 51; bb++){
                                    r[bb]= (new Integer(255-t)).byteValue();
                                    g[bb]= (new Integer(0)).byteValue();
                                    b[bb]= (new Integer(255)).byteValue();
                                    t=t+5;
                            }
                            t=0;
                            for (int bb = 51; bb < 102; bb++){
                                    r[bb]= (new Integer(0)).byteValue();
                                    g[bb]= (new Integer(t)).byteValue();
                                    b[bb]= (new Integer(255)).byteValue();
                                    t=t+5;
                            }
                            t=0;
                            for (int bb = 102; bb < 153; bb++){
                                    r[bb]= (new Integer(0)).byteValue();
                                    g[bb]= (new Integer(255)).byteValue();
                                    b[bb]= (new Integer(255-t)).byteValue();
                                    t=t+5;
                            }
                            t=0;
                            for (int bb = 153; bb < 204; bb++){
                                    r[bb]= (new Integer(t)).byteValue();
                                    g[bb]= (new Integer(255)).byteValue();
                                    b[bb]= (new Integer(0)).byteValue();
                                    t=t+5;
                            }
                            t=0;
                            for (int bb = 204; bb < 256; bb++){
                                    r[bb]= (new Integer(255)).byteValue();
                                    g[bb]= (new Integer(255-t)).byteValue();
                                    b[bb]= (new Integer(0)).byteValue();
                                    t=t+5;
                            }
                            break;
                case 4:
                            t=0;
                            for (int bb = 0; bb < 86; bb++){
                                    r[bb]= (new Integer(t)).byteValue();
                                    g[bb]= (new Integer(0)).byteValue();
                                    b[bb]= g[bb];
                                    t=t+3;
                            }
                            t=0;
                            for (int bb = 86; bb < 171; bb++){
                                    r[bb]= (new Integer(255)).byteValue();
                                    g[bb]= (new Integer(t)).byteValue();
                                    b[bb]= (new Integer(0)).byteValue();
                                    t=t+3;
                            }
                            t=0;
                            for (int bb = 171; bb < 256; bb++){
                                    r[bb]= (new Integer(255)).byteValue();
                                    g[bb]= (new Integer(255)).byteValue();
                                    b[bb]= (new Integer(t)).byteValue();
                                    t=t+3;
                            }
                            break;
                case 5:
                            t=0;
                                    for (int bb = 0; bb < 128; bb++){
                                            r[bb]= (new Integer(t)).byteValue();
                                            g[bb]= (new Integer(0)).byteValue();
                                            b[bb]= g[bb];
                                            t=t+2;
                                    }
                                    t=0;
                                    for (int bb = 128; bb < 256; bb++){
                                            r[bb]= (new Integer(255)).byteValue();
                                            g[bb]= (new Integer(t)).byteValue();
                                            b[bb]= (new Integer(0)).byteValue();
                                            t=t+2;
                                    }
                                    t=0;
                                    for (int bb = 191; bb < 255; bb++){
                                            b[bb]= (new Integer(t)).byteValue();
                                            t=t+4;
                                    }
                                    b[255]=(new Integer(255)).byteValue();
                            break;
                case 6:
		                 for (int bb = 0; bb < 64; bb++){
		                         r[bb]= (new Integer(0)).byteValue();
		                         g[bb]= (new Integer(2*bb)).byteValue();
		                         b[bb]= ( new Integer(2*bb-bb/25)).byteValue();
		                 }
		                 t=2;
		                 for (int bb = 64; bb < 128; bb++){
	                         r[bb]= (new Integer(t)).byteValue();
	                         g[bb]= (new Integer(129-t)).byteValue();
	                         b[bb]= ( new Integer(125+t)).byteValue();
	                         t+=2;
	                     }
		                 r[127]=(new Integer(127)).byteValue();
		                 g[127]=(new Integer(0)).byteValue();
		                 b[127]=(new Integer(254)).byteValue();

		                 t=0;
		                 for (int bb = 128; bb < 191; bb++){
	                         r[bb]= (new Integer(127+t-bb/50)).byteValue();
	                         g[bb]= (new Integer(t)).byteValue();
	                         b[bb]= ( new Integer(255-t*2)).byteValue();
	                         t+=2;
	                     }
		                 r[191]=(new Integer(253)).byteValue();
		                 g[191]=(new Integer(126)).byteValue();
		                 b[191]=(new Integer(4)).byteValue();
		                 t=0;
		                 for (int bb = 192; bb < 256; bb++){
		                         r[bb]= (new Integer(254)).byteValue();
		                         g[bb]= (new Integer(127+t)).byteValue();
		                         b[bb]= (new Integer(2*t)).byteValue();
		                         t=t+2;
		                 }

		                 break;
                case 7:
                		t=0;
                		int a=0;
                		for (int bb = 255; bb >191; bb--){
	                        r[bb]= (new Integer(255)).byteValue();
	                        a=255-t;
	                        if (a<0) a=0;
	                        g[bb]= (new Integer(a)).byteValue();
	                        a=255-2*t;
	                        if (a<0) a=0;
	                        b[bb]= (new Integer(255 -2*t)).byteValue();
	                        t=t+4;
                		}
                		t=0;
                		a=0;
                		for (int bb = 191; bb >127; bb--){
                			a=255-t;
	                        if (a<0) a=0;
	                        r[bb]= (new Integer(a)).byteValue();
	                        if(bb>176)
	                        	g[bb]= (new Integer(2)).byteValue();
	                        else if(bb>144)
	                        	g[bb]= (new Integer(1)).byteValue();
	                        else
	                        	g[bb]= (new Integer(0)).byteValue();
	                        b[bb]= (new Integer(0)).byteValue();
	                        t=t+4;
                		}
                		t=4;
                		a=-1;
                		int aa=1;
                		for (int bb = 127; bb >60; bb--){

                			if (a==2){
                				aa++;
                				a=-1;
                				if(aa>25) aa=25;
                			}
                			a++;
	                        r[bb]= (new Integer(aa)).byteValue();
	                        g[bb]= (new Integer(aa)).byteValue();
	                        if(t>255)
	                        	b[bb]= (new Integer(255)).byteValue();
	                        else
	                        	b[bb]= (new Integer(t)).byteValue();
	                        t=t+4;
                		}
                		t=4;
                		a=-1;
                		int aaa=aa;
                		for (int bb = 60; bb >-1; bb--){

                			if (a==4){
                				aa++;
                				a=-1;
                			}
                			a++;
	                        r[bb]= (new Integer(aa)).byteValue();
	                        g[bb]= (new Integer(aaa+=3)).byteValue();
	                        b[bb]= (new Integer(255)).byteValue();

                		}

                	break;

                default:
                            for (int bb = 0; bb < size; bb++){
                                    Integer l = new Integer(bb);
                                    r[bb]=g[bb]=b[bb]= l.byteValue();
                            }

            };

            IndexColorModel cm = new IndexColorModel(bity, size,r,g,b);
            return cm;

    } //koniec public IndexColorModel colorLUT(int PC_table)

	
}//end class
