ImageDataFactorypublic class ImageDataFactory extends Object implements AbstractImageDataFactoryCreates ImageData based on platform decoder and storage. |
Fields Summary |
---|
private static ImageDataFactory | imageDataFactorySingleton ImageDataFactory instance. |
Methods Summary |
---|
public ImageData | createImmutableCopy(ImageData mutableSource)Creates an immutable image from a source mutable image.
This method is useful for placing the contents of mutable images
into Choice objects. The application can create
an off-screen image
using the
{@link #createImage(int, int) createImage(w, h)}
method, draw into it using a Graphics object
obtained with the
{@link #getGraphics() getGraphics()}
method, and then create an immutable copy of it with this method.
The immutable copy may then be placed into Choice
objects.
ImageData data = new ImageData(mutableSource.getWidth(),
mutableSource.getHeight(),
false);
// Duplicate mutable image contents
try {
createImmutableImageDataCopy(data, mutableSource);
} catch(OutOfMemoryError e) {
garbageCollectImages(false);
try {
createImmutableImageDataCopy(data, mutableSource);
} catch(OutOfMemoryError e2) {
garbageCollectImages(true);
createImmutableImageDataCopy(data, mutableSource);
}
}
return data;
| public ImageData | createImmutableImageData(byte[] imageBytes, int imageOffset, int imageLength)Creates an immutable image which is decoded from the data stored in
the specified byte array at the specified offset and length. The data
must be in a self-identifying image file format supported by the
implementation, such as PNG.
The imageoffset and imagelength
parameters specify a range of
data within the imageData byte array. The
imageOffset parameter
specifies the
offset into the array of the first data byte to be used. It must
therefore lie within the range
[0..(imageData.length-1)] . The
imageLength
parameter specifies the number of data bytes to be used. It must be a
positive integer and it must not cause the range to extend beyond
the end
of the array. That is, it must be true that
imageOffset + imageLength < imageData.length .
This method is intended for use when loading an
image from a variety of sources, such as from
persistent storage or from the network.
ImageData data = new ImageData();
// width, height and native data will be set below
try {
createImmutableImageDecodeImage(data, imageBytes, imageOffset,
imageLength);
} catch(OutOfMemoryError e) {
garbageCollectImages(false);
try {
createImmutableImageDecodeImage(data,
imageBytes, imageOffset,
imageLength);
} catch(OutOfMemoryError e2) {
garbageCollectImages(true);
createImmutableImageDecodeImage(data, imageBytes, imageOffset,
imageLength);
}
}
return data;
| public ImageData | createImmutableImageData(ImageData dataSource, int x, int y, int width, int height, int transform)Creates an immutable image using pixel data from the specified
region of a source image, transformed as specified.
The source image may be mutable or immutable. For immutable source
images, transparency information, if any, is copied to the new
image unchanged.
On some devices, pre-transformed images may render more quickly
than images that are transformed on the fly using
drawRegion .
However, creating such images does consume additional heap space,
so this technique should be applied only to images whose rendering
speed is critical.
The transform function used must be one of the following, as defined
in the {@link javax.microedition.lcdui.game.Sprite Sprite} class:
Sprite.TRANS_NONE - causes the specified image
region to be copied unchanged
Sprite.TRANS_ROT90 - causes the specified image
region to be rotated clockwise by 90 degrees.
Sprite.TRANS_ROT180 - causes the specified image
region to be rotated clockwise by 180 degrees.
Sprite.TRANS_ROT270 - causes the specified image
region to be rotated clockwise by 270 degrees.
Sprite.TRANS_MIRROR - causes the specified image
region to be reflected about its vertical center.
Sprite.TRANS_MIRROR_ROT90 - causes the specified image
region to be reflected about its vertical center and then rotated
clockwise by 90 degrees.
Sprite.TRANS_MIRROR_ROT180 - causes the specified image
region to be reflected about its vertical center and then rotated
clockwise by 180 degrees.
Sprite.TRANS_MIRROR_ROT270 - causes the specified image
region to be reflected about its vertical center and then rotated
clockwise by 270 degrees.
The size of the returned image will be the size of the specified region
with the transform applied. For example, if the region is
100 x 50 pixels and the transform is
TRANS_ROT90 , the
returned image will be 50 x 100 pixels.
Note: If all of the following conditions
are met, this method may
simply return the source Image without creating a
new one:
- the source image is immutable;
- the region represents the entire source image; and
- the transform is
TRANS_NONE .
ImageData dataDest;
if ((transform & Image.TRANSFORM_SWAP_AXIS) != 0x0) {
dataDest = new ImageData(height, width, false);
} else {
dataDest = new ImageData(width, height, false);
}
// Copy native data from the source region
try {
createImmutableImageDataRegion(dataDest, dataSource,
x, y, width, height,
transform,
dataSource.isMutable());
} catch(OutOfMemoryError e) {
garbageCollectImages(false);
try {
createImmutableImageDataRegion(dataDest,
dataSource,
x, y, width, height,
transform,
dataSource.isMutable());
} catch(OutOfMemoryError e2) {
garbageCollectImages(true);
createImmutableImageDataRegion(dataDest, dataSource,
x, y, width, height,
transform,
dataSource.isMutable());
}
}
return dataDest;
| public ImageData | createImmutableImageData(java.io.InputStream stream)Creates an immutable image from decoded image data obtained from an
InputStream . This method blocks until all image data has
been read and decoded. After this method completes (whether by
returning or by throwing an exception) the stream is left open and its
current position is undefined.
ImageData data = new ImageData();
// width, height and native data will be set below
try {
getImageDataFromStream(data, stream);
} catch(OutOfMemoryError e) {
garbageCollectImages(false);
try {
getImageDataFromStream(data, stream);
} catch(OutOfMemoryError e2) {
garbageCollectImages(true);
getImageDataFromStream(data, stream);
}
}
return data;
| public ImageData | createImmutableImageData(int[] rgb, int width, int height, boolean processAlpha)Creates an immutable image from a sequence of ARGB values, specified
as 0xAARRGGBB .
The ARGB data within the rgb array is arranged
horizontally from left to right within each row,
row by row from top to bottom.
If processAlpha is true ,
the high-order byte specifies opacity; that is,
0x00RRGGBB specifies
a fully transparent pixel and 0xFFRRGGBB specifies
a fully opaque
pixel. Intermediate alpha values specify semitransparency. If the
implementation does not support alpha blending for image rendering
operations, it must replace any semitransparent pixels with fully
transparent pixels. (See Alpha Processing
for further discussion.) If processAlpha is
false , the alpha values
are ignored and all pixels must be treated as fully opaque.
Consider P(a,b) to be the value of the pixel
located at column a and row b of the
Image, where rows and columns are numbered downward from the
top starting at zero, and columns are numbered rightward from
the left starting at zero. This operation can then be defined
as:
P(a, b) = rgb[a + b * width];
|
for
0 <= a < width
0 <= b < height
|
ImageData data = new ImageData(width, height, false);
// create native image data below
try {
createImmutableImageDecodeRGBImage(data, rgb,
width, height,
processAlpha);
} catch(OutOfMemoryError e) {
garbageCollectImages(false);
try {
createImmutableImageDecodeRGBImage(data, rgb,
width, height,
processAlpha);
} catch(OutOfMemoryError e2) {
garbageCollectImages(true);
createImmutableImageDecodeRGBImage(data, rgb,
width, height,
processAlpha);
}
}
return data;
| public ImageData | createImmutableImageData(int imageDataArrayPtr, int imageDataArrayLength)Create a immutable image from romized image data.
ImageData data = new ImageData();
// width, height and native image data will be set below
if (!loadRomizedImage(data, imageDataArrayPtr,
imageDataArrayLength)) {
throw new IllegalArgumentException();
}
return data;
| private native void | createImmutableImageDataCopy(ImageData dest, ImageData source)Native function to create an immutable copy of an image data.
| private native void | createImmutableImageDataRegion(ImageData dataDest, ImageData dataSource, int x, int y, int width, int height, int transform, boolean isMutable)Native function that creates an immutable image data from
a region of another image data, applying the given transform
| private native void | createImmutableImageDecodeImage(ImageData data, byte[] inputData, int offset, int length)Native function to decode an ImageData from a byte array
| private native void | createImmutableImageDecodeRGBImage(ImageData data, int[] inputData, int width, int height, boolean processAlpha)Native function to decode an ImageData from an array of RGB data
| private native void | createMutableImageData(ImageData data, int width, int height)Create a mutable image data
| public ImageData | createOffScreenImageData(int width, int height)Creates a new, mutable image for off-screen drawing. Every pixel
within the newly created image is white. The width and height of the
image must both be greater than zero.
ImageData data = new ImageData(width, height, true);
// Create native image data and store its pointer in nativeImageData
try {
createMutableImageData(data, width, height);
} catch(OutOfMemoryError e) {
garbageCollectImages(false);
try {
createMutableImageData(data, width, height);
} catch(OutOfMemoryError e2) {
garbageCollectImages(true);
createMutableImageData(data, width, height);
}
}
return data;
| public ImageData | createResourceImageData(java.lang.String name)Creates an immutable image from decoded image data obtained from the
named resource. The name parameter is a resource name as defined by
{@link Class#getResourceAsStream(String)
Class.getResourceAsStream(name)}. The rules for resolving resource
names are defined in the
Application Resource Files section of the
java.lang package documentation.
if (name == null) {
throw new java.lang.NullPointerException();
}
ImageData data = new ImageData();
// width, height and native data will be set below
/*
* Load native image data from cache and create
* image, if available. If image is not cached,
* proceed to load and create image normally.
*/
if (loadAndCreateImmutableImageDataFromCache(data, name)) {
return data;
}
/*
* allocate an array and read in the bits using
* Class.getResourceAsStream(name);
*/
InputStream is = ImageData.class.getResourceAsStream(name);
/*
* If the InputStream "is" is null, when "name" is
* is not null, throw an IOException, not a NullPointerException
*/
if (is == null) {
throw new java.io.IOException();
}
try {
getImageDataFromStream(data, is);
} catch(OutOfMemoryError e) {
garbageCollectImages(false);
try {
getImageDataFromStream(data, is);
} catch(OutOfMemoryError e2) {
garbageCollectImages(true);
getImageDataFromStream(data, is);
}
}
return data;
| private static native void | garbageCollectImages(boolean doFullGC)Garbage collected to free native resources from zombie images.
| public static AbstractImageDataFactory | getImageDataFactory()Returns the singleton ImageDataFactory instance.
return imageDataFactory;
| private void | getImageDataFromStream(ImageData data, java.io.InputStream istream)helper function called by the create functions above.
Upon return, the input stream will be closed.
int blocksize = 4096; // the size of blocks to read and allocate
/*
* Allocate an array assuming available is correct.
* Only reading an EOF is the real end of file
* so allocate an extra byte to read the EOF into.
* If available is incorrect, increase the buffer
* size and keep reading.
*/
int l = istream.available();
byte[] buffer = new byte[l+1];
int length = 0;
// TBD: Guard against an implementation with incorrect available
while ((l = istream.read(buffer, length,
buffer.length-length)) != -1) {
length += l;
if (length == buffer.length) {
byte[] b = new byte[buffer.length + blocksize];
System.arraycopy(buffer, 0, b, 0, length);
buffer = b;
}
}
try {
createImmutableImageDecodeImage(data, buffer, 0, length);
} catch (IllegalArgumentException iae) {
// Data cannot be not decoded
throw new java.io.IOException();
} finally {
istream.close();
}
| private boolean | loadAndCreateImmutableImageDataFromCache(ImageData data, java.lang.String resName)Load and create image data from cache. The real work is done in
the native function.
MIDletSuite midletSuite =
MIDletStateHandler.getMidletStateHandler().getMIDletSuite();
int suiteId = midletSuite.getID();
try {
return loadAndCreateImmutableImageDataFromCache0(data,
suiteId, resName);
} catch(OutOfMemoryError e) {
garbageCollectImages(false);
try {
return loadAndCreateImmutableImageDataFromCache0(data,
suiteId,
resName);
} catch(OutOfMemoryError e2) {
garbageCollectImages(true);
return loadAndCreateImmutableImageDataFromCache0(data,
suiteId,
resName);
}
}
| private native boolean | loadAndCreateImmutableImageDataFromCache0(ImageData data, int suiteId, java.lang.String resName)Native function to load native image data from cache and create
an immutable image.
| private native boolean | loadRomizedImage(ImageData data, int imageDataArrayPtr, int imageDataArrayPtrLength)Native function to load an ImageData directly out of the rom image.
|
|