btobexprofiles/obexsendservices/obexservicesendutils/src/BTSUImageConverter.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     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:  Tools for image conversion.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "BTServiceUtils.h"
       
    21 #include "BTSUImageConverter.h"
       
    22 #include "BTSUDebug.h"
       
    23 
       
    24 #include <imageconversion.h>
       
    25 #include <bitmaptransforms.h>
       
    26 
       
    27 //CONSTANTS
       
    28 _LIT8( KBTSUImageTypeJpeg, "image/jpeg" );
       
    29 const TInt KBTSUJpegQualityFactor = 90;
       
    30 const TInt KBTSUImageThumbWidth   = 160;  
       
    31 const TInt KBTSUImageThumbHeight  = 120;
       
    32 
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CBTSUImageConverter::CBTSUImageConverter
       
    38 // Constructor.
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CBTSUImageConverter::CBTSUImageConverter(): CActive( EPriorityStandard )
       
    42     {
       
    43     CActiveScheduler::Add( this );
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // Destructor
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CBTSUImageConverter::~CBTSUImageConverter()
       
    51     {
       
    52     FLOG(_L("[BTSU]\t CBTSUImageConverter::~CBTSUImageConverter()"));
       
    53 
       
    54     Cancel();
       
    55     Reset();
       
    56     iFileSession.Close();
       
    57 
       
    58     FLOG(_L("[BTSU]\t CBTSUImageConverter::~CBTSUImageConverter() completed"));
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CBTSUImageConverter::NewL
       
    63 // Two-phased constructor.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CBTSUImageConverter* CBTSUImageConverter::NewL()
       
    67     {
       
    68     CBTSUImageConverter* self = new (ELeave) CBTSUImageConverter;
       
    69     CleanupStack::PushL( self );
       
    70     self->ConstructL();
       
    71     CleanupStack::Pop();
       
    72     return self;
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CBTSUImageConverter::ConstructL
       
    77 // Symbian 2nd phase constructor.
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void CBTSUImageConverter::ConstructL()
       
    81     {
       
    82     FLOG(_L("[BTSU]\t CBTSUImageConverter::ConstructL()"));
       
    83  
       
    84     User::LeaveIfError( iFileSession.Connect() );
       
    85     iFileSession.ShareProtected();
       
    86 
       
    87     FLOG(_L("[BTSU]\t CBTSUImageConverter::ConstructL() completed"));
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CBTSUImageConverter::GetImageSizeL
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 TSize CBTSUImageConverter::GetImageSizeL( RFile& aImageFile )
       
    95     {
       
    96     FLOG(_L("[BTSU]\t CBTSUImageConverter::GetImageSizeL()"));
       
    97    
       
    98     CImageDecoder* decoder = CImageDecoder::FileNewL(aImageFile,ContentAccess::EPeek );
       
    99     TSize size = decoder->FrameInfo().iOverallSizeInPixels;
       
   100     delete decoder;
       
   101 
       
   102     FTRACE(FPrint(_L("[BTSU]\t CBTSUImageConverter::GetImageSizeL() completed w=%d h=%d"), size.iWidth, size.iHeight ));
       
   103 
       
   104     return size;
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CBTSUImageConverter::CreateThumbnailL
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 void CBTSUImageConverter::CreateThumbnailL( RFile& aSourceFile, 
       
   112                                             const TDesC& aDestFile )
       
   113     {
       
   114     FLOG(_L("[BTSU]\t CBTSUImageConverter::CreateThumbnailL()"));
       
   115     
       
   116 
       
   117     Reset();
       
   118     DecodeImageL( aSourceFile );
       
   119     ScaleImageL();
       
   120     EncodeImageL( aDestFile, ETrue );
       
   121     Reset();
       
   122 
       
   123     FLOG(_L("[BTSU]\t CBTSUImageConverter::CreateThumbnailL() completed"));
       
   124     }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // CBTSUImageConverter::GetDisplayNameL
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 HBufC* CBTSUImageConverter::GetDisplayNameL( const TDesC8& aMimeType )
       
   131     {
       
   132     FLOG(_L("[BTSU]\t CBTSUImageConverter::GetDisplayNameL()"));
       
   133 
       
   134     if ( &aMimeType == NULL || 
       
   135          aMimeType.Length() == 0 || 
       
   136          aMimeType.Length() > KMaxFileName )
       
   137         {
       
   138         User::Leave( KErrArgument );
       
   139         }
       
   140 
       
   141     HBufC* name = NULL;
       
   142 
       
   143     // Get file types from decoder
       
   144     //
       
   145     RFileExtensionMIMETypeArray fileTypes;
       
   146     CleanupResetAndDestroyPushL( fileTypes );
       
   147     CImageDecoder::GetFileTypesL( fileTypes );
       
   148     
       
   149     // Find the display name for the mime type
       
   150     //
       
   151     for( TInt index = 0; index < fileTypes.Count(); index++ )
       
   152         {
       
   153         if ( fileTypes[index]->MIMEType() == aMimeType )
       
   154             {
       
   155             name = fileTypes[index]->DisplayName().AllocL();
       
   156             break;
       
   157             }
       
   158         }
       
   159 
       
   160     CleanupStack::PopAndDestroy(); // fileTypes 
       
   161 
       
   162     if ( name == NULL )
       
   163         {
       
   164         // No display name found, so the image type is not supported.
       
   165         //
       
   166         User::Leave( KErrNotSupported );
       
   167         }
       
   168 
       
   169     FLOG(_L("[BTSU]\t CBTSUImageConverter::GetDisplayNameL() completed"));
       
   170 
       
   171     return name;
       
   172     }
       
   173 
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CBTSUImageConverter::DoCancel
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void CBTSUImageConverter::DoCancel()
       
   180     {
       
   181     FLOG(_L("[BTSU]\t CBTSUImageConverter::DoCancel()"));
       
   182 
       
   183     if ( iDecoder )
       
   184         {
       
   185         iDecoder->Cancel();
       
   186         }
       
   187     if ( iScaler )
       
   188         {
       
   189         iScaler->Cancel();
       
   190         }
       
   191     if ( iEncoder )
       
   192         {
       
   193         iEncoder->Cancel();
       
   194         }
       
   195 
       
   196     FLOG(_L("[BTSU]\t CBTSUImageConverter::DoCancel() completed"));
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // CBTSUImageConverter::RunL
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 void CBTSUImageConverter::RunL()
       
   204     {
       
   205     FTRACE(FPrint(_L("[BTSU]\t CBTSUImageConverter::RunL() %d"), iStatus.Int() ));
       
   206 
       
   207     iWait.AsyncStop();
       
   208 
       
   209     FLOG(_L("[BTSU]\t CBTSUImageConverter::RunL() completed"));
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CBTSUImageConverter::DecodeImageL
       
   214 // -----------------------------------------------------------------------------
       
   215 //
       
   216 void CBTSUImageConverter::DecodeImageL( RFile& aSourceFile )
       
   217     {
       
   218     FLOG(_L("[BTSU]\t CBTSUImageConverter::DecodeImageL( )"));
       
   219 
       
   220     __ASSERT_DEBUG( iDecoder == NULL, BTSUPanic( EBTSUPanicExistingObject ) );
       
   221     __ASSERT_DEBUG( iFrameBitmap == NULL, BTSUPanic( EBTSUPanicExistingObject ) );
       
   222 
       
   223     // Create decoder
       
   224     //    
       
   225     iDecoder = CImageDecoder::FileNewL(aSourceFile, ContentAccess::EPeek );
       
   226 
       
   227     // Create a bitmap
       
   228     //
       
   229     iFrameBitmap = new ( ELeave ) CFbsBitmap;
       
   230     User::LeaveIfError( iFrameBitmap->Create( 
       
   231         iDecoder->FrameInfo().iOverallSizeInPixels,
       
   232         iDecoder->FrameInfo().iFrameDisplayMode ) );
       
   233     
       
   234     // Start decoding
       
   235     //
       
   236     iDecoder->Convert( &iStatus, *iFrameBitmap );
       
   237     SetActive();
       
   238 
       
   239     iWait.Start(); // Wait here until decoding is completed
       
   240     User::LeaveIfError( iStatus.Int() );
       
   241 
       
   242     FLOG(_L("[BTSU]\t CBTSUImageConverter::DecodeImageL() completed"));
       
   243     }
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // CBTSUImageConverter::EncodeImageL
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 void CBTSUImageConverter::EncodeImageL( const TDesC& aDestFile, 
       
   250                                         const TBool& aThumbnail )
       
   251     {
       
   252     FLOG(_L("[BTSU]\t CBTSUImageConverter::EncodeImageL()"));
       
   253 
       
   254     __ASSERT_DEBUG( iDecoder != NULL, BTSUPanic( EBTSUPanicNullPointer ) );
       
   255     __ASSERT_DEBUG( iEncoder == NULL, BTSUPanic( EBTSUPanicExistingObject ) );
       
   256     __ASSERT_DEBUG( iFrameImageData == NULL, BTSUPanic( EBTSUPanicExistingObject ) );
       
   257 
       
   258     // Create encoder
       
   259     //
       
   260     iEncoder = CImageEncoder::FileNewL( iFileSession, aDestFile, KBTSUImageTypeJpeg() );
       
   261 
       
   262     // Create frame image data
       
   263     //
       
   264     iFrameImageData = CFrameImageData::NewL();
       
   265     TJpegImageData* jpegFormat = new ( ELeave ) TJpegImageData;
       
   266     CleanupStack::PushL( jpegFormat );
       
   267 
       
   268     jpegFormat->iSampleScheme = TJpegImageData::EColor422;
       
   269     jpegFormat->iQualityFactor = KBTSUJpegQualityFactor;
       
   270     User::LeaveIfError( iFrameImageData->AppendImageData( jpegFormat ) );
       
   271     CleanupStack::Pop( jpegFormat );
       
   272 
       
   273     // Start encoding
       
   274     //
       
   275     if ( aThumbnail )
       
   276         {
       
   277         __ASSERT_DEBUG( iScaledBitmap != NULL, BTSUPanic( EBTSUPanicNullPointer ) );
       
   278         iEncoder->Convert( &iStatus, *iScaledBitmap, iFrameImageData );
       
   279         }
       
   280     else
       
   281         {
       
   282         __ASSERT_DEBUG( iFrameBitmap != NULL, BTSUPanic( EBTSUPanicNullPointer ) );
       
   283         iEncoder->Convert( &iStatus, *iFrameBitmap, iFrameImageData );
       
   284         }
       
   285     
       
   286     SetActive();
       
   287     iWait.Start(); // Wait here until encoding is completed
       
   288     User::LeaveIfError( iStatus.Int() );
       
   289 
       
   290     FLOG(_L("[BTSU]\t CBTSUImageConverter::EncodeImageL() completed"));
       
   291     }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CBTSUImageConverter::ScaleImageL
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 void CBTSUImageConverter::ScaleImageL()
       
   298     {
       
   299     FLOG(_L("[BTSU]\t CBTSUImageConverter::ScaleImageL( )"));
       
   300 
       
   301     __ASSERT_DEBUG( iDecoder != NULL, BTSUPanic( EBTSUPanicNullPointer ) );
       
   302     __ASSERT_DEBUG( iFrameBitmap != NULL, BTSUPanic( EBTSUPanicNullPointer ) );
       
   303     __ASSERT_DEBUG( iScaler == NULL, BTSUPanic( EBTSUPanicExistingObject ) );
       
   304     __ASSERT_DEBUG( iScaledBitmap == NULL, BTSUPanic( EBTSUPanicExistingObject ) );
       
   305 
       
   306     // Create scaler
       
   307     //
       
   308     iScaler = CBitmapScaler::NewL();
       
   309 
       
   310     // Create a bitmap
       
   311     //
       
   312     iScaledBitmap = new ( ELeave ) CFbsBitmap;
       
   313     TSize size;
       
   314     size.iWidth = KBTSUImageThumbWidth;
       
   315     size.iHeight = KBTSUImageThumbHeight;
       
   316     User::LeaveIfError( iScaledBitmap->Create( size,
       
   317         iDecoder->FrameInfo().iFrameDisplayMode ) );
       
   318     
       
   319     // Start scaling
       
   320     //
       
   321     iScaler->Scale( &iStatus, *iFrameBitmap, *iScaledBitmap, EFalse );
       
   322     SetActive();
       
   323 
       
   324     iWait.Start(); // Wait here until scaling is completed
       
   325     User::LeaveIfError( iStatus.Int() );
       
   326 
       
   327     FLOG(_L("[BTSU]\t CBTSUImageConverter::ScaleImageL() completed"));
       
   328     }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // CBTSUImageConverter::Reset
       
   332 // -----------------------------------------------------------------------------
       
   333 //
       
   334 void CBTSUImageConverter::Reset()
       
   335     {
       
   336     FLOG(_L("[BTSU]\t CBTSUImageConverter::Reset()"));
       
   337 
       
   338     delete iDecoder; iDecoder = NULL;
       
   339     delete iEncoder; iEncoder = NULL;
       
   340     delete iScaler; iScaler = NULL;
       
   341     
       
   342     delete iFrameImageData; iFrameImageData = NULL;
       
   343     delete iFrameBitmap; iFrameBitmap = NULL;
       
   344     delete iScaledBitmap; iScaledBitmap = NULL;
       
   345 
       
   346     FLOG(_L("[BTSU]\t CBTSUImageConverter::Reset() completed"));
       
   347     }
       
   348 
       
   349 //  End of File