diff --git a/showbits-typst-plugin/src/lib.rs b/showbits-typst-plugin/src/lib.rs index a64ce37..dd63ba6 100644 --- a/showbits-typst-plugin/src/lib.rs +++ b/showbits-typst-plugin/src/lib.rs @@ -1,27 +1,15 @@ use std::io::Cursor; use image::{ - ImageFormat, + ImageFormat, Luma, Pixel, imageops::{self, FilterType}, }; use mark::dither::{AlgoFloydSteinberg, AlgoStucki, Algorithm, DiffEuclid, Palette}; -use palette::{FromColor, IntoColor, LinLumaa, LinSrgb, Srgba}; +use palette::LinSrgb; use wasm_minimal_protocol::{initiate_protocol, wasm_func}; initiate_protocol!(); -// Palette <-> image color conversions - -fn image_to_palette(color: image::Rgba) -> Srgba { - let [r, g, b, a] = color.0; - Srgba::new(r, g, b, a).into_format() -} - -fn palette_to_image(color: Srgba) -> image::Rgba { - let color = color.into_format::(); - image::Rgba([color.red, color.green, color.blue, color.alpha]) -} - // Typst type conversions fn i64_from_bytes(bytes: &[u8]) -> Result { @@ -88,9 +76,11 @@ pub fn dither( if bright { for pixel in image.pixels_mut() { - let mut color = LinLumaa::from_color(image_to_palette(*pixel)); - color.luma = 1.0 - 0.4 * (1.0 - color.luma); - *pixel = palette_to_image(color.into_color()); + let [l] = pixel.to_luma().0; + let l = l as f32 / 255.0; // Convert to [0, 1] + let l = 1.0 - (0.4 * (1.0 - l)); // Lerp to [0.6, 1] + let l = (l.clamp(0.0, 1.0) * 255.0) as u8; // Convert back to [0, 255] + *pixel = Luma([l]).to_rgba(); } }