Revision: 2973
http://sourceforge.net/p/swingme/code/2973
Author: yuranet
Date: 2025-12-13 14:40:04 +0000 (Sat, 13 Dec 2025)
Log Message:
-----------
unpremultiply also done
Modified Paths:
--------------
iOSME/src/javax/microedition/lcdui/Image.java
Modified: iOSME/src/javax/microedition/lcdui/Image.java
===================================================================
--- iOSME/src/javax/microedition/lcdui/Image.java 2025-12-13 13:56:43 UTC (rev 2972)
+++ iOSME/src/javax/microedition/lcdui/Image.java 2025-12-13 14:40:04 UTC (rev 2973)
@@ -297,7 +297,7 @@
return context;
}
- public static final Image createRGBImage(int[] rgb, int width, int height, boolean processAlpha) {
+ public static Image createRGBImage(int[] rgb, int width, int height, boolean processAlpha) {
// TODO not optimum as we first create white image
Image img = createImage(width, height);
img.setRGB(rgb, 0, width, width, height, processAlpha);
@@ -491,6 +491,15 @@
for (int c = 0; c < height; c++) {
intPtr.copyTo((y+c) * srcScanLenght + x, rgb, offset + c * distScanLength, width);
}
+
+ int alphaMode = CoreGraphics.CGBitmapContextGetBitmapInfo(context) & CGBitmapInfo.AlphaInfoMask;
+ if (alphaMode == CGImageAlphaInfo.PremultipliedFirst || alphaMode == CGImageAlphaInfo.PremultipliedLast) {
+ for (int a = 0; a < height; a++) {
+ for (int b = 0; b < width; b++) {
+ rgb[offset + a * distScanLength + b] = unpremultiply(rgb[offset + a * distScanLength + b]);
+ }
+ }
+ }
}
public void setRGB(int[] rgbData, int offset, int srcScanLength, int width, int height, boolean processAlpha) {
@@ -524,6 +533,10 @@
}
}
+ /**
+ * TODO maybe we can use a system apple function to do this
+ * @see apple.accelerate.c.Accelerate#vImagePremultiplyData_ARGB8888(apple.accelerate.struct.vImage_Buffer, apple.accelerate.struct.vImage_Buffer, int)
+ */
private static int premultiplyAlpha(int argb) {
int a = argb >>> 24;
if (a == 255) return argb;
@@ -537,11 +550,35 @@
return (a << 24) | (r << 16) | (g << 8) | b;
}
+ /**
+ * TODO maybe we can use a system apple function to do this
+ * @see apple.accelerate.c.Accelerate#vImageUnpremultiplyData_ARGB8888(apple.accelerate.struct.vImage_Buffer, apple.accelerate.struct.vImage_Buffer, int)
+ */
+ public static int unpremultiply(int argb) {
+ int a = argb >>> 24; // alpha 0..255
+ if (a == 0 || a == 255) return argb; // fully transparent or opaque, no change needed
+
+ int r = (argb >> 16) & 0xFF;
+ int g = (argb >> 8) & 0xFF;
+ int b = argb & 0xFF;
+
+ // reverse premultiply with rounding
+ r = (r * 255 + a / 2) / a;
+ g = (g * 255 + a / 2) / a;
+ b = (b * 255 + a / 2) / a;
+
+ return (a << 24) | (r << 16) | (g << 8) | b;
+ }
+
public void setRGB(int x, int y, int color) {
Graphics g = getGraphics();
+ CGContextRef context = g.getContext();
int srcScanLenght = getIntsPerRow();
- IntPtr intPtr = CoreGraphics.CGBitmapContextGetData(g.getContext()).getIntPtr();
- intPtr.setValue(y * srcScanLenght + x, color);
+ int alphaMode = CoreGraphics.CGBitmapContextGetBitmapInfo(context) & CGBitmapInfo.AlphaInfoMask;
+ IntPtr intPtr = CoreGraphics.CGBitmapContextGetData(context).getIntPtr();
+ intPtr.setValue(y * srcScanLenght + x,
+ alphaMode == CGImageAlphaInfo.PremultipliedFirst || alphaMode == CGImageAlphaInfo.PremultipliedLast ?
+ premultiplyAlpha(color) : color);
}
public static void filter(Image source, Image bm, ColorMatrix cm) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|