Module

Puregrain.Internal.Quantize

Package
purescript-puregrain
Repository
roman-mkh/puregrain

Quantize's inside, for the drivers that resolve a quantizer once per run. Internal: may change without notice. Puregrain.Quantize re-exports the type, opaquely, with its constructors and runner.

#Quantize Source

newtype Quantize a

A quantizer: picks a pixel's output value; a is the pixel type. Build one with quantize, or quantizeWith when the decision depends on the pixel's position; run one with runQuantize. Opaque, so its representation can change without breaking anyone.

It gets the pixel's corrected value, with the error diffused into it already added, and must be total: accumulated error pushes values beyond the nominal range, below 0 or above 255, and the quantizer must still return a sensible value. ditherImage diffuses the difference between the corrected value and the output to the neighbours.

Constructors

#Context Source

type Context = { x :: Int, y :: Int }

Where the pixel being quantized sits: column x and row y, from 0 at the top-left. A record so it can grow, with a seed say, without breaking quantizers: a helper that needs only some fields can take forall r. { x :: Int, y :: Int | r }.

#Quantizer Source

type Quantizer a = Context -> a -> a

A quantizer's decision, resolved for one run: from the pixel's position and its corrected value (in light) to the written level (in light). What the core calls for each pixel.

#CompiledQuantize Source

type CompiledQuantize a = { quantize :: Quantizer a, quantizeRaised :: Context -> a -> a -> a, tone :: Context -> a -> a }

What a quantizer becomes once the driver has given it the gamma's conversion: its targets (levels, palette colors, cell colors) converted to light once, before the first pixel.

  • quantize: the decision, Quantizer a.
  • quantizeRaised: the same with the cut between the two levels around the value raised, per channel, by a fraction of their interval (the second argument, thresholdRaise, from 0 up to below one half: the threshold modulation of Zhou–Fang's method). Quantizers without such a cut (palettes, ordered dithering, quantizers of one's own) ignore it (ignoresRaise). Called only with a raise: without one, the drivers call quantize, so every kernel decides exactly as before.
  • tone: from the pixel's position and its original value (before any error, in light) to its tone per channel, 0 to 255: where the value lies between the two levels around it, scaled to 0..255. With black and white, the value itself; for quantizers of one's own, the value itself (plainTone); for palettes, the pixel's mean in every channel; for cells, the share along the cell's two colors, the same in every channel. What a method by tone looks its weights up by (Ostromoukhov's: the pattern a row of its table was tuned for has that share of the upper level's dots).

#compileQuantize Source

compileQuantize :: forall a. Conversion a -> Quantize a -> CompiledQuantize a

A quantizer resolved with the driver's conversion, once per run. Bind the result and call its functions for every pixel: calling compileQuantize again for each pixel would redo the quantizer's preparation each time, since PureScript shares nothing by itself.

#ignoresRaise Source

ignoresRaise :: forall a. Quantizer a -> Context -> a -> a -> a

The raised decision of a quantizer without a cut to raise: its ordinary one, the raise ignored.

#raisedBetween Source

raisedBetween :: Array Number -> Array Number -> Number -> Number -> Number

A decision between sorted levels with the cut between two neighbours raised: the value takes the lower level below the cut plus raise times their interval, else the upper; the lowest level at or below it, the highest at or above.

  • Array Number: the levels, sorted ascending, without duplicates.
  • Array Number: the cuts, one between each two neighbours.
  • Number: the raise, a fraction of the interval.
  • Number: the value.

#levelsAndCutsInLight Source

levelsAndCutsInLight :: Gamma -> NonEmptyArray Number -> { cuts :: Array Number, levels :: Array Number }

The levels a quantizer writes, rounded to whole bytes, in light, sorted and without duplicates, with the cuts between neighbours: their midpoints on stored values, converted to light, as nearestLevel decides.

#plainTone Source

plainTone :: forall a. Context -> a -> a

The tone of a quantizer that knows no levels: the value itself, as with black and white.

#toneBetween Source

toneBetween :: Array Number -> Number -> Number

Where a value lies between the two neighbouring levels around it, scaled to 0..255: (v − lo) × 255 / (hi − lo), multiplied first, so that with 2 levels the tone is the value itself exactly, and with 3 or 5 even levels a whole value's tone is whole. 0 at or below the lowest level, 255 at or above the highest, and 0 with a single level (no interval to lie in); at a level between two others, 0, the start of the interval above (255, the end of the one below, picks the same row of a symmetric table).

  • Array Number: the levels, sorted ascending, without duplicates.
  • Number: the value.

#levelsInLight Source

levelsInLight :: Gamma -> NonEmptyArray Number -> Array Number

The levels a quantizer writes, rounded to whole bytes (as Puregrain.Quantize.nearestLevel and Puregrain.Ordered.ordered round them), in light, sorted and without duplicates, for toneBetween.