• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 984
  • Last Modified:

getScaledInstance too slow, can I do it in Graphics2D?

Hi all,

I am using this command to resize the image,

image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH),

and it is too slow, about 1-2 sec per image. Is there a way to speed this up?

I am thinking of two approaches.

1. I heard that if I do the same in Grahics2D, it will be much faster. But there is not direct replacement for this call, so what should I do?
2. My images are 90% white space, so if I could tell it not to resize white space, I would be fine. But I can not dig this out of Java source code.

Thank you.
0
kerzner
Asked:
kerzner
  • 15
  • 11
  • 5
1 Solution
 
CEHJCommented:
Try using an AffineTransform on g2d.drawImage (assuming BufferedImage)
0
 
CEHJCommented:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics2D.html#drawImage(java.awt.Image, java.awt.geom.AffineTransform, java.awt.image.ImageObserver)
0
 
kerznerAuthor Commented:
How hard would it be to tell me exactly what code goes instead of mine?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
50% scale

AffineTransform tx = new AffineTransform();
tx.scale(0.5, 0.5);
g2d.drawImage(image, tx, this);
0
 
kerznerAuthor Commented:
I may be getting there, but how do I make it smooth, as this flag Image.SCALE_SMOOTH was want to do?
0
 
CEHJCommented:
You can set RenderingHints on the g2d
0
 
objectsCommented:
What exactly is it you are trying to achieve?
0
 
kerznerAuthor Commented:
I am trying to do this.

1. Load a few tif images of B/W printed pages.
2. Paint them in my screen, a few at a time.

The purpose of this is to review the quality of printed pages, and do this as quickly as possible, by viewing many pages at once. The Graphics2D approaches are not working for me.

At the moment, I am trying to extends the AreaAveragingScaleFilter in such a way as to make it very fast for B/W text images, where rgb is either black or white, but predominantly white and do not have to be processed at all.

I achieved moderate success, but am trying to write to Bill Graham, the author of the code.

Any advice will be appreciated.
0
 
objectsCommented:
When do you scale your images?
0
 
kerznerAuthor Commented:
I scale them the first time they are displayed, and cache that.
0
 
objectsCommented:
So is it just the initial load time that is too slow?
0
 
kerznerAuthor Commented:
No, it is the getScaledInstance that takes all the time.
0
 
objectsCommented:
> No, it is the getScaledInstance that takes all the time.

Yes but you only need to do that once at load time don't you.
0
 
kerznerAuthor Commented:
Yes, but even that takes at least 1 sec per image, so 6-8 images take more than any operator is willing to bear
0
 
objectsCommented:
So it is a load time issue, whats the difference between the time to load the image and the time to scale them (how big are the images)
0
 
objectsCommented:
What Hardware/OS are you running on?
0
 
kerznerAuthor Commented:
Windows, about 3 Gigaherts, lots of memory
0
 
objectsCommented:
And how do you load your images? and what is load time compared to scaling time?
0
 
kerznerAuthor Commented:
load time is 1 second for 4 images, scaling time 1 second per image
0
 
kerznerAuthor Commented:
everybody complains that it is slow, and I am planning to distribute the task of generating thumbnails between a few computers (and give myself the points)
0
 
objectsCommented:
do u need the original image and have you tried scaling while loading?
0
 
kerznerAuthor Commented:
I am loading with Jimi.getImage(). I do not think think it can scale while loading, but this sounds great if that could be doable
0
 
objectsCommented:
Instead load the image using an ImageProducer:

ImageProducer img = new FilteredImageSource(Jimi.getImageProducer(image), scalefilter);
0
 
kerznerAuthor Commented:
that looks very interesting, will try tomorrow, thank you
0
 
kerznerAuthor Commented:
ok, what do I use for sclalefilter?
0
 
objectsCommented:
Whatever algorithm you want to use for the scaling, eg. AreaAveragingScaleFilter
0
 
kerznerAuthor Commented:
Thank you, this seems to work. I may be missing some knowledge, but how do I control the size to which this image is reduced, and what are the good values for the new AreaAveragingScaleFilter(width, height) constructor?

This is what I am doing now

AreaAveragingScaleFilter scalefilter = new AreaAveragingScaleFilter(121, 121);
ImageProducer imageProducer = new FilteredImageSource(Jimi.getImageProducer(imagePath), scalefilter);
image = createImage(imageProducer);
g.drawImage(image, r.x, r.y, r.width, r.height, this);
0
 
objectsCommented:
> and what are the good values for the new AreaAveragingScaleFilter(width, height) constructor?

the size you want the resulting image to be
0
 
kerznerAuthor Commented:
Looks very good now, but still slower than running this utility, http://www.atalasoft.com/Components/ImgXControls/, and then displaying the result in Java. Feels like a pity to go outside of Java.

If not improvement is possible, I will accept this answer.
0
 
kerznerAuthor Commented:
Thank you, the accepted answer provided a speed up. Still, for my purposes speed up was not enough, and I am implementing the use a low-level C++ image conversion using atalasoft utility, also investigating using accelerated graphics opengl in Java 1.5
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 15
  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now