distance-field

Crates.iodistance-field
lib.rsdistance-field
version0.2.1
sourcesrc
created_at2017-12-16 12:19:56.315134
updated_at2024-02-09 21:27:58.077193
descriptionGenerate distance fields from images for pseudo-vector rendering
homepagehttps://github.com/tversteeg/distance-field
repositoryhttps://github.com/tversteeg/distance-field.git
max_upload_size
id43293
size87,425
Thomas Versteeg (tversteeg)

documentation

https://docs.rs/distance-field

README

distance-field

Build Status Crates.io Documentation License: GPL-3.0 Downloads

Documentation

Generate distance field bitmaps for rendering as pseudo-vector images in shaders.

An example usecase for the library would be to automatically convert asset images. You can achieve this by having a build.rs similar to this:

use std::fs::File;
use distance_field::DistanceFieldExt;

fn convert_image_to_dfield(input: &str, output: &str) {
    // Load the 'input' image
    let img = image::open(input).unwrap();

    // Generate a distance field from the image
    let outbuf = img.grayscale().distance_field(distance_field::Options {
        size: (128, 128),
        max_distance: 256,
        ..Default::default()
    });

    // Save it to 'output' as a PNG
    image::DynamicImage::ImageLuma8(outbuf).save(output).unwrap();
}

fn main() {
    convert_image_to_dfield("img/input.png", "output.png");
}

Generator binary

Checkout the repository and cd into it; then run:

cargo run --example generator -- img/input.png output.png 64 64

This will take the following image as input:

Rust logo

And generate a 64x64 distance field:

Distance field

Now we can use the generated distance field to create a vector image.

Using the distance field

First we need to scale it up with a linear interpolation:

Upscaled linear

Then we apply a treshold function:

Treshold

You can see that we have something which looks very similar to the original input image and that just from a 64x64 image! But it's still very pixelated and doesn't look like a vector image. This can be fixed by not doing a hard treshold but allowing some shades of gray.

Commit count: 74

cargo fmt