Contents

tayloraswift/swift-png

πŸ“·   **swift-png**   πŸ“Έ

Requirements

The swift-png library requires Swift 5.10 or later.

| Platform | Status | | -------- | ------ | | πŸ’¬ Documentation | [[Status]](https://github.com/tayloraswift/swift-png/actions/workflows/Documentation.yml) | | 🐧 Linux | [[Status]](https://github.com/tayloraswift/swift-png/actions/workflows/Tests.yml) | | 🍏 Darwin | [[Status]](https://github.com/tayloraswift/swift-png/actions/workflows/Tests.yml) | | 🍏 Darwin (iOS) | [[Status]](https://github.com/tayloraswift/swift-png/actions/workflows/Tests.yml) | | 🍏 Darwin (tvOS) | [[Status]](https://github.com/tayloraswift/swift-png/actions/workflows/Tests.yml) | | 🍏 Darwin (visionOS) | [[Status]](https://github.com/tayloraswift/swift-png/actions/workflows/Tests.yml) | | 🍏 Darwin (watchOS) | [[Status]](https://github.com/tayloraswift/swift-png/actions/workflows/Tests.yml) | | πŸ€– Android | [[Status]](https://github.com/tayloraswift/swift-png/actions/workflows/Tests.yml) |

Check deployment minimums

Getting started

To use swift-png in a project, add this descriptor to the dependencies list in your Package.swift file:

.package(url: "https://github.com/tayloraswift/swift-png", from: "4.5.0")

The library is powered by a native Swift DEFLATE implementation, which can be used as a standalone module.

Basic usage

Decode an image:

import PNG
func decode(png path: String) throws {
    guard
    let image: PNG.Image = try .decompress(path: path) else {
        // failed to access file from file system
    }

    let rgba: [PNG.RGBA<UInt8>] = image.unpack(as: PNG.RGBA<UInt8>.self),
        size: (x:Int, y:Int) = image.size
    // ...
}

Encode an image:

func encode(png path: String, size: (x:Int, y:Int), pixels: [PNG.RGBA<UInt8>]) throws {
    let image: PNG.Image = .init(
        packing: pixels,
        size: size,
        layout: .init(format: .rgba8(palette: [], fill: nil))
    )
    try image.compress(path: path, level: 9)
}

Features

  • Superior compression. swift-png supports minimum cost path-based DEFLATE optimization, which is why it offers four additional compression levels beyond what libpng supports.
  • Competitive performance. swift-png offers competitive performance compared to libpng. On appropriate CPU architectures, the swift-png encoder makes use of hardware-accelerated hash tables for even greater performance.
  • Pure Swift, all the way down. swift-png is powered by its own, native Swift DEFLATE implementation. It depends only on other Foundation-less, pure-Swift libraries, and therefore does not need to link Foundation. This also means the core components of swift-png work on any platform that Swift itself works on, and that swift-png’s performance improves as the Swift compiler matures.
  • First-class iPhone optimization support. swift-png requires no custom setup or third-party plugins to handle iPhone-optimized PNG images. iPhone-optimized images just work, on all platforms. Reproduce pngcrush’s output with bit width-aware alpha premultiplication), for seamless integration anywhere in your application stack.

See also

Package Metadata

Repository: tayloraswift/swift-png

Default branch: master

README: README.md