Skip to main content

Can't display PNG image correctly on Java

1 reply [Last post]
Joined: 2007-12-31

I am working on a small application which displays JPG and PNG files on screen. I have a PNG file of 4 bit color depth. It doesn't show exact quality in Java as shown in other pic viewer on Windows XP. However, the Java app did display other JPG file of 24bit color depth correctly.

The following is some relative code:
bimg = new BufferedImage(w,h, BufferedImage.TYPE_BYTE_GRAY);
Graphics gbimg = bimg.getGraphics();
gbimg.drawImage(zeImage, 0, 0, null);

Except "TYPE_BYTE_GRAY", I have tried all other field properties. The problem remains.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-01-14

Are you tryingto convert the PNG image to greyscale? (TYPE_BYTE_GRAY would assume that). If so, the Reason display differences among different applications lies in the color conversion to greyscale.

There are various approaches to greyscale an image. The simplest would be: take the average of red, green and blue for each pixel and take this value as brightness. This however is no good method of calculating a pixels brightness, as Red is brighter than blue. To get better results, colors must be converted to a more convenient color space, suchas LAB, where a color is represented as Brightness and chromatics. For more information, see:

I guess the reason for different results among applications is, that they use different approaches to greyscale an image.