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