Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
Invert.java
TheInvertfilter is also quite simple. It takes apart the red, green, and blue channels and
then inverts them by subtracting them from 255. These inverted values are packed back
into a pixel value and returned.

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

class Invert extends RGBImageFilter implements PlugInFilter {
public Image filter(Applet a, Image in) {
return a.createImage(new FilteredImageSource(in.getSource(), this));
}
public int filterRGB(int x, int y, int rgb) {
int r = 0xff - (rgb >> 16) & 0xff;
int g = 0xff - (rgb >> 8) & 0xff;
int b = 0xff - rgb & 0xff;
return (0xff000000 | r << 16 | g << 8 | b);
}
}

Figure 25-8 shows the image after it has been run through theInvertfilter.

776 Part II: The Java Library


FIGURE 25-8

Using theInvert
filter with
ImageFilterDemo
Free download pdf