Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
LookupOp can filter BufferedImages, to give various efects, as seen in the example below.
Using a LookupOp is similar the various other BufferedImageOp classes. In all cases, we can apply a filter to a source to generate a destination. The other BufferedImageOp classes are explained in further detail in these other sections of the notes (AffineTransformOp, ColorConvertOp, ConvolveOp & RescaleOp).
LookupDemo Example (Run Applet)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class LookupDemo extends JApplet
{
@Override
public void init()
{
this.setContentPane(new View());
}
public class View extends JPanel implements ItemListener
{
private short[][] colorChange;
private final String lookupNames[] =
{
"Normal", "Invert", "Invert Red",
"Invert Red and Green", "Posterized", "Threshold"
};
private final JComboBox lookupList = new JComboBox(lookupNames);
public View()
{
this.add(this.lookupList);
this.lookupList.addItemListener(this);
}
@Override
protected void paintComponent(Graphics g)
{
final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/people.jpg")).getImage();
final Graphics2D g2D = (Graphics2D) g;
if (this.colorChange == null) // no loookup filter has been applied to the image
{
g2D.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
else // a lookup filter has been applied to the image
{
// place the original file image into buffered image
final BufferedImage srcBufferedImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
final Graphics2D SrcG = srcBufferedImg.createGraphics();
SrcG.drawImage(image, 0, 0, getWidth(), getHeight(), this);
// make a new loopup filter
final LookupOp lookupOp = new LookupOp(new ShortLookupTable(0, this.colorChange), null);
// apply the lookup filter
final BufferedImage destBufferedImg = lookupOp.filter(srcBufferedImg, null);
// draw the destination buffered image onto the applet's panel
g2D.drawImage(destBufferedImg, 0, 0, getWidth(), getHeight(), this);
}
}
@Override
public void itemStateChanged(ItemEvent e)
{
final short[] normal = new short[256];
final short[] inverted = new short[256];
final short[] posterized = new short[256];
final short[] threshold = new short[256];
// make an inverted lookup table
for (int i = 0; i < 256; i++)
{
inverted[i] = (short) (255 - i);
}
// make a posterize loopup table
for (int i = 0; i < 256; i++)
{
posterized[i] = (short) (i - (i % 64)); // changing this value alters the effect
}
// make a threshold loopup table
for (int i = 0; i < 256; i++)
{
if (i < 128) // changing this value alters the effect
{
threshold[i] = (short) 0;
}
else
{
threshold[i] = (short) 255;
}
}
switch (this.lookupList.getSelectedIndex())
{
case 0:
{
this.colorChange = null;
break;
}
case 1:
{
this.colorChange = new short[][]
{
inverted, inverted, inverted
};
break;
}
case 2:
{
this.colorChange = new short[][]
{
inverted, normal, normal
};
break;
}
case 3:
{
this.colorChange = new short[][]
{
inverted, inverted, normal
};
break;
}
case 4:
{
this.colorChange = new short[][]
{
posterized, posterized, posterized
};
break;
}
case 5:
{
this.colorChange = new short[][]
{
threshold, threshold, threshold
};
break;
}
}
this.repaint();
}
}
}
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.