Skip to content

Instantly share code, notes, and snippets.

@K4stor
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save K4stor/a059a719e3a81bc397b1 to your computer and use it in GitHub Desktop.
Save K4stor/a059a719e3a81bc397b1 to your computer and use it in GitHub Desktop.
Hi. Nghia posted a nice article about color lookup tables with Core Image. Now I want to port that code to SWIFT, but all I get is a pink screen :D Actually just need the method of the CIFilter-category. As far as I can see the problem is the converting the data to an NSData object. The post can be found here http://nghiatran.me/2014/06/filter-m…
import Foundation
import GLKit
class ResponseFilter : CIFilter {
var inputImage:CIImage?
var responseName: NSString {
get {
return responseName_
}
set(newName) {
responseName_ = newName
loadCubeDataWithName(name: responseName_)
}
}
private var nsdata_:AnyObject?
private var responseName_: String = ""
override init() {
super.init()
nsdata_ = nil
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override var outputImage: CIImage! {
get {
if nsdata_ == nil {
return inputImage
}
var filter = CIFilter(name: "CIColorCube")
filter.setValue(nsdata_, forKey: "inputCubeData")
filter.setValue(64, forKey: "inputCubeDimension")
filter.setValue(inputImage, forKey: kCIInputImageKey)
return filter.outputImage
}
}
func loadCubeDataWithName(#name:String) {
var n:UInt = 64
var image = UIImage(named: name)
var width = CGImageGetWidth(image!.CGImage)
var height = CGImageGetHeight(image!.CGImage)
var rowNum = height / n
var columnNum = width / n
if width % n != 0 || height % n != 0 || rowNum * columnNum != n {
println("Damn invalid LUT image")
nsdata_ = nil
return
}
var bitmap:[CUnsignedChar] = self.createRGBABitmapFromImage(image!.CGImage)
let size:Int = Int(n * n * n * 4)
var data = [CFloat](count:size, repeatedValue: 0)
var bitmapOffset:Int = 0
var z:UInt = 0
var callTime = 0
for (var row:UInt = 0; row < rowNum; row++){
println("Row \(row)")
for (var y:UInt = 0; y < n; y++){
var tmp = z
for (var col:UInt = 0; col < columnNum; col++) {
for (var x:UInt = 0; x < n; x++){
var r = CFloat(bitmap[bitmapOffset])
var g = CFloat(bitmap[bitmapOffset + 1])
var b = CFloat(bitmap[bitmapOffset + 2])
var a = CFloat(bitmap[bitmapOffset + 3])
var dataOffset = Int((z*n*n + y*n + x) * 4)
data[dataOffset] = r / 255.0
data[dataOffset + 1] = g / 255.0
data[dataOffset + 2] = b / 255.0
data[dataOffset + 3] = a / 255.0
bitmapOffset += 4
callTime++
}
z++
}
z = tmp
}
z += columnNum
}
nsdata_ = NSData(bytesNoCopy: &data, length: size, freeWhenDone: true)
}
func createRGBABitmapFromImage(image:CGImageRef) -> [CUnsignedChar] {
var bitmapSize:Int
var bytesPerRow:UInt
var width = CGImageGetWidth(image)
var height = CGImageGetHeight(image)
bytesPerRow = width * 4
bitmapSize = Int(bytesPerRow * height)
var bitmap = [CUnsignedChar](count:bitmapSize, repeatedValue: 0)
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
let context = CGBitmapContextCreate(&bitmap, width, height, 8, bytesPerRow, colorSpace, bitmapInfo)
CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), image)
return bitmap
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment