Problem with IndexColorModel in Mac OS X

M

Martin Chan

Hi, I use IndexColorModel to convert a image from RGB to
binary(black/transparent ).

byte[] r = {(byte)0, (byte)0xff};
byte[] g = {(byte)0, (byte)0xff};
byte[] b = {(byte)0, (byte)0xff};
//{ black , transparent }
BINARY_COLOR_MODEL = new IndexColorModel(1, 2, r, g, b, 1);

The code convert an image to black/transparent. Please see the attached
file.
The code work well in Linux and Windows with J2SDK1.4.1_01

In Mac OS X 10.3.2 with Java 1.4.1_01, the image appears as black/white,
no transparent. Does anyone know is it a bug in Mac's Java runtime?

Also, the bufferedImage.getRGB(x, y) seems return a wrong value.


To run the program:
java TestIndexColorModel <imagefile>

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class TestIndexColorModel extends JFrame {

public TestIndexColorModel(String file) {

byte[] r = {(byte)0, (byte)0xff};
byte[] g = {(byte)0, (byte)0xff};
byte[] b = {(byte)0, (byte)0xff};
//{ black , white }
IndexColorModel BINARY_COLOR_MODEL = new IndexColorModel(1, 2,
r, g, b, 1);

java.awt.Image image = null;
try{
image = javax.imageio.ImageIO.read(new
java.io.FileInputStream(file));
}catch(Exception ex){
image = new java.awt.image.BufferedImage(1, 1,
java.awt.image.BufferedImage.TYPE_INT_RGB);
ex.printStackTrace();
}
int w = image.getWidth(this);
int h = image.getHeight(this);
//The white color should appear as transparent.

BufferedImage bufferedImage = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_BINARY, BINARY_COLOR_MODEL);

Graphics2D big = bufferedImage.createGraphics();
big.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
big.setRenderingHint(RenderingHints.KEY_DITHERING,
RenderingHints.VALUE_DITHER_ENABLE);
big.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
big.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_SPEED);
big.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_OFF);

//image is the orig. image in RGB
big.drawImage(image, 0, 0, w, h, new java.awt.Color(0, 0,0 ,0),
this);
big.dispose();

getContentPane().add(new JButton(new ImageIcon(bufferedImage)));
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new TestIndexColorModel(args[0]);
}

}
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top