Path: blob/master/src/java.desktop/share/native/libawt/java2d/loops/AlphaMacros.c
41159 views
/*1* Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "AlphaMacros.h"2627/*28* The following equation is used to blend each pixel in a compositing29* operation between two images (a and b). If we have Ca (Component of a)30* and Cb (Component of b) representing the alpha and color components31* of a given pair of corresponding pixels in the two source images,32* then Porter & Duff have defined blending factors Fa (Factor for a)33* and Fb (Factor for b) to represent the contribution of the pixel34* from the corresponding image to the pixel in the result.35*36* Cresult = Fa * Ca + Fb * Cb37*38* The blending factors Fa and Fb are computed from the alpha value of39* the pixel from the "other" source image. Thus, Fa is computed from40* the alpha of Cb and vice versa on a per-pixel basis.41*42* A given factor (Fa or Fb) is computed from the other alpha using43* one of the following blending factor equations depending on the44* blending rule and depending on whether we are computing Fa or Fb:45*46* Fblend = 047* Fblend = ONE48* Fblend = alpha49* Fblend = (ONE - alpha)50*51* The value ONE in these equations represents the same numeric value52* as is used to represent "full coverage" in the alpha component. For53* example it is the value 0xff for 8-bit alpha channels and the value54* 0xffff for 16-bit alpha channels.55*56* Each Porter-Duff blending rule thus defines a pair of the above Fblend57* equations to define Fa and Fb independently and thus to control58* the contributions of the two source pixels to the destination pixel.59*60* Rather than use conditional tests per pixel in the inner loop,61* we note that the following 3 logical and mathematical operations62* can be applied to any alpha value to produce the result of one63* of the 4 Fblend equations:64*65* Fcomp = ((alpha AND Fk1) XOR Fk2) PLUS Fk366*67* Through appropriate choices for the 3 Fk values we can cause68* the result of this Fcomp equation to always match one of the69* defined Fblend equations. More importantly, the Fcomp equation70* involves no conditional tests which can stall pipelined processor71* execution and typically compiles very tightly into 3 machine72* instructions.73*74* For each of the 4 Fblend equations the desired Fk values are75* as follows:76*77* Fblend Fk1 Fk2 Fk378* ------ --- --- ---79* 0 0 0 080* ONE 0 0 ONE81* alpha ONE 0 082* ONE-alpha ONE -1 ONE+183*84* This gives us the following derivations for Fcomp. Note that85* the derivation of the last equation is less obvious so it is86* broken down into steps and uses the well-known equality for87* two's-complement arithmetic "((n XOR -1) PLUS 1) == -n":88*89* ((alpha AND 0 ) XOR 0) PLUS 0 == 090*91* ((alpha AND 0 ) XOR 0) PLUS ONE == ONE92*93* ((alpha AND ONE) XOR 0) PLUS 0 == alpha94*95* ((alpha AND ONE) XOR -1) PLUS ONE+1 ==96* ((alpha XOR -1) PLUS 1) PLUS ONE ==97* (-alpha) PLUS ONE == ONE - alpha98*99* We have assigned each Porter-Duff rule an implicit index for100* simplicity of referring to the rule in parameter lists. For101* a given blending operation which uses a specific rule, we simply102* use the index of that rule to index into a table and load values103* from that table which help us construct the 2 sets of 3 Fk values104* needed for applying that blending rule (one set for Fa and the105* other set for Fb). Since these Fk values depend only on the106* rule we can set them up at the start of the outer loop and only107* need to do the 3 operations in the Fcomp equation twice per108* pixel (once for Fa and again for Fb).109* -------------------------------------------------------------110*/111112/*113* The following definitions represent terms in the Fblend114* equations described above. One "term name" is chosen from115* each of the following 3 pairs of names to define the table116* values for the Fa or the Fb of a given Porter-Duff rule.117*118* AROP_ZERO the first operand is the constant zero119* AROP_ONE the first operand is the constant one120*121* AROP_PLUS the two operands are added together122* AROP_MINUS the second operand is subtracted from the first123*124* AROP_NAUGHT there is no second operand125* AROP_ALPHA the indicated alpha is used for the second operand126*127* These names expand to numeric values which can be conveniently128* combined to produce the 3 Fk values needed for the Fcomp equation.129*130* Note that the numeric values used here are most convenient for131* generating the 3 specific Fk values needed for manipulating images132* with 8-bits of alpha precision. But Fk values for manipulating133* images with other alpha precisions (such as 16-bits) can also be134* derived from these same values using a small amount of bit135* shifting and replication.136*/137#define AROP_ZERO 0x00138#define AROP_ONE 0xff139#define AROP_PLUS 0140#define AROP_MINUS -1141#define AROP_NAUGHT 0x00142#define AROP_ALPHA 0xff143144/*145* This macro constructs a single Fcomp equation table entry from the146* term names for the 3 terms in the corresponding Fblend equation.147*/148#define MAKE_AROPS(add, xor, and) { AROP_ ## add, AROP_ ## and, AROP_ ## xor }149150/*151* These macros define the Fcomp equation table entries for each152* of the 4 Fblend equations described above.153*154* AROPS_ZERO Fblend = 0155* AROPS_ONE Fblend = 1156* AROPS_ALPHA Fblend = alpha157* AROPS_INVALPHA Fblend = (1 - alpha)158*/159#define AROPS_ZERO MAKE_AROPS( ZERO, PLUS, NAUGHT )160#define AROPS_ONE MAKE_AROPS( ONE, PLUS, NAUGHT )161#define AROPS_ALPHA MAKE_AROPS( ZERO, PLUS, ALPHA )162#define AROPS_INVALPHA MAKE_AROPS( ONE, MINUS, ALPHA )163164/*165* This table maps a given Porter-Duff blending rule index to a166* pair of Fcomp equation table entries, one for computing the167* 3 Fk values needed for Fa and another for computing the 3168* Fk values needed for Fb.169*/170AlphaFunc AlphaRules[] = {171{ {0, 0, 0}, {0, 0, 0} }, /* 0 - Nothing */172{ AROPS_ZERO, AROPS_ZERO }, /* 1 - RULE_Clear */173{ AROPS_ONE, AROPS_ZERO }, /* 2 - RULE_Src */174{ AROPS_ONE, AROPS_INVALPHA }, /* 3 - RULE_SrcOver */175{ AROPS_INVALPHA, AROPS_ONE }, /* 4 - RULE_DstOver */176{ AROPS_ALPHA, AROPS_ZERO }, /* 5 - RULE_SrcIn */177{ AROPS_ZERO, AROPS_ALPHA }, /* 6 - RULE_DstIn */178{ AROPS_INVALPHA, AROPS_ZERO }, /* 7 - RULE_SrcOut */179{ AROPS_ZERO, AROPS_INVALPHA }, /* 8 - RULE_DstOut */180{ AROPS_ZERO, AROPS_ONE }, /* 9 - RULE_Dst */181{ AROPS_ALPHA, AROPS_INVALPHA }, /*10 - RULE_SrcAtop */182{ AROPS_INVALPHA, AROPS_ALPHA }, /*11 - RULE_DstAtop */183{ AROPS_INVALPHA, AROPS_INVALPHA }, /*12 - RULE_Xor */184};185186187