imageeditorengine/filters/FilterScale/Src/CFilterScale.cpp
changeset 1 edfc90759b9f
equal deleted inserted replaced
0:57d4cdd99204 1:edfc90759b9f
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "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 * Ixonos Plc
       
    14 *
       
    15 * Description:  
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "CFilterScale.h"
       
    21 #include <e32math.h>
       
    22 
       
    23 const TInt KScaleBits = 12;
       
    24 
       
    25 EXPORT_C TInt CFilterScale::Create()
       
    26 	{
       
    27 	CFilterScale* ptr = NULL;
       
    28 	TRAPD( error, ptr = NewL(); );
       
    29 	if( error != KErrNone )
       
    30 		{
       
    31 		ptr = NULL;
       
    32 		}
       
    33 	return (TInt)((MImageFilter*)ptr);
       
    34 	}
       
    35 
       
    36 
       
    37 
       
    38 CFilterScale* CFilterScale::NewL()
       
    39 	{
       
    40 	CFilterScale* self = new( ELeave )CFilterScale();
       
    41 	CleanupStack::PushL( self );
       
    42 	self->ConstructL();
       
    43 	CleanupStack::Pop( self );
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 
       
    48 
       
    49 CFilterScale::~CFilterScale()
       
    50 	{
       
    51 	}
       
    52 
       
    53 
       
    54 
       
    55 CFilterScale::CFilterScale()
       
    56 	{
       
    57 	iScale = (1 << KScaleBits);
       
    58     iRelScale = 1.0;
       
    59 	}
       
    60 
       
    61 
       
    62 
       
    63 void CFilterScale::ConstructL()
       
    64 	{
       
    65 
       
    66 	}
       
    67 
       
    68 
       
    69 
       
    70 TRect CFilterScale::Rect()
       
    71 	{
       
    72 	if ( iRect == TRect(0,0,0,0) )
       
    73 	{
       
    74 		return iChild->Rect();
       
    75 	}
       
    76 	else
       
    77 	{
       
    78 		return iRect;
       
    79 	}
       
    80 	}
       
    81 
       
    82 TReal CFilterScale::Scale()
       
    83 	{
       
    84         return iRelScale;
       
    85 	}
       
    86 
       
    87 
       
    88 TSize CFilterScale::ViewPortSize()
       
    89 {
       
    90     return iChild->ViewPortSize();
       
    91 }
       
    92 
       
    93 TBlock * CFilterScale::GetBlockL ( const TRect & aRect )
       
    94 {
       
    95     //  Get the block to be scaled 
       
    96     TRect rect;
       
    97     rect.iTl.iX = iOrigo.iX + (((aRect.iTl.iX * iScale) >> KScaleBits));
       
    98     rect.iTl.iY = iOrigo.iY + (((aRect.iTl.iY * iScale) >> KScaleBits));
       
    99     rect.iBr.iX = iOrigo.iX + (((aRect.iBr.iX * iScale) >> KScaleBits)) + 1;
       
   100     rect.iBr.iY = iOrigo.iY + (((aRect.iBr.iY * iScale) >> KScaleBits)) + 1;
       
   101     
       
   102     TBlock * pSource = iChild->GetBlockL(rect);
       
   103     if (!pSource) return NULL;
       
   104 
       
   105     //  Create the scaled block
       
   106     TBlock * pDest = new (ELeave) TBlock (aRect);
       
   107     TUint32 * pD = pDest->iData;
       
   108 
       
   109     rect = iChild->Rect();
       
   110 
       
   111     for (TInt i = aRect.iTl.iY; i < aRect.iBr.iY; ++i)
       
   112     {
       
   113 
       
   114 	    TInt y = iOrigo.iY + ((i * iScale) >> KScaleBits);
       
   115         if ((y < rect.iTl.iY) || (y >= rect.iBr.iY))
       
   116         {
       
   117             pD += pDest->iWidth;
       
   118             continue;
       
   119         }
       
   120 
       
   121         for (TInt j = aRect.iTl.iX; j < aRect.iBr.iX; ++j, ++pD)
       
   122         {
       
   123 
       
   124 	        TInt x = iOrigo.iX + ((j * iScale) >> KScaleBits);
       
   125             if ((x < rect.iTl.iX) || (x >= rect.iBr.iX))
       
   126             {
       
   127                 continue;
       
   128             }
       
   129 	        
       
   130             if ( (x >= pSource->iRect.iTl.iX) && (x < pSource->iRect.iBr.iX) && 
       
   131                  (y >= pSource->iRect.iTl.iY) && (y < pSource->iRect.iBr.iY) )
       
   132 	        {
       
   133                 *pD = *(pSource->iData + (y - pSource->iRect.iTl.iY) * pSource->iWidth + (x - pSource->iRect.iTl.iX));
       
   134 	        }
       
   135         }
       
   136     }
       
   137     
       
   138     delete pSource;
       
   139     pSource = NULL;
       
   140 
       
   141     //  Return scaled block
       
   142     return pDest;
       
   143 }
       
   144 
       
   145 
       
   146 
       
   147 void CFilterScale::SetParent( MImageFilter* aParent )
       
   148 	{
       
   149 	iParent = aParent;
       
   150 	}
       
   151 
       
   152 
       
   153 
       
   154 void CFilterScale::SetChild( MImageFilter* aChild )
       
   155 	{
       
   156 	iChild = aChild;
       
   157 	}
       
   158 
       
   159 
       
   160 
       
   161 TInt CFilterScale::CmdL( const TDesC16& aCmd )
       
   162 	{
       
   163 
       
   164 	TLex lex( aCmd );
       
   165 
       
   166 	while( ! lex.Eos() )
       
   167 		{
       
   168 		TPtrC token = lex.NextToken();
       
   169 		if( token.Compare( _L("zoom") ) == 0 )
       
   170 			{
       
   171 			lex.Inc();
       
   172 			lex.Val( iZoom );
       
   173 			}
       
   174 		else if( token.Compare( _L("x") ) == 0 )
       
   175 			{
       
   176 			lex.Inc();
       
   177 			lex.Val( iPosition.iX );			
       
   178 			}
       
   179 		else if( token.Compare( _L("y") ) == 0 )
       
   180 			{
       
   181 			lex.Inc();
       
   182 			lex.Val( iPosition.iY );			
       
   183 			}
       
   184 		}
       
   185 
       
   186 	//	Get source and target sizes
       
   187     TRect rect = iChild->Rect();
       
   188 	TSize srcsize;
       
   189     srcsize.iWidth = rect.iBr.iX - rect.iTl.iX;
       
   190 	srcsize.iHeight = rect.iBr.iY - rect.iTl.iY;
       
   191 
       
   192     rect = iParent->Rect();
       
   193 	TSize tgtsize;
       
   194     tgtsize.iWidth = rect.iBr.iX - rect.iTl.iX;
       
   195 	tgtsize.iHeight = rect.iBr.iY - rect.iTl.iY;
       
   196 
       
   197 	//	Compute aspect ratio of the source
       
   198 	TInt ars = (srcsize.iWidth << KScaleBits) / srcsize.iHeight;
       
   199 
       
   200 	//	Compute aspect ratio of the target
       
   201 	TInt art = (tgtsize.iWidth << KScaleBits) / tgtsize.iHeight;
       
   202 
       
   203 	//	Select scale so that aspect ratio is preserved
       
   204 	if ( ars >= art )
       
   205 	{
       
   206 		iScale = (srcsize.iWidth << KScaleBits) / tgtsize.iWidth;
       
   207 	    iRelScale = (TReal)tgtsize.iWidth / (TReal)srcsize.iWidth;
       
   208         iOrigo.iX = iChild->Rect().iTl.iX;
       
   209 		TInt h = (srcsize.iWidth << KScaleBits) / art;
       
   210 		iOrigo.iY = iChild->Rect().iTl.iY + (srcsize.iHeight - h) / 2;
       
   211 	}
       
   212 	else
       
   213 	{
       
   214 		iScale = (srcsize.iHeight << KScaleBits) / tgtsize.iHeight;
       
   215 	    iRelScale = (TReal)tgtsize.iHeight / (TReal)srcsize.iHeight;
       
   216 		iOrigo.iY = iChild->Rect().iTl.iY;
       
   217 		TInt w = (srcsize.iHeight * art) >> KScaleBits;
       
   218 		iOrigo.iX = iChild->Rect().iTl.iX + (srcsize.iWidth - w) / 2 ;
       
   219 	}
       
   220 
       
   221     rect = iChild->Rect();
       
   222     iRect.iTl.iX = ((rect.iTl.iX * iScale) >> KScaleBits);
       
   223     iRect.iTl.iY = ((rect.iTl.iY * iScale) >> KScaleBits);
       
   224     iRect.iBr.iX = ((rect.iBr.iX * iScale) >> KScaleBits);
       
   225     iRect.iBr.iY = ((rect.iBr.iY * iScale) >> KScaleBits);
       
   226 	return 0;
       
   227 	}
       
   228 
       
   229 
       
   230 
       
   231 const char* CFilterScale::Type()
       
   232 	{
       
   233 	return "scale";
       
   234 	}
       
   235 	
       
   236 	
       
   237 #if !defined(EKA2)
       
   238 GLDEF_C TInt E32Dll( TDllReason )
       
   239     {
       
   240     return KErrNone;
       
   241     }	
       
   242 #endif