Merging RGBA Pixels in Supersampling
Merging RGBA Pixels in Supersampling
Andre L. D. P. Harder
Introduction
Supersampling algorithms, often used in contexts such as anti-aliasing and subscaling, rely on mapping many data-points (be they pixels or sub-pixels) to a
single one. Typically, these algorithms are presented assuming the background
color is already known, which, however, isnt always the case. Here, we extend
the simple averaging algorithm to pixels which have an alpha component. The
results are neither novel or unique, rather they are presented here in response
to the inhearent dificulty of finding a solution to this problem on the internet.
The main issue regarding using averaging techniques when not knowing the
background color can be illustrated by the following example: Suppose there
are 4 RGBA pixels, 2 (255, 0, 0, 255) and 2 (0, 0, 0, 0); Averaging gives us
(127, 0, 0, 127), which is a translucent dark red, whereas wed expect a translucent bright red. If the above blending where done, for instance, in drawing a red
line over a transparent background, which we then placed on a red background,
wed end up with a dark red line on a red background, instead of a fully red
image. Thus, simple averaging is incorrect here.
elements we wish to merge would each have the following colors, given by alpha
blending:
Cb = (Rb , Gb , Bb )
Ci = (Ri , Gi , Bi )
COi = Ai Ci + (1 Ai )Ab Cb
(1)
AOi = Ai + (1 Ai )Ab
Where Rb , Gb , Bb and Ab are the background color-channels, whose grouping
(without alpha) is Cb , Ri , Gi , Bi and Ai are the colors of the sub-pixel, with
a similar grouping Ci , COi are the resulting colors from the overlay, and AOi is
its alpha.
Now, merging these pixels through a simple average, we get the following:
CAV G =
=
=
AAV G =
=
=
1
N
1
N
1
N
1
N
1
N
1
N
COi
X
Ai Ci + (1 Ai )Ab Cb
X
1 X
Ai C i + 1
Ai Ab C b
N
X
COi
X
Ai + (1 Ai )Ab
X
1 X
Ai + (1
Ai )Ab
N
(2)
Where CAV G is the color-channel vector resulting from the averaging process,
and AAV G is the resulting alpha-channel value.
With this, we can now find the equivalent pixel color that, when applied
to the same background, will produce the same result. To accomplish this, we
manipulate the equality between this equivalent-pixel and the above average:
1 X
1 X
AC CC + (1 AC )Ab Cb =
Ai Ci + 1
Ai Ab C b
N
N
1 X
Ai
AC =
NP
1
Ai Ci
CC = N 1 P
Ai
PN
Ai Ci
= P
Ai
Thus proving the desired equivalence.
2
(3)