forked from Blendman974/faceclaw
EvenHub compativility fixes: Z-order changes, different text color distribution, text containers scroll
This commit is contained in:
1 parent
c713815694
commit
10a9c85f27
22 files changed
+1545
-610
No files matched your search
@@ -108,6 +108,64 @@ class ImageFileLoader private constructor() {
|
||||
}
|
||||
}
|
||||
|
||||
/** Longest side decodeRgbaFromBytes decodes at; larger images are subsampled. */
|
||||
private const val MAX_RGBA_SIDE = 4096
|
||||
|
||||
/**
|
||||
* Decode encoded image bytes (JPEG, PNG, WebP, GIF's first frame, BMP)
|
||||
* to [widthLo, widthHi, heightLo, heightHi, R, G, B, A, ...] at native
|
||||
* size, not premultiplied, so a transparent pixel keeps its colour;
|
||||
* empty when undecodable. The EvenHub host converts these to grey the
|
||||
* way the Even app does (app/apps/evenhub/image-pipeline.ts).
|
||||
*/
|
||||
@JvmStatic
|
||||
fun decodeRgbaFromBytes(buffer: java.nio.ByteBuffer?): ByteArray {
|
||||
if (buffer == null) return ByteArray(0)
|
||||
try {
|
||||
val source = buffer.duplicate()
|
||||
source.rewind()
|
||||
val data = ByteArray(source.remaining())
|
||||
source.get(data)
|
||||
if (data.isEmpty()) return ByteArray(0)
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
BitmapFactory.decodeByteArray(data, 0, data.size, bounds)
|
||||
if (bounds.outWidth <= 0 || bounds.outHeight <= 0) return ByteArray(0)
|
||||
var sample = 1
|
||||
while (maxOf(bounds.outWidth, bounds.outHeight) / sample > MAX_RGBA_SIDE) sample *= 2
|
||||
val options = BitmapFactory.Options().apply {
|
||||
inPreferredConfig = Bitmap.Config.ARGB_8888
|
||||
inPremultiplied = false
|
||||
inSampleSize = sample
|
||||
}
|
||||
val bitmap = BitmapFactory.decodeByteArray(data, 0, data.size, options) ?: return ByteArray(0)
|
||||
val width = bitmap.width
|
||||
val height = bitmap.height
|
||||
val argb = IntArray(width * height)
|
||||
bitmap.getPixels(argb, 0, width, 0, 0, width, height)
|
||||
bitmap.recycle()
|
||||
val packet = ByteArray(4 + argb.size * 4)
|
||||
packet[0] = (width and 0xff).toByte()
|
||||
packet[1] = ((width shr 8) and 0xff).toByte()
|
||||
packet[2] = (height and 0xff).toByte()
|
||||
packet[3] = ((height shr 8) and 0xff).toByte()
|
||||
for (i in argb.indices) {
|
||||
val pixel = argb[i]
|
||||
val o = 4 + i * 4
|
||||
packet[o] = (pixel shr 16).toByte()
|
||||
packet[o + 1] = (pixel shr 8).toByte()
|
||||
packet[o + 2] = pixel.toByte()
|
||||
packet[o + 3] = (pixel ushr 24).toByte()
|
||||
}
|
||||
return packet
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "RGBA image decode failed", e)
|
||||
return ByteArray(0)
|
||||
} catch (e: OutOfMemoryError) {
|
||||
Log.w(TAG, "RGBA image decode failed", e)
|
||||
return ByteArray(0)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale a bitmap to fit within maxWidth x maxHeight (preserving aspect,
|
||||
* never upscaling) and convert it to the grayscale packet format.
|
||||
|
||||
@@ -14,4 +14,10 @@
|
||||
+ (NSData *)bgraPacket:(CGImageRef)image maxSize:(NSInteger)maxSize;
|
||||
/** bgraPacket of encoded image bytes (JPEG, PNG, ...); nil when undecodable. */
|
||||
+ (NSData *)bgraPacketFromImageData:(NSData *)data maxSize:(NSInteger)maxSize;
|
||||
/**
|
||||
* [widthLo, widthHi, heightLo, heightHi, RGBA...] of encoded image bytes at native size
|
||||
* (at most 4096 px a side), first frame only; nil when undecodable. CoreGraphics
|
||||
* premultiplies, so transparent pixels come out black. For the EvenHub image pipeline.
|
||||
*/
|
||||
+ (NSData *)rgbaPacketFromImageData:(NSData *)data;
|
||||
@end
|
||||
@@ -158,4 +158,26 @@
|
||||
CGImageRelease(image);
|
||||
return packet;
|
||||
}
|
||||
+ (NSData *)rgbaPacketFromImageData:(NSData *)data {
|
||||
if (!data.length) return nil;
|
||||
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
||||
if (!source) return nil;
|
||||
CGImageRef image = CGImageSourceCreateImageAtIndex(source, 0, NULL);
|
||||
CFRelease(source);
|
||||
if (!image) return nil;
|
||||
size_t width = CGImageGetWidth(image), height = CGImageGetHeight(image);
|
||||
if (width == 0 || height == 0 || width > 4096 || height > 4096) { CGImageRelease(image); return nil; }
|
||||
uint8_t dimensions[] = { width & 255, (width >> 8) & 255, height & 255, (height >> 8) & 255 };
|
||||
NSMutableData *packet = [NSMutableData dataWithBytes:dimensions length:4];
|
||||
[packet increaseLengthBy:width * height * 4];
|
||||
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
|
||||
CGContextRef context = CGBitmapContextCreate((uint8_t *)packet.mutableBytes + 4, width, height, 8, width * 4, space,
|
||||
kCGBitmapByteOrder32Big | (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
|
||||
CGColorSpaceRelease(space);
|
||||
if (!context) { CGImageRelease(image); return nil; }
|
||||
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
|
||||
CGContextRelease(context);
|
||||
CGImageRelease(image);
|
||||
return packet;
|
||||
}
|
||||
@end
|
||||
Reference in new issue
Block a user