There have already been some posts about the Grails Framework on my blog, so here’s one about the ImageTools plugin. This plugin leverages some functionality of the Java Advanced Imaging API (JAI) to make handling images a lot easier for Grails developers. However, some people seem to be complaining about poor image quality – I guess they’re about the poor resizing-results when using the thumbnail functionality.
The default method for creating thumbnails is the following
def imageTool = new ImageTool()imageTool.load("/path/to/image.jpg")imageTool.thumbnail(640)
This will load the file, create a new ImageTool instance and make a thumbnail out of it, where the max width/height will be 640px. However the resizing quality is very bad because by default it uses nearest neighbor for interpolation. You will encounter this especially when you’re rendering small thumbnails out of large images.
A possible solution
Because there is no detailed documentation about the ImageTools plugin (actually I didn’t find any) one will have to inspect the source code to find something very cool: ImageTools also provides a method called “thumbnailSpecial” which can use other interpolation and rendering algorithms and you can use it instead of the basic “thumbail()”. It’s signature is as follows
thumbnailSpecial(float maxWidth,
float maxHeight,
int interPolationType,
int renderingType)
maxWidth and maxWidth are pretty self-explaining, just set them to the same values (for example 640) to achieve the same dimensions as above.
interPolationType is one of
1: “bilinear interpolation” – which is at least better than nearest
2: “bicubic interpolation” – which is even better and
3: “bicubic2 interpolation” – which may even better
these are JAI names for them, don’t ask me about the details
renderingType is one of
1: “scale” – the default function JAI uses
2: “SubsampleAverage” – which will provide better results
Please keep in mind that all the “better results” require a lot more rendering time and are likely to consume more memory – but the results will be way smoother then.
So that may help some people out there which have experienced poor quality at this end.
For me there are only two further caveats when using ImageTools/JAI.
First: performance is very bad when using pure Java mode (which will be the default unless you provide a “native accelerator” for JAI). On my Mac this is no problem – as far as I know Apple provides a native accelerator in its own JDK on Mac OS X (and I never got messages like detailed on the plugin page) – yeah, using CoreGraphics/CoreImage, whatever… correct me if I’m wrong ;-) but I never got it to run on my production system (Ubuntu Server).
Second: JAI seems to have some other strange problems with JPEGs on Linux (I tried both OpenJDK and Sun’s JDK, always JAI in pure Java mode): some JPEGs will have inverted colors in the resulting thumbnail – couldn’t find out any details but I think something about the JPEG implementation is broken. This also affects saving JPEGs for me. It got strange exceptions when saving images as JPEGs causing that to fail – so I fell back to using PNGs – which is everything but cool in many cases :-(
As said these two problems only occurred on Linux (Ubuntu 9.04) – not on Mac OS X (couldn’t test on Windows) – but since most web apps I’m writing will be running on Linux some day this is a very annoying problem – and unfortunately I don’t see any progress on the ImageTools plugin or JAI. Anybody else got this kind of problem?
So I’m very tended to give something like good old ImageMagick a try – maybe using im4java or JMagick – or something else.