vImage PixelBuffer生成的像素值数组在图像中产生了黑色条纹。

huangapple go评论64阅读模式
英文:

Array of pixel values for vImage PixelBuffer produces black bar in image

问题

在下面显示的代码中,我正在为`vImage.PixelBuffer`生成一个RGB像素值数组,并从该缓冲区创建图像。但生成的图像包含一个黑色条。对于导致黑色条的原因有任何想法吗?

```swift
import Accelerate

let width = 200
let height = 200

var pixelValues = [UInt8](repeating: 0, count: width * height * 3)

for i in 0..<pixelValues.count {
    pixelValues[i] = .random(in: 0...255)
}

let buffer = vImage.PixelBuffer(
    pixelValues: pixelValues,
    size: .init(width: width, height: height),
    pixelFormat: vImage.Interleaved8x3.self
)

let format = vImage_CGImageFormat(
    bitsPerComponent: 8,
    bitsPerPixel: 8 * 3,
    colorSpace: CGColorSpaceCreateDeviceRGB(),
    bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue)
)!

let image = buffer.makeCGImage(cgImageFormat: format)!

vImage PixelBuffer生成的像素值数组在图像中产生了黑色条纹。


<details>
<summary>英文:</summary>

In the code shown below, I&#39;m generating an array of RGB pixel values for the `vImage.PixelBuffer` and creating an image from that buffer. But the generated image contains a black bar. Any ideas on what is causing the black bar?

```swift
import Accelerate

let width = 200
let height = 200

var pixelValues = [UInt8](repeating: 0, count: width * height * 3)

for i in 0..&lt;pixelValues.count {
    pixelValues[i] = .random(in: 0...255)
}

let buffer = vImage.PixelBuffer(
    pixelValues: pixelValues,
    size: .init(width: width, height: height),
    pixelFormat: vImage.Interleaved8x3.self
)

let format = vImage_CGImageFormat(
    bitsPerComponent: 8,
    bitsPerPixel: 8 * 3,
    colorSpace: CGColorSpaceCreateDeviceRGB(),
    bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue)
)!

let image = buffer.makeCGImage(cgImageFormat: format)!

vImage PixelBuffer生成的像素值数组在图像中产生了黑色条纹。

答案1

得分: 0

我相信你正在使用的CGImageAlphaInfo枚举值表示要跳过未使用的 alpha 字节,但由于你没有 alpha 字节,你应该改用CGImageAlphaInfo.none.rawValue

将以下内容更改为:

bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue)

改为:

bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)

这样做会得到预期的结果。

英文:

I believe the CGImageAlphaInfo enum value you are using says to skip over the unused alpha byte, but since you don't have an alpha byte, you should use CGImageAlphaInfo.none.rawValue instead:

Change:

bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue)

To:

bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)

Doing this gives the expected result.

huangapple
  • 本文由 发表于 2023年7月13日 23:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定