Dither images using wasm plugin

This commit is contained in:
Joscha 2025-03-01 02:17:03 +01:00
parent 179d0653bb
commit 92ec72ab4b
14 changed files with 116 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 370 KiB

View file

@ -0,0 +1 @@
../lib.typ

View file

@ -0,0 +1,4 @@
#import "lib.typ";
#show: it => lib.init(it)
#lib.dither("image.png")

View file

@ -0,0 +1,24 @@
use std::io::Cursor;
use anyhow::Context;
use image::{ImageFormat, RgbaImage};
use showbits_typst::Typst;
pub struct Image {
pub image: RgbaImage,
}
impl Image {
pub fn into_typst(self) -> anyhow::Result<Typst> {
let mut bytes: Vec<u8> = Vec::new();
self.image
.write_to(&mut Cursor::new(&mut bytes), ImageFormat::Png)
.context("failed to encode image as png")?;
let typst = super::typst_with_lib()
.with_file("/image.png", bytes)
.with_main_file(include_str!("main.typ"));
Ok(typst)
}
}

View file

@ -0,0 +1 @@
../plugin.wasm

View file

@ -20,4 +20,14 @@
// the same size after tearing off the paper.
#let feed = v(64pt + 32pt)
#import plugin("plugin.wasm") as plugin
////////////
// Plugin //
////////////
#import plugin("plugin.wasm") as p
#let dither(path) = {
let bytes = read(path, encoding: none)
let dithered = p.dither(bytes)
image(dithered)
}