Contents

depasqualeorg/swift-stb-image

Swift package wrapper for the stb_image single-header C libraries. Provides cross-platform image decoding and high-quality resampling for Swift projects, particularly on Linux where CoreImage is not available.

Use

.package(url: "https://github.com/DePasqualeOrg/swift-stb-image", from: "0.1.0")

Then add Cstb to a target's dependencies and import Cstb in Swift code:

import Cstb

var width: Int32 = 0
var height: Int32 = 0
var channels: Int32 = 0
guard let pixels = stbi_loadf(path, &width, &height, &channels, 3) else {
    fatalError(String(cString: stbi_failure_reason()!))
}
defer { stbi_image_free(pixels) }

Refer to the upstream stb headers for the full API surface.

Updating the vendored code

Run scripts/update-vendored.sh to pull the latest stb_image.h and stb_image_resize2.h from nothings/stb. Optionally pass a git ref to pin to a specific commit or tag:

./scripts/update-vendored.sh           # tip of upstream master
./scripts/update-vendored.sh v2.30     # specific tag
./scripts/update-vendored.sh 31c1ad3   # specific commit

The script prints the resulting header versions and the upstream commit it pinned to, then shows a diff against the last committed state.

Platforms

The vendored C compiles cleanly on Linux, macOS, iOS, tvOS, visionOS, and Windows. On Apple platforms you'll usually want CoreImage instead — this package mainly exists so Swift projects targeting Linux don't need to shell out to apt-get install libpng-dev libjpeg-dev or pull in heavier dependencies.

License

The Swift wrapper code in this repository (Package.swift, cstb.c, the modulemap, and the documentation) is MIT-licensed; see LICENSE.

The vendored stb headers in Sources/Cstb/include/ are upstream-distributed under a dual MIT / Public Domain (Unlicense) license; the full text of both options is preserved verbatim at the bottom of each header file. This package consumes them under MIT to match the wrapper.

Package Metadata

Repository: depasqualeorg/swift-stb-image

Default branch: main

README: README.md