create PNG images in palette mode in Java

B

babz

Java does not allow us to use more than 16 entries in the color map of
the Index Color Model for palette mode PNG encoding.
This disrupts the quality of images drastically. Is there a way to
improve the image quality?

This is the code I used:

int cmap[] = {

0x00000000,
0xFF202020,
0xFF0000FF,
0xFF808080,
0xFF00FF00,
0xFF00FFFF,
0xFFFF0000,
0xFFFF00FF,
0xFFFFFF00,
0xFFFFFFFF,
};

IndexColorModel colorModel = new IndexColorModel(8,
cmap.length, cmap, 0, true, -1,
DataBuffer.TYPE_BYTE);

bufImg = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_INDEXED, colorModel);



Thanks,

Babz
 
R

Roedy Green

Java does not allow us to use more than 16 entries in the color map of
the Index Color Model for palette mode PNG encoding.

Where did you learn that? Perhaps there is some related class without
the restriction. The limit seems odd given that most indexed
colourmaps in the Windows universe are 8-bit.

I display 17..255-colour map gifs and pngs all the time in Java,
though I could not be certain all the colours are being rendered
properly.
 
T

Thomas Fritsch

babz said:
Java does not allow us to use more than 16 entries in the color map of
the Index Color Model for palette mode PNG encoding.
What did actually happen, when you converted a BufferedImage like below
with a large IndexColorModel (say with cmap having 100 entries) to PNG?
Did you get any error, or did the PNG have too few colors, or was it
otherwise of bad quality? I got good results when converting a JPEG
photo to PNG (with a 125-color palette).

The BufferedImage bufImg in your example below is still empty. Hence:
Exactly how do you fill the pixels into that BufferedImage? There are
probably many ways to do this.
How did you convert that BufferedImage to PNG? I assume you did it with
ImageIO.write(bufImg, "PNG", ...).
This disrupts the quality of images drastically. Is there a way to
improve the image quality?

This is the code I used:

int cmap[] = {
0x00000000,
0xFF202020,
0xFF0000FF,
0xFF808080,
0xFF00FF00,
0xFF00FFFF,
0xFFFF0000,
0xFFFF00FF,
0xFFFFFF00,
0xFFFFFFFF,
};
IndexColorModel colorModel = new IndexColorModel(8,
cmap.length, cmap, 0, true, -1,
DataBuffer.TYPE_BYTE);
bufImg = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_INDEXED, colorModel);
 
B

babz

Thomas said:
I got good results when converting a JPEG photo to PNG (with a 125-color palette)

can you plz give me the set of color map entries you used? Because now
using more than 16 entries does not give me the exception I was getting
before.

Thanks,

Babz
 
B

babz

Thomas said:
I got good results when converting a JPEG photo to PNG (with a 125-color palette)


can you plz give me the set of color map entries you used? Because now
using more than 16 entries does not give me the exception I was getting

before.

Thanks,


Babz
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top