Metal每日分享,LUT查找滤镜效果

本案例的目的是理解如何用Metal实现LUT颜色查找表滤镜,通过将颜色值存储在一张表中,在需要的时候通过索引在这张表上找到对应的颜色值,将原有色值替换成查找表中的色值;
摘要由CSDN通过智能技术生成

本案例的目的是理解如何用Metal实现LUT颜色查找表滤镜,通过将颜色值存储在一张表中,在需要的时候通过索引在这张表上找到对应的颜色值,将原有色值替换成查找表中的色值;

总结就是一种针对色彩空间的管理和转换技术,LUT 就是一个 RGB 组合到另一个 RGB 组合的映射关系表;


Demo

实操代码

// LUT查找滤镜
let filter = C7LookupTable.init(image: R.image("lut_abao"))

// 方案1:
let dest = BoxxIO.init(element: originImage, filter: filter)
ImageView.image = try? dest.output()

dest.filters.forEach {
    NSLog("%@", "\($0.parameterDescription)")
}

// 方案2:
ImageView.image = try? originImage.make(filter: filter)

// 方案3:
ImageView.image = originImage ->> filter

实现原理

  • 过滤器

这款滤镜采用并行计算编码器设计.compute(kernel: "C7LookupTable"),参数因子[intensity]

对外开放参数

  • intensity: 强度,其实就是调整mix混合平均值。
/// LUT映射滤镜
public struct C7LookupTable: C7FilterProtocol {
    
    public let lookupImage: C7Image?
    public let lookupTexture: MTLTexture?
    public var intensity: Float = 1.0
    
    public var modifier: Modifier {
        return .compute(kernel: "C7LookupTable")
    }
    
    public var factors: [Float] {
        return [intensity]
    }
    
    public var otherInputTextures: C7InputTextures {
        return lookupTexture == nil ? [] : [lookupTexture!]
    }
    
    public init(image: C7Image?) {
        self.lookupImage = image
        self.lookupTexture = image?.cgImage?.mt.newTexture()
    }
    
    public init(name: String) {
        self.init(image: R.image(name))
    }
}
  • 着色器

1、用蓝色值计算正方形的位置,得到quad1和quad2;
2、根据红色值和绿色值计算对应位置在整个纹理的坐标,得到texPos1和texPos2;
3、根据texPos1和texPos2读取映射结果newColor1和newColor2,再用蓝色值的小数部分进行mix操作;

kernel void C7LookupTable(texture2d<half, access::write> outputTexture [[texture(0)]],
                          texture2d<half, access::read> inputTexture [[texture(1)]],
                          texture2d<half, access::sample> lookupTexture [[texture(2)]],
                          constant float *intensity [[buffer(0)]],
                          uint2 grid [[thread_position_in_grid]]) {
    const half4 inColor = inputTexture.read(grid);
    const half 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值