|
1 /* |
|
2 * Copyright (c) 1999-2002 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 |
|
18 #include <e32def.h> // MAKE_TINT64 |
|
19 #include "javax_microedition_lcdui_Image.h" |
|
20 #include "CMIDToolkit.h" |
|
21 #include "CMIDImageLoader.h" |
|
22 #include "MIDUtils.h" |
|
23 #include "lcdgr.h" |
|
24 |
|
25 inline MMIDImage* Image(jint aHandle) |
|
26 { |
|
27 return MIDUnhand<MMIDImage>(aHandle); |
|
28 } |
|
29 |
|
30 struct TImageAttributes |
|
31 { |
|
32 jint iHandle; |
|
33 jint iWidth; |
|
34 jint iHeight; |
|
35 jint iTransparency; |
|
36 jint iSizeBytes; |
|
37 }; |
|
38 |
|
39 TInt WordAlignedSize(const TSize& aSize, TInt aBpp) |
|
40 { |
|
41 TInt scan = (((aSize.iWidth * aBpp) + 31) & ~31) >> 3; |
|
42 return (scan * aSize.iHeight); |
|
43 } |
|
44 |
|
45 LOCAL_C void GetAttribs(MMIDGraphicsFactory& /*aFactory*/, MMIDImage* aImage, TImageAttributes* aAttribs) |
|
46 { |
|
47 TSize size = aImage->Size(); |
|
48 TInt type = aImage->TransparencyType(); |
|
49 |
|
50 aAttribs->iWidth = size.iWidth; |
|
51 aAttribs->iHeight = size.iHeight; |
|
52 aAttribs->iTransparency = type; |
|
53 |
|
54 |
|
55 TInt colorBpp = 32; |
|
56 TInt alphaBpp = 0; |
|
57 if (type == MMIDImage::EMask) |
|
58 { |
|
59 // assume 1bpp mask bitmap |
|
60 alphaBpp = 1; |
|
61 } |
|
62 else if (type == MMIDImage::EAlpha) |
|
63 { |
|
64 // assume 8bpp alpha bitmap |
|
65 alphaBpp = 8; |
|
66 } |
|
67 |
|
68 aAttribs->iSizeBytes = WordAlignedSize(size, colorBpp) + WordAlignedSize(size, alphaBpp); |
|
69 } |
|
70 |
|
71 LOCAL_C void SetAttribArray(JNIEnv* aJni, jintArray aArray, const TImageAttributes& aAttribs) |
|
72 { |
|
73 ASSERT(aJni->GetArrayLength(aArray) == javax_microedition_lcdui_Image_ATTRIB_COUNT); |
|
74 TInt array[javax_microedition_lcdui_Image_ATTRIB_COUNT]; |
|
75 array[javax_microedition_lcdui_Image_ATTRIB_WIDTH] = aAttribs.iWidth; |
|
76 array[javax_microedition_lcdui_Image_ATTRIB_HEIGHT]= aAttribs.iHeight; |
|
77 array[javax_microedition_lcdui_Image_ATTRIB_TYPE] = aAttribs.iTransparency; |
|
78 array[javax_microedition_lcdui_Image_ATTRIB_SIZE] = aAttribs.iSizeBytes; |
|
79 aJni->SetIntArrayRegion(aArray, 0, javax_microedition_lcdui_Image_ATTRIB_COUNT, &(array[0])); |
|
80 } |
|
81 |
|
82 LOCAL_C void InvokeCreateMutableL(CMIDToolkit* aToolkit, TImageAttributes* aAttribs, jint aWidth, jint aHeight, jint aColor) |
|
83 { |
|
84 MMIDGraphicsFactory& factory = aToolkit->GraphicsFactory(); |
|
85 |
|
86 if (aColor != 0xffffffff) |
|
87 { |
|
88 User::Leave(KErrNotSupported); |
|
89 } |
|
90 |
|
91 TSize size(aWidth,aHeight); |
|
92 |
|
93 MMIDImage* image = factory.NewMutableImageL(size); |
|
94 CleanupDisposePushL(image); |
|
95 aAttribs->iHandle = aToolkit->RegisterComponentL(image, NULL); |
|
96 CleanupPopComponent(image); |
|
97 |
|
98 GetAttribs(factory, image, aAttribs); |
|
99 } |
|
100 |
|
101 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_Image__1createMutable |
|
102 ( |
|
103 JNIEnv* aJni, |
|
104 jobject, |
|
105 jint aToolkit, |
|
106 jint aWidth, |
|
107 jint aHeight, |
|
108 jint aColor, |
|
109 jintArray aAttribs |
|
110 ) |
|
111 { |
|
112 CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit); |
|
113 TImageAttributes attribs; |
|
114 |
|
115 jint err = toolkit->ExecuteTrap(&InvokeCreateMutableL, toolkit, &attribs, aWidth, aHeight, aColor); |
|
116 if (0 == err) |
|
117 { |
|
118 SetAttribArray(aJni, aAttribs, attribs); |
|
119 return attribs.iHandle; |
|
120 } |
|
121 |
|
122 return err; |
|
123 } |
|
124 |
|
125 LOCAL_C void InvokeCreateFromLoaderL(CMIDToolkit* aToolkit, TImageAttributes* aAttribs, jint aLoader) |
|
126 { |
|
127 MMIDGraphicsFactory& factory = aToolkit->GraphicsFactory(); |
|
128 |
|
129 CMIDImageLoader* loader = JavaUnhand<CMIDImageLoader>(aLoader); |
|
130 MMIDImage* image = factory.NewImageL(loader->Decoder()); |
|
131 CleanupDisposePushL(image); |
|
132 aAttribs->iHandle = aToolkit->RegisterComponentL(image, NULL); |
|
133 CleanupPopComponent(image); |
|
134 |
|
135 GetAttribs(factory, image, aAttribs); |
|
136 } |
|
137 |
|
138 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_Image__1createImmutable |
|
139 ( |
|
140 JNIEnv* aJni, |
|
141 jobject, |
|
142 jint aToolkit, |
|
143 jint aLoader, |
|
144 jintArray aAttributes |
|
145 ) |
|
146 { |
|
147 CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit); |
|
148 TImageAttributes attribs; |
|
149 |
|
150 jint error = toolkit->ExecuteTrap(InvokeCreateFromLoaderL, toolkit, &attribs, aLoader); |
|
151 if (KErrNone == error) |
|
152 { |
|
153 SetAttribArray(aJni, aAttributes, attribs); |
|
154 return attribs.iHandle; |
|
155 } |
|
156 |
|
157 return error; |
|
158 } |
|
159 |
|
160 LOCAL_C void InvokeCreateFromCanvasL(CMIDToolkit* aToolkit, TImageAttributes* aAttribs, jint aCanvas) |
|
161 { |
|
162 MMIDGraphicsFactory& factory = aToolkit->GraphicsFactory(); |
|
163 MMIDImage* image = factory.NewImageL(MIDUnhand<MMIDCanvas>(aCanvas)); |
|
164 CleanupDisposePushL(image); |
|
165 aAttribs->iHandle = aToolkit->RegisterComponentL(image, NULL); |
|
166 CleanupPopComponent(image); |
|
167 GetAttribs(factory, image, aAttribs); |
|
168 } |
|
169 |
|
170 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_Image__1createFromCanvas |
|
171 ( |
|
172 JNIEnv* aJni, |
|
173 jobject, |
|
174 jint aToolkit, |
|
175 jint aCanvas, |
|
176 jintArray aAttributes |
|
177 ) |
|
178 { |
|
179 CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit); |
|
180 TImageAttributes attribs; |
|
181 |
|
182 jint error = toolkit->ExecuteTrap(InvokeCreateFromCanvasL, toolkit, &attribs, aCanvas); |
|
183 if (KErrNone == error) |
|
184 { |
|
185 SetAttribArray(aJni, aAttributes, attribs); |
|
186 return attribs.iHandle; |
|
187 } |
|
188 |
|
189 return error; |
|
190 } |
|
191 |
|
192 |
|
193 LOCAL_C void InvokeCreateRegionL(CMIDToolkit* aToolkit, TImageAttributes* aAttribs, jint aSource, const TRect* aRect, jint aTransform) |
|
194 { |
|
195 MMIDGraphicsFactory& factory = aToolkit->GraphicsFactory(); |
|
196 |
|
197 MMIDImage* source = Image(aSource); |
|
198 TSize size = aRect->Size(); |
|
199 if (aTransform & 4) |
|
200 { |
|
201 TInt tmp = size.iWidth; |
|
202 size.iWidth = size.iHeight; |
|
203 size.iHeight = tmp; |
|
204 } |
|
205 MMIDImage* image = factory.NewImageL(size, source->TransparencyType()); |
|
206 CleanupDisposePushL(image); |
|
207 User::LeaveIfError(image->CopyRegion(source, *aRect, aTransform)); |
|
208 aAttribs->iHandle = aToolkit->RegisterComponentL(image, NULL); |
|
209 CleanupPopComponent(image); |
|
210 GetAttribs(factory, image, aAttribs); |
|
211 } |
|
212 |
|
213 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_Image__1createRegion |
|
214 ( |
|
215 JNIEnv* aJni, |
|
216 jobject, |
|
217 jint aToolkit, |
|
218 jint aSource, |
|
219 jint aX, |
|
220 jint aY, |
|
221 jint aWidth, |
|
222 jint aHeight, |
|
223 jint aTransform, |
|
224 jintArray aAttributes |
|
225 ) |
|
226 { |
|
227 CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit); |
|
228 TImageAttributes attribs; |
|
229 const TRect rect(TPoint(aX,aY),TSize(aWidth,aHeight)); |
|
230 |
|
231 jint error = toolkit->ExecuteTrap(InvokeCreateRegionL, toolkit, &attribs, aSource, &rect, aTransform); |
|
232 if (KErrNone == error) |
|
233 { |
|
234 SetAttribArray(aJni, aAttributes, attribs); |
|
235 return attribs.iHandle; |
|
236 } |
|
237 |
|
238 return error; |
|
239 } |
|
240 |
|
241 TInt GetTransparencyType(const jint* /*aPixels*/, jint /*aLength*/, jboolean aAlpha) |
|
242 { |
|
243 return aAlpha ? MMIDImage::EAlpha : MMIDImage::ENone; |
|
244 } |
|
245 |
|
246 LOCAL_C void InvokeCreateRGBL(CMIDToolkit* aToolkit, TImageAttributes* aAttribs, jint* aArray, jint aWidth, jint aHeight, jboolean aAlpha) |
|
247 { |
|
248 MMIDGraphicsFactory& factory = aToolkit->GraphicsFactory(); |
|
249 |
|
250 TSize size(aWidth,aHeight); |
|
251 TRect rect(size); |
|
252 TInt type = GetTransparencyType(aArray, aWidth*aHeight, aAlpha); |
|
253 MMIDImage* image = factory.NewImageL(size, type); |
|
254 |
|
255 CleanupDisposePushL(image); |
|
256 User::LeaveIfError(image->SetPixels((const TUint32*)aArray, aWidth*aHeight, 0, aWidth, rect, aAlpha)); |
|
257 aAttribs->iHandle = aToolkit->RegisterComponentL(image, NULL); |
|
258 CleanupPopComponent(image); |
|
259 |
|
260 GetAttribs(factory, image, aAttribs); |
|
261 } |
|
262 |
|
263 |
|
264 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_Image__1createRGB |
|
265 ( |
|
266 JNIEnv* aJni, |
|
267 jobject, |
|
268 jint aToolkit, |
|
269 jintArray aPixels, |
|
270 jint aWidth, |
|
271 jint aHeight, |
|
272 jboolean aAlpha, |
|
273 jintArray aAttributes |
|
274 ) |
|
275 { |
|
276 CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit); |
|
277 jint error = KErrNoMemory; |
|
278 |
|
279 jint length = aJni->GetArrayLength(aPixels); |
|
280 ASSERT(length >= aWidth*aHeight); |
|
281 |
|
282 jint* pixels = (jint*)aJni->GetIntArrayElements(aPixels, NULL); |
|
283 if (pixels) |
|
284 { |
|
285 TImageAttributes attribs; |
|
286 error = toolkit->ExecuteTrap(InvokeCreateRGBL, toolkit, &attribs, pixels, aWidth, aHeight, aAlpha); |
|
287 aJni->ReleaseIntArrayElements(aPixels, pixels, JNI_ABORT); |
|
288 if (KErrNone == error) |
|
289 { |
|
290 SetAttribArray(aJni, aAttributes, attribs); |
|
291 return attribs.iHandle; |
|
292 } |
|
293 } |
|
294 |
|
295 return error; |
|
296 } |
|
297 |
|
298 LOCAL_C jint InvokeCopyRegion(jint aTarget, jint aSource, const TRect* aRect, jint aTransform) |
|
299 { |
|
300 return Image(aTarget)->CopyRegion(Image(aSource), *aRect, aTransform); |
|
301 } |
|
302 |
|
303 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_Image__1copyRegion |
|
304 ( |
|
305 JNIEnv* /*aEnv*/, |
|
306 jobject, |
|
307 jint aToolkit, |
|
308 jint aTarget, |
|
309 jint aSource, |
|
310 jint aX, |
|
311 jint aY, |
|
312 jint aWidth, |
|
313 jint aHeight, |
|
314 jint aTransform |
|
315 ) |
|
316 { |
|
317 const TRect srcRect(TPoint(aX,aY), TSize(aWidth, aHeight)); |
|
318 return JavaUnhand<CMIDToolkit>(aToolkit)->Execute(&InvokeCopyRegion, aTarget, aSource, &srcRect, aTransform); |
|
319 } |
|
320 |
|
321 LOCAL_C jint InvokeSetPixels |
|
322 ( |
|
323 jint aHandle, |
|
324 jint* aArray, |
|
325 jint aLength, |
|
326 jint aOffset, |
|
327 jint aScanLength, |
|
328 const TRect* aRect, |
|
329 jboolean aAlpha |
|
330 ) |
|
331 { |
|
332 return Image(aHandle)->SetPixels((const TUint32*)aArray, aLength, aOffset, aScanLength, *aRect, aAlpha); |
|
333 } |
|
334 |
|
335 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_Image__1setPixels |
|
336 ( |
|
337 JNIEnv* aEnv, |
|
338 jobject, |
|
339 jint aToolkit, |
|
340 jint aHandle, |
|
341 jintArray aArray, |
|
342 jint aOffset, |
|
343 jint aScanLength, |
|
344 jint aX, |
|
345 jint aY, |
|
346 jint aWidth, |
|
347 jint aHeight, |
|
348 jboolean aAlpha |
|
349 ) |
|
350 { |
|
351 const TRect rect(TPoint(aX,aY),TSize(aWidth,aHeight)); |
|
352 |
|
353 jint err = KErrNoMemory; |
|
354 jint length = aEnv->GetArrayLength(aArray); |
|
355 jint* array = (jint*)aEnv->GetIntArrayElements(aArray, NULL); |
|
356 if (array) |
|
357 { |
|
358 err = JavaUnhand<CMIDToolkit>(aToolkit)->Execute(&InvokeSetPixels, aHandle, array, length, aOffset, aScanLength, &rect, aAlpha); |
|
359 aEnv->ReleaseIntArrayElements(aArray, array, JNI_ABORT); |
|
360 } |
|
361 |
|
362 return err; |
|
363 } |
|
364 |
|
365 LOCAL_C jint InvokeGetPixels |
|
366 ( |
|
367 jint aHandle, |
|
368 jint* aArray, |
|
369 jint aLength, |
|
370 jint aOffset, |
|
371 jint aScanLength, |
|
372 const TRect* aRect |
|
373 ) |
|
374 { |
|
375 return Image(aHandle)->GetPixels((TUint32*)aArray, aLength, aOffset, aScanLength, *aRect); |
|
376 } |
|
377 |
|
378 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_Image__1getPixels |
|
379 ( |
|
380 JNIEnv* aEnv, |
|
381 jobject, |
|
382 jint aToolkit, |
|
383 jint aHandle, |
|
384 jintArray aArray, |
|
385 jint aOffset, |
|
386 jint aScanLength, |
|
387 jint aX, |
|
388 jint aY, |
|
389 jint aWidth, |
|
390 jint aHeight |
|
391 ) |
|
392 { |
|
393 const TRect rect(TPoint(aX,aY),TSize(aWidth,aHeight)); |
|
394 jint err = KErrNoMemory; |
|
395 jint length = aEnv->GetArrayLength(aArray); |
|
396 jint* array = (jint*)aEnv->GetIntArrayElements(aArray, NULL); |
|
397 if (array) |
|
398 { |
|
399 err = JavaUnhand<CMIDToolkit>(aToolkit)->Execute(&InvokeGetPixels, aHandle, array, length, aOffset, aScanLength, &rect); |
|
400 aEnv->ReleaseIntArrayElements(aArray, array, 0); |
|
401 } |
|
402 |
|
403 return err; |
|
404 } |
|
405 |
|
406 |
|
407 LOCAL_C TInt InvokeDetectCollision(TInt aImageHandle1, const TRect* aRect1, TInt aTransform1,const TPoint* aPoint1, |
|
408 TInt aImageHandle2, const TRect* aRect2, TInt aTransform2,const TPoint* aPoint2) |
|
409 { |
|
410 MMIDImage* image1 = MIDUnhand<MMIDImage>(aImageHandle1); |
|
411 MMIDImage* image2 = MIDUnhand<MMIDImage>(aImageHandle2); |
|
412 return image1->DetectCollision(*aRect1,aTransform1,*aPoint1,image2,*aRect2,aTransform2,*aPoint2); |
|
413 } |
|
414 |
|
415 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_Image__1collision |
|
416 ( |
|
417 JNIEnv*, |
|
418 jclass, |
|
419 jint aToolkit, |
|
420 jint aImage1, |
|
421 jint aS1X1, |
|
422 jint aS1Y1, |
|
423 jint aS1X2, |
|
424 jint aS1Y2, |
|
425 jint aTransform1, |
|
426 jint aD1X1, |
|
427 jint aD1Y1, |
|
428 jint aImage2, |
|
429 jint aS2X1, |
|
430 jint aS2Y1, |
|
431 jint aS2X2, |
|
432 jint aS2Y2, |
|
433 jint aTransform2, |
|
434 jint aD2X1, |
|
435 jint aD2Y1) |
|
436 { |
|
437 const TRect rect1(TPoint(aS1X1,aS1Y1),TPoint(aS1X2,aS1Y2)); |
|
438 const TRect rect2(TPoint(aS2X1,aS2Y1),TPoint(aS2X2,aS2Y2)); |
|
439 const TPoint point1(TPoint(aD1X1,aD1Y1)); |
|
440 const TPoint point2(TPoint(aD2X1,aD2Y1)); |
|
441 CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit); |
|
442 return toolkit->Execute(InvokeDetectCollision, (TInt)aImage1, &rect1, (TInt)aTransform1, &point1,(TInt)aImage2, &rect2, (TInt)aTransform2, &point2); |
|
443 } |
|
444 |
|
445 jint InvokeSetTransparencyType(jint aImage, MMIDImage::TTransparencyType aType) |
|
446 { |
|
447 return MIDUnhand<MMIDImage>(aImage)->SetTransparencyType(aType); |
|
448 } |
|
449 |
|
450 |
|
451 JNIEXPORT jint JNICALL |
|
452 Java_javax_microedition_lcdui_Image__1setTransparencyType |
|
453 ( |
|
454 JNIEnv*, |
|
455 jobject /*aImageRef*/, |
|
456 jint aToolkitHandle, |
|
457 jint aImageHandle, |
|
458 jint aTransparencyType |
|
459 ) |
|
460 { |
|
461 if ((aTransparencyType < 0) || (aTransparencyType > MMIDImage::EAlpha)) |
|
462 { |
|
463 return KErrArgument; |
|
464 } |
|
465 MMIDImage::TTransparencyType type = (MMIDImage::TTransparencyType)aTransparencyType; |
|
466 return JavaUnhand<CMIDToolkit>(aToolkitHandle)->Execute(InvokeSetTransparencyType, aImageHandle, type); |
|
467 } |