Home > Articles

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

AffineTransformOp

An AffineTransformOp is a geometry filter. It does not work on the actual color of the pixels but on the shape of the picture. As its name suggests, however, it is not meant to perform any kind of geometry transformation. Instead, it is limited to linear mapping from 2D coordinates in the source image to 2D coordinates in the destination image.

This kind of filter is created with an AffineTransform instance, which you should be familiar with if you have worked with the Graphics2D class (this class is also discussed in Chapter 3, "Graphics Fundamentals"). An AffineTransform can be used to rotate, scale, translate, and shear objects in a 2D space.

The following code illustrates how to divide the size of an image by two using an AffineTransformOp:

BufferedImage dstImage = null;
AffineTransform transform =
    AffineTransform.getScaleInstance(0.5, 0.5);
AffineTransformOp op = new AffineTransformOp(transform,
    AffineTransformOp.TYPE_BILINEAR);
dstImage = op.filter(sourceImage, null);

The AffineTransformOp constructor used in this example takes two parameters: an AffineTransform, in this case a scale operation of 50 percent on both axes, and an interpolation type, which is equivalent to the interpolation rendering hint you can find in the RenderingHints class (see Chapter 3). You can also pass a RenderingHints instance instead of the interpolation type, in which case the interpolation rendering hint will be used.

Figure 8-4 shows the result of our scaling operation.

Figure 8-4

Figure 8-4 The size of the original image is reduced by 50 percent with an AffineTransformOp.

  • + Share This
  • 🔖 Save To Your Account