Created
June 29, 2026 16:22
-
-
Save ozgurodabasi/e94c654222c3433f4c7db99327a633ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import CoreImage | |
| import UIKit | |
| public final class IFKaleidoscopeFilter: IFFilterProtocol { | |
| public let id = "IFSKL01" | |
| public let name = "kaleidoscope" | |
| public let displayName = "Kaleidoscope" | |
| public let category = "Kaleidoscope" | |
| public let filterSet = "standard" | |
| public let order = 1 | |
| public var amount: Float = 1.0 { | |
| didSet { | |
| amount = min(1.0, max(0.0, amount)) | |
| // count: 2 (mirror) at low → 12 (mandala) at high | |
| let count = Int(2.0 + amount * 10.0) | |
| filter.setValue(count, forKey: "inputCount") | |
| } | |
| } | |
| private let filter: CIFilter | |
| private var context: CIContext { IFRenderContext.shared.ciContext } | |
| public init() { | |
| guard let filter = CIFilter(name: "CIKaleidoscope") else { | |
| fatalError("Failed to create CIKaleidoscope filter") | |
| } | |
| self.filter = filter | |
| filter.setValue(6, forKey: "inputCount") | |
| filter.setValue(0.0, forKey: kCIInputAngleKey) | |
| } | |
| public func apply(to image: CIImage) -> CIImage? { | |
| if amount == 0.0 { return image } | |
| let center = CIVector(x: image.extent.midX, y: image.extent.midY) | |
| filter.setValue(center, forKey: kCIInputCenterKey) | |
| filter.setValue(image, forKey: kCIInputImageKey) | |
| guard let outputImage = filter.outputImage else { return nil } | |
| return outputImage.cropped(to: image.extent) | |
| } | |
| public func generatePreview(from image: UIImage) -> UIImage { | |
| guard let ciImage = CIImage(image: image), | |
| let filteredImage = apply(to: ciImage), | |
| let cgImage = context.createCGImage(filteredImage, from: ciImage.extent) else { | |
| return image | |
| } | |
| return UIImage(cgImage: cgImage) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment