author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 13 Oct 2010 14:23:59 +0300 | |
branch | RCL_3 |
changeset 83 | 26b2b12093af |
parent 77 | 7cee158cb8cd |
permissions | -rw-r--r-- |
66 | 1 |
/* |
2 |
* Copyright (c) 1999 - 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 "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 |
package javax.microedition.lcdui; |
|
19 |
||
20 |
||
21 |
import java.io.InputStream; |
|
22 |
import java.io.IOException; |
|
23 |
import javax.microedition.lcdui.game.Sprite; |
|
24 |
||
25 |
import com.nokia.mj.impl.rt.legacy.MemoryUtil; |
|
26 |
||
27 |
import com.nokia.mj.impl.rt.legacy.NativeError; |
|
28 |
import com.nokia.mj.impl.rt.support.Finalizer; |
|
29 |
||
30 |
/** |
|
31 |
* Image API class. |
|
32 |
*/ |
|
33 |
public class Image |
|
34 |
{ |
|
35 |
private static final int ATTRIB_WIDTH = 0; |
|
36 |
private static final int ATTRIB_HEIGHT = 1; |
|
37 |
private static final int ATTRIB_TYPE = 2; |
|
38 |
private static final int ATTRIB_SIZE = 3; |
|
39 |
private static final int ATTRIB_COUNT = 4; |
|
40 |
||
41 |
static final int TRANSPARENCY_NONE = 0; |
|
42 |
static final int TRANSPARENCY_MASK = 1; |
|
43 |
static final int TRANSPARENCY_ALPHA = 2; |
|
44 |
||
45 |
/** |
|
46 |
* Image width, 0 if unknown |
|
47 |
*/ |
|
48 |
private int iWidth; |
|
49 |
||
50 |
/** |
|
51 |
* Image height 0 if unknown |
|
52 |
*/ |
|
53 |
private int iHeight; |
|
54 |
||
55 |
/** |
|
56 |
* true if image is mutable |
|
57 |
*/ |
|
58 |
private boolean iMutable; |
|
59 |
||
60 |
/** |
|
61 |
* true if image has transparency |
|
62 |
*/ |
|
63 |
private int iTransparency; |
|
64 |
||
65 |
/** |
|
66 |
* |
|
67 |
*/ |
|
68 |
private int iSizeBytes; |
|
69 |
||
70 |
/** |
|
71 |
* Peer handle |
|
72 |
*/ |
|
73 |
int iHandle; |
|
74 |
||
75 |
/** |
|
76 |
* The owning MIDlets toolkit |
|
77 |
*/ |
|
78 |
Toolkit iToolkit; |
|
79 |
||
80 |
private Finalizer mFinalizer = new Finalizer() |
|
81 |
{ |
|
82 |
public void finalizeImpl() |
|
83 |
{ |
|
84 |
doFinalize(); |
|
85 |
} |
|
86 |
}; |
|
87 |
||
88 |
private Image() |
|
89 |
{ |
|
90 |
iToolkit = Toolkit.getToolkit(); |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Create an image wrapping a canvas native frame buffer. |
|
95 |
*/ |
|
96 |
Image(Canvas aCanvas) |
|
97 |
{ |
|
98 |
this(); |
|
99 |
checkToolkit(aCanvas.iToolkit); |
|
100 |
final int[] attribs = new int[ATTRIB_COUNT]; |
|
101 |
synchronized (iToolkit) |
|
102 |
{ |
|
103 |
final int toolkit = iToolkit.getHandle(); |
|
104 |
iHandle = Toolkit.checkHandle(_createFromCanvas(toolkit, aCanvas.getContentHandle(), attribs)); |
|
105 |
} |
|
106 |
setAttributes(attribs, true); |
|
107 |
} |
|
108 |
||
109 |
private Image(int aWidth, int aHeight, int aColor) |
|
110 |
{ |
|
111 |
this(); |
|
112 |
final int[] attribs = new int[ATTRIB_COUNT]; |
|
113 |
synchronized (iToolkit) |
|
114 |
{ |
|
115 |
final int toolkit = iToolkit.getHandle(); |
|
116 |
iHandle = Toolkit.checkHandle(_createMutable(toolkit, aWidth, aHeight, aColor, attribs)); |
|
117 |
} |
|
118 |
setAttributes(attribs, true); |
|
119 |
} |
|
120 |
||
121 |
private Image(ImageLoader aLoader) |
|
122 |
{ |
|
123 |
this(); |
|
124 |
checkToolkit(aLoader.iToolkit); |
|
125 |
final int[] attribs = new int[ATTRIB_COUNT]; |
|
126 |
synchronized (iToolkit) |
|
127 |
{ |
|
128 |
final int toolkit = iToolkit.getHandle(); |
|
129 |
iHandle = Toolkit.checkHandle(_createImmutable(toolkit, aLoader.getHandle(), attribs)); |
|
130 |
} |
|
131 |
setAttributes(attribs, false); |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* |
|
136 |
*/ |
|
137 |
private Image(Image aSource, int aX, int aY, int aWidth, int aHeight, int aTransform) |
|
138 |
{ |
|
139 |
this(); |
|
140 |
checkToolkit(aSource.iToolkit); |
|
141 |
final int[] attribs = new int[ATTRIB_COUNT]; |
|
142 |
synchronized (iToolkit) |
|
143 |
{ |
|
144 |
final int toolkit = iToolkit.getHandle(); |
|
145 |
final int source = aSource.getHandle(true); |
|
146 |
iHandle = Toolkit.checkHandle(_createRegion(toolkit, source, aX, aY, aWidth, aHeight, aTransform,attribs)); |
|
147 |
} |
|
148 |
setAttributes(attribs, false); |
|
149 |
} |
|
150 |
||
151 |
private Image(int[] aRgbArray, int aWidth, int aHeight, boolean aProcessAlpha) |
|
152 |
{ |
|
153 |
this(); |
|
154 |
final int[] attribs = new int[ATTRIB_COUNT]; |
|
155 |
synchronized (iToolkit) |
|
156 |
{ |
|
157 |
final int toolkit = iToolkit.getHandle(); |
|
158 |
iHandle = Toolkit.checkHandle(_createRGB(toolkit, aRgbArray, aWidth, aHeight, aProcessAlpha, attribs)); |
|
159 |
} |
|
160 |
setAttributes(attribs, false); |
|
161 |
} |
|
162 |
||
163 |
// |
|
164 |
// API |
|
165 |
// |
|
166 |
public static Image createImage(byte[] aData,int aOffset,int aLength) |
|
167 |
{ |
|
168 |
final int space = aData.length - aOffset; // throws NullPointerException |
|
169 |
||
170 |
if ((aOffset < 0) || (space < aLength) || (aLength < 0)) |
|
171 |
{ |
|
172 |
throw new ArrayIndexOutOfBoundsException(); |
|
173 |
} |
|
174 |
||
175 |
final Toolkit toolkit = Toolkit.getToolkit(); |
|
176 |
ImageLoader loader = new ImageLoader(toolkit); |
|
177 |
try |
|
178 |
{ |
|
179 |
loader.append(aData, aOffset, aLength); |
|
180 |
return loadImage(loader); |
|
181 |
} |
|
182 |
catch (Throwable t) |
|
183 |
{ |
|
184 |
if (t instanceof OutOfMemoryError) |
|
185 |
{ |
|
186 |
throw(OutOfMemoryError)t; |
|
187 |
} |
|
188 |
throw new IllegalArgumentException("bad image format"); |
|
189 |
} |
|
190 |
finally |
|
191 |
{ |
|
192 |
loader.dispose(); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
// |
|
197 |
// API |
|
198 |
// |
|
199 |
public static Image createImage(Image aImage) |
|
200 |
{ |
|
201 |
return createImage(aImage, 0, 0, aImage.getWidth(), aImage.getHeight(), 0); |
|
202 |
} |
|
203 |
||
204 |
// |
|
205 |
// API |
|
206 |
// |
|
207 |
public static Image createImage(Image aImage, int aX, int aY, int aWidth, int aHeight, int aTransform) |
|
208 |
{ |
|
209 |
final int width = aImage.getWidth(); // throws NullPointerException |
|
210 |
final int height = aImage.getHeight(); |
|
211 |
||
212 |
if (aWidth <= 0 || aHeight <= 0) |
|
213 |
{ |
|
214 |
throw new IllegalArgumentException("invalid width or height"); |
|
215 |
} |
|
216 |
||
217 |
final int sx1 = aX; // left column |
|
218 |
final int sx2 = aX + aWidth; // right column |
|
219 |
||
220 |
final int sy1 = aY; // top row (inclusive) |
|
221 |
final int sy2 = aY + aHeight; // bottom row (exclusive) |
|
222 |
||
223 |
// |
|
224 |
// Check source x range lies within source image |
|
225 |
// |
|
226 |
final boolean xRangeError = (sx1 < 0) || (sx1 >= width) || (sx2 < 0) || (sx2 > width); |
|
227 |
final boolean yRangeError = (sy1 < 0) || (sy1 >= height) || (sy2 < 0) || (sy2 > height); |
|
228 |
||
229 |
if (xRangeError || yRangeError) |
|
230 |
{ |
|
231 |
final String info = "args=("+aX+','+aY+','+aWidth+','+aHeight+"), rect=("+sx1 +','+sy1+','+sx2+','+sy2+"), image width="+width+",height="+height; |
|
232 |
throw new IllegalArgumentException("createImage: Exceeding bounds of source image: " + info); |
|
233 |
} |
|
234 |
||
235 |
if (aTransform < Sprite.TRANS_NONE || aTransform > Sprite.TRANS_MIRROR_ROT90) |
|
236 |
{ |
|
237 |
throw new IllegalArgumentException("invalid transform"); |
|
238 |
} |
|
239 |
||
240 |
// |
|
241 |
// Check for trival case for immutable images. |
|
242 |
// |
|
243 |
if ((!aImage.isMutable()) && (aWidth == width) && (aHeight == height) && (aTransform == Sprite.TRANS_NONE)) |
|
244 |
{ |
|
245 |
return aImage; |
|
246 |
} |
|
247 |
||
248 |
try |
|
249 |
{ |
|
250 |
return new Image(aImage, aX, aY, aWidth, aHeight, aTransform); |
|
251 |
} |
|
252 |
catch (OutOfMemoryError oom) |
|
253 |
{ |
|
254 |
MemoryUtil.freeNativeMemory(); |
|
255 |
return new Image(aImage, aX, aY, aWidth, aHeight, aTransform); |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
// |
|
260 |
// API |
|
261 |
// |
|
262 |
public static Image createImage(InputStream aStream) throws java.io.IOException |
|
263 |
{ |
|
264 |
if (null == aStream) |
|
265 |
{ |
|
266 |
throw new NullPointerException(); |
|
267 |
} |
|
268 |
||
269 |
return createImageFromStream(aStream); |
|
270 |
} |
|
271 |
||
272 |
// |
|
273 |
// API |
|
274 |
// |
|
275 |
public static Image createImage(int aWidth,int aHeight) |
|
276 |
{ |
|
277 |
if (aWidth <= 0 || aHeight <= 0) |
|
278 |
{ |
|
279 |
throw new IllegalArgumentException(); |
|
280 |
} |
|
281 |
||
282 |
try |
|
283 |
{ |
|
284 |
return new Image(aWidth, aHeight, 0xffffffff); |
|
285 |
} |
|
286 |
catch (OutOfMemoryError oom) |
|
287 |
{ |
|
288 |
MemoryUtil.freeNativeMemory(); |
|
289 |
return new Image(aWidth, aHeight, 0xffffffff); |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
// |
|
294 |
// API |
|
295 |
// |
|
296 |
public static Image createImage(String aName) throws java.io.IOException |
|
297 |
{ |
|
298 |
if (aName == null) |
|
299 |
{ |
|
300 |
throw new NullPointerException(); |
|
301 |
} |
|
302 |
||
303 |
final Toolkit toolkit = Toolkit.getToolkit(); |
|
304 |
||
305 |
InputStream stream = toolkit.getResourceAsStream(aName); |
|
306 |
if (stream == null) |
|
307 |
{ |
|
308 |
// Retry that allows using relative name when image is in root level |
|
309 |
stream = toolkit.getResourceAsStream("/"+aName); |
|
310 |
if (stream == null) |
|
311 |
{ |
|
312 |
throw new IOException(); |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
try |
|
317 |
{ |
|
318 |
return createImageFromStream(stream); |
|
319 |
} |
|
320 |
finally |
|
321 |
{ |
|
322 |
stream.close(); |
|
323 |
} |
|
324 |
} |
|
325 |
||
326 |
public static Image createRGBImage(int[] aRgb, int aWidth, int aHeight, boolean aProcessAlpha) |
|
327 |
{ |
|
83
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
328 |
if (aRgb.length < (aWidth * aHeight)) |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
329 |
{ |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
330 |
throw new ArrayIndexOutOfBoundsException(); |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
331 |
} |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
332 |
|
66 | 333 |
if (aWidth <= 0 || aHeight <= 0) |
334 |
{ |
|
335 |
throw new IllegalArgumentException(); |
|
336 |
} |
|
337 |
||
338 |
try |
|
339 |
{ |
|
340 |
return new Image(aRgb, aWidth, aHeight, aProcessAlpha); |
|
341 |
} |
|
342 |
catch (OutOfMemoryError oom) |
|
343 |
{ |
|
344 |
MemoryUtil.freeNativeMemory(); |
|
345 |
return new Image(aRgb, aWidth, aHeight, aProcessAlpha); |
|
346 |
} |
|
347 |
} |
|
348 |
||
349 |
||
350 |
private static Image createImageFromStream(InputStream aStream) throws java.io.IOException |
|
351 |
{ |
|
352 |
// |
|
353 |
// maximum size of blocks to read from stream |
|
354 |
// |
|
355 |
final int blocksize = 4096; |
|
356 |
final byte[] buffer = new byte[blocksize]; |
|
357 |
||
358 |
// |
|
359 |
// create image loader to accept encoded image data. |
|
360 |
// |
|
361 |
final ImageLoader loader = new ImageLoader(Toolkit.getToolkit()); |
|
362 |
try |
|
363 |
{ |
|
364 |
int bytesRead = 0; |
|
365 |
||
366 |
// |
|
367 |
// read blocks from stream and append to decoder buffer |
|
368 |
// |
|
369 |
while ((bytesRead = aStream.read(buffer, 0, buffer.length)) != -1) |
|
370 |
{ |
|
371 |
if (loader.append(buffer, 0, bytesRead) == false) |
|
372 |
throw new IOException("loader.iHandle null: can't append more data"); |
|
373 |
} |
|
374 |
||
375 |
return loadImage(loader); |
|
376 |
} |
|
377 |
catch (Throwable t) |
|
378 |
{ |
|
379 |
if (t instanceof OutOfMemoryError) |
|
380 |
{ |
|
381 |
throw(OutOfMemoryError)t; |
|
382 |
} |
|
383 |
throw new IOException(); |
|
384 |
} |
|
385 |
finally |
|
386 |
{ |
|
387 |
loader.dispose(); |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
private static Image loadImage(ImageLoader aLoader) |
|
392 |
{ |
|
393 |
// |
|
394 |
// This is where the image is first decoded and a native representation allocated. |
|
395 |
// It is more likely to fail than the image instantiation, but more risky to retry. |
|
396 |
// |
|
397 |
||
398 |
// |
|
399 |
// REMOVED RETRY around the following line as it caused performance unit test |
|
400 |
// to panic. |
|
401 |
// |
|
402 |
aLoader.decode(); |
|
403 |
||
404 |
// |
|
405 |
// This is where device specific bitmaps are created if the image was decoded in |
|
406 |
// the wrong format. This is less likely to OOM than the decode above - but can |
|
407 |
// be retried. |
|
408 |
// |
|
409 |
try |
|
410 |
{ |
|
411 |
return new Image(aLoader); |
|
412 |
} |
|
413 |
catch (OutOfMemoryError oom) |
|
414 |
{ |
|
415 |
MemoryUtil.freeNativeMemory(); |
|
416 |
return new Image(aLoader); |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
public void getRGB(int[] aRgbData,int aOffset,int aScanLength,int aX,int aY,int aWidth,int aHeight) |
|
421 |
{ |
|
422 |
// |
|
423 |
// Image bounds |
|
424 |
// |
|
425 |
if (aWidth <= 0 || aHeight <= 0) |
|
426 |
{ |
|
427 |
return; |
|
428 |
} |
|
429 |
||
430 |
// |
|
431 |
// Array bounds |
|
432 |
// |
|
433 |
final long limit = (long)(aRgbData.length) - 1; // throws NullPointerException |
|
434 |
||
435 |
// |
|
436 |
// aScanLength may be negative in which case rows are written |
|
437 |
// into the buffer in reverse order. |
|
438 |
// |
|
439 |
long start; |
|
440 |
long end; |
|
441 |
||
442 |
if (aScanLength > 0) |
|
443 |
{ |
|
444 |
start = (long)aOffset; |
|
445 |
end = (long)aOffset + (long)aScanLength*((long)aHeight-1) + (long)aWidth - 1; |
|
446 |
} |
|
447 |
else |
|
448 |
{ |
|
449 |
end = (long)aOffset + (long)aWidth - 1; |
|
450 |
start = (long)aOffset + (long)aScanLength*((long)aHeight-1); |
|
451 |
} |
|
452 |
||
83
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
453 |
// |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
454 |
// aScanLength may be < 0, hence end < start is valid. |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
455 |
// |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
456 |
if ((start < 0) || (start > limit) || (end < 0) || (end > limit)) |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
457 |
{ |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
458 |
throw new ArrayIndexOutOfBoundsException("destination range exceeds array bounds"); |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
459 |
} |
26b2b12093af
Revision: v2.2.17
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
77
diff
changeset
|
460 |
|
66 | 461 |
final int width = iWidth; |
462 |
final int height = iHeight; |
|
463 |
||
464 |
final int sx1 = aX; // left column |
|
465 |
final int sx2 = aX + aWidth; // right column |
|
466 |
||
467 |
final int sy1 = aY; // top row (exclusive) |
|
468 |
final int sy2 = aY + aHeight; // bottom row (exclusive) |
|
469 |
||
470 |
// |
|
471 |
// Check source x range lies within source image |
|
472 |
// |
|
473 |
final boolean xRangeError = (sx1 < 0) || (sx1 >= width) || (sx2 < 0) || (sx2 > width); |
|
474 |
final boolean yRangeError = (sy1 < 0) || (sy1 >= height) || (sy2 < 0) || (sy2 > height); |
|
475 |
||
476 |
if (xRangeError || yRangeError) |
|
477 |
{ |
|
478 |
final String info = "args=("+aX+','+aY+','+aWidth+','+aHeight+"), rect=("+sx1 +','+sy1+','+sx2+','+sy2+"), image width="+width+",height="+height; |
|
479 |
throw new IllegalArgumentException("getRGB: Exceeding bounds of source image: " + info); |
|
480 |
} |
|
481 |
final int scanLength = Math.abs(aScanLength); |
|
482 |
||
483 |
if (scanLength < aWidth) |
|
484 |
{ |
|
485 |
throw new IllegalArgumentException("abs(Scanlength) should be >= width"); |
|
486 |
} |
|
487 |
synchronized (iToolkit) |
|
488 |
{ |
|
489 |
final int toolkit = iToolkit.getHandle(); |
|
490 |
final int handle = getHandle(true); |
|
491 |
||
492 |
NativeError.check(_getPixels(toolkit, handle, aRgbData, aOffset, aScanLength, aX, aY, aWidth, aHeight)); |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
/** |
|
497 |
*@returns a newly allocated thread-safe Graphics instance. |
|
498 |
*/ |
|
499 |
public Graphics getGraphics() |
|
500 |
{ |
|
501 |
if (!isMutable()) |
|
502 |
{ |
|
503 |
throw new IllegalStateException(); |
|
504 |
} |
|
505 |
return new Graphics(iToolkit, this, getHandle(false), iWidth, iHeight); |
|
506 |
} |
|
507 |
||
508 |
public int getWidth() |
|
509 |
{ |
|
510 |
return iWidth; |
|
511 |
} |
|
512 |
||
513 |
public int getHeight() |
|
514 |
{ |
|
515 |
return iHeight; |
|
516 |
} |
|
517 |
||
518 |
public boolean isMutable() |
|
519 |
{ |
|
520 |
return iMutable; |
|
521 |
} |
|
522 |
||
523 |
/** |
|
524 |
*@return true if this image has transparent pixels. |
|
525 |
* |
|
526 |
* May be used in game package to avoid native calls in collision |
|
527 |
* detection. |
|
528 |
*/ |
|
529 |
final boolean hasTransparency() |
|
530 |
{ |
|
531 |
return (TRANSPARENCY_NONE != iTransparency); |
|
532 |
} |
|
533 |
||
534 |
/** |
|
535 |
* Set the image transparency type. |
|
536 |
* |
|
537 |
* Added to support invalid requirement of Nokia UI API - infact API only |
|
538 |
* requires construction of mutable images with transparency - not after |
|
539 |
* the fact transformation of image transparency type. |
|
540 |
*/ |
|
541 |
void setTransparency(int aTransparency) |
|
542 |
{ |
|
543 |
if (aTransparency != TRANSPARENCY_ALPHA) |
|
544 |
{ |
|
545 |
throw new IllegalArgumentException(); |
|
546 |
} |
|
547 |
synchronized (iToolkit) |
|
548 |
{ |
|
549 |
NativeError.check(_setTransparencyType(iToolkit.getHandle(), getHandle(true), aTransparency)); |
|
550 |
iTransparency = aTransparency; |
|
551 |
} |
|
552 |
} |
|
553 |
||
554 |
/** |
|
555 |
*@return the peer handle, optionally flushing and syncing the graphics buffer. |
|
556 |
* |
|
557 |
* If pixel data from mutable images is accessed natively without syncing the |
|
558 |
* graphics buffer there is a chance that commands that would effect the |
|
559 |
* pixels read have not yet been executed. |
|
560 |
*/ |
|
561 |
final int getHandle(boolean aSync) |
|
562 |
{ |
|
563 |
if (aSync && isMutable()) |
|
564 |
{ |
|
565 |
iToolkit.sync(); |
|
566 |
} |
|
567 |
return iHandle; |
|
568 |
} |
|
569 |
||
570 |
/** |
|
571 |
* destroy native image. |
|
572 |
* Synchronization prevents Image to be disposed when in use. |
|
573 |
*/ |
|
574 |
synchronized private void dispose() |
|
575 |
{ |
|
576 |
boolean freed = false; |
|
577 |
synchronized (iToolkit) |
|
578 |
{ |
|
579 |
if (iHandle != 0) |
|
580 |
{ |
|
581 |
iToolkit.disposeObject(iHandle); |
|
582 |
iHandle = 0; |
|
583 |
freed = true; |
|
584 |
} |
|
585 |
} |
|
586 |
if (freed) |
|
587 |
{ |
|
588 |
MemoryUtil.nativeMemoryFreed(iSizeBytes); |
|
589 |
} |
|
590 |
} |
|
591 |
||
592 |
private void doFinalize() |
|
593 |
{ |
|
594 |
if (mFinalizer != null) |
|
595 |
{ |
|
596 |
registeredFinalize(); |
|
597 |
mFinalizer = null; |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
void registeredFinalize() |
|
602 |
{ |
|
603 |
dispose(); |
|
604 |
} |
|
605 |
||
606 |
static boolean collision |
|
607 |
( |
|
608 |
Image aImage1, int aS1X1, int aS1Y1, int aS1X2, int aS1Y2, int aTransform1, int aD1X, int aD1Y, |
|
609 |
Image aImage2, int aS2X1, int aS2Y1, int aS2X2, int aS2Y2, int aTransform2, int aD2X, int aD2Y |
|
610 |
) |
|
611 |
{ |
|
612 |
final int result; |
|
613 |
synchronized (aImage1.iToolkit) |
|
614 |
{ |
|
615 |
final int toolkit = aImage1.iToolkit.getHandle(); |
|
616 |
final int image1 = aImage1.getHandle(false); |
|
617 |
final int image2 = aImage2.getHandle(false); |
|
618 |
result = _collision( |
|
619 |
toolkit, |
|
620 |
image1, aS1X1, aS1Y1, aS1X2, aS1Y2, aTransform1, aD1X, aD1Y, |
|
621 |
image2, aS2X1, aS2Y1, aS2X2, aS2Y2, aTransform2, aD2X, aD2Y |
|
622 |
); |
|
623 |
} |
|
624 |
return (result > 0); |
|
625 |
} |
|
626 |
||
627 |
||
628 |
/** |
|
629 |
* copies pixels from source image to destination image, transforming as required. |
|
630 |
*/ |
|
631 |
private void copyRegion(Image aSource, int aX, int aY, int aWidth, int aHeight, int aTransform) |
|
632 |
{ |
|
633 |
synchronized (iToolkit) |
|
634 |
{ |
|
635 |
final int toolkit = iToolkit.getHandle(); |
|
636 |
final int target = getHandle(false); |
|
637 |
final int source = aSource.getHandle(true); |
|
638 |
NativeError.check(_copyRegion(toolkit, target, source, aX, aY, aWidth, aHeight, aTransform)); |
|
639 |
} |
|
640 |
} |
|
641 |
||
642 |
/** |
|
643 |
* writes pixels from aArray[0..(aWidth*aHeight-1)] into image, converting from |
|
644 |
* ARGB format to the image pixel format. |
|
645 |
*/ |
|
646 |
private void setRGB(int[] aRgbArray, int aOffset, int aScanLength, int aX, int aY, int aWidth, int aHeight, boolean aAlpha) |
|
647 |
{ |
|
648 |
synchronized (iToolkit) |
|
649 |
{ |
|
650 |
final int toolkit = iToolkit.getHandle(); |
|
651 |
final int handle = getHandle(false); |
|
652 |
NativeError.check(_setPixels(toolkit, handle, aRgbArray, aOffset, aScanLength, aX, aY ,aWidth, aHeight, aAlpha)); |
|
653 |
} |
|
654 |
} |
|
655 |
||
656 |
private void setAttributes(int[] aAttribs, boolean aMutable) |
|
657 |
{ |
|
658 |
iWidth = aAttribs[ATTRIB_WIDTH]; |
|
659 |
iHeight = aAttribs[ATTRIB_HEIGHT]; |
|
660 |
iTransparency = aAttribs[ATTRIB_TYPE]; |
|
661 |
iSizeBytes = aAttribs[ATTRIB_SIZE]; |
|
662 |
iMutable = aMutable; |
|
663 |
MemoryUtil.nativeMemoryAllocated(iSizeBytes); |
|
664 |
} |
|
665 |
||
666 |
private void checkToolkit(Toolkit aToolkit) |
|
667 |
{ |
|
668 |
if (aToolkit != iToolkit) |
|
669 |
{ |
|
670 |
throw new RuntimeException("Toolkit mismatch"); |
|
671 |
} |
|
672 |
} |
|
673 |
||
674 |
/** |
|
675 |
* Native constructor. |
|
676 |
*@param aToolkit pointer to native toolkit instance |
|
677 |
*@param aWidth image width, may be zero if unknown |
|
678 |
*@param aHeight image height, may be zero if unknown |
|
679 |
*@param aColor image initial color, may be (semi) transparent |
|
680 |
*@param aAttribs returns image attribs [width, height, transparency] |
|
681 |
*/ |
|
682 |
private native int _createMutable(int aToolkit, int aWidth, int aHeight, int aColor, int[] aAttribs); |
|
683 |
||
684 |
/** |
|
685 |
*@param aToolkit handle to native toolkit instance |
|
686 |
*@param aLoader handle to image loader containing decoded pixel data. |
|
687 |
*@param aAttribs returns image attribs [width, height, transparency] |
|
688 |
*/ |
|
689 |
private native int _createImmutable(int aToolkit, int aLoader, int[] aAttribs); |
|
690 |
||
691 |
/** |
|
692 |
*@param aToolkit handle |
|
693 |
*@param aCanvas handle of canvas with framebuffer bitmap |
|
694 |
*@param aAttribs returns image attribs [width, height, transparency] |
|
695 |
*/ |
|
696 |
private native int _createFromCanvas(int aToolkit, int aCanvas, int[] aAttribs); |
|
697 |
||
698 |
/** |
|
699 |
*@param aToolkit toolkit handle |
|
700 |
*@param aSource source image handle |
|
701 |
*@param aX source region x |
|
702 |
*@param aY source region y |
|
703 |
*@param aWidth source region width |
|
704 |
*@param aHeight source region height |
|
705 |
*@param aTransform transform to apply to source region |
|
706 |
*@param aAttribs returns image attribs [width, height, transparency] |
|
707 |
*/ |
|
708 |
private native int _createRegion(int aToolkit, int aSource, int aX, int aY, int aWidth, int aHeight, int aTransform, int[] aAttribs); |
|
709 |
||
710 |
/** |
|
711 |
*@param aToolkit toolkit handle |
|
712 |
*@param aArray pixel data array |
|
713 |
*@param aWidth pixel data width |
|
714 |
*@param aHeight pixel data height |
|
715 |
*@param aAlpha true if alpha component of pixel data is to be interpreted. |
|
716 |
*@param aAttribs returns image attribs [width, height, transparency] |
|
717 |
*/ |
|
718 |
private native int _createRGB(int aToolkit, int[] aArray, int aWidth, int aHeight, boolean aAlpha, int[] aAttribs); |
|
719 |
||
720 |
/** |
|
721 |
* |
|
722 |
*/ |
|
723 |
private native int _copyRegion(int aToolkit, int aTarget, int aSource, int aX, int aY, int aWidth, int aHeight, int aTransform); |
|
724 |
||
725 |
/** |
|
726 |
* |
|
727 |
*/ |
|
728 |
private native int _setPixels(int aToolkit, int aHandle, int[] aRgbArray, int aOffset, int aScanLength, int aX, int aY, int aWidth, int aHeight, boolean aAlpha); |
|
729 |
||
730 |
/** |
|
731 |
* |
|
732 |
*/ |
|
733 |
private native int _getPixels(int aToolkit, int aHandle, int[] aRgbArray, int aOffset, int aScanlength, int aX, int aY, int aWidth, int aHeight); |
|
734 |
||
735 |
/** |
|
736 |
*@return -ve on error, 1 for true 0 for false. |
|
737 |
*/ |
|
738 |
private static native int _collision |
|
739 |
( |
|
740 |
int aToolkit, |
|
741 |
int aImage1, int aS1X1, int aS1Y1, int aS1X2, int aS1Y2, int aTransform1, int aD1X, int aD1Y, |
|
742 |
int aImage2, int aS2X1, int aS2Y1, int aS2X2, int aS2Y2, int aTransform2, int aD2X, int aD2Y |
|
743 |
); |
|
744 |
||
745 |
private native int _setTransparencyType(int aToolkit, int aImage, int aType); |
|
746 |
} |