本案例的目的是理解如何用Metal實現調節圖片角度濾鏡,通過修改畫布大小,取出旋轉之後的座標點像素來達到旋轉效果;
效果圖
Demo
實操代碼
// 旋轉濾鏡
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.
- 濾鏡部分大致分爲以下幾個模塊:
最後
- 關於旋轉圖片濾鏡介紹與設計到此爲止吧。
- 慢慢再補充其他相關濾鏡,喜歡就給我點個星🌟吧。
-
濾鏡Demo地址,目前包含
100+
種濾鏡,同時也支持CoreImage混合使用。 - 再附上一個開發加速庫KJCategoriesDemo地址
- 再附上一個網絡基礎庫RxNetworksDemo地址
- 喜歡的老闆們可以點個星🌟,謝謝各位老闆!!!
✌️.