added iOS source code
[wl-app.git] / iOS / Pods / Kingfisher / Sources / Filter.swift
1 //
2 //  Filter.swift
3 //  Kingfisher
4 //
5 //  Created by Wei Wang on 2016/08/31.
6 //
7 //  Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
8 //
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:
15 //
16 //  The above copyright notice and this permission notice shall be included in
17 //  all copies or substantial portions of the Software.
18 //
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
25 //  THE SOFTWARE.
26
27
28
29 import CoreImage
30 import Accelerate
31
32 // Reuse the same CI Context for all CI drawing.
33 private let ciContext = CIContext(options: nil)
34
35 /// Transformer method which will be used in to provide a `Filter`.
36 public typealias Transformer = (CIImage) -> CIImage?
37
38 /// Supply a filter to create an `ImageProcessor`.
39 public protocol CIImageProcessor: ImageProcessor {
40     var filter: Filter { get }
41 }
42
43 extension CIImageProcessor {
44     public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
45         switch item {
46         case .image(let image):
47             return image.kf.apply(filter)
48         case .data(_):
49             return (DefaultImageProcessor.default >> self).process(item: item, options: options)
50         }
51     }
52 }
53
54 /// Wrapper for a `Transformer` of CIImage filters.
55 public struct Filter {
56     
57     let transform: Transformer
58
59     public init(tranform: @escaping Transformer) {
60         self.transform = tranform
61     }
62     
63     /// Tint filter which will apply a tint color to images.
64     public static var tint: (Color) -> Filter = {
65         color in
66         Filter { input in
67             let colorFilter = CIFilter(name: "CIConstantColorGenerator")!
68             colorFilter.setValue(CIColor(color: color), forKey: kCIInputColorKey)
69             
70             let colorImage = colorFilter.outputImage
71             let filter = CIFilter(name: "CISourceOverCompositing")!
72             filter.setValue(colorImage, forKey: kCIInputImageKey)
73             filter.setValue(input, forKey: kCIInputBackgroundImageKey)
74             #if swift(>=4.0)
75             return filter.outputImage?.cropped(to: input.extent)
76             #else
77             return filter.outputImage?.cropping(to: input.extent)
78             #endif
79         }
80     }
81     
82     public typealias ColorElement = (CGFloat, CGFloat, CGFloat, CGFloat)
83     
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]
91             
92             let paramsExposure = [kCIInputEVKey: inputEV]
93             #if swift(>=4.0)
94             let blackAndWhite = input.applyingFilter("CIColorControls", parameters: paramsColor)
95             return blackAndWhite.applyingFilter("CIExposureAdjust", parameters: paramsExposure)
96             #else
97             let blackAndWhite = input.applyingFilter("CIColorControls", withInputParameters: paramsColor)
98             return blackAndWhite.applyingFilter("CIExposureAdjust", withInputParameters: paramsExposure)
99             #endif
100         }
101         
102     }
103 }
104
105 extension Kingfisher where Base: Image {
106     /// Apply a `Filter` containing `CIImage` transformer to `self`.
107     ///
108     /// - parameter filter: The filter used to transform `self`.
109     ///
110     /// - returns: A transformed image by input `Filter`.
111     ///
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 {
114         
115         guard let cgImage = cgImage else {
116             assertionFailure("[Kingfisher] Tint image only works for CG-based image.")
117             return base
118         }
119         
120         let inputImage = CIImage(cgImage: cgImage)
121         guard let outputImage = filter.transform(inputImage) else {
122             return base
123         }
124         
125         guard let result = ciContext.createCGImage(outputImage, from: outputImage.extent) else {
126             assertionFailure("[Kingfisher] Can not make an tint image within context.")
127             return base
128         }
129         
130         #if os(macOS)
131             return fixedForRetinaPixel(cgImage: result, to: size)
132         #else
133             return Image(cgImage: result, scale: base.scale, orientation: base.imageOrientation)
134         #endif
135     }
136
137 }