Skip to content

Commit

Permalink
Refactor db
Browse files Browse the repository at this point in the history
  • Loading branch information
twitu committed Apr 18, 2024
1 parent a73fa8f commit 8c469f9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
42 changes: 16 additions & 26 deletions spritefire/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Fxd = FixedU8<U0>;
#[derive(Serialize, Deserialize, Debug)]
pub struct EmojiDatabase {
kdtree: KdTree<Fxd, u32, 3, 32, u32>,
symbols: Vec<(u64, String)>,
symbols: Vec<Emoji>,
}

impl EmojiDatabase {
Expand All @@ -29,33 +29,20 @@ impl EmojiDatabase {
}

pub fn from_emojis(emojis: Vec<Emoji>) -> Self {
let (symbols, colors): (Vec<_>, Vec<_>) = emojis
.into_iter()
.map(
|Emoji {
symbol,
color,
density: transparent,
}| {
let [r, g, b] = color;
(
(transparent, symbol),
[
FixedU8::from_num(r),
FixedU8::from_num(g),
FixedU8::from_num(b),
],
)
},
)
.unzip();

let mut kdtree = KdTree::new();
colors.into_iter().enumerate().for_each(|(i, color)| {
emojis.iter().enumerate().for_each(|(i, emoji)| {
let color = [
FixedU8::from_num(emoji.color[0]),
FixedU8::from_num(emoji.color[1]),
FixedU8::from_num(emoji.color[2]),
];
kdtree.add(&color, i as u32);
});

Self { symbols, kdtree }
Self {
symbols: emojis,
kdtree,
}
}

pub fn lookup_closest_dense_emoji(&self, rgb: Rgb<u8>) -> &str {
Expand All @@ -68,7 +55,10 @@ impl EmojiDatabase {
let nearest = self.kdtree.nearest_n::<SquaredEuclidean>(&point, 3);
let (_, symbol) = nearest
.iter()
.map(|item| &self.symbols[item.item as usize])
.map(|item| {
let emoji = &self.symbols[item.item as usize];
(emoji.density, &emoji.symbol)
})
.max()
.unwrap();
symbol
Expand All @@ -82,7 +72,7 @@ impl EmojiDatabase {
];

let index: usize = self.kdtree.nearest_one::<SquaredEuclidean>(&point).item as usize;
&self.symbols[index].1
&self.symbols[index].symbol
}

pub fn new_from_directory(dir_path: PathBuf) -> Self {
Expand Down
3 changes: 2 additions & 1 deletion spritefire/src/emoji.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use image::{self, DynamicImage, GenericImageView};
use serde::{Deserialize, Serialize};

/// Emoji symbol with the average color of its picture
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Emoji {
/// Unicode symbol for the emoji
pub symbol: String,
Expand Down

0 comments on commit 8c469f9

Please sign in to comment.