0

Possible Duplicate:
I am making some parts of java image transparent by some code, it works fine on the laptop I made, but not on others, Why?

Image test : the orignal image,
Image testt : the image after applying transparency

In my laptop, the color I want to make transparent becomes transparent, test as well as testt are rendered, but else where the testt image is not drawn, the test is drawn but testt is not.

The complete code for drawing a simple image with transparency:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.Serializable;


public class test extends Applet implements Runnable
{
    public static Image makeColorTransparent(Image im, final Color color) 
      {
        ImageFilter filter = new RGBImageFilter() 
        {

              public int markerRGB = color.getRGB() | 0xFF000000;               //color to make transparent

              public final int filterRGB(int x, int y, int rgb) 
              {
                if ( ( rgb | 0xFF000000 ) == markerRGB ) 
                {
                  // Mark the alpha bits as zero - transparent
                  return 0x00FFFFFF & rgb;
                }

                else 
                {
                  // nothing to do
                  return rgb;
                }
             }
        }; 

            ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
            return Toolkit.getDefaultToolkit().createImage(ip);

      }


    Image test;
    public void init()
    {

        setSize(600,600);

    }

    public void update (Graphics g)                                             //overriding the update for double buffering
    {       
        // initialize buffer
        Image dbImage = createImage (this.getSize().width, this.getSize().height);
        Graphics dbg = dbImage.getGraphics ();


        // clear screen in background
        dbg.setColor (getBackground());
        dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);


        // draw elements in background
        dbg.setColor (getForeground());
        paint (dbg);

        // draw image on the screen
        g.drawImage (dbImage, 0, 0, this);      
    }


    public void paint(Graphics g)
    {
        test = getImage(getCodeBase (), "tt.gif");
        Image testt = makeColorTransparent(test, Color.white);               

        g.drawImage (testt,0,0, this);
    }


    @Override
    public void run() {
        // TODO Auto-generated method stub
        repaint();

    }

}
Community
  • 1
  • 1
riya das
  • 31
  • 1
  • 5
  • Could it have something to do with 32/64-bit environments? I'm not sure if 64-bit java uses 64-bit colors, but if it does, that could be your problem since the bit masking screws up. – Accatyyc Apr 01 '12 at 07:11
  • Are you absolutely sure, the file "tt.gif" does exist on other computers that you launch your applications? And at exactly path that needed for application to find it? – yggdraa Apr 01 '12 at 07:16
  • yes, tt.gif is in bin folder of project folder of both computers... – riya das Apr 01 '12 at 08:20
  • Since you are using the same GIF file as the image input, I now suspect something else. Maybe, you should give more info about the two systems: its OS, its JRE version? 32-bit or 64-bit in OS/JRE? dedicated GPU or integrated GPU? – ecle Apr 01 '12 at 12:31
  • @Accatyyc The colour depth doesn't change with CPU-bitness, and Java has the same sizes for data types regardless of the VM. (That is, an `int` will be 32 bits wide on both the 32bit and the 64bit JVM, and they both use the same runtime, where pixels are `int`s.) – millimoose Apr 01 '12 at 13:43
  • solve the problem by making the image png/bmp – riya das Apr 05 '12 at 08:10

1 Answers1

0

Use PhiLho;s solution in How to make a color transparent in a BufferedImage and save as PNG

  private Image TransformColorToTransparency(BufferedImage image, Color c1, Color c2)
  {
    // Primitive test, just an example
    final int r1 = c1.getRed();
    final int g1 = c1.getGreen();
    final int b1 = c1.getBlue();
    final int r2 = c2.getRed();
    final int g2 = c2.getGreen();
    final int b2 = c2.getBlue();
    ImageFilter filter = new RGBImageFilter()
    {
      public final int filterRGB(int x, int y, int rgb)
      {
        int r = (rgb & 0xFF0000) >> 16;
        int g = (rgb & 0xFF00) >> 8;
        int b = rgb & 0xFF;
        if (r >= r1 && r <= r2 &&
            g >= g1 && g <= g2 &&
            b >= b1 && b <= b2)
        {
          // Set fully transparent but keep color
          return rgb & 0xFFFFFF;
        }
        return rgb;
      }
    };

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
      return Toolkit.getDefaultToolkit().createImage(ip);
  }

Or, http://marxsoftware.blogspot.com/2011/04/making-white-image-backgrounds.html

   public static Image makeColorTransparent(final BufferedImage im, final Color color)  
   {  
      final ImageFilter filter = new RGBImageFilter()  
      {  
         // the color we are looking for (white)... Alpha bits are set to opaque  
         public int markerRGB = color.getRGB() | 0xFFFFFFFF;  

         public final int filterRGB(final int x, final int y, final int rgb)  
         {  
            if ((rgb | 0xFF000000) == markerRGB)  
            {  
               // Mark the alpha bits as zero - transparent  
               return 0x00FFFFFF & rgb;  
            }  
            else  
            {  
               // nothing to do  
               return rgb;  
            }  
         }  
      };  

      final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);  
      return Toolkit.getDefaultToolkit().createImage(ip);  
   }  
}

Obviously, the difference between your problem and the second solution is here:

public int markerRGB = color.getRGB() | 0xFF000000;   

where instead of white color, you are looking for black color to set as a transparency color.

MIT's solution http://web.mit.edu/javalib/www/examples/if/start.html

class TransparentFilter extends RGBImageFilter {

    final static int aShift=24;
    final static int rShift=16;
    final static int gShift=8;
    final static int bShift=0;
    final static int aMask=0xff<<aShift;
    final static int rMask=0xff<<rShift;
    final static int gMask=0xff<<gShift;
    final static int bMask=0xff<<bShift;
    final static int rgbMask=rMask|gMask|bMask;

    // this fudge value is used in place of the harder (but proper) task of
    // performing statistical analysis of the saturation values (and
    // possibly distances) of highly-saturated pixels contained in the image
    float saturationFudge;

    TransparentFilter(float saturationFudge) {
        this.saturationFudge=saturationFudge;
        canFilterIndexColorModel=true;
    }

    public int filterRGB(int x, int y, int argb) {
        // separate the three colour channels (ignore incoming alpha)
        int r=(argb&rMask) >>> TransparentFilter.rShift;
        int g=(argb&gMask) >>> TransparentFilter.gShift;
        int b=(argb&bMask) >>> TransparentFilter.bShift;
        // convert to hsb so that we can use saturation to
        // establish the alpha (transparency) value of the pixel
        float[] hsb=Color.RGBtoHSB(r,g,b,null);
        float fa=255f*hsb[1]/this.saturationFudge;
        int a=Math.max(0,Math.min(255,Math.round(fa)))<<TransparentFilter.aShift;
        return a|(argb&TransparentFilter.rgbMask);
    }

}

Still, if all solutions don't work properly on test systems, we can suspect other possible culprits which we don't have enough information based on your original post:

  1. OS version 32/64-bit?
  2. JRE/JDK version 32/64-bit?
  3. GPU: hardware and driver?
  4. Display: 256-bit/16-bit/24-bit?

Also, we don't know what you have observed between those systems since no screenshots are provided.

The worst is you have to start using an image with transparent mask already set to solve you issue rather than relying on coding its transparency bit (unless if you are trying to create a Java based photo-editing application which needs a transparency filter).

Possible unsolved related issues ??:

  1. Java Swing transparency drawing issues
Community
  • 1
  • 1
ecle
  • 3,952
  • 1
  • 18
  • 22