Puregrain.Cells
- Package
- purescript-puregrain
- Repository
- roman-mkh/puregrain
Dithering with cells: the image is divided into cells of width ×
height pixels, and each cell may use only one or two colors, chosen
for it from a list of candidates. That's how the attribute cells of
8-bit home computers work, such as the ZX Spectrum's 8 × 8 cells with
an ink and a paper color (Puregrain.Machine.ZxSpectrum). A
CellMode holds the cell size and the candidates; a machine module
exports one per screen mode. How it works and which rule suits which
picture: docs/machine-screens/.
The image is processed in bands, one row of cells at a time. In each band:
- Every cell gets the candidate that fits its original pixels best. A candidate's score is the sum, over the cell's pixels, of each pixel's squared distance (in r, g and b) to the nearest color the candidate can produce: the nearest point on the segment between its two colors, since dithering can mix any share of them, or its one color. The lowest score wins; on a tie, the earlier candidate. A middle gray, for example, fits black and white perfectly, blue and white poorly.
- The band's rows are dithered: a
CellQuantizerturns each pixel into one of its cell's colors, and the kernel passes the error on, across cell and band borders.
ditherCells works on an image in memory, ditherBands on a lazy
stream of rows, with the same result:
-- error diffusion inside the cells
ditherCells floydSteinberg nearestInCell mode image
-- pure ordered dithering inside the cells, with a threshold map m
ditherCells noDiffusion (orderedInCell m) mode image
ditherCellsBy and ditherBandsBy take their settings as a
Puregrain.Dither.DitherParams (the kernel, the gamma, …) in place of
the kernel; ditherCellsWith and ditherBandsWith take a Gamma from
Puregrain.Gamma and a kernel. Under a gamma the cells are the same,
with the rows dithered in light.
#singleColor Source
singleColor :: RGB -> CellPaletteA cell palette of one color.
#colorPair Source
colorPair :: RGB -> RGB -> CellPaletteA cell palette of two colors. The same color twice is a single color.
RGB: the first color.RGB: the second color; the order matters only fororderedInCell.
#cellColors Source
cellColors :: CellPalette -> NonEmptyArray RGBThe palette's colors: one, or two in the order they were given.
#CellMode Source
type CellMode c = { candidates :: NonEmptyArray c, palette :: c -> CellPalette, size :: CellSize }A screen mode for cells: how big a cell is, and which palettes a cell
may use. A machine module exports one per screen mode, such as
Puregrain.Machine.ZxSpectrum.screen.
c: the candidate type, anything that stands for a cell's palette (for the ZX Spectrum,Attribute).size: the cell's width and height in pixels.palette: a candidate's colors.candidates: what a cell may use, in the order they're tried; on a tie the earlier wins.
For cells of your own, write the record; the candidates can be the palettes themselves:
mode :: CellMode CellPalette
mode =
{ size: { width: 4, height: 4 }
, palette: identity
, candidates: NEA.cons' (colorPair black white) [ colorPair blue yellow ]
}
To change one part of a machine's mode, update its record, as in
Zx.screen { candidates = fewer }.
#CellQuantizer Source
data CellQuantizerHow a pixel becomes one of its cell's colors. Build it with
nearestInCell or orderedInCell.
#nearestInCell Source
nearestInCell :: CellQuantizerEach pixel becomes the nearer of its cell's colors, by squared RGB distance (the first on a tie). With a kernel, that's error diffusion inside the cells.
#orderedInCell Source
orderedInCell :: ThresholdMap -> CellQuantizerOrdered dithering inside the cells. A pixel lies some fraction of the way from its cell's first color to the second, measured along the line between them; it becomes the second color where that fraction is above the map's threshold at the pixel's position, else the first. A single-color cell keeps its color.
ThresholdMap: the thresholds, fromPuregrain.Ordered.bayerorcompileThresholdMap. Positions count from the image's corner, so any size works: a map whose side divides the cell's repeats in every cell, a larger one spreads more tone steps over several cells.
With noDiffusion, that's pure ordered dithering. With another kernel
it's the hybrid: the difference between a pixel's value and its color
is passed on.
#ditherCellsBy Source
ditherCellsBy :: forall c. DitherParams -> CellQuantizer -> CellMode c -> Array (Array RGB) -> { cells :: Array (Array c), image :: Array (Array RGB) }Dithers an image in memory with cells, with the settings given.
c: the mode's candidate type (seeCellMode).DitherParams: the kernel, the gamma and the scan, fromPuregrain.Dither.defaultsand thewith…functions.CellQuantizer: how each pixel picks one of its cell's colors.CellMode c: the cell size, and the candidates with their colors.Array (Array RGB): the image, rows top to bottom, all of the same length.
Returns the candidate chosen for each cell, as rows of cells (top to bottom, each left to right), and the dithered image. An image whose size isn't a multiple of the cell size gets partial cells at the right and bottom edges. Stops with an error naming a row whose length differs from the first row's.
Under a gamma other than gammaOff, each cell's colors are still
chosen from the original pixels, as stored, so the cells are the same;
the band's rows are then dithered in light. orderedInCell measures
where a pixel lies between its cell's two colors in light;
nearestInCell still picks the nearer color on stored values, while
the error stays in light. When the cells' colors are whole numbers from
0 to 255, as a machine's are, every pixel comes out as exactly one of
its cell's colors, as a screen file needs.
ditherCellsBy (defaults # withKernel atkinson) nearestInCell Zx.screen picture
#ditherCells Source
ditherCells :: forall c. Kernel -> CellQuantizer -> CellMode c -> Array (Array RGB) -> { cells :: Array (Array c), image :: Array (Array RGB) }Dithers an image in memory with cells, with a kernel and no other setting.
c,CellQuantizer,CellMode c,Array (Array RGB): as forditherCellsBy.Kernel: where each pixel's error goes;noDiffusionfor nowhere.
Returns what ditherCellsBy (defaults # withKernel kernel) returns.
#ditherCellsWith Source
ditherCellsWith :: forall c. Gamma -> Kernel -> CellQuantizer -> CellMode c -> Array (Array RGB) -> { cells :: Array (Array c), image :: Array (Array RGB) }Dithers an image in memory with cells, with a gamma and a kernel.
c: as forditherCellsBy.Gamma: how the stored numbers turn into light:gammaOff, the same asditherCells, or a curve fromPuregrain.Gamma.Kernel,CellQuantizer,CellMode c,Array (Array RGB): as forditherCells.
Returns what ditherCellsBy (defaults # withKernel kernel # withGamma
gamma) returns.
#ditherBandsBy Source
ditherBandsBy :: forall c. DitherParams -> CellQuantizer -> CellMode c -> List (Array RGB) -> List { cells :: Array c, rows :: Array (Array RGB) }Dithers a stream of rows with cells, one band (row of cells) at a time, with the settings given.
c,DitherParams,CellQuantizerandCellMode c: as forditherCellsBy.LL.List (Array RGB): the image as a lazy list of rows, top to bottom, all of the same length; it may be endless.
Returns a lazy list of bands, top to bottom, each with the candidates
chosen for its cells (left to right) and its dithered rows: the same
result as ditherCellsBy. A band's rows are read and dithered only when
the band is first needed, so only one band is in memory. When a band
holds a row whose length differs from the first row's, ditherBands
stops there with an error naming it; the bands before it have come out.
#ditherBands Source
ditherBands :: forall c. Kernel -> CellQuantizer -> CellMode c -> List (Array RGB) -> List { cells :: Array c, rows :: Array (Array RGB) }Dithers a stream of rows with cells, one band at a time, with a kernel and no other setting.
c,CellQuantizer,CellMode c,LL.List (Array RGB): as forditherBandsBy.Kernel: where each pixel's error goes;noDiffusionfor nowhere.
Returns what ditherBandsBy (defaults # withKernel kernel) returns.
#ditherBandsWith Source
ditherBandsWith :: forall c. Gamma -> Kernel -> CellQuantizer -> CellMode c -> List (Array RGB) -> List { cells :: Array c, rows :: Array (Array RGB) }Dithers a stream of rows with cells, one band at a time, with a gamma and a kernel.
candGamma: as forditherCellsWith.Kernel,CellQuantizer,CellMode c,LL.List (Array RGB): as forditherBands.
Returns what ditherBandsBy (defaults # withKernel kernel # withGamma
gamma) returns.
Re-exports from Puregrain.Internal.Cells
#CellPalette Source
newtype CellPaletteThe colors one cell may use: one color, or two that dithering mixes.
Build it with singleColor or colorPair. The order of two colors
doesn't change which cells choose them, nor what nearestInCell
gives; with orderedInCell it sets the pattern's direction.
- Modules
- Puregrain
- Puregrain.
Cells - Puregrain.
Dither - Puregrain.
Gamma - Puregrain.
Internal. Cells - Puregrain.
Internal. Exact - Puregrain.
Internal. Fifo - Puregrain.
Internal. Gamma - Puregrain.
Internal. Image - Puregrain.
Internal. Kernel - Puregrain.
Internal. Noise - Puregrain.
Internal. Ostromoukhov - Puregrain.
Internal. Params - Puregrain.
Internal. Quantize - Puregrain.
Internal. Row - Puregrain.
Internal. SelfCheck - Puregrain.
Internal. State - Puregrain.
Internal. Step - Puregrain.
Internal. Util - Puregrain.
Internal. WeightsByTone - Puregrain.
Internal. ZhouFang - Puregrain.
Kernel - Puregrain.
Machine. C64 - Puregrain.
Machine. ZxSpectrum - Puregrain.
Ordered - Puregrain.
Palette - Puregrain.
Palette. Presets - Puregrain.
Pixel - Puregrain.
Quantize - Puregrain.
SelfCheck