/* Copyright (c) 2018 Wolt Enterprises Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ // swiftlint:disable all import UIKit extension UIImage { public func blurHash(numberOfComponents components: (Int, Int)) -> String? { let pixelWidth = Int(round(size.width * scale)) let pixelHeight = Int(round(size.height * scale)) let context = CGContext(data: nil, width: pixelWidth, height: pixelHeight, bitsPerComponent: 8, bytesPerRow: pixelWidth * 4, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)! context.scaleBy(x: scale, y: -scale) context.translateBy(x: 0, y: -size.height) UIGraphicsPushContext(context) draw(at: .zero) UIGraphicsPopContext() guard let cgImage = context.makeImage(), let dataProvider = cgImage.dataProvider, let data = dataProvider.data, let pixels = CFDataGetBytePtr(data) else { assertionFailure("Unexpected error!") return nil } let width = cgImage.width let height = cgImage.height let bytesPerRow = cgImage.bytesPerRow var factors: [(Float, Float, Float)] = [] for y in 0.. 0 { let actualMaximumValue = ac.map { max(abs($0.0), abs($0.1), abs($0.2)) }.max()! let quantisedMaximumValue = Int(max(0, min(82, floor(actualMaximumValue * 166 - 0.5)))) maximumValue = Float(quantisedMaximumValue + 1) / 166 hash += quantisedMaximumValue.encode83(length: 1) } else { maximumValue = 1 hash += 0.encode83(length: 1) } hash += encodeDC(dc).encode83(length: 4) for factor in ac { hash += encodeAC(factor, maximumValue: maximumValue).encode83(length: 2) } return hash } private func multiplyBasisFunction(pixels: UnsafePointer, width: Int, height: Int, bytesPerRow: Int, bytesPerPixel: Int, pixelOffset: Int, basisFunction: (Float, Float) -> Float) -> (Float, Float, Float) { var r: Float = 0 var g: Float = 0 var b: Float = 0 let buffer = UnsafeBufferPointer(start: pixels, count: height * bytesPerRow) for x in 0.. Int { let roundedR = linearTosRGB(value.0) let roundedG = linearTosRGB(value.1) let roundedB = linearTosRGB(value.2) return (roundedR << 16) + (roundedG << 8) + roundedB } private func encodeAC(_ value: (Float, Float, Float), maximumValue: Float) -> Int { let quantR = Int(max(0, min(18, floor(signPow(value.0 / maximumValue, 0.5) * 9 + 9.5)))) let quantG = Int(max(0, min(18, floor(signPow(value.1 / maximumValue, 0.5) * 9 + 9.5)))) let quantB = Int(max(0, min(18, floor(signPow(value.2 / maximumValue, 0.5) * 9 + 9.5)))) return quantR * 19 * 19 + quantG * 19 + quantB } private func signPow(_ value: Float, _ exp: Float) -> Float { copysign(pow(abs(value), exp), value) } private func linearTosRGB(_ value: Float) -> Int { let v = max(0, min(1, value)) if v <= 0.0031308 { return Int(v * 12.92 * 255 + 0.5) } else { return Int((1.055 * pow(v, 1 / 2.4) - 0.055) * 255 + 0.5) } } private func sRGBToLinear(_ value: Type) -> Float { let v = Float(Int64(value)) / 255 if v <= 0.04045 { return v / 12.92 } else { return pow((v + 0.055) / 1.055, 2.4) } } private let encodeCharacters: [String] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~".map { String($0) } extension BinaryInteger { func encode83(length: Int) -> String { var result = "" for i in 1...length { let digit = (Int(self) / pow(83, length - i)) % 83 result += encodeCharacters[Int(digit)] } return result } } private func pow(_ base: Int, _ exponent: Int) -> Int { (0..