ui/commandhandlers/commoncommandhandlers/src/glxcommandhandlerrotate.cpp
branchRCL_3
changeset 59 8e5f6eea9c9f
equal deleted inserted replaced
57:ea65f74e6de4 59:8e5f6eea9c9f
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <mpxcollectionpath.h>
       
    19 #include <mglxmedialist.h>
       
    20 #include <glxcommandfactory.h>
       
    21 #include <glxcommandhandlerrotate.h>
       
    22 #include <ExifRead.h>
       
    23 #include "OstTraceDefinitions.h"
       
    24 #ifdef OST_TRACE_COMPILER_IN_USE
       
    25 #include "glxcommandhandlerrotateTraces.h"
       
    26 #endif
       
    27 
       
    28 
       
    29 GlxCommandHandlerRotate::GlxCommandHandlerRotate()
       
    30     {
       
    31     OstTraceFunctionEntry0( GLXCOMMANDHANDLERROTATE_GLXCOMMANDHANDLERROTATE_ENTRY );
       
    32     OstTraceFunctionExit0( GLXCOMMANDHANDLERROTATE_GLXCOMMANDHANDLERROTATE_EXIT );
       
    33     }
       
    34 
       
    35 GlxCommandHandlerRotate::~GlxCommandHandlerRotate()
       
    36     {
       
    37     OstTraceFunctionEntry0( DUP1_GLXCOMMANDHANDLERROTATE_GLXCOMMANDHANDLERROTATE_ENTRY );
       
    38     OstTraceFunctionExit0( DUP1_GLXCOMMANDHANDLERROTATE_GLXCOMMANDHANDLERROTATE_EXIT );
       
    39     }
       
    40 
       
    41 CMPXCommand* GlxCommandHandlerRotate::CreateCommandL(TInt aCommandId,
       
    42         MGlxMediaList& /*aMediaList*/, TBool& aConsume) const
       
    43     {
       
    44     OstTraceFunctionEntry0( GLXCOMMANDHANDLERROTATE_CREATECOMMANDL_ENTRY );
       
    45     Q_UNUSED(aCommandId);
       
    46     Q_UNUSED(aConsume);
       
    47     OstTraceFunctionExit0( GLXCOMMANDHANDLERROTATE_CREATECOMMANDL_EXIT );
       
    48     return NULL;
       
    49     }
       
    50 
       
    51 void GlxCommandHandlerRotate::DoExecuteCommandL(TInt aCommandId,
       
    52         MGlxMediaList& aMediaList, TBool& aConsume)
       
    53     {
       
    54     OstTraceFunctionEntry0( GLXCOMMANDHANDLERROTATE_DOEXECUTECOMMANDL_ENTRY );
       
    55     Q_UNUSED(aCommandId);
       
    56     Q_UNUSED(aConsume);
       
    57     const TGlxMedia& item = aMediaList.Item(aMediaList.FocusIndex());
       
    58     TFileName uri;
       
    59     uri.Copy(item.Uri());
       
    60     TRAPD(err,RotateImageL(uri));
       
    61     if (err != KErrNone)
       
    62         {
       
    63         OstTrace0( TRACE_NORMAL, GLXCOMMANDHANDLERROTATE_DOEXECUTECOMMANDL, "GlxCommandHandlerRotate::DoExecuteCommandL Exif Update failed" );
       
    64         }
       
    65     OstTraceFunctionExit0( GLXCOMMANDHANDLERROTATE_DOEXECUTECOMMANDL_EXIT );
       
    66     }
       
    67 
       
    68 void GlxCommandHandlerRotate::RotateImageL(TFileName aFileToBeRotated)
       
    69     {
       
    70     OstTraceFunctionEntry0( GLXCOMMANDHANDLERROTATE_ROTATEIMAGEL_ENTRY );
       
    71     //Start an IFS session
       
    72     //File system 
       
    73     User::LeaveIfError(iFs.Connect());
       
    74     //first initialize the Exif Writer which is responsible for reading and writing the Exif Data
       
    75     //Will leave here itself in cases where the Exif data is absent
       
    76     InitializeExifWriterL(aFileToBeRotated);
       
    77     //read the Orientation tag stored in  the Exif Data
       
    78     TUint16 initialOrientation = ReadImageOrientationL();
       
    79     //as the image is rotated to 90 degrees clockwise calculate the new orientation by adding that angle
       
    80     TUint16 finalOrientation = CalculateFinalOrientationL(initialOrientation);
       
    81     // Set the Final Orientation on the file
       
    82     SetImageOrientationL(finalOrientation);
       
    83     // Clear the sessions acquired
       
    84     DestroyExifWriter();
       
    85     //close the File Session
       
    86     iFs.Close();
       
    87     OstTraceFunctionExit0( GLXCOMMANDHANDLERROTATE_ROTATEIMAGEL_EXIT );
       
    88     }
       
    89 
       
    90 void GlxCommandHandlerRotate::InitializeExifWriterL(
       
    91         TFileName aFileToBeRotated)
       
    92     {
       
    93     OstTraceFunctionEntry0( GLXCOMMANDHANDLERROTATE_INITIALIZEEXIFWRITERL_ENTRY );
       
    94     User::LeaveIfError(iFileHandle.Open(iFs, aFileToBeRotated, EFileWrite));
       
    95     TInt filesize;
       
    96     User::LeaveIfError(iFileHandle.Size(filesize));
       
    97     iExifData = HBufC8::NewL(filesize);
       
    98     TPtr8 ptr(iExifData->Des());
       
    99     User::LeaveIfError(iFileHandle.Read(ptr));
       
   100     iExifWriter = CExifModify::NewL(*iExifData, CExifModify::EModify,
       
   101             CExifModify::ENoJpegParsing);
       
   102     OstTraceFunctionExit0( GLXCOMMANDHANDLERROTATE_INITIALIZEEXIFWRITERL_EXIT );
       
   103     }
       
   104 
       
   105 TUint16 GlxCommandHandlerRotate::ReadImageOrientationL()
       
   106 {
       
   107 	OstTraceFunctionEntry0( GLXCOMMANDHANDLERROTATE_READIMAGEORIENTATIONL_ENTRY );
       
   108 	TUint16 initialOrientation;
       
   109 	const CExifRead* exifReader = iExifWriter->Reader(); //not owned
       
   110     User::LeaveIfError(exifReader->GetOrientation(initialOrientation));
       
   111 	OstTrace1( TRACE_NORMAL, GLXCOMMANDHANDLERROTATE_READIMAGEORIENTATIONL, "GlxCommandHandlerRotate::ReadImageOrientationL;initial orientation=%d", initialOrientation );
       
   112 	OstTraceFunctionExit0( GLXCOMMANDHANDLERROTATE_READIMAGEORIENTATIONL_EXIT );
       
   113 	return (initialOrientation);
       
   114 }
       
   115 
       
   116 TUint16 GlxCommandHandlerRotate::CalculateFinalOrientationL(
       
   117         TUint16 aInitialOrientation)
       
   118     {
       
   119     OstTraceFunctionEntry0( GLXCOMMANDHANDLERROTATE_CALCULATEFINALORIENTATIONL_ENTRY );
       
   120     /*
       
   121      * possible orientation state with angles for rotation
       
   122      * Possible Angles 0 - 90 - 180 - 270
       
   123      * Possible states 1 - 8 - 3 - 6 without a Flip
       
   124      * Possible states 2 - 7 - 4 - 5 when Flip is on
       
   125      */
       
   126     TUint16 finalOrientation = aInitialOrientation;
       
   127     if (aInitialOrientation > 8)
       
   128         {
       
   129         //invalid orientation passed Leave
       
   130         User::Leave(KErrCorrupt);
       
   131         }
       
   132     TInt rotOffset = 1;
       
   133     TInt isOrientationOdd = aInitialOrientation % 2;
       
   134     TInt initStateIndex = 0;
       
   135     TInt finalStateIndex = 0;
       
   136     //Setting the orientation states for the initial unflipped orientation combinations
       
   137     TInt orientationStateArray[] =
       
   138         {
       
   139         1, 8, 3, 6
       
   140         };
       
   141     //Seting the index for current orientation
       
   142     if (aInitialOrientation < 3)
       
   143         {
       
   144         initStateIndex = 0;
       
   145         }
       
   146     else if (aInitialOrientation >= 3 && aInitialOrientation < 5)
       
   147         {
       
   148         initStateIndex = 2;
       
   149         }
       
   150     else if (aInitialOrientation >= 5 && aInitialOrientation < 7)
       
   151         {
       
   152         initStateIndex = 3;
       
   153         }
       
   154     else if (aInitialOrientation >= 7 && aInitialOrientation <= 8)
       
   155         {
       
   156         initStateIndex = 1;
       
   157         }
       
   158     //Calculating the final orientation using the cyclic orientationStateArray. 
       
   159     //folding final index so that it behaves like a cyclic machine 
       
   160     finalStateIndex = (initStateIndex + rotOffset) % 4;
       
   161     finalOrientation = orientationStateArray[finalStateIndex];
       
   162     //Checking if a Flip was present 
       
   163     if (aInitialOrientation > 4 && isOrientationOdd)
       
   164         {
       
   165         finalOrientation -= 1;
       
   166         }
       
   167     if (aInitialOrientation < 5 && !isOrientationOdd)
       
   168         {
       
   169         finalOrientation += 1;
       
   170         }
       
   171     OstTrace1( TRACE_NORMAL, GLXCOMMANDHANDLERROTATE_CALCULATEFINALORIENTATIONL, "GlxCommandHandlerRotate::CalculateFinalOrientationL;finalOrientation=%d", finalOrientation );
       
   172     OstTraceFunctionExit0( GLXCOMMANDHANDLERROTATE_CALCULATEFINALORIENTATIONL_EXIT );
       
   173     return finalOrientation;
       
   174     }
       
   175 
       
   176 void GlxCommandHandlerRotate::SetImageOrientationL(TUint16 aFinalOrientation)
       
   177     {
       
   178     OstTraceFunctionEntry0( GLXCOMMANDHANDLERROTATE_SETIMAGEORIENTATIONL_ENTRY );
       
   179     iExifWriter->SetOrientationL(aFinalOrientation);
       
   180     HBufC8* modifiedexifData = NULL;
       
   181     modifiedexifData = iExifWriter->WriteDataL(iExifData->Des());
       
   182     User::LeaveIfError(iFileHandle.Write(0, modifiedexifData->Des()));
       
   183     delete modifiedexifData;
       
   184     OstTraceFunctionExit0( GLXCOMMANDHANDLERROTATE_SETIMAGEORIENTATIONL_EXIT );
       
   185     }
       
   186 
       
   187 void GlxCommandHandlerRotate::DestroyExifWriter()
       
   188     {
       
   189     OstTraceFunctionEntry0( GLXCOMMANDHANDLERROTATE_DESTROYEXIFWRITER_ENTRY );
       
   190     iFileHandle.Close();
       
   191     if (iExifData != NULL)
       
   192         {
       
   193         delete iExifData;
       
   194         iExifData = NULL;
       
   195         }
       
   196     if (iExifWriter != NULL)
       
   197         {
       
   198         delete iExifWriter;
       
   199         iExifWriter = NULL;
       
   200         }
       
   201     OstTraceFunctionExit0( GLXCOMMANDHANDLERROTATE_DESTROYEXIFWRITER_EXIT );
       
   202     }