gpt4 book ai didi

swift - CGImage 到 MPSTexture 或 MPSImage

转载 作者:可可西里 更新时间:2023-11-01 00:35:36 25 4
gpt4 key购买 nike

我有一个由 CVPixelbuffer (ARGB) 构建的 CGImage。我想将该 CGImage 转换为 MTLTexture。我使用:

  let texture: MTLTexture = try m_textureLoader.newTexture(with: cgImage, options: [MTKTextureLoaderOptionSRGB : NSNumber(value: true)] )

稍后我想在具有 3 个 channel 的 MPSImage 中使用纹理:

let sid   = MPSImageDescriptor(channelFormat: MPSImageFeatureChannelFormat.float16, width: 40, height: 40, featureChannels: 3)
preImage = MPSTemporaryImage(commandBuffer: commandBuffer, imageDescriptor: sid)
lanczos.encode(commandBuffer: commandBuffer, sourceTexture: texture!, destinationTexture: preImage.texture)
scale.encode (commandBuffer: commandBuffer, sourceImage: preImage, destinationImage: srcImage)

现在我的问题:textureLoader.newTexture(...) 如何将四个 ARGB channel 映射到 MPSImageDescriptor 中指定的 3 个 channel ?我怎样才能确保使用 RGB 组件而不是例如阿格?有没有办法指定 channel 映射?

谢谢,克里斯

最佳答案

为什么不直接从 CVPixelBuffer 构造 MTLTexture 呢?快多了!

在程序开始时执行一次:

// declare this somewhere, so we can re-use it
var textureCache: CVMetalTextureCache?

// create the texture cache object
guard CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, device, nil, &textureCache) == kCVReturnSuccess else {
print("Error: could not create a texture cache")
return false
}

一旦你有你的 CVPixelBuffer 就这样做:

let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)

var texture: CVMetalTexture?
CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache,
pixelBuffer, nil, .bgra8Unorm, width, height, 0, &texture)

if let texture = texture {
metalTexture = CVMetalTextureGetTexture(texture)
}

现在 metalTexture 包含一个包含 CVPixelBuffer 内容的 MTLTexture 对象。

关于swift - CGImage 到 MPSTexture 或 MPSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44906923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com