0% found this document useful (0 votes)
309 views2 pages

Merging RGBA Pixels in Supersampling

Averaging algorithm extension for use in supersampling/subscaling (e.g.: Anti-aliasing), for when the background is not known at the time of subsampling, and the pixels have an alpha component.

Uploaded by

André Harder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
309 views2 pages

Merging RGBA Pixels in Supersampling

Averaging algorithm extension for use in supersampling/subscaling (e.g.: Anti-aliasing), for when the background is not known at the time of subsampling, and the pixels have an alpha component.

Uploaded by

André Harder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Merging RGBA pixels for 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.

Solution and Proof

Algorithmically, we can write out the solution in the following way:


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}
Where R, G, B and A are an array of channels from N pixels. We can show
that this conversion is equivalent to averaging through the following:
Suppose we knew the resulting background of the sub-pixels in question,
and it was the same for all of them, equal to some (R, G, B, A). Then, the N

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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy