Alja7dali / png

decode, inspect, edit, and encode png images in a native swift framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

platforms: all releases build status documentation status issues language license: mpl2

png
4.0.2

Swift PNG is a pure, cross-platform Swift framework for decoding, inspecting, editing, and encoding PNG images. The framework does not depend on Foundation or any other packages, and will compile and provide consistent behavior on all Swift platforms. Swift PNG also comes with built-in file system support on Linux and MacOS.

Swift PNG is available under the Mozilla Public License 2.0. The example programs are public domain and can be adapted freely.

tutorials and example programs

  1. basic decoding (sources)
  2. basic encoding (sources)
  3. using indexed images (sources)
  4. using iphone-optimized images (sources)
  5. working with metadata (sources)
  6. using in-memory images (sources)
  7. online decoding (sources)
  8. custom color targets (sources)

api reference

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/kelvin13/swift-png", .exact("4.0.2")) 

basic usage

Decode an image:

import PNG 
func decode(png path:String) throws 
{
    guard let image:PNG.Data.Rectangular = 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.Data.Rectangular = .init(packing: pixels, size: size, 
        layout: .init(format: .rgba8(palette: [], fill: nil)))
    try image.compress(path: path, level: 9)
}

features

infrastructure

A list of build flags can be found here. Project automation scripts live in the utils/ directory, and are invoked as follows:

  • utils/examples [-c/--configuration debug | release]

    Builds and runs the example programs in the examples/ directory. The --configuration argument specifies the Swift build mode to use.

  • utils/benchmark [-t/--trials n0 n1 n2] [-s/--save] [-l/--load]

    Runs library performance and compression benchmarks, and generates a performance report.

    The --trials argument specifies the number of trials to run, for decompression, compression, and historical toolchain benchmarks, respectively. The --save argument makes this script store benchmark measurements in cache files; the --load argument makes this script use cached measurements instead of re-running the benchmarks.

  • utils/generate-documentation [-l/--local]

    Generates documentation pages for the library using Entrapta. A Github action invokes this script and deploys the output to the Swift PNG API reference website on every commit.

The CI runs Swift PNG’s test suites with the following invocations:

swift run            unit-test 
swift run -c release integration-test --compact
swift run -c release compression-test 

The integration-test product accepts a -c/--compact option, which specifies the output verbosity, and an -e/--print-expected-failures option, which makes it print the error messages for expected failures.

see also

About

decode, inspect, edit, and encode png images in a native swift framework

License:Mozilla Public License 2.0


Languages

Language:Swift 92.6%Language:Python 5.5%Language:C 1.2%Language:Shell 0.7%