use crate::{DitherError, Effect, ErrorKind, Mask, Result};
const FIXED_SCALE: i32 = 256;
const DIFFUSION_STRENGTH_SCALE: u16 = 256;
const FLOYD_STEINBERG: &[DiffusionTap] = &[
DiffusionTap::new(1, 0, 7),
DiffusionTap::new(-1, 1, 3),
DiffusionTap::new(0, 1, 5),
DiffusionTap::new(1, 1, 1),
];
const ATKINSON: &[DiffusionTap] = &[
DiffusionTap::new(1, 0, 1),
DiffusionTap::new(2, 0, 1),
DiffusionTap::new(-1, 1, 1),
DiffusionTap::new(0, 1, 1),
DiffusionTap::new(1, 1, 1),
DiffusionTap::new(0, 2, 1),
];
const JARVIS_JUDICE_NINKE: &[DiffusionTap] = &[
DiffusionTap::new(1, 0, 7),
DiffusionTap::new(2, 0, 5),
DiffusionTap::new(-2, 1, 3),
DiffusionTap::new(-1, 1, 5),
DiffusionTap::new(0, 1, 7),
DiffusionTap::new(1, 1, 5),
DiffusionTap::new(2, 1, 3),
DiffusionTap::new(-2, 2, 1),
DiffusionTap::new(-1, 2, 3),
DiffusionTap::new(0, 2, 5),
DiffusionTap::new(1, 2, 3),
DiffusionTap::new(2, 2, 1),
];
const STUCKI: &[DiffusionTap] = &[
DiffusionTap::new(1, 0, 8),
DiffusionTap::new(2, 0, 4),
DiffusionTap::new(-2, 1, 2),
DiffusionTap::new(-1, 1, 4),
DiffusionTap::new(0, 1, 8),
DiffusionTap::new(1, 1, 4),
DiffusionTap::new(2, 1, 2),
DiffusionTap::new(-2, 2, 1),
DiffusionTap::new(-1, 2, 2),
DiffusionTap::new(0, 2, 4),
DiffusionTap::new(1, 2, 2),
DiffusionTap::new(2, 2, 1),
];
const BURKES: &[DiffusionTap] = &[
DiffusionTap::new(1, 0, 8),
DiffusionTap::new(2, 0, 4),
DiffusionTap::new(-2, 1, 2),
DiffusionTap::new(-1, 1, 4),
DiffusionTap::new(0, 1, 8),
DiffusionTap::new(1, 1, 4),
DiffusionTap::new(2, 1, 2),
];
const SIERRA: &[DiffusionTap] = &[
DiffusionTap::new(1, 0, 5),
DiffusionTap::new(2, 0, 3),
DiffusionTap::new(-2, 1, 2),
DiffusionTap::new(-1, 1, 4),
DiffusionTap::new(0, 1, 5),
DiffusionTap::new(1, 1, 4),
DiffusionTap::new(2, 1, 2),
DiffusionTap::new(-1, 2, 2),
DiffusionTap::new(0, 2, 3),
DiffusionTap::new(1, 2, 2),
];
const TWO_ROW_SIERRA: &[DiffusionTap] = &[
DiffusionTap::new(1, 0, 4),
DiffusionTap::new(2, 0, 3),
DiffusionTap::new(-2, 1, 1),
DiffusionTap::new(-1, 1, 2),
DiffusionTap::new(0, 1, 3),
DiffusionTap::new(1, 1, 2),
DiffusionTap::new(2, 1, 1),
];
const SIERRA_LITE: &[DiffusionTap] = &[
DiffusionTap::new(1, 0, 2),
DiffusionTap::new(-1, 1, 1),
DiffusionTap::new(0, 1, 1),
];
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Colour {
pub red: u8,
pub green: u8,
pub blue: u8,
}
impl Colour {
pub const BLACK: Self = Self::new(0, 0, 0);
pub const WHITE: Self = Self::new(255, 255, 255);
pub const RED: Self = Self::new(255, 0, 0);
pub const GREEN: Self = Self::new(0, 255, 0);
pub const BLUE: Self = Self::new(0, 0, 255);
pub const CYAN: Self = Self::new(0, 255, 255);
pub const MAGENTA: Self = Self::new(255, 0, 255);
pub const YELLOW: Self = Self::new(255, 255, 0);
pub const fn new(red: u8, green: u8, blue: u8) -> Self {
Self { red, green, blue }
}
pub const fn rgb(self) -> [u8; 3] {
[self.red, self.green, self.blue]
}
}
impl From<[u8; 3]> for Colour {
fn from([red, green, blue]: [u8; 3]) -> Self {
Self::new(red, green, blue)
}
}
impl From<&[u8; 3]> for Colour {
fn from(colour: &[u8; 3]) -> Self {
Self::from(*colour)
}
}
impl From<&Colour> for Colour {
fn from(colour: &Colour) -> Self {
*colour
}
}
impl From<Colour> for [u8; 3] {
fn from(colour: Colour) -> Self {
colour.rgb()
}
}
pub type Color = Colour;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Palette {
colours: Box<[[u8; 3]]>,
}
impl Palette {
pub fn new<C>(colours: impl IntoIterator<Item = C>) -> Result<Self>
where
C: Into<Colour>,
{
let colours = colours
.into_iter()
.map(|colour| colour.into().rgb())
.collect::<Vec<_>>()
.into_boxed_slice();
if colours.is_empty() {
return Err(DitherError::new(
ErrorKind::InvalidParameter,
"a palette requires at least one colour",
));
}
Ok(Self { colours })
}
pub fn monochrome(colour: impl Into<Colour>) -> Self {
let colour = colour.into().rgb();
if colour == [0, 0, 0] || colour == [255, 255, 255] {
return Self::black_and_white();
}
Self {
colours: Box::new([[0, 0, 0], colour, [255, 255, 255]]),
}
}
pub fn black_and_white() -> Self {
Self {
colours: Box::new([[0, 0, 0], [255, 255, 255]]),
}
}
pub fn greyscale() -> Self {
Self {
colours: Box::new([
[0, 0, 0],
[36, 36, 36],
[73, 73, 73],
[109, 109, 109],
[146, 146, 146],
[182, 182, 182],
[219, 219, 219],
[255, 255, 255],
]),
}
}
pub fn game_boy() -> Self {
Self {
colours: Box::new([[15, 56, 15], [48, 98, 48], [139, 172, 15], [155, 188, 15]]),
}
}
pub fn cga() -> Self {
Self {
colours: Box::new([
[0, 0, 0],
[0, 0, 170],
[0, 170, 0],
[0, 170, 170],
[170, 0, 0],
[170, 0, 170],
[170, 85, 0],
[170, 170, 170],
[85, 85, 85],
[85, 85, 255],
[85, 255, 85],
[85, 255, 255],
[255, 85, 85],
[255, 85, 255],
[255, 255, 85],
[255, 255, 255],
]),
}
}
pub fn pico_8() -> Self {
Self {
colours: Box::new([
[0, 0, 0],
[29, 43, 83],
[126, 37, 83],
[0, 135, 81],
[171, 82, 54],
[95, 87, 79],
[194, 195, 199],
[255, 241, 232],
[255, 0, 77],
[255, 163, 0],
[255, 236, 39],
[0, 228, 54],
[41, 173, 255],
[131, 118, 156],
[255, 119, 168],
[255, 204, 170],
]),
}
}
pub fn colours(&self) -> &[[u8; 3]] {
&self.colours
}
pub fn iter(&self) -> impl ExactSizeIterator<Item = Colour> + '_ {
self.colours.iter().copied().map(Colour::from)
}
pub fn nearest(&self, colour: impl Into<Colour>) -> Colour {
Colour::from(self.nearest_colour(colour.into().rgb()))
}
#[inline]
pub fn nearest_colour(&self, colour: [u8; 3]) -> [u8; 3] {
if self.colours.as_ref() == [[0, 0, 0], [255, 255, 255]] {
let sum = u16::from(colour[0]) + u16::from(colour[1]) + u16::from(colour[2]);
return [if sum >= 383 { 255 } else { 0 }; 3];
}
*self
.colours
.iter()
.min_by_key(|candidate| colour_distance(colour, **candidate))
.expect("a palette is always non-empty")
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Threshold {
palette: Palette,
pixel_size: u32,
}
impl Threshold {
pub const fn new(palette: Palette) -> Self {
Self {
palette,
pixel_size: 1,
}
}
pub const fn palette(&self) -> &Palette {
&self.palette
}
pub fn with_pixel_size(mut self, pixel_size: u32) -> Result<Self> {
self.pixel_size = validate_pixel_size(pixel_size)?;
Ok(self)
}
pub const fn pixel_size(&self) -> u32 {
self.pixel_size
}
}
impl Effect for Threshold {
fn apply(
&self,
input: &[u8],
output: &mut [u8],
dimensions: (u32, u32),
mask: &Mask,
) -> Result<()> {
quantise_cells(
input,
output,
dimensions,
mask,
self.pixel_size,
|colour, _, _| self.palette.nearest_colour(colour),
);
Ok(())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OrderedDither {
palette: Palette,
matrix_size: u8,
pixel_size: u32,
}
impl OrderedDither {
pub fn new(palette: Palette, matrix_size: u8) -> Result<Self> {
if !matches!(matrix_size, 2 | 4 | 8) {
return Err(DitherError::new(
ErrorKind::InvalidParameter,
"Bayer matrix size must be 2, 4, or 8",
));
}
Ok(Self {
palette,
matrix_size,
pixel_size: 1,
})
}
pub const fn palette(&self) -> &Palette {
&self.palette
}
pub const fn matrix_size(&self) -> u8 {
self.matrix_size
}
pub fn with_pixel_size(mut self, pixel_size: u32) -> Result<Self> {
self.pixel_size = validate_pixel_size(pixel_size)?;
Ok(self)
}
pub const fn pixel_size(&self) -> u32 {
self.pixel_size
}
}
impl Effect for OrderedDither {
fn apply(
&self,
input: &[u8],
output: &mut [u8],
dimensions: (u32, u32),
mask: &Mask,
) -> Result<()> {
let size = u32::from(self.matrix_size);
let levels = (size * size) as i32;
let mut adjustments = [0; 64];
for y in 0..size {
for x in 0..size {
let threshold = i32::from(bayer_value(x, y, self.matrix_size));
adjustments[(y * size + x) as usize] =
(2 * threshold + 1 - levels) * 255 / (2 * levels);
}
}
quantise_cells(
input,
output,
dimensions,
mask,
self.pixel_size,
|colour, x, y| {
let adjustment = adjustments[((y % size) * size + x % size) as usize];
let colour =
colour.map(|channel| (i32::from(channel) + adjustment).clamp(0, 255) as u8);
self.palette.nearest_colour(colour)
},
);
Ok(())
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DiffusionTap {
offset_x: i32,
offset_y: i32,
weight: u32,
}
impl DiffusionTap {
pub const fn new(offset_x: i32, offset_y: i32, weight: u32) -> Self {
Self {
offset_x,
offset_y,
weight,
}
}
pub const fn offset_x(&self) -> i32 {
self.offset_x
}
pub const fn offset_y(&self) -> i32 {
self.offset_y
}
pub const fn weight(&self) -> u32 {
self.weight
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum DiffusionAlgorithm {
FloydSteinberg,
Atkinson,
JarvisJudiceNinke,
Stucki,
Burkes,
Sierra,
TwoRowSierra,
SierraLite,
}
impl DiffusionAlgorithm {
pub fn kernel(self) -> DiffusionKernel {
let (taps, divisor) = self.specification();
DiffusionKernel {
taps: taps.into(),
divisor,
preset: Some(self),
}
}
fn specification(self) -> (&'static [DiffusionTap], u32) {
match self {
Self::FloydSteinberg => (FLOYD_STEINBERG, 16),
Self::Atkinson => (ATKINSON, 8),
Self::JarvisJudiceNinke => (JARVIS_JUDICE_NINKE, 48),
Self::Stucki => (STUCKI, 42),
Self::Burkes => (BURKES, 32),
Self::Sierra => (SIERRA, 32),
Self::TwoRowSierra => (TWO_ROW_SIERRA, 16),
Self::SierraLite => (SIERRA_LITE, 4),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DiffusionKernel {
taps: Box<[DiffusionTap]>,
divisor: u32,
preset: Option<DiffusionAlgorithm>,
}
impl DiffusionKernel {
pub fn new(taps: impl Into<Box<[DiffusionTap]>>, divisor: u32) -> Result<Self> {
let taps = taps.into();
if taps.is_empty() {
return Err(DitherError::new(
ErrorKind::InvalidParameter,
"a diffusion kernel requires at least one tap",
));
}
if divisor == 0 {
return Err(DitherError::new(
ErrorKind::InvalidParameter,
"a diffusion kernel divisor must be greater than zero",
));
}
if taps.iter().any(|tap| tap.weight == 0) {
return Err(DitherError::new(
ErrorKind::InvalidParameter,
"diffusion tap weights must be greater than zero",
));
}
if taps
.iter()
.any(|tap| tap.offset_y < 0 || (tap.offset_y == 0 && tap.offset_x <= 0))
{
return Err(DitherError::new(
ErrorKind::InvalidParameter,
"diffusion taps must point forward in a raster scan",
));
}
Ok(Self {
taps,
divisor,
preset: None,
})
}
pub fn taps(&self) -> &[DiffusionTap] {
&self.taps
}
pub const fn divisor(&self) -> u32 {
self.divisor
}
pub const fn preset(&self) -> Option<DiffusionAlgorithm> {
self.preset
}
}
impl From<DiffusionAlgorithm> for DiffusionKernel {
fn from(algorithm: DiffusionAlgorithm) -> Self {
algorithm.kernel()
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum DiffusionScan {
#[default]
Raster,
Serpentine,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ErrorDiffusion {
palette: Palette,
kernel: DiffusionKernel,
scan: DiffusionScan,
pixel_size: u32,
strength: u16,
error_clamp: Option<u8>,
}
impl ErrorDiffusion {
pub fn new(palette: Palette, kernel: impl Into<DiffusionKernel>) -> Self {
Self {
palette,
kernel: kernel.into(),
scan: DiffusionScan::Raster,
pixel_size: 1,
strength: DIFFUSION_STRENGTH_SCALE,
error_clamp: None,
}
}
pub const fn palette(&self) -> &Palette {
&self.palette
}
pub const fn kernel(&self) -> &DiffusionKernel {
&self.kernel
}
pub const fn algorithm(&self) -> Option<DiffusionAlgorithm> {
self.kernel.preset()
}
pub const fn with_scan(mut self, scan: DiffusionScan) -> Self {
self.scan = scan;
self
}
pub const fn scan(&self) -> DiffusionScan {
self.scan
}
pub fn with_pixel_size(mut self, pixel_size: u32) -> Result<Self> {
self.pixel_size = validate_pixel_size(pixel_size)?;
Ok(self)
}
pub const fn pixel_size(&self) -> u32 {
self.pixel_size
}
pub fn with_strength(mut self, strength: f32) -> Result<Self> {
if !strength.is_finite() || !(0.0..=2.0).contains(&strength) {
return Err(DitherError::new(
ErrorKind::InvalidParameter,
"diffusion strength must be between zero and two",
));
}
self.strength = (strength * f32::from(DIFFUSION_STRENGTH_SCALE)).round() as u16;
Ok(self)
}
pub fn strength(&self) -> f32 {
f32::from(self.strength) / f32::from(DIFFUSION_STRENGTH_SCALE)
}
pub const fn with_error_clamp(mut self, maximum: u8) -> Self {
self.error_clamp = Some(maximum);
self
}
pub const fn without_error_clamp(mut self) -> Self {
self.error_clamp = None;
self
}
pub const fn error_clamp(&self) -> Option<u8> {
self.error_clamp
}
fn parameters<'a>(
&'a self,
taps: &'a [DiffusionTap],
custom_divisor: u32,
) -> DiffusionParameters<'a> {
DiffusionParameters {
palette: &self.palette,
taps,
pixel_size: self.pixel_size,
strength: self.strength,
error_clamp: self.error_clamp,
custom_divisor,
}
}
fn apply_scan<const SERPENTINE: bool>(
&self,
input: &[u8],
output: &mut [u8],
dimensions: (u32, u32),
mask: &Mask,
) {
if self.strength == DIFFUSION_STRENGTH_SCALE
&& self.error_clamp.is_none()
&& self.kernel.preset().is_some()
{
self.apply_kernel::<SERPENTINE, true>(input, output, dimensions, mask);
} else {
self.apply_kernel::<SERPENTINE, false>(input, output, dimensions, mask);
}
}
fn apply_kernel<const SERPENTINE: bool, const DEFAULT: bool>(
&self,
input: &[u8],
output: &mut [u8],
dimensions: (u32, u32),
mask: &Mask,
) {
match self.kernel.preset() {
Some(DiffusionAlgorithm::FloydSteinberg) => diffuse_error::<16, SERPENTINE, DEFAULT>(
input,
output,
dimensions,
mask,
self.parameters(FLOYD_STEINBERG, 0),
),
Some(DiffusionAlgorithm::Atkinson) => diffuse_error::<8, SERPENTINE, DEFAULT>(
input,
output,
dimensions,
mask,
self.parameters(ATKINSON, 0),
),
Some(DiffusionAlgorithm::JarvisJudiceNinke) => {
diffuse_error::<48, SERPENTINE, DEFAULT>(
input,
output,
dimensions,
mask,
self.parameters(JARVIS_JUDICE_NINKE, 0),
)
}
Some(DiffusionAlgorithm::Stucki) => diffuse_error::<42, SERPENTINE, DEFAULT>(
input,
output,
dimensions,
mask,
self.parameters(STUCKI, 0),
),
Some(DiffusionAlgorithm::Burkes) => diffuse_error::<32, SERPENTINE, DEFAULT>(
input,
output,
dimensions,
mask,
self.parameters(BURKES, 0),
),
Some(DiffusionAlgorithm::Sierra) => diffuse_error::<32, SERPENTINE, DEFAULT>(
input,
output,
dimensions,
mask,
self.parameters(SIERRA, 0),
),
Some(DiffusionAlgorithm::TwoRowSierra) => diffuse_error::<16, SERPENTINE, DEFAULT>(
input,
output,
dimensions,
mask,
self.parameters(TWO_ROW_SIERRA, 0),
),
Some(DiffusionAlgorithm::SierraLite) => diffuse_error::<4, SERPENTINE, DEFAULT>(
input,
output,
dimensions,
mask,
self.parameters(SIERRA_LITE, 0),
),
None => diffuse_error::<0, SERPENTINE, false>(
input,
output,
dimensions,
mask,
self.parameters(self.kernel.taps(), self.kernel.divisor),
),
}
}
}
impl Effect for ErrorDiffusion {
fn apply(
&self,
input: &[u8],
output: &mut [u8],
dimensions: (u32, u32),
mask: &Mask,
) -> Result<()> {
match self.scan {
DiffusionScan::Raster => self.apply_scan::<false>(input, output, dimensions, mask),
DiffusionScan::Serpentine => self.apply_scan::<true>(input, output, dimensions, mask),
}
Ok(())
}
}
fn quantise_cells(
input: &[u8],
output: &mut [u8],
dimensions: (u32, u32),
mask: &Mask,
pixel_size: u32,
mut quantise: impl FnMut([u8; 3], u32, u32) -> [u8; 3],
) {
let Some((min_x, min_y, max_x, max_y)) = mask.coverage_bounds() else {
return;
};
let image_width = dimensions.0 as usize;
if pixel_size == 1 {
for y in min_y..max_y {
for x in min_x..max_x {
let pixel_index = y as usize * image_width + x as usize;
if mask.coverage_bytes()[pixel_index] == 0 {
continue;
}
let byte_index = pixel_index * 4;
let colour = quantise(
[
input[byte_index],
input[byte_index + 1],
input[byte_index + 2],
],
x,
y,
);
output[byte_index..byte_index + 4].copy_from_slice(&[
colour[0],
colour[1],
colour[2],
input[byte_index + 3],
]);
}
}
return;
}
let start_x = align_to_grid(min_x, pixel_size);
let start_y = align_to_grid(min_y, pixel_size);
let mut y = start_y;
while y < max_y {
let mut x = start_x;
while x < max_x {
let bounds = cell_bounds(x, y, pixel_size, dimensions);
if let Some(colour) = sample_cell(input, mask, image_width, bounds) {
let colour = quantise(colour, x / pixel_size, y / pixel_size);
write_cell(input, output, mask, image_width, bounds, colour);
}
x = x.saturating_add(pixel_size);
}
y = y.saturating_add(pixel_size);
}
}
struct DiffusionParameters<'a> {
palette: &'a Palette,
taps: &'a [DiffusionTap],
pixel_size: u32,
strength: u16,
error_clamp: Option<u8>,
custom_divisor: u32,
}
fn diffuse_error<const DIVISOR: i32, const SERPENTINE: bool, const DEFAULT: bool>(
input: &[u8],
output: &mut [u8],
dimensions: (u32, u32),
mask: &Mask,
parameters: DiffusionParameters<'_>,
) {
let DiffusionParameters {
palette,
taps,
pixel_size,
strength,
error_clamp,
custom_divisor,
} = parameters;
let Some((min_x, min_y, max_x, max_y)) = mask.coverage_bounds() else {
return;
};
let divisor = if DIVISOR == 0 {
i64::from(custom_divisor)
} else {
i64::from(DIVISOR)
};
let image_width = dimensions.0 as usize;
let start_x = align_to_grid(min_x, pixel_size);
let start_y = align_to_grid(min_y, pixel_size);
let working_width = (max_x - start_x).div_ceil(pixel_size) as usize;
let working_height = (max_y - start_y).div_ceil(pixel_size) as usize;
let mut selected = Vec::with_capacity(working_width * working_height);
let mut working = Vec::with_capacity(working_width * working_height);
for cell_y in 0..working_height {
for cell_x in 0..working_width {
let x = start_x + cell_x as u32 * pixel_size;
let y = start_y + cell_y as u32 * pixel_size;
let bounds = cell_bounds(x, y, pixel_size, dimensions);
let colour = sample_cell(input, mask, image_width, bounds);
selected.push(colour.is_some());
working.push(
colour
.unwrap_or([0; 3])
.map(|channel| i32::from(channel) * FIXED_SCALE),
);
}
}
for cell_y in 0..working_height {
let reverse = SERPENTINE && (start_y / pixel_size + cell_y as u32) % 2 == 1;
for column in 0..working_width {
let cell_x = if reverse {
working_width - column - 1
} else {
column
};
let working_index = cell_y * working_width + cell_x;
if !selected[working_index] {
continue;
}
let adjusted =
working[working_index].map(|channel| channel.clamp(0, 255 * FIXED_SCALE));
let colour = palette.nearest_colour(
adjusted.map(|channel| ((channel + FIXED_SCALE / 2) / FIXED_SCALE) as u8),
);
let x = start_x + cell_x as u32 * pixel_size;
let y = start_y + cell_y as u32 * pixel_size;
write_cell(
input,
output,
mask,
image_width,
cell_bounds(x, y, pixel_size, dimensions),
colour,
);
let error = [
adjusted[0] - i32::from(colour[0]) * FIXED_SCALE,
adjusted[1] - i32::from(colour[1]) * FIXED_SCALE,
adjusted[2] - i32::from(colour[2]) * FIXED_SCALE,
];
let error = if DEFAULT {
error
} else if let Some(maximum) = error_clamp {
let maximum = i32::from(maximum) * FIXED_SCALE;
error.map(|channel| channel.clamp(-maximum, maximum))
} else {
error
};
for tap in taps {
let offset_x = i64::from(tap.offset_x);
let offset_x = if reverse { -offset_x } else { offset_x };
let offset_y = i64::from(tap.offset_y);
let neighbour_x = cell_x as i64 + offset_x;
let neighbour_y = cell_y as i64 + offset_y;
if neighbour_x < 0
|| neighbour_x >= working_width as i64
|| neighbour_y < 0
|| neighbour_y >= working_height as i64
{
continue;
}
let neighbour_x = neighbour_x as usize;
let neighbour_y = neighbour_y as usize;
let neighbour_index = neighbour_y * working_width + neighbour_x;
if !selected[neighbour_index] {
continue;
}
if !diffusion_path_is_selected(
&selected,
working_width,
cell_x,
cell_y,
offset_x,
offset_y,
) {
continue;
}
for channel in 0..3 {
if DEFAULT {
working[neighbour_index][channel] +=
error[channel] * tap.weight as i32 / DIVISOR;
} else {
let contribution =
i64::from(error[channel]) * i64::from(tap.weight) * i64::from(strength)
/ (divisor * i64::from(DIFFUSION_STRENGTH_SCALE));
let contribution =
contribution.clamp(i64::from(i32::MIN), i64::from(i32::MAX));
working[neighbour_index][channel] =
working[neighbour_index][channel].saturating_add(contribution as i32);
}
}
}
}
}
}
fn align_to_grid(coordinate: u32, pixel_size: u32) -> u32 {
coordinate - coordinate % pixel_size
}
fn cell_bounds(x: u32, y: u32, pixel_size: u32, dimensions: (u32, u32)) -> (u32, u32, u32, u32) {
(
x,
y,
x.saturating_add(pixel_size).min(dimensions.0),
y.saturating_add(pixel_size).min(dimensions.1),
)
}
fn sample_cell(
input: &[u8],
mask: &Mask,
image_width: usize,
bounds: (u32, u32, u32, u32),
) -> Option<[u8; 3]> {
if bounds.2 - bounds.0 == 1 && bounds.3 - bounds.1 == 1 {
let pixel_index = bounds.1 as usize * image_width + bounds.0 as usize;
if mask.coverage_bytes()[pixel_index] == 0 {
return None;
}
let byte_index = pixel_index * 4;
return Some([
input[byte_index],
input[byte_index + 1],
input[byte_index + 2],
]);
}
let mut totals = [0_u64; 3];
let mut total_coverage = 0_u64;
for y in bounds.1..bounds.3 {
for x in bounds.0..bounds.2 {
let pixel_index = y as usize * image_width + x as usize;
let coverage = u64::from(mask.coverage_bytes()[pixel_index]);
if coverage == 0 {
continue;
}
let byte_index = pixel_index * 4;
for channel in 0..3 {
totals[channel] += u64::from(input[byte_index + channel]) * coverage;
}
total_coverage += coverage;
}
}
(total_coverage != 0)
.then(|| totals.map(|total| ((total + total_coverage / 2) / total_coverage) as u8))
}
fn write_cell(
input: &[u8],
output: &mut [u8],
mask: &Mask,
image_width: usize,
bounds: (u32, u32, u32, u32),
colour: [u8; 3],
) {
for y in bounds.1..bounds.3 {
for x in bounds.0..bounds.2 {
let pixel_index = y as usize * image_width + x as usize;
if mask.coverage_bytes()[pixel_index] == 0 {
continue;
}
let byte_index = pixel_index * 4;
output[byte_index..byte_index + 4].copy_from_slice(&[
colour[0],
colour[1],
colour[2],
input[byte_index + 3],
]);
}
}
}
fn diffusion_path_is_selected(
selected: &[bool],
width: usize,
x: usize,
y: usize,
offset_x: i64,
offset_y: i64,
) -> bool {
let middle = match (offset_x, offset_y) {
(-2 | 2, 0) => Some((x as i64 + offset_x / 2, y as i64)),
(0, -2 | 2) => Some((x as i64, y as i64 + offset_y / 2)),
_ => None,
};
middle.is_none_or(|(x, y)| selected[y as usize * width + x as usize])
}
fn validate_pixel_size(pixel_size: u32) -> Result<u32> {
if pixel_size == 0 {
return Err(DitherError::new(
ErrorKind::InvalidParameter,
"dither pixel size must be greater than zero",
));
}
Ok(pixel_size)
}
fn bayer_value(x: u32, y: u32, size: u8) -> u8 {
if size == 1 {
return 0;
}
let x = x % u32::from(size);
let y = y % u32::from(size);
let half = u32::from(size / 2);
let offset = match (x / half, y / half) {
(0, 0) => 0,
(1, 0) => 2,
(0, 1) => 3,
(1, 1) => 1,
_ => unreachable!("coordinates are reduced to the Bayer matrix size"),
};
4 * bayer_value(x % half, y % half, size / 2) + offset
}
fn colour_distance(left: [u8; 3], right: [u8; 3]) -> u32 {
left.into_iter()
.zip(right)
.map(|(left, right)| {
let difference = i32::from(left) - i32::from(right);
(difference * difference) as u32
})
.sum()
}
#[cfg(test)]
mod tests {
use super::{
Color, Colour, DiffusionAlgorithm, DiffusionKernel, DiffusionScan, DiffusionTap,
ErrorDiffusion, OrderedDither, Palette, Threshold, bayer_value,
};
use crate::{Effect, ErrorKind, Mask, Point, Polygon, Renderer, Selection, SourceImage};
fn source(width: u32, height: u32, pixels: &[[u8; 4]]) -> SourceImage {
SourceImage {
width,
height,
pixels: pixels
.iter()
.flatten()
.copied()
.collect::<Vec<_>>()
.into_boxed_slice(),
}
}
fn diffusion(algorithm: DiffusionAlgorithm) -> ErrorDiffusion {
ErrorDiffusion::new(Palette::black_and_white(), algorithm)
}
#[test]
fn preserves_0_3_1_output_for_every_diffusion_preset() {
let pixels = (0..35)
.map(|index| {
let value = ((index * 47 + index * index * 3) % 256) as u8;
[value, value.wrapping_add(53), value.wrapping_mul(3), 200]
})
.collect::<Vec<_>>();
let source = source(7, 5, &pixels);
let algorithms = [
DiffusionAlgorithm::FloydSteinberg,
DiffusionAlgorithm::Atkinson,
DiffusionAlgorithm::JarvisJudiceNinke,
DiffusionAlgorithm::Stucki,
DiffusionAlgorithm::Burkes,
DiffusionAlgorithm::Sierra,
DiffusionAlgorithm::TwoRowSierra,
DiffusionAlgorithm::SierraLite,
];
let expected = [
0xad91ccff1c25d794,
0xef9744fca489e2ad,
0x0b826bc567c18a14,
0x2a9383304415dc4d,
0xf901ea661db4d834,
0xef9744fca489e2ad,
0x5ebc740cdf5958b4,
0x8c4a4e3463ac974d,
];
for (algorithm, expected) in algorithms.into_iter().zip(expected) {
let rendered = Renderer::new()
.render(&source, &diffusion(algorithm), &Selection::All)
.unwrap();
let hash = rendered
.rgba8_bytes()
.iter()
.fold(0xcbf29ce484222325_u64, |hash, byte| {
(hash ^ u64::from(*byte)).wrapping_mul(0x100000001b3)
});
assert_eq!(hash, expected, "changed output for {algorithm:?}");
}
}
#[test]
fn validates_and_exposes_custom_diffusion_kernels() {
let taps = [
DiffusionTap::new(1, 0, 7),
DiffusionTap::new(-1, 1, 3),
DiffusionTap::new(0, 1, 5),
DiffusionTap::new(1, 1, 1),
];
let kernel = DiffusionKernel::new(taps, 16).unwrap();
assert_eq!(kernel.taps(), taps);
assert_eq!(kernel.divisor(), 16);
assert_eq!(kernel.preset(), None);
assert_eq!(kernel.taps()[0].offset_x(), 1);
assert_eq!(kernel.taps()[0].offset_y(), 0);
assert_eq!(kernel.taps()[0].weight(), 7);
let preset = DiffusionAlgorithm::FloydSteinberg.kernel();
assert_eq!(preset.taps(), taps);
assert_eq!(preset.divisor(), 16);
assert_eq!(preset.preset(), Some(DiffusionAlgorithm::FloydSteinberg));
let invalid = [
DiffusionKernel::new(Vec::<DiffusionTap>::new(), 1).unwrap_err(),
DiffusionKernel::new([DiffusionTap::new(1, 0, 1)], 0).unwrap_err(),
DiffusionKernel::new([DiffusionTap::new(1, 0, 0)], 1).unwrap_err(),
DiffusionKernel::new([DiffusionTap::new(0, 0, 1)], 1).unwrap_err(),
DiffusionKernel::new([DiffusionTap::new(-1, 0, 1)], 1).unwrap_err(),
DiffusionKernel::new([DiffusionTap::new(0, -1, 1)], 1).unwrap_err(),
];
assert!(
invalid
.iter()
.all(|error| error.kind() == ErrorKind::InvalidParameter)
);
}
#[test]
fn custom_kernel_matches_its_builtin_equivalent() {
let source = source(
6,
2,
&[
[20, 40, 60, 1],
[70, 90, 110, 2],
[120, 140, 160, 3],
[170, 190, 210, 4],
[220, 240, 250, 5],
[100, 130, 160, 6],
[210, 180, 150, 7],
[160, 130, 100, 8],
[110, 80, 50, 9],
[60, 30, 10, 10],
[130, 170, 210, 11],
[230, 190, 150, 12],
],
);
let custom = DiffusionKernel::new(
[
DiffusionTap::new(1, 0, 7),
DiffusionTap::new(-1, 1, 3),
DiffusionTap::new(0, 1, 5),
DiffusionTap::new(1, 1, 1),
],
16,
)
.unwrap();
for scan in [DiffusionScan::Raster, DiffusionScan::Serpentine] {
let builtin = Renderer::new()
.render(
&source,
&diffusion(DiffusionAlgorithm::FloydSteinberg).with_scan(scan),
&Selection::All,
)
.unwrap();
let rendered = Renderer::new()
.render(
&source,
&ErrorDiffusion::new(Palette::black_and_white(), custom.clone())
.with_scan(scan),
&Selection::All,
)
.unwrap();
assert_eq!(rendered.rgba8_bytes(), builtin.rgba8_bytes());
}
}
#[test]
fn configures_diffusion_strength_and_error_clamping() {
let source = source(4, 1, &[[100, 100, 100, 70]; 4]);
let threshold = Renderer::new()
.render(
&source,
&Threshold::new(Palette::black_and_white()),
&Selection::All,
)
.unwrap();
let no_diffusion = diffusion(DiffusionAlgorithm::FloydSteinberg)
.with_strength(0.0)
.unwrap();
let no_error = diffusion(DiffusionAlgorithm::FloydSteinberg).with_error_clamp(0);
for effect in [&no_diffusion, &no_error] {
let rendered = Renderer::new()
.render(&source, effect, &Selection::All)
.unwrap();
assert_eq!(rendered.rgba8_bytes(), threshold.rgba8_bytes());
}
let configured = diffusion(DiffusionAlgorithm::Stucki)
.with_strength(1.5)
.unwrap()
.with_error_clamp(32);
assert_eq!(configured.strength(), 1.5);
assert_eq!(configured.error_clamp(), Some(32));
assert_eq!(configured.clone().without_error_clamp().error_clamp(), None);
assert_eq!(diffusion(DiffusionAlgorithm::Stucki).strength(), 1.0);
assert_eq!(diffusion(DiffusionAlgorithm::Stucki).error_clamp(), None);
for strength in [-0.1, 2.1, f32::NAN, f32::INFINITY] {
assert_eq!(
diffusion(DiffusionAlgorithm::Stucki)
.with_strength(strength)
.unwrap_err()
.kind(),
ErrorKind::InvalidParameter
);
}
}
#[test]
fn validates_palette_and_matches_custom_colours() {
let error = Palette::new(Vec::<[u8; 3]>::new()).unwrap_err();
assert_eq!(error.kind(), ErrorKind::InvalidParameter);
let palette = Palette::new([[255, 0, 0], [0, 0, 255]]).unwrap();
assert_eq!(palette.colours(), &[[255, 0, 0], [0, 0, 255]]);
assert_eq!(palette.nearest_colour([200, 10, 40]), [255, 0, 0]);
assert_eq!(palette.nearest_colour([40, 10, 200]), [0, 0, 255]);
assert_eq!(palette.nearest_colour([127, 0, 127]), [255, 0, 0]);
let black_and_white = Palette::black_and_white();
assert_eq!(black_and_white.nearest_colour([255, 127, 0]), [0; 3]);
assert_eq!(black_and_white.nearest_colour([0, 128, 255]), [255; 3]);
assert_eq!(
Palette::monochrome([1, 2, 3]).colours(),
&[[0, 0, 0], [1, 2, 3], [255, 255, 255]]
);
assert_eq!(
Palette::monochrome([0, 0, 0]).colours(),
&[[0, 0, 0], [255, 255, 255]]
);
assert_eq!(
Palette::monochrome([255, 255, 255]).colours(),
&[[0, 0, 0], [255, 255, 255]]
);
assert_eq!(
Palette::black_and_white().colours(),
&[[0, 0, 0], [255, 255, 255]]
);
}
#[test]
fn provides_colour_values_and_prefab_palettes() {
let colour = Colour::new(12, 34, 56);
let alias: Color = colour;
assert_eq!(alias.rgb(), [12, 34, 56]);
assert_eq!(Colour::from([12, 34, 56]), colour);
assert_eq!(<[u8; 3]>::from(colour), [12, 34, 56]);
let custom = Palette::new([Colour::BLACK, colour, Colour::WHITE]).unwrap();
assert_eq!(
custom.colours(),
&[[0, 0, 0], [12, 34, 56], [255, 255, 255]]
);
assert_eq!(
custom.iter().collect::<Vec<_>>(),
[Colour::BLACK, colour, Colour::WHITE]
);
assert_eq!(custom.nearest(Colour::new(10, 30, 50)), colour);
assert_eq!(
Palette::greyscale().colours(),
&[
[0, 0, 0],
[36, 36, 36],
[73, 73, 73],
[109, 109, 109],
[146, 146, 146],
[182, 182, 182],
[219, 219, 219],
[255, 255, 255],
]
);
assert_eq!(Palette::game_boy().colours().len(), 4);
assert_eq!(Palette::cga().colours().len(), 16);
assert_eq!(Palette::pico_8().colours().len(), 16);
assert_eq!(Palette::cga().colours()[6], [170, 85, 0]);
assert_eq!(Palette::pico_8().colours()[8], [255, 0, 77]);
}
#[test]
fn thresholds_exact_black_and_white_pixels_and_preserves_alpha() {
let source = source(
3,
1,
&[[10, 20, 30, 1], [128, 128, 128, 2], [245, 235, 225, 3]],
);
let rendered = Renderer::new()
.render(
&source,
&Threshold::new(Palette::black_and_white()),
&Selection::All,
)
.unwrap();
assert_eq!(rendered.pixel(0, 0), Some([0, 0, 0, 1]));
assert_eq!(rendered.pixel(1, 0), Some([255, 255, 255, 2]));
assert_eq!(rendered.pixel(2, 0), Some([255, 255, 255, 3]));
}
#[test]
fn thresholds_monochrome_with_black_colour_and_white() {
let source = source(
3,
1,
&[[10, 10, 10, 1], [240, 10, 10, 2], [250, 250, 250, 3]],
);
let rendered = Renderer::new()
.render(
&source,
&Threshold::new(Palette::monochrome([255, 0, 0])),
&Selection::All,
)
.unwrap();
assert_eq!(rendered.pixel(0, 0), Some([0, 0, 0, 1]));
assert_eq!(rendered.pixel(1, 0), Some([255, 0, 0, 2]));
assert_eq!(rendered.pixel(2, 0), Some([255, 255, 255, 3]));
}
#[test]
fn thresholds_custom_palette() {
let source = source(2, 1, &[[240, 20, 10, 4], [20, 10, 240, 5]]);
let effect = Threshold::new(Palette::new([[255, 0, 0], [0, 0, 255]]).unwrap());
let rendered = Renderer::new()
.render(&source, &effect, &Selection::All)
.unwrap();
assert_eq!(rendered.pixel(0, 0), Some([255, 0, 0, 4]));
assert_eq!(rendered.pixel(1, 0), Some([0, 0, 255, 5]));
}
#[test]
fn thresholds_only_the_selected_polygon() {
let source = source(3, 1, &[[100, 100, 100, 10]; 3]);
let polygon = Polygon::new([
Point::new(1.0, 0.0),
Point::new(2.0, 0.0),
Point::new(2.0, 1.0),
Point::new(1.0, 1.0),
])
.unwrap();
let rendered = Renderer::new()
.render(
&source,
&Threshold::new(Palette::black_and_white()),
&Selection::Polygon(polygon),
)
.unwrap();
assert_eq!(rendered.pixel(0, 0), Some([100, 100, 100, 10]));
assert_eq!(rendered.pixel(1, 0), Some([0, 0, 0, 10]));
assert_eq!(rendered.pixel(2, 0), Some([100, 100, 100, 10]));
}
#[test]
fn validates_bayer_matrix_sizes() {
for size in [2, 4, 8] {
assert_eq!(
OrderedDither::new(Palette::black_and_white(), size)
.unwrap()
.matrix_size(),
size
);
}
for size in [0, 1, 3, 16] {
assert_eq!(
OrderedDither::new(Palette::black_and_white(), size)
.unwrap_err()
.kind(),
ErrorKind::InvalidParameter
);
}
}
#[test]
fn uses_standard_bayer_matrices() {
let expected = [
(2, vec![0, 2, 3, 1]),
(
4,
vec![0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5],
),
(
8,
vec![
0, 32, 8, 40, 2, 34, 10, 42, 48, 16, 56, 24, 50, 18, 58, 26, 12, 44, 4, 36, 14,
46, 6, 38, 60, 28, 52, 20, 62, 30, 54, 22, 3, 35, 11, 43, 1, 33, 9, 41, 51, 19,
59, 27, 49, 17, 57, 25, 15, 47, 7, 39, 13, 45, 5, 37, 63, 31, 55, 23, 61, 29,
53, 21,
],
),
];
for (size, expected) in expected {
let actual = (0..size)
.flat_map(|y| (0..size).map(move |x| bayer_value(x, y, size as u8)))
.collect::<Vec<_>>();
assert_eq!(actual, expected);
}
}
#[test]
fn renders_exact_pixels_for_each_bayer_size() {
for size in [2, 4, 8] {
let source = source(size.into(), 1, &vec![[128, 128, 128, 90]; size.into()]);
let effect = OrderedDither::new(Palette::black_and_white(), size).unwrap();
let rendered = Renderer::new()
.render(&source, &effect, &Selection::All)
.unwrap();
for x in 0..u32::from(size) {
let value = if x % 2 == 0 { 0 } else { 255 };
assert_eq!(rendered.pixel(x, 0), Some([value, value, value, 90]));
}
}
}
#[test]
fn keeps_polygon_patterns_anchored_to_image_coordinates() {
let source = source(4, 1, &[[128, 128, 128, 80]; 4]);
let effect = OrderedDither::new(Palette::black_and_white(), 4).unwrap();
let all = Renderer::new()
.render(&source, &effect, &Selection::All)
.unwrap();
let polygon = Polygon::new([
Point::new(1.0, 0.0),
Point::new(3.0, 0.0),
Point::new(3.0, 1.0),
Point::new(1.0, 1.0),
])
.unwrap();
let selected = Renderer::new()
.render(&source, &effect, &Selection::Polygon(polygon))
.unwrap();
assert_eq!(selected.pixel(0, 0), source.pixel(0, 0));
assert_eq!(selected.pixel(1, 0), all.pixel(1, 0));
assert_eq!(selected.pixel(2, 0), all.pixel(2, 0));
assert_eq!(selected.pixel(3, 0), source.pixel(3, 0));
}
#[test]
fn ordered_dithering_is_repeatable() {
let source = source(
3,
1,
&[[40, 80, 120, 1], [100, 140, 180, 2], [160, 200, 240, 3]],
);
let effect = OrderedDither::new(
Palette::new([[0, 20, 40], [100, 120, 140], [220, 240, 255]]).unwrap(),
4,
)
.unwrap();
let first = Renderer::new()
.render(&source, &effect, &Selection::All)
.unwrap();
let second = Renderer::new()
.render(&source, &effect, &Selection::All)
.unwrap();
assert_eq!(first.rgba8_bytes(), second.rgba8_bytes());
}
#[test]
fn floyd_steinberg_produces_exact_pixels() {
let source = source(4, 1, &[[100, 100, 100, 70]; 4]);
let rendered = Renderer::new()
.render(
&source,
&diffusion(DiffusionAlgorithm::FloydSteinberg),
&Selection::All,
)
.unwrap();
assert_eq!(
rendered.rgba8_bytes(),
&[0, 0, 0, 70, 255, 255, 255, 70, 0, 0, 0, 70, 0, 0, 0, 70,]
);
}
#[test]
fn floyd_steinberg_does_not_import_error_across_a_polygon_boundary() {
let source = source(2, 1, &[[100, 100, 100, 20]; 2]);
let polygon = Polygon::new([
Point::new(1.0, 0.0),
Point::new(2.0, 0.0),
Point::new(2.0, 1.0),
Point::new(1.0, 1.0),
])
.unwrap();
let rendered = Renderer::new()
.render(
&source,
&diffusion(DiffusionAlgorithm::FloydSteinberg),
&Selection::Polygon(polygon),
)
.unwrap();
assert_eq!(rendered.pixel(0, 0), Some([100, 100, 100, 20]));
assert_eq!(rendered.pixel(1, 0), Some([0, 0, 0, 20]));
}
#[test]
fn floyd_steinberg_handles_narrow_edge_selections() {
let source = source(
1,
3,
&[[80, 80, 80, 1], [120, 120, 120, 2], [160, 160, 160, 3]],
);
let rendered = Renderer::new()
.render(
&source,
&diffusion(DiffusionAlgorithm::FloydSteinberg),
&Selection::All,
)
.unwrap();
assert_eq!(rendered.dimensions(), (1, 3));
assert_eq!(
rendered
.rgba8_bytes()
.as_chunks::<4>()
.0
.iter()
.map(|pixel| pixel[3])
.collect::<Vec<_>>(),
[1, 2, 3]
);
}
#[test]
fn atkinson_produces_exact_pixels() {
let source = source(4, 1, &[[100, 100, 100, 70]; 4]);
let rendered = Renderer::new()
.render(
&source,
&diffusion(DiffusionAlgorithm::Atkinson),
&Selection::All,
)
.unwrap();
assert_eq!(
rendered.rgba8_bytes(),
&[0, 0, 0, 70, 0, 0, 0, 70, 0, 0, 0, 70, 255, 255, 255, 70,]
);
}
#[test]
fn atkinson_does_not_jump_unselected_gaps() {
let input = [100, 100, 100, 1, 0, 0, 0, 2, 120, 120, 120, 3];
let mut output = input;
let mask = Mask::new(3, 1, vec![255, 0, 255]).unwrap();
diffusion(DiffusionAlgorithm::Atkinson)
.apply(&input, &mut output, (3, 1), &mask)
.unwrap();
assert_eq!(output, [0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3]);
}
#[test]
fn atkinson_handles_narrow_image_edges() {
let source = source(1, 2, &[[100, 100, 100, 4], [140, 140, 140, 5]]);
let rendered = Renderer::new()
.render(
&source,
&diffusion(DiffusionAlgorithm::Atkinson),
&Selection::All,
)
.unwrap();
assert_eq!(rendered.dimensions(), (1, 2));
assert_eq!(rendered.pixel(0, 0).unwrap()[3], 4);
assert_eq!(rendered.pixel(0, 1).unwrap()[3], 5);
}
#[test]
fn error_diffusion_is_repeatable() {
let source = source(
3,
2,
&[
[30, 60, 90, 1],
[70, 100, 130, 2],
[110, 140, 170, 3],
[150, 180, 210, 4],
[190, 220, 250, 5],
[230, 200, 170, 6],
],
);
let floyd = diffusion(DiffusionAlgorithm::FloydSteinberg);
let first = Renderer::new()
.render(&source, &floyd, &Selection::All)
.unwrap();
let second = Renderer::new()
.render(&source, &floyd, &Selection::All)
.unwrap();
assert_eq!(first.rgba8_bytes(), second.rgba8_bytes());
let atkinson = diffusion(DiffusionAlgorithm::Atkinson);
let first = Renderer::new()
.render(&source, &atkinson, &Selection::All)
.unwrap();
let second = Renderer::new()
.render(&source, &atkinson, &Selection::All)
.unwrap();
assert_eq!(first.rgba8_bytes(), second.rgba8_bytes());
}
#[test]
fn validates_dither_pixel_sizes() {
assert_eq!(Threshold::new(Palette::black_and_white()).pixel_size(), 1);
assert_eq!(
Threshold::new(Palette::black_and_white())
.with_pixel_size(3)
.unwrap()
.pixel_size(),
3
);
let errors = [
Threshold::new(Palette::black_and_white())
.with_pixel_size(0)
.unwrap_err(),
OrderedDither::new(Palette::black_and_white(), 2)
.unwrap()
.with_pixel_size(0)
.unwrap_err(),
diffusion(DiffusionAlgorithm::FloydSteinberg)
.with_pixel_size(0)
.unwrap_err(),
diffusion(DiffusionAlgorithm::Atkinson)
.with_pixel_size(0)
.unwrap_err(),
];
assert!(
errors
.iter()
.all(|error| error.kind() == ErrorKind::InvalidParameter)
);
}
#[test]
fn threshold_fills_logical_pixels_with_their_average_colour() {
let source = source(
4,
1,
&[
[0, 0, 0, 1],
[200, 200, 200, 2],
[100, 100, 100, 3],
[255, 255, 255, 4],
],
);
let effect = Threshold::new(Palette::black_and_white())
.with_pixel_size(2)
.unwrap();
let rendered = Renderer::new()
.render(&source, &effect, &Selection::All)
.unwrap();
assert_eq!(
rendered.rgba8_bytes(),
&[0, 0, 0, 1, 0, 0, 0, 2, 255, 255, 255, 3, 255, 255, 255, 4]
);
}
#[test]
fn ordered_dithering_scales_bayer_cells() {
let source = source(4, 1, &[[128, 128, 128, 9]; 4]);
let effect = OrderedDither::new(Palette::black_and_white(), 2)
.unwrap()
.with_pixel_size(2)
.unwrap();
let rendered = Renderer::new()
.render(&source, &effect, &Selection::All)
.unwrap();
assert_eq!(
rendered.rgba8_bytes(),
&[0, 0, 0, 9, 0, 0, 0, 9, 255, 255, 255, 9, 255, 255, 255, 9]
);
}
#[test]
fn scaled_dithering_keeps_cells_anchored_and_clipped_to_a_polygon() {
let source = source(4, 1, &[[128, 128, 128, 7]; 4]);
let polygon = Polygon::new([
Point::new(3.0, 0.0),
Point::new(4.0, 0.0),
Point::new(4.0, 1.0),
Point::new(3.0, 1.0),
])
.unwrap();
let effect = OrderedDither::new(Palette::black_and_white(), 2)
.unwrap()
.with_pixel_size(2)
.unwrap();
let rendered = Renderer::new()
.render(&source, &effect, &Selection::Polygon(polygon))
.unwrap();
assert_eq!(rendered.pixel(0, 0), source.pixel(0, 0));
assert_eq!(rendered.pixel(1, 0), source.pixel(1, 0));
assert_eq!(rendered.pixel(2, 0), source.pixel(2, 0));
assert_eq!(rendered.pixel(3, 0), Some([255, 255, 255, 7]));
}
#[test]
fn error_diffusion_operates_between_logical_pixels() {
let pixels = (1..=8)
.map(|alpha| [100, 100, 100, alpha])
.collect::<Vec<_>>();
let source = source(8, 1, &pixels);
let floyd = diffusion(DiffusionAlgorithm::FloydSteinberg)
.with_pixel_size(2)
.unwrap();
let atkinson = diffusion(DiffusionAlgorithm::Atkinson)
.with_pixel_size(2)
.unwrap();
let floyd = Renderer::new()
.render(&source, &floyd, &Selection::All)
.unwrap();
let atkinson = Renderer::new()
.render(&source, &atkinson, &Selection::All)
.unwrap();
let floyd_colours = [0, 0, 255, 255, 0, 0, 0, 0];
let atkinson_colours = [0, 0, 0, 0, 0, 0, 255, 255];
for (index, pixel) in floyd.rgba8_bytes().as_chunks::<4>().0.iter().enumerate() {
let value = floyd_colours[index];
assert_eq!(*pixel, [value, value, value, (index + 1) as u8]);
}
for (index, pixel) in atkinson.rgba8_bytes().as_chunks::<4>().0.iter().enumerate() {
let value = atkinson_colours[index];
assert_eq!(*pixel, [value, value, value, (index + 1) as u8]);
}
}
#[test]
fn supports_every_diffusion_algorithm_and_configuration() {
let pixels = (0..35)
.map(|index| {
[
(index * 47) as u8,
(index * 83) as u8,
(index * 131) as u8,
(index * 19) as u8,
]
})
.collect::<Vec<_>>();
let source = source(7, 5, &pixels);
let algorithms = [
DiffusionAlgorithm::FloydSteinberg,
DiffusionAlgorithm::Atkinson,
DiffusionAlgorithm::JarvisJudiceNinke,
DiffusionAlgorithm::Stucki,
DiffusionAlgorithm::Burkes,
DiffusionAlgorithm::Sierra,
DiffusionAlgorithm::TwoRowSierra,
DiffusionAlgorithm::SierraLite,
];
for algorithm in algorithms {
let effect = ErrorDiffusion::new(Palette::black_and_white(), algorithm)
.with_scan(DiffusionScan::Serpentine)
.with_pixel_size(2)
.unwrap();
let rendered = Renderer::new()
.render(&source, &effect, &Selection::All)
.unwrap();
assert_eq!(effect.algorithm(), Some(algorithm));
assert_eq!(effect.scan(), DiffusionScan::Serpentine);
assert_eq!(effect.pixel_size(), 2);
assert_eq!(effect.palette(), &Palette::black_and_white());
for (before, after) in source
.rgba8_bytes()
.as_chunks::<4>()
.0
.iter()
.zip(rendered.rgba8_bytes().as_chunks::<4>().0)
{
assert!(after[..3] == [0; 3] || after[..3] == [255; 3]);
assert_eq!(after[3], before[3]);
}
}
assert_eq!(
ErrorDiffusion::new(Palette::black_and_white(), DiffusionAlgorithm::Stucki)
.with_pixel_size(0)
.unwrap_err()
.kind(),
ErrorKind::InvalidParameter
);
}
#[test]
fn serpentine_scan_is_anchored_to_image_rows() {
let selected_row = [
[20, 20, 20, 1],
[70, 70, 70, 2],
[110, 110, 110, 3],
[140, 140, 140, 4],
[170, 170, 170, 5],
[220, 220, 220, 6],
];
let mut pixels = vec![[0, 0, 0, 255]; 6];
pixels.extend(selected_row);
let image = source(6, 2, &pixels);
let mask = Mask::new(6, 2, [vec![0; 6], vec![255; 6]].concat()).unwrap();
let effect = ErrorDiffusion::new(
Palette::black_and_white(),
DiffusionAlgorithm::FloydSteinberg,
)
.with_scan(DiffusionScan::Serpentine);
let mut actual = image.rgba8_bytes().to_vec();
effect
.apply(image.rgba8_bytes(), &mut actual, (6, 2), &mask)
.unwrap();
let reversed = selected_row.into_iter().rev().collect::<Vec<_>>();
let reversed_source = source(6, 1, &reversed);
let reference = Renderer::new()
.render(
&reversed_source,
&diffusion(DiffusionAlgorithm::FloydSteinberg),
&Selection::All,
)
.unwrap();
let expected = reference
.rgba8_bytes()
.as_chunks::<4>()
.0
.iter()
.rev()
.flatten()
.copied()
.collect::<Vec<_>>();
assert_eq!(&actual[6 * 4..], expected);
}
}