Java Reference
In-Depth Information
Example 11•13: ImageOps.java (continued)
public String getName() {return "Image Processing";}// From GraphicsExample
public int getWidth() { return WIDTH; }
// From GraphicsExample
public int getHeight() { return HEIGHT; }
// From GraphicsExample
Image image;
/** This constructor loads the image we will manipulate */
public ImageOps() {
java.net.URL imageurl = this.getClass().getResource("cover.gif");
image = new javax.swing.ImageIcon(imageurl).getImage();
}
// These arrays of bytes are used by the LookupImageOp image filters below
static byte[] brightenTable = new byte[256];
static byte[] thresholdTable = new byte[256];
static { // Initialize the arrays
for(int i = 0; i < 256; i++) {
brightenTable[i] = (byte)(Math.sqrt(i/255.0)*255);
thresholdTable[i] = (byte)((i < 225)?0:i);
}
}
// This AffineTransform is used by one of the image filters below
static AffineTransform mirrorTransform;
static { // Create and initialize the AffineTransform
mirrorTransform = AffineTransform.getTranslateInstance(127, 0);
mirrorTransform.scale(-1.0, 1.0); // flip horizontally
}
// These are the labels we'll display for each of the filtered images
static String[] filterNames = new String[] {
"Original", "Gray Scale", "Negative", "Brighten (linear)",
"Brighten (sqrt)", "Threshold", "Blur", "Sharpen",
"Edge Detect", "Mirror", "Rotate (center)", "Rotate (lower left)"
};
// The following BufferedImageOp image filter objects perform
// different types of image processing operations.
static BufferedImageOp[] filters = new BufferedImageOp[] {
// 1) No filter here. We'll display the original image
null,
// 2) Convert to Grayscale color space
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null),
// 3) Image negative. Multiply each color value by -1.0 and add 255
new RescaleOp(-1.0f, 255f, null),
// 4) Brighten using a linear formula that increases all color values
new RescaleOp(1.25f, 0, null),
// 5) Brighten using the lookup table defined above
new LookupOp(new ByteLookupTable(0, brightenTable), null),
// 6) Threshold using the lookup table defined above
new LookupOp(new ByteLookupTable(0, thresholdTable), null),
// 7) Blur by "convolving" the image with a matrix
new ConvolveOp(new Kernel(3, 3, new float[] {
.1111f,.1111f,.1111f,
.1111f,.1111f,.1111f,
.1111f,.1111f,.1111f,})),
// 8) Sharpen by using a different matrix
new ConvolveOp(new Kernel(3, 3, new float[] {
Search WWH ::




Custom Search