ImageFormat.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // ImageFormat.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/09/28.
  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. /// Represents image format.
  28. ///
  29. /// - unknown: The format cannot be recognized or not supported yet.
  30. /// - PNG: PNG image format.
  31. /// - JPEG: JPEG image format.
  32. /// - GIF: GIF image format.
  33. public enum ImageFormat {
  34. /// The format cannot be recognized or not supported yet.
  35. case unknown
  36. /// PNG image format.
  37. case PNG
  38. /// JPEG image format.
  39. case JPEG
  40. /// GIF image format.
  41. case GIF
  42. struct HeaderData {
  43. static var PNG: [UInt8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
  44. static var JPEG_SOI: [UInt8] = [0xFF, 0xD8]
  45. static var JPEG_IF: [UInt8] = [0xFF]
  46. static var GIF: [UInt8] = [0x47, 0x49, 0x46]
  47. }
  48. /// https://en.wikipedia.org/wiki/JPEG
  49. public enum JPEGMarker {
  50. case SOF0 //baseline
  51. case SOF2 //progressive
  52. case DHT //Huffman Table
  53. case DQT //Quantization Table
  54. case DRI //Restart Interval
  55. case SOS //Start Of Scan
  56. case RSTn(UInt8) //Restart
  57. case APPn //Application-specific
  58. case COM //Comment
  59. case EOI //End Of Image
  60. var bytes: [UInt8] {
  61. switch self {
  62. case .SOF0: return [0xFF, 0xC0]
  63. case .SOF2: return [0xFF, 0xC2]
  64. case .DHT: return [0xFF, 0xC4]
  65. case .DQT: return [0xFF, 0xDB]
  66. case .DRI: return [0xFF, 0xDD]
  67. case .SOS: return [0xFF, 0xDA]
  68. case .RSTn(let n): return [0xFF, 0xD0 + n]
  69. case .APPn: return [0xFF, 0xE0]
  70. case .COM: return [0xFF, 0xFE]
  71. case .EOI: return [0xFF, 0xD9]
  72. }
  73. }
  74. }
  75. }
  76. extension Data: KingfisherCompatibleValue {}
  77. // MARK: - Misc Helpers
  78. extension KingfisherWrapper where Base == Data {
  79. /// Gets the image format corresponding to the data.
  80. public var imageFormat: ImageFormat {
  81. guard base.count > 8 else { return .unknown }
  82. var buffer = [UInt8](repeating: 0, count: 8)
  83. base.copyBytes(to: &buffer, count: 8)
  84. if buffer == ImageFormat.HeaderData.PNG {
  85. return .PNG
  86. } else if buffer[0] == ImageFormat.HeaderData.JPEG_SOI[0],
  87. buffer[1] == ImageFormat.HeaderData.JPEG_SOI[1],
  88. buffer[2] == ImageFormat.HeaderData.JPEG_IF[0]
  89. {
  90. return .JPEG
  91. } else if buffer[0] == ImageFormat.HeaderData.GIF[0],
  92. buffer[1] == ImageFormat.HeaderData.GIF[1],
  93. buffer[2] == ImageFormat.HeaderData.GIF[2]
  94. {
  95. return .GIF
  96. }
  97. return .unknown
  98. }
  99. public func contains(jpeg marker: ImageFormat.JPEGMarker) -> Bool {
  100. guard imageFormat == .JPEG else {
  101. return false
  102. }
  103. let bytes = [UInt8](base)
  104. let markerBytes = marker.bytes
  105. for (index, item) in bytes.enumerated() where bytes.count > index + 1 {
  106. guard
  107. item == markerBytes.first,
  108. bytes[index + 1] == markerBytes[1] else {
  109. continue
  110. }
  111. return true
  112. }
  113. return false
  114. }
  115. }