awt: prepareImage and imageUpdate performance issue

F

flarosa

I'm trying to resize an image in a server application using AWT
functions. Since my application doesn't have a UI, I set the
java.awt.headless=true option.

To resize the image, I'm calling Image.getScaledInstance(). Since this
is an asynchronous function, I have to wait for it to complete before I
can use the image. To do that, I am calling Toolkit.prepareImage() with
an ImageObserver. When the image observer receives the ALLBITS flag, I
stop waiting.

The bottom line is, it works, but it is often painfully slow -- I'm
talking 30, 40, 50 seconds to resize a simple image. During this time
my ImageObserver gets called every few seconds with the SOMEBITS flag.
But other times it scales images in one or two seconds. All the image
data is in memory, so there aren't any network delays going on (the
original image is constructed from a byte array, not a URL or a
stream).

Is there some kind of thread priority, or other parameter I need to set
to make this work better? Or am I just doing something fundamentally
wrong?

Thanks.
 
F

flarosa

Turns out the priority of the thread processing the image is low. It
sometimes start off at 8, but quickly drops to 3. Since I'm running
this inside a complex J2EE application server, it's easy to believe
that there are a ton of other higher priority threads taking
precidence.

Boosting the thread priority up to 5 provides a reasonable level of
performance, although it still takes between 300 and 3000 milliseconds
to resize most of my images.

On a related note -- do I have to wait after calling
Toolkit.createImage(byte[])? I was waiting before, but found that if I
don't wait I still get the right results.

Thanks.
 
A

Andrew Thompson

On a related note -- do I have to wait after calling
Toolkit.createImage(byte[])? I was waiting before, but found that if I
don't wait I still get the right results.

That's a good question. I was working with image byte arrays
and stamping them out as you do, for an animated GIF writer.
Since there were a lot of images (around 200-250), I was not
bothering to display them, but instead write them directly to
the GIF encoder.

I found I was getting 'black' images until I added a MediaTracker
to wait on the image after I'd called creatImage(byte[]).

Having said that, the only method in Toolkit that mentions
'asynchronous' is prepareImage().

I'd like to get to the bottom of this, as the documentation
suggests to me that we should not have to wait on that method.
 
F

flarosa

The reason I get the same result either way in my application could be
because the getScaledInstance method turns around and waits for the
original image to load first before it tries to scale it. In any case,
I put back my original code that waits after createImage, figuring
better safe than sorry.

I tried using the MediaTracker as well and got about the same results
as I did with the ImageObserver before I adjusted the thread priority.
The MediaTracker is definitely more convenient, but since there is no
callback I can't boost the priority.

I agree that the AWT isn't well documented when it comes to this kind
of thing. Which is suprising considering how long it has been around. I
find myself wondering what happens if I decide to give up waiting for
an event -- there doesn't seem to be a way to deregister your
ImageObserver. I'm also not clear on what happens if you register two
different Observers to the same image -- does one replace the other or
are they both called? And why does the checkImage function take an
ImageObserver? Are you supposed to pass it the same observer you're
already using?
 
T

Thomas Fritsch

flarosa said:
I agree that the AWT isn't well documented when it comes to this kind
of thing. Which is suprising considering how long it has been around. I
find myself wondering what happens if I decide to give up waiting for
an event -- there doesn't seem to be a way to deregister your
ImageObserver.
You have to return false from imageUpdate() then. Quoted from the javadoc of
ImageObserver#imageUpdate: "This method should return true if further
updates are needed or false if the required information has been acquired."
I'm also not clear on what happens if you register two
different Observers to the same image -- does one replace the other or
are they both called?
I think both are called.
 
R

Roedy Green

Is there some kind of thread priority, or other parameter I need to set
to make this work better? Or am I just doing something fundamentally
wrong?

have you checked out imageIO? Is there anything in there for scaling
images.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top