?
Solved

getScaledInstance too slow, can I do it in Graphics2D?

Posted on 2004-08-23
31
Medium Priority
?
952 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

741 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