Skip to content

Instantly share code, notes, and snippets.

@FlexMonkey
Created January 14, 2016 05:47
Show Gist options
  • Save FlexMonkey/a18b0509ce8d2068457e to your computer and use it in GitHub Desktop.
Save FlexMonkey/a18b0509ce8d2068457e to your computer and use it in GitHub Desktop.
CoreImage Theshold filter based on custom CIColorKernel
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