Swift-WebP provides Swift wrappers around libwebp for encoding, decoding, and bitstream inspection.
Encode pixels from arrays, Data, or spans, and decode into Data or reusable buffers. Swift-WebP handles buffer validation and memory cleanup for you, with efficient decoding that avoids unnecessary output copies.
0.7.0 contains behavior changes to encoder input validation and UIImage output size.
See details in CHANGELOG.md
- Minimum Swift version: 6.2 (
swift-tools-version: 6.2inPackage.swift) - Swift language mode: 6
- libwebp: 1.6.0+ (via
libwebp-Xcode) - iOS deployment target: 13.0+
- macOS deployment target: 11.0+
- Swift Package Manager support
- Advanced encoding via
WebPEncoder+WebPEncoderConfig - Advanced decoding via
WebPDecoder+WebPDecoderOptions - WebP bitstream inspection via
WebPImageInspector - Cross-platform core APIs (Apple platforms + Linux)
Add Swift-WebP in your Package.swift:
.package(url: "https://github.com/ainame/Swift-WebP.git", from: "0.7.0")import WebP
let encoder = WebPEncoder()
let data = try encoder.encode(
rgbaBytes, // [UInt8], Data, or a borrowed Span<UInt8>
format: .rgba,
config: .preset(.picture, quality: 95),
originWidth: width,
originHeight: height,
stride: width * 4
)The encoder validates dimensions, stride, and input capacity. Provide at least stride * originHeight bytes, including padding after the final row. Borrowed spans avoid copying the input storage.
// Handles any bitmap layout: premultiplied, BGRA, and 16-bit images are converted first.
let data = try encoder.encode(normalizing: cgImage, config: .preset(.picture, quality: 95))
// Encodes the backing bytes as-is; `format` must match the image's layout with straight alpha.
let raw = try encoder.encode(cgImage, format: .rgba, config: .preset(.picture, quality: 95))Images from drawing or screen capture are premultiplied, so use encode(normalizing:) for them. The NSImage and UIImage encoders use it too.
import WebP
let decoder = WebPDecoder()
var options = WebPDecoderOptions()
options.useScaling = true
options.scaledWidth = targetWidth
options.scaledHeight = targetHeight
let rgbaData = try decoder.decode(webPData, options: options, format: .rgba)Set either scaled dimension to 0 to infer it while preserving the aspect ratio (after cropping, if enabled). Invalid decoder options throw WebPDecodingError.invalidParam.
import WebP
let decoder = WebPDecoder()
var options = WebPDecoderOptions()
let required = try decoder.requiredOutputByteCount(for: webPData, options: options, format: .rgba)
var output = [UInt8](repeating: 0, count: required)
let written = try decoder.decode(webPData, into: &output, options: options, format: .rgba)
print("decoded bytes:", written)#if canImport(CoreGraphics)
let cgImage = try decoder.decodeCGImage(from: webPData, options: options)
#endif
#if canImport(UIKit)
let image = try decoder.decodeUIImage(from: webPData, options: options)
#endif
#if canImport(AppKit)
let image = try decoder.decodeNSImage(from: webPData, options: options)
#endiflet feature = try WebPImageInspector.inspect(webPData)
print(feature.width, feature.height, feature.hasAlpha, feature.hasAnimation)The local development toolchain is Swift 6.4.0, selected by .swift-version. This file does not define the minimum supported Swift version.
Common local commands:
make format
swift build
swift testmake format runs the SwiftFormat SPM plugin.
The library enables strict memory-safety checking. Intentional C operations are marked with unsafe at the interoperability boundary; pointer-based APIs and low-level buffer configuration remain unsafe interfaces. Array, Data, and Span entry points handle those requirements internally.
See the benchmark guide for reproducible version comparisons, pipeline benchmarks, metric definitions, and interpretation limits.
Resource benchmark + validation:
Scripts/benchmark-resource.sh
Scripts/validate-resource.sh
Scripts/compare-with-cwebp.shYou can tune benchmark parameters with env vars such as:
MODE=pipeline|source-decode-only|encode-only|decode-only,
WIDTH, HEIGHT, ITERATIONS, WARMUP, QUALITY, THREADS_FLAG=off,
INPUT=/absolute/path/to/image, SOURCE_DECODE_PER_ITERATION=on.
Validation thresholds can be tuned with:
MAX_SOURCE_DECODE_AVG_MS, MAX_ENCODE_AVG_MS, MAX_DECODE_AVG_MS,
MAX_PIPELINE_ENCODE_AVG_MS, MAX_ENCODE_P95_MS, MAX_DECODE_P95_MS,
MAX_STAGE_PEAK_RSS_MB, MAX_PIPELINE_PEAK_RSS_MB.
Swift-WebP is available under the MIT license. See LICENSE.