Problem with IndexColorModel in Mac OS X

Discussion in 'Java' started by Martin Chan, Feb 17, 2004.

  1. Martin Chan

    Martin Chan Guest

    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]);
    }

    }
     
    Martin Chan, Feb 17, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bernhard Messerer
    Replies:
    1
    Views:
    691
    Roedy Green
    Aug 14, 2003
  2. Michael C. Gilson
    Replies:
    4
    Views:
    959
    Ken DeLong
    Aug 31, 2003
  3. bonkura_
    Replies:
    3
    Views:
    2,230
    Roedy Green
    Oct 22, 2005
  4. Replies:
    1
    Views:
    1,815
  5. Philipp
    Replies:
    4
    Views:
    5,333
    Philipp
    Dec 12, 2006
Loading...

Share This Page