Solved

getScaledInstance too slow, can I do it in Graphics2D?

Posted on 2004-08-23
31
941 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

695 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