ui/commandhandlers/commoncommandhandlers/src/glxcommandhandlerrotate.cpp
changeset 23 74c9f037fd5d
child 24 99ad1390cd33
equal deleted inserted replaced
5:f7f0874bfe7d 23:74c9f037fd5d
       
     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 <QDebug>
       
    23 #include <ExifRead.h>
       
    24 
       
    25 GlxCommandHandlerRotate::GlxCommandHandlerRotate()
       
    26 {
       
    27     qDebug("GlxCommandHandlerRotate::GlxCommandHandlerRotate() ");
       
    28 }
       
    29 
       
    30 GlxCommandHandlerRotate::~GlxCommandHandlerRotate()
       
    31 {
       
    32     qDebug("GlxCommandHandlerRotate::~GlxCommandHandlerRotate() ");
       
    33 }
       
    34 
       
    35 CMPXCommand* GlxCommandHandlerRotate::CreateCommandL(TInt aCommandId, MGlxMediaList& aMediaList, TBool& aConsume) const 
       
    36 {
       
    37 	Q_UNUSED(aCommandId);
       
    38 	Q_UNUSED(aConsume);
       
    39     qDebug("GlxCommandHandlerRotate::CreateCommandL");
       
    40     return NULL;
       
    41 }
       
    42 
       
    43 void GlxCommandHandlerRotate::DoExecuteCommandL(TInt aCommandId, MGlxMediaList& aMediaList, TBool& aConsume)
       
    44 {
       
    45 	Q_UNUSED(aCommandId);
       
    46 	Q_UNUSED(aConsume);
       
    47     qDebug("GlxCommandHandlerRotate::DoExecuteCommandL");
       
    48     const TGlxMedia& item = aMediaList.Item( aMediaList.FocusIndex() );
       
    49 	TFileName uri;
       
    50 	uri.Copy(item.Uri());
       
    51 	TRAPD(err,RotateImageL(uri));
       
    52 	if(err != KErrNone) {
       
    53 		qDebug("GlxCommandHandlerRotate::DoExecuteCommandL Exif Update failed");
       
    54 
       
    55 	}
       
    56 }
       
    57 void GlxCommandHandlerRotate::RotateImageL(TFileName aFileToBeRotated)
       
    58 {
       
    59 	//Start an IFS session
       
    60 	//File system 
       
    61     User::LeaveIfError(iFs.Connect());
       
    62 	//first initialize the Exif Writer which is responsible for reading and writing the Exif Data
       
    63 	//Will leave here itself in cases where the Exif data is absent
       
    64 	InitializeExifWriterL(aFileToBeRotated);
       
    65 	//read the Orientation tag stored in  the Exif Data
       
    66 	TUint16 initialOrientation = ReadImageOrientationL();
       
    67 	//as the image is rotated to 90 degrees clockwise calculate the new orientation by adding that angle
       
    68 	TUint16 finalOrientation = CalculateFinalOrientationL(initialOrientation);
       
    69 	// Set the Final Orientation on the file
       
    70 	SetImageOrientationL(finalOrientation);
       
    71 	// Clear the sessions acquired
       
    72 	DestroyExifWriter();
       
    73 	//close the File Session
       
    74 	iFs.Close();
       
    75 }
       
    76 
       
    77 void GlxCommandHandlerRotate::InitializeExifWriterL(TFileName aFileToBeRotated)
       
    78 {
       
    79 	User::LeaveIfError(iFileHandle.Open(iFs,
       
    80                     aFileToBeRotated, EFileWrite));
       
    81 	TInt filesize;
       
    82     User::LeaveIfError(iFileHandle.Size(filesize));
       
    83 	iExifData = HBufC8::NewL(filesize);
       
    84 	TPtr8 ptr(iExifData->Des());
       
    85     User::LeaveIfError(iFileHandle.Read(ptr));
       
    86    	iExifWriter = CExifModify::NewL(*iExifData,CExifModify::EModify,CExifModify::ENoJpegParsing);
       
    87 }
       
    88 
       
    89 TUint16 GlxCommandHandlerRotate::ReadImageOrientationL()
       
    90 {
       
    91 	TUint16 initialOrientation;
       
    92 	const CExifRead* exifReader = iExifWriter->Reader(); //not owned
       
    93     User::LeaveIfError(exifReader->GetOrientation(initialOrientation));
       
    94 	qDebug("GlxCommandHandlerRotate::ReadImageOrientationL initial orientation = %d", initialOrientation);
       
    95 	return (initialOrientation);
       
    96 }
       
    97 
       
    98 TUint16 GlxCommandHandlerRotate::CalculateFinalOrientationL(TUint16 aInitialOrientation)
       
    99 {
       
   100 	/*
       
   101      * possible orientation state with angles for rotation
       
   102      * Possible Angles 0 - 90 - 180 - 270
       
   103      * Possible states 1 - 8 - 3 - 6 without a Flip
       
   104      * Possible states 2 - 7 - 4 - 5 when Flip is on
       
   105      */
       
   106 	TUint16 finalOrientation = aInitialOrientation;
       
   107 	if(aInitialOrientation >8 ) {
       
   108 		//invalid orientation passed Leave
       
   109         User::Leave(KErrCorrupt);
       
   110     }
       
   111 	TInt rotOffset = 1;
       
   112 	TInt isOrientationOdd = aInitialOrientation % 2;
       
   113 	TInt initStateIndex = 0;
       
   114     TInt finalStateIndex = 0;
       
   115     //Setting the orientation states for the initial unflipped orientation combinations
       
   116     TInt orientationStateArray[] = {1,8,3,6};
       
   117     //Seting the index for current orientation
       
   118     if(aInitialOrientation < 3)
       
   119         {
       
   120         initStateIndex = 0;
       
   121         }
       
   122     else if(aInitialOrientation >= 3 && aInitialOrientation < 5)
       
   123         {
       
   124         initStateIndex = 2;
       
   125         }
       
   126     else if(aInitialOrientation >= 5 && aInitialOrientation < 7)
       
   127         {
       
   128         initStateIndex = 3;
       
   129         }
       
   130     else if(aInitialOrientation >= 7 && aInitialOrientation <= 8)
       
   131         {
       
   132         initStateIndex = 1;
       
   133         }
       
   134     //Calculating the final orientation using the cyclic orientationStateArray. 
       
   135     //folding final index so that it behaves like a cyclic machine 
       
   136     finalStateIndex = (initStateIndex+rotOffset)%4;
       
   137     finalOrientation = orientationStateArray[finalStateIndex];
       
   138     //Checking if a Flip was present 
       
   139     if(aInitialOrientation>4 && isOrientationOdd )
       
   140         {
       
   141         finalOrientation -= 1;
       
   142         }
       
   143     if(aInitialOrientation<5 && !isOrientationOdd)
       
   144         {
       
   145         finalOrientation += 1;
       
   146         }
       
   147 	qDebug("GlxCommandHandlerRotate::CalculateFinalOrientationL final orientation = %d", finalOrientation);
       
   148     return finalOrientation;
       
   149 }
       
   150 
       
   151 void GlxCommandHandlerRotate::SetImageOrientationL(TUint16 aFinalOrientation)
       
   152 {
       
   153 	iExifWriter->SetOrientationL(aFinalOrientation);
       
   154 	HBufC8* modifiedexifData=NULL;
       
   155 	modifiedexifData = iExifWriter->WriteDataL(iExifData->Des());
       
   156 	User::LeaveIfError(iFileHandle.Write(0,modifiedexifData->Des()));
       
   157     delete modifiedexifData;	
       
   158 }
       
   159 
       
   160 void GlxCommandHandlerRotate::DestroyExifWriter()
       
   161 {
       
   162 	iFileHandle.Close();
       
   163 	if(iExifData != NULL)
       
   164         {
       
   165         delete iExifData;
       
   166         iExifData = NULL;
       
   167         }
       
   168     if(iExifWriter != NULL)
       
   169         {
       
   170         delete iExifWriter;
       
   171         iExifWriter = NULL;
       
   172         } 
       
   173 }