I have the following code that renders the pixel data from one buffered image on to the main canvas (also a BufferedImage) via their DataBuffers.
Both images are of TYPE_INT_ARGB. I can’t remember when this started happening or what I did to cause it, but the image that is being rendered on screen now has a white background even though the BufferdImage itself says the background pixel data is 0,0,0,0 (transparent).
It’s being read from a PNG file through ImageIO and converted to TYPE_INT_ARGB.
When the image(s) (it isn’t isolated to one image either) are displayed in Firefox or Chrome, the background is transparent, as it is supposed to be, not white.
Any idea as to how the alpha is getting screwed up?
if(rdata.img.getType() != pri.getType())
rdata.img = ImageUtils.convertBufferedImage(rdata.img, pri.getType());
Rectangle r = new Rectangle(0,0,pri.getWidth(),pri.getHeight());
Rectangle r2 = new Rectangle(rdata.x, rdata.y, rdata.img.getWidth(), rdata.img.getHeight());
if(!r.contains(r2)) {
Rectangle ri = r2.createIntersection(r).getBounds();
int x = (int) Math.round(ri.getX());
int y = (int) Math.round(ri.getY());
int wt = (int) Math.round(ri.getWidth());
int ht = (int) Math.round(ri.getHeight());
int tx = x - rdata.x;
int ty = y - rdata.y;
if(wt > 0 && ht > 0)
pri.getRaster().setDataElements(x, y, rdata.img.getSubimage(tx, ty, wt, ht).getRaster());
} else {
pri.getRaster().setDataElements(rdata.x, rdata.y, rdata.img.getRaster());
}
EDIT:
I think I may have figured out the problem but not the answer…
Setting the Raster data in that manner sets EVERY pixel in the main canvas to the source image’s values, including transparent pixels, so the canvas has a sort of “hole” as a result, showing through to the back buffer canvas.
Perhaps I should use for loops instead of setDataElements?