Steve Begin
Steve Begin

Reputation: 295

MTLTexture with r32Float pixel format to bitmap

I have a MTLTexture with a .r32Float pixel format with values in the range 0 - 1.

What would be the best way to convert that texture to an 8bit or a 16 bits NSBitmapImageRep?

Upvotes: 1

Views: 728

Answers (1)

warrenm
warrenm

Reputation: 31782

Presumably you want your image in a grayscale colorspace. This is straightforward, if verbose, with 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)

Upvotes: 5

Related Questions