Skip to content

Commit

Permalink
PlatformImage: Handle image mutability a bit better.
Browse files Browse the repository at this point in the history
By default, MIDP creates immutable images in most cases, whereas
in one specific case, Nokia's DirectUtils creates a mutable image
while MIDP creates an immutable one, so we must have different
methods for each.
  • Loading branch information
AShiningRay committed Dec 9, 2024
1 parent 3455b0c commit e8b5ad9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/com/nokia/mid/ui/DirectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public class DirectUtils

public static Image createImage(byte[] imageData, int imageOffset, int imageLength)
{
return Image.createImage(imageData, imageOffset, imageLength);
return Image.createNokiaImage(imageData, imageOffset, imageLength);
}

public static Image createImage(int width, int height, int ARGBcolor)
{
Image image = Image.createImage(width, height);
Image image = Image.createImage(width, height); // This one already creates a mutable image
PlatformGraphics gc = (PlatformGraphics)image.getGraphics();
gc.clearRect(0, 0, width, height);
gc.setAlphaRGB(ARGBcolor);
Expand Down
26 changes: 24 additions & 2 deletions src/javax/microedition/lcdui/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Image
public int width;
public int height;


public static Image createImage(byte[] imageData, int imageOffset, int imageLength) throws IllegalArgumentException
{
Mobile.log(Mobile.LOG_DEBUG, Image.class.getPackage().getName() + "." + Image.class.getSimpleName() + ": " + "Create Image from image data ");
Expand All @@ -42,12 +43,30 @@ public static Image createImage(byte[] imageData, int imageOffset, int imageLeng
return new PlatformImage(imageData, imageOffset, imageLength);
}

// The only difference here is that DirectUtils creates a mutable image, not an immutable one.
public static Image createNokiaImage(byte[] imageData, int imageOffset, int imageLength) throws IllegalArgumentException
{
Mobile.log(Mobile.LOG_DEBUG, Image.class.getPackage().getName() + "." + Image.class.getSimpleName() + ": " + "Create DirectUtils Image from image data ");
if (imageData == null) {throw new NullPointerException();}
if (imageOffset + imageLength > imageData.length) {throw new ArrayIndexOutOfBoundsException();}

PlatformImage img = new PlatformImage(imageData, imageOffset, imageLength);
img.setMutable(true);

return img;
}

public static Image createImage(Image source)
{
Mobile.log(Mobile.LOG_DEBUG, Image.class.getPackage().getName() + "." + Image.class.getSimpleName() + ": " + "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; }

// Else, create an immutable copy of the received image.
PlatformImage newSource = new PlatformImage(source);
newSource.setMutable(false);

return new PlatformImage(source);
}

Expand All @@ -74,7 +93,10 @@ public static Image createImage(int width, int height)
{
Mobile.log(Mobile.LOG_DEBUG, Image.class.getPackage().getName() + "." + Image.class.getSimpleName() + ": " + "Create Image w,h " + width + ", " + height);
if (width <= 0 || height <= 0) {throw new IllegalArgumentException();}
return new PlatformImage(width, height);

PlatformImage img = new PlatformImage(width, height);
img.setMutable(true);
return img;
}

public static Image createImage(String name) throws IOException
Expand All @@ -101,6 +123,6 @@ public void getRGB(int[] rgbData, int offset, int scanlength, int x, int y, int

public int getWidth() { return width; }

public boolean isMutable() { return true; }
public boolean isMutable() { return platformImage.isMutable(); }

}
6 changes: 6 additions & 0 deletions src/org/recompile/mobile/PlatformImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class PlatformImage extends javax.microedition.lcdui.Image
protected BufferedImage canvas;
protected PlatformGraphics gc;

private boolean isMutable = false;

public BufferedImage getCanvas() { return canvas; }

public PlatformGraphics getGraphics() { return gc; }
Expand Down Expand Up @@ -272,6 +274,10 @@ public void setPixel(int x, int y, int color)
pixels[y * canvas.getWidth() + x] = color;
}

public boolean isMutable() { return isMutable; }

public void setMutable(boolean mutable) { isMutable = mutable; }

public static final BufferedImage transformImage(final BufferedImage image, final int transform)
{
// Return early if no transform is specified.
Expand Down

0 comments on commit e8b5ad9

Please sign in to comment.