High quality image rescaler

I’ve made Java port of the imageresampler library for high quality image rescaling. I’m using it for generating mipmaps as the simple box filter is too low on quality (especially for textures with obvious patterns).

Here it is: Resampler.java

Example usage:


BufferedImage img = ImageIO.read(new File("original.png"));
img = Resampler.rescale(img, width, height, gamma, 1.0f, new Resampler.LanczosFilter(4)/*BoxFilter()*/, BoundaryOp.WRAP);
ImageIO.write(img, "png", new File("scaled.png"));

You can also use the contructor for more custom usage including stream processing of big images.

Nice! Thanks for sharing.

I just use this code

public static BufferedImage getScaledImage(BufferedImage image, int targetWidth,
                                               int targetHeight, boolean highQuality) {
        int accWidth, accHeight;
        if (highQuality) {
            accHeight = image.getHeight();
            accWidth = image.getWidth();
        } else {
            accHeight = targetHeight;
            accWidth = targetWidth;
        }

        BufferedImage result = image;
        do {
            if (highQuality) {
                accHeight = Math.max(accHeight / 2, targetHeight);
                accWidth = Math.max(accWidth / 2, targetWidth);
            }
            BufferedImage tmp = new BufferedImage(accWidth, accHeight, image.getType());

            Graphics2D g2 = tmp.createGraphics();

            if (highQuality) {
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            } else {
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                    RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
            }
            g2.drawImage(result, 0, 0, accWidth, accHeight, null);

            result = tmp;
        } while (accHeight != targetHeight && accWidth != targetWidth);

        return result;
    }

don’t know where I found it but it was from some nice blog post about the problems of scaling images in Java.
It gives you bicubic interpolation and I never had any quality roblems with it