Skip to content

Commit

Permalink
Experiment with idealizing small circles
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Nov 13, 2023
1 parent 2774fc0 commit fb85430
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmdapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ keywords = ["svg", "computer-graphics"]
[dependencies]
clap = "2.33.3"
image = "0.23.10"
visioncortex = { version = "0.8.1" }
visioncortex = { version = "0.8.4", path = "../../visioncortex" }
fastrand = "1.8"
pyo3 = { version = "0.19.0", optional = true }

Expand Down
34 changes: 24 additions & 10 deletions cmdapp/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use super::config::{ColorMode, Config, ConverterConfig, Hierarchical};
use super::svg::SvgFile;
use fastrand::Rng;
use visioncortex::color_clusters::{KeyingAction, Runner, RunnerConfig, HIERARCHICAL_MAX};
use visioncortex::{Color, ColorImage, ColorName};
use visioncortex::{approximate_circle_with_spline, Color, ColorImage, ColorName, CompoundPath};

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;

const SMALL_CIRCLE: i32 = 12;

/// Convert an in-memory image into an in-memory SVG
pub fn convert(img: ColorImage, config: Config) -> Result<SvgFile, String> {
let config = config.into_converter_config();
Expand Down Expand Up @@ -167,15 +169,27 @@ fn color_image_to_svg(mut img: ColorImage, config: ConverterConfig) -> Result<Sv
let mut svg = SvgFile::new(width, height, config.path_precision);
for &cluster_index in view.clusters_output.iter().rev() {
let cluster = view.get_cluster(cluster_index);
let paths = cluster.to_compound_path(
&view,
false,
config.mode,
config.corner_threshold,
config.length_threshold,
config.max_iterations,
config.splice_threshold,
);
let paths = if cluster.rect.width() < SMALL_CIRCLE
&& cluster.rect.height() < SMALL_CIRCLE
&& cluster.to_shape(&view).is_circle()
{
let mut paths = CompoundPath::new();
paths.add_spline(approximate_circle_with_spline(
cluster.rect.left_top(),
cluster.rect.width(),
));
paths
} else {
cluster.to_compound_path(
&view,
false,
config.mode,
config.corner_threshold,
config.length_threshold,
config.max_iterations,
config.splice_threshold,
)
};
svg.add_path(paths, cluster.residue_color());
}

Expand Down

0 comments on commit fb85430

Please sign in to comment.