Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created October 30, 2014 22:54
Show Gist options
  • Select an option

  • Save chriseidhof/f6997b5b1d8e2e8ccc2b to your computer and use it in GitHub Desktop.

Select an option

Save chriseidhof/f6997b5b1d8e2e8ccc2b to your computer and use it in GitHub Desktop.
Image Processing
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var imageView: NSImageView!
func applicationDidFinishLaunching(aNotification: NSNotification) {
self.imageView.image = convert(myImage())
window.setFrame(window.screen!.frame, display: true)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
func convert(image: CIImage) -> NSImage {
let rep = NSCIImageRep(CIImage: image)!
let nsImage = NSImage(size: image.extent().size)
nsImage.addRepresentation(rep)
return nsImage
}
func filter(name: String, inputImage: CIImage, configure: CIFilter -> ()) -> CIImage {
let filter = CIFilter(name: name)
filter.setDefaults()
filter.setValue(inputImage, forKey: kCIInputImageKey)
configure(filter)
return filter.outputImage
}
func blur(radius: Double) -> Filter {
return { image in
filter("CIGaussianBlur", image, { f in
f.setValue(radius, forKey: "inputRadius")
})
}
}
func sepia(intensity: Double) -> Filter {
return { image in
filter("CISepiaTone", image) {
myFilter in
myFilter.setValue(intensity, forKey: "inputIntensity")
}
}
}
let distort : Filter = { input in
filter("CITwirlDistortion", input) { _ in }
}
typealias Filter = CIImage -> CIImage
infix operator >>> { associativity left }
func >>><A,B,C>(one: B -> C, two: A -> B) -> A -> C {
return { image in
one(two(image))
}
}
func myImage() -> CIImage {
let imageURL = NSURL(string: "http://localhost:8000/innsbruck2.jpg")!
let image = CIImage(contentsOfURL: imageURL)
let blurFilter = blur(10.0)
let sepiaFilter = sepia(0.7)
let finalFilter = sepiaFilter >>> blurFilter >>> distort
let outputImage = finalFilter(image)
return outputImage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment