imageeditorengine/filters/FilterDraw/Src/cfilterdraw.cpp
changeset 1 edfc90759b9f
child 12 18b321db4884
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 * Draw filter for Draw UI plugin.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include <fbs.h>
       
    22 #include <bitdev.h>
       
    23 #include <e32math.h>
       
    24 #include "cfilterdraw.h"
       
    25 
       
    26 const TInt KDrawBitmapWidth(640);
       
    27 const TInt KDrawBitmapHeight(480);
       
    28 // ---------------------------------------------------------------------------
       
    29 // Create
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 EXPORT_C TInt CFilterDraw::Create()
       
    33 	{
       
    34 	CFilterDraw* ptr = NULL;
       
    35 	TRAP_IGNORE( ptr = NewL() );
       
    36 	return (TInt)((MImageFilter*)ptr);
       
    37 	}
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // NewL
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 CFilterDraw* CFilterDraw::NewL()
       
    44 	{
       
    45 	return new( ELeave )CFilterDraw();
       
    46 	}
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // ~CFilterDraw
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CFilterDraw::~CFilterDraw()
       
    53 	{
       
    54 	delete[] iData;
       
    55 	iReadyToRender = EFalse;
       
    56 	// Close all paths
       
    57 	for( TInt i(0); i<iPaths.Count(); i++ )
       
    58 		{
       
    59 		iPaths[i].Close();
       
    60 		}
       
    61 	iPaths.Close();	
       
    62 	}
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // CFilterDraw
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 CFilterDraw::CFilterDraw():iData(NULL), 
       
    69     iBitmapSize(KDrawBitmapWidth, KDrawBitmapHeight)
       
    70 	{
       
    71 	}
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // Rect
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 TRect CFilterDraw::Rect()
       
    78 	{
       
    79 	ASSERT(iChild);
       
    80 	return iChild->Rect();
       
    81 	}
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // Scale
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 TReal CFilterDraw::Scale()
       
    88 	{
       
    89 	ASSERT(iChild);
       
    90 	return iChild->Scale();
       
    91 	}
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // ViewPortSize
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 TSize CFilterDraw::ViewPortSize()
       
    98 	{
       
    99 	ASSERT(iChild);
       
   100     return iChild->ViewPortSize();
       
   101 	}
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // GetBlockL
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 TBlock* CFilterDraw::GetBlockL( const TRect& aRect )
       
   108 	{
       
   109 	ASSERT(iChild);	
       
   110     TBlock* pB = iChild->GetBlockL( aRect );
       
   111     if( !pB ) return NULL;
       
   112     TUint32* pD = pB->iData;
       
   113        
       
   114     if( iData && iReadyToRender )
       
   115            {
       
   116            // Factor between real and viewed image
       
   117            TReal realToViewedFactorWidth;
       
   118            TReal realToViewedFactorHeight;
       
   119            RealToViewedFactories(realToViewedFactorWidth, realToViewedFactorHeight);           
       
   120            const TInt width( iBitmapSize.iWidth ); // Data bitmap width
       
   121 
       
   122            for( TInt y(pB->iRect.iTl.iY); y<pB->iRect.iBr.iY; ++y )
       
   123                {
       
   124                TInt realY(y / Scale() + 0.5);
       
   125                TInt viewY(realY / realToViewedFactorHeight + 0.5);               
       
   126                for( TInt x(pB->iRect.iTl.iX); x<pB->iRect.iBr.iX; ++x )
       
   127                    {
       
   128                    TInt realX(x / Scale() + 0.5);
       
   129                    TInt viewX(realX / realToViewedFactorWidth + 0.5);
       
   130 
       
   131                    TUint32 color = iData[ width*viewY + viewX ];
       
   132                    if( (color & 0xff000000) )
       
   133                        {                           
       
   134                        *pD = color;
       
   135                        }
       
   136                        
       
   137                    *pD++;
       
   138                    }                 
       
   139                }    
       
   140            }   
       
   141     return pB;
       
   142 	}
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // SetParent
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 void CFilterDraw::SetParent( MImageFilter* aParent )
       
   149 	{
       
   150 	ASSERT(aParent);
       
   151 	iParent = aParent;
       
   152 	}
       
   153 
       
   154 // ---------------------------------------------------------------------------
       
   155 // SetChild
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 void CFilterDraw::SetChild( MImageFilter* aChild )
       
   159 	{
       
   160 	ASSERT(aChild);
       
   161 	iChild = aChild;
       
   162 	}
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // CmdL
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 TInt CFilterDraw::CmdL( const TDesC16& aCmd )
       
   169 	{
       
   170 	ASSERT(iChild);
       
   171 	TLex lex (aCmd);
       
   172 	TPoint position(0,0);	
       
   173     // Factor between real and viewed image
       
   174     TReal realToViewedFactorWidth;
       
   175     TReal realToViewedFactorHeight;
       
   176     RealToViewedFactories(realToViewedFactorWidth, realToViewedFactorHeight);
       
   177     
       
   178 	//	Handle parameters
       
   179     while( !lex.Eos() )
       
   180 		{
       
   181 		TPtrC token = lex.NextToken();
       
   182 		if( token.Compare( _L("x") ) == 0 )
       
   183 			{
       
   184 			iReadyToRender = EFalse;
       
   185             TReal relscale = Scale();
       
   186             TInt param = 0;
       
   187 			lex.Inc ();
       
   188 			lex.Val (param);
       
   189 			// Coordinates on data bitmap
       
   190             position.iX = (TReal(param) / relscale) / realToViewedFactorWidth + 0.5;
       
   191 			}
       
   192 		else if( token.Compare( _L("y") ) == 0 )
       
   193 			{
       
   194             TReal relscale = Scale();
       
   195             TInt param = 0;
       
   196 			lex.Inc ();
       
   197 			lex.Val (param);
       
   198             position.iY = (TReal(param) / relscale) / realToViewedFactorHeight + 0.5;	    			
       
   199             RDebug::Print(_L("CFilterDraw::CmdL x:%d y:%d Scale:%g Rw:%d Rh:%d Vpw:%d Vph:%d Rtvw:%g Rtvh:%g"), 
       
   200                    position.iX,
       
   201                    position.iY,
       
   202                    relscale,
       
   203                    Rect().Size().iWidth,
       
   204                    Rect().Size().iHeight,
       
   205                    ViewPortSize().iWidth,
       
   206                    ViewPortSize().iHeight,
       
   207                    realToViewedFactorWidth,
       
   208                    realToViewedFactorHeight);   			
       
   209 		    
       
   210 			if(!iPaths.Count())
       
   211 				{
       
   212 				RDrawPath newPath;
       
   213 				User::LeaveIfError( newPath.Append(position) );
       
   214 				User::LeaveIfError( iPaths.Append( newPath ) );
       
   215 				}
       
   216 			else
       
   217 				{	
       
   218 				ASSERT(iPaths.Count());	
       
   219 				RDrawPath& lastPath = iPaths[iPaths.Count()-1];
       
   220 				User::LeaveIfError( lastPath.Append(position) );				
       
   221 				}
       
   222 			}
       
   223 		else if( token.Compare( _L("color") ) == 0 )
       
   224 			{
       
   225 			TUint32 color(0);
       
   226 			lex.Inc();
       
   227 			lex.Val( color, EDecimal );
       
   228 			TRgb rgb(color);
       
   229 			TUint32 colorValue = 
       
   230 			    ( rgb.Red() << 16 ) + ( rgb.Green() << 8 ) + rgb.Blue();
       
   231             ASSERT( iPaths.Count() );
       
   232 			RDrawPath& lastPath = iPaths[iPaths.Count()-1];            
       
   233             lastPath.SetColor( TRgb(colorValue) );			
       
   234 			}			
       
   235 		else if( token.Compare( _L("size") ) == 0 )
       
   236 			{
       
   237 			TInt size(0);
       
   238 			lex.Inc ();
       
   239 			lex.Val(size);
       
   240 			// Scale line size to match bitmap scale
       
   241 			TInt sizew( (TReal(size) / realToViewedFactorWidth) + 0.5 );
       
   242 			TInt sizeh( (TReal(size) / realToViewedFactorHeight) + 0.5 );
       
   243 			
       
   244 			if( !sizew )
       
   245 			    {
       
   246 			    sizew++;
       
   247 			    }
       
   248             if( !sizeh )
       
   249                 {
       
   250                 sizeh++;
       
   251                 }			
       
   252 						
       
   253 			if(iPaths.Count())	
       
   254 				{
       
   255 				RDrawPath& lastPath = iPaths[iPaths.Count()-1];
       
   256 				lastPath.SetSize( TSize(sizew, sizeh) );			
       
   257 				}
       
   258 			}
       
   259         else if( token.Compare( _L("lastItem") ) == 0 )
       
   260             {
       
   261             ASSERT(iPaths.Count()); 
       
   262             
       
   263             RDrawPath& lastPath = iPaths[iPaths.Count()-1];
       
   264             RDebug::Print(_L("CFilterDraw::CmdL lastItem count:%d size:%d r:%d g:%d b:%d"), 
       
   265                     iPaths.Count(), lastPath.Size().iHeight,
       
   266                     lastPath.Color().Red(),
       
   267                     lastPath.Color().Green(),
       
   268                     lastPath.Color().Blue()
       
   269                     );
       
   270 
       
   271             RDrawPath newPath;
       
   272             User::LeaveIfError( iPaths.Append( newPath ) );             
       
   273             }   		
       
   274         else if( token.Compare( _L("done") ) == 0 )
       
   275             {
       
   276             LoadFrameL();
       
   277             iReadyToRender = ETrue;
       
   278             } 				
       
   279 		}
       
   280 
       
   281     return KErrNone;
       
   282 	}
       
   283 
       
   284 // ---------------------------------------------------------------------------
       
   285 // Type
       
   286 // ---------------------------------------------------------------------------
       
   287 //
       
   288 const char* CFilterDraw::Type()
       
   289 	{
       
   290 	return "frame";
       
   291 	}
       
   292 
       
   293 // ---------------------------------------------------------------------------
       
   294 // LoadFrameL
       
   295 // ---------------------------------------------------------------------------
       
   296 //
       
   297 void CFilterDraw::LoadFrameL()
       
   298 	{ 
       
   299     RDebug::Print(_L("CFilterDraw::LoadFrameL w:%d h:%d"), 
       
   300         iBitmapSize.iWidth,
       
   301         iBitmapSize.iHeight);  
       
   302 
       
   303     //  Create a bitmap big enough to hold the drawed lines
       
   304 	CFbsBitmap* bitmap = new(ELeave) CFbsBitmap();
       
   305 	CleanupStack::PushL( bitmap );
       
   306     User::LeaveIfError( bitmap->Create( iBitmapSize, EColor16MA ) );
       
   307     
       
   308 	// create mask
       
   309 	CFbsBitmap* mask = new (ELeave) CFbsBitmap();	
       
   310 	CleanupStack::PushL( mask );
       
   311 	User::LeaveIfError( mask->Create(iBitmapSize,EColor16MA) );
       
   312 
       
   313 	CFbsBitmapDevice* maskDevice = CFbsBitmapDevice::NewL( mask );
       
   314 	CleanupStack::PushL( maskDevice );
       
   315 
       
   316     CFbsBitGc * maskContext(NULL);
       
   317     User::LeaveIfError( maskDevice->CreateContext(maskContext) );
       
   318     CleanupStack::PushL(maskContext);
       
   319     maskContext->SetPenStyle(CGraphicsContext::ESolidPen);
       
   320 	maskContext->SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   321 	maskContext->SetBrushColor(KRgbBlack);
       
   322     
       
   323     //  Create bitmap device and context
       
   324     CFbsBitmapDevice * bitmapDevice = CFbsBitmapDevice::NewL (bitmap); 
       
   325     CleanupStack::PushL (bitmapDevice);
       
   326 
       
   327 	//	Create bitmap graphics context
       
   328     CFbsBitGc * bitmapContext(NULL);
       
   329     User::LeaveIfError (bitmapDevice->CreateContext (bitmapContext));
       
   330     CleanupStack::PushL (bitmapContext);
       
   331       	
       
   332 	TDisplayMode dmode = bitmap->DisplayMode();
       
   333    		
       
   334 	for(TInt pathNumber(0); pathNumber<iPaths.Count(); pathNumber++)
       
   335    		{
       
   336    		RDrawPath path = iPaths[pathNumber];
       
   337 		bitmapContext->SetPenStyle (CGraphicsContext::ESolidPen);
       
   338 		bitmapContext->SetBrushStyle (CGraphicsContext::ESolidBrush);
       
   339 		bitmapContext->SetPenColor(path.Color());
       
   340 		bitmapContext->SetPenSize(path.Size());
       
   341 		maskContext->SetPenSize(path.Size());   		
       
   342 
       
   343 	    RDebug::Print(_L("CFilterDraw::LoadFrameL ps:%dx%d S:%g"), 
       
   344             path.Size().iWidth,
       
   345             path.Size().iHeight,
       
   346             Scale());  
       
   347 	    
       
   348 		CArrayFix<TPoint>* pointArray = NULL;
       
   349 		RDrawPath2PointArray(path, pointArray);
       
   350 		bitmapContext->DrawPolyLine( pointArray );
       
   351 		maskContext->DrawPolyLine( pointArray );
       
   352 
       
   353 		delete pointArray;
       
   354    		}
       
   355 		
       
   356 	//	Create memory buffer to hold rendered image data
       
   357 
       
   358     if( !iData )
       
   359         {
       
   360         iData = new (ELeave) TUint32 [iBitmapSize.iWidth * iBitmapSize.iHeight];
       
   361         }
       
   362     Mem::FillZ(iData, 
       
   363             iBitmapSize.iWidth * iBitmapSize.iHeight * sizeof (TUint32));
       
   364 
       
   365     TBitmapUtil bm (bitmap);
       
   366     bm.Begin(TPoint(0,0));
       
   367     TBitmapUtil maskbm (mask);
       
   368     maskbm.Begin(TPoint(0,0));    
       
   369     TRgb rgb(0);
       
   370     // Find drawed lines from bitmap    
       
   371     for (TInt y(0); y < iBitmapSize.iHeight - 1; y++ )
       
   372     	{
       
   373     	for (TInt x(0); x < iBitmapSize.iWidth - 1; x++ )
       
   374     		{
       
   375     		// Check mask first
       
   376     		maskbm.SetPos(TPoint(x,y));
       
   377     		if( maskbm.GetPixel() == KRgbBlack.Internal() )
       
   378     			{     			
       
   379     			bm.SetPos(TPoint(x,y));
       
   380     			rgb = bm.GetPixel();
       
   381        			iData[(iBitmapSize.iWidth*y)+x] = 
       
   382        				( rgb.Red() << 16 ) + ( rgb.Green() << 8 ) + rgb.Blue() | 
       
   383        				0xff000000;
       
   384     			}
       
   385     		}
       
   386     	}
       
   387 	bm.End();
       
   388 	maskbm.End();
       
   389 	// bitmapContext, bitmapDevice, maskContext, maskDevice, mask, bitmap
       
   390 	CleanupStack::PopAndDestroy(6, bitmap); 
       
   391     RDebug::Print(_L("CFilterDraw::LoadFrameL - end"));  	
       
   392 	}
       
   393 	
       
   394 // ---------------------------------------------------------------------------
       
   395 // RDrawPath2PointArray
       
   396 // ---------------------------------------------------------------------------
       
   397 //
       
   398 void CFilterDraw::RDrawPath2PointArray( 
       
   399 	const RDrawPath& aPath, CArrayFix<TPoint>*& aArrayPtr) const
       
   400     {   
       
   401     // if allocation fails just do nothing. +1 if count is zero
       
   402     aArrayPtr = new CArrayFixFlat<TPoint> ( aPath.Count() + 1 );
       
   403     if (aArrayPtr)
       
   404     	{
       
   405     	for( TInt i(0); i<aPath.Count(); i++ )
       
   406     		{
       
   407     		TPoint item = aPath[i];
       
   408     		TRAP_IGNORE( aArrayPtr->AppendL( item ) );
       
   409     		}
       
   410     	}
       
   411     }
       
   412 
       
   413 // ---------------------------------------------------------------------------
       
   414 // RealToViewedFactories
       
   415 // ---------------------------------------------------------------------------
       
   416 //
       
   417 void CFilterDraw::RealToViewedFactories(TReal& aWidth, TReal& aHeight)
       
   418     {
       
   419     // Factor between real and viewed image
       
   420     aWidth = TReal(ViewPortSize().iWidth) / 
       
   421         TReal(iBitmapSize.iWidth);
       
   422     aHeight = TReal(ViewPortSize().iHeight) / 
       
   423         TReal(iBitmapSize.iHeight);    
       
   424     }
       
   425