-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move across basic WebP Swift wrapper from stackotter/swift-libwebp
- Loading branch information
1 parent
db004ff
commit f0843ab
Showing
1 changed file
with
63 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,69 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright Contributors to the WebP Codec project | ||
|
||
@_exported import libwebp | ||
@_exported import sharpyuv | ||
@_exported import imageioutil | ||
import libwebp | ||
import Foundation | ||
|
||
public enum WebP | ||
public enum WebPError: Error | ||
{ | ||
public static let version = "1.4.0" | ||
case unknownDecodingError | ||
case unknownEncodingError | ||
} | ||
|
||
public struct WebP | ||
{ | ||
public static let version = "1.4.0" | ||
|
||
public var width: Int | ||
public var height: Int | ||
public var rgba: [UInt8] | ||
|
||
public init(width: Int, height: Int, rgba: [UInt8]) | ||
{ | ||
self.width = width | ||
self.height = height | ||
self.rgba = rgba | ||
} | ||
|
||
public static func decode(_ data: [UInt8]) throws -> WebP | ||
{ | ||
var width: Int32 = 0 | ||
var height: Int32 = 0 | ||
guard let rgba = WebPDecodeRGBA(data, data.count, &width, &height) else | ||
{ | ||
throw WebPError.unknownDecodingError | ||
} | ||
|
||
let webp = WebP( | ||
width: Int(width), | ||
height: Int(height), | ||
rgba: [UInt8](Data(bytes: rgba, count: Int(width * height * 4))) | ||
) | ||
WebPFree(rgba) | ||
return webp | ||
} | ||
|
||
/// - Parameter quality: A value from 0 to 100 which controls output quality. 100 produces | ||
/// the best quality output but also the largest output. | ||
public func encode(quality: Float) throws -> [UInt8] | ||
{ | ||
var data: UnsafeMutablePointer<UInt8>? | ||
let size = WebPEncodeRGBA( | ||
rgba, | ||
Int32(width), | ||
Int32(height), | ||
Int32(width * 4), | ||
quality, | ||
&data | ||
) | ||
|
||
guard let data = data else | ||
{ | ||
throw WebPError.unknownEncodingError | ||
} | ||
|
||
let dataCopy = [UInt8](Data(bytes: data, count: Int(size))) | ||
WebPFree(data) | ||
return dataCopy | ||
} | ||
} |