Skip to content

Commit

Permalink
Image + PlatformImage: Follow the javadocs more closely.
Browse files Browse the repository at this point in the history
According to the docs, there are some cases where we can just
return the image received as an argument straight away. Also,
since lcdui.Image isn't the one actually creating new images, and
instead it's PlatformImage, we should just declare each of Image's
createImage as capable of throwing IllegalArgumentExceptions and
handle these cases in PlatformImage's methods themselves.

Result: NET Lizard games don't complain about being unable to load
images from received byte arrays anymore, they just receive
the exception and handle it. Still, this is the sort of change
that can introduce regressions somewhere, despite being more in
line with the docs.
  • Loading branch information
AShiningRay committed Oct 9, 2024
1 parent 0bb967e commit 3d19934
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 67 deletions.
15 changes: 10 additions & 5 deletions src/javax/microedition/lcdui/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,20 +33,23 @@ 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;
}

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);
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
104 changes: 42 additions & 62 deletions src/org/recompile/mobile/PlatformImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -112,29 +101,28 @@ 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;
}

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;

Expand All @@ -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;
}
Expand Down

0 comments on commit 3d19934

Please sign in to comment.