added samples and made a mess

This commit is contained in:
Sander Hautvast 2022-03-02 09:10:11 +01:00
parent f8299c7186
commit 586a1e3cf4
1760 changed files with 267 additions and 88 deletions

View file

@ -1,13 +1,14 @@
use gloo_utils::document;
use image::RgbImage;
use wasm_bindgen::{Clamped, JsCast};
use wasm_bindgen::prelude::*;
use web_sys::{DragEvent, HtmlImageElement};
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement, ImageData};
use web_sys::Url;
use yew::{Component, Context, html, Html};
use crate::transform;
use crate::transform::ColorSample;
use crate::{samples, transform};
use crate::samples::{ColorSample, Samples};
pub enum Msg {
Dropped(DragEvent),
@ -16,7 +17,7 @@ pub enum Msg {
}
pub struct DropPhoto {
// color_samples: Vec<ColorSample>
samples: Samples
}
impl Component for DropPhoto {
@ -24,13 +25,12 @@ impl Component for DropPhoto {
type Properties = ();
fn create(_ctx: &Context<Self>) -> Self {
transform::init();
Self {
// color_samples: transform::init().expect("")
samples: Samples::new()
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
fn update(& mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Dragged(event) => {
event.prevent_default();
@ -78,7 +78,7 @@ impl Component for DropPhoto {
.unwrap();
let raw_pixels: Vec<u8> = imgdata.data().to_vec();
let rgb_src = RgbImage::from_raw(canvas.width(), canvas.height(), raw_pixels).unwrap();
// let transformed = transform::apply(rgb_src, &self.color_samples).expect("Cannot transform image");
let transformed = transform::apply(&rgb_src).expect("Cannot transform image");
let image_data = ImageData::new_with_u8_clamped_array_and_sh(Clamped(&rgb_src.to_vec()),
canvas.width(), canvas.height());
@ -101,21 +101,42 @@ impl Component for DropPhoto {
<img id="source-image" style="display:none" onload={link.callback(|_| Msg::ImageLoaded)}/>
<canvas id="source"></canvas>
<canvas id="dest"></canvas>
<div id="samples" class="hidden"></div>
<canvas id="buffer" class="hidden"></canvas>
</>
}
}
}
//make generic for element type
// fn with_element<F>(id: &str, action: F)
// where
// F: Fn(Element),
// {
// if let Some(element) = document()
// .get_element_by_id(id) {
// action(element);
// }
// }
// if the transformer needs a new sample, it uses HtmlImageElement to download it.
// all is async
pub fn add_sample(name: &'static str) {
let sample = document().create_element("img").unwrap().dyn_into::<HtmlImageElement>().expect("Cannot create img element");
sample.set_src(&format!("/static/samples/{}.jpg", name));
let samples = document().get_element_by_id("samples").unwrap();
let image_loaded = Closure::wrap(Box::new( |_: web_sys::Event| {
if let Some(canvas) = document().get_element_by_id("buffer").and_then(|e| e.dyn_into::<HtmlCanvasElement>().ok()) {
let ctx: CanvasRenderingContext2d = canvas
.get_context("2d")
.unwrap()
.unwrap()
.dyn_into::<CanvasRenderingContext2d>()
.unwrap();
canvas.set_width(sample.width());
canvas.set_height(sample.height());
ctx.draw_image_with_html_image_element(&sample, 0.0, 0.0).expect("Cannot draw image on canvas");
let imgdata = ctx
.get_image_data(0.0, 0.0, canvas.width() as f64, canvas.height() as f64)
.unwrap();
let raw_pixels: Vec<u8> = imgdata.data().to_vec();
let sample = RgbImage::from_raw(canvas.width(), canvas.height(), raw_pixels).unwrap();
samples::insert(name.to_owned(), ColorSample::new(&name, sample));
}
}) as Box<dyn FnMut(_)>);
sample.add_event_listener_with_callback("load", image_loaded.as_ref().unchecked_ref()).expect("cannot add onload listener");
samples.append_child(&sample);
}
fn create_element<'a, T>(element_type: &str) -> T
where
@ -126,7 +147,3 @@ fn create_element<'a, T>(element_type: &str) -> T
.dyn_into::<T>()
.expect(&format!("Cannot create element {}", element_type))
}
pub fn _get_image_data(canvas: &HtmlCanvasElement, ctx: &CanvasRenderingContext2d) -> ImageData {
ctx.get_image_data(0.0, 0.0, canvas.width() as f64, canvas.height() as f64).unwrap()
}

View file

@ -1,3 +1,5 @@
extern crate lazy_static;
mod app;
mod transform;
mod quantizer;

189
src/samples.rs Normal file
View file

@ -0,0 +1,189 @@
use std::collections::HashMap;
use std::sync::Mutex;
use image::RgbImage;
use lazy_static::lazy_static;
lazy_static! {
pub static ref SAMPLES: Vec<&'static str> = {
vec!["12110f", "131211", "21201b", "21201d", "212213", "22211b", "23221d", "23221f", "24231d", "252520", "262527", "262628", "272826",
"291f19", "292927", "292a29", "2a2a28", "2a3960", "2a3961", "2b3a5f", "2c2c24", "2c3b64", "2c3c40", "2e2d26", "2e2d28", "2e2d29",
"30301c", "303030", "303131", "303632", "31302b", "313131", "32373a", "32373b", "333a73", "334840", "343a6f", "343b73", "344845",
"353420", "35342f", "353a39", "353b43", "363531", "363c71", "36414c", "364968", "373530", "373a54", "373b46", "373d2f", "373d47",
"373d71", "382f1f", "383732", "383831", "383832", "383a46", "383c4f", "383d48", "384142", "38495a", "384b51", "393b39", "394235",
"3a4042", "3a4242", "3b4435", "3b4a6b", "3b4f6b", "3c3b34", "3c4d5f", "3d342a", "3d4343", "3d4a63", "3e3524", "3e3830", "3e3e32",
"3e3e4a", "3e3f27", "3e4543", "3f3f3a", "3f404f", "3f423b", "3f4b56", "3f527a", "3f5352", "40434b", "404358", "413327", "413d2c",
"413f56", "41403d", "414646", "41494e", "424b4a", "43423c", "43475c", "434943", "434a54", "435969", "44414a", "444933", "444d4e",
"453a2c", "453b2f", "454048", "454534", "45453e", "454c48", "454c4c", "463728", "463e36", "464226", "464540", "464832", "464e4d",
"473e2f", "47443b", "474550", "475141", "475237", "475f6e", "484841", "484846", "484853", "484b43", "484c49", "485162", "49323c",
"493c3b", "494941", "494a3b", "495950", "495c62", "495d6d", "4a4052", "4a4326", "4a4a42", "4a4c3f", "4a4c41", "4a5050", "4a5159",
"4a5f6e", "4b3541", "4b4737", "4b4941", "4b4b44", "4b4b57", "4b4c4b", "4b4d4c", "4b4f41", "4c4b57", "4c4f59", "4c505b", "4d4533",
"4d4b4b", "4d4b58", "4d4d44", "4d4d45", "4d4e47", "4d4f39", "4d545d", "4d5554", "4d5656", "4d616e", "4e3b2e", "4e3d1d", "4e4752",
"4e4f4b", "4e5454", "4f3124", "4f3d29", "4f4735", "4f4b47", "4f4e43", "4f4f4e", "4f5145", "4f514f", "4f584f", "4f5857", "50281e",
"50393a", "504543", "504729", "504f3b", "505052", "505440", "505542", "50574e", "505968", "50656a", "513c2e", "514131", "514636",
"514c39", "515145", "515241", "515762", "515a57", "51646b", "52524a", "525a61", "525c55", "534836", "534a31", "535250", "535959",
"544b3b", "544c3a", "545447", "54544c", "545531", "545650", "545863", "553d28", "55442a", "554936", "554a39", "554e30", "554e31",
"555133", "555556", "555f54", "55698e", "556f73", "56422d", "564635", "565648", "565656", "565843", "565e46", "566448", "566454",
"573823", "57422c", "574642", "574e3e", "575651", "57574f", "57584b", "575d54", "583f2d", "58462c", "584c3c", "58503f", "585348",
"585763", "585a56", "585f69", "594a35", "59594d", "596063", "596068", "596562", "596e6f", "5a4826", "5a4a38", "5a4a3b", "5a552e",
"5a5b4e", "5a5f49", "5a6168", "5a624d", "5a6674", "5b4332", "5b4636", "5b492c", "5b4a22", "5b5237", "5b542f", "5b5b50", "5b626c",
"5b6651", "5b666e", "5c4331", "5c462e", "5c4831", "5c4a37", "5c4f2b", "5c534e", "5c5c5c", "5c5f68", "5c655f", "5c6f70", "5d3c2b",
"5d3c2c", "5d4520", "5d4625", "5d495c", "5d4b31", "5d4b32", "5d4f4a", "5d5539", "5d5644", "5d5d55", "5d5e4f", "5e3d30", "5e3f32",
"5e4625", "5e4c29", "5e4c3a", "5e4d34", "5e504c", "5e685d", "5e693e", "5e6b51", "5f402b", "5f4827", "5f4e28", "5f5443", "5f544f",
"5f5741", "5f5a33", "5f6259", "5f664f", "5f6670", "5f6768", "5f6a4a", "60432b", "60452b", "604931", "60503a", "606a3e", "607c7b",
"614336", "616451", "616839", "616a69", "616d9d", "61786a", "617963", "622f24", "623a26", "623a27", "623e2b", "624035", "62432b",
"62492e", "62502f", "625a3a", "625b45", "626361", "63453b", "636547", "636563", "636744", "63674c", "636a61", "636c51", "638f7c",
"638f7d", "643327", "64391f", "64482c", "644e3b", "644f2e", "645466", "64586c", "645a70", "645e37", "64645b", "646463", "653933",
"654131", "654333", "654438", "65473a", "654f30", "654f42", "655230", "65532e", "655f4e", "656160", "656366", "656559", "656c64",
"656c72", "656c75", "65705a", "663429", "664720", "66513d", "665d50", "665e4c", "666047", "66655e", "667e69", "673d2a", "674154",
"674232", "674737", "674a2c", "674d37", "67514b", "675339", "675a3e", "675d51", "676568", "676b48", "676b5b", "678353", "682626",
"682726", "683326", "683849", "683d2a", "684230", "684635", "684d3f", "68533a", "685340", "68604e", "686148", "68716a", "68726a",
"68807d", "691e2c", "69432f", "694d24", "695857", "696051", "696343", "696960", "696f47", "697c90", "6a1f2d", "6a4029", "6a4125",
"6a4428", "6a4631", "6a493a", "6a4a29", "6a4e21", "6a4e2f", "6a6a62", "6a7360", "6b2231", "6b3a21", "6b4026", "6b523b", "6b5933",
"6b6251", "6b6f5c", "6b705a", "6b7375", "6c312d", "6c3727", "6c372b", "6c392a", "6c412b", "6c4331", "6c4738", "6c5928", "6c5937",
"6c5a37", "6c5b34", "6c5e64", "6c616a", "6c633a", "6c6641", "6c7370", "6c8c6e", "6d4624", "6d4834", "6d492a", "6d4f23", "6d5058",
"6d522d", "6d5732", "6d5a31", "6d5a33", "6d5a43", "6d6d65", "6d6e5f", "6d8759", "6e4028", "6e4624", "6e4a2e", "6e5034", "6e5631",
"6e5b60", "6e635f", "6e6a3b", "6e7251", "6e7776", "6e8a7f", "6f4b39", "6f5233", "6f542f", "6f5830", "6f5936", "6f5958", "6f5d3a",
"6f5e33", "6f6072", "6f7d76", "6f828c", "6f8f71", "70391d", "704022", "704620", "704f27", "705022", "70543b", "705528", "70554f",
"70673e", "70693e", "706b45", "707369", "70796e", "70916b", "71301e", "714728", "714730", "714831", "714a30", "715c2d", "716068",
"716235", "716857", "716a3c", "723932", "72422c", "72492d", "725627", "725733", "725922", "725a22", "725f40", "72603c", "72613a",
"72726a", "73342e", "733834", "733e24", "735544", "73572d", "735b5b", "735e4e", "737365", "73794b", "73795b", "737963", "737f77",
"742e34", "742f32", "74352d", "743d2e", "744833", "744f35", "745045", "745325", "745739", "74573c", "746234", "746264", "746457",
"74705d", "747848", "747d78", "753034", "753d1f", "755d30", "756535", "756a44", "757144", "757365", "757568", "7583a6", "758d72",
"763d34", "764650", "764d31", "765c4e", "766452", "766f52", "767241", "767244", "767251", "76766d", "773a3a", "774320", "774429",
"77452d", "774b34", "775245", "77593a", "775c2f", "77653f", "776541", "777568", "777767", "783620", "783836", "78383b", "78452a",
"78472f", "78482f", "785237", "785433", "785737", "785825", "785f3f", "78612c", "786539", "786879", "787245", "78766b", "787870",
"787e67", "793135", "793620", "79383b", "79404d", "794456", "795b29", "79616b", "796853", "79827e", "7a3638", "7a3c48", "7a432c",
"7a452d", "7a492d", "7a4b2f", "7a5628", "7a5b29", "7a5d24", "7a6950", "7a754a", "7a7e67", "7a8347", "7a8665", "7b3a27", "7b3c48",
"7b3f21", "7b472a", "7b5139", "7b5c2e", "7b5f39", "7b604e", "7b6125", "7b6224", "7b625c", "7b6656", "7b6944", "7b7563", "7b7a6b",
"7b7c6f", "7c3b34", "7c3e2a", "7c402d", "7c452b", "7c4542", "7c4a30", "7c4b2e", "7c5032", "7c5437", "7c5928", "7c5e31", "7c8164",
"7c8379", "7c867f", "7c8c5d", "7d4624", "7d4d34", "7d5645", "7d5926", "7d5928", "7d602e", "7d6134", "7d806b", "7d8577", "7d877c",
"7d877d", "7d8965", "7e4120", "7e472f", "7e4925", "7e4a37", "7e562d", "7e592d", "7e5d28", "7e7266", "7e726e", "7e7763", "7e7764",
"7e7964", "7e887d", "7e8c71", "7e8e52", "7e9b69", "7f401e", "7f4838", "7f4d37", "7f5639", "7f5827", "7f5e60", "7f604d", "7f6156",
"7f633d", "7f6b2e", "7f7347", "7f827a", "7f846a", "803d29", "803f2b", "80403b", "804937", "804b3d", "804e29", "804e2c", "805130",
"805335", "805441", "805644", "805d44", "806051", "806c51", "807a63", "807c53", "807e71", "80855b", "808c68", "813a26", "813e31",
"813f31", "814f30", "815230", "815737", "81583d", "815a32", "815b3d", "816b5b", "81734d", "817b5a", "81826e", "818370", "824432",
"824e2c", "825037", "82552e", "82562e", "825e50", "826130", "826243", "826321", "826d37", "827443", "82846f", "828b5e", "829399",
"833128", "834b31", "835231", "835341", "835747", "835949", "835f56", "836559", "836934", "836e42", "83856c", "838b4d", "84492a",
"844d2b", "844f39", "845028", "845231", "845439", "84644f", "84655a", "847b47", "847e68", "84a162", "84a163", "853022", "85481e",
"854854", "854c58", "855437", "855535", "85553e", "855b1f", "855e2e", "855e51", "856128", "856836", "856b35", "857a66", "85887c",
"859156", "85b0ad", "863d37", "864536", "864a2b", "864c25", "86532e", "865a33", "866345", "866c3b", "866d35", "866e4a", "867348",
"868159", "868550", "868c61", "869e61", "873e4f", "874724", "874e26", "874e39", "87522e", "87523c", "875844", "875b30", "875b3f",
"875c4a", "875d34", "875d41", "876330", "877163", "87744e", "87744f", "877a54", "877d72", "877f6c", "87b1ad", "884834", "884e2c",
"885837", "885c39", "885f41", "886055", "88685d", "88692f", "886a49", "886b31", "886e68", "886f30", "88775d", "887a4a", "887e72",
"888168", "88835b", "888492", "8895a0", "894b36", "895c34", "895e4a", "896156", "896d46", "897232", "89726f", "89785b", "8a4a32",
"8a5933", "8a5a42", "8a5f30", "8a633b", "8a6727", "8a6a2e", "8a7e46", "8a855b", "8a8e65", "8a9355", "8b4d30", "8b5745", "8b5a49",
"8b604f", "8b6c63", "8b773d", "8b7d8e", "8ba784", "8c4d36", "8c552d", "8c5d44", "8c6249", "8c6853", "8c6b34", "8c6c26", "8c6e55",
"8c6f36", "8c7754", "8c7d4d", "8c833b", "8c846e", "8c8568", "8c8762", "8c935c", "8c9a6e", "8d4328", "8d532b", "8d5634", "8d5734",
"8d5937", "8d6349", "8d643c", "8d6535", "8d692e", "8d7262", "8d7539", "8d7760", "8d895a", "8d96a8", "8d978c", "8d9f7f", "8da96c",
"8db29b", "8e4326", "8e4640", "8e4e2f", "8e5635", "8e714f", "8e783a", "8e7e54", "8e866f", "8e8955", "8e8b57", "8e8e4c", "8f4326",
"8f4f2e", "8f542c", "8f5624", "8f5a2d", "8f5c37", "8f6141", "8f653b", "8f6659", "8f7065", "8f735c", "8f7631", "8f783b", "8f8f4b",
"8fa27e", "90442f", "905121", "905229", "90523b", "905337", "905d3e", "906140", "906a41", "907741", "907b69", "908972", "90996b",
"909971", "909c6e", "90aa84", "90ab6f", "913826", "914c27", "914f39", "915352", "915539", "915729", "91603c", "916947", "917849",
"917a35", "91804b", "91967c", "91ac8c", "924c50", "92522e", "925428", "925b36", "925b39", "926649", "92694e", "926a4d", "926b42",
"926d31", "926d32", "927339", "927a51", "927c32", "92834a", "928652", "934753", "934b26", "934b2d", "935826", "935c33", "936f34",
"93724b", "93844d", "939254", "939977", "93a27b", "94533d", "94542f", "945528", "945732", "94573e", "945935", "945940", "94653a",
"947454", "947836", "94894c", "948a62", "94915a", "949872", "949b73", "949c64", "949c68", "949d70", "949f72", "94af8c", "94b294",
"955234", "95532e", "95532f", "95602a", "956a47", "957044", "957a52", "959c79", "959c7b", "964920", "965a37", "96602a", "966449",
"966a46", "966e34", "967758", "967938", "969e70", "96a67f", "975457", "975b29", "976b46", "976c5c", "976e38", "976e50", "977145",
"977332", "978560", "97a472", "97a965", "98472f", "98532d", "985a34", "986547", "986b34", "986c54", "98733a", "987a37", "987b6d",
"988042", "98915b", "989864", "996a38", "996d41", "997054", "997242", "99915e", "999d7e", "99a096", "9a4c2d", "9a5a2e", "9a5a32",
"9a5d2e", "9a642b", "9a661e", "9a7243", "9a7529", "9a772d", "9a7738", "9a7d63", "9a814f", "9a8b94", "9a8c56", "9a8e49", "9a934d",
"9a9c71", "9ab594", "9ab595", "9b4a34", "9b502f", "9b5436", "9b5b2e", "9b5d49", "9b6435", "9b6647", "9b6a38", "9b7139", "9b734a",
"9b7836", "9b7a44", "9b8352", "9b9559", "9c5121", "9c5932", "9c5d58", "9c6625", "9c6d3d", "9c7644", "9c7737", "9c7b5b", "9c7f3d",
"9c8445", "9d3c25", "9d482c", "9d5131", "9d592b", "9d6031", "9d6d38", "9d7451", "9d754c", "9d7e49", "9d803c", "9d8f96", "9d903c",
"9d9149", "9d9686", "9da3ad", "9e5137", "9e531f", "9e552d", "9e595c", "9e666b", "9e7849", "9e795c", "9e7b39", "9e8557", "9e9aa8",
"9e9b67", "9f4e29", "9f5661", "9f5c3c", "9f6025", "9f632e", "9f633b", "9f642c", "9f673b", "9f6c3b", "9f714b", "9f7354", "9f774d",
"9f792f", "9f7e3d", "9f7e6f", "9f804b", "9f876a", "9f9346", "9f9787", "9f9f95", "9fb168", "a04225", "a0462e", "a05227", "a05639",
"a0623a", "a06343", "a07154", "a07244", "a08a43", "a0ad87", "a1442a", "a14729", "a15e29", "a16427", "a16730", "a16739", "a16d30",
"a17034", "a17b4b", "a17e34", "a19258", "a25031", "a26320", "a26e41", "a2742b", "a28359", "a2844a", "a29eab", "a2a15f", "a2aa7d",
"a3432b", "a34a2e", "a35a32", "a35c2a", "a3602a", "a3696e", "a3743a", "a37442", "a3753a", "a37636", "a3773d", "a38f3e", "a39251",
"a39d7c", "a3ab84", "a44e2b", "a45132", "a45b20", "a46b35", "a46c77", "a4734d", "a4772e", "a47949", "a47e4f", "a47f5b", "a48455",
"a48d5f", "a4a16c", "a4ad80", "a55825", "a56130", "a5613b", "a56639", "a56768", "a5693e", "a56a32", "a57042", "a57b3a", "a57c41",
"a5947f", "a66139", "a66827", "a6722c", "a67555", "a67746", "a67a35", "a6814b", "a6823f", "a68630", "a69d54", "a7651c", "a76534",
"a76834", "a76a31", "a77039", "a77459", "a7802e", "a7804e", "a78152", "a78336", "a78a54", "a78d5a", "a78e6c", "a79455", "a79f8d",
"a7a4af", "a84d2d", "a85431", "a8623a", "a8775b", "a88057", "a8825c", "a88544", "a89671", "a94126", "a94c2f", "a95723", "a95923",
"a96d33", "a97a3f", "a97b5b", "a9977e", "a99ba4", "a9a6a9", "aa4226", "aa4a22", "aa4b2e", "aa4d27", "aa5225", "aa6d2c", "aa6e1c",
"aa883c", "aa8a75", "aa9144", "aa9163", "aaa6a7", "ab4a2c", "ab5312", "ab734f", "ab7e3d", "ab8146", "ab8d59", "ab954f", "ab965f",
"ab994a", "ac5918", "ac682a", "ac6a2f", "ac6b72", "ac795c", "ac7a47", "ac7b5c", "ac8558", "ac8943", "ac8a44", "ac9459", "ad5e27",
"ad6b21", "ad6e35", "ad7028", "ad793b", "ad7d3b", "ad8037", "ad8c3b", "ad8c61", "ad924f", "ada874", "adb588", "adc4a9", "ae5032",
"ae7024", "ae7945", "ae812e", "ae8241", "ae8654", "ae8667", "ae895b", "ae8b5c", "ae8c36", "ae943c", "ae9546", "af7734", "af8860",
"af9747", "af9a62", "af9d7f", "afb486", "afba86", "b04d2c", "b0883e", "b08e45", "b08f53", "b09050", "b0913a", "b0a64a", "b0ba8d",
"b1513a", "b15732", "b1682d", "b16a2b", "b18540", "b18f5e", "b19458", "b19955", "b19c5c", "b1a55d", "b1bf9a", "b1c4a6", "b2543f",
"b25528", "b26e43", "b2713a", "b27223", "b2731f", "b27329", "b27536", "b27836", "b27b28", "b27f27", "b28429", "b28738", "b28741",
"b2894c", "b28f41", "b2944f", "b29b50", "b29c52", "b2a36f", "b2babb", "b2c199", "b34b30", "b37229", "b37944", "b37a47", "b38840",
"b3a48c", "b46e1c", "b47149", "b4752a", "b4802f", "b4833c", "b49446", "b49a67", "b49e50", "b49f5c", "b4a04c", "b58a3c", "b58c44",
"b58d6d", "b58e47", "b59b53", "b59d56", "b5a05c", "b5b7b4", "b5c6ad", "b6612e", "b68431", "b68b64", "b68f44", "b69642", "b6974a",
"b6a56b", "b6b689", "b75129", "b7512e", "b77434", "b7752d", "b78f51", "b79454", "b7975b", "b79939", "b79c53", "b7a04f", "b7a256",
"b85132", "b87e42", "b8834a", "b89169", "b89170", "b89248", "b89542", "b89a4c", "b89b5a", "b9581a", "b95d38", "b96c30", "b98138",
"b9874d", "b9916a", "b99450",
"b99642", "b99843", "b99b4c", "b9a783", "b9a890", "b9bd97", "b9bf95", "ba832c", "ba8a4b", "ba9a5a", "baa053", "bac8af", "bb7d34",
"bb8c6a", "bb9235", "bb923d", "bb9b48", "bb9c4d", "bba14d", "bba25d", "bba362", "bba557", "bbab6a", "bc6d23", "bc6f36", "bc7f4f",
"bc867e", "bc8e47", "bc9241", "bc9777", "bc9878", "bca967", "bcb9be", "bcc29c", "bd8443", "bd8c46", "bd902b", "bda452", "bdab84",
"be5531", "be7330", "be7a2f", "be7e28", "be8355", "be872d", "be8829", "be9876", "be9e48", "bea043", "bea451", "beaa79", "beb98a",
"bec298", "becbb2", "bf6d24", "bf8853", "bf8d32", "bf9871", "bf9a6d", "bf9e3f", "bfa065", "bfa383", "bfa862", "bfaa59", "c05d3e",
"c0754c", "c07a44", "c07c39", "c07d4f", "c0862f", "c08b2e", "c09198", "c0949d", "c09735", "c09739", "c09e4a", "c0a751", "c0ab58",
"c17e32", "c18b59", "c18e5e", "c19e35", "c1a54d", "c1a873", "c1ac5c", "c28321", "c29231", "c29f3d", "c29f5c", "c2a451", "c2cfb5",
"c35b3c", "c37f47", "c3882f", "c38b2a", "c39368", "c39585", "c39a40", "c3a544", "c3a57a", "c3a64d", "c3a862", "c3b381", "c47b1c",
"c4a249", "c4a75c", "c4a84d", "c4a986", "c4ac78", "c5674d", "c58b35", "c58c2c", "c5a33f", "c5a956", "c5b177", "c5b697", "c5c776",
"c67634", "c6b686", "c76e4a", "c78a59", "c78b24", "c78c23", "c78e2d", "c7a84b", "c7b683", "c86646", "c8751b", "c8b26b", "c8b468",
"c8b58f", "c96e2c", "c96e33", "c98e34", "c98f2d", "c9953f", "c9a955", "c9b35f", "c9c495", "ca8d59", "ca8f49", "ca943f", "caa863",
"caaf62", "cab978", "cab98c", "cab997", "cac1c3", "cad3bb", "cb8427", "cb8727", "cb8b2c", "cb9138", "cbad8a", "cbbc83", "cbbc8b",
"cbd5bd", "cc933b", "ccb084", "ccb486", "ccbc72", "ccd2b8", "cd8926", "cd8b5a", "cd8d27", "cd912f", "cd9e41", "cda24b", "cdac5e",
"cdac81", "cdaf4d", "cdb65c", "cdb777", "cdbe77", "cdd2b8", "ce7837", "ce783d", "ce8826", "ce9150", "cebe70", "cf781b", "cf7836",
"cf922e", "cfae3c", "cfb376", "cfbf83", "d07a39", "d08a56", "d0ae49", "d0b340", "d0b98b", "d0ba62", "d19d70", "d1a849", "d1ab4d",
"d1ad50", "d1b14a", "d1b577", "d1b86c", "d1c384", "d29c48", "d2a230", "d2ac4d", "d2b88a", "d2c38c", "d3791b", "d4895f", "d49b62",
"d4af4f", "d4bf71", "d4c779", "d57a5b", "d5a376", "d5aa57", "d5b877", "d5bc6f", "d5c09f", "d5c170", "d5c678", "d5c793", "d69649",
"d69b72", "d6b664", "d6c3a9", "d7b647", "d7c189", "d7c797", "d8a677", "d8ba4f", "d8c27e", "d8c991", "d8caa9", "d9a268", "d9b849",
"d9cb9a", "d9cc65", "daa456", "dab948", "dabb4b", "dac66f", "dac8a4", "daca75", "dacc9a", "dace67", "db9a7c", "dbba4c", "dbca80",
"dbce69", "dbd07c", "dcbf6f", "dcc267", "dcd068", "ddb784", "ddc663", "ddd069", "dea94f", "decfa8", "ded18c", "ded1a5", "ded298",
"e0c980", "e0d2a5", "e19846", "e1b384", "e1cb56", "e1d3a2", "e1d487", "e2cc6d", "e2d786", "e2d8b6", "e3d162", "e3d4bd", "e3d6ae",
"e4a14a", "e4d7a6", "e4d97a", "e5d6a1", "e5d7b2", "e5d89d", "e5d8b1", "e6af8c", "e6cf6b", "e6d888", "e6d988", "e6d9b1", "e7c782",
"e7d9a6", "e7db7e", "e7dfc8", "e8ae8a", "e8dab3", "e8db90", "e8ddba", "e8de98", "e9cfa9", "e9de97", "e9e488", "eac551", "eae27f",
"eae2c2", "eae37f", "ebc851", "ebcc88", "ebd3ad", "ebe083", "ebe688", "ecc69d", "ece2bb", "ece2d0", "edd75e", "edde8f", "ede0a9",
"ede4a5", "ede4d0", "ede4d2", "ede5b8", "edea91", "edea92", "eee6d1", "efe2b2", "f0e5bc", "f0e788"]
};
}
pub struct Samples {
color_samples: HashMap<String, ColorSample>, //cache
}
impl Samples {
pub fn new() -> Self {
Self { color_samples: HashMap::new() }
}
}
pub fn contains(name: &str) -> bool {
SAMPLE_CACHE.lock().unwrap().color_samples.contains_key(name)
}
pub fn get(name: &str) -> Option<&ColorSample> {
let handle = SAMPLE_CACHE.lock().unwrap();
handle.color_samples.get(name)
}
pub fn insert(name: String, sample: ColorSample) {
SAMPLE_CACHE.lock().unwrap().color_samples.insert(name, sample);
}
pub struct ColorSample {
pub r: u8,
pub g: u8,
pub b: u8,
pub image: RgbImage,
}
impl ColorSample {
pub fn new(rgb: &str, image: RgbImage) -> Self {
let r = u8::from_str_radix(&rgb[0..2], 16).unwrap();
let g = u8::from_str_radix(&rgb[2..4], 16).unwrap();
let b = u8::from_str_radix(&rgb[4..6], 16).unwrap();
Self { r, g, b, image }
}
}
lazy_static! {
static ref SAMPLE_CACHE: Mutex<Samples> = Mutex::new(Samples::new());
}

View file

@ -1,38 +1,30 @@
use std::{error::Error, fs, result::Result};
use std::collections::HashMap;
use std::{error::Error, result::Result};
use image::{GenericImageView, ImageBuffer, Pixel, Rgb, RgbImage};
use imageproc::point::Point;
use crate::{quantizer, samples};
use crate::samples::SAMPLES;
use crate::samples::ColorSample;
struct Transformer {
color_samples: HashMap<String, ColorSample>, //cache
}
impl Transformer {
fn new() -> Self {
Self { color_samples: HashMap::new() }
}
pub fn apply(&mut self, src: RgbImage, color_samples: &mut HashMap<String, ColorSample>) -> Result<RgbImage, Box<dyn Error>> {
pub fn apply(src: &RgbImage) -> Result<RgbImage, Box<dyn Error>> {
let gauss = imageproc::filter::gaussian_blur_f32(&src, 2.0);
let median = imageproc::filter::median_filter(&gauss, 2, 2);
let quantized = quantizer::quantize(&median, 256);
let out = apply_samples_to_image(quantized, color_samples);
let out = apply_samples_to_image(quantized);
Ok(out)
}
}
fn apply_samples_to_image(mut src: RgbImage, color_samples: &mut HashMap<String, ColorSample>) -> RgbImage {
fn apply_samples_to_image(mut src: RgbImage) -> RgbImage {
let mut imgbuf = RgbImage::new(src.width(), src.height());
unsafe {
for y in 0..src.height() {
for x in 0..src.width() {
let pixel = &src.unsafe_get_pixel(x, y);
if imgbuf.unsafe_get_pixel(x, y).channels() == [0, 0, 0] {
if let Some(sample) = get_closest(olor_samples, pixel) {
if let Some(sample) = get_closest(pixel) {
fill(&mut src, sample, &mut imgbuf, pixel, x, y);
}
}
@ -107,28 +99,31 @@ fn is_same(p1: &Rgb<u8>, p2: &Rgb<u8>) -> bool {
}
fn get_closest(
color_samples: &mut HashMap<String, ColorSample>,
pixel: &Rgb<u8>,
) -> Option<&ColorSample> {
) -> Option<&'static ColorSample> {
let mut closest = None;
let mut min_diff: f32 = 4294967295.0; //0xFFFFFFFF
for sample in samples::SAMPLES {
let r = hex::decode(sample[0..2], ).unwrap()[0];
let g = hex::decode(sample[2..4], ).unwrap()[0];
let b = hex::decode(sample[4..6], ).unwrap()[0];
let diff = get_distance(r,g,b, pixel);
for sample in SAMPLES.iter() {
let r = hex::decode(&sample[0..2]).unwrap()[0];
let g = hex::decode(&sample[2..4]).unwrap()[0];
let b = hex::decode(&sample[4..6]).unwrap()[0];
let diff = get_distance(r, g, b, pixel);
if diff < min_diff {
closest = Some(sample);
min_diff = diff;
}
}
if color_samples.contains(&closest){
return color_samples.get(&closest);
if let Some(closest) = closest {
if samples::contains(closest) {
return samples::get(closest);
} else {
//download image
let image = ColorSample{r,g,b, image: RgbImage::from_raw(0,0,vec![]).unwrap()};
color_samples.insert(sample.to_owned(), image);
crate::app::add_sample( closest);
// wait for image to ownload
while !samples::contains(closest) {
//...
}
return samples::get(closest);
}
}
None
}
@ -140,43 +135,13 @@ fn get_distance(r: u8, g: u8, b: u8, c2: &Rgb<u8>) -> f32 {
return f32::sqrt(red_dif * red_dif + green_dif * green_dif + blue_dif * blue_dif);
}
// fn read_color_samples() -> Result<Vec<ColorSample>, Box<dyn Error>> {
// let paths = fs::read_dir("samples")?;
// let mut color_samples: Vec<ColorSample> = Vec::new();
// for path in paths {
// let path = path?.path();
// let filename = path.to_str().unwrap().to_owned();
//
// if filename.ends_with(".jpg") {
// let sample_image: RgbImage = image::open(&filename).unwrap().into_rgb8();
// let hex_r = &filename[8..10];
// let hex_g = &filename[10..12];
// let hex_b = &filename[12..14];
// color_samples.push(ColorSample {
// r: u8::from_str_radix(&hex_r, 16).unwrap(),
// g: u8::from_str_radix(&hex_g, 16).unwrap(),
// b: u8::from_str_radix(&hex_b, 16).unwrap(),
// image: sample_image,
// });
// }
// }
// Ok(color_samples)
// }
pub struct ColorSample {
r: u8,
g: u8,
b: u8,
image: RgbImage,
}
impl PartialEq for ColorSample{
impl PartialEq for ColorSample {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
self.r == other.r && self.g == other.g && self.b == other.b
}
fn ne(&self, other: &Self) -> bool {
self.name != other.name
!self.eq(other)
}
}
@ -216,6 +181,7 @@ struct Node {
next: Option<Box<Node>>,
}
#[cfg(test)]
mod test {
use super::*;
@ -230,3 +196,4 @@ mod test {
assert_eq!(1, list.pop().unwrap().x);
}
}

View file

@ -25,3 +25,7 @@ p {
width: 25%;
border: rgb(17, 122, 184) solid 3px;
}
.hidden{
display: none;
}

BIN
static/samples/12110f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
static/samples/131211.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
static/samples/21201b.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
static/samples/21201d.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
static/samples/212213.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
static/samples/22211b.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
static/samples/23221d.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
static/samples/23221f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
static/samples/24231d.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
static/samples/252520.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
static/samples/262527.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
static/samples/262628.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
static/samples/272826.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/291f19.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
static/samples/292927.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
static/samples/292a29.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
static/samples/2a2a28.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
static/samples/2a3960.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

BIN
static/samples/2a3961.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
static/samples/2b3a5f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
static/samples/2c2c24.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
static/samples/2c3b64.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
static/samples/2c3c40.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
static/samples/2e2d26.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
static/samples/2e2d28.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
static/samples/2e2d29.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

BIN
static/samples/30301c.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
static/samples/303030.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
static/samples/303131.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
static/samples/303632.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
static/samples/31302b.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
static/samples/313131.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
static/samples/32373a.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
static/samples/32373b.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
static/samples/333a73.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
static/samples/334840.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/343a6f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
static/samples/343b73.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
static/samples/344845.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
static/samples/353420.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
static/samples/35342f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

BIN
static/samples/353a39.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
static/samples/353b43.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

BIN
static/samples/363531.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
static/samples/363c71.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

BIN
static/samples/36414c.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/364968.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/373530.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
static/samples/373a54.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/373b46.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
static/samples/373d2f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/373d47.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/373d71.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
static/samples/382f1f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
static/samples/383732.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
static/samples/383831.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

BIN
static/samples/383832.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
static/samples/383a46.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
static/samples/383c4f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
static/samples/383d48.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/384142.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
static/samples/38495a.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
static/samples/384b51.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
static/samples/393b39.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
static/samples/394235.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
static/samples/3a4042.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
static/samples/3a4242.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
static/samples/3b4435.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
static/samples/3b4a6b.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/3b4f6b.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/3c3b34.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
static/samples/3c4d5f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
static/samples/3d342a.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

BIN
static/samples/3d4343.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
static/samples/3d4a63.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/3e3524.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/3e3830.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
static/samples/3e3e32.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/3e3e4a.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
static/samples/3e3f27.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

BIN
static/samples/3e4543.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
static/samples/3f3f3a.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

BIN
static/samples/3f404f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/3f423b.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
static/samples/3f4b56.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
static/samples/3f527a.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
static/samples/3f5352.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
static/samples/40434b.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
static/samples/404358.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

BIN
static/samples/413327.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
static/samples/413d2c.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
static/samples/413f56.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
static/samples/41403d.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

BIN
static/samples/414646.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
static/samples/41494e.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Some files were not shown because too many files have changed in this diff Show more