videoeditorengine/vedengine/videoprocessor/src/yuv2rgb24.cpp
changeset 0 951a5db380a0
equal deleted inserted replaced
-1:000000000000 0:951a5db380a0
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "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 * Ixonos Plc
       
    14 *
       
    15 * Description:  
       
    16 * Implementation of class CYuv2Rgb24.
       
    17 * YUV to EColor16M colorspace converter concrete classes. 
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 //  EXTERNAL RESOURCES  
       
    25 
       
    26 
       
    27 //  Include Files  
       
    28 
       
    29 #include <e32math.h>
       
    30 #include "yuvconverter.h"
       
    31 #include "yuv2rgb24.h"
       
    32 #include "brightnesscontrast.h"
       
    33 
       
    34 
       
    35 //  MEMBER FUNCTIONS
       
    36 
       
    37 
       
    38 //=============================================================================
       
    39 
       
    40 /*
       
    41 -----------------------------------------------------------------------------
       
    42 
       
    43     CYuv2Rgb24
       
    44 
       
    45     CYuv2Rgb24()
       
    46 
       
    47     Standard C++ constructor
       
    48 
       
    49 -----------------------------------------------------------------------------
       
    50 */
       
    51 
       
    52 CYuv2Rgb24::CYuv2Rgb24()
       
    53 {
       
    54     iRgbLookupTable = 0;
       
    55     iGamma = 65536;
       
    56     iBrightnessContrast = KMedBrightnessContrastIndex;
       
    57 
       
    58 }
       
    59 
       
    60 
       
    61 
       
    62 /*
       
    63 -----------------------------------------------------------------------------
       
    64 
       
    65     CYuv2Rgb24
       
    66 
       
    67     ~CYuv2Rgb24()
       
    68 
       
    69     Standard C++ destructor
       
    70 
       
    71 -----------------------------------------------------------------------------
       
    72 */
       
    73 
       
    74 CYuv2Rgb24::~CYuv2Rgb24()
       
    75 {
       
    76     User::Free(iRgbLookupTable);
       
    77 }
       
    78 
       
    79 
       
    80 
       
    81 /*
       
    82 -----------------------------------------------------------------------------
       
    83 
       
    84     CYuv2Rgb24
       
    85 
       
    86     ConstructL()
       
    87 
       
    88     Standard Symbian OS second-phase constructor. Initializes the object.
       
    89 
       
    90 -----------------------------------------------------------------------------
       
    91 */
       
    92 
       
    93 void CYuv2Rgb24::ConstructL(TUint aWidth, TUint aHeight, TUint aMaxWidth, TUint aMaxHeight)
       
    94 {
       
    95     // Remember the dimensions
       
    96 //    __ASSERT_ALWAYS(((aWidth & 1) == 0) && ((aHeight & 1) == 0),
       
    97 //                    User::Leave(KErrArgument));
       
    98     iWidth = aWidth;
       
    99     iHeight = aHeight;
       
   100     if ( iWidth > aMaxWidth ) {
       
   101         iCropWidth = (iWidth-aMaxWidth)/2;
       
   102         iWidth = aMaxWidth;
       
   103     }
       
   104     else {
       
   105         iCropWidth = 0;
       
   106     }
       
   107     if ( iHeight > aMaxHeight ) {
       
   108         iCropHeight = (iHeight-aMaxHeight)/2;
       
   109         iHeight = aMaxHeight;
       
   110     }
       
   111     else {
       
   112         iCropHeight = 0;
       
   113     }
       
   114 
       
   115     // Allocate the RGB saturate/gamma lookup table    
       
   116     iRgbLookupTable = (TUint8*) User::AllocL(ESaturateLength);
       
   117 
       
   118     // Initialize brightness & contrast value, this will calculate the conversion table
       
   119     // Since this uses the median index, it makes no difference if the preferred 
       
   120     // enhancement is this or gamma. Furthermore, changes to the value will be done using
       
   121     // the appropriate method.
       
   122     SetBrightnessContrast(KMaxBCInputIndex/2);
       
   123 }
       
   124 
       
   125 
       
   126 
       
   127 /*
       
   128 -----------------------------------------------------------------------------
       
   129 
       
   130     CYuv2Rgb24
       
   131 
       
   132     SetGamma()
       
   133 
       
   134     Sets the conversion gamma value and recalculates the look-up table
       
   135 
       
   136 -----------------------------------------------------------------------------
       
   137 */
       
   138 
       
   139 void CYuv2Rgb24::SetGamma(TInt aGamma)
       
   140 {
       
   141     TInt i, v;
       
   142     TReal vNorm;
       
   143     
       
   144     // Remember gamma and convert it to floating point
       
   145     iGamma = aGamma;
       
   146     TReal fGamma = TReal(iGamma) / TReal(65536);
       
   147 
       
   148     // Calculate table entries for all possible RGB values:
       
   149     for ( i = 0; i < ESaturateLength; i++ )
       
   150     {
       
   151         // Actual RGB value for this table index
       
   152         v = i - ESaturateOffset;
       
   153 
       
   154         // Saturate if <0 or >255, otherwise calculate gamma       
       
   155         if ( v < 0 )
       
   156             v = 0;
       
   157         else if ( v > 255 )
       
   158             v = 255;
       
   159         else
       
   160         {
       
   161             // Normalize v:
       
   162             vNorm = TReal(v) / TReal(255);
       
   163 
       
   164             // Gamma-correct: v = v ^ gamma
       
   165             Math::Pow(vNorm, vNorm, fGamma);
       
   166 
       
   167             // Scale back to [0..255] and clamp:
       
   168             vNorm = (TReal(255) * vNorm) + 0.5;
       
   169             v = (TInt) vNorm;
       
   170             if ( v < 0 ) v = 0;
       
   171             if ( v > 255 ) v = 255;
       
   172         }
       
   173 
       
   174         // 24bpp RGB has range [0..255] for all components, store to table:
       
   175         iRgbLookupTable[i] = (TUint8) v;
       
   176     }
       
   177 }
       
   178 
       
   179 /*
       
   180 -----------------------------------------------------------------------------
       
   181 
       
   182     CYuv2Rgb24
       
   183 
       
   184     SetBrightnessContrast()
       
   185 
       
   186     Sets the index to the predefined brightness&contrast lookup table 
       
   187     (KBrightnessContrastEnhParam) and recalculates the RGB look-up table
       
   188     The algorithm was developed by IMAAMI for Kenny display.
       
   189 
       
   190 -----------------------------------------------------------------------------
       
   191 */
       
   192 void CYuv2Rgb24::SetBrightnessContrast(TInt aBCIndex)
       
   193 {
       
   194     TInt i, v;
       
   195     TReal vNorm;
       
   196     
       
   197     // Convert & remember brightness-contrast index. aBCIndex == 0 to KMaxBCInputIndex.
       
   198     iBrightnessContrast = (aBCIndex*KMaxBrightnessContrastIndex)/KMaxBCInputIndex;
       
   199 
       
   200     // Calculate table entries for all possible RGB values:
       
   201     for ( i = 0; i < ESaturateLength; i++ )
       
   202     {
       
   203         // Actual RGB value for this table index
       
   204         v = 298 * (i - ESaturateOffset - 16) / 256;
       
   205         // (see Convert())
       
   206 
       
   207         // Saturate if <0 or >255, otherwise calculate value
       
   208         if ( v < 0 )
       
   209             v = 0;
       
   210         else if ( v > 255 )
       
   211             v = 255;
       
   212         else
       
   213         {
       
   214 
       
   215             // Normalize v:
       
   216             vNorm = TReal(v) / TReal(255);
       
   217 
       
   218             vNorm = KBrightnessContrastEnhParam[iBrightnessContrast].a * vNorm + KBrightnessContrastEnhParam[iBrightnessContrast].b;
       
   219             if ( vNorm < 0 ) 
       
   220                 vNorm = 0;
       
   221             else if ( vNorm > 1 )
       
   222                 vNorm = 1;
       
   223             Math::Pow( vNorm, vNorm, KBrightnessContrastEnhParam[iBrightnessContrast].g );
       
   224 
       
   225             // Scale back to [0..255] and clamp:
       
   226             vNorm = (TReal(255) * vNorm) + 0.5;
       
   227             v = (TInt) vNorm;
       
   228             if ( v < 0 ) v = 0;
       
   229             if ( v > 255 ) v = 255;
       
   230         }
       
   231 
       
   232         // 24bpp RGB has range [0..255] for all components, store to table:
       
   233         iRgbLookupTable[i] = (TUint8) v;
       
   234     }
       
   235 }
       
   236 
       
   237 
       
   238 /*
       
   239 -----------------------------------------------------------------------------
       
   240 
       
   241     CYuv2Rgb24
       
   242 
       
   243     Convert()
       
   244 
       
   245     Converts a YUV frame to a EColor16M frame
       
   246 
       
   247 -----------------------------------------------------------------------------
       
   248 */
       
   249 
       
   250 void CYuv2Rgb24::Convert(const TUint8 *aYBuf, const TUint8 *aUBuf,
       
   251                          const TUint8 *aVBuf,
       
   252                          TUint aBufWidth, TUint aBufHeight,
       
   253                          TUint8 *aTarget, TUint aTargetScanlineLength)
       
   254 {
       
   255     TUint cols;
       
   256     TUint rows = iHeight;
       
   257     TUint8 *target;
       
   258     TUint8 *target2;
       
   259     const TUint8 *yb, *yb2;
       
   260     TInt rc, gc, bc;
       
   261     TInt y;
       
   262     TInt uval, vval;
       
   263     TUint8 val;
       
   264 
       
   265 
       
   266     __ASSERT_ALWAYS((aBufWidth >= iWidth) && (aBufHeight >= iHeight),
       
   267                     User::Invariant());
       
   268 
       
   269     // Cropping needed? 
       
   270     if ( iCropWidth > 0 ) {
       
   271         //sets offset to buffers; from now on increments below will always result the same offset, since the increment is aBufWidth
       
   272         aYBuf += iCropWidth;
       
   273         aUBuf += iCropWidth/2;
       
   274         aVBuf += iCropWidth/2;
       
   275     }
       
   276     if ( iCropHeight > 0 ) {
       
   277         //skip lines on top
       
   278         aYBuf += iCropHeight*aBufWidth;
       
   279         aUBuf += (iCropHeight/2)*aBufWidth/2;
       
   280         aVBuf += (iCropHeight/2)*aBufWidth/2;
       
   281     }
       
   282     // We don't interpolate the chrominance values at all, since that way we
       
   283     // can save a lot of multiplications. This actually doesn't affect the
       
   284     // subjective picture quality much, if at all, with natural images.
       
   285 
       
   286     // Conversion is done 2x2 pixels at a time
       
   287 
       
   288     // Luminance-only conversion?
       
   289     if ( (aUBuf != NULL) && (aVBuf != NULL) )
       
   290     {
       
   291         // Full conversion
       
   292         
       
   293         // Convert all rows, two at a time
       
   294         while ( rows )
       
   295         {
       
   296             // Convert all pixels in this row, two at a time
       
   297             cols = iWidth;
       
   298             target = aTarget;
       
   299             target2 = aTarget + aTargetScanlineLength;
       
   300             yb = aYBuf;
       
   301             yb2 = aYBuf + aBufWidth;
       
   302         
       
   303             while ( cols )
       
   304             {
       
   305                 // Charles Poynton: Color FAQ
       
   306                 // (http://www.inforamp.net/~poynton/ColorFAQ.html)
       
   307                 // 30. How do I encode Y'CBCR components from computer R'G'B' ?
       
   308 
       
   309                 // [normalized]
       
   310                 // R =  1.1643828125 * (Y-16)  +  1.59602734375 * (Cr-128)
       
   311                 // G =  1.1643828125 * (Y-16)  +  -0.39178515625 * (Cb-128) + -0.81296875 * (Cr-128)
       
   312                 // B =  1.1643828125 * (Y-16)  +  2.01723046875 * (Cb-128)
       
   313 
       
   314                 // We'll use fixed-point with 16 bits of fractional part for
       
   315                 // accuracy. Besides, 24bpp RGB is not likely to be used in
       
   316                 // low-CPU devices in the near future...
       
   317 
       
   318                 // Red chrominance part for this 2x2 block:
       
   319                 vval = ((TInt) aVBuf[0]) - 128;
       
   320                 rc =  104597 * vval;
       
   321 
       
   322                 // Green chrominance:
       
   323                 uval = ((TInt) aUBuf[0]) - 128;
       
   324                 gc = -25676*uval - 53279*vval;
       
   325 
       
   326                 // Blue chrominance:
       
   327                 bc = 132201 * uval;
       
   328 
       
   329                 // Upper left pixel y part for all components:
       
   330                 y = 76309 * (((TInt) yb[0]) - 16) + 32768; // round up
       
   331 
       
   332                 // Calculate components and store:
       
   333                 // Bitmap format: bbbbbbbb gggggggg rrrrrrrr
       
   334                 target[0] = iRgbLookupTable[((y+bc) >> 16) + ESaturateOffset];
       
   335                 target[1] = iRgbLookupTable[((y+gc) >> 16) + ESaturateOffset];
       
   336                 target[2] = iRgbLookupTable[((y+rc) >> 16) + ESaturateOffset];
       
   337 
       
   338                 // Upper right pixel:
       
   339                 y = 76309 * (((TInt) yb[1]) - 16) + 32768;
       
   340                 target[3] = iRgbLookupTable[((y+bc) >> 16) + ESaturateOffset];
       
   341                 target[4] = iRgbLookupTable[((y+gc) >> 16) + ESaturateOffset];
       
   342                 target[5] = iRgbLookupTable[((y+rc) >> 16) + ESaturateOffset];
       
   343 
       
   344                 // Lower left:
       
   345                 y = 76309 * (((TInt) yb2[0]) - 16) + 32768;
       
   346                 target2[0] = iRgbLookupTable[((y+bc) >> 16) + ESaturateOffset];
       
   347                 target2[1] = iRgbLookupTable[((y+gc) >> 16) + ESaturateOffset];
       
   348                 target2[2] = iRgbLookupTable[((y+rc) >> 16) + ESaturateOffset];
       
   349 
       
   350                 // Lower right:
       
   351                 y = 76309 * (((TInt) yb2[1]) - 16) + 32768;
       
   352                 target2[3] = iRgbLookupTable[((y+bc) >> 16) + ESaturateOffset];
       
   353                 target2[4] = iRgbLookupTable[((y+gc) >> 16) + ESaturateOffset];
       
   354                 target2[5] = iRgbLookupTable[((y+rc) >> 16) + ESaturateOffset];
       
   355 
       
   356                 // Next two pixels:
       
   357                 target += 6;
       
   358                 target2 += 6;
       
   359                 yb += 2;
       
   360                 yb2 += 2;
       
   361                 aUBuf++;
       
   362                 aVBuf++;
       
   363                 cols -= 2;
       
   364             }
       
   365 
       
   366             // Next rows
       
   367             rows -= 2;
       
   368             aYBuf += 2*aBufWidth;
       
   369             aUBuf += (aBufWidth - iWidth)/2;
       
   370             aVBuf += (aBufWidth - iWidth)/2;
       
   371             aTarget += 2*aTargetScanlineLength;
       
   372         }
       
   373     }
       
   374     else
       
   375     {
       
   376         // No chrominance given, do a luminance-only conversion
       
   377         
       
   378         // Convert all rows
       
   379         while ( rows )
       
   380         {
       
   381             // Convert all pixels in this row, two at a time
       
   382             cols = iWidth;
       
   383             target = aTarget;
       
   384         
       
   385             while ( cols )
       
   386             {
       
   387                 // Do a pixel:
       
   388                 y = 76309 * (((TInt) aYBuf[0]) - 16) + 32768;
       
   389                 val = iRgbLookupTable[(y >> 16) + ESaturateOffset];
       
   390                 target[0] = val;
       
   391                 target[1] = val;
       
   392                 target[2] = val;
       
   393 
       
   394                 // And another one:
       
   395                 y = 76309 * (((TInt) aYBuf[1]) - 16) + 32768;
       
   396                 val = iRgbLookupTable[(y >> 16) + ESaturateOffset];
       
   397                 target[3] = val;
       
   398                 target[4] = val;
       
   399                 target[5] = val;
       
   400 
       
   401                 // Next two pixels:
       
   402                 target += 6;
       
   403                 aYBuf += 2;
       
   404                 cols -= 2;
       
   405             }
       
   406 
       
   407             // Next row
       
   408             rows--;
       
   409             aYBuf += aBufWidth - iWidth;
       
   410             aTarget += aTargetScanlineLength;
       
   411         }
       
   412     }
       
   413 }
       
   414 
       
   415 
       
   416 
       
   417 
       
   418 
       
   419 //  End of File