vtracer/
converter.rs

1use std::path::Path;
2use std::{fs::File, io::Write};
3
4use super::config::{ColorMode, Config, ConverterConfig, Hierarchical};
5use super::svg::SvgFile;
6use fastrand::Rng;
7use visioncortex::color_clusters::{KeyingAction, Runner, RunnerConfig, HIERARCHICAL_MAX};
8use visioncortex::{Color, ColorImage, ColorName};
9
10const NUM_UNUSED_COLOR_ITERATIONS: usize = 6;
11/// The fraction of pixels in the top/bottom rows of the image that need to be transparent before
12/// the entire image will be keyed.
13const KEYING_THRESHOLD: f32 = 0.2;
14
15/// Convert an in-memory image into an in-memory SVG
16pub fn convert(img: ColorImage, config: Config) -> Result<SvgFile, String> {
17    let config = config.into_converter_config();
18    match config.color_mode {
19        ColorMode::Color => color_image_to_svg(img, config),
20        ColorMode::Binary => binary_image_to_svg(img, config),
21    }
22}
23
24/// Convert an image file into svg file
25pub fn convert_image_to_svg(
26    input_path: &Path,
27    output_path: &Path,
28    config: Config,
29) -> Result<(), String> {
30    let img = read_image(input_path)?;
31    let svg = convert(img, config)?;
32    write_svg(svg, output_path)
33}
34
35fn color_exists_in_image(img: &ColorImage, color: Color) -> bool {
36    for y in 0..img.height {
37        for x in 0..img.width {
38            let pixel_color = img.get_pixel(x, y);
39            if pixel_color.r == color.r && pixel_color.g == color.g && pixel_color.b == color.b {
40                return true;
41            }
42        }
43    }
44    false
45}
46
47fn find_unused_color_in_image(img: &ColorImage) -> Result<Color, String> {
48    let special_colors = IntoIterator::into_iter([
49        Color::new(255, 0, 0),
50        Color::new(0, 255, 0),
51        Color::new(0, 0, 255),
52        Color::new(255, 255, 0),
53        Color::new(0, 255, 255),
54        Color::new(255, 0, 255),
55    ]);
56    let mut rng = Rng::new();
57    let random_colors =
58        (0..NUM_UNUSED_COLOR_ITERATIONS).map(|_| Color::new(rng.u8(..), rng.u8(..), rng.u8(..)));
59    for color in special_colors.chain(random_colors) {
60        if !color_exists_in_image(img, color) {
61            return Ok(color);
62        }
63    }
64    Err(String::from(
65        "unable to find unused color in image to use as key",
66    ))
67}
68
69fn should_key_image(img: &ColorImage) -> bool {
70    if img.width == 0 || img.height == 0 {
71        return false;
72    }
73
74    // Check for transparency at several scanlines
75    let threshold = ((img.width * 2) as f32 * KEYING_THRESHOLD) as usize;
76    let mut num_transparent_pixels = 0;
77    let y_positions = [
78        0,
79        img.height / 4,
80        img.height / 2,
81        3 * img.height / 4,
82        img.height - 1,
83    ];
84    for y in y_positions {
85        for x in 0..img.width {
86            if img.get_pixel(x, y).a == 0 {
87                num_transparent_pixels += 1;
88            }
89            if num_transparent_pixels >= threshold {
90                return true;
91            }
92        }
93    }
94
95    false
96}
97
98fn color_image_to_svg(mut img: ColorImage, config: ConverterConfig) -> Result<SvgFile, String> {
99    let width = img.width;
100    let height = img.height;
101
102    let key_color = if should_key_image(&img) {
103        let key_color = find_unused_color_in_image(&img)?;
104        for y in 0..height {
105            for x in 0..width {
106                if img.get_pixel(x, y).a == 0 {
107                    img.set_pixel(x, y, &key_color);
108                }
109            }
110        }
111        key_color
112    } else {
113        // The default color is all zeroes, which is treated by visioncortex as a special value meaning no keying will be applied.
114        Color::default()
115    };
116
117    let runner = Runner::new(
118        RunnerConfig {
119            diagonal: config.layer_difference == 0,
120            hierarchical: HIERARCHICAL_MAX,
121            batch_size: 25600,
122            good_min_area: config.filter_speckle_area,
123            good_max_area: (width * height),
124            is_same_color_a: config.color_precision_loss,
125            is_same_color_b: 1,
126            deepen_diff: config.layer_difference,
127            hollow_neighbours: 1,
128            key_color,
129            keying_action: if matches!(config.hierarchical, Hierarchical::Cutout) {
130                KeyingAction::Keep
131            } else {
132                KeyingAction::Discard
133            },
134        },
135        img,
136    );
137
138    let mut clusters = runner.run();
139
140    match config.hierarchical {
141        Hierarchical::Stacked => {}
142        Hierarchical::Cutout => {
143            let view = clusters.view();
144            let image = view.to_color_image();
145            let runner = Runner::new(
146                RunnerConfig {
147                    diagonal: false,
148                    hierarchical: 64,
149                    batch_size: 25600,
150                    good_min_area: 0,
151                    good_max_area: (image.width * image.height) as usize,
152                    is_same_color_a: 0,
153                    is_same_color_b: 1,
154                    deepen_diff: 0,
155                    hollow_neighbours: 0,
156                    key_color,
157                    keying_action: KeyingAction::Discard,
158                },
159                image,
160            );
161            clusters = runner.run();
162        }
163    }
164
165    let view = clusters.view();
166
167    let mut svg = SvgFile::new(width, height, config.path_precision);
168    for &cluster_index in view.clusters_output.iter().rev() {
169        let cluster = view.get_cluster(cluster_index);
170        let paths = cluster.to_compound_path(
171            &view,
172            false,
173            config.mode,
174            config.corner_threshold,
175            config.length_threshold,
176            config.max_iterations,
177            config.splice_threshold,
178        );
179        svg.add_path(paths, cluster.residue_color());
180    }
181
182    Ok(svg)
183}
184
185fn binary_image_to_svg(img: ColorImage, config: ConverterConfig) -> Result<SvgFile, String> {
186    let img = img.to_binary_image(|x| x.r < 128);
187    let width = img.width;
188    let height = img.height;
189
190    let clusters = img.to_clusters(false);
191
192    let mut svg = SvgFile::new(width, height, config.path_precision);
193    for i in 0..clusters.len() {
194        let cluster = clusters.get_cluster(i);
195        if cluster.size() >= config.filter_speckle_area {
196            let paths = cluster.to_compound_path(
197                config.mode,
198                config.corner_threshold,
199                config.length_threshold,
200                config.max_iterations,
201                config.splice_threshold,
202            );
203            svg.add_path(paths, Color::color(&ColorName::Black));
204        }
205    }
206
207    Ok(svg)
208}
209
210fn read_image(input_path: &Path) -> Result<ColorImage, String> {
211    let img = image::open(input_path);
212    let img = match img {
213        Ok(file) => file.to_rgba8(),
214        Err(_) => return Err(String::from("No image file found at specified input path")),
215    };
216
217    let (width, height) = (img.width() as usize, img.height() as usize);
218    let img = ColorImage {
219        pixels: img.as_raw().to_vec(),
220        width,
221        height,
222    };
223
224    Ok(img)
225}
226
227fn write_svg(svg: SvgFile, output_path: &Path) -> Result<(), String> {
228    let out_file = File::create(output_path);
229    let mut out_file = match out_file {
230        Ok(file) => file,
231        Err(_) => return Err(String::from("Cannot create output file.")),
232    };
233
234    write!(&mut out_file, "{}", svg).expect("failed to write file.");
235
236    Ok(())
237}