|
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 #include "tsgraphicfilescalinghandler.h" |
|
18 #include <imageconversion.h> |
|
19 |
|
20 // ----------------------------------------------------------------------------- |
|
21 // ----------------------------------------------------------------------------- |
|
22 // |
|
23 CTsGraphicFileScalingHandler::CTsGraphicFileScalingHandler(MImageReadyCallBack &aNotify, |
|
24 const TSize &aNewSize, |
|
25 TKindOfScaling aKindOfScaling |
|
26 /* = CTsGraphicFileScalingHandler::EIgnoreAspectRatio*/) : |
|
27 CActive(EPriorityNormal), |
|
28 mNotify(aNotify), |
|
29 mNewSize(aNewSize), |
|
30 mKindOfScaling(aKindOfScaling), |
|
31 mCurrentOperation(ENone) |
|
32 { |
|
33 CActiveScheduler::Add(this); |
|
34 } |
|
35 |
|
36 // ----------------------------------------------------------------------------- |
|
37 // ----------------------------------------------------------------------------- |
|
38 // |
|
39 EXPORT_C CTsGraphicFileScalingHandler::~CTsGraphicFileScalingHandler() |
|
40 { |
|
41 Cancel(); |
|
42 delete mInputBitmap; |
|
43 delete mOutputBitmap; |
|
44 delete mImageDecoder; |
|
45 delete mBitmapScaler; |
|
46 } |
|
47 |
|
48 // ----------------------------------------------------------------------------- |
|
49 // ----------------------------------------------------------------------------- |
|
50 // |
|
51 EXPORT_C CTsGraphicFileScalingHandler* CTsGraphicFileScalingHandler::NewL(MImageReadyCallBack &aNotify, |
|
52 RFs &aFs, |
|
53 const TDesC &aFileName, |
|
54 const TDesC8& aMimeType, |
|
55 const TSize &aNewSize, |
|
56 TKindOfScaling aKindOfScaling |
|
57 /* = CTsGraphicFileScalingHandler::EIgnoreAspectRatio*/) |
|
58 { |
|
59 CTsGraphicFileScalingHandler *self = CTsGraphicFileScalingHandler::NewLC(aNotify, |
|
60 aFs, |
|
61 aFileName, |
|
62 aMimeType, |
|
63 aNewSize, |
|
64 aKindOfScaling); |
|
65 CleanupStack::Pop(); |
|
66 return self; |
|
67 } |
|
68 |
|
69 // ----------------------------------------------------------------------------- |
|
70 // ----------------------------------------------------------------------------- |
|
71 // |
|
72 EXPORT_C CTsGraphicFileScalingHandler* CTsGraphicFileScalingHandler::NewLC(MImageReadyCallBack &aNotify, |
|
73 RFs &aFs, |
|
74 const TDesC &aFileName, |
|
75 const TDesC8& aMimeType, |
|
76 const TSize &aNewSize, |
|
77 TKindOfScaling aKindOfScaling |
|
78 /* = CTsGraphicFileScalingHandler::EIgnoreAspectRatio*/) |
|
79 { |
|
80 CTsGraphicFileScalingHandler *self = new (ELeave) CTsGraphicFileScalingHandler(aNotify, |
|
81 aNewSize, |
|
82 aKindOfScaling); |
|
83 |
|
84 CleanupStack::PushL(self); |
|
85 self->ConstructL(aFs, aFileName, aMimeType); |
|
86 return self; |
|
87 } |
|
88 |
|
89 // ----------------------------------------------------------------------------- |
|
90 // ----------------------------------------------------------------------------- |
|
91 // |
|
92 void CTsGraphicFileScalingHandler::ConstructL(RFs &aFs, const TDesC &aFileName, const TDesC8& aMimeType) |
|
93 { |
|
94 if (aFileName.Length() == 0 |
|
95 || aFs.IsValidName(aFileName) == EFalse) { |
|
96 User::Leave(KErrPathNotFound); |
|
97 } |
|
98 |
|
99 if (aMimeType.Length() == 0) { |
|
100 User::Leave(KErrBadName); |
|
101 } |
|
102 |
|
103 if (0>=mNewSize.iWidth || 0>=mNewSize.iHeight) { |
|
104 User::Leave(KErrCorrupt); |
|
105 } |
|
106 |
|
107 mInputBitmap = new(ELeave)CFbsBitmap(); |
|
108 DecodingOperationL(aFs, aFileName, aMimeType); |
|
109 SetActive(); |
|
110 } |
|
111 |
|
112 // ----------------------------------------------------------------------------- |
|
113 // ----------------------------------------------------------------------------- |
|
114 // |
|
115 EXPORT_C CTsGraphicFileScalingHandler* CTsGraphicFileScalingHandler::NewL(MImageReadyCallBack &aNotify, |
|
116 const CFbsBitmap &aImputFbsBitmap, |
|
117 const TSize &aNewSize, |
|
118 TKindOfScaling aKindOfScaling |
|
119 /* = CTsGraphicFileScalingHandler::EIgnoreAspectRatio*/) |
|
120 { |
|
121 CTsGraphicFileScalingHandler *self = CTsGraphicFileScalingHandler::NewLC(aNotify, |
|
122 aImputFbsBitmap, |
|
123 aNewSize, |
|
124 aKindOfScaling); |
|
125 CleanupStack::Pop(); |
|
126 return self; |
|
127 } |
|
128 |
|
129 // ----------------------------------------------------------------------------- |
|
130 // ----------------------------------------------------------------------------- |
|
131 // |
|
132 EXPORT_C CTsGraphicFileScalingHandler* CTsGraphicFileScalingHandler::NewLC(MImageReadyCallBack &aNotify, |
|
133 const CFbsBitmap &aImputFbsBitmap, |
|
134 const TSize &aNewSize, |
|
135 TKindOfScaling aKindOfScaling |
|
136 /* = CTsGraphicFileScalingHandler::EIgnoreAspectRatio*/) |
|
137 { |
|
138 CTsGraphicFileScalingHandler *self = new (ELeave) CTsGraphicFileScalingHandler(aNotify, |
|
139 aNewSize, |
|
140 aKindOfScaling); |
|
141 |
|
142 CleanupStack::PushL(self); |
|
143 self->ConstructL(aImputFbsBitmap); |
|
144 return self; |
|
145 } |
|
146 |
|
147 // ----------------------------------------------------------------------------- |
|
148 // ----------------------------------------------------------------------------- |
|
149 // |
|
150 void CTsGraphicFileScalingHandler::ConstructL(const CFbsBitmap &aImputFbsBitmap) |
|
151 { |
|
152 if (0>=mNewSize.iWidth || 0>=mNewSize.iHeight) { |
|
153 User::Leave(KErrCorrupt); |
|
154 } |
|
155 |
|
156 mInputBitmap = new(ELeave)CFbsBitmap(); |
|
157 User::LeaveIfError(mInputBitmap->Duplicate(aImputFbsBitmap.Handle())); |
|
158 |
|
159 ScalingOperationL(); |
|
160 SetActive(); |
|
161 } |
|
162 |
|
163 // ----------------------------------------------------------------------------- |
|
164 // ----------------------------------------------------------------------------- |
|
165 // |
|
166 void CTsGraphicFileScalingHandler::DoCancel() |
|
167 { |
|
168 switch (mCurrentOperation) { |
|
169 case EConvertBitmapFromFile: |
|
170 mImageDecoder->Cancel(); |
|
171 break; |
|
172 case EScale: |
|
173 mBitmapScaler->Cancel(); |
|
174 break; |
|
175 } |
|
176 mNotify.ImageReadyCallBack(KErrCancel, 0); |
|
177 } |
|
178 |
|
179 // ----------------------------------------------------------------------------- |
|
180 // ----------------------------------------------------------------------------- |
|
181 // |
|
182 void CTsGraphicFileScalingHandler::RunL() |
|
183 { |
|
184 User::LeaveIfError(iStatus.Int()); |
|
185 |
|
186 switch (mCurrentOperation) { |
|
187 case EConvertBitmapFromFile: { |
|
188 delete mImageDecoder; |
|
189 mImageDecoder = 0; |
|
190 |
|
191 ScalingOperationL(); |
|
192 SetActive(); |
|
193 break; |
|
194 } |
|
195 case EScale: { |
|
196 mCurrentOperation = ENone; |
|
197 |
|
198 delete mBitmapScaler; |
|
199 mBitmapScaler = 0; |
|
200 |
|
201 delete mInputBitmap; |
|
202 mInputBitmap = 0; |
|
203 |
|
204 if (mKindOfScaling == CTsGraphicFileScalingHandler::EKeepAspectRatioByExpanding) { |
|
205 User::LeaveIfError(mOutputBitmap->Resize(mNewSize)); |
|
206 } |
|
207 |
|
208 mNotify.ImageReadyCallBack(iStatus.Int(), mOutputBitmap); |
|
209 break; |
|
210 } |
|
211 } |
|
212 } |
|
213 |
|
214 // ----------------------------------------------------------------------------- |
|
215 // ----------------------------------------------------------------------------- |
|
216 // |
|
217 void CTsGraphicFileScalingHandler::DecodingOperationL(RFs &aFs, const TDesC &aFileName, const TDesC8& aMimeType) |
|
218 { |
|
219 // convert *.png to bitmap |
|
220 mImageDecoder = CImageDecoder::FileNewL(aFs, aFileName, aMimeType); |
|
221 const TFrameInfo frameInfo(mImageDecoder->FrameInfo(0)); |
|
222 mInputBitmap->Reset(); |
|
223 User::LeaveIfError(mInputBitmap->Create(frameInfo.iOverallSizeInPixels, |
|
224 frameInfo.iFrameDisplayMode)); |
|
225 |
|
226 mImageDecoder->Convert(&iStatus, *mInputBitmap, 0); |
|
227 mCurrentOperation = EConvertBitmapFromFile; |
|
228 } |
|
229 |
|
230 // ----------------------------------------------------------------------------- |
|
231 // ----------------------------------------------------------------------------- |
|
232 // |
|
233 void CTsGraphicFileScalingHandler::ScalingOperationL() |
|
234 { |
|
235 mBitmapScaler = CBitmapScaler::NewL(); |
|
236 mBitmapScaler->SetQualityAlgorithm(CBitmapScaler::EMaximumQuality); |
|
237 |
|
238 FixForDisplayModeNotSupportedByScalingOperation(); |
|
239 |
|
240 mOutputBitmap = new (ELeave)CFbsBitmap(); |
|
241 User::LeaveIfError(mOutputBitmap->Create(NewSizeToScalingOperation(), mInputBitmap->DisplayMode())); |
|
242 mBitmapScaler->Scale(&iStatus, *mInputBitmap, *mOutputBitmap, EFalse); |
|
243 mCurrentOperation = EScale; |
|
244 } |
|
245 |
|
246 // ----------------------------------------------------------------------------- |
|
247 // ----------------------------------------------------------------------------- |
|
248 // |
|
249 void CTsGraphicFileScalingHandler::FixForDisplayModeNotSupportedByScalingOperation() |
|
250 { |
|
251 if (EColor16MAP == mInputBitmap->DisplayMode()) { |
|
252 mInputBitmap->SetDisplayMode(EColor16MA); |
|
253 } |
|
254 } |
|
255 |
|
256 // ----------------------------------------------------------------------------- |
|
257 // ----------------------------------------------------------------------------- |
|
258 // |
|
259 TSize CTsGraphicFileScalingHandler::NewSizeToScalingOperation() |
|
260 { |
|
261 TSize originalSize = mInputBitmap->SizeInPixels(); |
|
262 float widthFactor = mNewSize.iWidth / (float)originalSize.iWidth; |
|
263 float heightFactor = mNewSize.iHeight / (float)originalSize.iHeight; |
|
264 |
|
265 TSize retSize(mNewSize); |
|
266 |
|
267 if (mKindOfScaling == CTsGraphicFileScalingHandler::EKeepAspectRatio) { |
|
268 retSize = (widthFactor < heightFactor) ? |
|
269 TSize(mNewSize.iWidth, widthFactor * originalSize.iHeight) : |
|
270 TSize(heightFactor * originalSize.iWidth, mNewSize.iHeight); |
|
271 } else if (mKindOfScaling == CTsGraphicFileScalingHandler::EKeepAspectRatioByExpanding) { |
|
272 retSize = (widthFactor < heightFactor) ? |
|
273 TSize(heightFactor * originalSize.iWidth, mNewSize.iHeight) : |
|
274 TSize(mNewSize.iWidth, widthFactor * originalSize.iHeight); |
|
275 } |
|
276 return retSize; |
|
277 } |
|
278 // ----------------------------------------------------------------------------- |
|
279 // ----------------------------------------------------------------------------- |
|
280 // |
|
281 TInt CTsGraphicFileScalingHandler::RunError(TInt aError) |
|
282 { |
|
283 mNotify.ImageReadyCallBack(aError, 0); |
|
284 return KErrNone; |
|
285 } |