Cross Reference: AlphaMacros.c
xref
: /
openjdk7
/
jdk
/
src
/
share
/
native
/
sun
/
java2d
/
loops
/
AlphaMacros.c
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
0
N/A
/*
2362
N/A
* Copyright (c) 2000, 2002, Oracle
and
/
or
its affiliates. All rights reserved.
0
N/A
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0
N/A
*
0
N/A
* This code is free software; you can redistribute it
and
/
or
modify it
0
N/A
* under the terms of the GNU General Public License version 2 only, as
2362
N/A
* published by the Free Software Foundation. Oracle designates this
0
N/A
* particular file as subject to the "Classpath" exception as provided
2362
N/A
* by Oracle in the LICENSE file that accompanied this code.
0
N/A
*
0
N/A
* This code is distributed in the hope that it will be useful, but WITHOUT
0
N/A
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0
N/A
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0
N/A
* version 2 for more details (a copy is included in the LICENSE file that
0
N/A
* accompanied this code).
0
N/A
*
0
N/A
* You should have received a copy of the GNU General Public License version
0
N/A
* 2 along with this work; if not, write to the Free Software Foundation,
0
N/A
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0
N/A
*
2362
N/A
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362
N/A
* or visit
www.oracle.c
om if you need additional information or have any
2362
N/A
* questions.
0
N/A
*/
0
N/A
0
N/A
#
include
"
AlphaMacros.h
"
0
N/A
0
N/A
/*
0
N/A
* The following equation is used to blend each pixel in a compositing
0
N/A
* operation between two images (a and b). If we have Ca (Component of a)
0
N/A
* and Cb (Component of b) representing the alpha and color components
0
N/A
* of a given pair of corresponding pixels in the two source images,
0
N/A
* then Porter & Duff have defined blending factors Fa (Factor for a)
0
N/A
* and Fb (Factor for b) to represent the contribution of the pixel
0
N/A
* from the corresponding image to the pixel in the result.
0
N/A
*
0
N/A
* Cresult = Fa * Ca + Fb * Cb
0
N/A
*
0
N/A
* The blending factors Fa and Fb are computed from the alpha value of
0
N/A
* the pixel from the "other" source image. Thus, Fa is computed from
0
N/A
* the alpha of Cb and vice versa on a per-pixel basis.
0
N/A
*
0
N/A
* A given factor (Fa or Fb) is computed from the other alpha using
0
N/A
* one of the following blending factor equations depending on the
0
N/A
* blending rule and depending on whether we are computing Fa or Fb:
0
N/A
*
0
N/A
* Fblend = 0
0
N/A
* Fblend = ONE
0
N/A
* Fblend = alpha
0
N/A
* Fblend = (ONE - alpha)
0
N/A
*
0
N/A
* The value ONE in these equations represents the same numeric value
0
N/A
* as is used to represent "full coverage" in the alpha component. For
0
N/A
* example it is the value 0xff for 8-bit alpha channels and the value
0
N/A
* 0xffff for 16-bit alpha channels.
0
N/A
*
0
N/A
* Each Porter-Duff blending rule thus defines a pair of the above Fblend
0
N/A
* equations to define Fa and Fb independently and thus to control
0
N/A
* the contributions of the two source pixels to the destination pixel.
0
N/A
*
0
N/A
* Rather than use conditional tests per pixel in the inner loop,
0
N/A
* we note that the following 3 logical and mathematical operations
0
N/A
* can be applied to any alpha value to produce the result of one
0
N/A
* of the 4 Fblend equations:
0
N/A
*
0
N/A
* Fcomp = ((alpha AND Fk1) XOR Fk2) PLUS Fk3
0
N/A
*
0
N/A
* Through appropriate choices for the 3 Fk values we can cause
0
N/A
* the result of this Fcomp equation to always match one of the
0
N/A
* defined Fblend equations. More importantly, the Fcomp equation
0
N/A
* involves no conditional tests which can stall pipelined processor
0
N/A
* execution and typically compiles very tightly into 3 machine
0
N/A
* instructions.
0
N/A
*
0
N/A
* For each of the 4 Fblend equations the desired Fk values are
0
N/A
* as follows:
0
N/A
*
0
N/A
* Fblend Fk1 Fk2 Fk3
0
N/A
* ------ --- --- ---
0
N/A
* 0 0 0 0
0
N/A
* ONE 0 0 ONE
0
N/A
* alpha ONE 0 0
0
N/A
* ONE-alpha ONE -1 ONE+1
0
N/A
*
0
N/A
* This gives us the following derivations for Fcomp. Note that
0
N/A
* the derivation of the last equation is less obvious so it is
0
N/A
* broken down into steps and uses the well-known equality for
0
N/A
* two's-complement arithmetic "((n XOR -1) PLUS 1) == -n":
0
N/A
*
0
N/A
* ((alpha AND 0 ) XOR 0) PLUS 0 == 0
0
N/A
*
0
N/A
* ((alpha AND 0 ) XOR 0) PLUS ONE == ONE
0
N/A
*
0
N/A
* ((alpha AND ONE) XOR 0) PLUS 0 == alpha
0
N/A
*
0
N/A
* ((alpha AND ONE) XOR -1) PLUS ONE+1 ==
0
N/A
* ((alpha XOR -1) PLUS 1) PLUS ONE ==
0
N/A
* (-alpha) PLUS ONE == ONE - alpha
0
N/A
*
0
N/A
* We have assigned each Porter-Duff rule an implicit index for
0
N/A
* simplicity of referring to the rule in parameter lists. For
0
N/A
* a given blending operation which uses a specific rule, we simply
0
N/A
* use the index of that rule to index into a table and load values
0
N/A
* from that table which help us construct the 2 sets of 3 Fk values
0
N/A
* needed for applying that blending rule (one set for Fa and the
0
N/A
* other set for Fb). Since these Fk values depend only on the
0
N/A
* rule we can set them up at the start of the outer loop and only
0
N/A
* need to do the 3 operations in the Fcomp equation twice per
0
N/A
* pixel (once for Fa and again for Fb).
0
N/A
* -------------------------------------------------------------
0
N/A
*/
0
N/A
0
N/A
/*
0
N/A
* The following definitions represent terms in the Fblend
0
N/A
* equations described above. One "term name" is chosen from
0
N/A
* each of the following 3 pairs of names to define the table
0
N/A
* values for the Fa or the Fb of a given Porter-Duff rule.
0
N/A
*
0
N/A
* AROP_ZERO the first operand is the constant zero
0
N/A
* AROP_ONE the first operand is the constant one
0
N/A
*
0
N/A
* AROP_PLUS the two operands are added together
0
N/A
* AROP_MINUS the second operand is subtracted from the first
0
N/A
*
0
N/A
* AROP_NAUGHT there is no second operand
0
N/A
* AROP_ALPHA the indicated alpha is used for the second operand
0
N/A
*
0
N/A
* These names expand to numeric values which can be conveniently
0
N/A
* combined to produce the 3 Fk values needed for the Fcomp equation.
0
N/A
*
0
N/A
* Note that the numeric values used here are most convenient for
0
N/A
* generating the 3 specific Fk values needed for manipulating images
0
N/A
* with 8-bits of alpha precision. But Fk values for manipulating
0
N/A
* images with other alpha precisions (such as 16-bits) can also be
0
N/A
* derived from these same values using a small amount of bit
0
N/A
* shifting and replication.
0
N/A
*/
0
N/A
#
define
AROP_ZERO
0x00
0
N/A
#
define
AROP_ONE
0xff
0
N/A
#
define
AROP_PLUS
0
0
N/A
#
define
AROP_MINUS
-
1
0
N/A
#
define
AROP_NAUGHT
0x00
0
N/A
#
define
AROP_ALPHA
0xff
0
N/A
0
N/A
/*
0
N/A
* This macro constructs a single Fcomp equation table entry from the
0
N/A
* term names for the 3 terms in the corresponding Fblend equation.
0
N/A
*/
0
N/A
#
define
MAKE_AROPS
(
add
,
xor
,
and
) {
AROP_
##
add
,
AROP_
##
and
,
AROP_
##
xor
}
0
N/A
0
N/A
/*
0
N/A
* These macros define the Fcomp equation table entries for each
0
N/A
* of the 4 Fblend equations described above.
0
N/A
*
0
N/A
* AROPS_ZERO Fblend = 0
0
N/A
* AROPS_ONE Fblend = 1
0
N/A
* AROPS_ALPHA Fblend = alpha
0
N/A
* AROPS_INVALPHA Fblend = (1 - alpha)
0
N/A
*/
0
N/A
#
define
AROPS_ZERO
MAKE_AROPS
(
ZERO
,
PLUS
,
NAUGHT
)
0
N/A
#
define
AROPS_ONE
MAKE_AROPS
(
ONE
,
PLUS
,
NAUGHT
)
0
N/A
#
define
AROPS_ALPHA
MAKE_AROPS
(
ZERO
,
PLUS
,
ALPHA
)
0
N/A
#
define
AROPS_INVALPHA
MAKE_AROPS
(
ONE
,
MINUS
,
ALPHA
)
0
N/A
0
N/A
/*
0
N/A
* This table maps a given Porter-Duff blending rule index to a
0
N/A
* pair of Fcomp equation table entries, one for computing the
0
N/A
* 3 Fk values needed for Fa and another for computing the 3
0
N/A
* Fk values needed for Fb.
0
N/A
*/
0
N/A
AlphaFunc
AlphaRules
[] = {
0
N/A
{ {0, 0, 0}, {0, 0, 0} },
/* 0 - Nothing */
0
N/A
{
AROPS_ZERO
,
AROPS_ZERO
},
/* 1 - RULE_Clear */
0
N/A
{
AROPS_ONE
,
AROPS_ZERO
},
/* 2 - RULE_Src */
0
N/A
{
AROPS_ONE
,
AROPS_INVALPHA
},
/* 3 - RULE_SrcOver */
0
N/A
{
AROPS_INVALPHA
,
AROPS_ONE
},
/* 4 - RULE_DstOver */
0
N/A
{
AROPS_ALPHA
,
AROPS_ZERO
},
/* 5 - RULE_SrcIn */
0
N/A
{
AROPS_ZERO
,
AROPS_ALPHA
},
/* 6 - RULE_DstIn */
0
N/A
{
AROPS_INVALPHA
,
AROPS_ZERO
},
/* 7 - RULE_SrcOut */
0
N/A
{
AROPS_ZERO
,
AROPS_INVALPHA
},
/* 8 - RULE_DstOut */
0
N/A
{
AROPS_ZERO
,
AROPS_ONE
},
/* 9 - RULE_Dst */
0
N/A
{
AROPS_ALPHA
,
AROPS_INVALPHA
},
/*10 - RULE_SrcAtop */
0
N/A
{
AROPS_INVALPHA
,
AROPS_ALPHA
},
/*11 - RULE_DstAtop */
0
N/A
{
AROPS_INVALPHA
,
AROPS_INVALPHA
},
/*12 - RULE_Xor */
0
N/A
};