skins/AknSkins/rlpluginsrc/AknsRlEffectPluginInvert.cpp
changeset 0 05e9090e2422
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Inverts bitmap channels.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "AknsRlEffectPluginInvert.h"
       
    21 #include "AknsRlEffectUtil.h"
       
    22 
       
    23 // ==================TEMPLATE IMPL. OF INVERT ==================================
       
    24 /**
       
    25 * Template implementation of Invert. Type defines the used data type for
       
    26 * iterating over the bitmap data. X, R, G and B define the used pixel color bit
       
    27 * layout.
       
    28 */
       
    29 template<class Type,TInt X, TInt R, TInt G, TInt B>
       
    30 class AknsRlEffectInvert
       
    31     {
       
    32     public:
       
    33     //------------------------------------------------------------------------
       
    34     static void Process( const CFbsBitmap& aTarget,
       
    35                          const CFbsBitmap& aSource,
       
    36                          const TUint8 aChannelMask,
       
    37                          const TInt aBlendFactor )
       
    38         {
       
    39         // ScanLineLength returns bytes, but width must match the Type
       
    40         TInt width  = CFbsBitmap::ScanLineLength( aSource.SizeInPixels().iWidth,
       
    41                                                   aSource.DisplayMode() ) / sizeof(Type);
       
    42         TInt height = aSource.SizeInPixels().iHeight;
       
    43 
       
    44         TInt pixelCount = width * height;
       
    45         TInt r,g,b;
       
    46 
       
    47         aTarget.LockHeap( ETrue ); // Lock the global bitmap heap
       
    48         Type* dataT = reinterpret_cast<Type*>( aTarget.DataAddress() );
       
    49         Type* dataS = reinterpret_cast<Type*>( aSource.DataAddress() );
       
    50 
       
    51         for( TInt index = 0; index < pixelCount; ++index )
       
    52             {
       
    53             r = AknsRlRgb<Type,X,R,G,B>::R8(*dataS);
       
    54             g = AknsRlRgb<Type,X,R,G,B>::G8(*dataS);
       
    55             b = AknsRlRgb<Type,X,R,G,B>::B8(*dataS);
       
    56 
       
    57             if( CAknsRlEffectPluginInvert::EMaskR & aChannelMask )
       
    58                 r = 255 - r;
       
    59             if( CAknsRlEffectPluginInvert::EMaskG & aChannelMask )
       
    60                 g = 255 - g;
       
    61             if( CAknsRlEffectPluginInvert::EMaskB & aChannelMask )
       
    62                 b = 255 - b;
       
    63 
       
    64             // Exposure blending
       
    65             // Note: It is assumed that arithmetic shifting is supported
       
    66             // -> negative values are shifted correctly
       
    67             r = ( r * aBlendFactor + (255 - aBlendFactor) * AknsRlRgb<Type,X,R,G,B>::R8(*dataS) ) >> 8;
       
    68             g = ( g * aBlendFactor + (255 - aBlendFactor) * AknsRlRgb<Type,X,R,G,B>::G8(*dataS) ) >> 8;
       
    69             b = ( b * aBlendFactor + (255 - aBlendFactor) * AknsRlRgb<Type,X,R,G,B>::B8(*dataS) ) >> 8;
       
    70 
       
    71             if( r < 0 ) r = 0; else if( r > 255 ) r = 255;
       
    72             if( g < 0 ) g = 0; else if( g > 255 ) g = 255;
       
    73             if( b < 0 ) b = 0; else if( b > 255 ) b = 255;
       
    74 
       
    75             AknsRlRgb<Type,X,R,G,B>::SetRgb8( dataT, TUint8(r), TUint8(g), TUint8(b) );
       
    76 
       
    77             dataT++;
       
    78             dataS++;
       
    79             }
       
    80 
       
    81         aTarget.UnlockHeap( ETrue ); // Unlock the global bitmap heap
       
    82         }
       
    83     };
       
    84 
       
    85 // ================== GRAYSCALE IMPL. OF INVERT ================================
       
    86 class AknsRlEffectInvertGray
       
    87     {
       
    88     public:
       
    89     //------------------------------------------------------------------------
       
    90     static void Process( const CFbsBitmap& aTarget,
       
    91                          const CFbsBitmap& aSource,
       
    92                          const TInt aBlendFactor )
       
    93         {
       
    94         TInt width = CFbsBitmap::ScanLineLength( aSource.SizeInPixels().iWidth,
       
    95                                                  aSource.DisplayMode() );
       
    96         TInt height = aSource.SizeInPixels().iHeight;
       
    97 
       
    98         TInt pixelCount = width * height;
       
    99         TInt shade;
       
   100 
       
   101         aTarget.LockHeap( ETrue ); // Lock the global bitmap heap
       
   102         TUint8* dataT = reinterpret_cast<TUint8*>( aTarget.DataAddress() );
       
   103         TUint8* dataS = reinterpret_cast<TUint8*>( aSource.DataAddress() );
       
   104 
       
   105         for( TInt index = 0; index < pixelCount; ++index )
       
   106             {
       
   107             shade = 255 - (*dataS);
       
   108 
       
   109             // Exposure blending
       
   110             // Note: It is assumed that arithmetic shifting is supported
       
   111             // -> negative values are shifted correctly
       
   112             shade = ( shade * aBlendFactor + (255 - aBlendFactor) * (*dataS) ) >> 8; //lint !e702 Arithmetic shifting assumed
       
   113 
       
   114             if( shade < 0 ) shade = 0;
       
   115             else if( shade > 255 ) shade = 255;
       
   116 
       
   117             *dataT = TUint8(shade);
       
   118 
       
   119             dataT++;
       
   120             dataS++;
       
   121             }
       
   122 
       
   123         aTarget.UnlockHeap( ETrue ); // Unlock the global bitmap heap
       
   124         }
       
   125     };
       
   126 
       
   127 // ============================ MEMBER FUNCTIONS ===============================
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CAknsRlEffectPluginInvert::CAknsRlEffectPluginInvert
       
   131 // C++ default constructor can NOT contain any code, that
       
   132 // might leave.
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 CAknsRlEffectPluginInvert::CAknsRlEffectPluginInvert()
       
   136     {
       
   137     }
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // Destructor
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 CAknsRlEffectPluginInvert::~CAknsRlEffectPluginInvert()
       
   144     {
       
   145     iContext = NULL; // Removes lint nag
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CAknsRlEffectPluginInvert::EffectUid
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 TUid CAknsRlEffectPluginInvert::EffectUid() const
       
   153     {
       
   154     return TUid::Uid( KAknsRlEffectPluginInvertUID );
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CAknsRlEffectPluginInvert::Effect
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 MAknsRlEffect* CAknsRlEffectPluginInvert::Effect( const TInt aInterface )
       
   162     {
       
   163     if( aInterface == KAknsRlEffectPluginInterfaceEffect )
       
   164         return this;
       
   165     return NULL;
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CAknsRlEffectPluginInvert::InitializeL
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CAknsRlEffectPluginInvert::InitializeL()
       
   173     {
       
   174     iContext = NULL;
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CAknsRlEffectPluginInvert::Release
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 void CAknsRlEffectPluginInvert::Release()
       
   182     {
       
   183     }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // CAknsRlEffectPluginInvert::ActivateL
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 void CAknsRlEffectPluginInvert::ActivateL( MAknsRlEffectContext* aContext )
       
   190     {
       
   191     if( !aContext ) // We absolutely need the context
       
   192         {
       
   193         User::Leave( KErrArgument );
       
   194         }
       
   195 
       
   196     iContext = aContext;
       
   197 
       
   198     iChannelMask = EMaskR | EMaskG | EMaskB;
       
   199     iBlendFactor = 255;
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CAknsRlEffectPluginInvert::Deactivate
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 void CAknsRlEffectPluginInvert::Deactivate()
       
   207     {
       
   208     }
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CAknsRlEffectPluginInvert::SetParametersL
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CAknsRlEffectPluginInvert::SetParametersL( MAknsRlParameterIterator& aParameters )
       
   215     {
       
   216     // Iterate over available parameters
       
   217     while( aParameters.HasNext() )
       
   218         {
       
   219         const TAknsRlParameterData* param = aParameters.NextL();
       
   220 
       
   221         // Fetch channel mask value
       
   222         if( param->iName->Compare( KAknsRlEffectInvertChannels ) == 0 )
       
   223             {
       
   224             if( param->iType != EAknsRlParameterTypeNumber )
       
   225                 User::Leave( KErrArgument );
       
   226 
       
   227             iChannelMask = TUint8(param->iNumber);
       
   228             }
       
   229         // Fetch blend factor value
       
   230         else if( param->iName->Compare( KAknsRlEffectInvertBlendFactor ) == 0 )
       
   231             {
       
   232             if( param->iType != EAknsRlParameterTypeNumber )
       
   233                 User::Leave( KErrArgument );
       
   234 
       
   235             iBlendFactor = param->iNumber;
       
   236             }
       
   237         }
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // CAknsRlEffectPluginInvert::GetCapabilities
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 void CAknsRlEffectPluginInvert::GetCapabilities( TAknsRlEffectCaps& aCaps )
       
   245     {
       
   246     aCaps.iOutputLayerSupport = KAknsRlLayerRGBOnly;
       
   247     aCaps.iInputLayerASupport = KAknsRlLayerRGBOnly;
       
   248     aCaps.iInputLayerBSupport = KAknsRlLayerNone;
       
   249     }
       
   250 
       
   251 // -----------------------------------------------------------------------------
       
   252 // CAknsRlEffectPluginInvert::Render
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 TInt CAknsRlEffectPluginInvert::Render( const TAknsRlRenderOpParam& aParam )
       
   256     {
       
   257     if( !iContext ) // We absolutely need the context
       
   258         {
       
   259         return KErrBadHandle;
       
   260         }
       
   261 
       
   262     // To do anything we need both, the output layer and input layer
       
   263     if( ( aParam.iOutputLayerStatus & KAknsRlLayerRGBOnly ) &&
       
   264         ( aParam.iInputLayerAStatus & KAknsRlLayerRGBOnly ) )
       
   265         {
       
   266         // Query the layers, uninitialized because we process the whole image
       
   267         TAknsRlLayerData dataTarget;
       
   268         TRAPD( err, iContext->GetLayerDataL( dataTarget, aParam.iOutputLayerIndex,
       
   269                                              aParam.iOutputLayerStatus, EFalse ) );
       
   270         if( KErrNone != err )
       
   271             return KErrArgument;
       
   272 
       
   273         TAknsRlLayerData dataSource;
       
   274         TRAP( err, iContext->GetLayerDataL( dataSource, aParam.iInputLayerAIndex,
       
   275                                             aParam.iInputLayerAStatus, EFalse ) );
       
   276         if( KErrNone != err )
       
   277             return KErrArgument;
       
   278 
       
   279         if( !dataTarget.iRGBBitmap ) // We need the target bitmap
       
   280             return KErrBadHandle;
       
   281 
       
   282         if( !dataSource.iRGBBitmap ) // We need the source bitmap
       
   283             return KErrBadHandle;
       
   284 
       
   285         TDisplayMode modeT = dataTarget.iRGBBitmap->DisplayMode();
       
   286         TDisplayMode modeS = dataSource.iRGBBitmap->DisplayMode();
       
   287 
       
   288         // Rgb -> Rgb modes
       
   289         if( EColor64K == modeS && EColor64K == modeT )
       
   290             {
       
   291             AknsRlEffectInvert<TUint16,0,5,6,5>::Process(
       
   292                             *dataTarget.iRGBBitmap,
       
   293                             *dataSource.iRGBBitmap,
       
   294                             iChannelMask,
       
   295                             iBlendFactor );
       
   296 
       
   297             }
       
   298         else if( EColor16MU == modeS && EColor16MU == modeT )
       
   299             {
       
   300             AknsRlEffectInvert<TUint32,8,8,8,8>::Process(
       
   301                             *dataTarget.iRGBBitmap,
       
   302                             *dataSource.iRGBBitmap,
       
   303                             iChannelMask,
       
   304                             iBlendFactor );
       
   305             }
       
   306         // Gray -> Gray mode
       
   307         else if( EGray256 == modeS && EGray256 == modeT )
       
   308             {
       
   309             AknsRlEffectInvertGray::Process(
       
   310                             *dataTarget.iRGBBitmap,
       
   311                             *dataSource.iRGBBitmap,
       
   312                             iBlendFactor );
       
   313             }
       
   314         else
       
   315             {
       
   316             // Provided layers have illegal display mode combination
       
   317             return KErrArgument;
       
   318             }
       
   319         }
       
   320     else
       
   321         {
       
   322         // Required layers were not provided
       
   323         return KErrArgument;
       
   324         }
       
   325 
       
   326     return KErrNone;
       
   327     }
       
   328 
       
   329 // End of File