skins/AknSkins/rlpluginsrc/AknsRlEffectUtil.cpp
changeset 0 05e9090e2422
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2005-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Utility class for effects.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 
       
    22 #include "AknsRlEffectUtil.h"
       
    23 
       
    24 // ============================ MEMBER FUNCTIONS ===============================
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // AknsRlUtil::Median9
       
    28 // -----------------------------------------------------------------------------
       
    29 //
       
    30 #define AKNS_RL_EFFECT_SORT(a, b) { if((a) > (b)) { temp=(a); (a) = (b); (b) = temp; } }
       
    31 TUint8 AknsRlUtil::Median9( TUint8 aArray[9] )
       
    32     {
       
    33     TUint8 temp; // Defined for AKNS_RL_EFFECT_SORT macro
       
    34     AKNS_RL_EFFECT_SORT(aArray[1], aArray[2]); AKNS_RL_EFFECT_SORT(aArray[4], aArray[5]); AKNS_RL_EFFECT_SORT(aArray[7], aArray[8]);
       
    35     AKNS_RL_EFFECT_SORT(aArray[0], aArray[1]); AKNS_RL_EFFECT_SORT(aArray[3], aArray[4]); AKNS_RL_EFFECT_SORT(aArray[6], aArray[7]);
       
    36     AKNS_RL_EFFECT_SORT(aArray[1], aArray[2]); AKNS_RL_EFFECT_SORT(aArray[4], aArray[5]); AKNS_RL_EFFECT_SORT(aArray[7], aArray[8]);
       
    37     AKNS_RL_EFFECT_SORT(aArray[0], aArray[3]); AKNS_RL_EFFECT_SORT(aArray[5], aArray[8]); AKNS_RL_EFFECT_SORT(aArray[4], aArray[7]);
       
    38     AKNS_RL_EFFECT_SORT(aArray[3], aArray[6]); AKNS_RL_EFFECT_SORT(aArray[1], aArray[4]); AKNS_RL_EFFECT_SORT(aArray[2], aArray[5]);
       
    39     AKNS_RL_EFFECT_SORT(aArray[4], aArray[7]); AKNS_RL_EFFECT_SORT(aArray[4], aArray[2]); AKNS_RL_EFFECT_SORT(aArray[6], aArray[4]);
       
    40     AKNS_RL_EFFECT_SORT(aArray[4], aArray[2]);
       
    41 
       
    42     return(aArray[4]);
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // AknsRlChannelBlend::Blend
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 TUint8 AknsRlChannelBlend::Blend( const TAknsRlChannelBlendMode aMode,
       
    50                                   const TInt aFactor,
       
    51                                   const TUint8 aA,
       
    52                                   const TUint8 aB )
       
    53     {
       
    54     // Some calculations will result in negative values, res must be signed
       
    55     TInt res = 0;
       
    56 
       
    57     // Note: It is assumed that arithmetic shifting is supported
       
    58     // -> negative values are shifted correctly
       
    59 
       
    60     switch(aMode)
       
    61         {
       
    62         case EAknsRlChannelBlendNormal:
       
    63             {
       
    64             res = aB;
       
    65             }
       
    66             break;
       
    67         case EAknsRlChannelBlendDarken:
       
    68             {
       
    69             if( aA < aB )
       
    70                 res = aA;
       
    71             else
       
    72                 res = aB;
       
    73             }
       
    74             break;
       
    75         case EAknsRlChannelBlendLighten:
       
    76             {
       
    77             if( aA > aB )
       
    78                 res = aA;
       
    79             else
       
    80                 res = aB;
       
    81             }
       
    82             break;
       
    83         case EAknsRlChannelBlendMultiply:
       
    84             {
       
    85             res = ( aA * aB ) >> 8;
       
    86             }
       
    87             break;
       
    88         case EAknsRlChannelBlendScreen:
       
    89             {
       
    90             res = 255 - ((( 255 - aA ) * ( 255 - aB )) >> 8 ); //lint !e702 Arithmetic shifting assumed
       
    91             }
       
    92             break;
       
    93         case EAknsRlChannelBlendOverlay:
       
    94             {
       
    95             if( aA < 127 )
       
    96                 res = 2 * (( aA * aB ) >> 8);
       
    97             else
       
    98                 res = 255 - 2 * ((( 255 - aA ) * ( 255 - aB )) >> 8 ); //lint !e702 Arithmetic shifting assumed
       
    99             }
       
   100             break;
       
   101         case EAknsRlChannelBlendHardLight:
       
   102             {
       
   103             if( aB < 127 )
       
   104                 res = 2 * (( aA * aB ) >> 8 );
       
   105             else
       
   106                 res = 255 - 2 * ((( 255 - aA ) * ( 255 - aB )) >> 8 ); //lint !e702 Arithmetic shifting assumed
       
   107             }
       
   108             break;
       
   109         case EAknsRlChannelBlendSoftLight:
       
   110             {
       
   111             res = 2 * (( aA * aB ) >> 8 ) + (( aA * aA ) >> 8 ) - 2 * (( aA * (( aA * aB ) >> 8 )) >> 8 ); //lint !e702 Arithmetic shifting assumed
       
   112             }
       
   113             break;
       
   114         case EAknsRlChannelBlendDifference:
       
   115             {
       
   116             res = aA - aB;
       
   117             if( res < 0 )
       
   118                 res = -res;
       
   119             }
       
   120             break;
       
   121         case EAknsRlChannelBlendDodge:
       
   122             {
       
   123             if( 255 == aB )
       
   124                 res = 255;
       
   125             else
       
   126                 {
       
   127                 res = ( aA << 8 ) / ( 255 - aB );
       
   128 
       
   129                 // Do clamping here to prevent artefacts in blending
       
   130                 if( res < 0 )
       
   131                     res = 0;
       
   132                 if( res > 255 )
       
   133                     res = 255;
       
   134                 }
       
   135             }
       
   136             break;
       
   137         case EAknsRlChannelBlendBurn:
       
   138             {
       
   139             if( 0 == aB )
       
   140                 res = 0; // This makes burn work as in Paint Shop Pro
       
   141             else
       
   142                 {
       
   143                 res = 255 - ((( 255 - aA ) << 8 ) / aB );
       
   144 
       
   145                 // Do clamping here to prevent artefacts in blending
       
   146                 if( res < 0 )
       
   147                     res = 0;
       
   148                 if( res > 255 )
       
   149                     res = 255;
       
   150                 }
       
   151             }
       
   152             break;
       
   153         default:
       
   154             break;
       
   155         }
       
   156 
       
   157     // Blend between result and the original value
       
   158     res = ( res * aFactor + ( 255 - aFactor ) * aA ) >> 8; //lint !e702 Arithmetic shifting assumed
       
   159 
       
   160     // Clamp the result to range [0, 255]
       
   161     if( res < 0 )
       
   162         return 0;
       
   163     if( res > 255 )
       
   164         return 255;
       
   165 
       
   166     return TUint8(res);
       
   167     }
       
   168 
       
   169 // End of file.