You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
10 KiB

  1. //
  2. // ImageProgressive.swift
  3. // Kingfisher
  4. //
  5. // Created by lixiang on 2019/5/10.
  6. //
  7. // Copyright (c) 2019 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. private let sharedProcessingQueue: CallbackQueue =
  29. .dispatch(DispatchQueue(label: "com.onevcat.Kingfisher.ImageDownloader.Process"))
  30. public struct ImageProgressive {
  31. /// A default `ImageProgressive` could be used across.
  32. public static let `default` = ImageProgressive(
  33. isBlur: true,
  34. isFastestScan: true,
  35. scanInterval: 0
  36. )
  37. /// Whether to enable blur effect processing
  38. let isBlur: Bool
  39. /// Whether to enable the fastest scan
  40. let isFastestScan: Bool
  41. /// Minimum time interval for each scan
  42. let scanInterval: TimeInterval
  43. public init(isBlur: Bool,
  44. isFastestScan: Bool,
  45. scanInterval: TimeInterval) {
  46. self.isBlur = isBlur
  47. self.isFastestScan = isFastestScan
  48. self.scanInterval = scanInterval
  49. }
  50. }
  51. protocol ImageSettable: AnyObject {
  52. var image: Image? { get set }
  53. }
  54. final class ImageProgressiveProvider: DataReceivingSideEffect {
  55. var onShouldApply: () -> Bool = { return true }
  56. func onDataReceived(_ session: URLSession, task: SessionDataTask, data: Data) {
  57. update(data: task.mutableData, with: task.callbacks)
  58. }
  59. private let option: ImageProgressive
  60. private let refresh: (Image) -> Void
  61. private let decoder: ImageProgressiveDecoder
  62. private let queue = ImageProgressiveSerialQueue()
  63. init?(_ options: KingfisherParsedOptionsInfo,
  64. refresh: @escaping (Image) -> Void) {
  65. guard let option = options.progressiveJPEG else { return nil }
  66. self.option = option
  67. self.refresh = refresh
  68. self.decoder = ImageProgressiveDecoder(
  69. option,
  70. processingQueue: options.processingQueue ?? sharedProcessingQueue,
  71. creatingOptions: options.imageCreatingOptions
  72. )
  73. }
  74. func update(data: Data, with callbacks: [SessionDataTask.TaskCallback]) {
  75. guard !data.isEmpty else { return }
  76. queue.add(minimum: option.scanInterval) { completion in
  77. guard self.onShouldApply() else {
  78. self.queue.clean()
  79. completion()
  80. return
  81. }
  82. func decode(_ data: Data) {
  83. self.decoder.decode(data, with: callbacks) { image in
  84. defer { completion() }
  85. guard self.onShouldApply() else { return }
  86. guard let image = image else { return }
  87. self.refresh(image)
  88. }
  89. }
  90. if self.option.isFastestScan {
  91. decode(self.decoder.scanning(data) ?? Data())
  92. } else {
  93. self.decoder.scanning(data).forEach { decode($0) }
  94. }
  95. }
  96. }
  97. }
  98. private final class ImageProgressiveDecoder {
  99. private let option: ImageProgressive
  100. private let processingQueue: CallbackQueue
  101. private let creatingOptions: ImageCreatingOptions
  102. private(set) var scannedCount = 0
  103. private(set) var scannedIndex = -1
  104. init(_ option: ImageProgressive,
  105. processingQueue: CallbackQueue,
  106. creatingOptions: ImageCreatingOptions) {
  107. self.option = option
  108. self.processingQueue = processingQueue
  109. self.creatingOptions = creatingOptions
  110. }
  111. func scanning(_ data: Data) -> [Data] {
  112. guard data.kf.contains(jpeg: .SOF2) else {
  113. return []
  114. }
  115. guard scannedIndex + 1 < data.count else {
  116. return []
  117. }
  118. var datas: [Data] = []
  119. var index = scannedIndex + 1
  120. var count = scannedCount
  121. while index < data.count - 1 {
  122. scannedIndex = index
  123. // 0xFF, 0xDA - Start Of Scan
  124. let SOS = ImageFormat.JPEGMarker.SOS.bytes
  125. if data[index] == SOS[0], data[index + 1] == SOS[1] {
  126. if count > 0 {
  127. datas.append(data[0 ..< index])
  128. }
  129. count += 1
  130. }
  131. index += 1
  132. }
  133. // Found more scans this the previous time
  134. guard count > scannedCount else { return [] }
  135. scannedCount = count
  136. // `> 1` checks that we've received a first scan (SOS) and then received
  137. // and also received a second scan (SOS). This way we know that we have
  138. // at least one full scan available.
  139. guard count > 1 else { return [] }
  140. return datas
  141. }
  142. func scanning(_ data: Data) -> Data? {
  143. guard data.kf.contains(jpeg: .SOF2) else {
  144. return nil
  145. }
  146. guard scannedIndex + 1 < data.count else {
  147. return nil
  148. }
  149. var index = scannedIndex + 1
  150. var count = scannedCount
  151. var lastSOSIndex = 0
  152. while index < data.count - 1 {
  153. scannedIndex = index
  154. // 0xFF, 0xDA - Start Of Scan
  155. let SOS = ImageFormat.JPEGMarker.SOS.bytes
  156. if data[index] == SOS[0], data[index + 1] == SOS[1] {
  157. lastSOSIndex = index
  158. count += 1
  159. }
  160. index += 1
  161. }
  162. // Found more scans this the previous time
  163. guard count > scannedCount else { return nil }
  164. scannedCount = count
  165. // `> 1` checks that we've received a first scan (SOS) and then received
  166. // and also received a second scan (SOS). This way we know that we have
  167. // at least one full scan available.
  168. guard count > 1 && lastSOSIndex > 0 else { return nil }
  169. return data[0 ..< lastSOSIndex]
  170. }
  171. func decode(_ data: Data,
  172. with callbacks: [SessionDataTask.TaskCallback],
  173. completion: @escaping (Image?) -> Void) {
  174. guard data.kf.contains(jpeg: .SOF2) else {
  175. CallbackQueue.mainCurrentOrAsync.execute { completion(nil) }
  176. return
  177. }
  178. func processing(_ data: Data) {
  179. let processor = ImageDataProcessor(
  180. data: data,
  181. callbacks: callbacks,
  182. processingQueue: processingQueue
  183. )
  184. processor.onImageProcessed.delegate(on: self) { (self, result) in
  185. guard let image = try? result.0.get() else {
  186. CallbackQueue.mainCurrentOrAsync.execute { completion(nil) }
  187. return
  188. }
  189. CallbackQueue.mainCurrentOrAsync.execute { completion(image) }
  190. }
  191. processor.process()
  192. }
  193. // Blur partial images.
  194. let count = scannedCount
  195. if option.isBlur, count < 6 {
  196. processingQueue.execute {
  197. // Progressively reduce blur as we load more scans.
  198. let image = KingfisherWrapper<Image>.image(
  199. data: data,
  200. options: self.creatingOptions
  201. )
  202. let radius = max(2, 14 - count * 4)
  203. let temp = image?.kf.blurred(withRadius: CGFloat(radius))
  204. processing(temp?.kf.data(format: .JPEG) ?? data)
  205. }
  206. } else {
  207. processing(data)
  208. }
  209. }
  210. }
  211. private final class ImageProgressiveSerialQueue {
  212. typealias ClosureCallback = ((@escaping () -> Void)) -> Void
  213. private let queue: DispatchQueue = .init(label: "com.onevcat.Kingfisher.ImageProgressive.SerialQueue")
  214. private var items: [DispatchWorkItem] = []
  215. private var notify: (() -> Void)?
  216. private var lastTime: TimeInterval?
  217. var count: Int { return items.count }
  218. func add(minimum interval: TimeInterval, closure: @escaping ClosureCallback) {
  219. let completion = { [weak self] in
  220. guard let self = self else { return }
  221. self.queue.async { [weak self] in
  222. guard let self = self else { return }
  223. guard !self.items.isEmpty else { return }
  224. self.items.removeFirst()
  225. if let next = self.items.first {
  226. self.queue.asyncAfter(
  227. deadline: .now() + interval,
  228. execute: next
  229. )
  230. } else {
  231. self.lastTime = Date().timeIntervalSince1970
  232. self.notify?()
  233. self.notify = nil
  234. }
  235. }
  236. }
  237. queue.async { [weak self] in
  238. guard let self = self else { return }
  239. let item = DispatchWorkItem {
  240. closure(completion)
  241. }
  242. if self.items.isEmpty {
  243. let difference = Date().timeIntervalSince1970 - (self.lastTime ?? 0)
  244. let delay = difference < interval ? interval - difference : 0
  245. self.queue.asyncAfter(deadline: .now() + delay, execute: item)
  246. }
  247. self.items.append(item)
  248. }
  249. }
  250. func notify(_ closure: @escaping () -> Void) {
  251. self.notify = closure
  252. }
  253. func clean() {
  254. queue.async { [weak self] in
  255. guard let self = self else { return }
  256. self.items.forEach { $0.cancel() }
  257. self.items.removeAll()
  258. }
  259. }
  260. }