rustitch
A Rust library for parsing PES embroidery files and rendering stitch data to images.
Part of the stitch-peek-rs project.
Usage
Add rustitch to your Cargo.toml:
[dependencies]
rustitch = "0.1"
Generate a thumbnail
let pes_data = std::fs::read("design.pes")?;
let png_bytes = rustitch::thumbnail(&pes_data, 256)?;
std::fs::write("preview.png", &png_bytes)?;
Parse and inspect a design
use rustitch::pes::{self, StitchCommand};
let data = std::fs::read("design.pes")?;
let design = pes::parse(&data)?;
println!("PES version: {}", std::str::from_utf8(&design.header.version).unwrap());
println!("Label: {}", design.pec_header.label);
println!("Colors: {}", design.pec_header.color_count);
let stitch_count = design.commands.iter()
.filter(|c| matches!(c, StitchCommand::Stitch { .. }))
.count();
println!("Stitches: {stitch_count}");
Resolve and render manually
use rustitch::pes;
let data = std::fs::read("design.pes")?;
let design = pes::parse(&data)?;
let resolved = pes::resolve(&design)?;
println!("Segments: {}", resolved.segments.len());
println!("Bounding box: ({}, {}) to ({}, {})",
resolved.bounds.min_x, resolved.bounds.min_y,
resolved.bounds.max_x, resolved.bounds.max_y);
let png_bytes = rustitch::render_thumbnail(&resolved, 512)?;
std::fs::write("large_preview.png", &png_bytes)?;
Supported formats
PES (Brother PE-Design) embroidery files, versions 1 through 10. The PEC section containing stitch data is consistent across versions.
How it works
- Parse the PES binary header to locate the PEC section
- Decode the PEC stitch byte stream (7-bit and 12-bit encoded relative movements, jumps, trims, color changes)
- Resolve relative movements into absolute coordinate segments grouped by thread color, using the 65-color Brother PEC palette
- Render anti-aliased line segments with tiny-skia, scaled to fit the requested size
- Encode as PNG with proper alpha handling
License
MIT