|
1 /* |
|
2 * Copyright (c) 2010 Ixonos Plc. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "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 * Ixonos Plc |
|
14 * |
|
15 * Description: |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "CFilterClipart.h" |
|
21 |
|
22 #include <e32math.h> |
|
23 |
|
24 const TInt KScaleBits = 12; |
|
25 const TInt KCoordBits = 8; |
|
26 |
|
27 EXPORT_C TInt CFilterClipart::Create() |
|
28 { |
|
29 CFilterClipart* ptr = NULL; |
|
30 TRAPD( error, ptr = NewL(); ); |
|
31 if( error != KErrNone ) |
|
32 { |
|
33 ptr = NULL; |
|
34 } |
|
35 return (TInt)((MImageFilter*)ptr); |
|
36 } |
|
37 |
|
38 |
|
39 |
|
40 CFilterClipart* CFilterClipart::NewL() |
|
41 { |
|
42 CFilterClipart* self = new( ELeave )CFilterClipart(); |
|
43 CleanupStack::PushL( self ); |
|
44 self->ConstructL(); |
|
45 CleanupStack::Pop( self ); |
|
46 return self; |
|
47 } |
|
48 |
|
49 |
|
50 |
|
51 CFilterClipart::~CFilterClipart() |
|
52 { |
|
53 delete[] iData; |
|
54 } |
|
55 |
|
56 |
|
57 |
|
58 CFilterClipart::CFilterClipart() |
|
59 { |
|
60 |
|
61 } |
|
62 |
|
63 |
|
64 |
|
65 void CFilterClipart::ConstructL() |
|
66 { |
|
67 iScale.iX = 1 << 8; |
|
68 iScale.iY = 0; |
|
69 iZoom = 1000; |
|
70 iAngle = 0; |
|
71 } |
|
72 |
|
73 |
|
74 |
|
75 TRect CFilterClipart::Rect() |
|
76 { |
|
77 return iChild->Rect(); |
|
78 } |
|
79 |
|
80 TReal CFilterClipart::Scale() |
|
81 { |
|
82 return iChild->Scale(); |
|
83 } |
|
84 |
|
85 TSize CFilterClipart::ViewPortSize() |
|
86 { |
|
87 return iChild->ViewPortSize(); |
|
88 } |
|
89 |
|
90 TBlock * CFilterClipart::GetBlockL ( const TRect & aRect ) |
|
91 { |
|
92 TBlock * pB = iChild->GetBlockL (aRect); |
|
93 if (!pB) return NULL; |
|
94 TUint32 * pD = pB->iData; |
|
95 |
|
96 for (TInt i = pB->iRect.iTl.iY; i < pB->iRect.iBr.iY; ++i) |
|
97 { |
|
98 for (TInt j = pB->iRect.iTl.iX; j < pB->iRect.iBr.iX; ++j, ++pD) |
|
99 { |
|
100 |
|
101 TUint32 c = *pD; |
|
102 |
|
103 TPoint pos = iCorrectPosition + TPoint (j << KCoordBits, i << KCoordBits); |
|
104 TInt x = ( pos.iX * iScale.iX + pos.iY * iScale.iY ) >> ( KScaleBits + KCoordBits ); |
|
105 TInt y = ( pos.iY * iScale.iX - pos.iX * iScale.iY ) >> ( KScaleBits + KCoordBits ); |
|
106 |
|
107 if ( (x >= 0) && (y >= 0) && (x < iSize.iWidth) && (y < iSize.iHeight) ) |
|
108 { |
|
109 |
|
110 TUint32 c2 = iData[ x + y * iSize.iWidth ]; |
|
111 TInt ca = c2 >> 24; |
|
112 |
|
113 if ( ca == 255 ) |
|
114 { |
|
115 *pD = c2 & 0xffffff; |
|
116 } |
|
117 else |
|
118 { |
|
119 TInt cc1 = (c2 & 0xff00ff) * ca + (c & 0xff00ff) * (255 - ca); |
|
120 TInt cc2 = (c2 & 0xff00) * ca + (c & 0xff00) * (255 - ca); |
|
121 *pD = (( cc1 >> 8) & 0xff00ff) + ((cc2 >> 8) & 0xff00); |
|
122 } |
|
123 } |
|
124 } |
|
125 } |
|
126 return pB; |
|
127 } |
|
128 |
|
129 |
|
130 void CFilterClipart::SetParent( MImageFilter* aParent ) |
|
131 { |
|
132 iParent = aParent; |
|
133 } |
|
134 |
|
135 |
|
136 |
|
137 void CFilterClipart::SetChild( MImageFilter* aChild ) |
|
138 { |
|
139 iChild = aChild; |
|
140 } |
|
141 |
|
142 |
|
143 |
|
144 TInt CFilterClipart::CmdL( const TDesC16& aCmd ) |
|
145 { |
|
146 |
|
147 TLex lex( aCmd ); |
|
148 |
|
149 while( ! lex.Eos() ) |
|
150 { |
|
151 TPtrC token = lex.NextToken(); |
|
152 if( token.Compare( _L("file") ) == 0 ) |
|
153 { |
|
154 TPtrC namec = lex.NextToken(); |
|
155 TPtrC name( namec.Ptr()+1, namec.Length()-2 ); |
|
156 iFileName.Copy (name); |
|
157 } |
|
158 else if( token.Compare( _L("load") ) == 0 ) |
|
159 { |
|
160 LoadImageL(); |
|
161 } |
|
162 else if( token.Compare( _L("clipart") ) == 0 ) |
|
163 { |
|
164 lex.Inc(); |
|
165 lex.Val( iClipart ); |
|
166 } |
|
167 else if( token.Compare( _L("mask") ) == 0 ) |
|
168 { |
|
169 lex.Inc(); |
|
170 lex.Val( iMask ); |
|
171 } |
|
172 else if( token.Compare( _L("x") ) == 0 ) |
|
173 { |
|
174 TReal relscale = iChild->Scale(); |
|
175 TInt param = 0; |
|
176 lex.Inc (); |
|
177 lex.Val (param); |
|
178 iPosition.iX = (TInt)((param / relscale) + 0.5); |
|
179 } |
|
180 else if( token.Compare( _L("y") ) == 0 ) |
|
181 { |
|
182 TReal relscale = iChild->Scale(); |
|
183 TInt param = 0; |
|
184 lex.Inc (); |
|
185 lex.Val (param); |
|
186 iPosition.iY = (TInt)((param / relscale) + 0.5); |
|
187 } |
|
188 else if( token.Compare( _L("width") ) == 0 ) |
|
189 { |
|
190 TReal relscale = iChild->Scale(); |
|
191 TInt param = 0; |
|
192 lex.Inc (); |
|
193 lex.Val (param); |
|
194 TInt width = (TInt)((param / relscale) + 0.5); |
|
195 iZoom = (width * 1000) / iSize.iWidth; |
|
196 } |
|
197 else if( token.Compare( _L("height") ) == 0 ) |
|
198 { |
|
199 TReal relscale = iChild->Scale(); |
|
200 TInt param = 0; |
|
201 lex.Inc (); |
|
202 lex.Val (param); |
|
203 TInt height = (TInt)((param / relscale) + 0.5); |
|
204 iZoom = (height * 1000) / iSize.iHeight; |
|
205 } |
|
206 else if( token.Compare( _L("zoom") ) == 0 ) |
|
207 { |
|
208 lex.Inc(); |
|
209 lex.Val( iZoom ); |
|
210 } |
|
211 else if( token.Compare( _L("angle") ) == 0 ) |
|
212 { |
|
213 lex.Inc(); |
|
214 lex.Val( iAngle ); |
|
215 } |
|
216 } |
|
217 |
|
218 TReal relscale = iChild->Scale(); |
|
219 TInt scaledx = (TInt)(iPosition.iX * relscale + 0.5); |
|
220 TInt scaledy = (TInt)(iPosition.iY * relscale + 0.5); |
|
221 TInt scaledz = (TInt)(iZoom * relscale + 0.5); |
|
222 |
|
223 TReal zoom = 0.001 * scaledz; |
|
224 TReal angle = 0.001 * iAngle; |
|
225 TReal rad = KPi * angle / 180.0; |
|
226 TReal r; |
|
227 TReal res; |
|
228 |
|
229 Math::Cos( r, rad ); |
|
230 r *= 1 << KScaleBits; |
|
231 //r /= zoom; |
|
232 Math::Round( res, r, 0 ); |
|
233 iScale.iX = (TInt)res; |
|
234 |
|
235 Math::Sin( r, rad ); |
|
236 r *= 1 << KScaleBits; |
|
237 //r /= zoom; |
|
238 Math::Round( res, r, 0 ); |
|
239 iScale.iY = (TInt)res; |
|
240 |
|
241 // helpers |
|
242 TPoint mid( iSize.iWidth/2, iSize.iHeight/2 ); |
|
243 TPoint sca( (TInt)(zoom * iScale.iX), (TInt)(zoom * iScale.iY) ); |
|
244 TInt xo = ( mid.iX * sca.iX - mid.iY * sca.iY ) >> ( KScaleBits - KCoordBits ); |
|
245 TInt yo = ( mid.iY * sca.iX + mid.iX * sca.iY ) >> ( KScaleBits - KCoordBits ); |
|
246 |
|
247 // |
|
248 // iCorrectPosition is clipart mid position corrected with |
|
249 // rotation ( otherwise rotation would be around topleft corner ) |
|
250 // |
|
251 iCorrectPosition = TPoint( xo,yo ) - TPoint( scaledx << KCoordBits, scaledy << KCoordBits ); |
|
252 |
|
253 // |
|
254 // with iScale screen coordinates are translated to texture coordinates |
|
255 // |
|
256 iScale.iX = (TInt)(1.0 / zoom * iScale.iX); |
|
257 iScale.iY = (TInt)(1.0 / zoom * iScale.iY); |
|
258 |
|
259 return 0; |
|
260 } |
|
261 |
|
262 |
|
263 |
|
264 void CFilterClipart::LoadImageL () |
|
265 { |
|
266 |
|
267 // create and load clipart |
|
268 CFbsBitmap * clipart = new (ELeave) CFbsBitmap; |
|
269 CleanupStack::PushL(clipart); |
|
270 User::LeaveIfError ( clipart->Load (iFileName, iClipart) ); |
|
271 |
|
272 // create and load mask |
|
273 CFbsBitmap * mask = new (ELeave) CFbsBitmap; |
|
274 CleanupStack::PushL(mask); |
|
275 User::LeaveIfError ( mask->Load (iFileName, iMask) ); |
|
276 |
|
277 // clipart size |
|
278 iSize = clipart->SizeInPixels(); |
|
279 |
|
280 // create new buffer |
|
281 delete[] iData; |
|
282 iData = new (ELeave) TUint32 [iSize.iWidth * iSize.iHeight]; |
|
283 |
|
284 // copy clipart data |
|
285 clipart->LockHeapLC(); |
|
286 |
|
287 TInt wsc = clipart->ScanLineLength (iSize.iWidth, clipart->DisplayMode()); |
|
288 TInt wsm = mask->ScanLineLength (iSize.iWidth, mask->DisplayMode()); |
|
289 TUint8 * pcos = (TUint8 *)clipart->DataAddress(); |
|
290 TUint8 * pmos = (TUint8 *)mask->DataAddress(); |
|
291 |
|
292 for ( TInt i = 0, k = 0; i < iSize.iHeight; ++i ) |
|
293 { |
|
294 |
|
295 TUint8 * pc = pcos; |
|
296 pcos += wsc; |
|
297 |
|
298 TUint8 * pm = pmos; |
|
299 pmos += wsm; |
|
300 |
|
301 for ( TInt j = 0; j < iSize.iWidth; ++j ) |
|
302 { |
|
303 iData[k++] = (TUint32)(*pc | (*(pc + 1) << 8) | (*(pc + 2) << 16) | (*pm++ << 24)); |
|
304 pc += 3; |
|
305 } |
|
306 } |
|
307 |
|
308 CleanupStack::PopAndDestroy(3); // LockHeapLC(), mask, clipart |
|
309 |
|
310 } |
|
311 |
|
312 |
|
313 |
|
314 const char* CFilterClipart::Type() |
|
315 { |
|
316 return "clipart"; |
|
317 } |
|
318 |
|
319 |
|
320 #if !defined(EKA2) |
|
321 GLDEF_C TInt E32Dll( TDllReason ) |
|
322 { |
|
323 return KErrNone; |
|
324 } |
|
325 #endif |