upnpmediaserver/contentdirectoryservice/src/upnpthumbnailcreator.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /** @file
       
     2 * Copyright (c) 2005-2006 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:  Thumbnail Creator.
       
    15 *
       
    16 */
       
    17  
       
    18 
       
    19 #include <e32svr.h>
       
    20 #include <imageconversion.h>
       
    21 #include <bitmaptransforms.h>
       
    22 #include <eikfutil.h>
       
    23 #include <gulicon.h> 
       
    24 
       
    25 #include "upnpthumbnailcreator.h"
       
    26 #include "upnpthumbnailcreator.h"
       
    27 #include "upnpcontentdirectoryglobals.h"
       
    28 
       
    29 #ifdef _DEBUG
       
    30 #define KLogFile _L("UPnPMediaServer.txt")
       
    31 #endif
       
    32 #include "upnpcustomlog.h"
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CUpnpThumbnailCreator::NewLC
       
    36 // Two-phased constructor.
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CUpnpThumbnailCreator* CUpnpThumbnailCreator::NewLC(MUpnpThumbnailCreatorObserver* aObserver)
       
    40 {
       
    41     CUpnpThumbnailCreator* self = new (ELeave) CUpnpThumbnailCreator(aObserver);
       
    42     CleanupStack::PushL(self);
       
    43     self->ConstructL();
       
    44     return self;
       
    45 }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CUpnpThumbnailCreator::ConstructL
       
    49 // Two-phased constructor.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 void CUpnpThumbnailCreator::ConstructL()
       
    53 {
       
    54     User::LeaveIfError(iFs.Connect());
       
    55     iScaler = CBitmapScaler::NewL();
       
    56     iSize.SetSize( KThumbnailWidth, KThumbnailHeight );
       
    57     iPhase = ESleep;
       
    58     CActiveScheduler::Add(this);
       
    59 }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CUpnpThumbnailCreator::CUpnpThumbnailCreator
       
    63 // Constructor
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CUpnpThumbnailCreator::CUpnpThumbnailCreator( MUpnpThumbnailCreatorObserver* aObserver )
       
    67     :CActive(EPriorityStandard), iOldSize(0,0)
       
    68 {
       
    69     iObserver = aObserver;
       
    70 }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CUpnpThumbnailCreator::~CUpnpThumbnailCreator
       
    74 // Destructor.
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 CUpnpThumbnailCreator::~CUpnpThumbnailCreator()
       
    78 {
       
    79     Cancel();
       
    80     delete iScaler;
       
    81     delete iEncoder;
       
    82     delete iDecoder;
       
    83     delete iBitmap;
       
    84     iFs.Close();
       
    85 }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CUpnpThumbnailCreator::RunError
       
    89 // (other items were commented in a header).
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 TInt CUpnpThumbnailCreator::RunError(TInt aErr)
       
    93 {
       
    94 	LOGS1("CUpnpThumbnailCreator::RunError(%d)", aErr);
       
    95     return KErrNone;
       
    96 }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CUpnpThumbnailCreator::RunL
       
   100 // (other items were commented in a header).
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CUpnpThumbnailCreator::RunL()
       
   104 {
       
   105     if( iStatus == KErrNone )
       
   106     {
       
   107         if( EDecodeObject == iPhase )
       
   108         {
       
   109             iScaler->Scale(&iStatus, *iBitmap, iSize, ETrue);
       
   110             iPhase = EDecode;
       
   111             SetActive();
       
   112         }
       
   113         else if( EDecode == iPhase )
       
   114         {
       
   115             EncodeAndConvertL();
       
   116             iPhase = EEncode;
       
   117             SetActive();
       
   118         }
       
   119         else if( EEncode == iPhase )
       
   120         {
       
   121             iPhase = ESleep;
       
   122             iObserver->NotifyThumbnailResultL(this, iStatus.Int());
       
   123         }
       
   124     }
       
   125     else
       
   126     {
       
   127         iObserver->NotifyThumbnailResultL(this, iStatus.Int());
       
   128     }
       
   129 }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CUpnpThumbnailCreator::DoCancel
       
   133 // (other items were commented in a header).
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 void CUpnpThumbnailCreator::DoCancel()
       
   137 {
       
   138     if( EDecode == iPhase )
       
   139     {
       
   140         iDecoder->Cancel();
       
   141     }
       
   142     if( EEncode == iPhase )
       
   143     {
       
   144         iEncoder->Cancel();
       
   145     }
       
   146     if( EDecodeObject == iPhase )
       
   147     {
       
   148         iDecoder->Cancel();
       
   149     }
       
   150 }
       
   151 
       
   152 
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CUpnpThumbnailCreator::RunError
       
   156 // Requester function
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 void CUpnpThumbnailCreator::EncodeAndConvertL()
       
   160 {
       
   161     if( IsActive() )
       
   162     {
       
   163         Cancel();
       
   164     }
       
   165 
       
   166     delete iEncoder;
       
   167     iEncoder = 0;
       
   168 
       
   169     iPath.Append(iFileName);
       
   170 
       
   171 
       
   172     iEncoder = CImageEncoder::FileNewL( iFs,
       
   173                                         iPath,
       
   174                                         KThumbMimeType,
       
   175                                         CImageEncoder::EOptionAlwaysThread);
       
   176 
       
   177     iEncoder->Convert( &iStatus, *iBitmap, 0 );
       
   178 }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CUpnpThumbnailCreator::CreateL
       
   182 // Requester function
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void CUpnpThumbnailCreator::CreateL(const TDesC& aSourceFile, const TDesC& aThumbName)
       
   186 {
       
   187 
       
   188 
       
   189     if( IsActive() )
       
   190     {
       
   191         Cancel();
       
   192     }
       
   193 
       
   194     iParse.Set( aSourceFile, NULL, NULL);
       
   195     TParse fp;
       
   196     User::LeaveIfError( fp.Set(aThumbName, 0 , 0) );
       
   197     iFileName = fp.NameAndExt();
       
   198     iExtension = fp.Ext();
       
   199     iPath = fp.DriveAndPath();
       
   200 
       
   201     TUint temp;
       
   202     if( iFs.Att( iPath, temp ) != KErrNone )
       
   203     {
       
   204         User::LeaveIfError( iFs.MkDirAll( iPath ) );
       
   205     }
       
   206 
       
   207     delete iDecoder;
       
   208     iDecoder = NULL;
       
   209     
       
   210     delete iBitmap;
       
   211     iBitmap = NULL;
       
   212     iBitmap = new (ELeave) CFbsBitmap();
       
   213 
       
   214     TSize size = GetSize(aSourceFile);
       
   215     size.SetSize( size.iWidth, size.iHeight );
       
   216     iOldSize = size; 
       
   217     CalculateSize( size );
       
   218     User::LeaveIfError(iBitmap->Create( size, iInfo.iFrameDisplayMode ));
       
   219 
       
   220     //Decode to the bitmap
       
   221     //RunL will be called when decoding is complete
       
   222     iPhase = EDecodeObject;
       
   223     iDecoder->Convert( &iStatus, *iBitmap );
       
   224     SetActive();
       
   225 }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CUpnpThumbnailCreator::CalculateSize
       
   229 // Requester function
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void CUpnpThumbnailCreator::CalculateSize( TSize& aLoadSize )
       
   233 {
       
   234     if( ( aLoadSize.iWidth <= KThumbnailWidth ) ||
       
   235         ( aLoadSize.iHeight <= KThumbnailHeight ) )
       
   236     {
       
   237         return;
       
   238     }
       
   239 
       
   240     TUint i( 8 ); // for 8 bits
       
   241 
       
   242     for( ; i > 0; i >>= 1 )
       
   243     {
       
   244         if( DivisionCeil( aLoadSize.iWidth,  i ) > KThumbnailWidth &&
       
   245         DivisionCeil( aLoadSize.iHeight, i ) > KThumbnailHeight )
       
   246         {
       
   247             break;
       
   248         }
       
   249     }
       
   250 
       
   251     aLoadSize.SetSize( DivisionCeil( aLoadSize.iWidth, i ),
       
   252                        DivisionCeil( aLoadSize.iHeight, i ) );
       
   253 }
       
   254 
       
   255 // -----------------------------------------------------------------------------
       
   256 // CUpnpThumbnailCreator::DivisionCeil
       
   257 // (other items were commented in a header).
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 TInt CUpnpThumbnailCreator::DivisionCeil( const TInt aVal, const TInt aDiv )
       
   261 {
       
   262     if( ( aVal % aDiv ) > 0 )
       
   263     {
       
   264         return (TInt)( ( aVal / aDiv ) + 1 );
       
   265     }
       
   266     else
       
   267     {
       
   268         return (TInt)( aVal / aDiv );
       
   269     }
       
   270 }
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CUpnpThumbnailCreator::GetFileName
       
   274 // (other items were commented in a header).
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 
       
   278 TFileName CUpnpThumbnailCreator::GetFileName()
       
   279 {
       
   280     return iFileName;
       
   281 }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // CUpnpThumbnailCreator::GetFileExt
       
   285 // (other items were commented in a header).
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 
       
   289 TPtrC CUpnpThumbnailCreator::GetFileExt()
       
   290 {
       
   291     return iExtension;
       
   292 }
       
   293 // CUpnpThumbnailCreator::GetPath
       
   294 // (other items were commented in a header).
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 TFileName CUpnpThumbnailCreator::GetPath()
       
   298 {
       
   299     return iPath;
       
   300 }
       
   301 
       
   302 // -----------------------------------------------------------------------------
       
   303 // CUpnpThumbnailCreator::GetSize
       
   304 // (other items were commented in a header).
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 TSize CUpnpThumbnailCreator::GetSize(const TDesC& aSourceFile)
       
   308 {
       
   309     TRAPD(sizeErr, iDecoder = CImageDecoder::FileNewL( iFs, aSourceFile ));
       
   310     if (sizeErr)
       
   311     {
       
   312         return TSize();
       
   313     }
       
   314     else
       
   315     {
       
   316         iInfo = iDecoder->FrameInfo();
       
   317         TSize size = iInfo.iOverallSizeInPixels;
       
   318         return size;    
       
   319     }
       
   320 }
       
   321