Last active
November 1, 2019 19:35
-
-
Save josephjoeljo/7c00376238322eac993ec4e79c6e07ee to your computer and use it in GitHub Desktop.
Gray Color Matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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