在为RGB图像创建灰度滤镜时,Swift中断了执行

时间:2016-01-24 13:14:58

标签: swift image-processing swift-playground rgba

我对Swift很新。我正在尝试使用均值方法创建灰度滤镜。

这是我正在使用的RGBAImage.swift文件:

import UIKit

public struct Pixel {
    public var value: UInt32

    public var red: UInt8 {
        get {
            return UInt8(value & 0xFF)
        }
        set {
            value = UInt32(newValue) | (value & 0xFFFFFF00)
        }
    }

    public var green: UInt8 {
        get {
            return UInt8((value >> 8) & 0xFF)
        }
        set {
            value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF)
        }
    }

    public var blue: UInt8 {
        get {
            return UInt8((value >> 16) & 0xFF)
        }
        set {
            value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF)
        }
    }

    public var alpha: UInt8 {
        get {
            return UInt8((value >> 24) & 0xFF)
        }
        set {
            value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF)
        }
    }
}

public struct RGBAImage {
    public var pixels: UnsafeMutableBufferPointer<Pixel>

    public var width: Int
    public var height: Int

    public init?(image: UIImage) {
        guard let cgImage = image.CGImage else { return nil }

        // Redraw image for correct pixel format
        let colorSpace = CGColorSpaceCreateDeviceRGB()

        var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue
        bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue & CGBitmapInfo.AlphaInfoMask.rawValue

        width = Int(image.size.width)
        height = Int(image.size.height)
        let bytesPerRow = width * 4

        let imageData = UnsafeMutablePointer<Pixel>.alloc(width * height)

        guard let imageContext = CGBitmapContextCreate(imageData, width, height, 8, bytesPerRow, colorSpace, bitmapInfo) else { return nil }
        CGContextDrawImage(imageContext, CGRect(origin: CGPointZero, size: image.size), cgImage)

        pixels = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: width * height)
    }

    public func toUIImage() -> UIImage? {
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue
        bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue & CGBitmapInfo.AlphaInfoMask.rawValue

        let bytesPerRow = width * 4

        let imageContext = CGBitmapContextCreateWithData(pixels.baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo, nil, nil)

        guard let cgImage = CGBitmapContextCreateImage(imageContext) else {return nil}
        let image = UIImage(CGImage: cgImage)

        return image
    }
}

这是我的代码:

import UIKit

let image = UIImage(named: "sample")!
let myRGBA = RGBAImage(image: image)!

struct grayscale_mean_method {
    var result: UIImage {
        for y in 0..<myRGBA.height {
            for x in 0..<myRGBA.width {
                let index = y * myRGBA.width + x
                var pixel = myRGBA.pixels[index]
                let intensity = (pixel.blue + pixel.green + pixel.red) / 3
                let gray = UInt8(max(0,min(255, intensity)))
                pixel.red = gray
                pixel.green = gray
                pixel.blue = gray
                myRGBA.pixels[index] = pixel
            }
        }
        return myRGBA.toUIImage()!
    }
}

var newImage = grayscale_mean_method.init().result

但是在分配

时出现错误
let intensity = (pixel.blue + pixel.green + pixel.red) / 3

Execution was interrupted, reason = EXC_BAD_INSTRUCTION (code =EXC_l386_INVOP, subcode=0x0) 

我搜索但没有找到解决方案。知道我做错了什么吗?

1 个答案:

答案 0 :(得分:2)

let intensity = (pixel.blue + pixel.green + pixel.red) / 3

添加的所有操作数都具有类型UInt8和添加 如果结果不合适,溢出(运行时异常) 进入UInt8的范围。要解决此问题,请转换所有值 在计算平均值之前到Int

let intensity = (Int(pixel.blue) + Int(pixel.green) + Int(pixel.red)) / 3