Skip to main content

Issue with scale operator

2 replies [Last post]
avoinemt
Offline
Joined: 2008-03-30

Hi,

I use JAI to make thumbnails of images. There are 2 issues I face:

1) It is very slow (and it shouldn't): around 4 to 5 seconds per thumbnail
2) Anti-aliasing does not work

The second issue is the one that bothers me the most as my thumbnails come out with a very poor quality. Here is a shortened version of the code I use (which also performs a rotation) :

Dimension clip = ClipUtil.clip(srcImageSize, frameSize);
float heightScale = clip.height / (float)srcImageSize.height;
float widthScale = clip.width / (float)srcImageSize.width;
ParameterBlock pb = new ParameterBlock();
pb.addSource(srcImage);
pb.add(widthScale);
pb.add(heightScale);
pb.add(0.0f);
pb.add(0.0f);
pb.add(new InterpolationNearest());
RenderingHints antialising = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
PlanarImage scaledImage = JAI.create("scale", pb, antialising);

log.trace("Rotate image if necessary");
// rotate image if necessary
if(orientation == null || orientation == Orientation.TOP_LEFT){
resultImage = scaledImage.getAsBufferedImage();
} else {
pb = new ParameterBlock();
pb.addSource(scaledImage);
TransposeType rotation = null;
if(orientation == Orientation.LEFT_BOTTOM){
rotation = TransposeDescriptor.ROTATE_270;
} else if(orientation == Orientation.TOP_RIGHT){
rotation = TransposeDescriptor.ROTATE_90;
} else if(orientation == Orientation.RIGHT_BOTTOM){
rotation = TransposeDescriptor.ROTATE_180;
}
pb.add(rotation);
PlanarImage rotatedImage = (PlanarImage)JAI.create("transpose", pb);
resultImage = rotatedImage.getAsBufferedImage();
}

I use JAI 1.1.3 with Sun's Java6 jvm on Linux.

Thanks!
Math

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous

Dear Math,
as mentioned in earlier threads you should use "filteredsubsample" instead of “scale” for downsampling.
http://www.j2ee.me/products/java-media/jai/forDevelopers/jai-apidocs/jav...

or just "subsampleaverage"
http://www.j2ee.me/products/java-media/jai/forDevelopers/jai-apidocs/jav...

e.g.
ParameterBlock pb = new ParameterBlock();
pb.addSource(pi);

pb.add(x); //x... integer downsample factor
pb.add(y); //y... integer downsample factor
pb.add(new float[] {1.0f}); //A quadrant symmetric filter generated from a Gaussian kernel
pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));
//or
//pb.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
//pb.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC));
//pb.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC_2));
PlanarImage thumb = JAI.create("filteredsubsample", pb);

//or
pb.add(x));//x... double downsample factor
pb.add(y); //y... double downsample factor
PlanarImage thumb = JAI.create("subsampleaverage", pb);

Hope this helps,
Helmut

avoinemt
Offline
Joined: 2008-03-30

I tried both solutions and the latter (using "subsampleaverage") seems to do the trick. I could not make the first solution work, either because I don't understand how it works, or it is not possible to scale the src image by a fraction (downsample factor is an Integer??).

Thanks for answering my question!
Math