Skip to content

Support for dithering when converting colors #123

@virtualritz

Description

@virtualritz

TL;DR, data with smooth gradients that is u16 or f32 per channel may exhibit banding when converted to u8 (even for the common case that the target space for the u8 data is non-linear).

There should be a way to either enable dithering when converting or to specify a custom way to do the dithering (e.g. a closure).

I would think that by default a random dither of some sort should be available and this could then be optimized (SIMD & co).

/// Assuming `value` is `f32` and could be
/// outside `0.0..1.0`.
let value: u8 = (
    u8::MAX as f32 * value +
    dither_amplitude * rng.gen_range(-1.0..1.0)()
    )
    .round()
    .clamp(u8::MIN, u8::MAX) as _;

A typical default for dither_amplitude is 0.5.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions