为什么对于具有透明背景的NSImage为何获得(0,0,0)的RGB值? [英] Why do I get RGB values of (0,0,0) for an NSImage with a transparent background?

查看:125
本文介绍了为什么对于具有透明背景的NSImage为何获得(0,0,0)的RGB值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从具有透明背景的NSImage提取RGB值时,我可以为特定图像中的实际彩色对象获取正确的RGB值,但是即使存在以下情况,我也可以获得(0,0,0)的RGB值在我测试的特定图像中看不到黑色像素。我的猜测是我从透明图像的各个部分得到(0,0,0)。如何从图像中减去透明背景后获得RGB值。当我调整图像大小并使用下面的代码提取像素值时,会发生这种现象。

When extracting the RGB values from an NSImage with a transparent background I obtain the correct RGB values for the actual colored object within a particular image, but I am also getting RGB values of (0,0,0) even though there are no black pixels in sight within the particular image I test with. My guess was that I am getting (0,0,0) from the parts of the transparent image. How do I only get the RGB values from the image minus the transparent background. This behavior happens when I resize the image and extract the pixels values with the code down below.

调整代码大小(贷方)

open func resizeImage(image:NSImage, newSize:NSSize) -> NSImage{

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

    rep?.size = newSize

    NSGraphicsContext.saveGraphicsState()
    let bitmap = NSGraphicsContext.init(bitmapImageRep: rep!)
    NSGraphicsContext.setCurrent(bitmap)
    image.draw(in: NSMakeRect(0, 0, newSize.width, newSize.height), from: NSMakeRect(0, 0, image.size.width, image.size.height), operation: .sourceOver, fraction: CGFloat(1))

    let newImage = NSImage(size: newSize)
    newImage.addRepresentation(rep!)
    return newImage

}

代码我以前从下面的NSImage中提取RGB值:

The code I used to extract the RGB values from an NSImage is down below:

RGB提取代码(贷项)

extension NSImage {

    func pixelData() -> [Pixel] {
        var bmp = self.representations[0] as! NSBitmapImageRep
        var data: UnsafeMutablePointer<UInt8> = bmp.bitmapData!
        var r, g, b, a: UInt8
        var pixels: [Pixel] = []

        NSLog("%d", bmp.pixelsHigh)
        NSLog("%d", bmp.pixelsWide)
        for var row in 0..<bmp.pixelsHigh {
            for var col in 0..<bmp.pixelsWide {
                r = data.pointee
                data = data.advanced(by: 1)
                g = data.pointee
                data =  data.advanced(by: 1)
                b = data.pointee
                data =  data.advanced(by: 1)
                a = data.pointee
                data =  data.advanced(by: 1)
                pixels.append(Pixel(r: r, g: g, b: b, a: a))
            }
        }

        return pixels
    }
}

class Pixel {

    var r: Float!
    var g: Float!
    var b: Float!

    init(r: UInt8, g: UInt8, b: UInt8, a: UInt8) {
        self.r = Float(r)
        self.g = Float(g)
        self.b = Float(b)
    }

}

我也尝试将大小调整代码的draw方法中的 operation 参数更改为 .copy ,但是没有运气。

I have also tried changing the operation parameter in the draw method of the resizing code to .copy, but with no luck.

推荐答案

您需要查看每个像素的alpha值。如果alpha值为0,则RGB值不相关。 (完全透明像素的颜色会被洗掉以使其完全清晰。)

You need to look at the alpha value of each pixel. If the alpha value is 0, the RGB value is irrelevant. (A fully transparent pixels's color is washed out to completely clear.)

这篇关于为什么对于具有透明背景的NSImage为何获得(0,0,0)的RGB值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆