/* <applet code=HistoGrab.class width=600 height=600> </applet> */


import java.applet.*;
import java.applet.Applet;
import java.awt.* ;
import java.awt.Toolkit;
import java.awt.image.* ;


public class HistoGrab extends Applet {

Dimension d;
Image img;
int iw, ih;
int pixels[]=new int [1];

int w, h;
int hist[] = new int[256];
int max_hist = 0;

public void init() {
d = getSize();
w = d.width;
h = d.height;

try {

Toolkit tk = Toolkit.getDefaultToolkit(); // Binds the Compontes of dyanmic Changes
// img = getImage(getDocumentBase(), getParameter("c:/ss.Bitmap Image"));

img = getImage ("c:/ss.JPEG");

MediaTracker t = new MediaTracker(this); // Keeps the Status of components


t.addImage(img, 0); // Passing image obj. & index

t.waitForID(0); // Method wait for all the images to be loaded

t.isErrorID(0); // Returns error if any in loading ID 0

iw = img.getWidth(null); // Media observer waiting for img to be Loaded & image width or -1 if not .
ih = img.getHeight(null);
pixels[] = new int[iw * ih];

PixelGrabber pg = new PixelGrabber(img, 0, 0, iw, ih,pixels, 0, iw);
pg.grabPixels();

for(int i:pixels.lenght())
System.out.println( pixels[i] );

} catch (InterruptedException e) { };


for (int i=0;i<iw*ih;i++ ) {

int p = pixels[i];
int r = 0xff & (p >> 16);
int g = 0xff & (p >> 8);
int b = 0xff & (p);
int y = (int) (.33 * r + .56 * g + .11 * b); // Gray Scale conversition
hist[y]++;
}

for (int i=0; i<256; i++) { // Binarization
if (hist[i] > max_hist)
max_hist = hist[i];

}
}

// public void update() {}

public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
int x = (w - 256) / 2;
int lasty = h - h * hist[0] / max_hist;
for (int i=0; i<256; i++, x++) {
int y = h - h * hist[i] / max_hist;

g.setColor(new Color(i, i, i));
g.fillRect(x, y, 1, h);
g.setColor(Color.red);
g.drawLine(x-1,lasty,x,y);
lasty = y;
}
}
}

/* public static void main(String []a) {

HistoGrab g= new HistoGrab();
}
*/