Syntactic Sugar for Accelerate/vImage and Core Image Filters

Related tags

Image ShinpuruImage
Overview

ShinpuruImage

Syntactic Sugar for Accelerate/vImage and Core Image Filters

Screenshot

ShinpuruImage offers developers a consistent and strongly typed interface to Apple's Core Image and vImage/Accelerate image filters without the need for boilerplate code.

ShinpuruImage filters are implemented as extensions to UIImage and can be chained together with a super easy syntax:

        let image = UIImage(named: "vegas.jpg")!
            .SIFastBlur(width: 10, height: 10, backgroundColor: UIColor.redColor())
            .SIMonochrome(color: UIColor.yellowColor(), intensity: 1)
            .SIRotate(angle: 0.3, backgroundColor: UIColor.purpleColor())

Installation

ShinpuruImage consists of three Swift files and to use ShinpuruImage in your own project, you simply need to copy:

Filters

Photo Effects

  • func SIPhotoEffectNoir() -> UIImage Applies a preconfigured set of effects that imitate black-and-white photography film with exaggerated contrast.
  • func SIPhotoEffectChrome() -> UIImage Applies a preconfigured set of effects that imitate vintage photography film with exaggerated color.
  • func SIPhotoEffectFade() -> UIImage Applies a preconfigured set of effects that imitate vintage photography film with diminished color.
  • func SIPhotoEffectInstant() -> UIImage Applies a preconfigured set of effects that imitate vintage photography film with distorted colors.
  • func SIPhotoEffectMono() -> UIImage Applies a preconfigured set of effects that imitate black-and-white photography film with low contrast.
  • func SIPhotoEffectProcess() -> UIImage Applies a preconfigured set of effects that imitate vintage photography film with emphasized cool colors.
  • func SIPhotoEffectTonal() -> UIImage Applies a preconfigured set of effects that imitate black-and-white photography film without significantly altering contrast.
  • func SIPhotoEffectTransfer() -> UIImage Applies a preconfigured set of effects that imitate vintage photography film with emphasized warm colors.

Color Effects

  • func SIFalseColor(#color0: UIColor, color1: UIColor) -> UIImage Maps luminance to a color ramp of two colors.
  • func SIPosterize(#levels: Int) -> UIImage Remaps red, green, and blue color components to the number of brightness values you specify for each color component.
  • func SIMonochrome(#color: UIColor, intensity: Float) -> UIImage Remaps colors so they fall within shades of a single color.

Stylize

  • func SIBloom(#radius: Float, intensity: Float) -> UIImage Softens edges and applies a pleasant glow to an image.
  • func SIGloom(#radius: Float, intensity: Float) -> UIImage Dulls the highlights of an image.
  • func SIPixellate(#scale: Float) -> UIImage Makes an image blocky by mapping the image to colored squares whose color is defined by the replaced pixels.

Blur

  • func SIGaussianBlur(#radius: Float) -> UIImage Spreads source pixels by an amount specified by a Gaussian distribution.

Color Adjustment

  • func SIColorControls(#saturation: Float, brightness: Float, contrast: Float) -> UIImage Adjusts saturation, brightness, and contrast values.
  • func SIExposureAdjust(#ev: Float) -> UIImage Adjusts the exposure setting for an image similar to the way you control exposure for a camera when you change the F-stop.
  • func SIGammaAdjust(#power: Float) -> UIImage Adjusts midtone brightness.
  • func SIHueAdjust(#power: Float) -> UIImage Changes the overall hue, or tint, of the source pixels.
  • func SIVibrance(#amount: Float) -> UIImage Adjusts the saturation of an image while keeping pleasing skin tones.
  • func SIWhitePointAdjust(#color: UIColor) -> UIImage Adjusts the reference white point for an image and maps all colors in the source using the new reference.

Morphology functions

  • func SIDilateFilter(#kernel: [UInt8]) -> UIImage Dilates an object
  • func SIErodeFilter(#kernel: [UInt8]) -> UIImage Erodes an object
  • func SIMaxFilter(#width: Int, height: Int) -> UIImage The morphological operation Max is a special case of the dilation operation whereby all the elements of the kernel have the same value.
  • func SIMinFilter(#width: Int, height: Int) -> UIImage The morphological operation Min is a special case of the erosion operation whereby all the elements of the kernel have the same value.

High Level Geometry Functions

  • func SIScale(#scaleX: Float, scaleY: Float) -> UIImage Resize the input image
  • func SIRotate(#angle: Float, backgroundColor: UIColor = UIColor.blackColor()) -> UIImage Rotate the input image around a center point by any amount
  • func SIRotateNinety(rotation: RotateNinety, backgroundColor: UIColor = UIColor.blackColor()) -> UIImage Rotate an image by 0, 90, 180 or 270 degrees
  • func SIHorizontalReflect() -> UIImage Reflect an image across a mirror plane at the center of the image
  • func SIVerticalReflect() -> UIImage Reflect an image across a mirror plane at the center of the image

Convolution

  • func SIBoxBlur(#width: Int, height: Int, backgroundColor: UIColor = UIColor.blackColor()) -> UIImage Apply a mean filter to the image.
  • func SIFastBlur(#width: Int, height: Int, backgroundColor: UIColor = UIColor.blackColor()) -> UIImage Apply a tent filter to the image.
  • func SIConvolutionFilter(#kernel: [Int16], divisor: Int, backgroundColor: UIColor = UIColor.blackColor()) -> UIImage All four channel convolution function

Histogram

  • func SIHistogramCalculation() -> (alpha: [UInt], red: [UInt], green: [UInt], blue: [UInt]) Returns a tuple containing four arrays of 256 UInt representing the histogram of the supplied image

Demo Application

The demo app contains three components:

  • RotateAndScale - demonstrates chained SIScale() and SIRotate() controlled by three numeric sliders
  • ColorControls - demonstrates SIColorControls controlled by three numeric sliders
  • Histogram - uses ios-charts to demonstrate the use of SIHistogramCalculation and the performance benefits of using fast filter chaining.

Filter Chaining

For the best perfomance when chaining image filters together, Shinpuru Image includes a SIFastChainableImage type that prevents the chain from converting to UIImage between individual chained filters. The syntax is slightly different and requires the source UIImage to be cast to a SIFastChainableImage and the final output to be converted back to a UIImage:

            let image = UIImage(named: "oculista.jpg")!

            let chained = SIFastChainableImage(image: image)
                .SIPhotoEffectFade()
                .SIGaussianBlur(radius: 5)
                .SIFalseColor(color0: UIColor.blueColor(), color1: UIColor.redColor())
                .SIPixellate(scale: 5)
                .toUIImage()

For complex chains of filters, using SIFastChainableImage can be four or fives times faster. However, in this mode, color management is turned off.

Blurring Images

To blur an image you have three options: a true Gaussian blur (SIGaussianBlur), a box blur (SIBoxBlur) and ShinpuruImage fast blur (SIFastBlur) which is based on vImageTentConvolve. Of the three, I've found SIBoxBlur to be the fastest and SIGaussianBlur to be the slowest. It's worth using measureBlock() to do your own performance testing.

You might also like...
Twitter Image Pipeline is a robust and performant image loading and caching framework for iOS clients

Twitter Image Pipeline (a.k.a. TIP) Background The Twitter Image Pipeline is a streamlined framework for fetching and storing images in an application

An image download extension of the image view written in Swift for iOS, tvOS and macOS.
An image download extension of the image view written in Swift for iOS, tvOS and macOS.

Moa, an image downloader written in Swift for iOS, tvOS and macOS Moa is an image download library written in Swift. It allows to download and show an

AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.

URLImage URLImage is a SwiftUI view that displays an image downloaded from provided URL. URLImage manages downloading remote image and caching it loca

AYImageKit is a Swift Library for Async Image Downloading, Show Name's Initials and Can View image in Separate Screen.
AYImageKit is a Swift Library for Async Image Downloading, Show Name's Initials and Can View image in Separate Screen.

AYImageKit AYImageKit is a Swift Library for Async Image Downloading. Features Async Image Downloading. Can Show Text Initials. Can have Custom Styles

A complete Mac App: drag an image file to the top section and the bottom section will show you the text of any QRCodes in the image.

QRDecode A complete Mac App: drag an image file to the top section and the bottom section will show you the text of any QRCodes in the image. QRDecode

Convert the image to hexadecimal to send the image to e-paper

ConvertImageToHex Convert the image to hexadecimal to send the image to e-paper Conversion Order // 0. hex로 변환할 이미지 var image = UIImage(named: "sample

Image-cropper - Image cropper for iOS

Image-cropper Example To run the example project, clone the repo, and run pod in

An extremely high-performance, lightweight, and energy-efficient pure Swift async web image loader with memory and disk caching for iOS and  Watch.

KFSwiftImageLoader KFSwiftImageLoader is an extremely high-performance, lightweight, and energy-efficient pure Swift async web image loader with memor

FMPhotoPicker is a modern, simple and zero-dependency photo picker with an elegant and customizable image editor
FMPhotoPicker is a modern, simple and zero-dependency photo picker with an elegant and customizable image editor

FMPhotoPicker is a modern, simple and zero-dependency photo picker with an elegant and customizable image editor Quick demo Batch select/deselect Smoo

Jogendra 113 Nov 28, 2022
📸 Instagram-like image picker & filters for iOS

YPImagePicker YPImagePicker is an instagram-like photo/video picker for iOS written in pure Swift. It is feature-rich and highly customizable to match

Yummypets 4k Dec 27, 2022
Simple image filters

Demo-Image-Filters Simple image filters Apply filters on images demo, Coded in swift language with below functionalities: Select image from phone gall

Jacky Patel 3 Dec 1, 2021
The collection of image filters with swift

SemanticImage The collection of image filters. How to use Setting Up 1, Add Sema

MLBoy 60 Dec 29, 2022
📷 A composable image editor using Core Image and Metal.

Brightroom - Composable image editor - building your own UI Classic Image Editor PhotosCrop Face detection Masking component ?? v2.0.0-alpha now open!

Muukii 2.8k Jan 3, 2023
📷 A composable image editor using Core Image and Metal.

Brightroom - Composable image editor - building your own UI Classic Image Editor PhotosCrop Face detection Masking component ?? v2.0.0-alpha now open!

Muukii 2.8k Jan 2, 2023
Instant camera hybrid with multiple effects and filters written in Swift.

Kontax Cam Download on the app store! No longer on the app store Kontax Cam is an instant camera built 100% using Swift for iOS. You can take your pho

Kevin Laminto 108 Dec 27, 2022
Playing with Core Image and Metal Shader Language for fun.

Playing with Core Image and Metal Shader Language for fun.

Makeeyaf 6 Jan 5, 2023
Core Image Filter Explorer & Showcase

Filterpedia Core Image Filter explorer Filterpedia is an iPad app for exploring (almost) the entire range of image filters offered by Apple's Core Ima

simon gladman 2.2k Dec 28, 2022
An implementation of High Pass Skin Smoothing using Apple's Core Image Framework

YUCIHighPassSkinSmoothing An implementation of High Pass Skin Smoothing using CoreImage.framework Available on both OS X and iOS. Ports A MetalPetal b

Yu Ao 1.2k Dec 17, 2022