This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

fix: bitmap colours in converted CGRef are off

+31 -9
+31 -9
posedetection/src/iosMain/kotlin/com/performancecoachlab/posedetection/camera/CameraEngine.kt
··· 21 21 import kotlinx.coroutines.Dispatchers 22 22 import kotlinx.coroutines.Job 23 23 import org.jetbrains.skia.ColorAlphaType 24 + import org.jetbrains.skia.ColorSpace 24 25 import org.jetbrains.skia.ColorType 25 26 import org.jetbrains.skia.Image 26 27 import org.jetbrains.skia.ImageInfo ··· 494 495 495 496 @OptIn(ExperimentalForeignApi::class) 496 497 internal fun UIImage.toSkiaImage(): Image? { 497 - val imageRef = 498 - CGImageCreateCopyWithColorSpace(this.CGImage, CGColorSpaceCreateDeviceRGB()) ?: return null 498 + val imageRef = this.CGImage ?: return null 499 499 500 500 val width = CGImageGetWidth(imageRef).toInt() 501 501 val height = CGImageGetHeight(imageRef).toInt() ··· 504 504 val data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) 505 505 val bytePointer = CFDataGetBytePtr(data) 506 506 val length = CFDataGetLength(data) 507 - val alphaInfo = CGImageGetAlphaInfo(imageRef) 508 507 509 - val alphaType = when (alphaInfo) { 510 - CGImageAlphaInfo.kCGImageAlphaPremultipliedFirst, CGImageAlphaInfo.kCGImageAlphaPremultipliedLast -> ColorAlphaType.PREMUL 511 - CGImageAlphaInfo.kCGImageAlphaFirst, CGImageAlphaInfo.kCGImageAlphaLast -> ColorAlphaType.UNPREMUL 512 - CGImageAlphaInfo.kCGImageAlphaNone, CGImageAlphaInfo.kCGImageAlphaNoneSkipFirst, CGImageAlphaInfo.kCGImageAlphaNoneSkipLast -> ColorAlphaType.OPAQUE 508 + val alphaType = when (CGImageGetAlphaInfo(imageRef)) { 509 + CGImageAlphaInfo.kCGImageAlphaPremultipliedFirst, 510 + CGImageAlphaInfo.kCGImageAlphaPremultipliedLast -> ColorAlphaType.PREMUL 511 + CGImageAlphaInfo.kCGImageAlphaFirst, 512 + CGImageAlphaInfo.kCGImageAlphaLast -> ColorAlphaType.UNPREMUL 513 + CGImageAlphaInfo.kCGImageAlphaNone, 514 + CGImageAlphaInfo.kCGImageAlphaNoneSkipFirst, 515 + CGImageAlphaInfo.kCGImageAlphaNoneSkipLast -> ColorAlphaType.OPAQUE 513 516 else -> ColorAlphaType.UNKNOWN 514 517 } 515 518 516 519 val byteArray = ByteArray(length.toInt()) { index -> 517 520 bytePointer!![index].toByte() 518 521 } 522 + 519 523 CFRelease(data) 520 - CFRelease(imageRef) 524 + CGImageRelease(imageRef) 525 + 526 + val skiaColorSpace = ColorSpace.sRGB 527 + val colorType = ColorType.RGBA_8888 528 + 529 + // Convert RGBA to BGRA 530 + for (i in byteArray.indices step 4) { 531 + val r = byteArray[i] 532 + val g = byteArray[i + 1] 533 + val b = byteArray[i + 2] 534 + val a = byteArray[i + 3] 535 + 536 + byteArray[i] = b 537 + byteArray[i + 2] = r 538 + } 521 539 522 540 return Image.makeRaster( 523 541 imageInfo = ImageInfo( 524 - width = width, height = height, colorType = ColorType.RGBA_8888, alphaType = alphaType 542 + width = width, 543 + height = height, 544 + colorType = colorType, 545 + alphaType = alphaType, 546 + colorSpace = skiaColorSpace 525 547 ), 526 548 bytes = byteArray, 527 549 rowBytes = bytesPerRow.toInt(),