Quality Image Resizing with JAI

ThumbMaster: High-Quality Thumbnails in Pure Java

Java Advanced Imaging (JAI) provides all the tools needed to implement high-performance, feature rich imaging applications in Java — with one exception. As any developer who has worked with JAI is aware, it is very difficult, if not impossible, to get high quality results when using JAI’s built-in image resizing operations. With ThumbMaster, changing a single line of code will allow you to resize photos to produce beautiful thumbnail images in pure Java — without using external tools such as Photoshop or ImageMagick.

Resized with Standard JAI

  • Wetlands, Bicubic
  • Forest, Bicubic

Resized with ThumbMaster

  • Wetlands, ThumbMaster
  • Forest, ThumbMaster

As you can see from the above samples, using ThumbMaster to resize images produces results that do not have the pixelation, aliasing, and other artifacts present even in images created using JAI’s highest quality bicubic interpolation.

Master Your Thumbnails in Minutes

ThumbMaster ships with a number of built-in helper methods that allow you to instantly begin creating your thumbnail-enabled application. For example, the following code snippet will create an image that fits inside a 150×150 pixel box while preserving the source image’s aspect ratio:

import net.devella.thumbs.ThumbMaster;

...
RenderedImage result =
    ThumbMaster.resizeImageToFit(sourceImage, 150, 150);
...
Example 1: Resizing an image using the static helper methods

That’s it! A complete list of the built-in helper methods is available in the ThumbMaster documentation.

Use ThumbMaster in an Existing JAI Application

If you prefer to use the JAI API directly, you can use ThumbMaster as a new implementation of JAI’s standard Interpolation interface. This means that your existing JAI applications can be updated to use ThumbMaster by changing as little as one line of code. The following resizing example is taken directly from the JAI Programmer’s Reference, and altered to use ThumbMaster:

import net.devella.thumbs.ThumbMasterInterpolation;

...

ParameterBlock pb = new ParameterBlock();
pb.addSource(im); // The source image
pb.add(0.2F); // The xScale
pb.add(0.2F); // The yScale
pb.add(0.0F); // The x translation
pb.add(0.0F); // The y translation
pb.add(new ThumbMasterInterpolation(0.2F)); // The interpolation

// Create the scale operation
RenderedImage result = JAI.create("scale", pb, null);

...
Example 2: Resizing an image using JAI and ThumbMaster

Here we use JAI to resize an image, and specify an instance of ThumbMasterInterpolation instead of one of the default JAI interpolations: Nearest Neighbor, Bilinear, Bicubic, etc. ThumbMaster provides new interpolation data, and JAI handles the image resizing. This means that any native acceleration provided by JAI will still be used when resising images with ThumbMaster.

© 2008 Sean Kleinjung. All Rights Reserved.