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