Skip to content

Non-blurred SVG image scaling with GC.drawImage(Image image, int destX, int destY, int destWidth, int destHeight) only works on Windows unless an ImageFileNameProvider is used #2917

@Phillipus

Description

@Phillipus
  • Eclipse 4.38

According to this:

When using the new drawImage method, images that can provide size-specific data (such as SVGs) are loaded at the destination size, avoiding blurry scaling.

However that's not the case for Mac or Linux unless an ImageFileNameProvider is used.

Run this Snippet on Windows, Mac and Linux and compare the results:

import java.nio.file.Path;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageFileNameProvider;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SVGImageTests {
    
    public static void main(String[] args) throws Exception {
        final Display display = new Display();

        final Shell shell = new Shell(display);
        shell.setText("SVG Image Test");
        shell.setLayout(new FillLayout());
        shell.setBounds(250, 50, 1000, 400);
        shell.setBackground(new Color(255, 255, 255));
        
        String imgPath = "image.svg";
        Path fullPath = Path.of(SVGImageTests.class.getResource(imgPath).toURI());
        
        // Image loaded in stream
        Image image1 = new Image(display, SVGImageTests.class.getResourceAsStream(imgPath));

        // Image loaded in file path
        Image image2 = new Image(display, fullPath.toString());
        
        // Image loaded in ImageFileNameProvider
        Image image3 = new Image(display, (ImageFileNameProvider) zoom -> {
            return zoom == 100 ? fullPath.toString() : null;
        });
        
        shell.addPaintListener(e -> {
            e.gc.drawImage(image1, 20, 10, 300, 300);
            e.gc.drawText("Image(Display, InputStream)", 80, 300);
            
            e.gc.drawImage(image2, 340, 10, 300, 300);
            e.gc.drawText("Image(Display, String)", 420, 300);
            
            e.gc.drawImage(image3, 660, 10, 300, 300);
            e.gc.drawText("Image(Display, ImageFileNameProvider)", 700, 300);
        });
        
        shell.open();

        while(!shell.isDisposed()) {
            if(!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }
}

Use this SVG image for the Snippet:

Image

Here's the outputs...

On Windows all Image methods result in sharp output:

Image

On Mac only using ImageFileNameProvider is sharp:

Image

On Linux only using ImageFileNameProvider is sharp:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions