bright only non-transparent pixels

Does anyone know how to make only the non-transparent part of an image bright or dark ? I tried the following code but it appears that rescalOp applies to every pixel of an image , non-transparent as well as transparent:

public static BufferedImage brightImage(BufferedImage bufferedImage)
{

  BufferedImage bi;

      float scaleFactor = 1.2f;
      RescaleOp op = new RescaleOp(scaleFactor, 1, null);
      bi = op.filter(bufferedImage, null);
  return(bi);

}

Jay

Why exactly do you only want to brighten the non-transparent pixels? By definition you won’t see the other pixels anyway, no matter how bright they are their alpha channel will still be 0. Or do you mean that the operation above adds to the value of Alpha as well as R,G & B? If so it is a bug in RescaleOp, since it is defined as not applying to the Alpha of a BufferedImage.

If it is such a bug you might be able to work around it with:
new RescaleOp(new float[]{scaleFactor,scaleFactor,scaleFactor,0},new float[]{1,1,1,0},null);

Or you could perhaps keep a copy of the original image and “fix” the alpha channel after the brightening. Or write your own filter operation that is a bit smarter.

Thanks for the response. The reason that I only want to bright the non-transparent pixels is that all the transparent color shows up as black when I apply the RescaleOp to a gif image which has transparent background . It appears that RescaleOp alters alpha channels as well.

When I followed your suggestion by creating a new RescalOp as

new RescaleOp(new float[]{scaleFactor,scaleFactor,scaleFactor,0},new float[]{1,1,1,0},null);

I received the following error:

java.lang.IllegalArgumentException: Number of scaling constants does not equal the number of color or color/alpha components
at java.awt.image.RescaleOp.filter(RescaleOp.java:323)

Ah… how did you load the GIF image… it could be using an indexed colour model or something and that may cause problems. The colours in a GIF palette DON’T have Alpha, yet the image itself can have a transparent area… very likely that is buggering up what you are trying to do.

You could try drawing the GIf to a new BufferedImage that you explicitly create with a 32-bit colour model… then use that 32-bit image for the RescaleOp. Or convert your GIf images to 32-bit PNGs instead.

The problem was caused by the way that the color indexed images were converted to RGB images . If I use BufferedImage.TYPE_INT_ARGB instead of BufferedImage.TYPE_INT_RGB , the transparent pixels work fine with ConvolveOp operation. But with Rescale, I got nothing drawn on the screen if the buffered image was created using BufferedImage.TYPE_INT_ARGB .

public static BufferedImage rgbImage(Image image)
{
BufferedImage bimage=new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_ARGB);

  Graphics2D g=bimage.createGraphics();
  g.drawImage(image,0,0,null);
  g.dispose();
  return bimage;

}

public static BufferedImage brightImage(BufferedImage bI, float scaleFactor)
{
BufferedImage bi;

BufferedImageOp op=new RescaleOp(scaleFactor,0,null);
bi=op.filter(bI,null);
return(bi);
}

Jay