awt: prepareImage and imageUpdate performance issue

Discussion in 'Java' started by flarosa, Aug 21, 2005.

  1. flarosa

    flarosa Guest

    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.
     
    flarosa, Aug 21, 2005
    #1
    1. Advertising

  2. flarosa

    flarosa Guest

    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.
     
    flarosa, Aug 21, 2005
    #2
    1. Advertising

  3. On 21 Aug 2005 02:38:26 -0700, flarosa wrote:

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

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "Just give me one more wish. I can't get enough of this!"
    New Order 'Slow Jam'
     
    Andrew Thompson, Aug 21, 2005
    #3
  4. flarosa

    flarosa Guest

    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?
     
    flarosa, Aug 21, 2005
    #4
  5. "flarosa" <> wrote
    [...]
    > 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.

    > And why does the checkImage function take an ImageObserver?

    I don't know, too
    > Are you supposed to pass it the same observer you're already using?
    >


    --
    "TFritsch$t-online:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Aug 22, 2005
    #5
  6. flarosa

    Roedy Green Guest

    On 21 Aug 2005 01:13:22 -0700, "flarosa" <> wrote
    or quoted :

    >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.
     
    Roedy Green, Aug 22, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Mark Bram
    Replies:
    2
    Views:
    5,968
    Robert Mark Bram
    Dec 11, 2003
  2. hiwa
    Replies:
    1
    Views:
    1,140
    Thomas Weidenfeller
    Mar 9, 2005
  3. Mideo

    Creme 3.27 and AWT frames

    Mideo, Jul 18, 2006, in forum: Java
    Replies:
    1
    Views:
    660
    Mideo
    Jul 21, 2006
  4. dave

    help with awt and jython

    dave, Dec 6, 2003, in forum: Python
    Replies:
    1
    Views:
    313
    Derrick 'dman' Hudson
    Dec 8, 2003
  5. shackman
    Replies:
    0
    Views:
    2,078
    shackman
    Aug 16, 2006
Loading...

Share This Page