|
Using Rasters for Image Processing, Part 1
by Anghel Leonard
Many modern image technologies are based on raster concept things like televisions, photo cameras, or printers. They provide superior image control, allowing you to process an entire image or only a piece of it, and full control over pixel's colors. You're free to develop new image filters, or allow images to mix. Basically, having a raster is like having a "playground" because you are free to try whatever you want to do with the image.
This article discusses rasters from the Java perspective. You'll find out what the components of a raster are in Java and how to exploit them in an application. You'll learn how to store, extract, and manipulate image samples, and then learn how how to extract a color band and how to create a zoom effect.
Creating a Raster
A raster is a rectangular area of pixels from an image or a piece of an image. Java rasters are representated by the java.awt.image.Raster class and its subclass. You create a raster using these classes or obtain one from an Image object, BufferedImage object or from an array of pixels. To store information about the pixels of a raster you use data buffers that represent instances of the direct DataBuffers subclasses. The SampleModel class and its subclasses manage the rules for extracting and storing pixels into the data buffers. These classes contain enough methods for providing to the programmer a large flexibility in working with rasters.
 Figure 1. Creating a Raster: A raster contains a DataBuffer and a SampleModel.
The DataBuffer and SampleModel Classes
To understand how theseDataBuffer and SampleModel work together to create a raster, it is important to be familiar with a few concepts. Suppose you have an image in the default color model (the AlphaRedGreenBlue color model). A pixel's components are categorized as "samples". Samples are defined as "bands." In this example, the pixel has four samples, represented by the four color components. This means that the image has four bands: red, green, blue, and transparency. The red band contains all the red samples of the image, the green one all the green samples, etc.
Remember that this is just an example. Not all images have four bands and not all images have these same bands. For example, a multi-spectral image has many bands, whereas an image represented by the elevation model will have only one.
 Figure 2. An Image Band: A possible red band fragment.
The abstract class SampleModel contains methods for extracting a pixel's samples, for storing them in data buffers, and for extracting them from data buffers. To store the samples in DataBuffers, you use arrays of primitive data types. In fact, a DataBuffer object encapsulates one or more arrays of primitive data types. Generally, each array in the data buffer is in fact, a block of data that has a specified dimension. The first block is known as block 0, the second is block 1, etc. When no block is specified block 0 is used, by default.
To create a DataBuffer, create an instance of one of the following subclasses:
- java.awt.image.DataBufferByte: the data is represented in the unsigned byte primitive type.
- java.awt.image.DataBufferShort: the data is represented in the short primitive type.
- java.awt.image.DataBufferUShort: the data is represented in the unsigned short primitive type.
- java.awt.image.DataBufferInt: the data is represented in the int primitive type.
- java.awt.image.DataBufferFloat: the data is represented in the float primitive type.
- java.awt.image.DataBufferDouble: the data is represented in the double primitive type.
There are two major ways to store samples in a DataBuffer:
The first way to store samples uses the ComponentSampleModel class (which is a direct subclass of SampleModel) and the two ComponentSampleModel's subclasses: PixelInterleavedSampleModel and BandedSampleModel. More bands can be in the same block or in separated blocks.
If you want to store one sample per DataBuffer element, and the samples of all bands to be in only one block, use the PixelInterleavedSampleModel class. If you want to store the samples for every band into a separate block, use the BandedSampleModel class. These two classes work with all the above listed DataBufferXXX classes.
 Figure 3. Storing Samples in a DataBuffer: This image shows the different models for storing samples in a DataBuffer.
The second way to store samples uses the SinglePixelPackedSampleModel and MultiPixelPackedSampleModel classes (both are subclasses of SampleModel). To store all the samples of one pixel per one DataBuffer element, use the SinglePixelPackedSampleModel class. In this case, the samples are stored into the first block. To store samples from different pixels per one DataBuffer element, use the MultiPixelPackedSampleModel class. These two classes work only with the DataBufferByte (8 bits-1 byte), DataBufferUShort (16 bits-2 bytes), and DataBufferInt (32 bits-4 bytes) classes.
 Figure 4. Using SinglePixelPackedSampleModel: Store all the samples of one pixel per one DataBuffer element using SinglePixelPackedSampleModel.
Remember that DataBuffer's blocks are counting from 0 to n-1. The bands stored into a DataBuffer are also counting from 0 to n-1.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|