SwiftDithering

1.4.0

Swift package focused on Dithering Images
lugalu/SwiftDithering

What's New

1.4.0

2024-08-16T22:45:03Z

New release! this is a big update for SwiftDithering, now you can use CIFilter for all Ordered Dithers, this also means I added support to mac for the GPU variants. The Error diffusion is not compatible with GPU due to the sequential nature and thresholds you can already achieve similar results with the built in CIFilters from Apple.

The parameters for the gpu tend to be a little more sensitive to the inputs, so bear that in mind. Anyhow if you find any bug or has a suggestion feel free to leave a issue in the repo.

I also don't think there's much more I can do, so this project is "DONE" for now

SwiftDithering

This is a library focused on bringing dithering to Swift since the CIFilter for dither is mostly noise. The implementation is done via the Accelerate framework.

Static Badge

Examples


  • Floyd-Steinberg:

    • 1-bit:

    • 2-bits:

Usage

To use the library you will have some access points based on UIImages and CIFilter

CPU:

import SwiftDithering

function yourFunction() {
//Highly Recommended
    Task{
        do{
        let ditheredImage = try image?.applyErrorDifusion(withType: .floydSteinberg)
        //or
        //try image?.applyOrderedDither(withSize: .bayer8x8)
        //or
        //try image?.applyThreshold(withType: .fixed)

        }catch{
          //handle errors here
        }
    }
  
}

the functions also have more accessible parameters, but for simplicity, this example does not, For more details check the Sample/DitherTester/DitherControl and go to any of the retrivedDitheredImages

GPU:

import SwiftDithering

function yourFunction() {
    let filter = OrderedDithering()
    filter.setValuesForKeys([
        "inputImage": inputCIImage,
        "ditherType": ditherType.getCIFilterID(),
        ...
    ])

   return filter?.outputImage
}

I recommend transforming the outputImage to CG before using in IOS, same thing for the respective variants on mac.

Plans And Features

Since is my first time doing something like this a lot of the code is prone to change, I will try to do it in the most non-disruptive way but I cannot guarantee it will work in every single version.

Currently Features:

  • Bayer matrix;
  • Floyd Steinberg Error Diffusion;
  • Stucky Error Difusion.
  • Fixed Threshold
  • Random Threshold
  • Uniform Threshold
  • Clustered Dots
  • Central White point
  • Balanced Centered point
  • Diagonal clustered
  • All Ordered Dithers are Now CPU and GPU supported.

Considerations

  • The CPU variants are calculated as sequential and are expensive so tasks and loadings are needed for the user UI.
  • Feel free to suggest performance improvements and report any bug that you may find while using this lib
  • These dithers are based on the matlab article stored in the archive.today and other research

Description

  • Swift Tools 5.7.1
View More Packages from this Author

Dependencies

  • None
Last updated: Fri May 16 2025 04:54:40 GMT-0900 (Hawaii-Aleutian Daylight Time)