Revision: 2971
http://sourceforge.net/p/swingme/code/2971
Author: yuranet
Date: 2025-12-13 13:50:49 +0000 (Sat, 13 Dec 2025)
Log Message:
-----------
fix creating image with alpha from int[]
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 12:22:31 UTC (rev 2970)
+++ iOSME/src/javax/microedition/lcdui/Image.java 2025-12-13 13:50:49 UTC (rev 2971)
@@ -230,7 +230,7 @@
int size = Math.max(available, 8 * 1024);
BufferedInputStream buffInput = new BufferedInputStream(stream, size);
- //byte[] bytes = stream.readAllBytes?(); // added in java 9
+ //byte[] bytes = stream.readAllBytes(); // added in java 9
byte[] bytes = Streams.readFullyNoClose(buffInput);
return createImage(bytes, 0, bytes.length);
@@ -495,12 +495,25 @@
public void setRGB(int[] rgbData, int offset, int srcScanLength, int width, int height, boolean processAlpha) {
Graphics g = getGraphics();
+ CGContextRef context = g.getContext();
+
int distScanLength = getIntsPerRow();
- IntPtr intPtr = CoreGraphics.CGBitmapContextGetData(g.getContext()).getIntPtr();
+ int info = CoreGraphics.CGBitmapContextGetBitmapInfo(context);
+ //int order = info & CGBitmapInfo.ByteOrderMask; // CGBitmapInfo.ByteOrder32Little
+ int alpha = info & CGBitmapInfo.AlphaInfoMask; // CGImageAlphaInfo.PremultipliedFirst
+
+ IntPtr intPtr = CoreGraphics.CGBitmapContextGetData(context).getIntPtr();
for (int y = 0; y < height; y++) {
if (processAlpha) {
- intPtr.copyFrom(rgbData, offset+ y * srcScanLength, y * distScanLength, width);
+ if (alpha == CGImageAlphaInfo.PremultipliedFirst || alpha == CGImageAlphaInfo.PremultipliedLast) {
+ for (int x = 0; x < width; x++) {
+ intPtr.setValue(y * distScanLength + x, premultiplyAlpha(rgbData[offset + y * srcScanLength + x]));
+ }
+ }
+ else {
+ intPtr.copyFrom(rgbData, offset + y * srcScanLength, y * distScanLength, width);
+ }
}
else {
// TODO this could be better
@@ -511,6 +524,19 @@
}
}
+ private static int premultiplyAlpha(int argb) {
+ int a = argb >>> 24;
+ if (a == 255) return argb;
+ int r = (argb >> 16) & 0xFF;
+ int g = (argb >> 8) & 0xFF;
+ int b = argb & 0xFF;
+ // + 127 is half of 255, and is added so we do not round down the result
+ r = (r * a + 127) / 255;
+ g = (g * a + 127) / 255;
+ b = (b * a + 127) / 255;
+ return (a << 24) | (r << 16) | (g << 8) | b;
+ }
+
public void setRGB(int x, int y, int color) {
Graphics g = getGraphics();
int srcScanLenght = getIntsPerRow();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|