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