imaging/imagingfws/src/Test/TImageTran/TImageTran.cpp
changeset 0 5752a19fdefe
equal deleted inserted replaced
-1:000000000000 0:5752a19fdefe
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "TImageTran.h"
       
    17 
       
    18 /*
       
    19 *    Starting point of the application
       
    20 */
       
    21 
       
    22 TInt E32Main() 
       
    23     {
       
    24     __UHEAP_MARK;
       
    25     // Setup TRAP harness
       
    26     CTrapCleanup* cleanup = CTrapCleanup::New();
       
    27     CITArgsandFuncts *iITArgsandFunct = NULL;
       
    28     TRAPD(error, iITArgsandFunct = CITArgsandFuncts::NewL());
       
    29     if(error != KErrNone)
       
    30         {
       
    31         return error;
       
    32         }
       
    33 
       
    34     TInt r = KErrNoMemory;
       
    35     if ( cleanup ) 
       
    36         {
       
    37         TRAP(r, iITArgsandFunct->ImageTranMainL());
       
    38         __ASSERT_ALWAYS( !r, User::Invariant() );
       
    39         delete cleanup;
       
    40         }
       
    41     
       
    42     delete  iITArgsandFunct; 
       
    43     // check we didn't leak any memory
       
    44     __UHEAP_MARKEND;
       
    45     return r;
       
    46     }
       
    47 
       
    48 void CITArgsandFuncts::ConstructL()
       
    49     {
       
    50     
       
    51     }
       
    52         
       
    53 CITArgsandFuncts* CITArgsandFuncts::NewL()
       
    54 	{
       
    55 	CITArgsandFuncts* self = new (ELeave) CITArgsandFuncts();
       
    56 	CleanupStack::PushL(self);
       
    57 	self->ConstructL();
       
    58 	CleanupStack::Pop(self);
       
    59 	return self;
       
    60 	}
       
    61     
       
    62 /*
       
    63  * Start the image transformation by parsing the commandline arguments
       
    64  */
       
    65 void CITArgsandFuncts::ImageTranMainL() 
       
    66     {
       
    67     iConsole = Console::NewL( _L( "iConsole"), TSize( KConsFullScreen,KConsFullScreen ) );
       
    68     CleanupStack::PushL( iConsole );
       
    69     iCmdArgs  = CCommandLineArguments::NewLC();
       
    70     
       
    71     CActiveScheduler* iScheduler =new(ELeave) CActiveScheduler;
       
    72 	CleanupStack::PushL(iScheduler);
       
    73 	CActiveScheduler::Install(iScheduler);
       
    74     
       
    75     TInt err;
       
    76     TRAP( err, ImageTransformL() ); /*Retrive the command line args, parse args  and transform*/
       
    77     if ( err ) 
       
    78         {
       
    79         iConsole->Printf( _L("TRAP: error code(%d)\n"), err );
       
    80         }
       
    81 
       
    82     iConsole->Printf( _L( "[press any key]\n" ) );
       
    83     iConsole->Getch();
       
    84     CleanupStack::PopAndDestroy();
       
    85     CActiveScheduler::Stop();
       
    86     }
       
    87     
       
    88 /*
       
    89 *  Retrive the command line args, parse args and perform transform
       
    90 *  operation based on the option.
       
    91 */
       
    92 void CITArgsandFuncts::ImageTransformL() 
       
    93     {
       
    94     iConsole->Printf( _L( "   Image Transformation Tool\n" ) );
       
    95     TInt err = KErrNone;
       
    96  
       
    97     iToolName.Copy(KToolName);    
       
    98 
       
    99     if ( iCmdArgs->Count() >= 0 ) 
       
   100         {
       
   101         iConsole->Printf( _L( "Arguments available\n" ) );
       
   102         
       
   103         for ( int i=0; i < iCmdArgs->Count(); i++ ) 
       
   104             {
       
   105             iArgumentPtr.Set(iCmdArgs->Arg(i));
       
   106             iConsole->Printf(_L("%S "), &iArgumentPtr);
       
   107             iArgBuf.Append( iCmdArgs->Arg(i) );
       
   108             iArgBuf.Append( _L( " " ) );
       
   109             }
       
   110          iConsole->Printf(_L("\n"));
       
   111         //parse switches to get the filenames and the comamnd options
       
   112         ParseSwitches(iCmdArgs->Count());
       
   113 
       
   114         //Fecth the image size for autosqueeze purpose
       
   115         if(iTransOpt.iTransform == ITRAN_AUTOSQU)        
       
   116             {
       
   117             Fetch_ImageSize();
       
   118             }
       
   119         
       
   120         //prepare for transformation
       
   121         //RFbsSession::Connect();            
       
   122         User::LeaveIfError( iFs.Connect() );
       
   123         CleanupClosePushL(iFs);
       
   124 
       
   125         iImageTransform = CImageTransform::NewL(iFs);
       
   126         TInt fSize = 0;
       
   127         // get the maxinputfileSize
       
   128 		RFile file;
       
   129 		err = file.Open(iFs, iInputfile, EFileRead);
       
   130 		CleanupClosePushL(file);
       
   131 		
       
   132 		if(err != KErrNone)
       
   133 		    {
       
   134 		    if(err == KErrNotFound)
       
   135 		        {
       
   136                 iConsole->Printf(_L("Inputfile doesn't exist in the specified path\n"));
       
   137 				ExitImageTranTool();
       
   138 		        }
       
   139             else if(err == KErrBadName)    		        
       
   140                 {
       
   141                 iConsole->Printf(_L("Inputfile name doesn't confirm to required syntax\n"));
       
   142     		    ExitImageTranTool();
       
   143                 }
       
   144             else if(err == KErrPathNotFound)    		        
       
   145                 {
       
   146                 iConsole->Printf(_L("Specified Inputfile path or folder not found\n"));
       
   147     		    ExitImageTranTool();
       
   148                 }
       
   149             else if(err == KErrInUse)    		        
       
   150                 {
       
   151                 iConsole->Printf(_L("Specified Inputfile being opened for another task\n"));
       
   152     		    ExitImageTranTool();
       
   153                 }
       
   154 			else
       
   155 				{
       
   156 			    iConsole->Printf(_L("Opening the inputfile has failed with error : %d\n"), err);
       
   157 	    		ExitImageTranTool();
       
   158 				}                                
       
   159 		    }
       
   160 
       
   161 		// get the filesize
       
   162 		file.Size(fSize);
       
   163 	    CleanupStack::PopAndDestroy(&file);		
       
   164 		if(iProposedSize >= fSize)
       
   165 		    {
       
   166 		    iConsole->Printf(_L(" Proposed size for squeeze should be less than the input file size\n"));
       
   167             ExitImageTranTool();
       
   168 		    }
       
   169         //perform transformations based on command
       
   170         TRAP(err, Peform_TransformationL());
       
   171         if(err != KErrNone)
       
   172 	        {
       
   173 	        iConsole->Printf(_L(" Transformation failed %d\n"), err);
       
   174             ImageTranUsage();
       
   175 	        }
       
   176         CleanupStack::PopAndDestroy(&iFs);
       
   177         }
       
   178     }
       
   179     
       
   180 /* Parse optional switches.
       
   181  * Returns iCmdArgs[] index of first file-name argument (== aArgCount if none).
       
   182  * Any file names with indexes <= aLastFileArg are ignored;
       
   183  * they have presumably been processed in a previous iteration.
       
   184  * (Pass 0 for last_file_arg_seen on the first or only iteration.)
       
   185  */
       
   186 void CITArgsandFuncts::ParseSwitches ( TInt aArgCount)
       
   187     {
       
   188       int argNum = 1;
       
   189       
       
   190       ReadNextArg(argNum);
       
   191       /* Scan command line options, adjust parameters */
       
   192         if (iArgumentPtr.Left(1).Compare(_L("-")) == 0) 
       
   193             {
       
   194             if (KeywordMatch(iArgumentPtr.Mid(1), _L("squeeze"), 2)) //Squeeze
       
   195                 {
       
   196                 /* Read Next argument */
       
   197                 ReadNextArg(++argNum);
       
   198                 /* ParseSqueeze parameters */
       
   199                 argNum = ParseSqueezeParams(argNum);
       
   200                 }
       
   201             else if (KeywordMatch(iArgumentPtr.Mid(1),_L("autosqueeze"), 4)) //Autosqueeze
       
   202                 {
       
   203                 /* Read Next argument */
       
   204                 ReadNextArg(++argNum);
       
   205                 /* ParseAutoSqueeze parameters */
       
   206                 argNum = ParseAutoSqueezeParams(argNum);
       
   207                 }  
       
   208             else if (KeywordMatch(iArgumentPtr.Mid(1),_L("flip"),  1)) //Flip
       
   209                 {
       
   210                 /* Read Next argument */
       
   211                 ReadNextArg(++argNum);
       
   212                 /* ParseFlip parameters */
       
   213                 argNum = ParseFlipParams(argNum);
       
   214                 } 
       
   215             else if (KeywordMatch(iArgumentPtr.Mid(1),_L("rotate"), 2)) //Rotate
       
   216                 {
       
   217                 /* Read Next argument */
       
   218                 ReadNextArg(++argNum);
       
   219                 /* ParseRotate parameters */
       
   220                 argNum = ParseRotateParams(argNum);
       
   221                 }
       
   222 			else if (KeywordMatch(iArgumentPtr.Mid(1),_L("transpose"), 2)) //Transpose
       
   223                 {
       
   224                 /* Read Next argument */
       
   225                 ReadNextArg(++argNum);
       
   226                 /* ParseTranspose parameters */
       
   227                 argNum = ParseTransposeParams(argNum);
       
   228                 }
       
   229 			else if (KeywordMatch(iArgumentPtr.Mid(1),_L("transverse"), 2)) //Transverse
       
   230                 {
       
   231                 /* Read Next argument */
       
   232                 ReadNextArg(++argNum);
       
   233                 /* ParseTransverse parameters */
       
   234                 argNum = ParseTransverseParams(argNum);
       
   235                 }
       
   236 			else if (KeywordMatch(iArgumentPtr.Mid(1),_L("overlay"), 2)) //Overlay
       
   237                 {
       
   238                 /* Read Next argument */
       
   239                 ReadNextArg(++argNum);
       
   240                 /* ParseTransverse parameters */
       
   241                 argNum = ParseOverlayParams(argNum);
       
   242                 }
       
   243 			else if (KeywordMatch(iArgumentPtr.Mid(1),_L("help"), 2)) //Overlay
       
   244                 {
       
   245                 Select_Transform(ITRAN_HELP);
       
   246                 ImageTranUsage();
       
   247                 }                                                                  
       
   248             else 
       
   249                 {
       
   250                 /* unknown switch */
       
   251                 iConsole->Printf( _L( "Unknown [switches] in the command\n") );
       
   252                 ImageTranUsage();
       
   253                 }
       
   254             }
       
   255 
       
   256     if(aArgCount <= 1)
       
   257         {
       
   258         iConsole->Printf( _L( "[switches] is missng from the command\n"));
       
   259         ImageTranUsage();
       
   260         }      
       
   261     }
       
   262     
       
   263 /*
       
   264 * Perform transformations squeeze,autosqueeze,
       
   265 * rotation/flip,overlay,
       
   266 */
       
   267 void CITArgsandFuncts::Peform_TransformationL()
       
   268     {
       
   269 	CActiveTransformListener* listener= new (ELeave) CActiveTransformListener;
       
   270 	CleanupStack::PushL(listener);		
       
   271     TInt error = KErrNone;
       
   272    
       
   273     if(iTransOpt.iTransform == ITRAN_SQU)
       
   274         {
       
   275         SetupTransformation(iTransOpt.iTransform);
       
   276         iTransExtn->SetDestSizeInBytes((TInt)iProposedSize);
       
   277         listener->InitialiseActiveListener(iImageTransform);
       
   278         // launch the transform
       
   279     	iImageTransform->Transform(listener->iStatus);
       
   280     	CActiveScheduler::Start();
       
   281         }
       
   282     else if(iTransOpt.iTransform == ITRAN_AUTOSQU)
       
   283         {
       
   284         SetupTransformation(iTransOpt.iTransform);
       
   285         /* Set proposed size to destination size */
       
   286         iSqueezeAutoResizeParams.iMaxDestSizeInBytes = (TInt)iProposedSize;
       
   287         iTransExtn->SetAdvancedSqueezeModeL(&iSqueezeAutoResizeParams);
       
   288         listener->InitialiseActiveListener(iImageTransform);
       
   289     	// launch the transform
       
   290     	iImageTransform->Transform(listener->iStatus);
       
   291     	CActiveScheduler::Start();
       
   292         }
       
   293 	else if(iTransOpt.iTransform == ITRAN_ROTATE)
       
   294 		{
       
   295 		SetupTransformation(iTransOpt.iTransform);
       
   296 		iTransOrientation->SetOrientationL(iOrientation);
       
   297 		listener->InitialiseActiveListener(iImageTransform);
       
   298     	// launch the transform
       
   299     	iImageTransform->Transform(listener->iStatus);
       
   300     	CActiveScheduler::Start();
       
   301 		}
       
   302 	else if(iTransOpt.iTransform == ITRAN_FLIP)
       
   303 		{
       
   304 		SetupTransformation(iTransOpt.iTransform);
       
   305 		iTransOrientation->SetOrientationL(iOrientation);
       
   306 		listener->InitialiseActiveListener(iImageTransform);
       
   307     	// launch the transform
       
   308     	iImageTransform->Transform(listener->iStatus);
       
   309     	CActiveScheduler::Start();
       
   310 		}
       
   311 	else if(iTransOpt.iTransform == ITRAN_TRANSPOSE)
       
   312 		{
       
   313 		SetupTransformation(iTransOpt.iTransform);
       
   314 		iTransOrientation->SetOrientationL(iOrientation);
       
   315 		listener->InitialiseActiveListener(iImageTransform);
       
   316     	// launch the transform
       
   317     	iImageTransform->Transform(listener->iStatus);
       
   318     	CActiveScheduler::Start();
       
   319 		}
       
   320 	else if(iTransOpt.iTransform == ITRAN_TRANSVERSE)
       
   321 		{
       
   322 		SetupTransformation(iTransOpt.iTransform);
       
   323 		iTransOrientation->SetOrientationL(iOrientation);
       
   324 		listener->InitialiseActiveListener(iImageTransform);
       
   325     	// launch the transform
       
   326     	iImageTransform->Transform(listener->iStatus);
       
   327     	CActiveScheduler::Start();
       
   328 		}
       
   329 	else if(iTransOpt.iTransform == ITRAN_OVERLAY)
       
   330 		{
       
   331 		SetupOverlayTrans();
       
   332 		listener->InitialiseActiveListener(iImageTransform);
       
   333     	// launch the transform
       
   334     	iImageTransform->Transform(listener->iStatus);
       
   335     	CActiveScheduler::Start();
       
   336 		}										
       
   337 	//check status of transformation
       
   338 	error = listener->iStatus.Int();
       
   339 	if(error != KErrNone)
       
   340 	    {
       
   341 	    if(error == KErrArgument)
       
   342 		    {
       
   343 		    iConsole->Printf(_L("Bad transofrmation arguments in the specified command \n"));
       
   344 		    }
       
   345 		else if(error == KErrNoMemory)
       
   346 			{
       
   347 			iConsole->Printf(_L("Transofrmation failed due to not enough memory for processing\n"));		
       
   348 			}
       
   349 		else if(error == KErrNotSupported)
       
   350 			{
       
   351 			iConsole->Printf(_L("Transofrmation on the specifed inputfile format/arguments is not supported\n"));				
       
   352 			}
       
   353 		else
       
   354 			{
       
   355 			iConsole->Printf(_L("Transformation failed with error : %d\n"), error);				
       
   356 			}
       
   357         ExitImageTranTool();
       
   358 	    }
       
   359     else
       
   360         {
       
   361 	    iConsole->Printf(_L("Transformation is successful\n"));
       
   362         iConsole->Printf( _L( "[press any key]\n" ) );
       
   363         iConsole->Getch();		    
       
   364 	    User::Exit(EXIT_SUCCESS);            
       
   365         }		    
       
   366 	CleanupStack::PopAndDestroy(listener);
       
   367     }
       
   368     
       
   369 /* 
       
   370  * To detect multiple transform options,
       
   371  * and set the transforma option slected 
       
   372  */
       
   373 void CITArgsandFuncts::Select_Transform (ITRAN_CODES aTransform)
       
   374     {
       
   375     if (iTransOpt.iTransform == ITRAN_NONE || iTransOpt.iTransform == aTransform) 
       
   376         {
       
   377         iTransOpt.iTransform = aTransform;
       
   378         } 
       
   379     else
       
   380         {
       
   381         iConsole->Printf(_L("%s: can only do one image transformation at a time\n"), &iToolName);
       
   382         ImageTranUsage();
       
   383         }
       
   384     }
       
   385     
       
   386 /* 
       
   387 * Usage of the Imagetran Command line tool */
       
   388 
       
   389 void CITArgsandFuncts::ImageTranUsage ()
       
   390     {
       
   391     TKeyCode charKey;
       
   392     
       
   393     iConsole->Printf(_L("usage: %S [switches] [parameters] \n"), &iToolName);
       
   394     iConsole->Printf(_L("Switches for modifying the image:\n"));
       
   395 	iConsole->Printf(_L("       -squeeze     : squeeze the image <Press 1 for parameters>\n"));
       
   396 	iConsole->Printf(_L("       -autosqueeze : autosqueeze the image <Press 2 for parameters>\n"));
       
   397 	iConsole->Printf(_L("       -flip        : flip the image <Press 3 for parameters>\n"));
       
   398 	iConsole->Printf(_L("       -rotate      : rotate the image <Press 4 for parameters>\n"));
       
   399 	iConsole->Printf(_L("       -transpose   : transpose the image <Press 5 for parameters>\n"));
       
   400 	iConsole->Printf(_L("       -transverse  : transverse the image <Press 6 for parameters>\n"));
       
   401 	iConsole->Printf(_L("       -overlay     : overlay/replace the image <Press 7 for parameters>\n"));
       
   402 	iConsole->Printf(_L(" <Press 1 to 7 for [switch] parameters>\n"));
       
   403 	charKey = iConsole->Getch();	    	
       
   404 	do
       
   405 		{
       
   406 	    if(charKey == 49)
       
   407 		    {
       
   408 			iConsole->Printf(_L("Parameters  for squeeze are.....:\n"));
       
   409 			iConsole->Printf(_L("       -p [proposedsizeinbytes]  <proposedszie for autosqueeze>\n"));		    	
       
   410 		    }
       
   411 	    else if(charKey == 50)
       
   412 		    {
       
   413 			iConsole->Printf(_L("Parameters  for autosqueeze are.....:\n"));
       
   414 			iConsole->Printf(_L("       -p [proposedsizeinbytes]       <proposedszie for autosqueeze>\n"));
       
   415 			iConsole->Printf(_L("       -max [maximagesize]            <maximagesize for autosqueeze>\n"));
       
   416 			iConsole->Printf(_L("       -min [minimagesize]            <minimagesize for autosqueeze>\n"));
       
   417 			iConsole->Printf(_L("       -sqopt [autosqueezeoptions]    <autosqueeze options as mentioned below>\n"));
       
   418 			iConsole->Printf(_L("              		0  					<PreserveSourceEncodingQuality>\n"));
       
   419 			iConsole->Printf(_L("              		1					<PrioritizeLargeImageize>\n"));
       
   420 			iConsole->Printf(_L("              		2					<ResizePrioritizeHighEncodingQuality>\n"));
       
   421 			iConsole->Printf(_L("              		4					<MiddleCourse>\n")); 
       
   422 			iConsole->Printf(_L("       -mode [colourmodeoptions]             <colourmodeoptions for autosqueeze are as below>\n"));
       
   423 			iConsole->Printf(_L("                   0                         <420>\n"));
       
   424 			iConsole->Printf(_L("                   1                         <422>\n"));
       
   425 			iConsole->Printf(_L("                   2                         <444>\n"));
       
   426 			iConsole->Printf(_L("                   3                         <411>\n"));
       
   427 			iConsole->Printf(_L("        -q [qualityfactor]             <quality factor for autosqueeze>\n"));
       
   428 		    }
       
   429 		else if(charKey == 51)
       
   430 		    {
       
   431 			iConsole->Printf(_L("Parameters  for flip are.....:\n"));
       
   432 			iConsole->Printf(_L("       -flipopt [horizontal|vertical] <flip image left-right or top-bottom>\n"));
       
   433 		    }
       
   434 		else if(charKey == 52)
       
   435 		    {
       
   436 			iConsole->Printf(_L("Parameters  for rotate are.....:\n"));
       
   437 	    	iConsole->Printf(_L("       -ang [0|90|180|270]            <rotate image (degrees clockwise)>\n"));
       
   438 		    }
       
   439 		else if(charKey == 53)
       
   440 		    {
       
   441 			iConsole->Printf(_L("Parameters  for transpose are.....:\n"));
       
   442 		    }
       
   443 		else if(charKey == 54)
       
   444 		    {
       
   445 			iConsole->Printf(_L("Parameters  for transverse are.....:\n"));
       
   446 		    }	    	    	    
       
   447 		else if(charKey == 55)
       
   448 		    {
       
   449 			iConsole->Printf(_L("Parameters  for overlay are.....:\n"));
       
   450 			iConsole->Printf(_L("       -flipopt [horizontal|vertical] <flip image left-right or top-bottom>\n"));
       
   451 		    iConsole->Printf(_L("       -ovl [overlayfile]             <overlay image of type jpg,png,mbm>\n"));
       
   452 	    	iConsole->Printf(_L("       -pos [X,Y]            		   <X and Y co-ordinates of an overlay image>\n"));
       
   453 		    }
       
   454 	 	iConsole->Printf(_L("Common parameters for all the switches and the only parameters for transpose and transverse switch\n"));
       
   455 	    iConsole->Printf(_L("       -i [inputfile]                 <inputfilename for transformation>\n"));
       
   456 	    iConsole->Printf(_L("       -o [outputfile]                <outputfilename for transformation>\n"));
       
   457 	    iConsole->Printf(_L(" <Press 1 to 7 for [switch] parameters>\n"));
       
   458 		charKey = iConsole->Getch();
       
   459 		
       
   460 		}while(charKey >= 49 && charKey <= 55);
       
   461 	if(charKey < 49 || charKey > 55)
       
   462 		{
       
   463 		iConsole->Printf(_L(" Invalid keypress no switch command available for keypress \n"));
       
   464 		iConsole->Getch();
       
   465 		}
       
   466 	User::Exit(EXIT_SUCCESS);
       
   467     }
       
   468  
       
   469 
       
   470 /* Exit from the imagetran tool  */    
       
   471 void CITArgsandFuncts::ExitImageTranTool()
       
   472 	{
       
   473     iConsole->Printf(_L( "[press any key]\n" ) );
       
   474     iConsole->Getch();    
       
   475     User::Exit(EXIT_FAILURE);		
       
   476 	}
       
   477 	
       
   478 /*
       
   479 *   Destructor
       
   480 */    
       
   481 CITArgsandFuncts::~CITArgsandFuncts()
       
   482     {
       
   483     delete iImageTransform;
       
   484     delete iCmdArgs;
       
   485     delete iConsole;
       
   486     delete iScheduler;
       
   487     delete iTransExtn;
       
   488     }
       
   489