Created
January 14, 2016 05:47
-
-
Save FlexMonkey/a18b0509ce8d2068457e to your computer and use it in GitHub Desktop.
CoreImage Theshold filter based on custom CIColorKernel
This file contains hidden or 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
class ThresholdFilter: CIFilter | |
{ | |
var inputImage : CIImage? | |
var threshold: CGFloat = 0.75 | |
let thresholdKernel = CIColorKernel(string: | |
"kernel vec4 thresholdFilter(__sample image, float threshold)" + | |
"{" + | |
" float luma = (image.r * 0.2126) + (image.g * 0.7152) + (image.b * 0.0722);" + | |
" return (luma > threshold) ? vec4(1.0, 1.0, 1.0, 1.0) : vec4(0.0, 0.0, 0.0, 0.0);" + | |
"}" | |
) | |
override var outputImage : CIImage! | |
{ | |
guard let inputImage = inputImage, | |
thresholdKernel = thresholdKernel else | |
{ | |
return nil | |
} | |
let extent = inputImage.extent | |
let arguments = [inputImage, threshold] | |
return thresholdKernel.applyWithExtent(extent, arguments: arguments) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment