diff --git a/src/javax/microedition/lcdui/Image.java b/src/javax/microedition/lcdui/Image.java index a8cecd90..79487759 100644 --- a/src/javax/microedition/lcdui/Image.java +++ b/src/javax/microedition/lcdui/Image.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStream; +import javax.microedition.lcdui.game.Sprite; import org.recompile.mobile.Mobile; import org.recompile.mobile.PlatformImage; @@ -32,13 +33,13 @@ public class Image public int width; public int height; - public static Image createImage(byte[] imageData, int imageOffset, int imageLength) + public static Image createImage(byte[] imageData, int imageOffset, int imageLength) throws IllegalArgumentException { //System.out.println("Create Image from image data "); if (imageData == null) {throw new NullPointerException();} if (imageOffset + imageLength > imageData.length) {throw new ArrayIndexOutOfBoundsException();} + PlatformImage t = new PlatformImage(imageData, imageOffset, imageLength); - if(t.isNull) { throw new IllegalArgumentException(); } return t; } @@ -46,6 +47,9 @@ public static Image createImage(Image source) { //System.out.println("Create Image from Image "); if (source == null) {throw new NullPointerException();} + // If the source is immutable, just return it, despite the docs not mentioning it directly + if (!source.isMutable()) { return source; } + return new PlatformImage(source); } @@ -55,15 +59,17 @@ public static Image createImage(Image img, int x, int y, int width, int height, if (img == null) {throw new NullPointerException();} if (x+width > img.getWidth() || y+height > img.getHeight()) {throw new IllegalArgumentException();} if (width <= 0 || height <= 0) {throw new IllegalArgumentException();} + + if(!img.isMutable() && (x+width == img.getWidth() && y+height == img.getHeight()) && transform == Sprite.TRANS_NONE) { return img; } + return new PlatformImage(img, x, y, width, height, transform); } - public static Image createImage(InputStream stream) throws IOException + public static Image createImage(InputStream stream) throws IOException, IllegalArgumentException { //System.out.println("Create Image stream"); if (stream == null) {throw new NullPointerException();} PlatformImage t = new PlatformImage(stream); - if(t.isNull) { throw new IOException(); } return t; } @@ -79,7 +85,6 @@ public static Image createImage(String name) throws IOException //System.out.println("Create Image " + name); if (name == null) {throw new NullPointerException();} PlatformImage t = new PlatformImage(name); - if(t.isNull) { throw new IOException(); } return t; } diff --git a/src/org/recompile/mobile/PlatformImage.java b/src/org/recompile/mobile/PlatformImage.java index 1f9f7c7e..59ee1373 100644 --- a/src/org/recompile/mobile/PlatformImage.java +++ b/src/org/recompile/mobile/PlatformImage.java @@ -37,8 +37,6 @@ public class PlatformImage extends javax.microedition.lcdui.Image protected BufferedImage canvas; protected PlatformGraphics gc; - public boolean isNull = false; - public BufferedImage getCanvas() { return canvas; @@ -79,30 +77,21 @@ public PlatformImage(String name) InputStream stream = Mobile.getPlatform().loader.getMIDletResourceAsStream(name); - if(stream==null) - { - System.out.println("Couldn't Load Image Stream (can't find "+name+")"); - isNull = true; - } + if(stream==null) { throw new NullPointerException("Can't load image from resource, as the returned image is null."); } else { - try - { - temp = ImageIO.read(stream); - width = (int)temp.getWidth(); - height = (int)temp.getHeight(); - - canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - createGraphics(); - - gc.drawImage2(temp, 0, 0); - } - catch (Exception e) - { - System.out.println("Couldn't Load Image Stream " + name); - e.printStackTrace(); - isNull = true; - } + try { temp = ImageIO.read(stream); } + catch (IOException e) { throw new IllegalArgumentException("Failed to read image from resource:" + e.getMessage()); } + + if(temp == null) { throw new NullPointerException("Couldn't load image from resource: Image is null"); } + + width = (int)temp.getWidth(); + height = (int)temp.getHeight(); + + canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + createGraphics(); + + gc.drawImage2(temp, 0, 0); } platformImage = this; } @@ -112,22 +101,19 @@ public PlatformImage(InputStream stream) // Create Image from InputStream // System.out.println("Image From Stream"); BufferedImage temp; - try - { - temp = ImageIO.read(stream); - width = (int)temp.getWidth(); - height = (int)temp.getHeight(); + try { temp = ImageIO.read(stream); } + catch (IOException e) { throw new IllegalArgumentException("Failed to read image from InputStream:" + e.getMessage()); } + + if(temp == null) { throw new NullPointerException("Couldn't load image from InputStream: Image is null"); } - canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - createGraphics(); + width = (int)temp.getWidth(); + height = (int)temp.getHeight(); + + canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + createGraphics(); + + gc.drawImage2(temp, 0, 0); - gc.drawImage2(temp, 0, 0); - } - catch(Exception e) - { - System.out.println("Couldn't Load Image Stream"); - isNull = true; - } platformImage = this; } @@ -135,6 +121,8 @@ public PlatformImage(InputStream stream) public PlatformImage(Image source) { // Create Image from Image + if(source == null) { throw new NullPointerException("Couldn't load image: Image is null"); } + width = source.platformImage.width; height = source.platformImage.height; @@ -149,30 +137,22 @@ public PlatformImage(Image source) public PlatformImage(byte[] imageData, int imageOffset, int imageLength) { // Create Image from Byte Array Range (Data is PNG, JPG, etc.) - try - { - InputStream stream = new ByteArrayInputStream(imageData, imageOffset, imageLength); - - BufferedImage temp; - - temp = ImageIO.read(stream); - width = (int)temp.getWidth(); - height = (int)temp.getHeight(); - - canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - createGraphics(); - - gc.drawImage2(temp, 0, 0); - } - catch(Exception e) - { - System.out.println("Couldn't Load Image Data From Byte Array"); - canvas = new BufferedImage(Mobile.getPlatform().lcdWidth, Mobile.getPlatform().lcdHeight, BufferedImage.TYPE_INT_ARGB); - createGraphics(); - //System.out.println(e.getMessage()); - //e.printStackTrace(); - isNull = true; - } + InputStream stream = new ByteArrayInputStream(imageData, imageOffset, imageLength); + + BufferedImage temp; + + try { temp = ImageIO.read(stream); } + catch (IOException e) { throw new IllegalArgumentException("Failed to read image from Byte Array." + e.getMessage()); } + + if(temp == null) { throw new NullPointerException("Couldn't load image from byte array: Image is null"); } + + width = temp.getWidth(); + height = temp.getHeight(); + + canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + createGraphics(); + + gc.drawImage2(temp, 0, 0); platformImage = this; }