|
1 /* |
|
2 * Copyright (c) 2006 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 the License "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 * Icon convert to convert icon for png to mbm format |
|
16 * |
|
17 * |
|
18 */ |
|
19 |
|
20 #include <fbs.h> |
|
21 #include <ImageConversion.h> |
|
22 #include <BitmapTransforms.h> |
|
23 #include "IconConverter.h" |
|
24 #include "WidgetUIOperationsWatcher.h" |
|
25 |
|
26 |
|
27 // CONSTANTS |
|
28 _LIT( KTempPath,"c:\\system\\temp\\" ); |
|
29 |
|
30 const TInt KIconSizeLarge = 88; |
|
31 const TInt KIconSizeMedium = 32; |
|
32 const TInt KIconSizeSmall = 24; |
|
33 |
|
34 |
|
35 using namespace SwiUI; |
|
36 |
|
37 // ============================================================================ |
|
38 // CIconConverter::NewL() |
|
39 // two-phase constructor |
|
40 // |
|
41 // @since 3.1 |
|
42 // @param aController - controller for callback to notify the completion |
|
43 // @param aFs - file session |
|
44 // @return pointer to CIconConverter |
|
45 // ============================================================================ |
|
46 // |
|
47 CIconConverter* CIconConverter::NewL( |
|
48 MConverterController* aController, |
|
49 RFs& aFs ) |
|
50 { |
|
51 CIconConverter* self = |
|
52 new(ELeave) CIconConverter( aController , aFs ); |
|
53 CleanupStack::PushL( self ); |
|
54 |
|
55 self->ConstructL(); |
|
56 |
|
57 CleanupStack::Pop( self ); |
|
58 return self; |
|
59 } |
|
60 |
|
61 // ============================================================================ |
|
62 // CIconConverter::CIconConverter() |
|
63 // C++ default constructor |
|
64 // |
|
65 // @since 3.1 |
|
66 // ============================================================================ |
|
67 CIconConverter::CIconConverter( |
|
68 MConverterController* aController, |
|
69 RFs& aFs ) : |
|
70 CActive( EPriorityStandard ), |
|
71 iController( aController ), |
|
72 iFs( aFs ) |
|
73 { |
|
74 RFbsSession::Connect(); |
|
75 CActiveScheduler::Add( this ); |
|
76 } |
|
77 |
|
78 // ============================================================================ |
|
79 // CIconConverter::ConstructL() |
|
80 // Symbian default constructor |
|
81 // |
|
82 // @since 3.1 |
|
83 // ============================================================================ |
|
84 void CIconConverter::ConstructL() |
|
85 { |
|
86 // create the destination bitmap |
|
87 iOriginalBitmap = new ( ELeave ) CFbsBitmap; |
|
88 iOriginalBitmapMask = new ( ELeave ) CFbsBitmap; |
|
89 |
|
90 iTempBitmap = new ( ELeave ) CFbsBitmap; |
|
91 iTempBitmapMask = new ( ELeave ) CFbsBitmap; |
|
92 iTempPath = KTempPath().AllocL(); |
|
93 iIconSizes = new CArrayFixFlat<TSize>( 3 ); |
|
94 iIconSizes->InsertL( 0, TSize( KIconSizeLarge, KIconSizeLarge ) ); |
|
95 iIconSizes->InsertL( 1, TSize( KIconSizeMedium, KIconSizeMedium ) ); |
|
96 iIconSizes->InsertL( 2, TSize( KIconSizeSmall, KIconSizeSmall ) ); |
|
97 } |
|
98 |
|
99 |
|
100 // ============================================================================ |
|
101 // CIconConverter::~CIconConverter() |
|
102 // destructor |
|
103 // |
|
104 // @since 3.1 |
|
105 // ============================================================================ |
|
106 CIconConverter::~CIconConverter() |
|
107 { |
|
108 Cancel(); |
|
109 |
|
110 // CImageDecoder must be deleted first otherwise a related thread might panic |
|
111 if ( iImageDecoder ) |
|
112 { |
|
113 delete iImageDecoder; |
|
114 } |
|
115 if ( iOriginalBitmap ) |
|
116 { |
|
117 delete iOriginalBitmap; |
|
118 } |
|
119 if ( iOriginalBitmapMask ) |
|
120 { |
|
121 delete iOriginalBitmapMask; |
|
122 } |
|
123 if ( iOutputFileName ) |
|
124 { |
|
125 delete iOutputFileName; |
|
126 } |
|
127 if ( iTempBitmap ) |
|
128 { |
|
129 delete iTempBitmap; |
|
130 } |
|
131 if ( iTempBitmapMask ) |
|
132 { |
|
133 delete iTempBitmapMask; |
|
134 } |
|
135 if ( iScaler ) |
|
136 { |
|
137 delete iScaler; |
|
138 } |
|
139 if ( iTempPath ) |
|
140 { |
|
141 delete iTempPath; |
|
142 } |
|
143 iIconFile.Close(); |
|
144 iIconPngFile.Close(); |
|
145 RFbsSession::Disconnect(); |
|
146 if ( iIconSizes ) |
|
147 { |
|
148 iIconSizes->Reset(); |
|
149 delete iIconSizes; |
|
150 } |
|
151 } |
|
152 |
|
153 |
|
154 // ============================================================================ |
|
155 // CIconConverter::StartToDecodeL |
|
156 // use image decoder to decode the image |
|
157 // |
|
158 // @since 3.1 |
|
159 // ============================================================================ |
|
160 void CIconConverter::StartToDecodeL( |
|
161 const TDesC& aInputFileName, |
|
162 const TDesC& aOutputFileName ) |
|
163 { |
|
164 iState = EConvertingFile; |
|
165 delete iImageDecoder; |
|
166 iImageDecoder = NULL; |
|
167 |
|
168 delete iOutputFileName; |
|
169 iOutputFileName = 0; |
|
170 |
|
171 iOutputFileName = aOutputFileName.AllocL(); |
|
172 |
|
173 // create the decoder |
|
174 iImageDecoder = CImageDecoder::FileNewL( iFs, aInputFileName ); |
|
175 |
|
176 // Extract information about the image, now we've read the header |
|
177 TFrameInfo info = iImageDecoder->FrameInfo( 0 ); |
|
178 |
|
179 iOriginalBitmap->Create( info.iOverallSizeInPixels, info.iFrameDisplayMode ); |
|
180 |
|
181 // If the PNG has a built in transparency, use it to build the mask |
|
182 if ( info.iFlags & TFrameInfo::ETransparencyPossible ) |
|
183 { |
|
184 // If we have a full alpha channel, use that |
|
185 if ( info.iFlags & TFrameInfo::EAlphaChannel ) |
|
186 { |
|
187 User::LeaveIfError( iOriginalBitmapMask->Create( |
|
188 info.iOverallSizeInPixels, |
|
189 EGray256 ) ); |
|
190 } |
|
191 else |
|
192 { |
|
193 User::LeaveIfError( iOriginalBitmapMask->Create( |
|
194 info.iOverallSizeInPixels, |
|
195 EGray2 ) ); |
|
196 } |
|
197 |
|
198 iImageDecoder->Convert( |
|
199 &iStatus, *iOriginalBitmap, *iOriginalBitmapMask ); |
|
200 } |
|
201 else |
|
202 { |
|
203 iImageDecoder->Convert( &iStatus, *iOriginalBitmap ); |
|
204 } |
|
205 |
|
206 // start conversion to bitmap |
|
207 SetActive(); |
|
208 } |
|
209 |
|
210 // ============================================================================ |
|
211 // CIconConverter::RunL() |
|
212 // Handle various stages of icon conversion |
|
213 // |
|
214 // @since 3.1 |
|
215 // ============================================================================ |
|
216 void CIconConverter::RunL() |
|
217 { |
|
218 // If there is an error in the previous stage, then leave. Otherwise, |
|
219 // call the handle function |
|
220 User::LeaveIfError( iStatus.Int() ); |
|
221 |
|
222 switch ( iState ) |
|
223 { |
|
224 case EConvertingFile: |
|
225 DoProcessMaskL(); |
|
226 break; |
|
227 |
|
228 case EScalingIcon: |
|
229 DoMaskScalingL(); |
|
230 break; |
|
231 |
|
232 case EScalingMask: |
|
233 case EFinalize: |
|
234 DoIconStoreL(); |
|
235 break; |
|
236 |
|
237 default: |
|
238 User::Leave( KErrNotSupported ); |
|
239 break; |
|
240 }; |
|
241 |
|
242 } |
|
243 |
|
244 // ============================================================================ |
|
245 // CIconConverter::RunError() |
|
246 // Notify client with error |
|
247 // |
|
248 // @since 3.1 |
|
249 // ============================================================================ |
|
250 TInt CIconConverter::RunError( TInt aError ) |
|
251 { |
|
252 // If any error occurred, then complete the client with the error. |
|
253 if ( iClientStatus ) |
|
254 { |
|
255 User::RequestComplete( iClientStatus, aError ); |
|
256 } |
|
257 |
|
258 // There is nothing more to do if NotifyCompletionL leaves. |
|
259 TRAP_IGNORE( iController->NotifyCompletionL( aError ) ); |
|
260 |
|
261 return KErrNone; |
|
262 } |
|
263 |
|
264 // ============================================================================ |
|
265 // CIconConverter::DoCancel() |
|
266 // cancel icon conversion |
|
267 // |
|
268 // @since 3.1 |
|
269 // ============================================================================ |
|
270 void CIconConverter::DoCancel() |
|
271 { |
|
272 switch (iState) |
|
273 { |
|
274 case EConvertingFile: |
|
275 if ( iImageDecoder ) |
|
276 { |
|
277 iImageDecoder->Cancel(); |
|
278 } |
|
279 |
|
280 break; |
|
281 |
|
282 case EScalingIcon: |
|
283 case EScalingMask: |
|
284 if ( iScaler ) |
|
285 { |
|
286 iScaler->Cancel(); |
|
287 } |
|
288 break; |
|
289 |
|
290 }; |
|
291 |
|
292 if ( iClientStatus ) |
|
293 { |
|
294 User::RequestComplete( iClientStatus, KErrCancel ); |
|
295 } |
|
296 |
|
297 // no need to call NotifyCompletionL() because cancel can only be |
|
298 // caused by the client |
|
299 } |
|
300 |
|
301 // ============================================================================ |
|
302 // CIconConverter::DoProcessMaskL() |
|
303 // process the bitmap mask |
|
304 // |
|
305 // @since 3.1 |
|
306 // ============================================================================ |
|
307 void CIconConverter::DoProcessMaskL() |
|
308 { |
|
309 // we use white to mean transparent at this stage, simply for efficiency |
|
310 // since all the canvases we will copy in to begin as white |
|
311 |
|
312 if ( iOriginalBitmapMask->Handle() == 0 ) |
|
313 { |
|
314 // Create a mask that shows the whole bitmap as an icon |
|
315 // (all black) |
|
316 User::LeaveIfError( iOriginalBitmapMask->Create( |
|
317 iOriginalBitmap->SizeInPixels(), EGray2 ) ); |
|
318 CFbsBitmapDevice* device = |
|
319 CFbsBitmapDevice::NewL( iOriginalBitmapMask ); |
|
320 CleanupStack::PushL( device ); |
|
321 |
|
322 CFbsBitGc* gc; |
|
323 User::LeaveIfError( device->CreateContext( gc ) ); |
|
324 gc->SetBrushStyle( CGraphicsContext::ESolidBrush ); |
|
325 gc->SetDrawMode( CGraphicsContext::EDrawModePEN ); |
|
326 gc->SetBrushColor( KRgbBlack ); |
|
327 // Create a big black image |
|
328 gc->Clear(); |
|
329 delete gc; |
|
330 CleanupStack::PopAndDestroy( device ); |
|
331 } |
|
332 else |
|
333 { |
|
334 // Invert the mask obtained from the PNG |
|
335 CFbsBitmapDevice* device = |
|
336 CFbsBitmapDevice::NewL( iOriginalBitmapMask ); |
|
337 CleanupStack::PushL(device); |
|
338 CFbsBitGc* gc; |
|
339 User::LeaveIfError( device->CreateContext( gc ) ); |
|
340 gc->SetDrawMode( CGraphicsContext::EDrawModeNOTSCREEN ); |
|
341 gc->Clear(); |
|
342 delete gc; |
|
343 CleanupStack::PopAndDestroy( device ); |
|
344 } |
|
345 |
|
346 // Scale the icon to the sizes required |
|
347 iCurrentSizeIndex = 0; |
|
348 DoIconScalingL(); |
|
349 } |
|
350 |
|
351 // ============================================================================ |
|
352 // CIconConverter::DoIconScalingL() |
|
353 // Scale the bitmap |
|
354 // |
|
355 // @since 3.1 |
|
356 // ============================================================================ |
|
357 void CIconConverter::DoIconScalingL() |
|
358 { |
|
359 // free any current icons to prevent memory leaks |
|
360 iTempBitmap->Reset(); |
|
361 // current target size |
|
362 TSize size = iIconSizes->At( iCurrentSizeIndex ); |
|
363 |
|
364 iState = EScalingIcon; |
|
365 // Create a canvas to hold the scaled icon, of the same depth |
|
366 User::LeaveIfError( |
|
367 iTempBitmap->Create( size, iOriginalBitmap->DisplayMode() ) ); |
|
368 DoScalingL( *iOriginalBitmap, *iTempBitmap ); |
|
369 } |
|
370 |
|
371 // ============================================================================ |
|
372 // CIconConverter::DoMaskScalingL() |
|
373 // Scale the bitmap mask |
|
374 // |
|
375 // @since 3.1 |
|
376 // ============================================================================ |
|
377 void CIconConverter::DoMaskScalingL() |
|
378 { |
|
379 // Reset the mask to prevent memory leaks |
|
380 iTempBitmapMask->Reset(); |
|
381 // current target size |
|
382 TSize size = iIconSizes->At( iCurrentSizeIndex ); |
|
383 |
|
384 iState = EScalingMask; |
|
385 // Create a canvas to hold the scaled icon, of 8 bit colour depth |
|
386 User::LeaveIfError( iTempBitmapMask->Create( size, EGray256 ) ); |
|
387 DoScalingL( *iOriginalBitmapMask, *iTempBitmapMask ); |
|
388 } |
|
389 |
|
390 // ============================================================================ |
|
391 // CIconConverter::DoScalingL() |
|
392 // Scale |
|
393 // |
|
394 // @since 3.1 |
|
395 // ============================================================================ |
|
396 void CIconConverter::DoScalingL( |
|
397 CFbsBitmap& aBitmapSource, CFbsBitmap& aBitmapTarget ) |
|
398 { |
|
399 ScalerL().Scale( &iStatus, aBitmapSource, aBitmapTarget, ETrue ); |
|
400 SetActive(); |
|
401 } |
|
402 |
|
403 // ============================================================================ |
|
404 // CIconConverter::ScalerL() |
|
405 // Create bitmap scalar |
|
406 // |
|
407 // @since 3.1 |
|
408 // ============================================================================ |
|
409 CBitmapScaler& CIconConverter::ScalerL() |
|
410 { |
|
411 if ( iScaler == NULL ) |
|
412 { |
|
413 iScaler = CBitmapScaler::NewL(); |
|
414 // always use highest quality scaling |
|
415 User::LeaveIfError( iScaler->SetQualityAlgorithm( CBitmapScaler::EMaximumQuality ) ); |
|
416 } |
|
417 return *iScaler; |
|
418 } |
|
419 |
|
420 // ============================================================================ |
|
421 // CIconConverter::DoIconStoreL() |
|
422 // Store icon and mask files |
|
423 // |
|
424 // @since 3.1 |
|
425 // ============================================================================ |
|
426 void CIconConverter::DoIconStoreL() |
|
427 { |
|
428 // Store the icon and its mask in temporary files until we are ready |
|
429 // to create the final icon |
|
430 |
|
431 // Icon is stored at index n, mask at index n+1 |
|
432 TInt iconIndex = iCurrentSizeIndex * 2; |
|
433 TFileName iconFile = *iTempPath; |
|
434 GetTempIconName( iconIndex++, iconFile ); |
|
435 |
|
436 TFileName maskFile = *iTempPath; |
|
437 GetTempIconName( iconIndex, maskFile ); |
|
438 |
|
439 // invert the masks before saving |
|
440 |
|
441 CFbsBitmapDevice* device = CFbsBitmapDevice::NewL( iTempBitmapMask ); |
|
442 CleanupStack::PushL( device ); |
|
443 |
|
444 CFbsBitGc* gc; |
|
445 User::LeaveIfError( device->CreateContext( gc ) ); |
|
446 gc->SetDrawMode( CGraphicsContext::EDrawModeNOTSCREEN ); |
|
447 gc->Clear(); |
|
448 |
|
449 delete gc; |
|
450 CleanupStack::PopAndDestroy( device ); |
|
451 |
|
452 // save the bitmaps |
|
453 User::LeaveIfError( iTempBitmap->Save( iconFile ) ); |
|
454 User::LeaveIfError( iTempBitmapMask->Save( maskFile ) ); |
|
455 |
|
456 if ( ++iCurrentSizeIndex < iIconSizes->Count() ) |
|
457 { |
|
458 // do the next icon size |
|
459 DoIconScalingL(); |
|
460 } |
|
461 else |
|
462 { |
|
463 DoCreateFinalIconL(); |
|
464 } |
|
465 |
|
466 } |
|
467 |
|
468 // ============================================================================ |
|
469 // CIconConverter::DoCreateFinalIconL() |
|
470 // Create the final icon |
|
471 // |
|
472 // @since 3.1 |
|
473 // ============================================================================ |
|
474 void CIconConverter::DoCreateFinalIconL() |
|
475 { |
|
476 TInt i, elements = 0; |
|
477 // one icon, one mask per size |
|
478 TInt bitmapCount = iIconSizes->Count() * 2; |
|
479 |
|
480 TFileName** filenames = new ( ELeave ) TFileName*[bitmapCount]; |
|
481 CleanupStack::PushL( filenames ); |
|
482 TInt32* uniqueIds = new ( ELeave ) TInt32[bitmapCount]; |
|
483 CleanupStack::PushL( uniqueIds ); |
|
484 |
|
485 TInt err = KErrNone; |
|
486 |
|
487 for ( i = 0; i < bitmapCount; ++i ) |
|
488 { |
|
489 filenames[i] = NULL; |
|
490 filenames[i] = new TFileName( *iTempPath ); |
|
491 elements = i; |
|
492 if ( filenames[i] == NULL ) |
|
493 { |
|
494 // we need to cleanup this structure |
|
495 err = KErrNoMemory; |
|
496 goto cleanup; |
|
497 } |
|
498 GetTempIconName( i, *filenames[i] ); |
|
499 uniqueIds[i] = 0; |
|
500 } |
|
501 |
|
502 TRAP( err, CFbsBitmap::StoreL( |
|
503 *iOutputFileName, bitmapCount, ( const TDesC** )filenames, uniqueIds ) ); |
|
504 |
|
505 cleanup: |
|
506 for ( i = 0; i <= elements; ++i ) |
|
507 { |
|
508 if ( filenames[i] == NULL ) |
|
509 { |
|
510 // if we failed to allocate a filename, then we would not have continued |
|
511 break; |
|
512 } |
|
513 else |
|
514 { |
|
515 delete filenames[i]; |
|
516 } |
|
517 } |
|
518 |
|
519 CleanupStack::PopAndDestroy( 2, filenames ); |
|
520 |
|
521 // There is no recovery on a leave and we don't want to trigger |
|
522 // RunError here since that will also call NotifyCompletionL. |
|
523 TRAP_IGNORE( iController->NotifyCompletionL( err ) ); |
|
524 } |
|
525 |
|
526 // ============================================================================ |
|
527 // CIconConverter::GetTempIconName() |
|
528 // Get temporary icon name |
|
529 // |
|
530 // @since 3.1 |
|
531 // ============================================================================ |
|
532 void CIconConverter::GetTempIconName( TInt aIndex, TFileName& aIconName ) |
|
533 { |
|
534 _LIT( KIcon, "ICON" ); |
|
535 _LIT( KBmp, ".MBM" ); |
|
536 aIconName.Append( KIcon ); |
|
537 aIconName.AppendNum( static_cast<TInt64>( aIndex ) ); |
|
538 aIconName.Append( KBmp ); |
|
539 } |
|
540 |
|
541 |