Skip to content

Commit 36b16de

Browse files
committed
Key transparent image in webapp
1 parent 7887c1e commit 36b16de

File tree

5 files changed

+92
-10
lines changed

5 files changed

+92
-10
lines changed

webapp/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ console_log = { version = "0.2", features = ["color"] }
2222
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
2323
serde = { version = "1.0", features = ["derive"] }
2424
serde_json = "1.0"
25-
visioncortex = "0.6.0"
25+
visioncortex = "0.8.1"
2626

2727
# The `console_error_panic_hook` crate provides better debugging of panics by
2828
# logging them with `console.error`. This is great for development, but requires

webapp/app/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ document.addEventListener('paste', function (e) {
3535

3636
// Download as SVG
3737
document.getElementById('export').addEventListener('click', function (e) {
38-
const blob = new Blob([new XMLSerializer().serializeToString(svg)], {type: 'octet/stream'}),
38+
const blob = new Blob([
39+
`<?xml version="1.0" encoding="UTF-8"?>\n`,
40+
`<!-- Generator: visioncortex VTracer -->\n`,
41+
new XMLSerializer().serializeToString(svg)
42+
], {type: 'octet/stream'}),
3943
url = window.URL.createObjectURL(blob);
4044

4145
this.href = url;
@@ -444,7 +448,7 @@ class ConverterRunner {
444448
this.converter.init();
445449
this.stopped = false;
446450
if (clustering_mode == 'binary') {
447-
svg.style.background = '#000';
451+
svg.style.background = '#fff';
448452
canvas.style.display = 'none';
449453
} else {
450454
svg.style.background = '';

webapp/src/conversion/binary_image.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl BinaryImageConverter {
7777
self.params.max_iterations,
7878
self.params.splice_threshold
7979
);
80-
let color = Color::color(&ColorName::White);
80+
let color = Color::color(&ColorName::Black);
8181
self.svg.prepend_path(
8282
&paths,
8383
&color,

webapp/src/conversion/color_image.rs

+84-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use wasm_bindgen::prelude::*;
2-
use visioncortex::PathSimplifyMode;
3-
use visioncortex::color_clusters::{IncrementalBuilder, Clusters, Runner, RunnerConfig, HIERARCHICAL_MAX};
2+
use visioncortex::{Color, ColorImage, PathSimplifyMode};
3+
use visioncortex::color_clusters::{Clusters, Runner, RunnerConfig, HIERARCHICAL_MAX, IncrementalBuilder, KeyingAction};
44

55
use crate::canvas::*;
66
use crate::svg::*;
77

88
use serde::Deserialize;
99
use super::util;
1010

11+
const KEYING_THRESHOLD: f32 = 0.2;
12+
1113
#[derive(Debug, Deserialize)]
1214
pub struct ColorImageConverterParams {
1315
pub canvas_id: String,
@@ -67,7 +69,26 @@ impl ColorImageConverter {
6769
pub fn init(&mut self) {
6870
let width = self.canvas.width() as u32;
6971
let height = self.canvas.height() as u32;
70-
let image = self.canvas.get_image_data_as_color_image(0, 0, width, height);
72+
let mut image = self.canvas.get_image_data_as_color_image(0, 0, width, height);
73+
74+
let key_color = if Self::should_key_image(&image) {
75+
if let Ok(key_color) = Self::find_unused_color_in_image(&image) {
76+
for y in 0..height as usize {
77+
for x in 0..width as usize {
78+
if image.get_pixel(x, y).a == 0 {
79+
image.set_pixel(x, y, &key_color);
80+
}
81+
}
82+
}
83+
key_color
84+
} else {
85+
Color::default()
86+
}
87+
} else {
88+
// The default color is all zeroes, which is treated by visioncortex as a special value meaning no keying will be applied.
89+
Color::default()
90+
};
91+
7192
let runner = Runner::new(RunnerConfig {
7293
diagonal: self.params.layer_difference == 0,
7394
hierarchical: HIERARCHICAL_MAX,
@@ -78,6 +99,12 @@ impl ColorImageConverter {
7899
is_same_color_b: 1,
79100
deepen_diff: self.params.layer_difference,
80101
hollow_neighbours: 1,
102+
key_color,
103+
keying_action: if self.params.hierarchical == "cutout" {
104+
KeyingAction::Keep
105+
} else {
106+
KeyingAction::Discard
107+
},
81108
}, image);
82109
self.stage = Stage::Clustering(runner.start());
83110
}
@@ -108,6 +135,8 @@ impl ColorImageConverter {
108135
is_same_color_b: 1,
109136
deepen_diff: 0,
110137
hollow_neighbours: 0,
138+
key_color: Default::default(),
139+
keying_action: KeyingAction::Discard,
111140
}, image);
112141
self.stage = Stage::Reclustering(runner.start());
113142
},
@@ -167,4 +196,56 @@ impl ColorImageConverter {
167196
}) as i32
168197
}
169198

199+
fn color_exists_in_image(img: &ColorImage, color: Color) -> bool {
200+
for y in 0..img.height {
201+
for x in 0..img.width {
202+
let pixel_color = img.get_pixel(x, y);
203+
if pixel_color.r == color.r && pixel_color.g == color.g && pixel_color.b == color.b {
204+
return true
205+
}
206+
}
207+
}
208+
false
209+
}
210+
211+
fn find_unused_color_in_image(img: &ColorImage) -> Result<Color, String> {
212+
let special_colors = IntoIterator::into_iter([
213+
Color::new(255, 0, 0),
214+
Color::new(0, 255, 0),
215+
Color::new(0, 0, 255),
216+
Color::new(255, 255, 0),
217+
Color::new(0, 255, 255),
218+
Color::new(255, 0, 255),
219+
Color::new(128, 128, 128),
220+
]);
221+
for color in special_colors {
222+
if !Self::color_exists_in_image(img, color) {
223+
return Ok(color);
224+
}
225+
}
226+
Err(String::from("unable to find unused color in image to use as key"))
227+
}
228+
229+
fn should_key_image(img: &ColorImage) -> bool {
230+
if img.width == 0 || img.height == 0 {
231+
return false;
232+
}
233+
234+
// Check for transparency at several scanlines
235+
let threshold = ((img.width * 2) as f32 * KEYING_THRESHOLD) as usize;
236+
let mut num_transparent_boundary_pixels = 0;
237+
let y_positions = [0, img.height / 4, img.height / 2, 3 * img.height / 4, img.height - 1];
238+
for y in y_positions {
239+
for x in 0..img.width {
240+
if img.get_pixel(x, y).a == 0 {
241+
num_transparent_boundary_pixels += 1;
242+
}
243+
if num_transparent_boundary_pixels >= threshold {
244+
return true;
245+
}
246+
}
247+
}
248+
249+
false
250+
}
170251
}

webapp/src/conversion/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
mod binary_image;
22
mod color_image;
33
mod util;
4-
5-
pub use binary_image::*;
6-
pub use color_image::*;

0 commit comments

Comments
 (0)