Eric Bergman-Terrell's Blog

Java Programming Tip: How to Speed Up SWT Image Scaling
March 19, 2009

In the past, when I ran Vault 3 on my Acer Aspire One netbook, rendering photos was too slow to be usable. Additionally the images, once they finally did render, where aliased. In other words they had the "jaggies". I've ignored this issue for a while, since I figured the Acer's fingernail-sized Intel Atom CPU was simply too slow to handle images efficiently. But a few days ago I noticed that photo rendering was slow and ugly when I ran Vault 3 on my desktop computer in a Ubuntu VM.

I experimented with image scaling using the Java 2D API with slightly frustrating results. Although images scaled in a reasonable time with reasonable quality, the results were not as good as the original code provided under Windows, even to my presbyopic eyes. I briefly considered using the Java 2D API to render images on Linux, and using SWT image rendering on Windows, but maintaining distinct code bases for different platforms did not appeal to me.

Today I experimented with the SWT image scaling code and learned something very interesting. When running on Windows, graphics run in "advanced mode", which offloads image processing to the graphics card, for improved performance. Modern graphics cards are better at image processing than general-purpose CPUs are. When running on Linux (Ubuntu) in a virtual machine, the graphics mode defaulted to "advanced mode", but with slow and ugly results. Turning off advanced mode yielded attractive results much more quickly. Turning off advanced graphics on my Ubuntu VM, and on my Acer Aspire One, resulted in at least a ten-fold performance increase, along with substantially higher image quality. I'll take fast and attractive over slow and ugly every time!

...
GC gc = new GC(scaled);

try {
    gc.setAntialias(SWT.ON);
    gc.setInterpolation(SWT.HIGH);

    Globals.getLogger().info(String.format("initial advanced value: %s", gc.getAdvanced()));

    gc.setAdvanced(Globals.getPreferenceStore().getBoolean(PreferenceKeys.AdvancedGraphics));

    Globals.getLogger().info(String.format("begin drawImage: anti-aliasing: %d interpolation: %d advanced: %s", gc.getAntialias(), gc.getInterpolation(), gc.getAdvanced())); 

    startTime = System.nanoTime();

    gc.drawImage(originalImage, 0, 0, originalImage.getBounds().width, originalImage.getBounds().height, 0, 0, scaleWidth, scaleHeight);

    Globals.getLogger().info(String.format("end drawImage, ms: %d", (System.nanoTime() - startTime) / 1000000));
}
finally {
    gc.dispose();
}
...
Keywords: SWT, Image Scaling, Graphics, Windows, Linux, Ubuntu, Anti-Aliasing, Jaggies, Intel Atom, Acer Aspire One, Java, Java 2d API, Vault 3

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
.NET Public-Key (Asymmetric) Cryptography DemoJuly 20, 2025
Raspberry Pi 3B+ Photo FrameJune 17, 2025
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021