use crate::{DitherError, Effect, ErrorKind, Mask, Result};
const FIXED_SCALE: i32 = 256;
const FLOYD_STEINBERG: &[(i64, i64, i32)] = &[(1, 0, 7), (-1, 1, 3), (0, 1, 5), (1, 1, 1)];
const ATKINSON: &[(i64, i64, i32)] = &[
(1, 0, 1),
(2, 0, 1),
(-1, 1, 1),
(0, 1, 1),
(1, 1, 1),
(0, 2, 1),
];
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Palette {
colours: Box<[[u8; 3]]>,
}
impl Palette {
pub fn new(colours: impl Into<Box<[[u8; 3]]>>) -> Result<Self> {
let colours = colours.into();
if colours.is_empty() {
return Err(DitherError::new(
ErrorKind::InvalidParameter,
"a palette requires at least one colour",
));
}
Ok(Self { colours })
}
pub fn monochrome(colour: [u8; 3]) -> Self {
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 colours(&self) -> &[[u8; 3]] {
&self.colours
}
#[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, Debug, Eq, PartialEq)]
pub struct FloydSteinberg {
palette: Palette,
pixel_size: u32,
}
impl FloydSteinberg {
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 FloydSteinberg {
fn apply(
&self,
input: &[u8],
output: &mut [u8],
dimensions: (u32, u32),
mask: &Mask,
) -> Result<()> {
diffuse_error::<16>(
input,
output,
dimensions,
mask,
&self.palette,
FLOYD_STEINBERG,
self.pixel_size,
);
Ok(())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Atkinson {
palette: Palette,
pixel_size: u32,
}
impl Atkinson {
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 Atkinson {
fn apply(
&self,
input: &[u8],
output: &mut [u8],
dimensions: (u32, u32),
mask: &Mask,
) -> Result<()> {
diffuse_error::<8>(
input,
output,
dimensions,
mask,
&self.palette,
ATKINSON,
self.pixel_size,
);
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);
}
}
fn diffuse_error<const DIVISOR: i32>(
input: &[u8],
output: &mut [u8],
dimensions: (u32, u32),
mask: &Mask,
palette: &Palette,
neighbours: &[(i64, i64, i32)],
pixel_size: u32,
) {
let Some((min_x, min_y, max_x, max_y)) = mask.coverage_bounds() else {
return;
};
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 {
for cell_x in 0..working_width {
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,
];
for &(offset_x, offset_y, weight) in neighbours {
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 {
working[neighbour_index][channel] += error[channel] * weight / DIVISOR;
}
}
}
}
}
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::{Atkinson, FloydSteinberg, 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(),
}
}
#[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 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,
&FloydSteinberg::new(Palette::black_and_white()),
&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,
&FloydSteinberg::new(Palette::black_and_white()),
&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,
&FloydSteinberg::new(Palette::black_and_white()),
&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,
&Atkinson::new(Palette::black_and_white()),
&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();
Atkinson::new(Palette::black_and_white())
.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,
&Atkinson::new(Palette::black_and_white()),
&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 = FloydSteinberg::new(Palette::black_and_white());
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 = Atkinson::new(Palette::black_and_white());
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(),
FloydSteinberg::new(Palette::black_and_white())
.with_pixel_size(0)
.unwrap_err(),
Atkinson::new(Palette::black_and_white())
.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 = FloydSteinberg::new(Palette::black_and_white())
.with_pixel_size(2)
.unwrap();
let atkinson = Atkinson::new(Palette::black_and_white())
.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]);
}
}
}