JAI Add alpha channel to RenderedImage

I have two RenderedImages

. I want to do Overlay Operation

with these two images and so they must match the datatype and the number of stripes.
The problem is that one image has 3 stripes (RGB) and the second image has 4 stripes (ARGB).

My question is, how do I add an alpha channel to the first image so I can do it Overlay Operation

?

EDIT
Ok, I found a way to add an alpha channel to the first image. Below is the code. I just created a single strip persistent image and combined it with my first image.

ParameterBlock pb = new ParameterBlock();
pb.add(new Float(finalImage.getWidth())).add(new Float(finalImage.getHeight()));
pb.add(new Byte[] {new Byte((byte)0xFF)});
RenderedImage alpha = JAI.create("constant", pb);

finalImage = BandMergeDescriptor.create(finalImage, alpha, null);

      

Now the problem is that every time I add the overlay the image changes colors. All colors become shades of red or pink. When I add a second overlay, the image is normal again, but the first overlay changes colors. All black areas turn white.

In addition, the background of the overlay is not transparent. It is gray.

Below are sample images, so you can see the colors change:

original picture

after adding the first overlay

after adding the second overlay

As you can see, the image and overlays change colors and the background of the overlay is not transparent.

Can you help me solve this problem so that the image is displayed correctly? Thank!

+3


source to share


2 answers


You can try to create a new BufferedImage with ARGB model and just paint an opaque background image in that new BufferedImage. Then you have a BufferedImage with an alpha channel (although all pixels are opaque), so composition will hopefully work.



0


source


im not sure about TYPE_4BYTE_ARGB as I usually work with BufferedImages from TYPE_INT_ARGB, but I often used the RGB BufferedImage paint method on the new ARGB BufferedImage and then paint that to other things without issue. color change suggests that unwanted changes are occurring to other channels during the blending process, since it does not look like a particular image. If your overlay operation is like drawing one image to another using alpha, then I would suggest using the Graphics.drawImage () / drawRenderedImage () method for the overlay itself, not to mention that the background is not even needed in that case.

code:



public RenderedImage overlay(RenderedImage back, RenderedImage front, AffineTransform overlayTransformation)
{

    BufferedImage newBack = new BufferedImage(back.getWidth(), back.getHeight(), TYPE_3BYTE_RGB);
    newBack.setData(back.getData());
    Graphics2D graphics = (Graphics2D)(newBack.getGraphics());
    graphics.drawRenderedImage(front, overlayTransformation);
    return newBack;

}

      

you may want the original back Raster not to be modified though.

0


source







All Articles