ImageFormat.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. }
  49. extension Data: KingfisherCompatibleValue {}
  50. // MARK: - Misc Helpers
  51. extension KingfisherWrapper where Base == Data {
  52. /// Gets the image format corresponding to the data.
  53. public var imageFormat: ImageFormat {
  54. var buffer = [UInt8](repeating: 0, count: 8)
  55. (base as NSData).getBytes(&buffer, length: 8)
  56. if buffer == ImageFormat.HeaderData.PNG {
  57. return .PNG
  58. } else if buffer[0] == ImageFormat.HeaderData.JPEG_SOI[0] &&
  59. buffer[1] == ImageFormat.HeaderData.JPEG_SOI[1] &&
  60. buffer[2] == ImageFormat.HeaderData.JPEG_IF[0]
  61. {
  62. return .JPEG
  63. } else if buffer[0] == ImageFormat.HeaderData.GIF[0] &&
  64. buffer[1] == ImageFormat.HeaderData.GIF[1] &&
  65. buffer[2] == ImageFormat.HeaderData.GIF[2]
  66. {
  67. return .GIF
  68. }
  69. return .unknown
  70. }
  71. }