Rendering Images in JAI
- Applying Rendering Hints
- Managing Memory
- Scheduling Tiles
- Reformatting an Image
- Extending the Border
- A Rendering Example
- A Closer Look at the PlanarImage Class
- Using the RenderedOp Class
- Working with Tiles
- A Tiled-Image Viewer
- Writing to Pixels
- Creating an Aggregate Image
- A JAI Image Browser
- The Renderable Layer
- Conclusion
In Chapter 10 we introduced JAI image rendering through a couple of examples. In this chapter we'll discuss this topic in greater detail. Although the underlying device-rendering model is the same, rendering JAI images is often more complex than rendering AWT images or buffered images. The main reason is the underlying tiling mechanism, which enables the rendering of large images.
As mentioned in Chapter 10, JAI has two types of rendering modes: rendered and renderable. Each has its own rendering philosophy. We'll look at both of these layers from the JAI perspective by explaining the relevant API and then giving some examples.
Before you render a JAI image, you may need to perform an operation that enables smooth and proper rendering. This operation may involve passing some rendering hints, allocating enough memory for tiles, or even restructuring the image itself. Let's look at some of these operations before we delve into the rendering process itself.
Applying Rendering Hints
The rendering-hints concept was introduced in Java 2D (see Chapter 5). The java.awt.RenderingHints class represents rendering hints. In this representation, each rendering hint is a key-value pair. Rendering-related methods of the Graphics2D and BufferedImageOp classes take RenderingHints as an input. When there are multiple rendering hints, they are passed to these methods as the Map object of a key-value pair. The rendering-hints key is of type RenderingHints.Key, and the value is an object.
JAI doesn't use the Java 2D rendering hints for rendering a node. Instead it uses its own rendering hints, the keys for which are defined in the JAI class. A rendering-hint key in JAI is of type JAI.RenderingKey, which is an inner class of JAI and a subclass of java.awt.RenderingHints.Key.
TABLE 11.1 Rendering Hints in JAI
Key |
Value |
Comments |
KEY_BORDER_EXTENDER |
Objects created from BorderExtender and its subclasses |
There are five border types: zero fill, constant fill, copy, reflection, and wrap. |
KEY_TILE_CACHE |
TileCache object |
This key controls the amount of memory allotted for caching tiles. |
KEY_OPERATION_BOUND |
One of OpImage.OP_COMPUTE_ BOUND, OpImage.OP_IO_ BOUND, or OpImage.OP_ NETWORK_BOUND |
This key indicates whether an operation is computation-, I/O-, or network-bound. |
KEY_OPERATION_REGISTRY |
OperationRegistry object |
With this key you can choose an operation registry other than the default. |
KEY_INTERPOLATION |
Objects created from the Interpolation class |
JAI implements its own interpolation classes. There are four types of interpolation: nearest neighbor, bilinear, bicubic, and bicubic2 (see Chapters 7 and 12). |
KEY_IMAGE_LAYOUT |
ImageLayout object |
This key indicates the tile layout of a JAI image. |
JAI rendering hints are typically passed to an operator that creates a node in the rendering chain. The rendering hints are applied when a node is evaluated—that is, when the image is ready for rendering.
We obtain the value for a JAI rendering hint by creating an instance from an appropriate class in JAI. Table 11.1 lists the JAI rendering-hint keys and their corresponding values.
Let's look at some prerendering operations, some of which are rendering hints.