5 // Created by Wei Wang on 2016/08/31.
7 // Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 // Reuse the same CI Context for all CI drawing.
33 private let ciContext = CIContext(options: nil)
35 /// Transformer method which will be used in to provide a `Filter`.
36 public typealias Transformer = (CIImage) -> CIImage?
38 /// Supply a filter to create an `ImageProcessor`.
39 public protocol CIImageProcessor: ImageProcessor {
40 var filter: Filter { get }
43 extension CIImageProcessor {
44 public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
46 case .image(let image):
47 return image.kf.apply(filter)
49 return (DefaultImageProcessor.default >> self).process(item: item, options: options)
54 /// Wrapper for a `Transformer` of CIImage filters.
55 public struct Filter {
57 let transform: Transformer
59 public init(tranform: @escaping Transformer) {
60 self.transform = tranform
63 /// Tint filter which will apply a tint color to images.
64 public static var tint: (Color) -> Filter = {
67 let colorFilter = CIFilter(name: "CIConstantColorGenerator")!
68 colorFilter.setValue(CIColor(color: color), forKey: kCIInputColorKey)
70 let colorImage = colorFilter.outputImage
71 let filter = CIFilter(name: "CISourceOverCompositing")!
72 filter.setValue(colorImage, forKey: kCIInputImageKey)
73 filter.setValue(input, forKey: kCIInputBackgroundImageKey)
75 return filter.outputImage?.cropped(to: input.extent)
77 return filter.outputImage?.cropping(to: input.extent)
82 public typealias ColorElement = (CGFloat, CGFloat, CGFloat, CGFloat)
84 /// Color control filter which will apply color control change to images.
85 public static var colorControl: (ColorElement) -> Filter = { arg -> Filter in
86 let (brightness, contrast, saturation, inputEV) = arg
87 return Filter { input in
88 let paramsColor = [kCIInputBrightnessKey: brightness,
89 kCIInputContrastKey: contrast,
90 kCIInputSaturationKey: saturation]
92 let paramsExposure = [kCIInputEVKey: inputEV]
94 let blackAndWhite = input.applyingFilter("CIColorControls", parameters: paramsColor)
95 return blackAndWhite.applyingFilter("CIExposureAdjust", parameters: paramsExposure)
97 let blackAndWhite = input.applyingFilter("CIColorControls", withInputParameters: paramsColor)
98 return blackAndWhite.applyingFilter("CIExposureAdjust", withInputParameters: paramsExposure)
105 extension Kingfisher where Base: Image {
106 /// Apply a `Filter` containing `CIImage` transformer to `self`.
108 /// - parameter filter: The filter used to transform `self`.
110 /// - returns: A transformed image by input `Filter`.
112 /// - Note: Only CG-based images are supported. If any error happens during transforming, `self` will be returned.
113 public func apply(_ filter: Filter) -> Image {
115 guard let cgImage = cgImage else {
116 assertionFailure("[Kingfisher] Tint image only works for CG-based image.")
120 let inputImage = CIImage(cgImage: cgImage)
121 guard let outputImage = filter.transform(inputImage) else {
125 guard let result = ciContext.createCGImage(outputImage, from: outputImage.extent) else {
126 assertionFailure("[Kingfisher] Can not make an tint image within context.")
131 return fixedForRetinaPixel(cgImage: result, to: size)
133 return Image(cgImage: result, scale: base.scale, orientation: base.imageOrientation)