奇怪的蜂巢NSBitmapImageRep

路加

我尝试建立形象。逐像素 因此,我首先建立一些类,该类可以按循环在每个像素处绘制不同的颜色。但是,仅当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)

阿尔法= 255

alpha设置为255

阿尔法= 250

alpha设置为196

阿尔法127

这次Alpha等于127。

阿尔法63

和64。

我哪里错了?

肯·托马斯(Ken Thomases)

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

NSBitmapImageRep类参考

Alpha预乘法和位图格式

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.

You have used the -init... method without the bitmapFormat parameter. In that case, you need to query the bitmapFormat of the resulting object and make sure you build your pixel values to match that format. Note that the format dictates where the alpha appears in the component order, whether the color components are premultiplied by the alpha, and whether the components are floating point or integer.

You can switch to using the -init... method that does have the bitmapFormat parameter and specify NSAlphaNonpremultipliedBitmapFormat mixed in with your choice of other flags (first or last, integer or floating point, endianness). Note, though, that not all possible formats are supported for drawing.

顺便说一句,我强烈建议阅读的章节NSImage,并NSBitmapImageRep10.6了AppKit发行说明搜索“ NSImage,CGImage和CoreGraphics阻抗匹配”,然后通过“ NSBitmapImageRep:CoreGraphics阻抗匹配和性能说明”部分开始阅读,此处最相关。尤其是最后一部分,提供了有关直接使用像素的重要信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章