Module

Puregrain.Dither

Package
purescript-puregrain
Repository
roman-mkh/puregrain

Dithering a whole image, in three ways that give the same rows for the same input, with a kernel (Puregrain.Kernel) and a quantizer (Puregrain.Quantize, Puregrain.Palette, Puregrain.Ordered):

  • ditherImage: an image in memory, all at once;
  • ditherRows: a lazy stream of rows, each dithered when it's needed;
  • initDithering and ditherRow: one row at a time, with you running the loop. For rows that come from effects, such as a network stream or a file read piece by piece:
-- readRow and writeRow are your own, here in Aff
loop :: Dithering Number -> Aff Unit
loop dithering = readRow >>= case _ of
  Nothing -> pure unit
  Just row -> case ditherRow dithering row of
    Left problem -> throwError (error problem)
    Right { row: dithered, next } -> writeRow dithered *> loop next

main = launchAff_ (loop (initDithering floydSteinberg (threshold 128.0)))

Aff keeps such a loop from growing the stack, however long the stream; in Effect, write it with tailRecM.

Each takes its settings in one of two ways:

  • ditherImageBy, ditherRowsBy and initDitheringBy take a DitherParams: the kernel, the gamma and the scan (raster or serpentine), built from defaults with the with… functions;
  • ditherImage, ditherRows and initDithering take just a kernel, and their twins ditherImageWith, ditherRowsWith and initDitheringWith a Puregrain.Gamma.Gamma and a kernel: shorthands for the …By drivers with those settings.
-- the same rows, twice
ditherImageBy (defaults # withKernel floydSteinberg # withGamma gammaSrgb) (threshold 128.0) image
ditherImageWith gammaSrgb floydSteinberg (threshold 128.0) image

A gamma keeps the amount of light, by that curve, rather than the average of the stored numbers; gammaOff is the plain result. Which gamma for which view: docs/gamma.md.

#ditherImageBy Source

ditherImageBy :: forall a. Ring a => Scalable a => MapChannels a => ColorChannels a => DitherParams -> Quantize a -> Array (Array a) -> Array (Array a)

Dithers an image in memory, with the settings given.

  • a: the pixel type, Number for gray, RGB or RGBA; any type with Ring, Scalable, MapChannels and ColorChannels works.
  • DitherParams: the kernel, the gamma and the scan, from defaults and the with… functions.
  • Quantize a: picks each pixel's output value.
  • Array (Array a): the image, rows top to bottom, each left to right, all of the same length.

Returns the dithered image, all at once, as stored values. Under a gamma other than gammaOff, each row is taken to light, dithered there, and brought back: the error and the amounts of light (ordered dithering's patterns) are in light; the choices of the nearest level or color stay on stored values, as the eye sees them; a level or a palette color of whole numbers comes back exactly. Stops with an error naming a row whose length differs from the first row's.

ditherImageBy (defaults # withKernel floydSteinberg # withGamma gammaSrgb) (threshold 128.0) image

#ditherImage Source

ditherImage :: forall a. Ring a => Scalable a => Kernel -> Quantize a -> Array (Array a) -> Array (Array a)

Dithers an image in memory, with a kernel and no other setting.

  • a: the pixel type, Number for gray, RGB or RGBA; any type with Ring and Scalable works, ColorChannels not needed.
  • Kernel: where each pixel's error goes; noDiffusion for nowhere.
  • Quantize a: picks each pixel's output value.
  • Array (Array a): the image, as for ditherImageBy.

Returns what ditherImageBy (defaults # withKernel kernel) returns.

#ditherImageWith Source

ditherImageWith :: forall a. Ring a => Scalable a => ColorChannels a => Gamma -> Kernel -> Quantize a -> Array (Array a) -> Array (Array a)

Dithers an image in memory, with a gamma and a kernel.

  • a: the pixel type, as for ditherImageBy but without MapChannels: any type with Ring, Scalable and ColorChannels.
  • Gamma: how the stored numbers turn into light: gammaOff, the same as ditherImage, or gamma1p2, gamma1p5, gamma1p8 or gammaSrgb (linear light).
  • Kernel, Quantize a, Array (Array a): as for ditherImage.

Returns what ditherImageBy (defaults # withKernel kernel # withGamma gamma) returns.

ditherImageWith gammaSrgb floydSteinberg (threshold 128.0) image

#ditherRowsBy Source

ditherRowsBy :: forall a. Ring a => Scalable a => MapChannels a => ColorChannels a => DitherParams -> Quantize a -> List (Array a) -> List (Array a)

Dithers a lazy stream of rows, with the settings given.

  • a, DitherParams and Quantize a: as for ditherImageBy.
  • LL.List (Array a): the rows, top to bottom, all of the same length; the list may be endless.

Returns the dithered rows as a lazy list, the same rows as ditherImageBy. Each row is read and dithered only when its result is first needed, so only the rows in flight are in memory. At a row whose length differs from the first row's, it stops with an error naming the row; the rows before it have come out.

#ditherRows Source

ditherRows :: forall a. Ring a => Scalable a => Kernel -> Quantize a -> List (Array a) -> List (Array a)

Dithers a lazy stream of rows, with a kernel and no other setting.

  • a, Kernel and Quantize a: as for ditherImage.
  • LL.List (Array a): the rows, as for ditherRowsBy.

Returns what ditherRowsBy (defaults # withKernel kernel) returns.

#ditherRowsWith Source

ditherRowsWith :: forall a. Ring a => Scalable a => ColorChannels a => Gamma -> Kernel -> Quantize a -> List (Array a) -> List (Array a)

Dithers a lazy stream of rows, with a gamma and a kernel.

  • a and Gamma: as for ditherImageWith.
  • Kernel, Quantize a, LL.List (Array a): as for ditherRows.

Returns what ditherRowsBy (defaults # withKernel kernel # withGamma gamma) returns.

#Dithering Source

newtype Dithering a

An image being dithered one row at a time, for ditherRow: the settings, the quantizer, and the error waiting for the rows still to come; a is the pixel type. Opaque.

An immutable value: keep and reuse one, for an undo, say. Dithering a row from it again redoes that row's work, and no more.

#initDitheringBy Source

initDitheringBy :: forall a. Ring a => Scalable a => MapChannels a => ColorChannels a => DitherParams -> Quantize a -> Dithering a

The state before the first row, for ditherRow, with the settings given.

  • a, DitherParams and Quantize a: as for ditherImageBy.

Dithers nothing yet; the first row fixes the image's width. ditherRow then gives the same rows as ditherImageBy, each as stored values.

#initDithering Source

initDithering :: forall a. Ring a => Scalable a => Kernel -> Quantize a -> Dithering a

The state before the first row, for ditherRow, with a kernel and no other setting.

  • a, Kernel and Quantize a: as for ditherImage.

Returns what initDitheringBy (defaults # withKernel kernel) returns.

#initDitheringWith Source

initDitheringWith :: forall a. Ring a => Scalable a => ColorChannels a => Gamma -> Kernel -> Quantize a -> Dithering a

The state before the first row, for ditherRow, with a gamma and a kernel.

  • a and Gamma: as for ditherImageWith.
  • Kernel and Quantize a: as for initDithering.

Returns what initDitheringBy (defaults # withKernel kernel # withGamma gamma) returns.

#ditherRow Source

ditherRow :: forall a. Ring a => Scalable a => Dithering a -> Array a -> Either String { next :: Dithering a, row :: Array a }

Dithers the next row.

  • Dithering a: the state, from initDitheringBy, initDithering, initDitheringWith or the previous row.
  • Array a: the row, as long as the first one.

Returns the dithered row and the state for the next one: the same rows as the matching ditherImage…. Left, with a message naming the row, for a row of a different length; the state passed in is untouched, so you can carry on with a corrected row.

Re-exports from Puregrain.Internal.Params

#Scan Source

data Scan

The order the pixels are visited in, for withScan: raster or serpentine. Opaque.

#DitherParams Source

newtype DitherParams

How an image is dithered, apart from the quantizer: the diffusion, where each pixel's error goes (a kernel, or a method by tone); the gamma, in which light; and the scan, the order the pixels are visited in. Opaque: start from defaults and change one setting at a time with the with… functions, chained with #:

params = defaults # withKernel floydSteinberg # withGamma gammaSrgb # withScan serpentine

dithered = ditherImageBy params (threshold 128.0) image

The order of the with… calls doesn't matter; a later call of the same one replaces the earlier setting. The quantizer is an argument of its own, since its type depends on the driver: a Quantize a for ditherImageBy, a cell rule for Puregrain.Cells.ditherCellsBy.

#Diffusion Source

data Diffusion

How each pixel's error is spread, for withDiffusion: a kernel's fixed weights (fixedKernel), or weights that depend on the pixel's tone (ostromoukhov). Opaque.

#zhouFang Source

zhouFang :: Diffusion

Zhou and Fang's error diffusion with threshold modulation (SIGGRAPH 2003): Ostromoukhov's three neighbours with weights of its own by tone, and a threshold raised at random, 128 + (noise mod 128) × m(tone), with a strength m by tone. The noise breaks up the regular patterns Ostromoukhov's method still shows in the mid-tones, for a little more grain.

The threshold is raised by a fraction of the interval between the two levels around a value, so with more levels each interval gets the black-and-white patterns, scaled; per channel by each channel's tone; in a machine's cells along the cell's two colors. Palettes, ordered dithering and quantizers of one's own have no threshold to raise: with them, it diffuses with its weights alone. The noise comes from a hash of the pixel's position, with a fixed seed: the same picture dithers alike everywhere, every time. It always scans serpentine. Its tables, its sources and what it looks like: docs/weights-by-tone/zhou-fang.md.

#withScan Source

withScan :: Scan -> DitherParams -> DitherParams

Sets the scan, the order the pixels are visited in.

  • Scan: raster, every row left to right (the default), or serpentine, the rows alternating.
  • DitherParams: the settings to change.

Returns the settings with that scan, the others unchanged.

#withKernel Source

withKernel :: Kernel -> DitherParams -> DitherParams

Sets the kernel: the same as withDiffusion (fixedKernel kernel).

  • Kernel: where each pixel's error goes, from Puregrain.Kernel; noDiffusion for nowhere.
  • DitherParams: the settings to change.

Returns the settings with that kernel, the others unchanged.

#withGamma Source

withGamma :: Gamma -> DitherParams -> DitherParams

Sets the gamma.

  • Gamma: how the stored numbers turn into light, from Puregrain.Gamma: gammaOff, gamma1p2, gamma1p5, gamma1p8 or gammaSrgb. Which for which view: docs/gamma.md.
  • DitherParams: the settings to change.

Returns the settings with that gamma, the others unchanged.

#withDiffusion Source

withDiffusion :: Diffusion -> DitherParams -> DitherParams

Sets the diffusion. It and withKernel set the same setting, so the later of the two counts.

  • Diffusion: fixedKernel with a kernel, or ostromoukhov.
  • DitherParams: the settings to change.

Returns the settings with that diffusion, the others unchanged.

params = defaults # withDiffusion ostromoukhov

#serpentine Source

serpentine :: Scan

Serpentine scanning: the rows alternate, the first (row 0) left to right, the second right to left, and so on, top to bottom. On a row scanned right to left the kernel is mirrored, so each pixel's error still goes to pixels not yet processed: an offset's dx then counts to the left. It's the usual remedy for the patterns error diffusion draws when every row is scanned the same way.

Each pixel's quantizer still gets its own position, counted from the image's left and top edges, so ordered dithering and machine cells work as with raster. With noDiffusion it changes nothing. What it changes, and what it doesn't: docs/error-diffusion.md.

#raster Source

raster :: Scan

Every row left to right, top to bottom: the default.

#ostromoukhov Source

ostromoukhov :: Diffusion

Ostromoukhov's variable-coefficient error diffusion (SIGGRAPH 2001): three neighbours, right, down-left and down, whose weights depend on the pixel's tone, from a table tuned so that flat areas of every gray come out as even patterns, without the lines and lattices of Floyd–Steinberg in its worst grays.

Each channel looks its weights up by its own tone. The tone is where the pixel's original value lies between the two levels around it, so that a mix of two levels gets the row tuned for that mix: with black and white, the value itself; with a palette, the mean of the pixel's channels, the same in each, as palette colors have no order to lie between and the palette decides on the whole pixel; in a machine's cells, the share along the cell's two colors.

It always scans serpentine, as the method is defined, whatever withScan says. Its table, its sources and what it looks like: docs/weights-by-tone/ostromoukhov.md.

#fixedKernel Source

fixedKernel :: Kernel -> Diffusion

A kernel's diffusion: the same weights for every pixel, from Puregrain.Kernel.

#defaults Source

defaults :: DitherParams

The settings to start from, each one neutral: no diffusion (noDiffusion), so each pixel is only quantized; gammaOff; and raster, every row left to right. Dithering with them alone is plain quantizing, or pure ordered dithering with a quantizer from Puregrain.Ordered; add a kernel with withKernel for error diffusion.