Skip to content

armgit / klad

Public Imported from GitHub
0

A drawing app built for agent/human collaboration

Rust

PNG exports compress on every core

Indicode BVffc86d7e1716
diff --git a/Cargo.lock b/Cargo.lock
index b881486..6c44ff0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1819,6 +1819,8 @@ version = "0.1.0"
dependencies = [
"anyhow",
"bytemuck",
+ "crc32fast",
+ "flate2",
"half",
"klad-brush",
"klad-core",
diff --git a/crates/render/Cargo.toml b/crates/render/Cargo.toml
index 5cfd68b..d4fd7a2 100644
--- a/crates/render/Cargo.toml
+++ b/crates/render/Cargo.toml
@@ -18,3 +18,5 @@ skia-safe = "0.157.0"
skrifa = "0.44.0"
zune-jpeg = "0.5.15"
png = "0.18.1"
+flate2 = "1.1.10"
+crc32fast = "1.5.4"
diff --git a/crates/render/src/export.rs b/crates/render/src/export.rs
index 615d6d2..5e65654 100644
--- a/crates/render/src/export.rs
+++ b/crates/render/src/export.rs
@@ -1,10 +1,10 @@
-use crate::{Ctx, Rasters, background, draw_clipped_alone, draw_layer, draw_layers, f16_info, rgba8_info};
+use crate::{Ctx, Rasters, background, draw_clipped_alone, draw_layer, draw_layers, f16_info};
use anyhow::{Result, anyhow, bail};
use klad_core::doc::{Blend, Document, Layer};
use rayon::prelude::*;
use skia_safe::{
- AlphaType, ColorSpace, ColorType, CubicResampler, FilterMode, Image, ImageInfo, MipmapMode, Pixmap,
- SamplingOptions, image::CachingHint, png_encoder, surfaces,
+ AlphaType, ColorSpace, ColorType, CubicResampler, FilterMode, Image, ImageInfo, MipmapMode, SamplingOptions,
+ image::CachingHint, surfaces,
};
pub const SNAP: f32 = 0.13;
@@ -149,30 +149,20 @@ pub fn dither8(image: &Image) -> Result<Vec<u8>> {
pub fn png8(image: &Image) -> Result<Vec<u8>> {
let (w, h) = (image.width() as u32, image.height() as u32);
- let mut rgba = dither8(image)?;
- let pixmap = Pixmap::new(&rgba8_info(w, h), &mut rgba, w as usize * 4).ok_or_else(|| anyhow!("bad pixmap"))?;
- encode(&pixmap)
+ Ok(crate::pngwrite::write(w, h, 8, &dither8(image)?))
}
pub fn png16(image: &Image) -> Result<Vec<u8>> {
- let (w, h) = (image.width(), image.height());
- let mut pixels = f16_pixels(image, AlphaType::Unpremul)?;
- let info = ImageInfo::new((w, h), ColorType::RGBAF16, AlphaType::Unpremul, ColorSpace::new_srgb());
- let pixmap = Pixmap::new(
- &info,
- bytemuck::cast_slice_mut::<half::f16, u8>(&mut pixels),
- w as usize * 8,
- )
- .ok_or_else(|| anyhow!("bad pixmap"))?;
- encode(&pixmap)
+ let (w, h) = (image.width() as u32, image.height() as u32);
+ Ok(png_of_f16(w, h, &f16_pixels(image, AlphaType::Unpremul)?))
}
-pub(crate) fn encode(pixmap: &Pixmap) -> Result<Vec<u8>> {
- let mut out = Vec::new();
- if !png_encoder::encode(pixmap, &mut out, &png_encoder::Options::default()) {
- bail!("PNG encoding failed");
- }
- Ok(out)
+pub(crate) fn png_of_f16(width: u32, height: u32, pixels: &[half::f16]) -> Vec<u8> {
+ let wide: Vec<u8> = pixels
+ .par_iter()
+ .flat_map_iter(|v| ((v.to_f32().clamp(0.0, 1.0) * 65535.0).round() as u16).to_be_bytes())
+ .collect();
+ crate::pngwrite::write(width, height, 16, &wide)
}
#[cfg(test)]
@@ -382,3 +372,64 @@ mod tests {
assert_eq!(frame.pixel(48, 16)[3], 0, "outside the base: clipped away");
}
}
+
+#[cfg(test)]
+mod timing {
+ use super::*;
+
+ #[test]
+ #[ignore = "timing: PICTURE=<png> cargo test --release -p klad-render png_timing -- --ignored --nocapture"]
+ fn png_timing() {
+ let path = std::env::var("PICTURE").unwrap();
+ let rgba = crate::assets::decode(&std::fs::read(&path).unwrap()).unwrap();
+ let info = ImageInfo::new(
+ (rgba.width as i32, rgba.height as i32),
+ ColorType::RGBA8888,
+ AlphaType::Unpremul,
+ ColorSpace::new_srgb(),
+ );
+ let image = skia_safe::images::raster_from_data(
+ &info,
+ skia_safe::Data::new_copy(&rgba.pixels),
+ rgba.width as usize * 4,
+ )
+ .unwrap();
+ let time = |f: &dyn Fn() -> usize| {
+ let t = std::time::Instant::now();
+ let n = f();
+ (t.elapsed().as_secs_f64() * 1000.0, n)
+ };
+ let (dither, _) = time(&|| dither8(&image).unwrap().len());
+ let skia = |pixmap: &skia_safe::Pixmap| {
+ let mut out = Vec::new();
+ skia_safe::png_encoder::encode(pixmap, &mut out, &skia_safe::png_encoder::Options::default());
+ out.len()
+ };
+ let (before, before_bytes) = time(&|| {
+ let mut rgba = dither8(&image).unwrap();
+ let (w, h) = (image.width() as u32, image.height() as u32);
+ let pixmap = skia_safe::Pixmap::new(&crate::rgba8_info(w, h), &mut rgba, w as usize * 4).unwrap();
+ skia(&pixmap)
+ });
+ let same = {
+ let ours = png8(&image).unwrap();
+ let mut reader = png::Decoder::new(std::io::Cursor::new(&ours[..])).read_info().unwrap();
+ let mut buf = vec![0; reader.output_buffer_size().unwrap()];
+ reader.next_frame(&mut buf).unwrap();
+ buf == dither8(&image).unwrap()
+ };
+ println!(
+ "skia png8 {before:.0} ms ({} KB); ours decodes to the same pixels: {same}",
+ before_bytes / 1024
+ );
+ let (eight, bytes8) = time(&|| png8(&image).unwrap().len());
+ let (sixteen, bytes16) = time(&|| png16(&image).unwrap().len());
+ println!(
+ "{}x{}: dither {dither:.0} ms, png8 {eight:.0} ms ({} KB), png16 {sixteen:.0} ms ({} KB)",
+ image.width(),
+ image.height(),
+ bytes8 / 1024,
+ bytes16 / 1024
+ );
+ }
+}
diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs
index 54fe626..e3bbd11 100644
--- a/crates/render/src/lib.rs
+++ b/crates/render/src/lib.rs
@@ -4,6 +4,7 @@ pub mod export;
pub mod form;
pub mod onion;
pub mod overlay;
+pub mod pngwrite;
pub mod shapes;
pub mod sheet;
pub mod sprites;
diff --git a/crates/render/src/pngwrite.rs b/crates/render/src/pngwrite.rs
new file mode 100644
index 0000000..b820d01
--- /dev/null
+++ b/crates/render/src/pngwrite.rs
@@ -0,0 +1,148 @@
+use crc32fast::Hasher;
+use flate2::{Compress, Compression, FlushCompress, Status};
+use rayon::prelude::*;
+
+const STRIP: usize = 256 << 10;
+const IDAT: usize = 1 << 20;
+const ADLER: u32 = 65521;
+
+pub fn write(width: u32, height: u32, depth: u8, rows: &[u8]) -> Vec<u8> {
+ let bpp = 4 * depth as usize / 8;
+ let line = width as usize * bpp;
+ let filtered: Vec<Vec<u8>> = (0..height as usize)
+ .into_par_iter()
+ .map(|y| {
+ let row = &rows[y * line..(y + 1) * line];
+ let above = (y > 0).then(|| &rows[(y - 1) * line..y * line]);
+ filter(row, above, bpp)
+ })
+ .collect();
+ let per_strip = (STRIP / (line + 1)).max(1);
+ let strips: Vec<&[Vec<u8>]> = filtered.chunks(per_strip).collect();
+ let last = strips.len().saturating_sub(1);
+ let packed: Vec<Vec<u8>> = strips
+ .par_iter()
+ .enumerate()
+ .map(|(i, strip)| deflate(&strip.concat(), i == last))
+ .collect();
+ let sum = filtered.iter().fold((1u32, 0u32), |acc, row| adler(acc, row));
+ let mut zlib = Vec::with_capacity(packed.iter().map(Vec::len).sum::<usize>() + 6);
+ zlib.extend_from_slice(&[0x78, 0x9c]);
+ packed.iter().for_each(|p| zlib.extend_from_slice(p));
+ zlib.extend_from_slice(&((sum.1 << 16) | sum.0).to_be_bytes());
+ let mut out = Vec::with_capacity(zlib.len() + 64);
+ out.extend_from_slice(&[0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]);
+ let mut header = Vec::with_capacity(13);
+ header.extend_from_slice(&width.to_be_bytes());
+ header.extend_from_slice(&height.to_be_bytes());
+ header.extend_from_slice(&[depth, 6, 0, 0, 0]);
+ chunk(&mut out, b"IHDR", &header);
+ chunk(&mut out, b"sRGB", &[0]);
+ zlib.chunks(IDAT).for_each(|part| chunk(&mut out, b"IDAT", part));
+ chunk(&mut out, b"IEND", &[]);
+ out
+}
+
+fn chunk(out: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) {
+ out.extend_from_slice(&(data.len() as u32).to_be_bytes());
+ out.extend_from_slice(kind);
+ out.extend_from_slice(data);
+ let mut crc = Hasher::new();
+ crc.update(kind);
+ crc.update(data);
+ out.extend_from_slice(&crc.finalize().to_be_bytes());
+}
+
+fn deflate(data: &[u8], last: bool) -> Vec<u8> {
+ let mut packer = Compress::new(Compression::new(6), false);
+ let mut out = Vec::with_capacity(data.len() / 2 + 4096);
+ let flush = match last {
+ true => FlushCompress::Finish,
+ false => FlushCompress::Sync,
+ };
+ loop {
+ out.reserve(64 << 10);
+ let read = packer.total_in() as usize;
+ let status = packer
+ .compress_vec(&data[read..], &mut out, flush)
+ .expect("deflate never fails on memory input");
+ let room = out.len() < out.capacity();
+ match last {
+ true if status == Status::StreamEnd => break,
+ false if packer.total_in() as usize == data.len() && room => break,
+ _ => {}
+ }
+ }
+ out
+}
+
+fn adler((a, b): (u32, u32), data: &[u8]) -> (u32, u32) {
+ data.chunks(5552).fold((a, b), |(mut a, mut b), part| {
+ part.iter().for_each(|&v| {
+ a += v as u32;
+ b += a;
+ });
+ (a % ADLER, b % ADLER)
+ })
+}
+
+fn paeth(a: u8, b: u8, c: u8) -> u8 {
+ let p = a as i16 + b as i16 - c as i16;
+ let (pa, pb, pc) = ((p - a as i16).abs(), (p - b as i16).abs(), (p - c as i16).abs());
+ match (pa <= pb && pa <= pc, pb <= pc) {
+ (true, _) => a,
+ (false, true) => b,
+ _ => c,
+ }
+}
+
+fn filter(row: &[u8], above: Option<&[u8]>, bpp: usize) -> Vec<u8> {
+ let zero = vec![0u8; row.len()];
+ let up = above.unwrap_or(&zero);
+ let predict = |kind: u8, i: usize| {
+ let a = if i >= bpp { row[i - bpp] } else { 0 };
+ let c = if i >= bpp { up[i - bpp] } else { 0 };
+ match kind {
+ 0 => 0,
+ 1 => a,
+ 2 => up[i],
+ 3 => ((a as u16 + up[i] as u16) / 2) as u8,
+ _ => paeth(a, up[i], c),
+ }
+ };
+ let line = |kind: u8| -> Vec<u8> { (0..row.len()).map(|i| row[i].wrapping_sub(predict(kind, i))).collect() };
+ let cost = |bytes: &[u8]| bytes.iter().map(|&v| (v as i8).unsigned_abs() as u32).sum::<u32>();
+ let (kind, bytes) = (0..5u8)
+ .map(|k| (k, line(k)))
+ .min_by_key(|(k, bytes)| (cost(bytes), *k))
+ .expect("five filters");
+ std::iter::once(kind).chain(bytes).collect()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn decoded(bytes: &[u8]) -> (png::OutputInfo, Vec<u8>) {
+ let mut reader = png::Decoder::new(std::io::Cursor::new(bytes)).read_info().unwrap();
+ let mut buf = vec![0; reader.output_buffer_size().unwrap()];
+ let info = reader.next_frame(&mut buf).unwrap();
+ (info, buf)
+ }
+
+ #[test]
+ fn what_it_writes_decodes_to_the_same_pixels_at_8_and_16_bits() {
+ let (w, h) = (300u32, 700u32);
+ let eight: Vec<u8> = (0..w * h * 4)
+ .map(|i| ((i * 7 + (i / 1200) * 13) % 251) as u8 ^ ((i % 4 == 3) as u8 * 0x80))
+ .collect();
+ let (info, back) = decoded(&write(w, h, 8, &eight));
+ assert_eq!((info.width, info.height, info.bit_depth as u8), (w, h, 8));
+ assert_eq!(back, eight);
+ let sixteen: Vec<u8> = (0..w * h * 8).map(|i| ((i * 31 + i / 2400) % 256) as u8).collect();
+ let (info, back) = decoded(&write(w, h, 16, &sixteen));
+ assert_eq!(info.bit_depth as u8, 16);
+ assert_eq!(back, sixteen);
+ assert_eq!(write(w, h, 8, &eight), write(w, h, 8, &eight));
+ }
+}
diff --git a/crates/render/src/sprites.rs b/crates/render/src/sprites.rs
index 678ad78..3b09e72 100644
--- a/crates/render/src/sprites.rs
+++ b/crates/render/src/sprites.rs
@@ -1,10 +1,9 @@
-use crate::export::{dither8, encode, f16_pixels};
-use crate::rgba8_info;
-use anyhow::{Result, anyhow, bail};
+use crate::export::{dither8, f16_pixels, png_of_f16};
+use anyhow::{Result, bail};
use klad_core::doc::{Clip, Document};
use klad_core::rng::{FNV_OFFSET, fnv};
use serde_json::{Value, json};
-use skia_safe::{AlphaType, ColorSpace, ColorType, Image, ImageInfo, Pixmap};
+use skia_safe::{AlphaType, Image};
#[derive(Clone, Debug, PartialEq)]
pub struct Animation {
@@ -197,20 +196,10 @@ impl Pixels {
pub fn png(&mut self) -> Result<Vec<u8>> {
let (w, h) = (self.width, self.height);
- let pixmap = match &mut self.data {
- Data::Rgba8(d) => Pixmap::new(&rgba8_info(w, h), d, w as usize * 4),
- Data::Rgba16(d) => {
- let info = ImageInfo::new(
- (w as i32, h as i32),
- ColorType::RGBAF16,
- AlphaType::Unpremul,
- ColorSpace::new_srgb(),
- );
- Pixmap::new(&info, bytemuck::cast_slice_mut(d), w as usize * 8)
- }
- }
- .ok_or_else(|| anyhow!("bad pixmap"))?;
- encode(&pixmap)
+ Ok(match &self.data {
+ Data::Rgba8(d) => crate::pngwrite::write(w, h, 8, d),
+ Data::Rgba16(d) => png_of_f16(w, h, d),
+ })
}
}
diff --git a/docs/DESIGN.md b/docs/DESIGN.md
index c33a06d..5a50d50 100644
--- a/docs/DESIGN.md
+++ b/docs/DESIGN.md
@@ -339,7 +339,7 @@ A brush is JSON. The library is built in (`crates/core/brushes/library/`: `ink`
- Frame keys read a stroke file only when its size, time or inode changed (writes are atomic renames, so a rewrite always gets a new inode); an edit no longer clears them.
- **Onion skin** (M3) is computed in `klad-render` (`onion`), one piece of code for the CLI, the editor's canvas and its frame cache. For a layer at a frame it lists the drawings before and after the current one: counted in drawings (the exposures before and after, each drawing once at its nearest distance, the current drawing and links to it skipped) or in frames (the drawings shown 1, 2, ... frames away). Opacity is `opacity x falloff^(distance - 1)` (defaults 0.5 and 0.6, 2 before and 2 after); those before are tinted red and those after green (`rgb x 0.25 + tint x alpha x 0.75`) unless the tint is off. In a looping range the count wraps around inside the range, so the last drawing of a cycle shows the first. The ghosts of a layer are composed (furthest first, f32 per pixel, on tiles in parallel, deterministic) into one raster, which the compositor draws right under that layer, outside its opacity, blend and mask; the ghosts of a clip group's layers go under the whole group. Hidden layers show none.
- **Sheets** (M3): `klad render --sheet` lays frames out in a grid (columns about square, `--columns`), each cell the frame at `--cell` px on its longest side with its frame number, a `hold` mark when it looks exactly like the frame before, and per layer (top first, listed in the heading) the cel it shows: `c5` starts a drawing, `|c5` holds it, `c9>c1` a link, `.` blank. `--drawings` merges frames that look the same into one cell titled with their range. A clip's sheet is headed with the clip's name and its fps. Frames that look the same are composed once; drawings load in batches under 768 MB, so long sheets of big canvases stay in memory.
-- Export (`klad export`) renders the image, or one layer or group alone (a clipped layer alone stays clipped to its base's shape), at a frame, at a scale from 1/16 to 8, on transparency unless `--background`. Vector nodes are drawn at the export scale. Above full size the paint is drawn again at the export size: each drawing's marks are scaled (`markedit::transform_mark`: samples, sizes, tapers and cuts; fill coverage resampled bilinearly) and replayed with the brushes at that scale (`BrushSet::at_scale`: grain scaled with it, speed and bristle spacing measured in canvas px), the rasters drawn one to one (`Ctx::detail`), so lines stay crisp and textures keep their size; sprite sheets and frames do the same with their vector cels rasterized at that size. That replays without the cache (the lemons at 2x: 5.5 s instead of 0.7 s). Below full size the cached rasters are mipmapped down. 8-bit PNGs are ordered-dithered (8x8 Bayer) from the f16 pixels, colour and alpha, so gradients do not band; values within an eighth of a level of an 8-bit value are written as that value, so flat colours stay flat. `--depth 16` writes 16-bit PNG. Working renders (`klad render`, `klad hash`) stay undithered so their hashes hold.
+- Export (`klad export`) renders the image, or one layer or group alone (a clipped layer alone stays clipped to its base's shape), at a frame, at a scale from 1/16 to 8, on transparency unless `--background`. Vector nodes are drawn at the export scale. Above full size the paint is drawn again at the export size: each drawing's marks are scaled (`markedit::transform_mark`: samples, sizes, tapers and cuts; fill coverage resampled bilinearly) and replayed with the brushes at that scale (`BrushSet::at_scale`: grain scaled with it, speed and bristle spacing measured in canvas px), the rasters drawn one to one (`Ctx::detail`), so lines stay crisp and textures keep their size; sprite sheets and frames do the same with their vector cels rasterized at that size. That replays without the cache (the lemons at 2x: 5.5 s instead of 0.7 s). Below full size the cached rasters are mipmapped down. PNGs are written by `klad-render/pngwrite.rs` on every core: rows filtered in parallel (the filter with the smallest sum per row), compressed in strips of 256 KB as one deflate stream (each strip ends on a sync flush, the last finishes it), in IDAT chunks of 1 MB, with an sRGB chunk; the strips depend on the row size only, so the bytes are the same on any machine (2400x1800: 115 ms instead of Skia's 457 ms, the same size within 2%). 8-bit PNGs are ordered-dithered (8x8 Bayer) from the f16 pixels, colour and alpha, so gradients do not band; values within an eighth of a level of an 8-bit value are written as that value, so flat colours stay flat. `--depth 16` writes 16-bit PNG. Working renders (`klad render`, `klad hash`) stay undithered so their hashes hold.
## The agent interface: `klad` CLI