Solved

getScaledInstance too slow, can I do it in Graphics2D?

Posted on 2004-08-23
31
925 Views
Last Modified: 2008-01-09
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
Comment
Question by:kerzner
  • 15
  • 11
  • 5
31 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11873562
Try using an AffineTransform on g2d.drawImage (assuming BufferedImage)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11873577
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
 
LVL 1

Author Comment

by:kerzner
ID: 11873755
How hard would it be to tell me exactly what code goes instead of mine?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 11873806
50% scale

AffineTransform tx = new AffineTransform();
tx.scale(0.5, 0.5);
g2d.drawImage(image, tx, this);
0
 
LVL 1

Author Comment

by:kerzner
ID: 11874080
I may be getting there, but how do I make it smooth, as this flag Image.SCALE_SMOOTH was want to do?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11874170
You can set RenderingHints on the g2d
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11874187
0
 
LVL 92

Expert Comment

by:objects
ID: 11875406
What exactly is it you are trying to achieve?
0
 
LVL 1

Author Comment

by:kerzner
ID: 11875597
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
 
LVL 92

Expert Comment

by:objects
ID: 11875743
When do you scale your images?
0
 
LVL 1

Author Comment

by:kerzner
ID: 11875884
I scale them the first time they are displayed, and cache that.
0
 
LVL 92

Expert Comment

by:objects
ID: 11876020
So is it just the initial load time that is too slow?
0
 
LVL 1

Author Comment

by:kerzner
ID: 11876029
No, it is the getScaledInstance that takes all the time.
0
 
LVL 92

Expert Comment

by:objects
ID: 11876042
> 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
 
LVL 1

Author Comment

by:kerzner
ID: 11876211
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
 
LVL 92

Expert Comment

by:objects
ID: 11876288
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
 
LVL 92

Expert Comment

by:objects
ID: 11876576
What Hardware/OS are you running on?
0
 
LVL 1

Author Comment

by:kerzner
ID: 11877066
Windows, about 3 Gigaherts, lots of memory
0
 
LVL 92

Expert Comment

by:objects
ID: 11877122
And how do you load your images? and what is load time compared to scaling time?
0
 
LVL 1

Author Comment

by:kerzner
ID: 11877151
load time is 1 second for 4 images, scaling time 1 second per image
0
 
LVL 1

Author Comment

by:kerzner
ID: 11877155
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
 
LVL 92

Expert Comment

by:objects
ID: 11877163
do u need the original image and have you tried scaling while loading?
0
 
LVL 1

Author Comment

by:kerzner
ID: 11877174
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 11877854
Instead load the image using an ImageProducer:

ImageProducer img = new FilteredImageSource(Jimi.getImageProducer(image), scalefilter);
0
 
LVL 1

Author Comment

by:kerzner
ID: 11877899
that looks very interesting, will try tomorrow, thank you
0
 
LVL 1

Author Comment

by:kerzner
ID: 11881426
ok, what do I use for sclalefilter?
0
 
LVL 92

Expert Comment

by:objects
ID: 11887273
Whatever algorithm you want to use for the scaling, eg. AreaAveragingScaleFilter
0
 
LVL 1

Author Comment

by:kerzner
ID: 11887574
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
 
LVL 92

Expert Comment

by:objects
ID: 11887657
> and what are the good values for the new AreaAveragingScaleFilter(width, height) constructor?

the size you want the resulting image to be
0
 
LVL 1

Author Comment

by:kerzner
ID: 11887750
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
 
LVL 1

Author Comment

by:kerzner
ID: 11897520
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question