The Great Kuwahara Filter it’s wonders and difficulties, this post is about how I did it, what can I learn for future projects, my dificulties, and last but not least how fun it was.
Kuwahara Filter is a denoising technique, during it’s time was a greate inovation to the world of machine assisted exams, things like X-Rays, Ultrasound, etc.
With time better denoise techniques appeared and this filter started being used in a different direction stylization, the way this filter tries to preserve edges and at the same time denoise creates a painting-like effect on images, that can be interesting.
With lots and lots of research, my implementation is now based on Acerola AcerolaFX implementation but with other researches on shader toy and google in general to implement the Generalized Variant.
With this out of the way let’s talk about CIFilters
CIFilter are Apple’s implementation of Image Filters they can be made with Deprecated OpenGL or Metal CI, unfortunately Metal CI is not available for SPM yet since we can’t specify build rules, that means we need to use the deprecated variant. No problem right?
Since is a deprecated feature there’s some jank attached to it, firstly we have to specify a cSetting called: CI_SILENCE_GL_DEPRECATION so we don’t get pesky and annoying warning in other’s people projects.
Now we simply have to make a giant string with Apple’s OpenGL implementation…
You didn’t read it wrong the solution is a giant string, you can choose 2 ways
"kernel float4 yourThing(){" + "return float4(1); }"
or
"""
kernel float4 yourThing() {
return float4(1);
}
"""
I Used the second one since is cleaner has better identation, and is easier to see mistakes, but according to my limited sample size people tend to use the string concatenation for some reason.
even with this weird thing on the way I was able to learn the language by hitting my head against it until stuff worked, sometimes it’s incredible what can you learn by just brute forcing XD.
Besides a new language? not much to be honest, a lot of the math behind this filter went over my head, they look like ancient hieroglyphs for me, at least this shows me that I really need to pick my math studies up.
I already have the next project in my mind.
I Plan to update SwiftDithering to support CIFIlters with this technique that I found, unfortunately Floyd-Steinberg can’t be ported, but SwiftDithering can still keep it’s CPU variant because is fast enough, different from this one.
As Always you can find the open source code in my github repo or my SwiftPackageIndex page.
There’s also a Sample project in the gitHub repo if you want to test drive it. I plan to create a new repo with all the sample apps if I find another interesting visual effect to boot, but no promises yet.
Peace,
Lugalu.