From e8b5ad9a4d3d3a83d61790e595757a2b8584c19d Mon Sep 17 00:00:00 2001 From: AShiningRay Date: Mon, 9 Dec 2024 15:05:30 -0300 Subject: [PATCH] PlatformImage: Handle image mutability a bit better. 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. --- src/com/nokia/mid/ui/DirectUtils.java | 4 ++-- src/javax/microedition/lcdui/Image.java | 26 +++++++++++++++++++-- src/org/recompile/mobile/PlatformImage.java | 6 +++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/com/nokia/mid/ui/DirectUtils.java b/src/com/nokia/mid/ui/DirectUtils.java index ef5ef076..4cd8f190 100644 --- a/src/com/nokia/mid/ui/DirectUtils.java +++ b/src/com/nokia/mid/ui/DirectUtils.java @@ -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); diff --git a/src/javax/microedition/lcdui/Image.java b/src/javax/microedition/lcdui/Image.java index 75198fd5..cacf672a 100644 --- a/src/javax/microedition/lcdui/Image.java +++ b/src/javax/microedition/lcdui/Image.java @@ -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 "); @@ -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); } @@ -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 @@ -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(); } } diff --git a/src/org/recompile/mobile/PlatformImage.java b/src/org/recompile/mobile/PlatformImage.java index c05cbf7a..c978d1ba 100644 --- a/src/org/recompile/mobile/PlatformImage.java +++ b/src/org/recompile/mobile/PlatformImage.java @@ -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; } @@ -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.