ImageProcessor.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. //
  2. // ImageProcessor.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2016/08/26.
  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. import Foundation
  27. import CoreGraphics
  28. #if canImport(AppKit)
  29. import AppKit
  30. #endif
  31. /// Represents an item which could be processed by an `ImageProcessor`
  32. ///
  33. /// - image: Input image. The processor should provide a way to apply
  34. /// processing on this `image` and return the result image.
  35. /// - data: Input data. The processor should provide a way to apply
  36. /// processing on this `image` and return the result image.
  37. public enum ImageProcessItem {
  38. /// Input image. The processor should provide a way to apply
  39. /// processing on this `image` and return the result image.
  40. case image(Image)
  41. /// Input data. The processor should provide a way to apply
  42. /// processing on this `image` and return the result image.
  43. case data(Data)
  44. }
  45. /// An `ImageProcessor` would be used to convert some downloaded data to an image.
  46. public protocol ImageProcessor {
  47. /// Identifier of the processor. It will be used to identify the processor when
  48. /// caching and retrieving an image. You might want to make sure that processors with
  49. /// same properties/functionality have the same identifiers, so correct processed images
  50. /// could be retrieved with proper key.
  51. ///
  52. /// - Note: Do not supply an empty string for a customized processor, which is already taken by
  53. /// the `DefaultImageProcessor`. It is recommended to use a reverse domain name notation
  54. /// string of your own for the identifier.
  55. var identifier: String { get }
  56. /// Process an input `ImageProcessItem` item to an image for this processor.
  57. ///
  58. /// - parameter item: Input item which will be processed by `self`
  59. /// - parameter options: Options when processing the item.
  60. ///
  61. /// - returns: The processed image.
  62. ///
  63. /// - Note: The return value will be `nil` if processing failed while converting data to image.
  64. /// If input item is already an image and there is any errors in processing, the input
  65. /// image itself will be returned.
  66. /// - Note: Most processor only supports CG-based images.
  67. /// watchOS is not supported for processors containing filter, the input image will be
  68. /// returned directly on watchOS.
  69. func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image?
  70. }
  71. public extension ImageProcessor {
  72. /// Append an `ImageProcessor` to another. The identifier of the new `ImageProcessor`
  73. /// will be "\(self.identifier)|>\(another.identifier)".
  74. ///
  75. /// - parameter another: An `ImageProcessor` you want to append to `self`.
  76. ///
  77. /// - returns: The new `ImageProcessor` will process the image in the order
  78. /// of the two processors concatenated.
  79. public func append(another: ImageProcessor) -> ImageProcessor {
  80. let newIdentifier = identifier.appending("|>\(another.identifier)")
  81. return GeneralProcessor(identifier: newIdentifier) {
  82. item, options in
  83. if let image = self.process(item: item, options: options) {
  84. return another.process(item: .image(image), options: options)
  85. } else {
  86. return nil
  87. }
  88. }
  89. }
  90. }
  91. func ==(left: ImageProcessor, right: ImageProcessor) -> Bool {
  92. return left.identifier == right.identifier
  93. }
  94. func !=(left: ImageProcessor, right: ImageProcessor) -> Bool {
  95. return !(left == right)
  96. }
  97. typealias ProcessorImp = ((ImageProcessItem, KingfisherOptionsInfo) -> Image?)
  98. struct GeneralProcessor: ImageProcessor {
  99. let identifier: String
  100. let p: ProcessorImp
  101. func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  102. return p(item, options)
  103. }
  104. }
  105. /// The default processor. It convert the input data to a valid image.
  106. /// Images of .PNG, .JPEG and .GIF format are supported.
  107. /// If an image is given, `DefaultImageProcessor` will do nothing on it and just return that image.
  108. public struct DefaultImageProcessor: ImageProcessor {
  109. /// A default `DefaultImageProcessor` could be used across.
  110. public static let `default` = DefaultImageProcessor()
  111. /// Identifier of the processor.
  112. /// - Note: See documentation of `ImageProcessor` protocol for more.
  113. public let identifier = ""
  114. /// Initialize a `DefaultImageProcessor`
  115. public init() {}
  116. /// Process an input `ImageProcessItem` item to an image for this processor.
  117. ///
  118. /// - parameter item: Input item which will be processed by `self`
  119. /// - parameter options: Options when processing the item.
  120. ///
  121. /// - returns: The processed image.
  122. ///
  123. /// - Note: See documentation of `ImageProcessor` protocol for more.
  124. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  125. switch item {
  126. case .image(let image):
  127. return image.kf.scaled(to: options.scaleFactor)
  128. case .data(let data):
  129. return KingfisherClass.image(data: data, options: options.imageCreatingOptions)
  130. }
  131. }
  132. }
  133. public struct RectCorner: OptionSet {
  134. public let rawValue: Int
  135. public static let topLeft = RectCorner(rawValue: 1 << 0)
  136. public static let topRight = RectCorner(rawValue: 1 << 1)
  137. public static let bottomLeft = RectCorner(rawValue: 1 << 2)
  138. public static let bottomRight = RectCorner(rawValue: 1 << 3)
  139. public static let all: RectCorner = [.topLeft, .topRight, .bottomLeft, .bottomRight]
  140. public init(rawValue: Int) {
  141. self.rawValue = rawValue
  142. }
  143. var cornerIdentifier: String {
  144. if self == .all {
  145. return ""
  146. }
  147. return "_corner(\(rawValue))"
  148. }
  149. }
  150. #if !os(macOS)
  151. /// Processor for adding an blend mode to images. Only CG-based images are supported.
  152. public struct BlendImageProcessor: ImageProcessor {
  153. /// Identifier of the processor.
  154. /// - Note: See documentation of `ImageProcessor` protocol for more.
  155. public let identifier: String
  156. /// Blend Mode will be used to blend the input image.
  157. public let blendMode: CGBlendMode
  158. /// Alpha will be used when blend image.
  159. public let alpha: CGFloat
  160. /// Background color of the output image. If `nil`, it will stay transparent.
  161. public let backgroundColor: Color?
  162. /// Initialize an `BlendImageProcessor`
  163. ///
  164. /// - parameter blendMode: Blend Mode will be used to blend the input image.
  165. /// - parameter alpha: Alpha will be used when blend image.
  166. /// From 0.0 to 1.0. 1.0 means solid image, 0.0 means transparent image.
  167. /// Default is 1.0.
  168. /// - parameter backgroundColor: Background color to apply for the output image. Default is `nil`.
  169. public init(blendMode: CGBlendMode, alpha: CGFloat = 1.0, backgroundColor: Color? = nil) {
  170. self.blendMode = blendMode
  171. self.alpha = alpha
  172. self.backgroundColor = backgroundColor
  173. var identifier = "com.onevcat.Kingfisher.BlendImageProcessor(\(blendMode.rawValue),\(alpha))"
  174. if let color = backgroundColor {
  175. identifier.append("_\(color.hex)")
  176. }
  177. self.identifier = identifier
  178. }
  179. /// Process an input `ImageProcessItem` item to an image for this processor.
  180. ///
  181. /// - parameter item: Input item which will be processed by `self`
  182. /// - parameter options: Options when processing the item.
  183. ///
  184. /// - returns: The processed image.
  185. ///
  186. /// - Note: See documentation of `ImageProcessor` protocol for more.
  187. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  188. switch item {
  189. case .image(let image):
  190. return image.kf.scaled(to: options.scaleFactor)
  191. .kf.image(withBlendMode: blendMode, alpha: alpha, backgroundColor: backgroundColor)
  192. case .data:
  193. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  194. }
  195. }
  196. }
  197. #endif
  198. #if os(macOS)
  199. /// Processor for adding an compositing operation to images. Only CG-based images are supported in macOS.
  200. public struct CompositingImageProcessor: ImageProcessor {
  201. /// Identifier of the processor.
  202. /// - Note: See documentation of `ImageProcessor` protocol for more.
  203. public let identifier: String
  204. /// Compositing operation will be used to the input image.
  205. public let compositingOperation: NSCompositingOperation
  206. /// Alpha will be used when compositing image.
  207. public let alpha: CGFloat
  208. /// Background color of the output image. If `nil`, it will stay transparent.
  209. public let backgroundColor: Color?
  210. /// Initialize an `CompositingImageProcessor`
  211. ///
  212. /// - parameter compositingOperation: Compositing operation will be used to the input image.
  213. /// - parameter alpha: Alpha will be used when compositing image.
  214. /// From 0.0 to 1.0. 1.0 means solid image, 0.0 means transparent image.
  215. /// Default is 1.0.
  216. /// - parameter backgroundColor: Background color to apply for the output image. Default is `nil`.
  217. public init(compositingOperation: NSCompositingOperation,
  218. alpha: CGFloat = 1.0,
  219. backgroundColor: Color? = nil)
  220. {
  221. self.compositingOperation = compositingOperation
  222. self.alpha = alpha
  223. self.backgroundColor = backgroundColor
  224. var identifier = "com.onevcat.Kingfisher.CompositingImageProcessor(\(compositingOperation.rawValue),\(alpha))"
  225. if let color = backgroundColor {
  226. identifier.append("_\(color.hex)")
  227. }
  228. self.identifier = identifier
  229. }
  230. /// Process an input `ImageProcessItem` item to an image for this processor.
  231. ///
  232. /// - parameter item: Input item which will be processed by `self`
  233. /// - parameter options: Options when processing the item.
  234. ///
  235. /// - returns: The processed image.
  236. ///
  237. /// - Note: See documentation of `ImageProcessor` protocol for more.
  238. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  239. switch item {
  240. case .image(let image):
  241. return image.kf.scaled(to: options.scaleFactor)
  242. .kf.image(
  243. withCompositingOperation: compositingOperation,
  244. alpha: alpha,
  245. backgroundColor: backgroundColor)
  246. case .data:
  247. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  248. }
  249. }
  250. }
  251. #endif
  252. /// Processor for making round corner images. Only CG-based images are supported in macOS,
  253. /// if a non-CG image passed in, the processor will do nothing.
  254. public struct RoundCornerImageProcessor: ImageProcessor {
  255. /// Identifier of the processor.
  256. /// - Note: See documentation of `ImageProcessor` protocol for more.
  257. public let identifier: String
  258. /// Corner radius will be applied in processing.
  259. public let cornerRadius: CGFloat
  260. /// The target corners which will be applied rounding.
  261. public let roundingCorners: RectCorner
  262. /// Target size of output image should be. If `nil`, the image will keep its original size after processing.
  263. public let targetSize: CGSize?
  264. /// Background color of the output image. If `nil`, it will stay transparent.
  265. public let backgroundColor: Color?
  266. /// Initialize a `RoundCornerImageProcessor`
  267. ///
  268. /// - parameter cornerRadius: Corner radius will be applied in processing.
  269. /// - parameter targetSize: Target size of output image should be. If `nil`,
  270. /// the image will keep its original size after processing.
  271. /// Default is `nil`.
  272. /// - parameter corners: The target corners which will be applied rounding. Default is `.all`.
  273. /// - parameter backgroundColor: Background color to apply for the output image. Default is `nil`.
  274. public init(
  275. cornerRadius: CGFloat,
  276. targetSize: CGSize? = nil,
  277. roundingCorners corners: RectCorner = .all,
  278. backgroundColor: Color? = nil)
  279. {
  280. self.cornerRadius = cornerRadius
  281. self.targetSize = targetSize
  282. self.roundingCorners = corners
  283. self.backgroundColor = backgroundColor
  284. self.identifier = {
  285. var identifier = ""
  286. if let size = targetSize {
  287. identifier = "com.onevcat.Kingfisher.RoundCornerImageProcessor(\(cornerRadius)_\(size)\(corners.cornerIdentifier))"
  288. } else {
  289. identifier = "com.onevcat.Kingfisher.RoundCornerImageProcessor(\(cornerRadius)\(corners.cornerIdentifier))"
  290. }
  291. if let backgroundColor = backgroundColor {
  292. identifier += "_\(backgroundColor)"
  293. }
  294. return identifier
  295. }()
  296. }
  297. /// Process an input `ImageProcessItem` item to an image for this processor.
  298. ///
  299. /// - parameter item: Input item which will be processed by `self`
  300. /// - parameter options: Options when processing the item.
  301. ///
  302. /// - returns: The processed image.
  303. ///
  304. /// - Note: See documentation of `ImageProcessor` protocol for more.
  305. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  306. switch item {
  307. case .image(let image):
  308. let size = targetSize ?? image.kf.size
  309. return image.kf.scaled(to: options.scaleFactor)
  310. .kf.image(
  311. withRoundRadius: cornerRadius,
  312. fit: size,
  313. roundingCorners: roundingCorners,
  314. backgroundColor: backgroundColor)
  315. case .data:
  316. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  317. }
  318. }
  319. }
  320. /// Specify how a size adjusts itself to fit a target size.
  321. ///
  322. /// - none: Not scale the content.
  323. /// - aspectFit: Scale the content to fit the size of the view by maintaining the aspect ratio.
  324. /// - aspectFill: Scale the content to fill the size of the view
  325. public enum ContentMode {
  326. case none
  327. case aspectFit
  328. case aspectFill
  329. }
  330. /// Processor for resizing images. Only CG-based images are supported in macOS.
  331. public struct ResizingImageProcessor: ImageProcessor {
  332. /// Identifier of the processor.
  333. /// - Note: See documentation of `ImageProcessor` protocol for more.
  334. public let identifier: String
  335. /// The reference size for resizing operation.
  336. public let referenceSize: CGSize
  337. /// Target content mode of output image should be.
  338. /// Default to ContentMode.none
  339. public let targetContentMode: ContentMode
  340. /// Initialize a `ResizingImageProcessor`.
  341. ///
  342. /// - Parameters:
  343. /// - referenceSize: The reference size for resizing operation.
  344. /// - mode: Target content mode of output image should be.
  345. ///
  346. /// - Note:
  347. /// The instance of `ResizingImageProcessor` will follow its `mode` property
  348. /// and try to resizing the input images to fit or fill the `referenceSize`.
  349. /// That means if you are using a `mode` besides of `.none`, you may get an
  350. /// image with its size not be the same as the `referenceSize`.
  351. ///
  352. /// **Example**: With input image size: {100, 200},
  353. /// `referenceSize`: {100, 100}, `mode`: `.aspectFit`,
  354. /// you will get an output image with size of {50, 100}, which "fit"s
  355. /// the `referenceSize`.
  356. ///
  357. /// If you need an output image exactly to be a specified size, append or use
  358. /// a `CroppingImageProcessor`.
  359. public init(referenceSize: CGSize, mode: ContentMode = .none) {
  360. self.referenceSize = referenceSize
  361. self.targetContentMode = mode
  362. if mode == .none {
  363. self.identifier = "com.onevcat.Kingfisher.ResizingImageProcessor(\(referenceSize))"
  364. } else {
  365. self.identifier = "com.onevcat.Kingfisher.ResizingImageProcessor(\(referenceSize), \(mode))"
  366. }
  367. }
  368. /// Process an input `ImageProcessItem` item to an image for this processor.
  369. ///
  370. /// - parameter item: Input item which will be processed by `self`
  371. /// - parameter options: Options when processing the item.
  372. ///
  373. /// - returns: The processed image.
  374. ///
  375. /// - Note: See documentation of `ImageProcessor` protocol for more.
  376. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  377. switch item {
  378. case .image(let image):
  379. return image.kf.scaled(to: options.scaleFactor)
  380. .kf.resize(to: referenceSize, for: targetContentMode)
  381. case .data:
  382. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  383. }
  384. }
  385. }
  386. /// Processor for adding blur effect to images. `Accelerate.framework` is used underhood for
  387. /// a better performance. A simulated Gaussian blur with specified blur radius will be applied.
  388. public struct BlurImageProcessor: ImageProcessor {
  389. /// Identifier of the processor.
  390. /// - Note: See documentation of `ImageProcessor` protocol for more.
  391. public let identifier: String
  392. /// Blur radius for the simulated Gaussian blur.
  393. public let blurRadius: CGFloat
  394. /// Initialize a `BlurImageProcessor`
  395. ///
  396. /// - parameter blurRadius: Blur radius for the simulated Gaussian blur.
  397. public init(blurRadius: CGFloat) {
  398. self.blurRadius = blurRadius
  399. self.identifier = "com.onevcat.Kingfisher.BlurImageProcessor(\(blurRadius))"
  400. }
  401. /// Process an input `ImageProcessItem` item to an image for this processor.
  402. ///
  403. /// - parameter item: Input item which will be processed by `self`
  404. /// - parameter options: Options when processing the item.
  405. ///
  406. /// - returns: The processed image.
  407. ///
  408. /// - Note: See documentation of `ImageProcessor` protocol for more.
  409. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  410. switch item {
  411. case .image(let image):
  412. let radius = blurRadius * options.scaleFactor
  413. return image.kf.scaled(to: options.scaleFactor)
  414. .kf.blurred(withRadius: radius)
  415. case .data:
  416. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  417. }
  418. }
  419. }
  420. /// Processor for adding an overlay to images. Only CG-based images are supported in macOS.
  421. public struct OverlayImageProcessor: ImageProcessor {
  422. /// Identifier of the processor.
  423. /// - Note: See documentation of `ImageProcessor` protocol for more.
  424. public let identifier: String
  425. /// Overlay color will be used to overlay the input image.
  426. public let overlay: Color
  427. /// Fraction will be used when overlay the color to image.
  428. public let fraction: CGFloat
  429. /// Initialize an `OverlayImageProcessor`
  430. ///
  431. /// - parameter overlay: Overlay color will be used to overlay the input image.
  432. /// - parameter fraction: Fraction will be used when overlay the color to image.
  433. /// From 0.0 to 1.0. 0.0 means solid color, 1.0 means transparent overlay.
  434. public init(overlay: Color, fraction: CGFloat = 0.5) {
  435. self.overlay = overlay
  436. self.fraction = fraction
  437. self.identifier = "com.onevcat.Kingfisher.OverlayImageProcessor(\(overlay.hex)_\(fraction))"
  438. }
  439. /// Process an input `ImageProcessItem` item to an image for this processor.
  440. ///
  441. /// - parameter item: Input item which will be processed by `self`
  442. /// - parameter options: Options when processing the item.
  443. ///
  444. /// - returns: The processed image.
  445. ///
  446. /// - Note: See documentation of `ImageProcessor` protocol for more.
  447. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  448. switch item {
  449. case .image(let image):
  450. return image.kf.scaled(to: options.scaleFactor)
  451. .kf.overlaying(with: overlay, fraction: fraction)
  452. case .data:
  453. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  454. }
  455. }
  456. }
  457. /// Processor for tint images with color. Only CG-based images are supported.
  458. public struct TintImageProcessor: ImageProcessor {
  459. /// Identifier of the processor.
  460. /// - Note: See documentation of `ImageProcessor` protocol for more.
  461. public let identifier: String
  462. /// Tint color will be used to tint the input image.
  463. public let tint: Color
  464. /// Initialize a `TintImageProcessor`
  465. ///
  466. /// - parameter tint: Tint color will be used to tint the input image.
  467. public init(tint: Color) {
  468. self.tint = tint
  469. self.identifier = "com.onevcat.Kingfisher.TintImageProcessor(\(tint.hex))"
  470. }
  471. /// Process an input `ImageProcessItem` item to an image for this processor.
  472. ///
  473. /// - parameter item: Input item which will be processed by `self`
  474. /// - parameter options: Options when processing the item.
  475. ///
  476. /// - returns: The processed image.
  477. ///
  478. /// - Note: See documentation of `ImageProcessor` protocol for more.
  479. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  480. switch item {
  481. case .image(let image):
  482. return image.kf.scaled(to: options.scaleFactor)
  483. .kf.tinted(with: tint)
  484. case .data:
  485. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  486. }
  487. }
  488. }
  489. /// Processor for applying some color control to images. Only CG-based images are supported.
  490. /// watchOS is not supported.
  491. public struct ColorControlsProcessor: ImageProcessor {
  492. /// Identifier of the processor.
  493. /// - Note: See documentation of `ImageProcessor` protocol for more.
  494. public let identifier: String
  495. /// Brightness changing to image.
  496. public let brightness: CGFloat
  497. /// Contrast changing to image.
  498. public let contrast: CGFloat
  499. /// Saturation changing to image.
  500. public let saturation: CGFloat
  501. /// InputEV changing to image.
  502. public let inputEV: CGFloat
  503. /// Initialize a `ColorControlsProcessor`
  504. ///
  505. /// - parameter brightness: Brightness changing to image.
  506. /// - parameter contrast: Contrast changing to image.
  507. /// - parameter saturation: Saturation changing to image.
  508. /// - parameter inputEV: InputEV changing to image.
  509. public init(brightness: CGFloat, contrast: CGFloat, saturation: CGFloat, inputEV: CGFloat) {
  510. self.brightness = brightness
  511. self.contrast = contrast
  512. self.saturation = saturation
  513. self.inputEV = inputEV
  514. self.identifier = "com.onevcat.Kingfisher.ColorControlsProcessor(\(brightness)_\(contrast)_\(saturation)_\(inputEV))"
  515. }
  516. /// Process an input `ImageProcessItem` item to an image for this processor.
  517. ///
  518. /// - parameter item: Input item which will be processed by `self`
  519. /// - parameter options: Options when processing the item.
  520. ///
  521. /// - returns: The processed image.
  522. ///
  523. /// - Note: See documentation of `ImageProcessor` protocol for more.
  524. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  525. switch item {
  526. case .image(let image):
  527. return image.kf.scaled(to: options.scaleFactor)
  528. .kf.adjusted(brightness: brightness, contrast: contrast, saturation: saturation, inputEV: inputEV)
  529. case .data:
  530. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  531. }
  532. }
  533. }
  534. /// Processor for applying black and white effect to images. Only CG-based images are supported.
  535. /// watchOS is not supported.
  536. public struct BlackWhiteProcessor: ImageProcessor {
  537. /// Identifier of the processor.
  538. /// - Note: See documentation of `ImageProcessor` protocol for more.
  539. public let identifier = "com.onevcat.Kingfisher.BlackWhiteProcessor"
  540. /// Initialize a `BlackWhiteProcessor`
  541. public init() {}
  542. /// Process an input `ImageProcessItem` item to an image for this processor.
  543. ///
  544. /// - parameter item: Input item which will be processed by `self`
  545. /// - parameter options: Options when processing the item.
  546. ///
  547. /// - returns: The processed image.
  548. ///
  549. /// - Note: See documentation of `ImageProcessor` protocol for more.
  550. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  551. return ColorControlsProcessor(brightness: 0.0, contrast: 1.0, saturation: 0.0, inputEV: 0.7)
  552. .process(item: item, options: options)
  553. }
  554. }
  555. /// Processor for cropping an image. Only CG-based images are supported.
  556. /// watchOS is not supported.
  557. public struct CroppingImageProcessor: ImageProcessor {
  558. /// Identifier of the processor.
  559. /// - Note: See documentation of `ImageProcessor` protocol for more.
  560. public let identifier: String
  561. /// Target size of output image should be.
  562. public let size: CGSize
  563. /// Anchor point from which the output size should be calculate.
  564. /// The anchor point is consisted by two values between 0.0 and 1.0.
  565. /// It indicates a related point in current image.
  566. /// See `CroppingImageProcessor.init(size:anchor:)` for more.
  567. public let anchor: CGPoint
  568. /// Initialize a `CroppingImageProcessor`
  569. ///
  570. /// - Parameters:
  571. /// - size: Target size of output image should be.
  572. /// - anchor: The anchor point from which the size should be calculated.
  573. /// Default is `CGPoint(x: 0.5, y: 0.5)`, which means the center of input image.
  574. /// - Note:
  575. /// The anchor point is consisted by two values between 0.0 and 1.0.
  576. /// It indicates a related point in current image, eg: (0.0, 0.0) for top-left
  577. /// corner, (0.5, 0.5) for center and (1.0, 1.0) for bottom-right corner.
  578. /// The `size` property of `CroppingImageProcessor` will be used along with
  579. /// `anchor` to calculate a target rectangle in the size of image.
  580. ///
  581. /// The target size will be automatically calculated with a reasonable behavior.
  582. /// For example, when you have an image size of `CGSize(width: 100, height: 100)`,
  583. /// and a target size of `CGSize(width: 20, height: 20)`:
  584. /// - with a (0.0, 0.0) anchor (top-left), the crop rect will be `{0, 0, 20, 20}`;
  585. /// - with a (0.5, 0.5) anchor (center), it will be `{40, 40, 20, 20}`
  586. /// - while with a (1.0, 1.0) anchor (bottom-right), it will be `{80, 80, 20, 20}`
  587. public init(size: CGSize, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5)) {
  588. self.size = size
  589. self.anchor = anchor
  590. self.identifier = "com.onevcat.Kingfisher.CroppingImageProcessor(\(size)_\(anchor))"
  591. }
  592. /// Process an input `ImageProcessItem` item to an image for this processor.
  593. ///
  594. /// - parameter item: Input item which will be processed by `self`
  595. /// - parameter options: Options when processing the item.
  596. ///
  597. /// - returns: The processed image.
  598. ///
  599. /// - Note: See documentation of `ImageProcessor` protocol for more.
  600. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  601. switch item {
  602. case .image(let image):
  603. return image.kf.scaled(to: options.scaleFactor)
  604. .kf.crop(to: size, anchorOn: anchor)
  605. case .data: return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  606. }
  607. }
  608. }
  609. /// Concatenate two `ImageProcessor`s. `ImageProcessor.appen(another:)` is used internally.
  610. ///
  611. /// - parameter left: First processor.
  612. /// - parameter right: Second processor.
  613. ///
  614. /// - returns: The concatenated processor.
  615. public func >>(left: ImageProcessor, right: ImageProcessor) -> ImageProcessor {
  616. return left.append(another: right)
  617. }
  618. extension Color {
  619. var hex: String {
  620. var r: CGFloat = 0
  621. var g: CGFloat = 0
  622. var b: CGFloat = 0
  623. var a: CGFloat = 0
  624. #if os(macOS)
  625. (usingColorSpace(.sRGB) ?? self).getRed(&r, green: &g, blue: &b, alpha: &a)
  626. #else
  627. getRed(&r, green: &g, blue: &b, alpha: &a)
  628. #endif
  629. let rInt = Int(r * 255) << 24
  630. let gInt = Int(g * 255) << 16
  631. let bInt = Int(b * 255) << 8
  632. let aInt = Int(a * 255)
  633. let rgba = rInt | gInt | bInt | aInt
  634. return String(format:"#%08x", rgba)
  635. }
  636. }