Image Processing Previos Lab Review

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

public class test2 extends Applet
{
Image pic, bwpic, greypic, redpic, greenpic, bluepic;

public void init()
{

pic = getImage(getDocumentBase(), "bear.gif");
MediaTracker t = new MediaTracker(this);
t.addImage(pic, 0);
try
{
t.waitForAll();
}
catch (Exception e)
{
}

ImageProducer p1 = pic.getSource();
BWFilter BnW = new BWFilter();
bwpic = createImage(new FilteredImageSource(p1, BnW));

ImageProducer p2 = pic.getSource();
GreyFilter grey = new GreyFilter();
greypic = createImage(new FilteredImageSource(p2, grey));

ImageProducer p3 = pic.getSource();
RedFilter red = new RedFilter();
redpic = createImage(new FilteredImageSource(p3, red));

ImageProducer p4 = pic.getSource();
GreenFilter green = new GreenFilter();
greenpic = createImage(new FilteredImageSource(p4, green));

ImageProducer p5 = pic.getSource();
BlueFilter blue = new BlueFilter();
bluepic = createImage(new FilteredImageSource(p5, blue));

}

public void paint(Graphics g)
{
g.drawImage(pic,0, 10, this);
g.drawImage(bwpic, 300, 10, this);
g.drawImage(greypic, 600, 10, this);
g.drawImage(redpic, 0, 400, this);
g.drawImage(greenpic, 300, 400, this);
g.drawImage(bluepic, 600, 400, this);
}
}


class BWFilter extends RGBImageFilter
{
public int filterRGB (int x, int y, int rgb)
{
int alpha = (rgb >> 24) & 0xff;
int red = (int) (((rgb >> 16) & 0xff) * 0.3f);
int green = (int) (((rgb >> 8) & 0xff) * 0.59f);
int blue = (int) (((rgb >> 0) & 0xff) * 0.11f);
int bws = red + green + blue;

if(bws > 127)
{
bws = 255;
}
else
{
bws = 0;
}
return ((alpha & 0xff) << 24) | ((bws & 0xff) << 16) | ((bws & 0xff) << 8) | ((bws & 0xff) << 0);
}
}

class GreyFilter extends RGBImageFilter
{

public int filterRGB (int x, int y, int rgb)
{
int alpha = (rgb >> 24) & 0xff;
int red = (int) (((rgb >> 16) & 0xff) * 0.3f);
int green = (int) (((rgb >> 8) & 0xff) * 0.59f);
int blue = (int) (((rgb >> 0) & 0xff) * 0.11f);
int bws = red + green + blue;
return ((alpha & 0xff) << 24) | ((bws & 0xff) << 16) | ((bws & 0xff) << 8) | ((bws & 0xff) << 0);
}
}

class RedFilter extends RGBImageFilter
{

public int filterRGB(int x, int y, int rgb)
{
return rgb & 0xffff0000;
}
}

class GreenFilter extends RGBImageFilter
{
public int filterRGB(int x, int y, int rgb)
{
return rgb & 0xff00ff00;
}
}

class BlueFilter extends RGBImageFilter
{

public int filterRGB(int x, int y, int rgb)
{
return rgb & 0xff0000ff;
}
}
Thanks to GCA and Siew Hao
Related Posts with Thumbnails