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
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015