EXC_BAD_INSTRUCTION Swift Dithering函数

时间:2016-12-09 04:01:27

标签: ios swift

我一直在尝试在Swift中创建一个抖动函数,但我一直在遇到问题。我注意到代码循环恰好适用于我选择的随机图像的前9000个像素。但后来它给了我一个运行时错误,我到处寻找,我似乎无法解决这个问题。请帮忙。

file:///Users/jeffn/Desktop/MyPlayground34.playground/:错误:游乐场执行中止:错误:执行被中断,原因:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)。 该过程一直处于被中断的位置,使用“thread return -x”返回到表达式求值之前的状态。

   import UIKit





 struct Pixel {
var value: UInt32
var red: UInt8 {
    get { return UInt8(value & 0xFF) }
    set { value = UInt32(newValue) | (value & 0xFFFFFF00) }
}
var green: UInt8 {
    get { return UInt8((value >> 8) & 0xFF) }
    set { value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF) }
}
var blue: UInt8 {
    get { return UInt8((value >> 16) & 0xFF) }
    set { value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF) }
}
var alpha: UInt8 {
    get { return UInt8((value >> 24) & 0xFF) }
    set { value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF) }
}}

 public struct RGBA {
var pixels: UnsafeMutableBufferPointer<Pixel>
var width: Int
var height: Int

init?(image: UIImage) {
    guard let cgImage = image.cgImage else { return nil }
    width = Int(image.size.width)
    height = Int(image.size.height)
    let bitsPerComponent = 8
    let bytesPerPixel = 4
    let bytesPerRow = width * bytesPerPixel
    let imageData = UnsafeMutablePointer<Pixel>.allocate(capacity: width * height)
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
    bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
    guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { return nil }
    imageContext.draw(cgImage, in: CGRect(origin: CGPoint(x: 0,y :0), size: image.size))
    pixels = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: width * height)
}

public func toUIImage() -> UIImage? {
    let bitsPerComponent = 8
    let bytesPerPixel = 4
    let bytesPerRow = width * bytesPerPixel
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
    bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
    let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil)
    guard let cgImage = imageContext!.makeImage() else {return nil}
    let image = UIImage(cgImage: cgImage)
    return image
}
}

public func contrast(image: UIImage) -> RGBA {
let rgba = RGBA(image: image)!

var new_red: UInt8 = 0
var new_green = 0
var new_blue = 0
var new_alpha = 0

var error_red: UInt8 = 0
var error_green = 0
var error_blue = 0
var error_alpha = 0

var pixel_1_red: UInt8 = 0

var output_red: UInt8 = 0

let w1: Double = 7.00/16.00
let w2: Double = 3.00/16.00
let w3: Double = 5.00/16.00
let w4: Double = 1.00/16.00


for y in 0..<rgba.height-1{
    for x in 0..<rgba.width-1{
        var index = y * rgba.width + x
        var index_1 = y*rgba.width + x + 1

        var pixel = rgba.pixels[index]
        var pixel_1 = rgba.pixels[index_1]
        pixel_1_red = pixel_1.red



        if(pixel.red < 128){new_red = 0} else {new_red = 255}
        error_red = new_red - pixel.red


        var double_error_red = Double(error_red)*w1
        var int_error_red = UInt8(double_error_red)

        output_red = pixel_1_red + int_error_red


        pixel_1.red = output_red
        rgba.pixels[index_1] = pixel_1


    }
}

return rgba
}




 let image = UIImage(named: "newlowpassfilter.jpg")!
 let rgba = contrast(image: image)
 let newImage = rgba.toUIImage()
 image

 newImage

1 个答案:

答案 0 :(得分:0)

也许整数溢出是你的问题。

使用我的一个示例jpg-image进行测试时,此行导致溢出:

output_red = pixel_1_red + int_error_red

您可能需要将行更改为以下内容:

let temp_red = Int(pixel_1_red) + Int(int_error_red)
output_red = temp_red > 255 ? 255 : UInt8(temp_red)

在Swift中,+运算符检测到溢出并导致应用程序崩溃,忽略运算符&+的溢出不适合图像处理。您可能需要关心代码中每个操作的结果范围。