Convert or create PNG images in palette mode in Java

B

babz

AoA ..

does any one know how to create PNG images in Palette mode in java??

PNGQUANT is a software wriiten in C which does the conversion. But i
need java code for that..

Any ideas???

Thanks

Babz
 
T

Thomas Fritsch

babz said:
AoA ..

does any one know how to create PNG images in Palette mode in java??

PNGQUANT is a software wriiten in C which does the conversion. But i
need java code for that..

Any ideas???

Thanks

Babz
It is rather straight-forward in java. Create a BufferedImage with an
IndexColorModel, and write it to a PNG file using javax.imageio.
The example below writes a simple PNG image (100x100 pixel, red square
on transparent background):

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;

public class Test {
public static void main(String args[]) throws IOException {
int cmap[] = {
0x00000000, /*transparent*/
0xFF000000, /*black*/
0xFFFF0000, /*red*/
0xFFFFFF00, /*yellow*/
0xFF00FF00, /*green*/
//......
};
IndexColorModel colorModel = new IndexColorModel(8,
cmap.length, cmap, 0, true, -1, DataBuffer.TYPE_BYTE);
BufferedImage image = new BufferedImage(100, 100,
BufferedImage.TYPE_BYTE_INDEXED, colorModel);
Graphics2D g = image.createGraphics();
g.setBackground(new Color(0,0,0,0)); /*transparent*/
g.clearRect(0 , 0, image.getWidth(), image.getHeight());
g.setColor(Color.red);
g.draw(new Rectangle(10, 10, 40, 40));
ImageIO.write(image, "PNG", new File("example.png"));
}
}
 
B

babz

it does not serve the purpose. An image converted by pngquant renders
fine in IE but the masked image does not show up properly.
 
B

babz

Thanks Thomas,

The code gives me transparent results but there some prob with the
BLACK color. If a draw a black line, it renders it as transparent. but
for other colors like red, green, etc.. it works fine..

Do u know the reason??

Thanks,

Babz
 
T

Thomas Fritsch

babz said:
The code gives me transparent results but there some prob with the
BLACK color. If a draw a black line, it renders it as transparent. but
for other colors like red, green, etc.. it works fine..

Do u know the reason??
Your description sounds very much like the known Sun bug
"opaque colors become transparent with IndexColorModel"
<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4974546>

But: I modified my posted example (replaced "Color.red" by
"Color.black") and rerun it (with Java 1.4.2_05). The generated PNG file
looks OK for me (displaying black on transparent, in Mozilla and IE).

I don't know what is different between mine and yours. I suggest you
post your code fragment here.
 
R

Roedy Green

The code gives me transparent results but there some prob with the
BLACK color. If a draw a black line, it renders it as transparent. but
for other colors like red, green, etc.. it works fine..

Do u know the reason??

lets say you had a pane of clear glass, and you had some transparent
fireflies sitting on the glass flashing their tails.

You could see through the glass but the fireflies would ADD to the
light coming through the glass.

Imagine a sick firefly sitting on the glass not flashing his tail. He
adds nothing to the light coming through the glass. He would just be
transparent as if he were not even there.

The sick firefly is like BLACK on a transparent background.
For black you want the colour to be opaque to be visible.

Black is like lime green used in TV as the transparent colour for
matte work.

P.S. one TV sketch that tickled my funnybone involved a TV weatherman
who wore a lime green suit, and of course became semi-invisible when
they did the map overlays.
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top