New Custom Core Image Filters

DZone 's Guide to

New Custom Core Image Filters

The eponymous Simon Gladman is back again with more knowledge on custom core image filters in Swift.

· Mobile Zone ·
Free Resource

While writing Core Image for Swift, I've created quite a few custom Core Image filters all of which have been added to my Filterpedia app. To demonstrate the potential of filters based on custom kernel routines, here's a little roundup of some of the recent ones I've written.

If you're writing a photo editing app or want to add a post effect to your SceneKit or SpriteKit based game or app, writing custom kernels can really help your product stand out from the crowd. 

Color Directed Blur

This filter borrows a technique from the Kuwahara Filter: it creates four square "windows" around the current pixel in the North East, North West, South East and South West directions and calculates the average color of each. The shader then finds and returns the averaged color with the closest color match to the current pixel by measuring the distance in a 3D configuration space based on red, green and blue.

Color Directed Blur's final result is quite "painterly" - areas of similar colors are blurred, but edges are preserved. 

Difference of Gaussians

My Difference of Gaussians filter subtracts one Gaussian blurred version of the input image from another. Although the radius of both blurs is the same, the sigma differs between the two. The blurring weight matrices are generated dynamically and the blur filters are implemented as separable horizontal and vertical 9 pixel Core Image convolution kernels.

This filter uses existing Core Image filters in a composite filter rather than a custom kernel.

Polar Pixellate 

This filter uses code written by Jake Gundersen and Brad Larsen from GPUImage. Rather than creating square pixels, it creates arc shaped pixels surrounding an arbitrary center. 

Compound Eye

My Compound Eye filter simulates the eye of an insect or crustacean by creating multiple reflected images in a hexagonal grid. The lens size, lens refractive index and the background color can all be controlled. 

This filter is discussed in length in Core Image for Swift

Refracted Text

My Refracted Text filter accepts a string parameter which it renders as a virtual lens over a background image. It uses Core Image's Height Field from Mask filter which is passed to a custom kernel that uses the height field's slope at every point to calculate a refraction vector. Both the refracted image and the background image can be individually blurred.

This filter is discussed in length in Core Image for Swift

CMYK Registration Mismatch

Some color printing is based on four ink colors: cyan, magenta, yellow and black. If the registration of these colors isn't accurate, the final result displays blurred color ghosting. My CMYK Registration Mismatch filter simulates this effect by sampling cyan, magenta, cyan and black from surrounding pixels and merging them back to RGB colors for a final output. The filter code includes useful GLSL functions for converting RGB colors to CMYK and back again.

Core Image for Swift

If you want to learn more about creating custom Core Image filters, may I recommend my book, Core Image for Swift. It discusses different kernel types (color, warp and general) and is a great introduction to Core Image Kernel Language. There are template filters with all the boilerplate code to start writing custom kernels straight away. 

If you're already familiar with writing shader code, you may want to take a look at my open source Sweetcorn app - it creates kernel routines using a node based user interface and now supports both color and warp kernels:

Core Image for Swift is available from both Apple's iBooks Store or, as a PDF, from Gumroad. IMHO, the iBooks version is better, especially as it contains video assets which the PDF version doesn't.

core image filters, swift

Published at DZone with permission of Simon Gladman , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}