//import fullscreen.*;
//import japplemenubar.*;

/**
 * Getting Started with Capture.
 * 
 * GSVideo version by Andres Colubri. 
 * 
 * Reading and displaying an image from an attached Capture device. 
 */ 
import codeanticode.gsvideo.*;

GSCapture cam;
//FullScreen fs;

void setup() {
  size(800, 550);
  //fs = new FullScreen(this);
  //fs.enter(); 
  /*
  // List functionality still not ready on Linux
  String[] cameras = GSCapture.list();

  if (cameras.length == 0)
  {
  println("There are no cameras available for capture.");
  exit();
  } else {
  println("Available cameras:");
  for (int i = 0; i < cameras.length; i++)
  println(cameras[i]);
  cam = new GSCapture(this, 320, 240, cameras[0]);
  }

  However, different cameras can be selected by using their device file:
  cam = new GSCapture(this, 640, 480, "/dev/video0");
  cam = new GSCapture(this, 640, 480, "/dev/video1");
  etc.
  */
  cam = new GSCapture(this, 320, 240, "/dev/video0");
}

void draw() {
  int[] paleta = new int[256];
  int[] paleta2 = new int[256];

  paleta[0]=color(0,0,0);

  for(int i=0;i<85;i++){
    paleta[i] = color(0,0,i*3);
  }

  for(int i=85;i<170;i++){
    paleta[i] = color(0,(i-85)*3,255-(i-85)*3);
  }

  for(int i=170;i<255;i++){
    paleta[i] = color((i-170)*3,255-(i-170)*3,0);
  }

  paleta[255] = color(255,0,0);


  /////////////////////////////////////////////////////

  paleta2[0]=color(0,0,0);

  for(int i=0;i<51;i++){
    paleta2[i] = color(0,0,i*5);
  }

  for(int i=51;i<102;i++){
    paleta2[i] = color((i-51)*5,0,255);
  }

  for(int i=102;i<153;i++){
    paleta2[i] = color(255,0,255-((i-102)*5));
  }

  for(int i=153;i<204;i++){
    paleta2[i] = color(255,(i-153)*5,0);
  }

  for(int i=204;i<255;i++){
    paleta2[i] = color(255,255,(i-204)*5);
  }     

  paleta2[255] = color(255,255,255);


  ////////////////////////////////////////////////////


  int y;
  float r,g,b,grs,pr,pg,pb; 
  y = 0;
  loadPixels();
  if (cam.available() == true) {
    cam.read();
    cam.loadPixels();
    for(int i=0;i<240;i++){
      for(int x=320*i;x<320*(i+1);x++){

	r = red(cam.pixels[y]); 
	g = green(cam.pixels[y]);
	b = blue(cam.pixels[y]);

	grs=(r+g+b)/3;

	pixels[y+(i*480)] = color(b,g,r); 
	// pixels[320+y+(i*320)] = color(paleta2[int(grs)]); 

	y=y+1;
      }
    }

    for(int v=0;v<70;v++){
      for(int z=30;z<60;z++){


	r = red(cam.pixels[(100*320)+((320*v)+(100+z))]); 
	g = green(cam.pixels[(100*320)+((320*v)+(100+z))]);
	b = blue(cam.pixels[(100*320)+((320*v)+(100+z))]);
	//  pixels[204800+(v*640)+z] = color(b,g,r); 
	//  pixels[256000+(z*640)+v] = color(b,g,r); 

	for(int s=0;s<8;s++){
	  pixels[((800*250)+(z*800)+v)+s+(7*v)] = color(b,g,r); 

	  grs=(r+g+b)/3;

	  pixels[((800*310)+(z*800)+v)+s+(7*v)] = color(int(grs),int(grs),int(grs)); 
	}

	for (int t=0;t<5;t++){

	  for (int u=0;u<255;u++){
	    pixels[320+(800*(255-u))+t+(v*5)+10]= color(0,0,0);
	  }

	  grs=(r+g+b)/3;
	  //pixels[320+(640*(300-int(grs)+1))+t+(v*4)+10]= color(255,255,255);
	  pixels[320+(800*(255-int(grs)))+(v*4)+10]= color(255,255,0);
	  //pixels[320+(640*(300-int(grs)-1))+t+(v*4)+10]= color(255,255,255);
	}
      }
    }

    updatePixels();

  }
}