我正在学习如何编写自定义核心图像过滤器,但是当颜色值在 0 - 1 之间时,呈现的颜色与内核代码中指定的颜色不同。(当等于 0 或 1 时,颜色是正确的。)
这是我的过滤器代码:
import UIKit
class testFilter: CIFilter
{
// simply return half red color (0.5)
var colorKernel = CIColorKernel(string:
"kernel vec4 theFilter()" +
"{ return vec4(0.5, 0.0, 0.0, 1.0); }"
)
override var outputImage: CIImage!
{
let rect = CGRect(x: 0, y: 0, width: 200, height: 100)
return colorKernel.applyWithExtent(rect,arguments: nil)
}
}
用法:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// generate image
let filter = testFilter()
let ciImage = f.outputImage
let image = UIImage(CIImage: ciImage)
// add to view
let v = UIImageView(frame: CGRect(x: 50, y: 50, width: 200, height: 100))
v.image = image
view.addSubview(v)
}
}
输出图像如下所示,实际红色分量值在 190 左右(我使用 Photoshop 检查了颜色值)。对于 0.5 的值,我认为正确的输出值是 255 * 0.5 = 122.5?