8000 GitHub - gewill/Swift-WebP: A thin libwebp wrapper in Swift offering both encode and decode APIs · GitHub
[go: up one dir, main page]

Skip to content
forked from ainame/Swift-WebP

About

A thin libwebp wrapper in Swift offering both encode and decode APIs

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

Swift-WebP

Important

This is an unofficial community package. It is not affiliated with, endorsed by, or maintained by Google or the WebP project.

Swift-WebP provides Swift wrappers around libwebp for encoding, decoding, and bitstream inspection.

Notice

v0.7.0 renamed the package product and module to SwiftWebP. See MIGRATION.md and CHANGELOG.md for upgrade notes.

Support Versions

  • Swift toolchain: 6.2.3 (.swift-version)
  • Swift language mode: 6
  • libwebp: 1.6.0+ (via webp-spm)
  • iOS deployment target: 13.0+
  • macOS deployment target: 11.0+

Features

  • 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)

Installation

Add Swift-WebP in your Package.swift:

.package(name: "SwiftWebP", url: "https://github.com/gewill/Swift-WebP.git", from: "0.7.0")

Then add the SwiftWebP product to your target dependencies and import the SwiftWebP module.

.product(name: "SwiftWebP", package: "SwiftWebP")

Development

Common local commands:

make format
swift build
swift test

make format runs the SwiftFormat SPM plugin.

Resource benchmark + validation:

Scripts/benchmark-resource.sh
Scripts/validate-resource.sh
Scripts/compare-with-cwebp.sh

You 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.

Usage

Encoding

import SwiftWebP

let encoder = WebPEncoder()
let data = try encoder.encode(
    rgbaPointer,
    format: .rgba,
    config: .preset(.picture, quality: 95),
    originWidth: width,
    originHeight: height,
    stride: width * 4
)

Decoding to raw pixel bytes

import SwiftWebP

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)

Decoding into caller-owned memory

import SwiftWebP

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 output.withUnsafeMutableBufferPointer { buffer in
    try decoder.decode(webPData, into: buffer, options: options, format: .rgba)
}
print("decoded bytes:", written)

Decoding to platform images

#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)
#endif

Inspecting WebP metadata

let feature = try WebPImageInspector.inspect(webPData)
print(feature.width, feature.height, feature.hasAlpha, feature.hasAnimation)

License and Notices

Swift-WebP is available under the MIT license.

This project is an independent Swift wrapper around libwebp. WebP and libwebp are names of their respective projects and rightsholders; this repository does not claim ownership of those projects or imply endorsement.

Third-party license references:

About

A thin libwebp wrapper in Swift offering both encode and decode APIs

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0