javauis/lcdui_akn/javalcdui/src.nokialcdui/CMIDNativeConverter.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  Wrapper for converter used for drawing to Canvas or Image.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include "CMIDNativeConverter.h"
       
    21 #include "CMIDConvertFactory.h"
       
    22 
       
    23 const TInt KAlphaShift = 8;
       
    24 
       
    25 CMIDNativeConverter::CMIDNativeConverter()
       
    26 {
       
    27     iDefaultConverter = TMID8888Format();
       
    28     iAlphaMode = ENone;
       
    29 }
       
    30 
       
    31 CMIDNativeConverter::~CMIDNativeConverter()
       
    32 {
       
    33     delete iNativeFormat;
       
    34 }
       
    35 
       
    36 CMIDNativeConverter* CMIDNativeConverter::NewL(const TMIDBitmapParameters& aParameters)
       
    37 {
       
    38     CMIDNativeConverter* self = new(ELeave)CMIDNativeConverter;
       
    39     CleanupStack::PushL(self);
       
    40     self->ConstructL(aParameters);
       
    41     CleanupStack::Pop(self);
       
    42     return self;
       
    43 }
       
    44 
       
    45 void CMIDNativeConverter::ConstructL(const TMIDBitmapParameters& aParameters)
       
    46 {
       
    47     iAlphaBitmap = aParameters.iAlpha;
       
    48     iAlphaMode = aParameters.iAlphaMode;
       
    49     iScanlength = aParameters.iScanLength;
       
    50 
       
    51     iNativeFormat = CMIDConvertFactory::NewConverterL(aParameters.iFormat);
       
    52     iNativeFormat->InitializeL(aParameters);
       
    53 }
       
    54 
       
    55 void CMIDNativeConverter::Convert(TMIDInternalARGB& aResult, TUint32 aColor) const
       
    56 {
       
    57     iDefaultConverter.Convert(aResult, aColor);
       
    58 }
       
    59 
       
    60 void CMIDNativeConverter::GetPixel(TMIDInternalARGB& aResult) const
       
    61 {
       
    62     if (iNativeFormat->IsAlpha())
       
    63     {
       
    64         iNativeFormat->GetPixelWithAlpha(aResult);
       
    65     }
       
    66     iNativeFormat->GetPixel(aResult);
       
    67 }
       
    68 
       
    69 void CMIDNativeConverter::PlotPixel(const TMIDInternalARGB& aInternal)
       
    70 {
       
    71     iNativeFormat->PlotPixel(aInternal);
       
    72 }
       
    73 
       
    74 void CMIDNativeConverter::PlotPixelWithAlpha(const TMIDInternalARGB& aInternal)
       
    75 {
       
    76     if (aInternal.iA == KAlphaFullTransparent)
       
    77     {
       
    78         // totally transparent, no need to do anything
       
    79         return;
       
    80     }
       
    81 
       
    82     if (aInternal.iA == KAlphaFullOpaque)
       
    83     {
       
    84         iNativeFormat->PlotPixel(aInternal);
       
    85     }
       
    86     else
       
    87     {
       
    88         // calculating partial transparency
       
    89         TMIDInternalARGB alphaPixel = aInternal;
       
    90         TMIDInternalARGB belowPixel;
       
    91         iNativeFormat->GetPixel(belowPixel);
       
    92         CalculateAlpha(alphaPixel, belowPixel);
       
    93         iNativeFormat->PlotPixel(alphaPixel);
       
    94     }
       
    95 }
       
    96 
       
    97 void CMIDNativeConverter::PlotPixelWithAlphaBitmap(const TMIDInternalARGB& aInternal)
       
    98 {
       
    99     if (aInternal.iA == KAlphaFullTransparent)
       
   100     {
       
   101         // totally transparent, no need to do anything
       
   102         return;
       
   103     }
       
   104     if (aInternal.iA == KAlphaFullOpaque)
       
   105     {
       
   106         iNativeFormat->PlotPixelWithAlpha(aInternal);
       
   107     }
       
   108     else
       
   109     {
       
   110         // calculating partial transparency
       
   111         TMIDInternalARGB alphaPixel = aInternal;
       
   112         TMIDInternalARGB belowPixel;
       
   113         iNativeFormat->GetPixel(belowPixel);
       
   114         CalculateAlpha(alphaPixel, belowPixel);
       
   115         iNativeFormat->PlotPixelWithAlpha(alphaPixel);
       
   116     }
       
   117 }
       
   118 
       
   119 void CMIDNativeConverter::SetBitmaps(TUint32* aBitmap)
       
   120 {
       
   121     iNativeFormat->SetBitmap(aBitmap);
       
   122 }
       
   123 
       
   124 void CMIDNativeConverter::SetDrawRect(TRect aDrawRect)
       
   125 {
       
   126     iDrawRect = aDrawRect;
       
   127     iPosition = aDrawRect.iTl;
       
   128     iNativeFormat->iOffset = iDrawRect.iTl.iX + iDrawRect.iTl.iY * iScanlength;
       
   129 }
       
   130 
       
   131 void CMIDNativeConverter::PlotPixel(const TPoint& aPos,
       
   132                                     const TMIDInternalARGB& aInternal, const TRect& aClip)
       
   133 {
       
   134     // is in clip rect
       
   135     if (aClip.Contains(aPos))
       
   136     {
       
   137         iNativeFormat->iOffset = aPos.iX + aPos.iY * iScanlength;
       
   138         PlotPixelWithAlphaBitmap(aInternal);
       
   139     }
       
   140 }
       
   141 
       
   142 void CMIDNativeConverter::DrawScanLine(TPoint aPos, TInt aLength,
       
   143                                        const TMIDInternalARGB& aInternal, const TRect& aClip)
       
   144 {
       
   145 
       
   146     if ((aPos.iY < aClip.iTl.iY) ||
       
   147             (aPos.iY >= aClip.iBr.iY)||
       
   148             (aPos.iX > aClip.iBr.iX))
       
   149     {
       
   150         return;
       
   151     }
       
   152     if (aPos.iX > aClip.iBr.iX)
       
   153     {
       
   154         return;
       
   155     }
       
   156     if (aPos.iX < aClip.iTl.iX)
       
   157     {
       
   158         aLength -= aClip.iTl.iX - aPos.iX;
       
   159         aPos.iX = aClip.iTl.iX;
       
   160     }
       
   161     if (aPos.iX + aLength > aClip.iBr.iX)
       
   162     {
       
   163         aLength = aClip.iBr.iX - aPos.iX;
       
   164     }
       
   165 
       
   166     iNativeFormat->iOffset = aPos.iX + aPos.iY * iScanlength;
       
   167     for (TInt i = 0; i < aLength; i++)
       
   168     {
       
   169         PlotPixelWithAlphaBitmap(aInternal);
       
   170         ++(iNativeFormat->iOffset);
       
   171     }
       
   172 }
       
   173 
       
   174 void CMIDNativeConverter::CalculateAlpha(TMIDInternalARGB& aTargetColor,
       
   175         const TMIDInternalARGB& aBelowColor)
       
   176 {
       
   177     TInt alphaBelow(KAlphaFullOpaque - aTargetColor.iA + 1);
       
   178     TInt alpha(aTargetColor.iA);
       
   179 
       
   180     if (aBelowColor.iA + aTargetColor.iA > KAlphaFullOpaque)
       
   181     {
       
   182         aTargetColor.iA = KAlphaFullOpaque;
       
   183     }
       
   184     else
       
   185     {
       
   186         aTargetColor.iA = (TUint8)(aBelowColor.iA + aTargetColor.iA);
       
   187     }
       
   188 
       
   189     aTargetColor.iR = (TUint8)((aBelowColor.iR * alphaBelow +
       
   190                                 aTargetColor.iR * alpha) >> KAlphaShift);
       
   191 
       
   192     aTargetColor.iG = (TUint8)((aBelowColor.iG * alphaBelow +
       
   193                                 aTargetColor.iG * alpha) >> KAlphaShift);
       
   194 
       
   195     aTargetColor.iB = (TUint8)((aBelowColor.iB * alphaBelow +
       
   196                                 aTargetColor.iB * alpha) >> KAlphaShift);
       
   197 
       
   198 }
       
   199 
       
   200 TBool CMIDNativeConverter::HasAlphaBitmap()
       
   201 {
       
   202     return iNativeFormat->IsAlpha();
       
   203 }