Hang displaying a particular image through AWT

N

Nelson Ashton

I've got incomplete information about this, but it seems to be a color
conversion problem.

Symptom: program displays several images, in response to various
inputs. One input, triggering a particular image, results in a lengthy
hang. After a full minute the UI is responsive again, but if the
JLabel displaying the image gets covered and then uncovered again
there's another hang.

Image is a jpeg. So are the ones without troublesome behavior. Some of
those others are larger.

Image is loaded via ImageIO into a BufferedImage. The image data
should therefore stay in RAM and it shouldn't be making a trip to the
disk to read the file every time it needs to repaint the label.

Changing "image" to "image.getScaledInstance(image.getWidth(null),
image.getHeight(null), Image.SCALE_SMOOTH)" gets rid of the hangs on
repaint, but not the initial hang.

CPU profiling during the hang shows a color conversion routine in
sun.* eating up CPU.

Suspicion: The jpeg has an unusual internal color model, and AWT is
stupidly converting it over and over again and not just the once,
except that getScaledInstance results in a lasting conversion.

Bug?

Workaround: use getScaledInstance, and at least it only hangs the once.
 
K

Knute Johnson

Nelson said:
I've got incomplete information about this, but it seems to be a color
conversion problem.

Symptom: program displays several images, in response to various
inputs. One input, triggering a particular image, results in a lengthy
hang. After a full minute the UI is responsive again, but if the
JLabel displaying the image gets covered and then uncovered again
there's another hang.

Image is a jpeg. So are the ones without troublesome behavior. Some of
those others are larger.

Image is loaded via ImageIO into a BufferedImage. The image data
should therefore stay in RAM and it shouldn't be making a trip to the
disk to read the file every time it needs to repaint the label.

Changing "image" to "image.getScaledInstance(image.getWidth(null),
image.getHeight(null), Image.SCALE_SMOOTH)" gets rid of the hangs on
repaint, but not the initial hang.

CPU profiling during the hang shows a color conversion routine in
sun.* eating up CPU.

Suspicion: The jpeg has an unusual internal color model, and AWT is
stupidly converting it over and over again and not just the once,
except that getScaledInstance results in a lasting conversion.

Bug?

Workaround: use getScaledInstance, and at least it only hangs the once.

If it is a ColorSpace issue, you can easily convert it to a compatible
ColorSpace with the method below.

public static BufferedImage convertToCompatible(BufferedImage image) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();

BufferedImage compatible =
gc.createCompatibleImage(image.getWidth(),
image.getHeight());

if (compatible.getType() == image.getType())
return image;

ColorConvertOp op = new ColorConvertOp(
image.getColorModel().getColorSpace(),
compatible.getColorModel().getColorSpace(),null);

return op.filter(image,compatible);
}
 
N

Nelson Ashton

If it is a ColorSpace issue, you can easily convert it to a compatible
ColorSpace with the method below.

Thanks. The initial load is still slow, but I rigged it to pop up a
message and set an hourglass pointer for the few seconds it still
takes for the troublesome image.
 

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

No members online now.

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top