suraj shreshta
suraj shreshta

Reputation: 1

Image Resampling Bicubic Interpolation Java

I have resized image but its quality is low. i heard of bicubic interpolation but i cant get any implementation code. Here is my code:

private static BufferedImage resize(BufferedImage image, int width, int height) 
{ 
    int w = image.getWidth(), h = image.getHeight();
    int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType();
    BufferedImage resizedImage = new BufferedImage(width, height, type);
    Graphics2D g = resizedImage.createGraphics();
    g.setComposite(AlphaComposite.Src);
    g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    //g.drawImage(image, 0, 0, width, height, null);
    g.scale((double)width/w,(double)height/h);
    g.drawRenderedImage(image, null);
    g.dispose();
    return resizedImage; 
}   

I want to get the best quality after upsampling and downsampling.

Upvotes: 0

Views: 3327

Answers (1)

Paul R
Paul R

Reputation: 212969

Bicubic interpolation and other interpolation schemes are needed only when you need to resize by some non-integer scale factor. What you're probably missing is that you need to filter your image prior to downsampling (when shrinking) or filter after upsampling (when expanding) to avoid aliasing artefacts, which is an entirely different problem. This applies to both integer and non-integer scale factors.

Upvotes: 1

Related Questions