Revision: 2474
http://sourceforge.net/p/swingme/code/2474
Author: yuranet
Date: 2021-06-24 23:21:17 +0000 (Thu, 24 Jun 2021)
Log Message:
-----------
can load images
Modified Paths:
--------------
iOSME/src/javax/microedition/lcdui/Image.java
Modified: iOSME/src/javax/microedition/lcdui/Image.java
===================================================================
--- iOSME/src/javax/microedition/lcdui/Image.java 2021-06-24 10:21:37 UTC (rev 2473)
+++ iOSME/src/javax/microedition/lcdui/Image.java 2021-06-24 23:21:17 UTC (rev 2474)
@@ -6,11 +6,15 @@
import java.io.OutputStream;
import javax.microedition.lcdui.game.Sprite;
import android.graphics.ColorMatrix;
+import org.moe.natj.general.ptr.BytePtr;
+import org.moe.natj.general.ptr.impl.PtrFactory;
import apple.coregraphics.opaque.CGContextRef;
import apple.coregraphics.struct.CGSize;
+import apple.foundation.NSData;
import apple.uikit.UIImage;
import apple.uikit.c.UIKit;
import apple.coregraphics.c.CoreGraphics;
+import libcore.io.Streams;
/**
* for saving of image please use
@@ -18,8 +22,65 @@
*/
public class Image {
- private UIImage bitmap;
+ private UIImage uiImage;
+ public static Image createImage(String resource) throws IOException {
+
+ // try load any image that may be in a group
+ UIImage img = UIImage.imageNamed(resource.startsWith("/") ? resource.substring(1) : resource);
+ if (img != null) {
+ return new Image(img);
+ }
+
+ // for images that are copied during build step, they are in a folder not a group, so we need to have a folder name
+ // https://stackoverflow.com/questions/11499925/uiimage-imagenamed-does-not-find-image-within-blue-referenced-xcode-folder
+ img = UIImage.imageNamed("Graphics" + resource);
+
+ if (img != null) {
+ return new Image(img);
+ }
+
+/* can do this with newer versions of java
+ URL url = Image.class.getResource(resource);
+ if (url != null) {
+ try {
+ Path path = Paths.get(url.toURI());
+ byte[] bytes = Files.readAllBytes(path);
+
+ // https://discuss.multi-os-engine.org/t/stream-to-nsdata-and-moe-pointer-magic/306
+ BytePtr ptr = PtrFactory.newByteArray(bytes);
+ NSData data = NSData.dataWithBytesLength( ptr, bytes.length );
+ UIImage image = UIImage.imageWithData( data );
+ return new Image(image);
+ }
+ catch (Exception ex) {
+ throw new IOException(ex);
+ }
+ }
+*/
+
+ InputStream in = Image.class.getResourceAsStream(resource);
+ if (in == null) {
+ throw new IOException("can not find: " + resource);
+ }
+ return createImage(in);
+ }
+
+ public static Image createImage(InputStream stream) throws IOException {
+ int available = stream.available();
+ int size = Math.max(available, 8 * 1024);
+ BufferedInputStream buffInput = new BufferedInputStream(stream, size);
+
+ //byte[] bytes = stream.readAllBytes?(); // added in java 9
+ byte[] bytes = Streams.readFullyNoClose(buffInput);
+
+ // https://discuss.multi-os-engine.org/t/stream-to-nsdata-and-moe-pointer-magic/306
+ BytePtr ptr = PtrFactory.newByteArray(bytes);
+ NSData data = NSData.dataWithBytesLength(ptr, bytes.length);
+ UIImage image = UIImage.imageWithData(data);
+ return new Image(image);
+ }
+
public static Image createImage(Image image, int x, int y, int width, int height, int transform) {
if (image == null) {
throw new NullPointerException();
@@ -38,7 +99,7 @@
UIKit.UIGraphicsBeginImageContextWithOptions(CoreGraphics.CGSizeMake(width, height), false, 1.0);
bmp = UIKit.UIGraphicsGetImageFromCurrentImageContext();
- //bmp = Bitmap.createBitmap(image.bitmap, x, y, width, height);
+ //bmp = Bitmap.createBitmap(image.uiImage, x, y, width, height);
res = new Image(bmp);
@@ -47,13 +108,13 @@
case Sprite.TRANS_ROT270:
case Sprite.TRANS_MIRROR_ROT90:
case Sprite.TRANS_MIRROR_ROT270: {
- //res = Image.createImage(height, width, image.bitmap.getConfig());
+ //res = Image.createImage(height, width, image.uiImage.getConfig());
throw new UnsupportedOperationException();
//break;
}
default: {
throw new UnsupportedOperationException();
- //res = Image.createImage(width, height, image.bitmap.getConfig());
+ //res = Image.createImage(width, height, image.uiImage.getConfig());
}
}
@@ -65,43 +126,14 @@
return res;
}
- public static Image createImage(InputStream stream) throws IOException {
- UIImage bitmap = null;
-
- int available = stream.available();
- int size = Math.max(available, 8 * 1024);
- BufferedInputStream buffInput = new BufferedInputStream(stream, size);
/*
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inPreferredConfig = Bitmap.Config.ARGB_8888;
- options.inDither = false;
- options.inPurgeable = true; // http://www.droidnova.com/2d-sprite-animation-in-android-addendum,505.html
-
- OutOfMemoryError error = null;
- try {
- bitmap = BitmapFactory.decodeStream(buffInput,null,options);
- } catch (OutOfMemoryError e) {
- cleanMem();
- error = e;
- buffInput.reset();
- buffInput.mark(1024);
- bitmap = BitmapFactory.decodeStream(buffInput,null,options);
- }
-
- if (bitmap == null) {
- throw new IOException("BitmapFactory.decodeStream returned null " + stream + " available=" + available + " " + options.outMimeType + " " + options.outWidth + "x" + options.outHeight + " " + error);
- }
-*/
- return new Image(bitmap);
- }
-/*
private static Image createImage(int width, int height, Config config) {
- Bitmap bitmap;
+ Bitmap uiImage;
OutOfMemoryError error=null;
try {
- bitmap = Bitmap.createBitmap(width, height, config);
+ uiImage = Bitmap.createBitmap(width, height, config);
// for some reason this sometimes just returns null instead of throwing a error
- if (bitmap==null) {
+ if (uiImage==null) {
throw new OutOfMemoryError("Bitmap.createBitmap returned null");
}
}
@@ -108,12 +140,12 @@
catch (OutOfMemoryError e) {
cleanMem();
error = e;
- bitmap = Bitmap.createBitmap(width, height, config);
+ uiImage = Bitmap.createBitmap(width, height, config);
}
- if (bitmap==null) {
+ if (uiImage==null) {
throw new OutOfMemoryError("Bitmap.createBitmap returned null for w="+width+" h="+height+" config="+config+" error="+error );
}
- return new Image(bitmap);
+ return new Image(uiImage);
}
*/
public static Image createImage(int width, int height) {
@@ -128,15 +160,6 @@
throw new UnsupportedOperationException();
}
- public static Image createImage(String resource) throws IOException {
-
- InputStream in = Image.class.getResourceAsStream(resource);
- if (in == null) {
- throw new IOException("can not find: "+resource);
- }
- return createImage(in);
- }
-
public static Image createImage(byte[] imgData, int offset, int length) {
/*
BitmapFactory.Options opts = new BitmapFactory.Options();
@@ -147,17 +170,17 @@
// http://www.droidnova.com/2d-sprite-animation-in-android-addendum,505.html
}
- Bitmap bitmap;
+ Bitmap uiImage;
try {
- bitmap = BitmapFactory.decodeByteArray(imgData, offset, length, opts);
+ uiImage = BitmapFactory.decodeByteArray(imgData, offset, length, opts);
} catch (OutOfMemoryError e) {
cleanMem();
- bitmap = BitmapFactory.decodeByteArray(imgData, offset, length, opts);
+ uiImage = BitmapFactory.decodeByteArray(imgData, offset, length, opts);
}
- if (bitmap==null) {
+ if (uiImage==null) {
return null;
}
- return new Image(bitmap);
+ return new Image(uiImage);
*/
throw new UnsupportedOperationException();
}
@@ -165,41 +188,41 @@
public static final Image createRGBImage(int[] rgb, int width, int height, boolean processAlpha) {
/*
Bitmap.Config config = (processAlpha) ? Bitmap.Config.ARGB_4444 : Bitmap.Config.RGB_565;
- Bitmap bitmap;
+ Bitmap uiImage;
try {
- bitmap = Bitmap.createBitmap(rgb, width, height, config);
+ uiImage = Bitmap.createBitmap(rgb, width, height, config);
} catch (OutOfMemoryError e) {
cleanMem();
- bitmap = Bitmap.createBitmap(rgb, width, height, config);
+ uiImage = Bitmap.createBitmap(rgb, width, height, config);
}
- return new Image(bitmap);
+ return new Image(uiImage);
*/
throw new UnsupportedOperationException();
}
- private Image(UIImage bitmap) {
- if (bitmap == null) {
+ private Image(UIImage img) {
+ if (img == null) {
throw new NullPointerException();
}
- this.bitmap = bitmap;
+ this.uiImage = img;
}
public UIImage getUIImage() {
- return bitmap;
+ return uiImage;
}
public int getWidth() {
- return (int)bitmap.size().width();
+ return (int) uiImage.size().width();
}
public int getHeight() {
- return (int)bitmap.size().height();
+ return (int) uiImage.size().height();
}
public Graphics getGraphics() {
- //if (!bitmap.isMutable()) {
+ //if (!uiImage.isMutable()) {
// throw new IllegalStateException("Image is immutable");
//}
@@ -215,23 +238,23 @@
}
public void getRGB(int[] rgb, int offset, int scanlength, int x, int y, int width, int height) {
- //bitmap.getPixels(rgb, offset, scanlength, x, y, width, height);
+ //uiImage.getPixels(rgb, offset, scanlength, x, y, width, height);
throw new UnsupportedOperationException();
}
public boolean isMutable() {
- //return bitmap.isMutable();
+ //return uiImage.isMutable();
throw new UnsupportedOperationException();
}
public void setRGB(int x, int y, int color) {
- //bitmap.setPixel(x, y, color);
+ //uiImage.setPixel(x, y, color);
throw new UnsupportedOperationException();
}
@Override
public String toString() {
- return "Image{"+bitmap.toString()+"}";
+ return "Image{"+ uiImage.toString()+"}";
}
public static void filter(Image source, Image bm, ColorMatrix cm) {
@@ -238,7 +261,7 @@
/*
android.graphics.Paint paint = new android.graphics.Paint();
paint.setColorFilter(new android.graphics.ColorMatrixColorFilter(cm));
- new Canvas(bm.bitmap).drawBitmap(source.bitmap, 0, 0, paint);
+ new Canvas(bm.uiImage).drawBitmap(source.uiImage, 0, 0, paint);
*/
throw new UnsupportedOperationException();
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|