tayloraswift/swift-jpeg
π¦ **swift-jpeg** π¦
Requirements
The core framework has no external dependencies, including Foundation, and should compile and provide consistent behavior on all Swift platforms. The framework supports additional features, such as file system support, on Linux and MacOS.
The swift-jpeg library requires Swift 6.0 or later.
| Platform | Status | | -------- | ------ | | π¬ Documentation | [[Status]](https://github.com/tayloraswift/swift-jpeg/actions/workflows/Documentation.yml) | | π§ Linux | [[Status]](https://github.com/tayloraswift/swift-jpeg/actions/workflows/Tests.yml) | | π§ Linux (musl) | [[Status]](https://github.com/tayloraswift/swift-jpeg/actions/workflows/Tests.yml) | | π Darwin | [[Status]](https://github.com/tayloraswift/swift-jpeg/actions/workflows/Tests.yml) | | π Darwin (iOS) | [[Status]](https://github.com/tayloraswift/swift-jpeg/actions/workflows/Tests.yml) | | π Darwin (tvOS) | [[Status]](https://github.com/tayloraswift/swift-jpeg/actions/workflows/Tests.yml) | | π Darwin (visionOS) | [[Status]](https://github.com/tayloraswift/swift-jpeg/actions/workflows/Tests.yml) | | π Darwin (watchOS) | [[Status]](https://github.com/tayloraswift/swift-jpeg/actions/workflows/Tests.yml) | | π€ Android | [[Status]](https://github.com/tayloraswift/swift-jpeg/actions/workflows/Tests.yml) |
[tutorials and example programs](examples/)
[api reference](https://swiftinit.org/docs/swift-jpeg/jpeg/)
getting started
To use swift-jpeg in a project, add this descriptor to the dependencies list in your Package.swift:
.package(url: "https://github.com/tayloraswift/swift-jpeg", from: "2.0.0")basic usage
Decode an image:
import JPEG
func decode(jpeg path: String) throws {
guard
let image: JPEG.Data.Rectangular<JPEG.Common> = try .decompress(path: path) else {
// failed to access file from file system
}
let rgb: [JPEG.RGB] = image.unpack(as: JPEG.RGB.self)
let size: (x:Int, y:Int) = image.size
// ...
}Encode an image:
import JPEG
func encode(
jpeg path: String,
size: (x:Int, y:Int),
pixels rgb: [JPEG.RGB],
compression: Double, // 0.0 = highest quality
) throws {
let layout: JPEG.Layout<JPEG.Common> = .init(
format: .ycc8,
process: .baseline,
components: [
1: (factor: (2, 2), qi: 0), // Y
2: (factor: (1, 1), qi: 1), // Cb
3: (factor: (1, 1), qi: 1), // Cr
],
scans: [
.sequential((1, \.0, \.0), (2, \.1, \.1), (3, \.1, \.1)),
]
)
let jfif: JPEG.JFIF = .init(version: .v1_2, density: (72, 72, .inches))
let image: JPEG.Data.Rectangular<JPEG.Common> = .pack(
size: size,
layout: layout,
metadata: [.jfif(jfif)],
pixels: rgb
)
try image.compress(
path: path,
quanta: [
0: JPEG.CompressionLevel.luminance(compression).quanta,
1: JPEG.CompressionLevel.chrominance(compression).quanta
]
)
}Package Metadata
Repository: tayloraswift/swift-jpeg
Default branch: master
README: README.md