Friday, March 6, 2009

PROGRAM TO ROTATE AN IMAGE BY 90 DEG>>

Here is code :

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

public class rotat extends Applet implements ActionListener
{
Image img,imag,img90,img180,img270;
int i,j,k;
int flag,iw,ih,pixels[],a[],rgb[];
int R1[],G1[],B1[];
int rotate_red[][],rotate_green[][],rotate_blue[][];

Button load,Rotate1,Rotate2,Rotate3,Rotate4;

public void init()
{
load = new Button("Load Image");
Rotate1 = new Button("2");
Rotate2 = new Button("1");
Rotate3 = new Button("3");

add(load);

add(Rotate2);
add(Rotate1);
add(Rotate3);

load.addActionListener(this);
Rotate1.addActionListener(this);
Rotate2.addActionListener(this);
Rotate3.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)
{
String source = (String)ae.getActionCommand();

if(source.equals("Load Image"))
{
img = getImage(getDocumentBase(),"real.jpg");

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);
try
{
pg.grabPixels();
}
catch(Exception e) {}

rgb = new int[iw * ih];
R1 = new int[iw * ih];
G1 = new int[iw * ih];
B1 = new int[iw * ih];

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

repaint();
}
else if(source.equals("2"))
{
for (i=0; i {
rgb[i] = pixels[i];
R1[i] = (rgb[i] >> 16) & 0xff;
G1[i] = (rgb[i] >> 8) & 0xff;
B1[i] = (rgb[i] >> 0) & 0xff;
}

rotate_red = new int[ih][iw];
rotate_green = new int[ih][iw];
rotate_blue = new int[ih][iw];

k = 0;
for(i = 0;i < ih;i++)
{
for(j = 0;j < iw;j++)
{
rotate_red[i][j] = R1[k];
rotate_green[i][j] = G1[k];
rotate_blue[i][j] = B1[k];

k++;
}
}

k = 0;
for(i = 0;i < iw;i++)
{
for(j = ih-1;j >= 0;j--)
{
R1[k] = rotate_red[j][iw-1-i];
G1[k] = rotate_green[j][iw-1-i];
B1[k] = rotate_blue[j][iw-1-i];

k++;
}
}

a = new int[iw * ih];

for(i=0;i {
if (R1[i] > 255) R1[i] = 255;
if (R1[i] < 0) R1[i] = 0;
if (G1[i] > 255) G1[i] = 255;
if (G1[i] < 0) G1[i] = 0;
if (B1[i] > 255) B1[i] = 255;
if (B1[i] < 0) B1[i] = 0;

a[i] = (rgb[i] & 0xff000000) | (R1[i]<<16) | (G1[i] << 8) | (B1[i] <<0);
}
imag = createImage(new MemoryImageSource(iw,ih,a,0,iw));

repaint();
}
else if(source.equals("1"))
{
for (i=0; i {
rgb[i] = pixels[i];
R1[i] = (rgb[i] >> 16) & 0xff;
G1[i] = (rgb[i] >> 8) & 0xff;
B1[i] = (rgb[i] >> 0) & 0xff;
}

rotate_red = new int[ih][iw];
rotate_green = new int[ih][iw];
rotate_blue = new int[ih][iw];

k = 0;
for(i = 0;i < ih;i++)
{
for(j = 0;j < iw;j++)
{
rotate_red[i][j] = R1[k];
rotate_green[i][j] = G1[k];
rotate_blue[i][j] = B1[k];

k++;
}
}

k = 0;
for(i = ih -1;i >= 0;i--)
{
for(j = iw-1;j >= 0;j--)
{
R1[k] = rotate_red[i][j];
G1[k] = rotate_green[i][j];
B1[k] = rotate_blue[i][j];

k++;
}
}

a = new int[iw * ih];

for(i=0;i {
if (R1[i] > 255) R1[i] = 255;
if (R1[i] < 0) R1[i] = 0;
if (G1[i] > 255) G1[i] = 255;
if (G1[i] < 0) G1[i] = 0;
if (B1[i] > 255) B1[i] = 255;
if (B1[i] < 0) B1[i] = 0;

a[i] = (rgb[i] & 0xff000000) | (R1[i]<<16) | (G1[i] << 8) | (B1[i] <<0);
}
imag = createImage(new MemoryImageSource(iw,ih,a,0,iw));

repaint();
}
else if(source.equals("2"))
{
for (i=0; i {
rgb[i] = pixels[i];
R1[i] = (rgb[i] >> 16) & 0xff;
G1[i] = (rgb[i] >> 8) & 0xff;
B1[i] = (rgb[i] >> 0) & 0xff;
}

rotate_red = new int[ih][iw];
rotate_green = new int[ih][iw];
rotate_blue = new int[ih][iw];

k = 0;
for(i = 0;i < ih;i++)
{
for(j = 0;j < iw;j++)
{
rotate_red[i][j] = R1[k];
rotate_green[i][j] = G1[k];
rotate_blue[i][j] = B1[k];

k++;
}
}

k = 0;
for(i = iw - 1;i >=0;i--)
{
for(j = 0;j < ih;j++)
{
R1[k] = rotate_red[i][j];
G1[k] = rotate_green[i][j];
B1[k] = rotate_blue[i][j];

k++;
}
}

a = new int[iw * ih];

for(i=0;i {
if (R1[i] > 255) R1[i] = 255;
if (R1[i] < 0) R1[i] = 0;
if (G1[i] > 255) G1[i] = 255;
if (G1[i] < 0) G1[i] = 0;
if (B1[i] > 255) B1[i] = 255;
if (B1[i] < 0) B1[i] = 0;

a[i] = (rgb[i] & 0xff000000) | (R1[i]<<16) | (G1[i] << 8) | (B1[i] <<0);
}
imag = createImage(new MemoryImageSource(iw,ih,a,0,iw));

repaint();
}

}

// public void update() {}

public void paint(Graphics g)
{
g.drawImage(img,20,50,this);
g.drawImage(imag,400,50,this);

}

}

No comments:

Post a Comment