英文:
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)!
<details>
<summary>英文:</summary>
In the code shown below, I'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..<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)!
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论