Skip to content

Instantly share code, notes, and snippets.

@josephjoeljo
Last active November 1, 2019 19:35
Show Gist options
  • Save josephjoeljo/7c00376238322eac993ec4e79c6e07ee to your computer and use it in GitHub Desktop.
Save josephjoeljo/7c00376238322eac993ec4e79c6e07ee to your computer and use it in GitHub Desktop.
Gray Color Matrix
// vim: syntax=Swift
Swift Color matrix
let redCoefficient: Float = 0.2126
let greenCoefficient: Float = 0.7152
let blueCoefficient: Float = 0.0722
let divisor: Int32 = 0x1000
let fDivisor = Float(divisor)
var coefficientsMatrix = [
Int16(redCoefficient * fDivisor),
Int16(greenCoefficient * fDivisor),
Int16(blueCoefficient * fDivisor)
]
let preBias: [Int16] = [0, 0, 0, 0]
let postBias: Int32 = 0
guard let monoFormat = vImage_CGImageFormat(
bitsPerComponent: 8,
bitsPerPixel: 8,
colorSpace: CGColorSpaceCreateDeviceGray(),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue),
renderingIntent: .defaultIntent) else {
return
}
let result = try? destinationBuffer.createCGImage(format: monoFormat)
if let result = result {
imageView.image = UIImage(cgImage: result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment