|
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 } |
|
34 |
|
35 // ----------------------------------------------------------------------------- |
|
36 // ----------------------------------------------------------------------------- |
|
37 // |
|
38 EXPORT_C CTsGraphicFileScalingHandler::~CTsGraphicFileScalingHandler() |
|
39 { |
|
40 Cancel(); |
|
41 delete mBitmapFromFile; |
|
42 delete mBitmapOutput; |
|
43 delete mImageDecoder; |
|
44 delete mBitmapScaler; |
|
45 } |
|
46 |
|
47 // ----------------------------------------------------------------------------- |
|
48 // ----------------------------------------------------------------------------- |
|
49 // |
|
50 EXPORT_C CTsGraphicFileScalingHandler* CTsGraphicFileScalingHandler::NewL(MImageReadyCallBack &aNotify, |
|
51 RFs &aFs, |
|
52 const TDesC &aFileName, |
|
53 const TDesC8& aMimeType, |
|
54 const TSize &aNewSize, |
|
55 TKindOfScaling aKindOfScaling |
|
56 /* = CTsGraphicFileScalingHandler::EIgnoreAspectRatio*/) |
|
57 { |
|
58 CTsGraphicFileScalingHandler *self = CTsGraphicFileScalingHandler::NewLC(aNotify, |
|
59 aFs, |
|
60 aFileName, |
|
61 aMimeType, |
|
62 aNewSize, |
|
63 aKindOfScaling); |
|
64 CleanupStack::Pop(); |
|
65 return self; |
|
66 } |
|
67 |
|
68 // ----------------------------------------------------------------------------- |
|
69 // ----------------------------------------------------------------------------- |
|
70 // |
|
71 EXPORT_C CTsGraphicFileScalingHandler* CTsGraphicFileScalingHandler::NewLC(MImageReadyCallBack &aNotify, |
|
72 RFs &aFs, |
|
73 const TDesC &aFileName, |
|
74 const TDesC8& aMimeType, |
|
75 const TSize &aNewSize, |
|
76 TKindOfScaling aKindOfScaling |
|
77 /* = CTsGraphicFileScalingHandler::EIgnoreAspectRatio*/) |
|
78 { |
|
79 CTsGraphicFileScalingHandler *self = new (ELeave) CTsGraphicFileScalingHandler(aNotify, |
|
80 aNewSize, |
|
81 aKindOfScaling); |
|
82 |
|
83 CleanupStack::PushL(self); |
|
84 self->ConstructL(aFs, aFileName, aMimeType); |
|
85 return self; |
|
86 } |
|
87 |
|
88 // ----------------------------------------------------------------------------- |
|
89 // ----------------------------------------------------------------------------- |
|
90 // |
|
91 void CTsGraphicFileScalingHandler::ConstructL(RFs &aFs, const TDesC &aFileName, const TDesC8& aMimeType) |
|
92 { |
|
93 if (aFileName.Length() == 0 |
|
94 || aFs.IsValidName(aFileName) == EFalse) { |
|
95 User::Leave(KErrPathNotFound); |
|
96 } |
|
97 |
|
98 if (aMimeType.Length() == 0) { |
|
99 User::Leave(KErrBadName); |
|
100 } |
|
101 |
|
102 if(0>=mNewSize.iWidth || 0>=mNewSize.iHeight) { |
|
103 User::Leave(KErrCorrupt); |
|
104 } |
|
105 |
|
106 CActiveScheduler::Add(this); |
|
107 |
|
108 mBitmapScaler = CBitmapScaler::NewL(); |
|
109 mBitmapScaler->SetQualityAlgorithm(CBitmapScaler::EMaximumQuality); |
|
110 |
|
111 // convert *.png to bitmap |
|
112 mImageDecoder = CImageDecoder::FileNewL(aFs, aFileName, aMimeType); |
|
113 mBitmapFromFile = new(ELeave)CFbsBitmap(); |
|
114 const TFrameInfo frameInfo(mImageDecoder->FrameInfo(0)); |
|
115 User::LeaveIfError(mBitmapFromFile->Create(frameInfo.iOverallSizeInPixels, |
|
116 frameInfo.iFrameDisplayMode)); |
|
117 |
|
118 mImageDecoder->Convert(&iStatus, *mBitmapFromFile, 0); |
|
119 mCurrentOperation = EConvertBitmapFromFile; |
|
120 SetActive(); |
|
121 } |
|
122 |
|
123 // ----------------------------------------------------------------------------- |
|
124 // ----------------------------------------------------------------------------- |
|
125 // |
|
126 void CTsGraphicFileScalingHandler::DoCancel() |
|
127 { |
|
128 switch (mCurrentOperation) { |
|
129 case EConvertBitmapFromFile: |
|
130 mImageDecoder->Cancel(); |
|
131 break; |
|
132 case EScale: |
|
133 mBitmapScaler->Cancel(); |
|
134 break; |
|
135 } |
|
136 mNotify.ImageReadyCallBack(KErrCancel, 0); |
|
137 } |
|
138 |
|
139 // ----------------------------------------------------------------------------- |
|
140 // ----------------------------------------------------------------------------- |
|
141 // |
|
142 void CTsGraphicFileScalingHandler::RunL() |
|
143 { |
|
144 User::LeaveIfError(iStatus.Int()); |
|
145 |
|
146 switch (mCurrentOperation) { |
|
147 case EConvertBitmapFromFile: { |
|
148 delete mImageDecoder; |
|
149 mImageDecoder = 0; |
|
150 |
|
151 mBitmapOutput = new (ELeave)CFbsBitmap(); |
|
152 User::LeaveIfError(mBitmapOutput->Create(Scaling(), mBitmapFromFile->DisplayMode())); |
|
153 mBitmapScaler->Scale(&iStatus, *mBitmapFromFile, *mBitmapOutput, EFalse); |
|
154 mCurrentOperation = EScale; |
|
155 SetActive(); |
|
156 break; |
|
157 } |
|
158 case EScale: { |
|
159 mCurrentOperation = ENone; |
|
160 |
|
161 delete mBitmapScaler; |
|
162 mBitmapScaler = 0; |
|
163 |
|
164 delete mBitmapFromFile; |
|
165 mBitmapFromFile = 0; |
|
166 |
|
167 if (mKindOfScaling == CTsGraphicFileScalingHandler::EKeepAspectRatioByExpanding) { |
|
168 User::LeaveIfError(mBitmapOutput->Resize(mNewSize)); |
|
169 } |
|
170 |
|
171 mNotify.ImageReadyCallBack(iStatus.Int(), mBitmapOutput); |
|
172 break; |
|
173 } |
|
174 } |
|
175 } |
|
176 |
|
177 // ----------------------------------------------------------------------------- |
|
178 // ----------------------------------------------------------------------------- |
|
179 // |
|
180 TSize CTsGraphicFileScalingHandler::Scaling() |
|
181 { |
|
182 TSize originalSize = mBitmapFromFile->SizeInPixels(); |
|
183 float widthFactor = mNewSize.iWidth / (float)originalSize.iWidth; |
|
184 float heightFactor = mNewSize.iHeight / (float)originalSize.iHeight; |
|
185 |
|
186 TSize retSize(mNewSize); |
|
187 |
|
188 if (mKindOfScaling == CTsGraphicFileScalingHandler::EKeepAspectRatio) { |
|
189 retSize = (widthFactor < heightFactor) ? |
|
190 TSize(mNewSize.iWidth, widthFactor * originalSize.iHeight) : |
|
191 TSize(heightFactor * originalSize.iWidth, mNewSize.iHeight); |
|
192 } else if (mKindOfScaling == CTsGraphicFileScalingHandler::EKeepAspectRatioByExpanding) { |
|
193 retSize = (widthFactor < heightFactor) ? |
|
194 TSize(heightFactor * originalSize.iWidth, mNewSize.iHeight) : |
|
195 TSize(mNewSize.iWidth, widthFactor * originalSize.iHeight); |
|
196 } |
|
197 return retSize; |
|
198 } |
|
199 // ----------------------------------------------------------------------------- |
|
200 // ----------------------------------------------------------------------------- |
|
201 // |
|
202 TInt CTsGraphicFileScalingHandler::RunError(TInt aError) |
|
203 { |
|
204 mNotify.ImageReadyCallBack(aError, 0); |
|
205 return KErrNone; |
|
206 } |