gpt4 book ai didi

macos - 奇怪的行为 NSBitmapImageRep

转载 作者:行者123 更新时间:2023-11-28 13:06:56 25 4
gpt4 key购买 nike

我尝试构建图像。逐个像素。所以首先我构建了一些类,它可以在每个像素上循环绘制不同的颜色。但它仅在将 alpha 设置为 255 时才有效。更改 alpha 会使颜色变暗并改变图片。大小和位置都可以。

 var rep:NSBitmapImageRep = NSBitmapImageRep(
bitmapDataPlanes: nil,
pixelsWide: width,
pixelsHigh: height,
bitsPerSample: 8,
samplesPerPixel: 4,
hasAlpha: true,
isPlanar: false,
colorSpaceName: NSDeviceRGBColorSpace,
bytesPerRow: 0,
bitsPerPixel: 0)!

for posX in 0-offset...width+offset*2-1 {
for posY in 0-offset...height+offset*2-1 {

var R = Int(Float(posX)/Float(width)*255)
var G = Int(Float(posY)/Float(height)*255)
var B = Int(rand() % 256)
pixel = [R,G,B,alpha]
rep.setPixel(&pixel, atX: posX, y: posY)
}
}

rep.drawInRect(bounds)

Alpha = 255

alpha 设置为 255

Alpha = 250

alpha 设置为 196

Alpha 127

这次 alpha 等于 127。

alpha 63

还有 64 个

我哪里错了?

最佳答案

最可能的问题是您没有将 alpha 与颜色分量预乘。

来自NSBitmapImageRep class reference :

Alpha Premultiplication and Bitmap Formats

When creating a bitmap using a premultiplied format, if a coverage (alpha) plane exists, the bitmap’s color components are premultiplied with it. In this case, if you modify the contents of the bitmap, you are therefore responsible for premultiplying the data. Note that premultiplying generally has negligible effect on output quality. For floating-point image data, premultiplying color components is a lossless operation, but for fixed-point image data, premultiplication can introduce small rounding errors. In either case, more rounding errors may appear when compositing many premultiplied images; however, such errors are generally not readily visible.

For this reason, you should not use an NSBitmapImageRep object if you want to manipulate image data. To work with data that is not premultiplied, use the Core Graphics framework instead. (Specifically, create images using the CGImageCreate function and kCGImageAlphaLast parameter.) Alternatively, include the NSAlphaNonpremultipliedBitmapFormat flag when creating the bitmap.

Note

Use the bitmapFormat parameter to the initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:samplesPerPixel:hasAlpha:isPlanar:colorSpaceName:bitmapFormat:bytesPerRow:bitsPerPixel: method to specify the format for creating a bitmap. When creating or retrieving a bitmap with other methods, the bitmap format depends on the original source of the image data. Check the bitmapFormat property before working with image data.

您使用了 -init... 方法,没有 bitmapFormat 参数。在这种情况下,您需要查询结果对象的 bitmapFormat 并确保构建像素值以匹配该格式。请注意,格式规定了 alpha 在组件顺序中的位置、颜色组件是否预乘了 alpha 以及组件是 float 还是整数。

您可以切换到使用具有 bitmapFormat 参数的 -init... 方法,并指定 NSAlphaNonpremultipliedBitmapFormat 与您的选择混合其他标志(第一个或最后一个,整数或 float ,字节顺序)。但请注意,并非所有可能的格式都支持绘图。

顺便说一下,我强烈建议阅读 10.6 AppKit release notes 中有关 NSImageNSBitmapImageRep 的部分。 .搜索“NSImage、CGImage 和 CoreGraphics 阻抗匹配”,然后通过“NSBitmapImageRep:CoreGraphics 阻抗匹配和性能说明”部分开始阅读,这与此处最相关。特别是最后一节包含有关直接使用像素的重要信息。

关于macos - 奇怪的行为 NSBitmapImageRep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32416828/

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