* refactor(mobile): move the native core decoders into a codecs crate * make Format private * rename the decode export and options for what they hold * drop the unused ios and server groups and rename the target params (cherry picked from commit 11bf875313782b8a9e10269bb6f85226b7bdcd72)
40 lines
1.8 KiB
Swift
40 lines
1.8 KiB
Swift
import Foundation
|
|
|
|
enum NativeCore {
|
|
private static let library = dlopen("@rpath/native_core_ffi.framework/native_core_ffi", RTLD_NOW)
|
|
|
|
private static func symbol<T>(_ name: String, as type: T.Type) -> T? {
|
|
guard let library, let symbol = dlsym(library, name) else { return nil }
|
|
return unsafeBitCast(symbol, to: type)
|
|
}
|
|
|
|
private static let coreThumbhash = symbol("native_core_thumbhash", as: NativeCoreThumbhashFn.self)
|
|
private static let coreDecodeImage = symbol("native_core_decode_image", as: NativeCoreDecodeImageFn.self)
|
|
private static let coreCancel = symbol("native_core_cancel", as: NativeCoreCancelFn.self)
|
|
|
|
static func thumbhash(_ hash: Data) -> (width: Int, height: Int, pointer: UnsafeMutableRawPointer)? {
|
|
guard let coreThumbhash else { return nil }
|
|
var width: Int32 = 0
|
|
var height: Int32 = 0
|
|
return hash.withUnsafeBytes { bytes in
|
|
guard let pointer = coreThumbhash(bytes.bindMemory(to: UInt8.self).baseAddress, bytes.count, &width, &height)
|
|
else { return nil }
|
|
return (Int(width), Int(height), UnsafeMutableRawPointer(pointer))
|
|
}
|
|
}
|
|
|
|
static func decodeImage(_ data: Data, targetWidth: Int64?, targetHeight: Int64?, maxPixelSize: Int, cancelled: UnsafeMutablePointer<Bool>) -> (width: Int, height: Int, pointer: UnsafeMutableRawPointer)? {
|
|
guard let coreDecodeImage else { return nil }
|
|
var info = NativeCoreImageInfo()
|
|
return data.withUnsafeBytes { bytes in
|
|
guard let pointer = coreDecodeImage(bytes.bindMemory(to: UInt8.self).baseAddress, bytes.count, false, UInt32(clamping: targetWidth ?? 0), UInt32(clamping: targetHeight ?? 0), UInt32(clamping: maxPixelSize), cancelled, &info)
|
|
else { return nil }
|
|
return (Int(info.width), Int(info.height), UnsafeMutableRawPointer(pointer))
|
|
}
|
|
}
|
|
|
|
static func cancel(_ flag: UnsafeMutablePointer<Bool>) {
|
|
coreCancel?(flag)
|
|
}
|
|
}
|