Created
October 28, 2015 17:11
-
-
Save dsowsy/40fae2a1fcc917d2a7d6 to your computer and use it in GitHub Desktop.
GPUImage Filter chaining in Swift. Uses LinkedList from Wayne Bishop's SwiftStructures
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
struct FilterBuilder { | |
let list = LinkedList<GPUImageFilter>() | |
mutating func build(filters: GPUImageFilter...) -> GPUImageFilterGroup { | |
let group = GPUImageFilterGroup() | |
for filter in filters { list.addLink(filter) } | |
var i = 0 | |
while (list.linkAtIndex(i).next != nil ){ | |
let curr = list.linkAtIndex(i) | |
group.addFilter(curr.key) | |
curr.key.addTarget(curr.next?.key) | |
i++ | |
} | |
group.initialFilters = [filters.first as! AnyObject] | |
group.terminalFilter = filters.last | |
return group | |
} | |
// For chaining still image filters, in a clean reusable way. | |
func filterImage(image: UIImage, filters:GPUImageFilter...) -> UIImage { | |
var result : UIImage = image | |
var picture : GPUImagePicture | |
for filter in filters { | |
picture = GPUImagePicture(image: result) | |
picture.addTarget(filter) | |
filter.forceProcessingAtSize(image.size) | |
filter.useNextFrameForImageCapture() | |
picture.processImage() | |
result = filter.imageFromCurrentFramebuffer() | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment