我有一个带有 .r32Float 像素格式的 MTLTexture,其值在 0 - 1 范围内。
将该纹理转换为 8 位或 16 位 NSBitmapImageRep 的最佳方法是什么?
最佳答案
大概您希望您的图像处于灰度色彩空间中。这很简单,如果冗长的话,使用 vImage:
let width = texture.width
let height = texture.height
let sourceRowBytes = width * MemoryLayout<Float>.size
let floatValues = UnsafeMutablePointer<Float>.allocate(capacity: width * height)
defer {
floatValues.deallocate()
}
texture.getBytes(floatValues,
bytesPerRow: sourceRowBytes,
from: MTLRegionMake2D(0, 0, width, height),
mipmapLevel: 0)
var sourceBuffer = vImage_Buffer(data: floatValues,
height: vImagePixelCount(height),
width: vImagePixelCount(width),
rowBytes: sourceRowBytes)
let destRowBytes = width
let byteValues = malloc(width * height)!
var destBuffer = vImage_Buffer(data: byteValues,
height: vImagePixelCount(height),
width: vImagePixelCount(width),
rowBytes: destRowBytes)
vImageConvert_PlanarFtoPlanar8(&sourceBuffer, &destBuffer, 1.0, 0.0, vImage_Flags(kvImageNoFlags))
let bytesPtr = byteValues.assumingMemoryBound(to: UInt8.self)
let provider = CGDataProvider(data: CFDataCreateWithBytesNoCopy(kCFAllocatorDefault,
bytesPtr,
width * height,
kCFAllocatorDefault))!
let colorSpace = CGColorSpace(name: CGColorSpace.linearGray)!
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)
let image = CGImage(width: width,
height: height,
bitsPerComponent: 8,
bitsPerPixel: 8,
bytesPerRow: destRowBytes,
space: colorSpace,
bitmapInfo: bitmapInfo,
provider: provider,
decode: nil,
shouldInterpolate: false,
intent: .defaultIntent)!
let imageRep = NSBitmapImageRep(cgImage: image)
关于swift - 具有 r32Float 像素格式的 MTLTexture 到位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51864442/