Use this:
Code:
/*
* cloadimage.cpp
*
* Created on: 09.02.2009
* Author: TAMHAN
*/
#include "mobilepaint/cloadimage.h"
#include "LocaNotePaint.h"
CLoadImage* CLoadImage::NewL(MLoadImageObserver* iContainer)
{
CLoadImage* self = CLoadImage::NewLC(iContainer);
CleanupStack::Pop (self );
return self;
}
CLoadImage* CLoadImage::NewLC(MLoadImageObserver* iContainer)
{
CLoadImage* self = new ( ELeave ) CLoadImage();
CleanupStack::PushL (self );
self->iContainer=iContainer;
self->ConstructL();
self->iBackBufferRotator=NULL;
self->iImageDecoder=NULL;
return self;
}
CLoadImage::~CLoadImage()
{
Cancel();
if(iImageDecoder!=NULL)
{
delete iImageDecoder;
iImageDecoder=NULL;
}
iFs.Close();
if(iBackBufferRotator)
{
delete iBackBufferRotator;
iBackBufferRotator=NULL;
}
}
void CLoadImage::LoadAndRotateL(CFbsBitmap* aBitmap, TFileName& aFileName)
{
if (!IsActive())
{
if (iImageDecoder)
{
delete iImageDecoder;
iImageDecoder = NULL;
}
targetBitmap=aBitmap; //persist bitmap
localBitmap = new (ELeave) CFbsBitmap;
iImageDecoder = CImageDecoder::FileNewL(iFs, aFileName);
localBitmap->Create(iImageDecoder->FrameInfo().iOverallSizeInPixels,EColor64K);
iImageDecoder->Convert(&iStatus, *localBitmap);
SetActive();
}
}
void CLoadImage::RotationCompletedL(TInt /*aError*/)
{
TSize bmpSize = localBitmap->SizeInPixels();
int scanLineLength = CFbsBitmap::ScanLineLength( bmpSize.iWidth, EColor4K );
if (targetBitmap && targetBitmap->DataAddress() )
{
memcpy( (void*)targetBitmap->DataAddress(), (void*)localBitmap->DataAddress(), scanLineLength * bmpSize.iHeight );
}
delete localBitmap;
if(iBackBufferRotator)
{
delete iBackBufferRotator;
iBackBufferRotator=NULL;
}
localBitmap=NULL;
this->RunL();
}
void CLoadImage::ArmL(CFbsBitmap* aBitmap, TFileName& aFileName)
{
myBitmap=aBitmap;
myFileName=aFileName;
}
void CLoadImage::LoadL()
{
if(myBitmap!=NULL && myFileName.Length()>3)
LoadL(myBitmap,myFileName);
}
void CLoadImage::LoadL(CFbsBitmap* aBitmap, TFileName& aFileName)
{
if (!IsActive())
{
if (iImageDecoder)
{
delete iImageDecoder;
iImageDecoder = NULL;
}
localBitmap=NULL; //Just in case
iImageDecoder = CImageDecoder::FileNewL( iFs, aFileName);
iImageDecoder->Convert( &iStatus, *aBitmap);
SetActive();
}
}
void CLoadImage::DoCancel()
{
if(iBackBufferRotator!=NULL && iBackBufferRotator->IsActive())
{
iBackBufferRotator->Cancel();
}
if(iImageDecoder)
iImageDecoder->Cancel();
//cleanup moved to destructor
}
void CLoadImage::RunL()
{
//call iObserver - called when done
if(localBitmap==NULL)
{
iContainer->ImageLoaded();
}
else
{//rotate and call observer from there
if(localBitmap->SizeInPixels()==targetBitmap->SizeInPixels())
{
this->RotationCompletedL(KErrNone);
}
else
{
iBackBufferRotator=CBitmapRotatorAO::NewL(this);
if (localBitmap->SizeInPixels().iWidth < localBitmap->SizeInPixels().iHeight)
{
iBackBufferRotator->Rotate(CBitmapRotator::ERotation270DegreesClockwise,localBitmap);
}
else
{
iBackBufferRotator->Rotate(CBitmapRotator::ERotation90DegreesClockwise,localBitmap);
}
}
}
}
TInt CLoadImage::RunError(TInt /*aError*/)
{
//This call must ALWAYS be here as it is needed to "chain" the AO execution
//see more in the ListView ImageLoaded routine
iContainer->ImageLoaded();
return KErrNone;
}
void CLoadImage::ConstructL()
{
this->localBitmap=NULL;
CActiveScheduler::Add(this);
User::LeaveIfError( iFs.Connect() );
}
CLoadImage::CLoadImage(): CActive(EPriorityStandard)
{
}
void CLoadImage::GetEncoderImageTypesL(
RImageTypeDescriptionArray& aImageTypeArray )
{
CImageEncoder::GetImageTypesL( aImageTypeArray );
}
TBool CLoadImage::INRI()
{//when Jesus christ died, he said INRI. So does CLoadImage
if(iImageDecoder!=NULL)
return false;
else
return true;
}
header
Code:
// INCLUDES
#ifndef CLOADIMAGE_H
#define CLOADIMAGE_H
#include <aknappui.h>
#include <aknutils.h>
#include <ImageConversion.h>
#include "mobilepaint/cbitmaprotatorao.h"
#include "prefs/LocaNoteGeneric.h"
class CLocaNotePaint;
class MLoadImageObserver
{
public:
virtual void ImageLoaded()=0;
};
class CLoadImage : public CActive, MBitmapRotatorObserver
{
public:
static CLoadImage* NewL(MLoadImageObserver* iContainer);
static CLoadImage* NewLC(MLoadImageObserver* iContainer);
virtual ~CLoadImage();
public:
void LoadL(CFbsBitmap* aBitmap, TFileName& aFileName);
void ArmL(CFbsBitmap* aBitmap, TFileName& aFileName);
void LoadL();
void DoCancel();
void RunL();
TInt RunError(TInt aError);
void GetEncoderImageTypesL(RImageTypeDescriptionArray& aImageTypeArray);
TBool INRI();
void LoadAndRotateL(CFbsBitmap* aBitmap, TFileName& aFileName);
private:
void ConstructL();
CLoadImage();
//Darn MBitmapRotatorObserver
private:
void RotationCompletedL(TInt aError);
public:
//Must be public as the bitmap must be scaled when done by the handler
CFbsBitmap* myBitmap;
private:
CImageDecoder* iImageDecoder;
CBitmapRotatorAO* iBackBufferRotator;
CFbsBitmap* targetBitmap;
CFbsBitmap* localBitmap;
RFs iFs;
MLoadImageObserver* iContainer;
TFileName myFileName;
};
#endif
I am very successful with that on PNG files.