TUTORIAL
 
Introduction

ImageResize4J offers 24 interpolation filters for high-quality image resize and scaling. Interpolation filter selection depends on a current task: the fastest interpolation filter is the NEAREST_NEIGHBOR and the most qualitative are IDEAL filter families and SHARP (SHARP_LIGHT/SHARP_MORE) filter families. Other interpolation filters are the balance between speed & quality.

! SHARP (SHARP_LIGHT/SHARP_MORE) filter families are the most appropriate solution for producing sharp and nice-looking photo thumbnails.

! Interpolation filters with more length are more qualitative but slower.

 

 
Java2D-based Image Scaling
//source image
BufferedImage sourceImage = ...;

//Create an instance of ImageResizeProcessor class
//with the "Sharp Light" interpolation filter of length=5
ImageResizeProcessor processor =
        new ImageResizeProcessor(ImageResizeProcessor.TYPE_SHARP_LIGHT_5);

//scale BufferedImage by factor = 0.2 with maintaining the aspect ratio
//of the original image dimension
BufferedImage scaledImage_1 = processor.scale(sourceImage, 0.2f, -1);

//scale BufferedImage by X-axis scale factor = 0.8 
//and Y-axis scale factor = 0.6
BufferedImage scaledImage_2 = processor.scale(sourceImage, 0.8f0.6f);
TOP
 
Java2D-based Image Resize
//source image
BufferedImage sourceImage = ...;

//Create an instance of ImageResizeProcessor class
//with the "Sharp Light" interpolation filter of length=5
ImageResizeProcessor processor =
        new ImageResizeProcessor(ImageResizeProcessor.TYPE_SHARP_LIGHT_5);

//resize BufferedImage to the width=300px with maintaining
//the aspect ratio of the original image dimension
BufferedImage resizedImage_1 = processor.resize(sourceImage, 300, -1);

//resize BufferedImage to the width=300px and height=200px
BufferedImage resizedImage_2 = processor.resize(sourceImage, 300200);
TOP
 
JAI-based Image Scaling
//source image
PlanarImage sourceImage = ...;

//scale PlanarImage by factor 0.2 with maintaining the aspect ratio
//of the original image dimension
//using "Sharp Light" interpolation filter of length=5
RenderedOp renderedOp_1 =
        ImprovedScaleDescriptor.create(sourceImage,
                new Float(0.2f), null,
                ImprovedScaleInterpolation.SHARP_LIGHT_5, null);

//scale PlanarImage by X-axis scale factor = 0.8
//and Y-axis scale factor = 0.6
//using "Sharp Light" interpolation filter of length=5
RenderedOp renderedOp_2 =
        ImprovedScaleDescriptor.create(sourceImage,
                new Float(0.8f)new Float(0.6f),
                ImprovedScaleInterpolation.SHARP_LIGHT_5, null);
TOP
 
JAI-based Image Resize
//source image
PlanarImage sourceImage = ...;
        
//resize PlanarImage to the width=300px with maintaining
//the aspect ratio of the original image dimension
//using "Sharp Light" interpolation filter of length=5
RenderedOp renderedOp_1 =
        ImprovedScaleDescriptor.createResize(sourceImage,
                new Integer(300), null,
                ImprovedScaleInterpolation.SHARP_LIGHT_5, null);
        
//resize PlanarImage to the width=300px and height=200px
//using "Sharp Light" interpolation filter of length=5
RenderedOp renderedOp_2 =
        ImprovedScaleDescriptor.createResize(sourceImage,
                new Integer(300)new Integer(200),
                ImprovedScaleInterpolation.SHARP_LIGHT_5, null);        
TOP
 
Performance Tips

To produce high-quality thumbnails from large images with an appropriate speed s.c. "two phase" approach may be used:

  • First Phase generates an intermediate image using the faster interpolation filter likes NEAREST_NEIGHBOR;
  • Second Phase generates one-or-more high quality thumbnails using the more qualitative interpolation filters like IDEAL or SHARP (SHARP_LIGHT/SHARP_MORE) families;

The below results demonstrate the advantage of such approach (example source code, source image):

1-Phase Java2D Bicubic 1-Phase IR4J Nearest Neighbor 1-Phase IR4J Sharp 5 2-Phase IR4J Nearest Neighbor/Sharp 5
72x96 thumbnails

Average processing: 1014ms

Average processing: 128ms

Average processing: 1140ms

Average processing: 139ms
96x128 thumbnails

Average processing: 1012ms

Average processing: 121ms

Average processing: 1107ms

Average processing: 169ms
120x160 thumbnails

Average processing: 1114ms

Average processing: 119ms

Average processing: 1014ms

Average processing: 202ms