R/error_diffusion_matrices.R

#-----------------------------------------------------------------------------
#' Named list of standard error diffusion matrices
#'
#' The way error is diffused is defined by a matrix of values called
#' the 'error diffusion matrix'.   It defines how the error is distributed to
#' the surrounding pixels.
#'
#' The matrix must contain one \code{NA} value in the first row to indicate the
#' position of the current pixel.
#'
#' Matrices should mostly sum to 1 (a key exception is the Atkinson diffusion
#' matrix which purposefully does not diffuse 25% of the error)
#'
#' @export
#-----------------------------------------------------------------------------
diffusion_matrix <- list(
  #-----------------------------------------------------------------------------
  # 1-d Error Diffusion (only pushes error forwards to the next pixel)
  #-----------------------------------------------------------------------------
  one_d = matrix(
    c(NA, 1),
    ncol=2, byrow = TRUE),


  #-----------------------------------------------------------------------------
  # Floyd Steinberg
  #-----------------------------------------------------------------------------
  floyd_steinberg = matrix(
    c(0, NA, 7,
      3,  5, 1),
    ncol=3, byrow = TRUE) / 16,


  #-----------------------------------------------------------------------------
  # Fake Floyd Steinberg
  #-----------------------------------------------------------------------------
  fake_floyd_steinberg = matrix(
    c(NA, 3,
      3 , 2),
    ncol=2, byrow = TRUE) / 8,


  #-----------------------------------------------------------------------------
  # Stucki
  #-----------------------------------------------------------------------------
  stucki = matrix(
    c(0, 0, NA, 8, 4,
      2, 4, 8 , 4, 2,
      1, 2, 4 , 2, 1),
    ncol=5, byrow = TRUE) / 42,


  #-----------------------------------------------------------------------------
  # Burkes
  #-----------------------------------------------------------------------------
  burkes = matrix(
    c(0, 0, NA, 8, 4,
      2, 4,  8, 4, 2),
    ncol=5, byrow = TRUE) / 32,


  #-----------------------------------------------------------------------------
  # Atkinson
  #-----------------------------------------------------------------------------
  atkinson = matrix(
    c(0, NA, 1, 1,
      1,  1, 1, 0,
      0,  1, 0, 0),
    ncol=4, byrow = TRUE) / 8,


  #-----------------------------------------------------------------------------
  # Jarvis Judice Ninke
  #-----------------------------------------------------------------------------
  jarvis_judice_ninke = matrix(
    c(0, 0, NA, 7, 5,
      3, 5,  7, 5, 3,
      1, 3,  5, 3, 1),
    ncol=5, byrow = TRUE) / 48,


  #-----------------------------------------------------------------------------
  # Sierra
  #-----------------------------------------------------------------------------
  sierra = matrix(
    c(0, 0, NA, 5, 3,
      2, 4,  5, 4, 2,
      0, 2,  3, 2, 0),
    ncol=5, byrow = TRUE) / 32,


  #-----------------------------------------------------------------------------
  # Two-row Sierra
  #-----------------------------------------------------------------------------
  two_row_sierra = matrix(
    c(0, 0, NA, 4, 3,
      1, 2,  3, 2, 1),
    ncol=5, byrow = TRUE) / 16,


  #-----------------------------------------------------------------------------
  # Sierra Lite
  #-----------------------------------------------------------------------------
  sierra_lite = matrix(
    c(0, NA, 2,
      1,  1, 0),
    ncol=3, byrow = TRUE) / 4
)
coolbutuseless/dithr documentation built on May 14, 2019, 6:09 a.m.