activityfw/activitydatabase/hsactivitydbserver/src/activitygraphicfilescaling.cpp
changeset 80 397d00875918
parent 73 4bc7b118b3df
equal deleted inserted replaced
73:4bc7b118b3df 80:397d00875918
    19 
    19 
    20 
    20 
    21 // -----------------------------------------------------------------------------
    21 // -----------------------------------------------------------------------------
    22 // -----------------------------------------------------------------------------
    22 // -----------------------------------------------------------------------------
    23 //
    23 //
    24 CGraphicsSalingHandler::CGraphicsSalingHandler(MImageReadyCallBack &aNotify, 
    24 CGraphicsSalingHandler::CGraphicsSalingHandler(MImageReadyCallBack &aNotify,
    25                                                const TSize &aNewSize, 
    25                             const TSize &aNewSize,
    26                                                TKindOfScaling aKindOfScaling):
    26                             TKindOfScaling aKindOfScaling
       
    27                             /* = CGraphicsSalingHandler::EIgnoreAspectRatio*/) :
    27     CActive(EPriorityNormal),
    28     CActive(EPriorityNormal),
    28     mNotify(aNotify),
    29     mNotify(aNotify),
    29     mNewSize(aNewSize),
    30     mNewSize(aNewSize),
    30     mKindOfScaling(aKindOfScaling),
    31     mKindOfScaling(aKindOfScaling),
    31     mCurrentOperation(ENone)
    32     mCurrentOperation(ENone)
    46 
    47 
    47 // -----------------------------------------------------------------------------
    48 // -----------------------------------------------------------------------------
    48 // -----------------------------------------------------------------------------
    49 // -----------------------------------------------------------------------------
    49 //
    50 //
    50 CGraphicsSalingHandler* CGraphicsSalingHandler::NewL(MImageReadyCallBack &aNotify,
    51 CGraphicsSalingHandler* CGraphicsSalingHandler::NewL(MImageReadyCallBack &aNotify,
    51                                                      RFs &aFs,
    52                                 RFs &aFs,
    52                                                      const TDesC &aFileName,
    53                                 const TDesC &aFileName,
    53                                                      const TDesC8& aMimeType,
    54                                 const TDesC8& aMimeType,
    54                                                      const TSize &aNewSize,
    55                                 const TSize &aNewSize,
    55                                                      TKindOfScaling aKindOfScaling)
    56                                 TKindOfScaling aKindOfScaling
    56 {
    57                                 /* = CGraphicsSalingHandler::EIgnoreAspectRatio*/)
    57     CGraphicsSalingHandler *self = CGraphicsSalingHandler::NewLC(aNotify, 
    58 {
    58                                                                  aFs, 
    59     CGraphicsSalingHandler *self = CGraphicsSalingHandler::NewLC(aNotify,
    59                                                                  aFileName, 
    60                                                                  aFs,
    60                                                                  aMimeType, 
    61                                                                  aFileName,
    61                                                                  aNewSize, 
    62                                                                  aMimeType,
       
    63                                                                  aNewSize,
    62                                                                  aKindOfScaling);
    64                                                                  aKindOfScaling);
    63     CleanupStack::Pop();
    65     CleanupStack::Pop();
    64     return self;
    66     return self;
    65 }
    67 }
    66 
    68 
    67 // -----------------------------------------------------------------------------
    69 // -----------------------------------------------------------------------------
    68 // -----------------------------------------------------------------------------
    70 // -----------------------------------------------------------------------------
    69 //
    71 //
    70 CGraphicsSalingHandler* CGraphicsSalingHandler::NewLC(MImageReadyCallBack &aNotify,
    72 CGraphicsSalingHandler* CGraphicsSalingHandler::NewLC(MImageReadyCallBack &aNotify,
    71                                               RFs &aFs,
    73                                 RFs &aFs,
    72                                               const TDesC &aFileName,
    74                                 const TDesC &aFileName,
    73                                               const TDesC8& aMimeType,
    75                                 const TDesC8& aMimeType,
    74                                               const TSize &aNewSize,
    76                                 const TSize &aNewSize,
    75                                               TKindOfScaling aKindOfScaling)
    77                                 TKindOfScaling aKindOfScaling
    76 {
    78                                 /* = CGraphicsSalingHandler::EIgnoreAspectRatio*/)
    77     CGraphicsSalingHandler *self = new (ELeave) CGraphicsSalingHandler(aNotify, 
    79 {
    78                                                                        aNewSize, 
    80     CGraphicsSalingHandler *self = new (ELeave) CGraphicsSalingHandler(aNotify,
       
    81                                                                        aNewSize,
    79                                                                        aKindOfScaling);
    82                                                                        aKindOfScaling);
       
    83 
       
    84     
       
    85 
    80     CleanupStack::PushL(self);
    86     CleanupStack::PushL(self);
    81     self->ConstructL(aFs, aFileName, aMimeType);
    87     self->ConstructL(aFs, aFileName, aMimeType);
    82     return self;
    88     return self;
    83 }
    89 }
    84 
    90 
    85 // -----------------------------------------------------------------------------
    91 // -----------------------------------------------------------------------------
    86 // -----------------------------------------------------------------------------
    92 // -----------------------------------------------------------------------------
    87 //
    93 //
    88 void CGraphicsSalingHandler::ConstructL(RFs &aFs, const TDesC &aFileName, const TDesC8& aMimeType)
    94 void CGraphicsSalingHandler::ConstructL(RFs &aFs, const TDesC &aFileName, const TDesC8& aMimeType)
    89 {
    95 {
    90     CActiveScheduler::Add(this);
       
    91 
       
    92     if (aFileName.Length() == 0
    96     if (aFileName.Length() == 0
    93         || aFs.IsValidName(aFileName) == EFalse) {
    97         || aFs.IsValidName(aFileName) == EFalse) {
    94         User::Leave(KErrPathNotFound);
    98         User::Leave(KErrPathNotFound);
    95     }
    99     }
       
   100 
       
   101     if (aMimeType.Length() == 0) {
       
   102         User::Leave(KErrBadName);
       
   103     }
       
   104 
       
   105     if(0>=mNewSize.iWidth || 0>=mNewSize.iHeight) {
       
   106        User::Leave(KErrCorrupt);
       
   107     }
       
   108 
       
   109     CActiveScheduler::Add(this);
    96 
   110 
    97     mBitmapScaler = CBitmapScaler::NewL();
   111     mBitmapScaler = CBitmapScaler::NewL();
    98     mBitmapScaler->SetQualityAlgorithm(CBitmapScaler::EMaximumQuality);
   112     mBitmapScaler->SetQualityAlgorithm(CBitmapScaler::EMaximumQuality);
    99 
   113 
   100     // convert *.png to bitmap
   114     // convert *.png to bitmap
   101     mImageDecoder = CImageDecoder::FileNewL(aFs, aFileName, aMimeType);
   115     mImageDecoder = CImageDecoder::FileNewL(aFs, aFileName, aMimeType);
   102     mBitmapFromFile = new(ELeave)CFbsBitmap();
   116     mBitmapFromFile = new(ELeave)CFbsBitmap();
   103     const TFrameInfo frameInfo(mImageDecoder->FrameInfo(0));
   117     const TFrameInfo frameInfo(mImageDecoder->FrameInfo(0));
   104     User::LeaveIfError(mBitmapFromFile->Create(frameInfo.iOverallSizeInPixels, 
   118     User::LeaveIfError(mBitmapFromFile->Create(frameInfo.iOverallSizeInPixels, 
   105                                                frameInfo.iFrameDisplayMode));
   119                                                frameInfo.iFrameDisplayMode));
   106     
   120 
   107     mImageDecoder->Convert(&iStatus, *mBitmapFromFile, 0);
   121     mImageDecoder->Convert(&iStatus, *mBitmapFromFile, 0);
   108     mCurrentOperation = EConvertBitmapFromFile;
   122     mCurrentOperation = EConvertBitmapFromFile;
   109     SetActive();
   123     SetActive();
   110 }
   124 }
   111 
   125 
   147             SetActive();
   161             SetActive();
   148             break;
   162             break;
   149         }
   163         }
   150     case EScale: {
   164     case EScale: {
   151             mCurrentOperation = ENone;
   165             mCurrentOperation = ENone;
   152             
   166 
   153             delete mBitmapScaler;
   167             delete mBitmapScaler;
   154             mBitmapScaler = 0;
   168             mBitmapScaler = 0;
   155             
   169             
   156             delete mBitmapFromFile;
   170             delete mBitmapFromFile;
   157             mBitmapFromFile = 0;
   171             mBitmapFromFile = 0;
   158             
   172 
       
   173             if (mKindOfScaling == CGraphicsSalingHandler::EKeepAspectRatioByExpanding) {
       
   174                 User::LeaveIfError(mBitmapOutput->Resize(mNewSize));
       
   175             }
       
   176 
   159             mNotify.ImageReadyCallBack(iStatus.Int(), mBitmapOutput);
   177             mNotify.ImageReadyCallBack(iStatus.Int(), mBitmapOutput);
   160             break;
   178             break;
   161         }
   179         }
   162     }
   180     }
   163 }
   181 }
   168 TSize CGraphicsSalingHandler::Scaling()
   186 TSize CGraphicsSalingHandler::Scaling()
   169 {
   187 {
   170    TSize originalSize = mBitmapFromFile->SizeInPixels();
   188    TSize originalSize = mBitmapFromFile->SizeInPixels();
   171    float widthFactor = mNewSize.iWidth / (float)originalSize.iWidth;
   189    float widthFactor = mNewSize.iWidth / (float)originalSize.iWidth;
   172    float heightFactor = mNewSize.iHeight / (float)originalSize.iHeight;
   190    float heightFactor = mNewSize.iHeight / (float)originalSize.iHeight;
       
   191 
   173    TSize retSize(mNewSize);
   192    TSize retSize(mNewSize);
       
   193 
   174    if (mKindOfScaling == CGraphicsSalingHandler::EKeepAspectRatio) {
   194    if (mKindOfScaling == CGraphicsSalingHandler::EKeepAspectRatio) {
   175        retSize = (widthFactor < heightFactor) ?
   195        retSize = (widthFactor < heightFactor) ?
   176                  TSize(mNewSize.iWidth, widthFactor * originalSize.iHeight) :
   196                  TSize(mNewSize.iWidth, widthFactor * originalSize.iHeight) :
   177                  TSize(heightFactor * originalSize.iWidth, mNewSize.iHeight);
   197                  TSize(heightFactor * originalSize.iWidth, mNewSize.iHeight);
   178    } else if (mKindOfScaling == CGraphicsSalingHandler::EKeepAspectRatioByExpanding) {
   198    } else if (mKindOfScaling == CGraphicsSalingHandler::EKeepAspectRatioByExpanding) {