Friday, March 6, 2009

JAVA PROGRAM FOR NEGATIVE OF AN IMAGE

This is java program to convert image
to its corresponding negative image.
compile below programme in jcreator le,pro.

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

public class sunneg extends Applet
{
Dimension d;

int iw, ih;

int w, h1,y;
int x,i,j,temp,bnum;


Image img,imag;
int pixels[],a[],b[],c[],arr[],z[],h[];

int hist[] = new int[256];
int hc[] = new int[256];
int max_hist = 0;

public void init()
{
d = getSize();
w = d.width;
h1 = d.height;
y=0;
try{
img = getImage(getDocumentBase(),"real.jpg");
MediaTracker t = new MediaTracker(this);
t.addImage(img, 0);
t.waitForID(0);
iw = img.getWidth(null);
ih = img.getHeight(null);
pixels = new int[iw * ih];
PixelGrabber pg = new PixelGrabber(img, 0, 0, iw, ih,pixels, 0, iw);
pg.grabPixels();
}catch(InterruptedException e){};

int x=0,y=0;
int a[]= new int[iw*ih];
for(int i=0;i<(iw*ih);i++)
{
int rgb = pixels[i];
rgb =(int)(255-rgb);
//int r = 0xff - (rgb >> 16) & 0xff;
//int g = 0xff - (rgb >> 8) & 0xff;
//int b = 0xff - rgb & 0xff;
//int k = (int) (.56 * g + .33 * r + .11 * b);
//int r= rgb>>16;
/*int g= rgb>>8;
int b=rgb;
r= 255-r;
g=255-g;
b=255-b;
//a[i] = (0xff000000 | r<< 16 | g << 8 | b);
a[i] = ( r<< 16 | g << 8 | b);*/
a[i]=(int)(255-rgb);
int r = 0xff - (rgb >> 16) & 0xff;
int g = 0xff - (rgb >> 8) & 0xff;
int b = 0xff - rgb & 0xff;
r = (int)(255-r);
g = (int)(255-g);
b = (int)(255-b);
int k = (int) (.56 * g + .33 * r + .11 * b);
a[i] = (0xff000000 | r<< 16 | g << 8 | b);


}
img = createImage(new MemoryImageSource(iw,ih,a,0,iw));
}

public void paint(Graphics g)
{
g.drawImage(img, 0, 0, null);
}
}

and then run html code in jcreator:

No comments:

Post a Comment