繁体   English   中英

在 iOS 中使用 Swift 导出 16 位图像

[英]Exporting 16 bit image with Swift in iOS

我想导出一个 16 位图像,我有下面的代码

let bv = malloc(width * height * 4)!
var db = vImage_Buffer(data: bv,
                               height: vImagePixelCount(height),
                               width: vImagePixelCount(width),
                               rowBytes: width*2)
//        vImageConvert_PlanarFtoPlanar8(&sourceBuffer, &destBuffer, 1.0, 0.0, vImage_Flags(kvImageNoFlags))
vImageConvert_PlanarFtoPlanar16F(&sourceBuffer, &db, vImage_Flags(kvImageNoFlags))
let bp = bv.assumingMemoryBound(to: UInt8.self)
let p = CGDataProvider(data: CFDataCreateWithBytesNoCopy(kCFAllocatorDefault,
                                                                bp,
                                                                width * height,
                                                                kCFAllocatorDefault))!

let cgImage = CGImage(width: width,
                      height: height,
                      bitsPerComponent: 5,
                      bitsPerPixel: 16,
                      bytesPerRow: width,
                      space: CGColorSpace(name: CGColorSpace.linearSRGB)!,
                      bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipFirst.rawValue),
                      provider: p,
                      decode: nil,
                      shouldInterpolate: false,
                      intent: .defaultIntent)!

let savePath = self.documentsPath.appendingPathComponent("camera")
let sURL = savePath.appendingPathComponent(String(format: "image.png")
if let imageDestination = CGImageDestinationCreateWithURL(smoothedSceneDepthURL as CFURL, kUTTypePNG, 1, nil) {
    CGImageDestinationAddImage(imageDestination, cgImage, nil)
    CGImageDestinationFinalize(imageDestination)
}

Apple 文档说这些 bitsPerComponent 和 bitsPerPixel 的值对 iOS 来说是正确

但我收到以下错误: [Unknown process name] CGImageCreate: invalid image bits/pixel or bytes/row.

我能够以灰度导出 8 位图像,并且可以在需要时发布参数顺便说一句

如果每个像素有 16 位,那么每行将有2*width字节:

                  bytesPerRow: 2 * width,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM