如何缩放从 PNG 加载的透明 org.eclipse.swt.graphics.Image; Java

How to scale a transparent org.eclipse.swt.graphics.Image, loaded from a PNG; Java

我有一个 org.eclipse.swt.graphics.Image,从 PNG 文件加载,并希望以高质量(抗锯齿、插值)对其进行缩放。但我不想失去透明度而只获得白色背景。 (我需要这张图片将其放在 org.eclipse.swt.widgets.Label 上。)

有人知道怎么做吗? 谢谢!

这里使用 Sean Bright 描述的方法:,我们可以从图像中提取 alpha 信息并用它来填充负责透明度的 ImageData.alphaData 数组:

public static Image resizeImage(Display display, Image image, int width, int height) {

    Image scaled = new Image(display, 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();

    ImageData canvasData = scaled.getImageData();
    canvasData.alphaData = new byte[width * height];

    // This is the hacky bit that is making assumptions about
    // the underlying ImageData. In my case it is 32 bit data
    // so every 4th byte in the data array is the alpha for that
    // pixel...
    for (int idx = 0; idx < (width * height); idx++) {
        int coord = (idx * 4) + 3;
        canvasData.alphaData[idx] = canvasData.data[coord];
    }

    // Now that we've set the alphaData, we can create our
    // final image
    Image finalImage = new Image(display, canvasData);

    scaled.dispose();

    return finalImage;
}

请注意,此方法假定您使用的是 32 位色深;否则它不会工作。

基于Mark's answer我找到了一个没有“hacky bit”的更好的解决方案:首先从原点复制alphaData然后使用GC缩放图像。

public static Image scaleImage(final Device device, final Image orig, final int scaledWidth, final int scaledHeight) {     
    final Rectangle origBounds = orig.getBounds();     
    if (origBounds.width == scaledWidth && origBounds.height == scaledHeight) {     
        return orig;     
    }     

    final ImageData origData = orig.getImageData();     
    final ImageData destData = new ImageData(scaledWidth, scaledHeight, origData.depth, origData.palette);     
    if (origData.alphaData != null) {     
        destData.alphaData = new byte[destData.width * destData.height];     
        for (int destRow = 0; destRow < destData.height; destRow++) {     
            for (int destCol = 0; destCol < destData.width; destCol++) {     
                final int origRow = destRow * origData.height / destData.height;     
                final int origCol = destCol * origData.width / destData.width;     
                final int o = origRow * origData.width + origCol;     
                final int d = destRow * destData.width + destCol;     
                destData.alphaData[d] = origData.alphaData[o];     
            }     
        }     
    }     

    final Image dest = new Image(device, destData);     

    final GC gc = new GC(dest);     
    gc.setAntialias(SWT.ON);     
    gc.setInterpolation(SWT.HIGH);     
    gc.drawImage(orig, 0, 0, origBounds.width, origBounds.height, 0, 0, scaledWidth, scaledHeight);     
    gc.dispose();

    return dest;
}

这样我们就不必对底层 ImageData 做出假设。