Procházet zdrojové kódy

Add color adjust and B&W processor

onevcat před 9 roky
rodič
revize
9e802fa7bf
2 změnil soubory, kde provedl 57 přidání a 1 odebrání
  1. 34 1
      Sources/Image.swift
  2. 23 0
      Sources/ImageProcessor.swift

+ 34 - 1
Sources/Image.swift

@@ -47,6 +47,9 @@ import CoreGraphics
 
 #if os(iOS) || os(macOS) || os(tvOS)
 import Accelerate
+import CoreImage
+    
+private let ciContext = CIContext(options: nil)
 #endif
 
 // MARK: - Image Properties
@@ -452,7 +455,6 @@ extension Image {
                 return self
             }
             
-            
             let imageRef: CGImage
             if !cgImage.isARGB8888 {
                 // Convert to ARGB if it isn't
@@ -587,6 +589,37 @@ extension Image {
             return tintedImage ?? self
         #endif
     }
+    
+    func kf_adjusted(brightness: CGFloat, contrast: CGFloat, saturation: CGFloat, inputEV: CGFloat) -> Image {
+        #if os(watchOS)
+        return self
+        #else
+        guard let cgImage = cgImage else {
+            assertionFailure("[Kingfisher] B&W only works for CG-based image.")
+            return self
+        }
+        let input = CIImage(cgImage: cgImage)
+        
+        let paramsColor = [kCIInputBrightnessKey: brightness,
+                             kCIInputContrastKey: contrast,
+                           kCIInputSaturationKey: saturation]
+            
+        let blackAndWhite = input.applyingFilter("CIColorControls", withInputParameters: paramsColor)
+        let paramsExposure = [kCIInputEVKey: inputEV]
+        let output = blackAndWhite.applyingFilter("CIExposureAdjust", withInputParameters: paramsExposure)
+        
+        guard let result = ciContext.createCGImage(output, from: output.extent) else {
+            assertionFailure("Can not make an B&W image within context.")
+            return self
+        }
+            
+        #if os(macOS)
+        return Image(cgImage: result, size: .zero)
+        #else
+        return Image(cgImage: result)
+        #endif
+        #endif
+    }
 }
 
 // MARK: - Decode

+ 23 - 0
Sources/ImageProcessor.swift

@@ -122,6 +122,29 @@ public struct TintImageProcessor: ImageProcessor {
     }
 }
 
+public struct ColorAdjustProcesoor: ImageProcessor {
+    public let brightness: CGFloat
+    public let contrast: CGFloat
+    public let saturation: CGFloat
+    public let inputEV: CGFloat
+    
+    public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
+        switch item {
+        case .image(let image):
+            return image.kf_adjusted(brightness: brightness, contrast: contrast, saturation: saturation, inputEV: inputEV)
+        case .data(_):
+            return (DefaultProcessor() |> self).process(item: item, options: options)
+        }
+    }
+}
+
+public struct BlackWhiteProcessor: ImageProcessor {
+    public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
+        return ColorAdjustProcesoor(brightness: 0.0, contrast: 1.0, saturation: 0.0, inputEV: 0.7)
+            .process(item: item, options: options)
+    }
+}
+
 infix operator |>: AdditionPrecedence
 public func |>(left: ImageProcessor, right: ImageProcessor) -> ImageProcessor {
     return left.append(another: right)