Harbeth 是 Apple 的 Metal 框架上的一小部分实用程序和扩展,致力于使您的 Swift GPU 代码更加简洁,让您更快地构建管道原型。本文就来介绍与设计基于GPU的滤镜,图形处理和滤镜制作…
目前,Metal Moudle 最重要的特点可以总结如下:
支持运算符函数式操作
支持快速设计滤镜
支持输出源的快速扩展
支持相机采集特效
支持矩阵卷积
滤镜部分大致分为以下几个模块:
总结下来目前共有100+
种滤镜供您使用。
原始代码:
ImageView.image = originImage
注入滤镜代码:
let filter = C7ColorMatrix4x4(matrix: Matrix4x4.sepia)
ImageView.image = try? originImage.make(filter: filter)
注入边缘检测滤镜
var filter = C7EdgeGlow()
filter.lineColor = UIColor.blue
生成相机采集器
let collector = C7FilterCollector(callback: {
self.ImageView.image = $0
})
collector.captureSession.sessionPreset = AVCaptureSession.Preset.hd1280x720
collector.filter = filter
ImageView.layer.addSublayer(collector)
核心,基础核心板块
Float
。MTLTexture
的数组输出,输出板块
C7FilterProtocal
public protocol C7FilterProtocol {
/// 编码器类型和对应的函数名
///
/// 计算需要对应的`kernel`函数名
/// 渲染需要一个`vertex`着色器函数名和一个`fragment`着色器函数名
var modifier: Modifier { get }
/// 制作缓冲区
/// 设置修改参数因子,需要转换为`Float`。
var factors: [Float] { get }
/// 多输入源扩展
/// 包含 `MTLTexture` 的数组
var otherInputTextures: C7InputTextures { get }
/// 改变输出图像的大小
func outputSize(input size:C7Size)-> C7Size
}
Float
类型。设计一款灵魂出窍滤镜,
public struct C7SoulOut: C7FilterProtocol {
/// The adjusted soul, from 0.0 to 1.0, with a default of 0.5
public var soul: Float = 0.5
public var maxScale: Float = 1.5
public var maxAlpha: Float = 0.5
public var modifier: Modifier {
return .compute(kernel: "C7SoulOut")
}
public var factors: [Float] {
return [soul, maxScale, maxAlpha]
}
public init() { }
}
此过滤器需要三个参数:
soul
:调整后的灵魂,从 0.0 到 1.0,默认为 0.5maxScale
:最大灵魂比例maxAlpha
:最大灵魂的透明度编写基于并行计算内核函数
kernel void C7SoulOut(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::sample> inputTexture [[texture(1)]],
constant float *soulPointer [[buffer(0)]],
constant float *maxScalePointer [[buffer(1)]],
constant float *maxAlphaPointer [[buffer(2)]],
uint2 grid [[thread_position_in_grid]]) {
constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
const half4 inColor = inputTexture.read(grid);
const float x = float(grid.x) / outputTexture.get_width();
const float y = float(grid.y) / outputTexture.get_height();
const half soul = half(*soulPointer);
const half maxScale = half(*maxScalePointer);
const half maxAlpha = half(*maxAlphaPointer);
const half alpha = maxAlpha * (1.0h - soul);
const half scale = 1.0h + (maxScale - 1.0h) * soul;
const half soulX = 0.5h + (x - 0.5h) / scale;
const half soulY = 0.5h + (y - 0.5h) / scale;
const half4 soulMask = inputTexture.sample(quadSampler, float2(soulX, soulY));
const half4 outColor = inColor * (1.0h - alpha) + soulMask * alpha;
outputTexture.write(outColor, grid);
}
var filter = C7SoulOut()
filter.soul = 0.5
filter.maxScale = 2.0
/// 直接显示在ImageView
ImageView.image = try? originImage.makeImage(filter: filter)
soul
值就完事,简单嘛。/// 1.转换成BGRA
let filter1 = C7Color2(with: .color2BGRA)
/// 2.调整颗粒度
var filter2 = C7Granularity()
filter2.grain = 0.8
/// 3.调整白平衡
var filter3 = C7WhiteBalance()
filter3.temperature = 5555
/// 4.调整高光阴影
var filter4 = C7HighlightShadow()
filter4.shadows = 0.4
filter4.highlights = 0.5
/// 5.组合操作
let AT = C7FilterTexture.init(texture: originImage.mt.toTexture()!)
let result = AT ->> filter1 ->> filter2 ->> filter3 ->> filter4
/// 6.获取结果
filterImageView.image = result.outputImage()
/// 1.转换成RBGA
let filter1 = C7Color2(with: .color2RBGA)
/// 2.调整颗粒度
var filter2 = C7Granularity()
filter2.grain = 0.8
/// 3.配置灵魂效果
var filter3 = C7SoulOut()
filter3.soul = 0.7
/// 4.组合操作
let group: [C7FilterProtocol] = [filter1, filter2, filter3]
/// 5.获取结果
filterImageView.image = try? originImage.makeGroup(filters: group)
两种方式都可以处理多滤镜方案,怎么选择就看你心情。✌️
pod 'Harbeth'
pod 'OpencvQueen'
100+
种滤镜,同时也支持CoreImage混合使用。✌️.