swift-png

4.4.1

decode, inspect, edit, and encode png images in pure swift
tayloraswift/swift-png

What's New

4.4.1

2024-04-04T00:28:17Z

this release patches a critical vulnerability that occurs when the DEFLATE backend attempts to compress very short inputs (less than 3 bytes) causing a runtime crash.

for applications using the library for PNG compression, the attack is only effective if the server is asked to compress a 1x1 pixel grayscale image, as all other types of PNG images create data streams that are longer than 2 bytes.

all Swift PNG users are advised to upgrade to 4.4.1 immediately.

png
4.4

ci status

swift package index versions swift package index platforms

Swift PNG is a Foundation-less, cross-platform framework for decoding, inspecting, editing, and encoding PNG images. The framework is written in pure Swift, and will compile and provide consistent behavior on all Swift platforms. The library also comes with built-in file system support on linux, macOS, and Windows.

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

Swift PNG is available under the Apache 2.0 license. The example programs are public domain and can be adapted freely.

Swift PNG’s documentation is available on Swiftinit!

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.4.0"))

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

  • Powerful interfaces. Swift PNG’s expressive, strongly-typed APIs make working with PNG images easy for beginners and advanced users alike. If your code compiles, you’re already most of the way there. Power users can take advantage of custom indexing, manual decoding workflows, and user-defined color targets.

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

  • Batteries included. Swift PNG comes with built-in color targets with support for premultiplied alpha. Convolution and deconvolution helper functions make implementing custom color targets a breeze.

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

  • Comprehensive metadata support. Swift PNG can parse and validate all public PNG chunks, which are accessible as strongly-typed metadata records.

  • Modern error handling. Swift PNG has a fully stateless and Swift-native error-handling system.

See also

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

Last updated: Wed Nov 20 2024 23:04:09 GMT-1000 (Hawaii-Aleutian Standard Time)