JFLib

img_core/quantize.rs#

Source: lib/img_core/quantize.rs

quantize -- colour quantization: palette + index image (the 8-bit encoders: GIF, BMP 8 bit,
PNG palette)                                                        [src/img/quantize.s]

  quantize(sf, max_colors, dither, pal, idx) -> (colours, transparent index or -1)
  quantize_ex(sf, max_colors, cfg, pal, idx) -> (colours, transparent index or -1)
  quantize_alloc(sf, max_colors, cfg, pal) -> Some((colours, transparent index, idx))
  opt_qcfg(opt) -> cfg from ImgOpt::dither / athresh                [imgcore.s opt_qcfg]

Input: FMT_PARGB (un-premultiplied), FMT_ARGB (straight), FMT_XRGB, FMT_A8 (grey).
max_colors is clamped to 2..256.
cfg (quantize_ex) = dither level 0..8 in the low byte (0 = none, 8 = full Floyd-Steinberg;
the diffused error is scaled by level / 8; > 8 counts as 8)
  | threshold << IQ_THRESH_SHIFT: pixels with alpha < threshold (0..256) become
    transparent, all others opaque with their straight colour
  | IQF_WHITE: composite over white first (c + (255 - a) premultiplied), no transparency
  | IQF_KEEPALPHA: exact palette only, alpha kept per entry (straight ARGB, every alpha-0
    pixel counts as 0x00000000); more than max_colors colours -> 0 colours (no Wu fallback;
    dither and threshold are ignored)
quantize(sf, max, dither, ...) = quantize_ex with level 8 when dither != 0 (else 0) and
threshold 128.

Exact palette: when the image has at most max_colors distinct colours (the transparent
pixels count as one colour) the palette holds exactly those, in order of first appearance.
Otherwise: Wu's variance-minimizing quantizer (X. Wu, Graphics Gems II, 1991) on a
32 x 32 x 32 histogram of the opaque pixels (exact 64-bit moments) splits RGB space into
max_colors boxes (max_colors - 1 when there are transparent pixels); a palette colour is the
rounded mean of its box.  Pixels map to the nearest palette colour (squared RGB distance,
lowest index on ties, searched for the centre of the pixel's 64 x 64 x 64 cell and cached per
cell), with serpentine Floyd-Steinberg error diffusion (7/16, 3/16, 5/16, 1/16; the
accumulated error e (in 16ths) is applied as (e * level + 64) >> 7) when the level is not 0.
Transparent pixels get the last palette entry (0x00000000) and take no part in the diffusion.

  pal  u32[256] straight 0xAARRGGBB: alpha 255, or 0x00000000 for the transparent entry
       (IQF_KEEPALPHA: any alpha); entries past the count are set to 0.  Left untouched
       when the result is 0 colours.
  idx  w * h bytes, rows packed (stride w).  As in the assembly, a failed exact palette
       (IQF_KEEPALPHA) leaves the indices written before the failing pixel.
Result: (number of palette entries 1..max_colors, index of the transparent entry or -1);
(0, -1) for an empty surface, an unknown format or an index buffer smaller than w * h.
IQ_THRESH_SHIFT: u32 = 8  // cfg: threshold << IQ_THRESH_SHIFT
IQF_KEEPALPHA: u32 = 1 << 20  // exact palette with per-entry alpha only
IQF_WHITE: u32 = 1 << 21  // composite over white first (no transparent entry)
  • fn quantize(sf: &Surface, max_colors: i32, dither: u32, pal: &mut [u32; 256], idx: &mut [u8]) -> (u32, i32) img_quantize: cfg = level 8 (dither != 0) or 0, threshold 128

  • fn quantize_alloc(sf: &Surface, max_colors: i32, cfg: u32, pal: &mut [u32; 256]) -> Option<(u32, i32, Vec<u8>)> img_quantize_alloc: quantize_ex into a new index image (w * h bytes); None on failure

  • fn opt_qcfg(opt: &ImgOpt) -> u32 opt_qcfg: the quantize_ex cfg from the biased ImgOpt::dither (0 = level 7, n = level n - 1, at most 8) and ImgOpt::athresh (0 = 128, n = threshold n - 1, at most 255) [imgcore.s]

  • fn quantize_ex(sf: &Surface, max_colors: i32, cfg: u32, pal_out: &mut [u32; 256], idx: &mut [u8]) -> (u32, i32) img_quantize_ex (see the module header)

Source: docs/API.md in the JFLib repository.