Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Reformatting an Image

Often an image needs to be changed before it is rendered. For example, when you load images using the JAI codec, they are not typically tiled because most formats do not support tiling. And even in formats that do support tiling (e.g., TIFF, FlashPix), images may not be properly tiled. A planar image that is not tiled is like a buffered image, and all the advantages of tiling are lost.

One solution is to tile the image in memory. The ImageLayout class lets you restructure the image into tiles. After loading an image in memory, sometimes you will want to change the image bounds, SampleModel, ColorModel, and so on. The ImageLayout class lets you do all that. The ImageLayout object is also used as a value of the RenderingHints key.

ImageLayout has several attributes related to the image to be generated, and each of these attributes has a bit mask (see Table 11.2).

The ImageLayout class has get and set methods for each of these attributes. Each bit mask can be set, unset, or checked for its state, through the following methods:

  • public ImageLayout setValid(int mask)

  • public ImageLayout unsetValid(int mask)

  • public final boolean isValid(int mask)

TABLE 11.2 ImageLayout Attributes and Associated Bit Masks

Attribute

Mask

ColorModel

COLOR_MODEL_MASK

SampleModel

SAMPLE_MODEL_MASK

height

HEIGHT_MASK

width

WIDTH_MASK

minX

MIN_X_MASK

minY

MIN_Y_MASK

tileWidth

TILE_WIDTH_MASK

tileHeight

TILE_HEIGHT_MASK

tileGridXOffset

TILE_GRID_OFFSET_X_MASK

tileGridYOffset

TILE_GRID_OFFSET_Y_MASK


  • public int getValidMask()

  • ImageLayout also has methods for setting a group of masks:

  • public ImageLayout unsetImageBounds()

  • This method unsets the bit masks associated with the minX, minY, width, and height attributes.

  • public ImageLayout unsetTileLayout()

  • This method unsets the bit masks associated with the tileGridXOffset, tileGridYOffset, tileWidth, and tileHeight attributes.

If the mask is set, the get method returns the value that was set by the ImageLayout object. If the mask is unset, the get method returns the attribute from the original rendered image.

Using the Format Operator

The TileLayout class is typically used in conjunction with the Format operator to restructure an image. The source image for the Format operator can be a rendered image or a renderable image. The tile layout is passed to the Format operator through RenderingHints. As mentioned earlier, the KEY_IMAGE_LAYOUT key in this case is defined in the JAI class itself. The value of this key is the ImageLayout object. Table 11.3 lists the parameters of the Format operator.

The example in Listing 11.1 uses the Format operator.

TABLE 11.3 Format Operator Parameters

Operator Name

Parameter

Type

Default Value

Description

Format

transferType

int

TYPE_BYTE

Data type of the output image.
The value should be one of
TYPE_BYTE,TYPE_SHORT,
TYPE_USHORT,TYPE_INT,
TYPE_FLOAT,or TYPE_DOUBLE.
These constants are defined in the
java.awt.image.DataBuffer
class.


public static RenderedOp reformatImage(PlanarImage img, Dimension tileDim) {
  int tileWidth = tileDim.width;
  int tileHeight = tileDim.height;
  ImageLayout tileLayout = new ImageLayout(img);
  tileLayout.setTileWidth(tileWidth);
  tileLayout.setTileHeight(tileHeight);

  HashMap map = new HashMap();
  map.put(JAI.KEY_IMAGE_LAYOUT, tileLayout);
  map.put(JAI.KEY_INTERPOLATION,
     Interpolation.getInstance(Interpolation.INTERP_BICUBIC));
  RenderingHints tileHints = new RenderingHints(map);

  ParameterBlock pb = new ParameterBlock();
  pb.addSource(img);
  return JAI.create("format", pb, tileHints);
}

In Listing 11.1 the ImageLayout object is created with a specified tile width and height. This object is then saved in HashMap, as is the other rendering-hint object, Interpolation. A RenderingHints object is constructed with this HashMap instance and then passed to JAI.create(). This method uses the ParameterBlock and RenderingHints parameters to execute the Format operator. The image returned by this operator has the new tile layout and other parameters set by the ImageLayout object.

Although the ParameterBlock object in this method is created for a planar image, it can be a renderable image as well.

  • + Share This
  • 🔖 Save To Your Account