ugh
This commit is contained in:
16
Cargo.lock
generated
16
Cargo.lock
generated
@ -172,6 +172,17 @@ version = "1.1.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
|
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "auto-palette"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65e79afc54a4a4629411811a644e778c7d8f6889b8ecf4c1d4a13f3f33db408d"
|
||||||
|
dependencies = [
|
||||||
|
"image",
|
||||||
|
"num-traits",
|
||||||
|
"thiserror 2.0.12",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
@ -387,9 +398,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.2.22"
|
version = "1.2.23"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "32db95edf998450acc7881c932f94cd9b05c87b4b2599e8bab064753da4acfd1"
|
checksum = "5f4ac86a9e5bc1e2b3449ab9d7d3a6a405e3d1bb28d7b9be8614f55846ae3766"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"jobserver",
|
"jobserver",
|
||||||
"libc",
|
"libc",
|
||||||
@ -4053,6 +4064,7 @@ name = "watcat"
|
|||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arabic_reshaper",
|
"arabic_reshaper",
|
||||||
|
"auto-palette",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
"http-cache-reqwest",
|
"http-cache-reqwest",
|
||||||
"image",
|
"image",
|
||||||
|
|||||||
@ -12,6 +12,9 @@ reqwest-middleware = "0.4.2"
|
|||||||
http-cache-reqwest = "0.15.1"
|
http-cache-reqwest = "0.15.1"
|
||||||
palette = "0.7.6"
|
palette = "0.7.6"
|
||||||
tiny-skia = "0.11.4"
|
tiny-skia = "0.11.4"
|
||||||
|
auto-palette = "0.8.2"
|
||||||
|
#rand = "0.9.1"
|
||||||
|
#rayon = "1.10.0"
|
||||||
# text rendering dependencies
|
# text rendering dependencies
|
||||||
arabic_reshaper = "0.4.2"
|
arabic_reshaper = "0.4.2"
|
||||||
unicode-bidi = "0.3.18"
|
unicode-bidi = "0.3.18"
|
||||||
|
|||||||
59
src/img.rs
59
src/img.rs
@ -1,7 +1,15 @@
|
|||||||
|
|
||||||
use image::{RgbaImage, ExtendedColorType, ImageEncoder, load_from_memory};
|
use image::{RgbaImage, ExtendedColorType, ImageEncoder, load_from_memory};
|
||||||
use image::codecs::png::PngEncoder;
|
use image::codecs::png::PngEncoder;
|
||||||
use tiny_skia::{Pixmap, Transform, PixmapPaint, BlendMode, FilterQuality};
|
use tiny_skia::{Pixmap, Transform, PixmapPaint, BlendMode, FilterQuality, Color, Paint, Shader};
|
||||||
|
use palette::{Srgb, Lab, FromColor};
|
||||||
|
use auto_palette::{ImageData, Palette};
|
||||||
|
/*use palette::color_difference::{EuclideanDistance, ImprovedDeltaE};
|
||||||
|
use palette::white_point::D65;
|
||||||
|
use rand::{SeedableRng};
|
||||||
|
use rand::prelude::{IteratorRandom};
|
||||||
|
use rand::rngs::{SmallRng};
|
||||||
|
use std::ops::{Add, Div};//{Add, Sub, Mul};*/
|
||||||
|
|
||||||
pub fn save_to_memory_png(p: &RgbaImage) -> Result<Vec<u8>, String> {
|
pub fn save_to_memory_png(p: &RgbaImage) -> Result<Vec<u8>, String> {
|
||||||
let (width, height) = p.dimensions();
|
let (width, height) = p.dimensions();
|
||||||
@ -34,3 +42,52 @@ pub fn scale_skia_pixmap(p: Pixmap, width: u32, height: u32) -> Option<Pixmap> {
|
|||||||
new_pixmap.draw_pixmap(0, 0, p.as_ref(), &pixmap_paint, matrix, None);
|
new_pixmap.draw_pixmap(0, 0, p.as_ref(), &pixmap_paint, matrix, None);
|
||||||
Some(new_pixmap)
|
Some(new_pixmap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn lab_to_skia_color(c: Lab) -> Result<Color, String> {
|
||||||
|
let rgb: Srgb<f32> = Srgb::from_color(c).into_format();
|
||||||
|
match Color::from_rgba(rgb.red, rgb.green, rgb.blue, 1.0) {
|
||||||
|
Some(c) => Ok(c),
|
||||||
|
None => Err(format!("Invalid color: {rgb:?}")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn color_paint_from_lab_color<'a>(c: Lab) -> Result<Paint<'a>, String> {
|
||||||
|
//let color = lab_to_skia_color(c)?;
|
||||||
|
Ok(Paint {
|
||||||
|
shader: Shader::SolidColor(lab_to_skia_color(c)?),
|
||||||
|
anti_alias: true,
|
||||||
|
blend_mode: BlendMode::Source,
|
||||||
|
force_hq_pipeline: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dominant_colors_as_paints<'a>(p: &Pixmap) -> Result<(Paint<'a>, Paint<'a>), String> {
|
||||||
|
let imagedata = match ImageData::new(p.width(), p.height(), p.data()) {
|
||||||
|
Ok(i) => i,
|
||||||
|
Err(_) => return Err("Failed to load imagedata".to_string()),
|
||||||
|
};
|
||||||
|
let pal: Palette<f32> = match Palette::extract(&imagedata) {
|
||||||
|
Ok(p) => p,
|
||||||
|
Err(_) => return Err("Failed to to create palette".to_string()),
|
||||||
|
};
|
||||||
|
let colors: Vec<_> = match pal.find_swatches(2) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(_) => return Err("Failed to get swatches".to_string()),
|
||||||
|
};
|
||||||
|
let colors = colors.iter().map(|swatch| {
|
||||||
|
let rgb = swatch.color().to_rgb();
|
||||||
|
Srgb::new(rgb.r, rgb.g, rgb.b).into()
|
||||||
|
}).collect::<Vec<Srgb<f32>>>();
|
||||||
|
Ok((Paint {
|
||||||
|
shader: Shader::SolidColor(Color::from_rgba(colors[0].red, colors[0].green, colors[0].blue, 1.0).unwrap()),
|
||||||
|
anti_alias: true,
|
||||||
|
blend_mode: BlendMode::Source,
|
||||||
|
force_hq_pipeline: false,
|
||||||
|
},
|
||||||
|
Paint {
|
||||||
|
shader: Shader::SolidColor(Color::from_rgba(colors[1].red, colors[1].green, colors[1].blue, 1.0).unwrap()),
|
||||||
|
anti_alias: true,
|
||||||
|
blend_mode: BlendMode::Source,
|
||||||
|
force_hq_pipeline: false,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|||||||
39
src/main.rs
39
src/main.rs
@ -1,7 +1,7 @@
|
|||||||
#![allow(dead_code)]
|
//#![allow(dead_code)]
|
||||||
#![allow(unused_variables)]
|
//#![allow(unused_variables)]
|
||||||
#![allow(unused_imports)]
|
//#![allow(unused_imports)]
|
||||||
#![allow(unused_macros)]
|
//#![allow(unused_macros)]
|
||||||
extern crate dotenvy;
|
extern crate dotenvy;
|
||||||
extern crate lastfm;
|
extern crate lastfm;
|
||||||
extern crate reqwest;
|
extern crate reqwest;
|
||||||
@ -23,9 +23,9 @@ extern crate image;
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::sync::Once;
|
use std::sync::Once;
|
||||||
|
|
||||||
use palette::color_difference::ImprovedCiede2000;
|
//use palette::color_difference::ImprovedCiede2000;
|
||||||
use palette::lab::Lab;
|
//use palette::lab::Lab;
|
||||||
use palette::white_point::D65;
|
//use palette::white_point::D65;
|
||||||
|
|
||||||
extern crate sqlx;
|
extern crate sqlx;
|
||||||
use sqlx::sqlite::SqlitePool;
|
use sqlx::sqlite::SqlitePool;
|
||||||
@ -89,6 +89,7 @@ macro_rules! handle_option {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// this function is almost assuredly the least idiomatic rust code I will ever write.
|
// this function is almost assuredly the least idiomatic rust code I will ever write.
|
||||||
// but until I know what the "correct" way is, it'll have to do
|
// but until I know what the "correct" way is, it'll have to do
|
||||||
fn parse_cielab(s: String) -> Result<(f32, f32, f32), &'static str> {
|
fn parse_cielab(s: String) -> Result<(f32, f32, f32), &'static str> {
|
||||||
@ -109,7 +110,7 @@ fn parse_cielab(s: String) -> Result<(f32, f32, f32), &'static str> {
|
|||||||
&[a, b, c] => Ok((a, b, c)),
|
&[a, b, c] => Ok((a, b, c)),
|
||||||
_ => Err("Error parsing cielab string"),
|
_ => Err("Error parsing cielab string"),
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/*fn validate_color(col: &PixelWand) -> Option<Lab> {
|
/*fn validate_color(col: &PixelWand) -> Option<Lab> {
|
||||||
let color_str = match col.get_color_as_string() {
|
let color_str = match col.get_color_as_string() {
|
||||||
@ -143,6 +144,7 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|||||||
Some(track) => track,
|
Some(track) => track,
|
||||||
None => return Reply::Text("Nothing playing.".to_string()),
|
None => return Reply::Text("Nothing playing.".to_string()),
|
||||||
};
|
};
|
||||||
|
#[allow(unused_variables)]
|
||||||
let track_info = text::TrackInfo {
|
let track_info = text::TrackInfo {
|
||||||
title: track.name,
|
title: track.name,
|
||||||
artist: track.artist.name,
|
artist: track.artist.name,
|
||||||
@ -172,13 +174,20 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|||||||
let mut main_image = handle_option!(tiny_skia::Pixmap::new(FMI_WIDTH, FMI_HEIGHT), "Failed to load main image");
|
let mut main_image = handle_option!(tiny_skia::Pixmap::new(FMI_WIDTH, FMI_HEIGHT), "Failed to load main image");
|
||||||
let mut album_image = handle_result!(tiny_skia::Pixmap::decode_png(&image_base), "Failed to load album art image");
|
let mut album_image = handle_result!(tiny_skia::Pixmap::decode_png(&image_base), "Failed to load album art image");
|
||||||
//let album_image = handle_result!(tiny_skia::Pixmap::decode_png(&image_rgba8), "Failed to decode album art");
|
//let album_image = handle_result!(tiny_skia::Pixmap::decode_png(&image_rgba8), "Failed to decode album art");
|
||||||
let color_paint = tiny_skia::Paint {
|
#[allow(unused_variables)]
|
||||||
shader: tiny_skia::Shader::SolidColor(tiny_skia::Color::WHITE),
|
let (dom, sub) = match img::dominant_colors_as_paints(&album_image) {
|
||||||
anti_alias: true,
|
Ok(c) => c,
|
||||||
blend_mode: tiny_skia::BlendMode::Source,
|
Err(e) => return Reply::Text(e),
|
||||||
force_hq_pipeline: false,
|
|
||||||
};
|
};
|
||||||
main_image.fill_rect(tiny_skia::Rect::from_xywh(0.0, 0.0, FMI_WIDTH as f32, FMI_HEIGHT as f32).unwrap(), &color_paint, tiny_skia::Transform::identity(), None);
|
//let background_paint = handle_result!(img::color_paint_from_lab_color(dom), "Failed to create color paint");
|
||||||
|
main_image.fill_rect(tiny_skia::Rect::from_xywh(0.0, 0.0, FMI_WIDTH as f32, FMI_HEIGHT as f32).unwrap(), &dom, tiny_skia::Transform::identity(), None);
|
||||||
|
let mut pb = tiny_skia::PathBuilder::new();
|
||||||
|
pb.move_to(FMI_WIDTH as f32, 0.0);
|
||||||
|
pb.line_to((FMI_WIDTH - FMI_HEIGHT) as f32, FMI_HEIGHT as f32);
|
||||||
|
pb.line_to(FMI_WIDTH as f32, FMI_HEIGHT as f32);
|
||||||
|
pb.close();
|
||||||
|
let path = pb.finish().unwrap();
|
||||||
|
main_image.fill_path(&path, &sub, tiny_skia::FillRule::Winding, tiny_skia::Transform::identity(), None);
|
||||||
let art_size = FMI_HEIGHT- 2 * FMI_GAP;
|
let art_size = FMI_HEIGHT- 2 * FMI_GAP;
|
||||||
album_image = handle_option!(img::scale_skia_pixmap(album_image, art_size, art_size), "Failed to scale bitmap");
|
album_image = handle_option!(img::scale_skia_pixmap(album_image, art_size, art_size), "Failed to scale bitmap");
|
||||||
//let art_scale_matrix = tiny_skia::Transform::from_transla(art_size, art_size).pre_translate(FMI_GAP as f32, FMI_GAP as f32);
|
//let art_scale_matrix = tiny_skia::Transform::from_transla(art_size, art_size).pre_translate(FMI_GAP as f32, FMI_GAP as f32);
|
||||||
@ -294,7 +303,7 @@ async fn fmi(ctx: &Context, arg: &str, id: UserId, avatar: Option<String>) -> Re
|
|||||||
Err(e) => return Reply::Text(e.to_string()),
|
Err(e) => return Reply::Text(e.to_string()),
|
||||||
};
|
};
|
||||||
let mut avatar_image = handle_result!(tiny_skia::Pixmap::decode_png(&avatar_base), "Failed to load album art image");
|
let mut avatar_image = handle_result!(tiny_skia::Pixmap::decode_png(&avatar_base), "Failed to load album art image");
|
||||||
let mut avatar_mask = handle_result!(tiny_skia::Mask::decode_png(include_bytes!("avatar_mask.png")), "Failed to load album art image");
|
let avatar_mask = handle_result!(tiny_skia::Mask::decode_png(include_bytes!("avatar_mask.png")), "Failed to load album art image");
|
||||||
let (avatar_x, avatar_y) = (FMI_WIDTH - FMI_AVATAR_SIZE - FMI_AVATAR_GAP, FMI_HEIGHT - FMI_AVATAR_SIZE - FMI_AVATAR_GAP);
|
let (avatar_x, avatar_y) = (FMI_WIDTH - FMI_AVATAR_SIZE - FMI_AVATAR_GAP, FMI_HEIGHT - FMI_AVATAR_SIZE - FMI_AVATAR_GAP);
|
||||||
avatar_image.apply_mask(&avatar_mask);
|
avatar_image.apply_mask(&avatar_mask);
|
||||||
avatar_image = handle_option!(img::scale_skia_pixmap(avatar_image, FMI_AVATAR_SIZE, FMI_AVATAR_SIZE), "Failed to scale avatar image");
|
avatar_image = handle_option!(img::scale_skia_pixmap(avatar_image, FMI_AVATAR_SIZE, FMI_AVATAR_SIZE), "Failed to scale avatar image");
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
/*
|
||||||
// this code is mostly just blindly ported from
|
// this code is mostly just blindly ported from
|
||||||
// https://github.com/adamanldo/Cosmo/blob/946af25dcac8a81cb7b49d1a4f83731a5f7c7b46/cogs/utils/fmi_text.py
|
// https://github.com/adamanldo/Cosmo/blob/946af25dcac8a81cb7b49d1a4f83731a5f7c7b46/cogs/utils/fmi_text.py
|
||||||
// since I'm using the same text rendering library, my code is almost one-to-one
|
// since I'm using the same text rendering library, my code is almost one-to-one
|
||||||
@ -101,18 +102,20 @@ fn draw_info(image: &mut RgbaImage, text: String, text_type: TextField, font: &S
|
|||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(e) => Err(e.to_string()),
|
Err(e) => Err(e.to_string()),
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct TrackInfo {
|
pub struct TrackInfo {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub artist: String,
|
pub artist: String,
|
||||||
pub album: String,
|
pub album: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fmi_text(width: u32, height: u32, track: TrackInfo, fonts: &(SuperFont<'static>, SuperFont<'static>), font_size: f32, dark_text: bool) -> Result<Vec<u8>, String> {
|
/*pub fn fmi_text(width: u32, height: u32, track: TrackInfo, fonts: &(SuperFont<'static>, SuperFont<'static>), font_size: f32, dark_text: bool) -> Result<Vec<u8>, String> {
|
||||||
let mut img = RgbaImage::new(width, height);
|
let mut img = RgbaImage::new(width, height);
|
||||||
draw_info(&mut img, track.title, TextField::Title, &fonts.1.clone(), font_size, dark_text)?;
|
draw_info(&mut img, track.title, TextField::Title, &fonts.1.clone(), font_size, dark_text)?;
|
||||||
draw_info(&mut img, track.artist, TextField::Artist, &fonts.0.clone(), font_size, dark_text)?;
|
draw_info(&mut img, track.artist, TextField::Artist, &fonts.0.clone(), font_size, dark_text)?;
|
||||||
draw_info(&mut img, track.album, TextField::Album, &fonts.0.clone(), font_size, dark_text)?;
|
draw_info(&mut img, track.album, TextField::Album, &fonts.0.clone(), font_size, dark_text)?;
|
||||||
img::save_to_memory_png(&img)
|
img::save_to_memory_png(&img)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user