Solved

getScaledInstance too slow, can I do it in Graphics2D?

Posted on 2004-08-23
31
901 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
Comment Utility
Try using an AffineTransform on g2d.drawImage (assuming BufferedImage)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
How hard would it be to tell me exactly what code goes instead of mine?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
50% scale

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

Author Comment

by:kerzner
Comment Utility
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
Comment Utility
You can set RenderingHints on the g2d
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
0
 
LVL 92

Expert Comment

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

Author Comment

by:kerzner
Comment Utility
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
Comment Utility
When do you scale your images?
0
 
LVL 1

Author Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
What Hardware/OS are you running on?
0
 
LVL 1

Author Comment

by:kerzner
Comment Utility
Windows, about 3 Gigaherts, lots of memory
0
 
LVL 92

Expert Comment

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

Author Comment

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

Author Comment

by:kerzner
Comment Utility
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
Comment Utility
do u need the original image and have you tried scaling while loading?
0
 
LVL 1

Author Comment

by:kerzner
Comment Utility
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
Comment Utility
Instead load the image using an ImageProducer:

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

Author Comment

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

Author Comment

by:kerzner
Comment Utility
ok, what do I use for sclalefilter?
0
 
LVL 92

Expert Comment

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

Author Comment

by:kerzner
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 71
countX 22 68
mapShare challenge 13 67
micro services vs rest web services 16 45
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now