「这是我参加2022初次更文应战的第9天,活动详情查看:2022初次更文应战」
本事例的意图是了解如何用Metal实现调节图片视点滤镜,经过修正画布巨细,取出旋转之后的坐标点像从来达到旋转作用;
Demo
- HarbethDemo地址
- iDay每日共享文档地址
实操代码
// 旋转滤镜
let filter = C7Rotate.init(angle: 180)
// 计划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: "C7Rotate")
,参数因子[Degree(value: angle).radians]
对外开放参数
-
angle
: 视点旋转,单位是度。
/// 旋转
public struct C7Rotate: C7FilterProtocol {
/// Angle to rotate, unit is degree
@DegreeRange public var angle: Float
public var modifier: Modifier {
return .compute(kernel: "C7Rotate")
}
public var factors: [Float] {
return [Degree(value: angle).radians]
}
public func outputSize(input size: C7Size) -> C7Size {
return mode.rotate(angle: Degree(value: angle).radians, size: size)
}
private var mode: ShapeMode = .fitSize
public init(mode: ShapeMode = .fitSize, angle: Float = 0) {
self.angle = angle
self.mode = mode
}
}
将视点控制在0 ~ 360范围之间,
/// 0.0 ..< 360.0 范围视点区间特点包装器
@propertyWrapper public struct DegreeRange {
public var wrappedValue: Float {
didSet {
let value = wrappedValue.truncatingRemainder(dividingBy: 360.0)
wrappedValue = value >= 0 ? value : 360 + value
}
}
public init(wrappedValue: Float) {
let value = wrappedValue.truncatingRemainder(dividingBy: 360.0)
self.wrappedValue = value >= 0 ? value : 360 + value
}
}
将视点转换成弧度供着色器运用,
public struct Degree {
public let value: Float
public var radians: Float {
return Float(value * Float.pi / 180.0)
}
}
// MARK - Negative Degrees
public prefix func -(degree: Degree) -> Degree {
return Degree(value: -1 * degree.value)
}
核算旋转之后的尺度
public func rotate(angle: Float, size: C7Size) -> C7Size {
switch self {
case .none:
return size
case .fitSize:
let w = Int(abs(sin(angle) * Float(size.height)) + abs(cos(angle) * Float(size.width)))
let h = Int(abs(sin(angle) * Float(size.width)) + abs(cos(angle) * Float(size.height)))
return C7Size(width: w, height: h)
}
}
- 着色器
获取新画布尺度然后再对坐标点归一化处理,核算超出部分视点,然后算出点坐标(inX, inY)取出对应点的像素,超出部分则用空像素填充;
kernel void C7Rotate(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::sample> inputTexture [[texture(1)]],
constant float *angle [[buffer(0)]],
uint2 grid [[thread_position_in_grid]]) {
const float outX = float(grid.x) - outputTexture.get_width() / 2.0f;
const float outY = float(grid.y) - outputTexture.get_height() / 2.0f;
const float dd = distance(float2(outX, outY), float2(0, 0));
const float pi = 3.14159265358979323846264338327950288;
const float w = inputTexture.get_width();
const float h = inputTexture.get_height();
float outAngle = atan(outY / outX);
if (outX < 0) { outAngle += pi; };
const float inAngle = outAngle - float(*angle);
const float inX = (cos(inAngle) * dd + w / 2.0f) / w;
const float inY = (sin(inAngle) * dd + h / 2.0f) / h;
// Set empty pixel when out of range
if (inX * w < -1 || inX * w > w + 1 || inY * h < -1 || inY * h > h + 1) {
outputTexture.write(half4(0), grid);
return;
}
constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
const half4 outColor = inputTexture.sample(quadSampler, float2(inX, inY));
outputTexture.write(outColor, grid);
}
Harbeth功能清单
- 支撑ios体系和macOS体系
- 支撑运算符函数式操作
- 支撑多种模式数据源 UIImage, CIImage, CGImage, CMSampleBuffer, CVPixelBuffer.
- 支撑快速规划滤镜
- 支撑合并多种滤镜作用
- 支撑输出源的快速扩展
- 支撑相机采集特效
- 支撑视频添加滤镜特效
- 支撑矩阵卷积
- 支撑运用体系 MetalPerformanceShaders.
- 支撑兼容 CoreImage.
- 滤镜部分大致分为以下几个模块:
- Blend:图画融合技术
- Blur:含糊作用
- Pixel:图画的根本像素色彩处理
- Effect:作用处理
- Lookup:查找表过滤器
- Matrix: 矩阵卷积滤波器
- Shape:图画形状巨细相关
- Visual: 视觉动态特效
- MPS: 体系 MetalPerformanceShaders.
最终
- 关于旋转图片滤镜介绍与规划到此为止吧。
- 慢慢再补充其他相关滤镜,喜爱就给我点个星吧。
-
滤镜Demo地址,目前包含
100+
种滤镜,同时也支撑CoreImage混合运用。 - 再附上一个开发加速库KJCategoriesDemo地址
- 再附上一个网络基础库RxNetworksDemo地址
- 喜爱的老板们可以点个星,谢谢各位老板!!!
✌️.