img
ImageFilter
Given the ImageProducer and ImageConsumer interface pair--and their concrete classes
MemoryImageSource and PixelGrabber--you can create an arbitrary set of translation filters
that takes a source of pixels, modifies them, and passes them on to an arbitrary consumer.
This mechanism is analogous to the way concrete classes are created from the abstract I/O
classes InputStream, OutputStream, Reader, and Writer (described in Chapter 19). This
stream model for images is completed by the introduction of the ImageFilter class. Some
subclasses of ImageFilter in the java.awt.image package are AreaAveragingScaleFilter,
CropImageFilter, ReplicateScaleFilter, and RGBImageFilter. There is also an implementation
of ImageProducer called FilteredImageSource, which takes an arbitrary ImageFilter and wraps
it around an ImageProducer to filter the pixels it produces. An instance of FilteredImageSource
can be used as an ImageProducer in calls to createImage( ), in much the same way that
BufferedInputStreams can be passed off as InputStreams.
In this chapter, we examine two filters: CropImageFilter and RGBImageFilter.
CropImageFilter
CropImageFilter filters an image source to extract a rectangular region. One situation in which
this filter is valuable is where you want to use several small images from a single, larger source
image. Loading twenty 2K images takes much longer than loading a single 40K image that
has many frames of an animation tiled into it. If every subimage is the same size, then you
can easily extract these images by using CropImageFilter to disassemble the block once
your applet starts. Here is an example that creates 16 images taken from a single image.
The tiles are then scrambled by swapping a random pair from the 16 images 32 times.
/*
* <applet code=TileImage.class width=288 height=399>
* <param name=img value=picasso.jpg>
* </applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
public class TileImage extends Applet {
Image img;
Image cell[] = new Image[4*4];
int iw, ih;
int tw, th;
public void init() {
try {
img = getImage(getDocumentBase(), getParameter("img"));
MediaTracker t = new MediaTracker(this);
t.addImage(img, 0);
t.waitForID(0);
iw = img.getWidth(null);
ih = img.getHeight(null);
tw = iw / 4;
th = ih / 4;
CropImageFilter f;
FilteredImageSource fis;
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home