Files
craft-libs/crates/fig/src/images.rs
T
Dylan M. Taylor 26b36b4f9e Add craft-fig: the .fig container, read and written (#905)
* Add craft-kiwi: the kiwi binary schema format, decoded and encoded

The generic kiwi decoder and encoder from FigCraft's .fig import and export
(crates/format/src/fig/kiwi.rs at 3b9bf8c9), moved as it is: binary schema
decode and encode, the compact text form of a schema, message decode into a
tree keyed by field name, and message encode checked against the schema.

The value budget that follows a .fig file's compressed size stays with the
.fig reader; here decode_message_within takes the budget and
DEFAULT_MAX_VALUES is the floor FigCraft used.

* Add craft-fig: the .fig container, read and written

The app-independent half of FigCraft's .fig import and export
(crates/format/src/fig at 3b9bf8c9): the ZIP archive with its image
budget, the chunked canvas (deflate, zlib and zstd chunks), the value
budget that follows the compressed canvas, images carried in the
message's blobs, and the vector network and geometry blob encodings.
The message stays a craft-kiwi tree read by field name; mapping it onto
a document stays in the app.

* craft-kiwi: no scan per field in schema decode; the text form keeps the limits

Schema::decode compared each message field id with every earlier one: 20
definitions of 10 000 fields, within every cap, took a billion
comparisons. A set per definition now.

Schema::parse applies the definition and field caps of the binary form,
looks type names up in an index instead of scanning the lines per field,
accepts a struct or message without fields, and refuses two fields of one
name and a message field id that is 0 (it ends a message) or repeated.
2026-11-11 18:08:28 -05:00

86 lines
3.8 KiB
Rust

//! Images that travel inside the message instead of in `images/`.
use std::collections::BTreeMap;
use std::sync::Arc;
use craft_kiwi::Kv;
/// Longest image hash (bytes) an image is found by: `images/` names and embedded images both
/// stop at 64 bytes (128 hex digits).
pub const MAX_IMAGE_HASH: usize = 64;
/// Image bytes that travel inside the message, by image hash (lowercase hex): a paint's
/// `image.dataBlob` indexes the message's `blobs`. Copies written by plugins and other tools embed
/// their images this way, and a bare canvas (what the clipboard carries) has no `images/`. The
/// walk is iterative (no recursion on nested data) and skips blobs larger than `max_image` bytes.
/// Returns the index of each hash's blob.
pub fn embedded_images(msg: &Kv, max_image: u64) -> BTreeMap<String, usize> {
let blobs = msg.get("blobs").map(Kv::list).unwrap_or_default();
let mut found = BTreeMap::new();
let mut pending = vec![msg];
while let Some(value) = pending.pop() {
match value {
Kv::Object(fields) => {
if let Some((hash, index)) = value.get("image").and_then(|image| embedded_image(image, blobs, max_image)) {
found.entry(hash).or_insert(index);
}
pending.extend(fields.iter().filter(|(name, _)| *name != "blobs").map(|(_, field)| field));
}
Kv::Array(items) => pending.extend(items),
_ => {}
}
}
found
}
/// The images that travel inside the message ([`embedded_images`]), copied out of it once per
/// blob: every hash naming the same blob shares its copy. A file can point any number of hashes
/// at one large blob, and a copy for each would multiply it.
pub fn embedded_copies(msg: &Kv, max_image: u64) -> BTreeMap<String, Arc<[u8]>> {
let blobs = msg.get("blobs").map(Kv::list).unwrap_or_default();
let mut copies: BTreeMap<usize, Arc<[u8]>> = BTreeMap::new();
let mut out = BTreeMap::new();
for (hash, index) in embedded_images(msg, max_image) {
let Some(bytes) = blob(blobs, index) else { continue };
out.insert(hash, Arc::clone(copies.entry(index).or_insert_with(|| Arc::from(bytes))));
}
out
}
/// Embedded images ([`embedded_copies`]) whose blob an earlier hash already names, each with that
/// hash. Hashes that also have an `images/` member (`in_zip`) keep their own name: the member is
/// read first. Pointing every alias at one name lets a reader copy and hash each blob once,
/// however many hashes a file points at it.
pub fn embedded_aliases(embedded: &BTreeMap<String, Arc<[u8]>>, in_zip: impl Fn(&str) -> bool) -> BTreeMap<String, String> {
let mut first: BTreeMap<usize, &String> = BTreeMap::new();
let mut out = BTreeMap::new();
for (hash, bytes) in embedded {
if in_zip(hash) {
continue;
}
// Hashes naming one blob share its copy, so the copy's address tells the blob.
let canonical = *first.entry(bytes.as_ptr() as usize).or_insert(hash);
if canonical != hash {
out.insert(hash.clone(), canonical.clone());
}
}
out
}
/// The hash and blob index of an `Image` value whose `dataBlob` points at a usable blob.
fn embedded_image(image: &Kv, blobs: &[Kv], max_image: u64) -> Option<(String, usize)> {
let hash = image.get("hash").and_then(Kv::bytes).filter(|h| !h.is_empty() && h.len() <= MAX_IMAGE_HASH)?;
let Some(Kv::Uint(index)) = image.get("dataBlob") else { return None };
let index = usize::try_from(*index).ok()?;
let bytes = blob(blobs, index)?;
if bytes.is_empty() || bytes.len() as u64 > max_image {
return None;
}
Some((hash.iter().map(|b| format!("{b:02x}")).collect(), index))
}
/// The bytes of blob `index`.
fn blob<'k>(blobs: &'k [Kv], index: usize) -> Option<&'k [u8]> {
blobs.get(index)?.get("bytes").and_then(Kv::bytes)
}