1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::image_ext::*;
use crate::*;

use ::image::Pixel;
use riddle_math::{Rect, SpacialNumericConversion, Vector2};

const GRID_INF: Vector2<i64> = Vector2 {
	x: 0xFFFF,
	y: 0xFFFF,
};

/// Generate a distance field for the given image using 8SSEDT. The scale argument roughly maps to
/// how many pixels deep the boundary region is between min and max values.
///
/// # Examples
///
/// ```
/// # use riddle_image::*; fn main() -> Result<(), ImageError> {
/// let png_bytes = include_bytes!("../../examples/image-distance-field/sample.png");
/// let png_img = Image::load(&png_bytes[..], ImageFormat::Png)?;
///
/// let processed_img = filters::distance_field(png_img, 10.0);
/// # Ok(()) }
/// ```
///
/// # Resources:
///
/// * <http://www.codersnotes.com/notes/signed-distance-fields/>
/// * <https://github.com/Lisapple/8SSEDT>
pub fn distance_field(source: Image, scale: f64) -> Image {
	let source_bounds: Rect<i64> = source.rect().convert();
	let source_img = source.image_rgbaimage();
	let mut inside_grid = vec![Vector2::new(0, 0); (source.width() * source.height()) as usize];
	let mut outside_grid = vec![Vector2::new(0, 0); (source.width() * source.height()) as usize];

	// Generate initial grids
	for y in 0..source.height() {
		for x in 0..source.width() {
			let grid_offset = (y * source.width() + x) as usize;
			let source_pixel = source_img.get_pixel(x, y).to_luma();
			if source_pixel.0[0] > 127 {
				outside_grid[grid_offset] = GRID_INF;
			} else {
				inside_grid[grid_offset] = GRID_INF;
			}
		}
	}

	do_grid_passes(&mut inside_grid, &source_bounds);
	do_grid_passes(&mut outside_grid, &source_bounds);

	let result_img = ::image::ImageBuffer::from_fn(source.width(), source.height(), |x, y| {
		let p_inside = get_grid_point(&inside_grid, &source_bounds, Vector2::new(x, y).convert());
		let p_outside = get_grid_point(&outside_grid, &source_bounds, Vector2::new(x, y).convert());

		let len_inside = (p_inside.magnitude_squared() as f64).sqrt();
		let len_outside = (p_outside.magnitude_squared() as f64).sqrt();

		let val = (((len_outside - len_inside) / scale) + 0.5).clamp(0.0, 1.0);

		::image::Luma([(val * 255.0) as u8])
	});

	let dyn_img: ::image::DynamicImage = ::image::DynamicImage::ImageLuma8(result_img);
	Image::image_from_dynimage(dyn_img)
}

fn get_grid_point(
	grid: &[Vector2<i64>],
	bounds: &Rect<i64>,
	location: Vector2<i64>,
) -> Vector2<i64> {
	if bounds.contains_point(location) {
		grid[((location.y * bounds.dimensions.x) + location.x) as usize]
	} else {
		GRID_INF
	}
}

fn set_grid_point(
	grid: &mut Vec<Vector2<i64>>,
	bounds: &Rect<i64>,
	location: Vector2<i64>,
	value: Vector2<i64>,
) {
	grid[((location.y * bounds.dimensions.x) + location.x) as usize] = value;
}

fn do_grid_passes(grid: &mut Vec<Vector2<i64>>, bounds: &Rect<i64>) {
	do_pass(
		grid,
		bounds,
		0_i64..bounds.dimensions.y,
		0_i64..bounds.dimensions.x,
		&[
			Vector2 { x: -1, y: -1 },
			Vector2 { x: 0, y: -1 },
			Vector2 { x: 1, y: -1 },
			Vector2 { x: -1, y: 0 },
			Vector2 { x: 0, y: 0 },
		],
	);

	do_pass(
		grid,
		bounds,
		0_i64..bounds.dimensions.y,
		(0_i64..bounds.dimensions.x).rev(),
		&[Vector2 { x: 0, y: 0 }, Vector2 { x: 1, y: 0 }],
	);

	do_pass(
		grid,
		bounds,
		(0_i64..bounds.dimensions.y).rev(),
		(0_i64..bounds.dimensions.x).rev(),
		&[
			Vector2 { x: 0, y: 0 },
			Vector2 { x: 1, y: 0 },
			Vector2 { x: -1, y: 1 },
			Vector2 { x: 0, y: 1 },
			Vector2 { x: 1, y: 1 },
		],
	);

	do_pass(
		grid,
		bounds,
		(0_i64..bounds.dimensions.y).rev(),
		0_i64..bounds.dimensions.x,
		&[Vector2 { x: -1, y: 0 }, Vector2 { x: 0, y: 0 }],
	);
}

fn do_pass<YIter: Iterator<Item = i64>, XIter: Iterator<Item = i64> + Clone>(
	grid: &mut Vec<Vector2<i64>>,
	bounds: &Rect<i64>,
	y_range: YIter,
	x_range: XIter,
	neighbours: &[Vector2<i64>],
) {
	for y in y_range {
		for x in x_range.clone() {
			let location = Vector2::new(x, y);
			let new_val = neighbours
				.iter()
				.map(|offset| {
					let other = get_grid_point(grid, bounds, location + *offset);
					other + *offset
				})
				.min_by_key(|p| p.magnitude_squared())
				.unwrap();
			set_grid_point(grid, bounds, location, new_val);
		}
	}
}