I’m trying to render anti aliased text to an offscreen buffer for caching purposes, but I’m having a little trouble rendering the cached text back to the screen. I’m drawing the strings onto a BufferedImage using the pixel format BufferedImage.TYPE_INT_ARGB and the composite function AlphaComposite.Src
When sampling my offscreen buffer I can see the pixel are stored with premultiplied alpha for some reason. That is, a semi transparent white pixel is stored as 0x7F7F7F7F instead of 0x7FFFFFFF. So my brain tells me that I should use the AlphaComposite.SrcOver composite when drawing this offscreen buffer onto another graphics object, but when doing so I get really poor results. I just can’t understand what’s going wrong. For instance if I draw my offscreen buffer onto a completely white graphics object i get gray edges where anti aliasing occurs. How can this be?? With AlphaComposite.SrcOver I should be getting a completely white surface.
JavaDoc from AlphaComposite.SrcOver
[quote]The source is composited over the destination (Porter-Duff Source Over Destination rule).
Fs = 1 and Fd = (1-As), thus:
Ar = As + Ad*(1-As)
Cr = Cs + Cd*(1-As)
[/quote]
So let’s try a semi transparent white pixel on top of a white pixel using this formula:
Ar = 0.5 + 1.0 * (1.0 - 0.5) = 0.5 + 0.5 = 1.0
Cr = (0.5, 0.5, 0.5) + (1.0, 1.0, 1.0) * (1.0 - 0.5) = (0.5, 0.5, 0.5) + (0.5, 0.5, 0.5) = (1.0, 1.0, 1.0)
So why am I seeing gray pixels ?
My conclusion: awt’s implementation of AlphaComposite.SrcOver is not functioning properly