satengine/SatServer/Engine/src/CSatSColorIconConverter.cpp
changeset 46 2fa1fa551b0b
parent 42 35488577e233
child 48 78df25012fda
equal deleted inserted replaced
42:35488577e233 46:2fa1fa551b0b
     1 /*
       
     2 * Copyright (c) 2002-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:  Converts color icon to CFbsBitmap
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include    <gdi.h>
       
    20 #include    <fbs.h>
       
    21 #include    "CSatSColorIconConverter.h"
       
    22 #include    "TSatBitOP.h"
       
    23 #include    "SatLog.h"
       
    24 
       
    25 const TInt KHeaderSize = 6;
       
    26 const TInt KClutEntrySize = 3;
       
    27 const TInt KRedShift = 16;
       
    28 const TInt KGreenShift = 8;
       
    29 const TInt KPixelSize = 8;
       
    30 
       
    31 // ======== MEMBER FUNCTIONS ========
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CSatSColorIconConverter::CSatSColorIconConverter
       
    35 // C++ default constructor can NOT contain any code, that
       
    36 // might leave.
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CSatSColorIconConverter::CSatSColorIconConverter()
       
    40     {
       
    41     LOG( SIMPLE, "SATENGINE: \
       
    42         CSatSColorIconConverter::CSatSColorIconConverter calling - exiting" )
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CSatSColorIconConverter::CheckDataValidityL
       
    47 // (other items were commented in a header).
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 void CSatSColorIconConverter::CheckDataValidityL(
       
    51     const TDesC8& aData ) const
       
    52     {
       
    53     LOG( SIMPLE,
       
    54         "SATENGINE: CSatSColorIconConverter::CheckDataValidityL calling" )
       
    55     TInt err( KErrNone );
       
    56     const TInt dataLength( aData.Length() );
       
    57 
       
    58     if ( dataLength >= KHeaderSize )
       
    59         {
       
    60         LOG( SIMPLE, "SATENGINE: CSatSColorIconConverter::CheckDataValidityL \
       
    61              dataLength bigger than KHeaderSize" )
       
    62         const TInt width( aData[0] );
       
    63         const TInt height( aData[1] );
       
    64         const TInt bpp( aData[2] );
       
    65 
       
    66         if ( dataLength < ( width * height * bpp / KPixelSize + KHeaderSize ) )
       
    67             {
       
    68             LOG( SIMPLE, "SATENGINE: CSatSColorIconConverter::\
       
    69             CheckDataValidityL KErrCorrupt" )
       
    70             err = KErrCorrupt;
       
    71             }
       
    72         }
       
    73 
       
    74     else
       
    75         {
       
    76         LOG( SIMPLE, "SATENGINE: CSatSColorIconConverter::CheckDataValidityL \
       
    77              dataLength less than KHeaderSize" )
       
    78         err = KErrCorrupt;
       
    79         }
       
    80 
       
    81     User::LeaveIfError( err );
       
    82     LOG( SIMPLE,
       
    83         "SATENGINE: CSatSColorIconConverter::CheckDataValidityL exiting" )
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CSatSColorIconConverter::CheckClutValidityL
       
    88 // (other items were commented in a header).
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CSatSColorIconConverter::CheckClutValidityL(
       
    92     const TDesC8& aData,
       
    93     const TDesC8& aClut ) const
       
    94     {
       
    95     LOG( SIMPLE,
       
    96         "SATENGINE: CSatSColorIconConverter::CheckClutValidityL calling" )
       
    97     const TInt clutEntries( aData[3] );
       
    98     const TInt expectedClutLength( clutEntries * KClutEntrySize );
       
    99 
       
   100     if ( aClut.Length() < expectedClutLength )
       
   101         {
       
   102         LOG( SIMPLE,
       
   103         "SATENGINE: CSatSColorIconConverter::CheckClutValidityL KErrCorrupt" )
       
   104         User::Leave( KErrCorrupt );
       
   105         }
       
   106 
       
   107     LOG( SIMPLE,
       
   108         "SATENGINE: CSatSColorIconConverter::CheckClutValidityL exiting" )
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CSatSColorIconConverter::ConvertL
       
   113 // Converts color icon to CFbsBitamp.
       
   114 //
       
   115 // aData has following format( ETSI TS 131 102 V4.10.0 ).
       
   116 //  1 byte:         Icon width
       
   117 //  2 byte:         Icon height
       
   118 //  3 byte:         Bits per raster image point. (1 to 8)
       
   119 //  7 to end byte:  Image body.
       
   120 //
       
   121 // The image body has following format.
       
   122 //
       
   123 //   Byte 1     Byte 2
       
   124 //  1001 1100 1110 1100 ....
       
   125 //
       
   126 // If the Bits per pixel ratio is 3 then
       
   127 // 1st raster point CLUT reference is 100
       
   128 // 2nd ------------ "" -------------- 111
       
   129 // 3rd ------------ "" -------------- 001
       
   130 // 4th ------------ "" -------------- 110
       
   131 // etc...
       
   132 //
       
   133 // One CLUT entry is 3 bytes and each byte defines the intensity of color (RGB).
       
   134 // The CLUT is in following format:
       
   135 // 1st CLUT entry
       
   136 //      1 byte Red color intensity
       
   137 //      2 byte Green color intensity
       
   138 //      3 byte Blue color intensity
       
   139 // 2nd CLUT entry
       
   140 //      4 byte Red color intensity
       
   141 //      5 byte Green color intensity
       
   142 //      6 byte Blue color intensity
       
   143 // (other items were commented in a header).
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 CFbsBitmap* CSatSColorIconConverter::ConvertL(
       
   147     const TDesC8& aData,
       
   148     const TDesC8& aClut ) const
       
   149     {
       
   150     LOG( SIMPLE, "SATENGINE: CSatSColorIconConverter::ConvertL calling" )
       
   151 
       
   152     // Check that data is valid.
       
   153     CheckDataValidityL( aData );
       
   154     CheckClutValidityL( aData, aClut );
       
   155 
       
   156     const TUint8 width( aData[0] );
       
   157     const TUint8 height( aData[1] );
       
   158 
       
   159     // Create the bitmap
       
   160     CFbsBitmap* bitmap = CreateBitmapL( width, height, EColor16M );
       
   161 
       
   162     // Separate the header from image body.
       
   163     const TPtrC8 iconData(
       
   164         aData.Mid( KHeaderSize, aData.Length() - KHeaderSize ) );
       
   165 
       
   166     const TUint8 bpp( aData[2] );
       
   167     TBitmapUtil bitmapUtil( bitmap );
       
   168     bitmapUtil.Begin( TPoint( 0, 0 ) );
       
   169     TInt x(0);
       
   170     TInt y(0);
       
   171     // Go through the image body to convert the data.
       
   172     for ( y = 0; y < height; y++ )
       
   173         {
       
   174         bitmapUtil.SetPos( TPoint( 0, y ) );
       
   175 
       
   176         for ( x = 0; x < width; x++ )
       
   177             {
       
   178             // Calculate the starting bit index of current pixel
       
   179             // location.
       
   180             const TInt startBit( ( y * width + x ) * bpp );
       
   181 
       
   182             // Get the entry from icon data.
       
   183             const TUint8 clutReference(
       
   184                 TSatBitOP::ExtractBits( startBit, bpp, iconData ) );
       
   185 
       
   186             // clut entry is 3 bytes long and therefore the start of
       
   187             // the clut entry is multiplied with 3.
       
   188             const TInt clutEntryPoint( clutReference * KClutEntrySize );
       
   189 
       
   190             // Take the color intensities from clut entry.
       
   191             const TUint red( aClut[clutEntryPoint] );
       
   192             const TUint green( aClut[clutEntryPoint + 1] );
       
   193             const TUint blue( aClut[clutEntryPoint + 2] );
       
   194 
       
   195             // Cannot use TRgb here, because of different interpration of
       
   196             // r,g,b order than bitmapUtil.
       
   197             bitmapUtil.SetPixel(
       
   198                 red << KRedShift | green << KGreenShift | blue );
       
   199 
       
   200             bitmapUtil.IncXPos();
       
   201             }
       
   202         }
       
   203     LOG3( SIMPLE,"SATENGINE: CSatSColorIconConverter::ConvertL, \
       
   204           x: %d, y: %d", x, y )
       
   205     bitmapUtil.End();
       
   206 
       
   207     LOG( SIMPLE, "SATENGINE: CSatSColorIconConverter::ConvertL exiting" )
       
   208     return bitmap;
       
   209     }