mirror of
https://github.com/visioncortex/vtracer.git
synced 2026-09-28 06:51:21 -07:00
Alpha channel handling in CLI (#23)
* Support transparent color images * Remove unnecessary conditional * Add temporary git url to visioncortex dependency * Use fastrand instead of rand * Reduce the number of random iterations when keying * Add heuristic to avoid expensive calculations for non-transparent input * Add three additional special keying colours * Add transparency check to some inner pixels
This commit is contained in:
+2
-1
@@ -13,4 +13,5 @@ keywords = ["svg", "computer-graphics"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
clap = "2.33.3"
|
clap = "2.33.3"
|
||||||
image = "0.23.10"
|
image = "0.23.10"
|
||||||
visioncortex = "0.6.0"
|
visioncortex = { version = "0.7.0", git = "https://github.com/visioncortex/visioncortex" }
|
||||||
|
fastrand = "1.8"
|
||||||
+103
-2
@@ -1,11 +1,17 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::{fs::File, io::Write};
|
use std::{fs::File, io::Write};
|
||||||
|
|
||||||
|
use fastrand::Rng;
|
||||||
use visioncortex::{Color, ColorImage, ColorName};
|
use visioncortex::{Color, ColorImage, ColorName};
|
||||||
use visioncortex::color_clusters::{Runner, RunnerConfig, HIERARCHICAL_MAX};
|
use visioncortex::color_clusters::{Runner, RunnerConfig, KeyingAction, HIERARCHICAL_MAX};
|
||||||
use super::config::{Config, ColorMode, Hierarchical, ConverterConfig};
|
use super::config::{Config, ColorMode, Hierarchical, ConverterConfig};
|
||||||
use super::svg::SvgFile;
|
use super::svg::SvgFile;
|
||||||
|
|
||||||
|
const NUM_UNUSED_COLOR_ITERATIONS: usize = 6;
|
||||||
|
/// The fraction of pixels in the top/bottom rows of the image that need to be transparent before
|
||||||
|
/// the entire image will be keyed.
|
||||||
|
const KEYING_THRESHOLD: f32 = 0.2;
|
||||||
|
|
||||||
/// Convert an image file into svg file
|
/// Convert an image file into svg file
|
||||||
pub fn convert_image_to_svg(config: Config) -> Result<(), String> {
|
pub fn convert_image_to_svg(config: Config) -> Result<(), String> {
|
||||||
let config = config.into_converter_config();
|
let config = config.into_converter_config();
|
||||||
@@ -15,8 +21,80 @@ pub fn convert_image_to_svg(config: Config) -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn color_exists_in_image(img: &ColorImage, color: Color) -> bool {
|
||||||
|
for y in 0..img.height {
|
||||||
|
for x in 0..img.width {
|
||||||
|
let pixel_color = img.get_pixel(x, y);
|
||||||
|
if pixel_color.r == color.r && pixel_color.g == color.g && pixel_color.b == color.b {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_unused_color_in_image(img: &ColorImage) -> Result<Color, String> {
|
||||||
|
let special_colors = IntoIterator::into_iter([
|
||||||
|
Color::new(255, 0, 0),
|
||||||
|
Color::new(0, 255, 0),
|
||||||
|
Color::new(0, 0, 255),
|
||||||
|
Color::new(255, 255, 0),
|
||||||
|
Color::new(0, 255, 255),
|
||||||
|
Color::new(255, 0, 255),
|
||||||
|
]);
|
||||||
|
let rng = Rng::new();
|
||||||
|
let random_colors = (0..NUM_UNUSED_COLOR_ITERATIONS).map(|_| {
|
||||||
|
Color::new(
|
||||||
|
rng.u8(..),
|
||||||
|
rng.u8(..),
|
||||||
|
rng.u8(..),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
for color in special_colors.chain(random_colors) {
|
||||||
|
if !color_exists_in_image(img, color) {
|
||||||
|
return Ok(color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(String::from("unable to find unused color in image to use as key"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_key_image(img: &ColorImage) -> bool {
|
||||||
|
if img.width == 0 || img.height == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for transparency in top and bottom rows of pixels
|
||||||
|
let threshold = ((img.width * 2) as f32 * KEYING_THRESHOLD) as usize;
|
||||||
|
let mut num_transparent_boundary_pixels = 0;
|
||||||
|
for x in 0..img.width {
|
||||||
|
if img.get_pixel(x, 0).a == 0 {
|
||||||
|
num_transparent_boundary_pixels += 1;
|
||||||
|
}
|
||||||
|
if img.get_pixel(x, img.height - 1).a == 0 {
|
||||||
|
num_transparent_boundary_pixels += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if num_transparent_boundary_pixels >= threshold {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for transparency in some inner pixels
|
||||||
|
let x_positions = [img.width / 4, img.width / 2, 3 * img.width / 4];
|
||||||
|
let y_positions = [img.height / 4, img.height / 2, 3 * img.height / 4];
|
||||||
|
for x in x_positions {
|
||||||
|
for y in y_positions {
|
||||||
|
if img.get_pixel(x, y).a == 0 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
|
fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
|
||||||
let (img, width, height);
|
let (mut img, width, height);
|
||||||
match read_image(config.input_path) {
|
match read_image(config.input_path) {
|
||||||
Ok(values) => {
|
Ok(values) => {
|
||||||
img = values.0;
|
img = values.0;
|
||||||
@@ -26,6 +104,21 @@ fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
|
|||||||
Err(msg) => return Err(msg),
|
Err(msg) => return Err(msg),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let key_color = if should_key_image(&img) {
|
||||||
|
let key_color = find_unused_color_in_image(&img)?;
|
||||||
|
for y in 0..height {
|
||||||
|
for x in 0..width {
|
||||||
|
if img.get_pixel(x, y).a == 0 {
|
||||||
|
img.set_pixel(x, y, &key_color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
key_color
|
||||||
|
} else {
|
||||||
|
// The default color is all zeroes, which is treated by visioncortex as a special value meaning no keying will be applied.
|
||||||
|
Color::default()
|
||||||
|
};
|
||||||
|
|
||||||
let runner = Runner::new(RunnerConfig {
|
let runner = Runner::new(RunnerConfig {
|
||||||
diagonal: config.layer_difference == 0,
|
diagonal: config.layer_difference == 0,
|
||||||
hierarchical: HIERARCHICAL_MAX,
|
hierarchical: HIERARCHICAL_MAX,
|
||||||
@@ -36,6 +129,12 @@ fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
|
|||||||
is_same_color_b: 1,
|
is_same_color_b: 1,
|
||||||
deepen_diff: config.layer_difference,
|
deepen_diff: config.layer_difference,
|
||||||
hollow_neighbours: 1,
|
hollow_neighbours: 1,
|
||||||
|
key_color,
|
||||||
|
keying_action: if matches!(config.hierarchical, Hierarchical::Cutout) {
|
||||||
|
KeyingAction::Keep
|
||||||
|
} else {
|
||||||
|
KeyingAction::Discard
|
||||||
|
},
|
||||||
}, img);
|
}, img);
|
||||||
|
|
||||||
let mut clusters = runner.run();
|
let mut clusters = runner.run();
|
||||||
@@ -55,6 +154,8 @@ fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
|
|||||||
is_same_color_b: 1,
|
is_same_color_b: 1,
|
||||||
deepen_diff: 0,
|
deepen_diff: 0,
|
||||||
hollow_neighbours: 0,
|
hollow_neighbours: 0,
|
||||||
|
key_color,
|
||||||
|
keying_action: KeyingAction::Discard,
|
||||||
}, image);
|
}, image);
|
||||||
clusters = runner.run();
|
clusters = runner.run();
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user