Module

Puregrain.Internal.Kernel

Package
purescript-puregrain
Repository
roman-mkh/puregrain

The compiled form of a kernel, built once per image so the per-pixel step doesn't filter the kernel again for every pixel (PureScript shares nothing automatically). Internal: may change without notice.

#CompiledOffset Source

type CompiledOffset a = { dx :: Int, dy :: Int, weightFor :: a -> a }

One neighbour of a compiled kernel: where its share of the error goes (dx, dy, as in Offset), and the weights it gets, one per channel, as a function of the pixel's tone. A kernel's weights don't depend on the tone (fixed); a method by tone's do. The positions stay plain data, so a tone can change how much error a neighbour gets, never where it goes.

#CompiledKernel Source

type CompiledKernel a = { currentOffsets :: Array (CompiledOffset a), futureLayers :: Array (Array (CompiledOffset a)), maxDepth :: Int, needsTone :: Boolean, thresholdRaise :: Maybe ({ x :: Int, y :: Int } -> a -> a) }

Precomputed per kernel: the offsets in the current row, the offsets of each later row grouped by dy (layer i holds those with dy == i + 1), the deepest dy, and whether the weights depend on the tone (needsTone: only then does the step ask the quantizer for it), and the threshold's raise, from the pixel's position and tone, for a method that modulates its threshold (Zhou–Fang's; Nothing for the others, which then cost nothing for it). Built once by compileKernel or compileOffsets, then passed on as plain data through the row and step code.

#compileKernel Source

#compileOffsets Source

compileOffsets :: forall a. Boolean -> Array (CompiledOffset a) -> CompiledKernel a

Compiled offsets sorted by row, as compileKernel sorts a kernel's.

  • Boolean: whether their weights depend on the tone.
  • Array (CompiledOffset a): the offsets.

#fixed Source

fixed :: forall a. Semiring a => Scalable a => Offset -> CompiledOffset a

An offset whose weight is the same for every tone and channel: the weight's pixel, (w, w, w), is built once. The step multiplies it by the error, channel by channel, which gives the same numbers as scale w error: w × 1 is w, and the products are the same.

#byTone Source

byTone :: forall a. MapChannels a => Int -> Int -> Array Number -> CompiledOffset a

An offset whose weights depend on the tone: its column holds one weight per tone, 0 to 255, and each channel looks its own up by its own tone, rounded to the nearest row and kept within 0..255 (a tone that isn't a finite number counts as 0, as Data.Int.round makes it).

  • Int, Int: the offset's dx and dy.
  • Array Number: the column, 256 weights.

#paddingFor Source

paddingFor :: forall r. { dx :: Int | r } -> Int

The zeros a queue starts with: one per column its error skips to the right. For any offset with a dx, compiled or not.

#skipFor Source

skipFor :: forall r. { dx :: Int | r } -> Int

The entries a finished queue drops at its front: one per column its error went past the left edge.