|
1 /* |
|
2 * Copyright (c) 2005-2007 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 * Provides methods for CPbk2ImageManager implementation classes |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 |
|
22 #include "CPbk2ImageWriterBase.h" |
|
23 |
|
24 // From Phonebook2 |
|
25 #include "CPbk2ImageReader.h" |
|
26 #include "MPbk2ImageOperationObservers.h" |
|
27 #include "CPbk2ImageData.h" |
|
28 |
|
29 // From System |
|
30 #include <imageconversion.h> |
|
31 #include <bitmaptransforms.h> |
|
32 |
|
33 /// Unnamed namespace for local defintions |
|
34 namespace { |
|
35 |
|
36 // LOCAL CONSTANTS |
|
37 |
|
38 /// JPEG file mime type |
|
39 _LIT8(KJpegMimeType, "image/jpeg"); |
|
40 |
|
41 // States for CPbk2ImageWriterBase |
|
42 enum TWriterState |
|
43 { |
|
44 EStateInitialize = 0, |
|
45 EStateCheckFormatAndSize, |
|
46 EStateScaleBitmap, |
|
47 EStateCreateImage, |
|
48 EStateConvertToJpeg, |
|
49 EStateStoreImage, |
|
50 EStateComplete, |
|
51 EStateEnd |
|
52 }; |
|
53 |
|
54 #ifdef _DEBUG |
|
55 enum TPanicCode |
|
56 { |
|
57 EPanicPreCond_ConstructL, |
|
58 EPanicPreCond_CheckFormatAndSizeL, |
|
59 EPanicPreCond_ScaleBitmapL, |
|
60 EPanicPreCond_CreateImageL, |
|
61 EPanicPreCond_ConvertToJpegL, |
|
62 EPanicPreCond_StoreImageL, |
|
63 EPanicPreCond_DoCancel |
|
64 }; |
|
65 #endif // _DEBUG |
|
66 |
|
67 |
|
68 // ==================== LOCAL FUNCTIONS ==================== |
|
69 |
|
70 /** |
|
71 * Comparison operator for TSize objects. Compares width and height members. |
|
72 * |
|
73 * @param aLhs Reference to left hand side TSize |
|
74 * @param aRhs Reference to right hand side TSize |
|
75 * @return ETrue if lhs's width and height are less or equal to rhs's |
|
76 * width and height. Otherwise returns EFalse. |
|
77 */ |
|
78 inline TBool operator<=(const TSize& aLhs, const TSize& aRhs) |
|
79 { |
|
80 return (aLhs.iWidth<=aRhs.iWidth && aLhs.iHeight<=aRhs.iHeight); |
|
81 } |
|
82 |
|
83 #ifdef _DEBUG |
|
84 void Panic(TPanicCode aReason) |
|
85 { |
|
86 _LIT(KPanicText, "CPbk2ImageWriterBase"); |
|
87 User::Panic(KPanicText,aReason); |
|
88 } |
|
89 #endif // _DEBUG |
|
90 } // namespace |
|
91 |
|
92 |
|
93 // ================= MEMBER FUNCTIONS ======================= |
|
94 |
|
95 // -------------------------------------------------------------------------- |
|
96 // CPbk2ImageWriterBase::CPbk2ImageWriterBase |
|
97 // -------------------------------------------------------------------------- |
|
98 // |
|
99 CPbk2ImageWriterBase::CPbk2ImageWriterBase |
|
100 (MPbk2ImageSetObserver& aObserver) : |
|
101 CActive(CActive::EPriorityStandard), |
|
102 iObserver(aObserver) |
|
103 { |
|
104 CActiveScheduler::Add(this); |
|
105 } |
|
106 |
|
107 // -------------------------------------------------------------------------- |
|
108 // CPbk2ImageWriterBase::~CPbk2ImageWriterBase |
|
109 // -------------------------------------------------------------------------- |
|
110 // |
|
111 CPbk2ImageWriterBase::~CPbk2ImageWriterBase() |
|
112 { |
|
113 Cancel(); |
|
114 delete iImageEncoder; |
|
115 delete iBitmapScaler; |
|
116 delete iBitmap; |
|
117 delete iImageData; |
|
118 delete iImageReader; |
|
119 } |
|
120 |
|
121 // -------------------------------------------------------------------------- |
|
122 // CPbk2ImageWriterBase::ConstructL |
|
123 // -------------------------------------------------------------------------- |
|
124 // |
|
125 void CPbk2ImageWriterBase::ConstructL() |
|
126 { |
|
127 __ASSERT_DEBUG(!iImageReader, Panic(EPanicPreCond_ConstructL)); |
|
128 iImageReader = CPbk2ImageReader::NewL(*this); |
|
129 } |
|
130 |
|
131 // -------------------------------------------------------------------------- |
|
132 // CPbk2ImageWriterBase::StartWriteL |
|
133 // -------------------------------------------------------------------------- |
|
134 // |
|
135 void CPbk2ImageWriterBase::StartWriteL(const CFbsBitmap& aBitmap) |
|
136 { |
|
137 CancelWrite(); |
|
138 iBitmap = new(ELeave) CFbsBitmap; |
|
139 User::LeaveIfError(iBitmap->Duplicate(aBitmap.Handle())); |
|
140 ScaleBitmapL(); |
|
141 } |
|
142 |
|
143 // -------------------------------------------------------------------------- |
|
144 // CPbk2ImageWriterBase::StartWriteL |
|
145 // -------------------------------------------------------------------------- |
|
146 // |
|
147 void CPbk2ImageWriterBase::StartWriteL(const TDesC8& aBuffer) |
|
148 { |
|
149 CancelWrite(); |
|
150 |
|
151 if (iImageData) |
|
152 { |
|
153 delete iImageData; |
|
154 iImageData = NULL; |
|
155 } |
|
156 iImageData = CPbk2ImageBufferData::NewL(aBuffer); |
|
157 |
|
158 iImageReader->RecognizeFormatFromBufferL(aBuffer); |
|
159 |
|
160 TPbk2ImageManagerParams readerParams; |
|
161 SetupReaderParams(readerParams); |
|
162 iImageReader->ReadFromBufferL(aBuffer,&readerParams); |
|
163 } |
|
164 |
|
165 // -------------------------------------------------------------------------- |
|
166 // CPbk2ImageWriterBase::StartWriteL |
|
167 // -------------------------------------------------------------------------- |
|
168 // |
|
169 void CPbk2ImageWriterBase::StartWriteL(const TDesC& aFileName) |
|
170 { |
|
171 CancelWrite(); |
|
172 |
|
173 if (iImageData) |
|
174 { |
|
175 delete iImageData; |
|
176 iImageData = NULL; |
|
177 } |
|
178 iImageData = CPbk2ImageFileData::NewL(aFileName); |
|
179 |
|
180 iImageReader->RecognizeFormatFromFileL(aFileName); |
|
181 |
|
182 TPbk2ImageManagerParams readerParams; |
|
183 SetupReaderParams(readerParams); |
|
184 iImageReader->ReadFromFileL(aFileName, &readerParams); |
|
185 } |
|
186 |
|
187 // -------------------------------------------------------------------------- |
|
188 // CPbk2ImageWriterBase::CancelWrite |
|
189 // -------------------------------------------------------------------------- |
|
190 // |
|
191 void CPbk2ImageWriterBase::CancelWrite() |
|
192 { |
|
193 Cancel(); |
|
194 } |
|
195 |
|
196 // -------------------------------------------------------------------------- |
|
197 // CPbk2ImageWriterBase::ImageReadComplete |
|
198 // -------------------------------------------------------------------------- |
|
199 // |
|
200 void CPbk2ImageWriterBase::ImageReadComplete |
|
201 (CPbk2ImageReader& /*aReader*/, CFbsBitmap* aBitmap) |
|
202 { |
|
203 delete iBitmap; |
|
204 iBitmap = aBitmap; |
|
205 NextState(); |
|
206 } |
|
207 |
|
208 // -------------------------------------------------------------------------- |
|
209 // CPbk2ImageWriterBase::ImageReadFailed |
|
210 // -------------------------------------------------------------------------- |
|
211 // |
|
212 void CPbk2ImageWriterBase::ImageReadFailed |
|
213 (CPbk2ImageReader& /*aReader*/, TInt aError) |
|
214 { |
|
215 CancelWrite(); |
|
216 iObserver.Pbk2ImageSetFailed(*this,aError); |
|
217 } |
|
218 |
|
219 // -------------------------------------------------------------------------- |
|
220 // CPbk2ImageWriterBase::ImageOpenComplete |
|
221 // -------------------------------------------------------------------------- |
|
222 // |
|
223 void CPbk2ImageWriterBase::ImageOpenComplete(CPbk2ImageReader& /*aReader*/) |
|
224 { |
|
225 NextState(); |
|
226 } |
|
227 |
|
228 // -------------------------------------------------------------------------- |
|
229 // CPbk2ImageWriterBase::NextState |
|
230 // -------------------------------------------------------------------------- |
|
231 // |
|
232 void CPbk2ImageWriterBase::NextState() |
|
233 { |
|
234 ++iState; |
|
235 TRAPD(err, ExecuteStateL()); |
|
236 if (err) |
|
237 { |
|
238 CancelWrite(); |
|
239 iObserver.Pbk2ImageSetFailed(*this,err); |
|
240 } |
|
241 } |
|
242 |
|
243 // -------------------------------------------------------------------------- |
|
244 // CPbk2ImageWriterBase::ExecuteStateL |
|
245 // -------------------------------------------------------------------------- |
|
246 // |
|
247 void CPbk2ImageWriterBase::ExecuteStateL() |
|
248 { |
|
249 switch (iState) |
|
250 { |
|
251 case EStateCheckFormatAndSize: |
|
252 { |
|
253 CheckFormatAndSizeL(); |
|
254 break; |
|
255 } |
|
256 case EStateScaleBitmap: |
|
257 { |
|
258 ScaleBitmapL(); |
|
259 break; |
|
260 } |
|
261 case EStateCreateImage: |
|
262 { |
|
263 CreateImageL(); |
|
264 break; |
|
265 } |
|
266 case EStateConvertToJpeg: |
|
267 { |
|
268 ConvertToJpegL(); |
|
269 break; |
|
270 } |
|
271 case EStateStoreImage: |
|
272 { |
|
273 StoreImageL(); |
|
274 break; |
|
275 } |
|
276 case EStateComplete: |
|
277 { |
|
278 Complete(); |
|
279 break; |
|
280 } |
|
281 default: |
|
282 { |
|
283 // Media server objects might sometimes complete although they have |
|
284 // been canceled. Catch those cases here. |
|
285 CancelWrite(); |
|
286 break; |
|
287 } |
|
288 } |
|
289 } |
|
290 |
|
291 // -------------------------------------------------------------------------- |
|
292 // CPbk2ImageWriterBase::CheckFormatAndSizeL |
|
293 // -------------------------------------------------------------------------- |
|
294 // |
|
295 void CPbk2ImageWriterBase::CheckFormatAndSizeL() |
|
296 { |
|
297 __ASSERT_DEBUG(iImageReader, Panic(EPanicPreCond_CheckFormatAndSizeL)); |
|
298 iState = EStateCheckFormatAndSize; |
|
299 TFrameInfo frameInfo; |
|
300 iImageReader->FrameInfo(0,frameInfo); |
|
301 if (frameInfo.iOverallSizeInPixels <= ImageSize() && |
|
302 iImageReader->MimeString() == KJpegMimeType) |
|
303 { |
|
304 // Image resolution is <= personal image size and it is in JPEG |
|
305 // format -> store the image directly skipping scaling and |
|
306 // conversion. |
|
307 iImageReader->Cancel(); |
|
308 StoreImageL(); |
|
309 } |
|
310 } |
|
311 |
|
312 // -------------------------------------------------------------------------- |
|
313 // CPbk2ImageWriterBase::ScaleBitmapL |
|
314 // -------------------------------------------------------------------------- |
|
315 // |
|
316 void CPbk2ImageWriterBase::ScaleBitmapL() |
|
317 { |
|
318 __ASSERT_DEBUG(iBitmap, Panic(EPanicPreCond_ScaleBitmapL)); |
|
319 iState = EStateScaleBitmap; |
|
320 if (iBitmap->SizeInPixels() <= ImageSize()) |
|
321 { |
|
322 // Bitmap size is <= personal image size -> skip scaling |
|
323 CreateImageL(); |
|
324 } |
|
325 else |
|
326 { |
|
327 if (!iBitmapScaler) |
|
328 { |
|
329 iBitmapScaler = CBitmapScaler::NewL(); |
|
330 } |
|
331 iBitmapScaler->Scale(&iStatus, *iBitmap, ImageSize()); |
|
332 SetActive(); |
|
333 } |
|
334 } |
|
335 |
|
336 // -------------------------------------------------------------------------- |
|
337 // CPbk2ImageWriterBase::CreateImageL |
|
338 // -------------------------------------------------------------------------- |
|
339 // |
|
340 void CPbk2ImageWriterBase::CreateImageL() |
|
341 { |
|
342 __ASSERT_DEBUG( |
|
343 iBitmap && iBitmap->SizeInPixels()<=ImageSize() && !iImageEncoder, |
|
344 Panic(EPanicPreCond_CreateImageL)); |
|
345 iState = EStateCreateImage; |
|
346 iImageEncoder = CreateImageWriterL(); |
|
347 TRequestStatus* status = &iStatus; |
|
348 User::RequestComplete(status, KErrNone); |
|
349 SetActive(); |
|
350 } |
|
351 |
|
352 // -------------------------------------------------------------------------- |
|
353 // CPbk2ImageWriterBase::ConvertToJpegL |
|
354 // -------------------------------------------------------------------------- |
|
355 // |
|
356 void CPbk2ImageWriterBase::ConvertToJpegL() |
|
357 { |
|
358 __ASSERT_DEBUG( |
|
359 iBitmap && iBitmap->SizeInPixels()<=ImageSize() && iImageEncoder, |
|
360 Panic(EPanicPreCond_ConvertToJpegL)); |
|
361 |
|
362 iImageEncoder->Convert(&iStatus, *iBitmap); |
|
363 SetActive(); |
|
364 } |
|
365 |
|
366 // -------------------------------------------------------------------------- |
|
367 // CPbk2ImageWriterBase::StoreImageL |
|
368 // -------------------------------------------------------------------------- |
|
369 // |
|
370 void CPbk2ImageWriterBase::StoreImageL() |
|
371 { |
|
372 iState = EStateStoreImage; |
|
373 if (iImageEncoder) |
|
374 { |
|
375 // Store converted image |
|
376 StoreImageL(*iImageEncoder); |
|
377 } |
|
378 else |
|
379 { |
|
380 // Store input image directly |
|
381 __ASSERT_DEBUG(iImageData, Panic(EPanicPreCond_StoreImageL)); |
|
382 StoreImageL(*iImageData); |
|
383 } |
|
384 // this causes moving to next state asynchronously |
|
385 TRequestStatus* status = &iStatus; |
|
386 User::RequestComplete(status, KErrNone); |
|
387 SetActive(); |
|
388 } |
|
389 |
|
390 // -------------------------------------------------------------------------- |
|
391 // CPbk2ImageWriterBase::Complete |
|
392 // -------------------------------------------------------------------------- |
|
393 // |
|
394 void CPbk2ImageWriterBase::Complete() |
|
395 { |
|
396 // End state machine |
|
397 iState = EStateEnd; |
|
398 // Notify observer |
|
399 iObserver.Pbk2ImageSetComplete(*this); |
|
400 } |
|
401 |
|
402 // -------------------------------------------------------------------------- |
|
403 // CPbk2ImageWriterBase::SetupReaderParams |
|
404 // -------------------------------------------------------------------------- |
|
405 // |
|
406 void CPbk2ImageWriterBase::SetupReaderParams(TPbk2ImageManagerParams& aParams) |
|
407 { |
|
408 aParams.iSize = ImageSize(); |
|
409 aParams.iFlags = |
|
410 TPbk2ImageManagerParams::EScaleImage | |
|
411 TPbk2ImageManagerParams::EUseFastScaling; |
|
412 } |
|
413 |
|
414 // -------------------------------------------------------------------------- |
|
415 // CPbk2ImageWriterBase::RunL |
|
416 // -------------------------------------------------------------------------- |
|
417 // |
|
418 void CPbk2ImageWriterBase::RunL() |
|
419 { |
|
420 TInt status = iStatus.Int(); |
|
421 if (status == KErrNone) |
|
422 { |
|
423 NextState(); |
|
424 } |
|
425 else |
|
426 { |
|
427 iObserver.Pbk2ImageSetFailed(*this, status); |
|
428 } |
|
429 } |
|
430 |
|
431 // -------------------------------------------------------------------------- |
|
432 // CPbk2ImageWriterBase::RunError |
|
433 // -------------------------------------------------------------------------- |
|
434 // |
|
435 TInt CPbk2ImageWriterBase::RunError(TInt aError) |
|
436 { |
|
437 iObserver.Pbk2ImageSetFailed(*this, aError); |
|
438 return KErrNone; |
|
439 } |
|
440 |
|
441 // -------------------------------------------------------------------------- |
|
442 // CPbk2ImageWriterBase::DoCancel |
|
443 // -------------------------------------------------------------------------- |
|
444 // |
|
445 void CPbk2ImageWriterBase::DoCancel() |
|
446 { |
|
447 __ASSERT_DEBUG(iImageReader, Panic(EPanicPreCond_DoCancel)); |
|
448 // Cancel own operations |
|
449 iState = EStateInitialize; |
|
450 if (iImageEncoder) |
|
451 { |
|
452 iImageEncoder->Cancel(); |
|
453 delete iImageEncoder; |
|
454 iImageEncoder = NULL; |
|
455 } |
|
456 if (iBitmapScaler) |
|
457 { |
|
458 iBitmapScaler->Cancel(); |
|
459 } |
|
460 |
|
461 iImageReader->Cancel(); |
|
462 delete iImageData; |
|
463 iImageData = NULL; |
|
464 delete iBitmap; |
|
465 iBitmap = NULL; |
|
466 } |
|
467 |
|
468 // End of File |