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