Demo
The composite demo operates on two source images, two alpha channels
with scrollbars used to control the percentage contribution
of each source image, and a single composite destination image.
With a second alpha channel scrollbar value of 255, the compositing
operation becomes a simple blend operation between the two
source images. The contribution to the destination image is
controlled by the first alpha channel. In the initial state
of the demo, 100% of the second image and 0% of the first
image are set. Please note that the alpha images need not
be constant.
JAI
The JAI composite operation requires six parameters. Two source
images, two alpha images (second one may be null), a boolean
flag indicating whether or not alpha has been multiplied to
both sources and destination, and a "alphaFirst" flag where
true indicates that the alpha channel is the first band of
the destination image.
For example,
pb = new ParameterBlock()
pb.addSource(source1);
pb.addSource(source2);
pb.add(alpha1);
pb.add(null);
pb.add(new Boolean(false));
pb.add(new Boolean(false));
destination = JAI.create("composite", pb, null);
Theory
The composite operation combines two images based on their
alpha values at each pixel. This is done on a per-band
basis. The two source images are expected to have the
same number of bands and the same data type. The
destination image has the same data type as the two
source images but with one extra band. The extra band contains
the resultant alpha channel.
The destination pixel values may be viewed as representing
a fractional pixel coverage or transparency factor.
Specifically, composite implements the Porter-Duff "over"
rule (see Computer Graphics, July 1984) in which the
output color of a pixel with source value/alpha tuples
(A, a)
and (B, b)
is given by
a*A + (1 - a)*(b*B).
The output alpha value
is given by a + (1 - a)*b
. For premultiplied
source tuples (a*A, a)
and (b*B, b)
,
the premultiplied output value is simply (a*A) + (1 - a)*(b*B).
The color channels of the two source images are supplied via
source1
and source2
. The two sources must
be either both pre-multiplied by alpha or not. An alpha channel should
not be included in either source1
or source2
.
The alpha channel of the first source images must be supplied
via the source1Alpha
parameter. This parameter may not
be null. The alpha channel of the second source image may be supplied
via the source2Alpha
parameter. This parameter may be
null, in which case the second source is considered completely opaque.
The alpha images should be single-banded and have the same data type
as well as dimensions as their corresponding source images.
The alphaPremultiplied
parameter indicates whether
or not the supplied alpha image is premultiplied to both the source
images. It also indicates whether the destination image color channels
have the alpha values multiplied to the pixel color values.
The destination image is the combination of the two source images.
It has the color channels and one additional alpha channel (the band
index depends on alphaFirst
parameter). Whether alpha
value is pre-multiplied to the color channels also depends on the value
of alphaPremultiplied
(pre-multiplied if true).