Skip to main content

Mosaic image has 'weird' colors when overlaying two images on top of each other

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
4 replies [Last post]
newmanw10
Offline
Joined: 2009-08-17

I am creating a mosaic out of a lot of images and some of the images overlap. My problem is in the overlap portion, the pixel colors are not that of one or the other image.

As a test I tried just reading in two images and mosaic'ing them together on top of each other. I am using OVERLAY mosaic type and the result that I want is one image completely on top of the other image. i.e. I do not want to see any pixels of the bottom image. I do not think that I am seeing pixels from the bottom image but some of the pixels on the top image are showing up incorrect.

Is there something more I can specify such that when trying to choose between two pixels always just choose one or the other.

here is some sample code

I have also verified that the alpha bits for the image are either 255 ( completly opaque), or 0 completly transparent.

Is there still some sort of calc going on that is making some of the pixels come out weird instead of just seeing one image on top of the other?

BufferedImage image1 = .......

// need to get this image to rgba
IndexColorModel icm = (IndexColorModel) image1.getColorModel();
byte[ ][ ] data = new byte[4][icm.getMapSize()];

icm.getReds(data[0]);
icm.getGreens(data[1]);
icm.getBlues(data[2]);
icm.getAlphas(data[3]);
LookupTableJAI lut = new LookupTableJAI(data);
RenderedOp rgbaImage1 = JAI.create("lookup", image1, lut);


BufferedImage image2 = .......

// need to get this image to rgba
icm = (IndexColorModel) image2.getColorModel();
data = new byte[4][icm.getMapSize()];
icm.getReds(data[0]);
icm.getGreens(data[1]);
icm.getBlues(data[2]);
icm.getAlphas(data[3]);
lut = new LookupTableJAI(data);

RenderedOp rgbaImage2 = JAI.create("lookup", image2, lut);
ParameterBlockJAI params = new ParameterBlockJAI("mosaic");
params.addSource(rgbaImage1);
params.addSource(rgbaImage2);
params.setParameter("mosaicType", MosaicDescriptor.MOSAIC_TYPE_OVERLAY);

RenderedOp mosaic = JAI.create("mosaic", params);// Then show the image, since both are in position x/y 0/0 then one should be on top of the other

Please help.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
erunquist
Offline
Joined: 2009-05-01

I haven't used mosaic, but the doco makes it sound like the priority given to each image is based on the sourceAlpha when you use the overlay mosaic type. If I understand it correctly, the sourceAlphas argument needs to be an array of alpha masks, with one mask for each source image. The masks need to be filled with 0's (don't use the source image pixel) and 1's (use the source image pixel).

If you make an alpha mask that's all 1's and supply N copies of it, where N is the number of source images, then it looks like the source image with the lowest index will win when the source image pixels overlap.

Unless you want the output image to have an alpha mask, you probably don't need alpha masks directly on the source images.

newmanw10
Offline
Joined: 2009-08-17

Looking at the doco I still do not understand how to create the sourceAlphas array. According to the documentation the sourceAlpha array is an array of PlanarImage's. SO do I need to create N (number of source images) PlanarImages that have an alpha mask of 1? If so I have no idea how to do that.

Sorry for the confusion I am very new to JAI and this is still very confusing to me.

Thanks so much for you help!

erunquist
Offline
Joined: 2009-05-01

Give this a try...

// Make a single-band image for our sourceAlpha
ParameterBlock pb = new ParameterBlock();
pb.add(new Float(YOUR_IMAGE_WIDTH));
pb.add(new Float(YOUR_IMAGE_HEIGHT));
pb.add(new Byte[] {1});

RenderedOp sourceAlpha = JAI.create("constant", pb);

PlanarImage[] sourceAlphas = new PlanarImage[] {sourceAlpha, sourceAlpha};

// Do the mosaic of two images
ParameterBlockJAI params = new ParameterBlockJAI("mosaic");
params.addSource(rgbImage1);
params.addSource(rgbImage2);
params.setParameter("mosaicType", MosaicDescriptor.MOSAIC_TYPE_OVERLAY);
params.setParameter("sourceAlphas", sourceAlphas);

RenderedOp mosaic = JAI.create("mosaic", params);

newmanw10
Offline
Joined: 2009-08-17

Perfect thanks!