jdk/src/java.desktop/share/native/libawt/sun/java2d/loops/AlphaMacros.c
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 5506 jdk/src/share/native/sun/java2d/loops/AlphaMacros.c@202f599c92aa
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "AlphaMacros.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The following equation is used to blend each pixel in a compositing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * operation between two images (a and b).  If we have Ca (Component of a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * and Cb (Component of b) representing the alpha and color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * of a given pair of corresponding pixels in the two source images,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * then Porter & Duff have defined blending factors Fa (Factor for a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * and Fb (Factor for b) to represent the contribution of the pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * from the corresponding image to the pixel in the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *    Cresult = Fa * Ca + Fb * Cb
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The blending factors Fa and Fb are computed from the alpha value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the pixel from the "other" source image.  Thus, Fa is computed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the alpha of Cb and vice versa on a per-pixel basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A given factor (Fa or Fb) is computed from the other alpha using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * one of the following blending factor equations depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * blending rule and depending on whether we are computing Fa or Fb:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *    Fblend = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *    Fblend = ONE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *    Fblend = alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *    Fblend = (ONE - alpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The value ONE in these equations represents the same numeric value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * as is used to represent "full coverage" in the alpha component.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * example it is the value 0xff for 8-bit alpha channels and the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * 0xffff for 16-bit alpha channels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Each Porter-Duff blending rule thus defines a pair of the above Fblend
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * equations to define Fa and Fb independently and thus to control
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * the contributions of the two source pixels to the destination pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Rather than use conditional tests per pixel in the inner loop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * we note that the following 3 logical and mathematical operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * can be applied to any alpha value to produce the result of one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * of the 4 Fblend equations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *    Fcomp = ((alpha AND Fk1) XOR Fk2) PLUS Fk3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Through appropriate choices for the 3 Fk values we can cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * the result of this Fcomp equation to always match one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * defined Fblend equations.  More importantly, the Fcomp equation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * involves no conditional tests which can stall pipelined processor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * execution and typically compiles very tightly into 3 machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * instructions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * For each of the 4 Fblend equations the desired Fk values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *       Fblend            Fk1        Fk2       Fk3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *       ------            ---        ---       ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *          0               0          0         0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *         ONE              0          0        ONE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *        alpha            ONE         0         0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *      ONE-alpha          ONE        -1       ONE+1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * This gives us the following derivations for Fcomp.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * the derivation of the last equation is less obvious so it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * broken down into steps and uses the well-known equality for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * two's-complement arithmetic "((n XOR -1) PLUS 1) == -n":
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *     ((alpha AND  0 ) XOR  0) PLUS   0        == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *     ((alpha AND  0 ) XOR  0) PLUS  ONE       == ONE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *     ((alpha AND ONE) XOR  0) PLUS   0        == alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *     ((alpha AND ONE) XOR -1) PLUS ONE+1      ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *         ((alpha XOR -1) PLUS 1) PLUS ONE     ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *         (-alpha) PLUS ONE                    == ONE - alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * We have assigned each Porter-Duff rule an implicit index for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * simplicity of referring to the rule in parameter lists.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * a given blending operation which uses a specific rule, we simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * use the index of that rule to index into a table and load values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * from that table which help us construct the 2 sets of 3 Fk values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * needed for applying that blending rule (one set for Fa and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * other set for Fb).  Since these Fk values depend only on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * rule we can set them up at the start of the outer loop and only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * need to do the 3 operations in the Fcomp equation twice per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * pixel (once for Fa and again for Fb).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * -------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * The following definitions represent terms in the Fblend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * equations described above.  One "term name" is chosen from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * each of the following 3 pairs of names to define the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * values for the Fa or the Fb of a given Porter-Duff rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *    AROP_ZERO     the first operand is the constant zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *    AROP_ONE      the first operand is the constant one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *    AROP_PLUS     the two operands are added together
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *    AROP_MINUS    the second operand is subtracted from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *    AROP_NAUGHT   there is no second operand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *    AROP_ALPHA    the indicated alpha is used for the second operand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * These names expand to numeric values which can be conveniently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * combined to produce the 3 Fk values needed for the Fcomp equation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * Note that the numeric values used here are most convenient for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * generating the 3 specific Fk values needed for manipulating images
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * with 8-bits of alpha precision.  But Fk values for manipulating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * images with other alpha precisions (such as 16-bits) can also be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * derived from these same values using a small amount of bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * shifting and replication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
#define AROP_ZERO       0x00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
#define AROP_ONE        0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
#define AROP_PLUS       0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
#define AROP_MINUS      -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
#define AROP_NAUGHT     0x00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
#define AROP_ALPHA      0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * This macro constructs a single Fcomp equation table entry from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * term names for the 3 terms in the corresponding Fblend equation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
#define MAKE_AROPS(add, xor, and)  { AROP_ ## add, AROP_ ## and, AROP_ ## xor }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * These macros define the Fcomp equation table entries for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * of the 4 Fblend equations described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *    AROPS_ZERO      Fblend = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *    AROPS_ONE       Fblend = 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *    AROPS_ALPHA     Fblend = alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *    AROPS_INVALPHA  Fblend = (1 - alpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
#define AROPS_ZERO      MAKE_AROPS( ZERO, PLUS,  NAUGHT )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
#define AROPS_ONE       MAKE_AROPS( ONE,  PLUS,  NAUGHT )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
#define AROPS_ALPHA     MAKE_AROPS( ZERO, PLUS,  ALPHA  )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
#define AROPS_INVALPHA  MAKE_AROPS( ONE,  MINUS, ALPHA  )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * This table maps a given Porter-Duff blending rule index to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * pair of Fcomp equation table entries, one for computing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * 3 Fk values needed for Fa and another for computing the 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * Fk values needed for Fb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
AlphaFunc AlphaRules[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {   {0, 0, 0},      {0, 0, 0}       },      /* 0 - Nothing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    {   AROPS_ZERO,     AROPS_ZERO      },      /* 1 - RULE_Clear */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    {   AROPS_ONE,      AROPS_ZERO      },      /* 2 - RULE_Src */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    {   AROPS_ONE,      AROPS_INVALPHA  },      /* 3 - RULE_SrcOver */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    {   AROPS_INVALPHA, AROPS_ONE       },      /* 4 - RULE_DstOver */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    {   AROPS_ALPHA,    AROPS_ZERO      },      /* 5 - RULE_SrcIn */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    {   AROPS_ZERO,     AROPS_ALPHA     },      /* 6 - RULE_DstIn */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    {   AROPS_INVALPHA, AROPS_ZERO      },      /* 7 - RULE_SrcOut */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    {   AROPS_ZERO,     AROPS_INVALPHA  },      /* 8 - RULE_DstOut */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    {   AROPS_ZERO,     AROPS_ONE       },      /* 9 - RULE_Dst */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    {   AROPS_ALPHA,    AROPS_INVALPHA  },      /*10 - RULE_SrcAtop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    {   AROPS_INVALPHA, AROPS_ALPHA     },      /*11 - RULE_DstAtop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    {   AROPS_INVALPHA, AROPS_INVALPHA  },      /*12 - RULE_Xor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
};