Remove typst plugin
It was nice but really slow.
This commit is contained in:
parent
3a2637bb34
commit
d9dbd90ff2
12 changed files with 1 additions and 576 deletions
|
|
@ -1,16 +1,5 @@
|
|||
#!/usr/bin/env fish
|
||||
|
||||
set_color --bold yellow
|
||||
echo "##################"
|
||||
echo "## typst plugin ##"
|
||||
echo "##################"
|
||||
set_color normal
|
||||
|
||||
./meta/build-typst-plugin
|
||||
or return 1
|
||||
|
||||
echo
|
||||
|
||||
set_color --bold yellow
|
||||
echo "########################"
|
||||
echo "## thermal printer ui ##"
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
#!/usr/bin/env fish
|
||||
|
||||
rustup target add wasm32-unknown-unknown
|
||||
or return 1
|
||||
|
||||
pushd showbits-typst-plugin
|
||||
|
||||
cargo build --release --target wasm32-unknown-unknown
|
||||
or return 1
|
||||
|
||||
popd
|
||||
|
|
@ -2,10 +2,6 @@
|
|||
|
||||
cargo clippy
|
||||
|
||||
pushd showbits-typst-plugin
|
||||
cargo clippy
|
||||
popd
|
||||
|
||||
pushd showbits-thermal-printer-ui
|
||||
pnpm check
|
||||
popd
|
||||
|
|
|
|||
|
|
@ -2,10 +2,6 @@
|
|||
|
||||
cargo clean
|
||||
|
||||
pushd showbits-typst-plugin
|
||||
cargo clean
|
||||
popd
|
||||
|
||||
rm -rf node_modules
|
||||
rm -rf showbits-thermal-printer-ui/node_modules
|
||||
rm -rf showbits-thermal-printer-ui/dist
|
||||
|
|
|
|||
5
meta/fmt
5
meta/fmt
|
|
@ -1,9 +1,4 @@
|
|||
#!/usr/bin/env fish
|
||||
|
||||
cargo fmt
|
||||
|
||||
pushd showbits-typst-plugin
|
||||
cargo fmt
|
||||
popd
|
||||
|
||||
pnpm fmt
|
||||
|
|
|
|||
|
|
@ -9,10 +9,5 @@ pub mod text;
|
|||
pub mod tictactoe;
|
||||
|
||||
fn typst_with_lib() -> Typst {
|
||||
Typst::new()
|
||||
.with_file("/lib/main.typ", include_str!("documents/lib/main.typ"))
|
||||
.with_file(
|
||||
"/lib/plugin.wasm",
|
||||
include_bytes!("documents/lib/plugin.wasm"),
|
||||
)
|
||||
Typst::new().with_file("/lib/main.typ", include_str!("documents/lib/main.typ"))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,38 +23,3 @@
|
|||
// Determined by experiments so that the top and bottom white border are roughly
|
||||
// the same size after tearing off the paper.
|
||||
#let feed = v(64pt + 32pt)
|
||||
|
||||
////////////
|
||||
// Plugin //
|
||||
////////////
|
||||
|
||||
#import plugin("plugin.wasm") as p
|
||||
|
||||
#let _number_to_bytes(n) = int(n).to-bytes(size: 8)
|
||||
|
||||
#let _bool_to_bytes(b) = _number_to_bytes(if b { 1 } else { 0 })
|
||||
|
||||
#let _str_to_bytes(s) = {
|
||||
bytes(s)
|
||||
}
|
||||
|
||||
#let _length_to_bytes(l) = {
|
||||
let l = l.pt()
|
||||
let n = if l > 10000 { -1 } else { int(l) }
|
||||
_number_to_bytes(n)
|
||||
}
|
||||
|
||||
#let dither(
|
||||
data,
|
||||
bright: true,
|
||||
algorithm: "floyd-steinberg",
|
||||
) = layout(size => {
|
||||
let dithered = p.dither(
|
||||
data,
|
||||
_length_to_bytes(size.width),
|
||||
_length_to_bytes(size.height),
|
||||
_bool_to_bytes(bright),
|
||||
_str_to_bytes(algorithm),
|
||||
)
|
||||
image(dithered)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
../../../../showbits-typst-plugin/target/wasm32-unknown-unknown/release/showbits_typst_plugin.wasm
|
||||
1
showbits-typst-plugin/.gitignore
vendored
1
showbits-typst-plugin/.gitignore
vendored
|
|
@ -1 +0,0 @@
|
|||
/target/
|
||||
339
showbits-typst-plugin/Cargo.lock
generated
339
showbits-typst-plugin/Cargo.lock
generated
|
|
@ -1,339 +0,0 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "adler2"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
||||
|
||||
[[package]]
|
||||
name = "approx"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "by_address"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "64fa3c856b712db6612c019f14756e64e4bcea13337a6b33b696333a9eaa2d06"
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck"
|
||||
version = "1.21.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ef657dfab802224e671f5818e9a4935f9b1957ed18e58292690cc39e7a4092a3"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder-lite"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fast-srgb8"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd2e7510819d6fbf51a5545c8f922716ecfb14df168a3242f7d33e0239efe6a1"
|
||||
|
||||
[[package]]
|
||||
name = "fdeflate"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c"
|
||||
dependencies = [
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "11faaf5a5236997af9848be0bef4db95824b1d534ebc64d0f0c6cf3e67bd38dc"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "image"
|
||||
version = "0.25.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cd6f44aed642f18953a158afeb30206f4d50da59fbc66ecb53c66488de73563b"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"byteorder-lite",
|
||||
"num-traits",
|
||||
"png",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mark"
|
||||
version = "0.0.0"
|
||||
source = "git+https://github.com/Garmelon/mark.git?rev=2a862a69d69abc64ddd7eefd1e1ff3d05ce3b6e4#2a862a69d69abc64ddd7eefd1e1ff3d05ce3b6e4"
|
||||
dependencies = [
|
||||
"image",
|
||||
"palette",
|
||||
"rand 0.9.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e3e04debbb59698c15bacbb6d93584a8c0ca9cc3213cb423d31f760d8843ce5"
|
||||
dependencies = [
|
||||
"adler2",
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "palette"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4cbf71184cc5ecc2e4e1baccdb21026c20e5fc3dcf63028a086131b3ab00b6e6"
|
||||
dependencies = [
|
||||
"approx",
|
||||
"fast-srgb8",
|
||||
"palette_derive",
|
||||
"phf",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "palette_derive"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f5030daf005bface118c096f510ffb781fc28f9ab6a32ab224d8631be6851d30"
|
||||
dependencies = [
|
||||
"by_address",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "phf"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078"
|
||||
dependencies = [
|
||||
"phf_macros",
|
||||
"phf_shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "phf_generator"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d"
|
||||
dependencies = [
|
||||
"phf_shared",
|
||||
"rand 0.8.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "phf_macros"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f84ac04429c13a7ff43785d75ad27569f2951ce0ffd30a3321230db2fc727216"
|
||||
dependencies = [
|
||||
"phf_generator",
|
||||
"phf_shared",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "phf_shared"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5"
|
||||
dependencies = [
|
||||
"siphasher",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "png"
|
||||
version = "0.17.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82151a2fc869e011c153adc57cf2789ccb8d9906ce52c0b39a6b5697749d7526"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"crc32fast",
|
||||
"fdeflate",
|
||||
"flate2",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"rand_core 0.6.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94"
|
||||
dependencies = [
|
||||
"rand_core 0.9.2",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.9.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a509b1a2ffbe92afab0e55c8fd99dea1c280e8171bd2d88682bb20bc41cbc2c"
|
||||
dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "showbits-typst-plugin"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"image",
|
||||
"mark",
|
||||
"palette",
|
||||
"wasm-minimal-protocol",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "simd-adler32"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe"
|
||||
|
||||
[[package]]
|
||||
name = "siphasher"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.98"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe"
|
||||
|
||||
[[package]]
|
||||
name = "venial"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61584a325b16f97b5b25fcc852eb9550843a251057a5e3e5992d2376f3df4bb2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-minimal-protocol"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/astrale-sharp/wasm-minimal-protocol.git?rev=90336ebf2d99844fd8f8e99ea7096af96526cbf4#90336ebf2d99844fd8f8e99ea7096af96526cbf4"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"venial",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcf01143b2dd5d134f11f545cf9f1431b13b749695cb33bcce051e7568f99478"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712c8386f4f4299382c9abee219bee7084f78fb939d88b6840fcc1320d5f6da2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
[package]
|
||||
name = "showbits-typst-plugin"
|
||||
version = "0.0.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
palette = "0.7.6"
|
||||
|
||||
[dependencies.image]
|
||||
version = "0.25.5"
|
||||
default-features = false
|
||||
features = ["png"]
|
||||
|
||||
[dependencies.mark]
|
||||
git = "https://github.com/Garmelon/mark.git"
|
||||
rev = "2a862a69d69abc64ddd7eefd1e1ff3d05ce3b6e4"
|
||||
|
||||
[dependencies.wasm-minimal-protocol]
|
||||
git = "https://github.com/astrale-sharp/wasm-minimal-protocol.git"
|
||||
rev = "90336ebf2d99844fd8f8e99ea7096af96526cbf4"
|
||||
|
||||
[profile.release]
|
||||
lto = true # Enable link-time optimization
|
||||
strip = true # Strip symbols from binary*
|
||||
opt-level = 'z' # Optimize for size
|
||||
codegen-units = 1 # Reduce number of codegen units to increase optimizations
|
||||
panic = 'abort' # Abort on panic
|
||||
|
||||
[lints]
|
||||
rust.unsafe_code = { level = "forbid", priority = 1 }
|
||||
# Lint groups
|
||||
rust.deprecated_safe = "warn"
|
||||
rust.future_incompatible = "warn"
|
||||
rust.keyword_idents = "warn"
|
||||
rust.rust_2018_idioms = "warn"
|
||||
rust.unused = "warn"
|
||||
# Individual lints
|
||||
rust.non_local_definitions = "warn"
|
||||
rust.redundant_imports = "warn"
|
||||
rust.redundant_lifetimes = "warn"
|
||||
rust.single_use_lifetimes = "warn"
|
||||
rust.unit_bindings = "warn"
|
||||
rust.unused_crate_dependencies = "warn"
|
||||
rust.unused_import_braces = "warn"
|
||||
rust.unused_lifetimes = "warn"
|
||||
rust.unused_qualifications = "warn"
|
||||
# Clippy
|
||||
clippy.use_self = "warn"
|
||||
|
||||
[workspace] # Prevent package from being included in outer workspace
|
||||
|
|
@ -1,106 +0,0 @@
|
|||
use std::io::Cursor;
|
||||
|
||||
use image::{
|
||||
ImageFormat, Luma, Pixel,
|
||||
imageops::{self, FilterType},
|
||||
};
|
||||
use mark::dither::{AlgoFloydSteinberg, AlgoStucki, Algorithm, DiffEuclid, Palette};
|
||||
use palette::LinSrgb;
|
||||
use wasm_minimal_protocol::{initiate_protocol, wasm_func};
|
||||
|
||||
initiate_protocol!();
|
||||
|
||||
// Typst type conversions
|
||||
|
||||
fn i64_from_bytes(bytes: &[u8]) -> Result<i64, String> {
|
||||
let bytes: [u8; 8] = bytes.try_into().map_err(|it| format!("{it}"))?;
|
||||
Ok(i64::from_le_bytes(bytes))
|
||||
}
|
||||
|
||||
fn bool_from_bytes(bytes: &[u8]) -> Result<bool, String> {
|
||||
Ok(i64_from_bytes(bytes)? != 0)
|
||||
}
|
||||
|
||||
fn str_from_bytes(bytes: &[u8]) -> Result<&str, String> {
|
||||
std::str::from_utf8(bytes).map_err(|it| format!("{it}"))
|
||||
}
|
||||
|
||||
fn size_from_bytes(bytes: &[u8]) -> Result<Option<u32>, String> {
|
||||
let size = i64_from_bytes(bytes)?;
|
||||
|
||||
if size < 0 {
|
||||
return Ok(None); // Unlimited width
|
||||
}
|
||||
|
||||
let size: u32 = size.try_into().map_err(|_| "size too large")?;
|
||||
Ok(Some(size))
|
||||
}
|
||||
|
||||
// Typst methods
|
||||
|
||||
#[wasm_func]
|
||||
pub fn dither(
|
||||
image: &[u8],
|
||||
max_width: &[u8],
|
||||
max_height: &[u8],
|
||||
bright: &[u8],
|
||||
algorithm: &[u8],
|
||||
) -> Result<Vec<u8>, String> {
|
||||
let max_width = size_from_bytes(max_width)?;
|
||||
let max_height = size_from_bytes(max_height)?;
|
||||
let bright = bool_from_bytes(bright)?;
|
||||
let algorithm = str_from_bytes(algorithm)?;
|
||||
|
||||
let mut image = image::load_from_memory(image)
|
||||
.map_err(|it| format!("Failed to read image: {it:?}"))?
|
||||
.to_rgba8();
|
||||
|
||||
let image_width = image.width();
|
||||
let image_height = image.height();
|
||||
|
||||
let scale_factor = match (max_width, max_height) {
|
||||
(None, None) => 1.0,
|
||||
(None, Some(height)) => height as f32 / image_height as f32,
|
||||
(Some(width), None) => width as f32 / image_width as f32,
|
||||
(Some(width), Some(height)) => {
|
||||
(width as f32 / image_width as f32).min(height as f32 / image_height as f32)
|
||||
}
|
||||
};
|
||||
|
||||
let target_width = (image_width as f32 * scale_factor) as u32;
|
||||
let target_height = (image_height as f32 * scale_factor) as u32;
|
||||
|
||||
if image_width != target_width || image_height != target_height {
|
||||
image = imageops::resize(&image, target_width, target_height, FilterType::CatmullRom);
|
||||
}
|
||||
|
||||
if bright {
|
||||
for pixel in image.pixels_mut() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
let palette = Palette::new(vec![
|
||||
LinSrgb::new(0.0, 0.0, 0.0),
|
||||
LinSrgb::new(1.0, 1.0, 1.0),
|
||||
]);
|
||||
|
||||
let dithered = match algorithm {
|
||||
"floyd-steinberg" => {
|
||||
<AlgoFloydSteinberg as Algorithm<LinSrgb, DiffEuclid>>::run(image, &palette)
|
||||
}
|
||||
"stucki" => <AlgoStucki as Algorithm<LinSrgb, DiffEuclid>>::run(image, &palette),
|
||||
it => Err(format!("Unknown algorithm: {it}"))?,
|
||||
};
|
||||
|
||||
let mut bytes: Vec<u8> = Vec::new();
|
||||
dithered
|
||||
.write_to(&mut Cursor::new(&mut bytes), ImageFormat::Png)
|
||||
.map_err(|it| format!("Failed to write image: {it:?}"))?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue