SWT: How to do High Quality Image Resize
Asked Answered
A

3

8

My application needs to resize ImageData. Unfortunately, I haven't gotten the results I want with GC (with antialias on and interpolation on high), or ImageData.scaledTo(). The resulting image is of too low a quality to be acceptable. What's the best way to do a high quality ImageData resize?

Edit: I'm scaling down.

Acrimony answered 20/1, 2011 at 21:22 Comment(1)
Ah yes, forgot to specify. Down.Acrimony
E
11

AWT offers image scaling with different modes. For downscaling, Area Averaging offers the best quality. One could of course re-implement the area averaging scaling algorithm for SWT, or, for a quick solution which often suffices:

  • convert the SWT image to an AWT image
  • rescale it using an appropriate mode (area averaging if downscaling, bilinerar otherwise)
  • convert it back to an SWT image

The code to convert between SWT and AWT images can be found here.

/**
 * Resizes an image, using the given scaling factor. Constructs a new image resource, please take care of resource
 * disposal if you no longer need the original one. This method is optimized for quality, not for speed.
 * 
 * @param image source image
 * @param scale scale factor (<1 = downscaling, >1 = upscaling)
 * @return scaled image
 */
public static org.eclipse.swt.graphics.Image resize (org.eclipse.swt.graphics.Image image, float scale) {
    int w = image.getBounds().width;
    int h = image.getBounds().height;

    // convert to buffered image
    BufferedImage img = convertToAWT(image.getImageData());

    // resize buffered image
    int newWidth = Math.round(scale * w);
    int newHeight = Math.round(scale * h);

    // determine scaling mode for best result: if downsizing, use area averaging, if upsizing, use smooth scaling
    // (usually bilinear).
    int mode = scale < 1 ? BufferedImage.SCALE_AREA_AVERAGING : BufferedImage.SCALE_SMOOTH;
    java.awt.Image scaledImage = img.getScaledInstance(newWidth, newHeight, mode);

    // convert the scaled image back to a buffered image
    img = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
    img.getGraphics().drawImage(scaledImage, 0, 0, null);

    // reconstruct swt image
    ImageData imageData = convertToSWT(img);
    return new org.eclipse.swt.graphics.Image(Display.getDefault(), imageData);
}

public static BufferedImage convertToAWT (ImageData data) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;
    if (palette.isDirect) {
        colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
        BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height),
            false, null);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                RGB rgb = palette.getRGB(pixel);
                pixelArray[0] = rgb.red;
                pixelArray[1] = rgb.green;
                pixelArray[2] = rgb.blue;
                raster.setPixels(x, y, 1, 1, pixelArray);
            }
        }
        return bufferedImage;
    } else {
        RGB[] rgbs = palette.getRGBs();
        byte[] red = new byte[rgbs.length];
        byte[] green = new byte[rgbs.length];
        byte[] blue = new byte[rgbs.length];
        for (int i = 0; i < rgbs.length; i++) {
            RGB rgb = rgbs[i];
            red[i] = (byte) rgb.red;
            green[i] = (byte) rgb.green;
            blue[i] = (byte) rgb.blue;
        }
        if (data.transparentPixel != -1) {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
        } else {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height),
            false, null);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                pixelArray[0] = pixel;
                raster.setPixel(x, y, pixelArray);
            }
        }
        return bufferedImage;
    }
}

public static ImageData convertToSWT (BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
                data.setPixel(x, y, pixel);
            }
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}
Extraditable answered 11/5, 2012 at 9:13 Comment(2)
That's what I ended up doing in the end.Acrimony
This appears to turn transparency to black. Can that be changed such that transparency becomes white, or better yet, is retained?Sevastopol
N
11

The accepted solution doesn't deal with transparency. Here's a snippet I've came across that does hi-quality resize and preserves transparency as well:

public static Image resize(Image image, int width, int height) {
  Image scaled = new Image(Display.getDefault(), width, height);
  GC gc = new GC(scaled);
  gc.setAntialias(SWT.ON);
  gc.setInterpolation(SWT.HIGH);
  gc.drawImage(image, 0, 0,image.getBounds().width, image.getBounds().height, 0, 0, width, height);
  gc.dispose();
  image.dispose(); // don't forget about me!
  return scaled;
}

I've found it here:

http://aniszczyk.org/2007/08/09/resizing-images-using-swt/

Nichollenicholls answered 12/9, 2016 at 11:50 Comment(0)
B
1

We've had success with ImageMagick / JMagick. http://www.jmagick.org/index.html

The only problem is that if the images are user uploads, and you have a large user base, you will have memory leaks because of invalid image files and so on.

Burr answered 21/1, 2011 at 1:9 Comment(3)
Actually, it's a desktop application so there should be no problems. So that means I'll have to distribute ImageMagick with my application?Acrimony
Yes. You'll have to distribute imageMagick as well. I'm not sure how that will be if your users are on multiple platforms. The other option is to create a webservice and have the desktop app use the remote service. I'd bet you could find a service that does this and bypass the issue entirely.Burr
Multiple platforms should be okay, it's just Windows, Linux, and Apple. I'll just make sure I get the right build for the right platform, 32-bit only. I don't think using a webservice would be a viable solution in this case: it's resizing a very large queue.Acrimony

© 2022 - 2024 — McMap. All rights reserved.