Hello there,
I am trying to solve an issue I’m having with creating a pixel buffer from a UIImage in Kotlin multiplatform.
My current system is setup with Kotlin as a common shared business logic (Core) and then swift running on top of it to handle the UI.
I am working with image processing on the Kotlin (core) layer.
During a capture session, I am sending frames into Core as UIImage’s.
I’m trying to create a CVPixelBuffer from the UIImage, but am getting stuck on creating the actual buffer object.
This is what my function looks like:
fun UIImage.getCVPixelBuffer(): CVPixelBufferRefVar? {
val imageWidth = this.size.useContents { this.width }.toULong()
val imageHeight = this.size.useContents { this.height }.toULong()
val cgImage = this.CGImage
val attDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 2, null, null)
CFDictionaryAddValue(attDictionary, kCVPixelBufferCGImageCompatibilityKey, kCFBooleanTrue)
CFDictionaryAddValue(attDictionary, kCVPixelBufferCGBitmapContextCompatibilityKey, kCFBooleanTrue)
// FAILING HERE -----------------
// this ref needs to be initialized, but I can't figure out how ... ?
val pixelBuffer: CVPixelBufferRefVar? = null
CVPixelBufferCreate(
kCFAllocatorDefault,
imageWidth,
imageHeight,
kCVPixelFormatType_32ABGR,
attDictionary,
pixelBuffer?.ptr // this ptr does not exist yet :(((((
)
// condition will always fail since the pixelBuffer var is not properly given the value
if (pixelBuffer !== null) {
val flags = CVPixelBufferLockFlags.MIN_VALUE
CVPixelBufferLockBaseAddress(pixelBuffer.value, flags)
val pxdata = CVPixelBufferGetBaseAddress(pixelBuffer.value)
val context = CGBitmapContextCreate(
pxdata,
imageWidth,
imageHeight,
8,
CVPixelBufferGetBytesPerRow(pixelBuffer.value),
CGColorSpaceCreateDeviceRGB(),
CGImageAlphaInfo.kCGImageAlphaPremultipliedFirst.value
)
println("created context")
if (context != null) {
CGContextDrawImage(
context,
CGRectMake(
0.0,
0.0,
imageWidth.toDouble(),
imageHeight.toDouble()
),
cgImage
)
println("drew image in context")
} else {
CVPixelBufferUnlockBaseAddress(pixelBuffer.value, flags)
return null
}
CVPixelBufferUnlockBaseAddress(pixelBuffer.value, flags)
return pixelBuffer
} else {
// always fails with this message :(
println("pixelBufferRef is null after Create function")
}
return null
}
My best guess is that I need to somehow initialize the pixelBuffer: CVPixelBufferRefVar in order for it to be passed into the CVPixelBufferCreate() function correctly?
Any help would be appreciated!!