Reputation: 394
I'm trying to use rotated grid supersampling (RGSS) for anti-aliasing, but my sub-pixels have an alpha channel in them. How do I merge them into the final pixel?
Normally, one would just average the RGB channels, but with alpha, this doesn't quite work, e.g.: suppose there are 4 RGBA pixels, 2 x (255, 0, 0, 255) and 2 x (0, 0, 0, 0); Averaging gives (127, 0, 0, 127), which is a translucent dark red, which is wrong -- We'd expect something like (255, 0, 0, 127).
Doing standard alpha blending also doesn't produce good results, since there may be two pixels with different colors, both with alpha = 255, thus the last one applied become the color of the pixel, ignoring all others.
My current (improvised) approach, which returns the expected value for the above example, is the following (pseudocode):
merge(R[N], G[N], B[N], A[N])
RR = GG = BB = AA = 0;
for i in 1 .. N:
RR += R[i]*A[i]
GG += G[i]*A[i]
BB += B[i]*A[i]
AA += A[i]
return {RR/AA, GG/AA, BB/AA, AA/N}
I still get jagged edges with near-vertical/horizontal boundaries, however. What is the correct way of merging the sub-pixels, given they have arbitrary alphas, and the background is not known at the time of computation? Is supersampling not the appropriate choice for this? If not, what would be?
Upvotes: 2
Views: 481
Reputation: 394
After some deliberation, I've concluded that the above "improvised approach" is in fact correct, and that the jagged-edge problems encountered were due to the selected sub-pixels being too close to each other.
I've linked a proof I wrote that shows that the above solution is equivalent to the averaging technique, except that the above doesn't require a known background.
http://www.scribd.com/doc/250638206/Merging-RGBA-Pixels-in-Supersampling
P.S.: I would have posted a reduced version of the proof here as the answer, but for it to look right I needed to show equations, which required me to upload images, and SO won't let me upload those until I have 10 reps :(
Upvotes: 1