Here's a simple filter that extracts only the blue parts of an image.
import java.awt.image.*;
public class BlueFilter extends RGBImageFilter {
public BlueFilter() {
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
return rgb & 0xFF0000FF;
}
}
canFilterIndexColorModel
is set to true
because the filtering is independent of position. The bitwise and
operator, &
, is used to select only the first
eight bits and last eight bits of the rgb
value. The
transparency level is stored in the first eight bits. Thu blue
component of the pixel is stored in the last eight bits. FF is
11111111 so all blue and transparency bits are retained. The
remaining bits vanish.