// Client program code

/*
Author: Srikanta Patnaik and Team 
Machine Intelligence Laboratory, University College of Engineering, Burla, India
Date of Writing: April, 2003; Version: 1.0
Book: Robot Cognition and Navigation: An Experiment with Mobile Robots
Publisher: Springer
ISBN: 978-3-540-23446-3
URL: http://www.springer.com/3-540-23446-2
 
*/


// filename : ColCamera.java
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;

public class ColCamera extends Frame implements Runnable,ActionListener
{
Image img;
boolean flag=true;
MemoryImageSource imsource;
int pixels[];int h,w;
Socket client;
DataInputStream dis;
DataOutputStream dos;
String input;
StringTokenizer st;
Button b;

public ColCamera()
{
super("Image Capture");
int n;
// open the socket 
try{client = new Socket("192.168.0.9",9905);
if(client == null)System.exit(0);
System.out.println("connected");

// set no TCP delay
client.setTcpNoDelay(true);

// get the input and output details
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());

// set the size of window
w=320;
h=240;
System.out.println(w+" "+h);
dos.write((new String("recd")).getBytes());
dos.flush();
}catch(Exception e){}
// the image 
pixels = new int[h*w];
imsource = new MemoryImageSource(w,h,pixels,0,w);
imsource.setAnimated(true);
img = createImage(imsource);		

setLayout(null);

// button for color image
b = new Button("Color");
b.setBounds(10,250,120,30);
b.addActionListener(this);
add(b);

// button for grayscale image
b = new Button("GrayScale");
b.setBounds(190,250,120,30);
b.addActionListener(this);
add(b);

		
setSize(321,290);
setVisible(true);

// the window features
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}

// the function for action performed
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Color"))	flag = true;
if (e.getActionCommand().equals("GrayScale"))flag = false;
}
// for converting color image into grayscale
public void grayscale()
{
for (int i = 0; i < w*h ;i++ )
{
int temp = pixels[i];
int r = (temp>>16)&0xff;
int g = (temp>>8)&0xff;
int b = (temp)&0xff;
int k = (int)(0.56*g+0.11*r+0.33*b);
pixels[i] = (0xff000000|k<<16|k<<8|k);
}
}

// the main run() fuction
public void run()
{
while(true)
{
int n = 0,c = 0,r = 0,g = 0,b = 0,kount = 0,x = 0,y=0;
while (true)
{
// read the values RGB and count
try
{
r =dis.read();
g =dis.read();
b =dis.read();
kount =dis.read();
}
catch(Exception e){}
				
//for next line
if((r==0)&&(kount==0)&&(g==0)&&(b==0))
{
y++;
if(y==240)break;				
}
else
{
for(int i = 0;i<kount;i++)
{
x=x%(w*h);
pixels[x] = (255<<24)|(r<<16)|(g<<8)|b;
x++;
}					
}

// image not there break
if((r==-1)||(g==-1)||(b==-1))	break;
}

if (flag) // color image 
{
updateImage();repaint();
}
else // grayscale
{
grayscale();
updateImage();repaint();
}
}
}

public void updateImage()
{
imsource.newPixels(0,0,w,h);
}

// for udating the image
public void update(Graphics g)
{
g.drawImage(img,0,0,null);
}

// painting the image
public void paint(Graphics g)
{
update(g);
}

// main
public static void main(String[] args) 
{
System.out.println("Hello World!");
ColCamera a = new ColCamera();
try{Thread t  = new Thread(a);t.start();}catch(Exception e){}	
}
}
