rustitch
A Rust library for parsing embroidery files and rendering stitch data to images.
Supported formats: PES, DST, EXP, JEF, VP3
Part of the stitch-peek-rs project.
Usage
Add rustitch to your Cargo.toml:
[dependencies]
rustitch = "0.2"
Generate a thumbnail
// PES files (backward-compatible API)
let pes_data = std::fs::read("design.pes")?;
let png_bytes = rustitch::thumbnail(&pes_data, 256)?;
std::fs::write("preview.png", &png_bytes)?;
// Any supported format
let dst_data = std::fs::read("pattern.dst")?;
let png_bytes = rustitch::thumbnail_format(&dst_data, 256, rustitch::Format::Dst)?;
std::fs::write("preview.png", &png_bytes)?;
Parse and inspect a PES 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)?;
Format detection
use rustitch::format::{self, Format};
use std::path::Path;
// Detect from file extension
let fmt = format::detect_from_extension(Path::new("design.jef"));
assert_eq!(fmt, Some(Format::Jef));
// Detect from file content (magic bytes)
let data = std::fs::read("design.pes")?;
let fmt = format::detect_from_bytes(&data);
assert_eq!(fmt, Some(Format::Pes));
Supported formats
| Format | Manufacturer | Colors | Notes |
|---|---|---|---|
| PES | Brother PE-Design | Embedded (PEC palette) | Versions 1-10 |
| DST | Tajima | Default palette | 3-byte bit-packed records |
| EXP | Melco/Bernina | Default palette | Simple 2-byte encoding |
| JEF | Janome | Embedded (Janome palette) | Structured header with color table |
| VP3 | Pfaff/Viking | Embedded (RGB) | Hierarchical format with per-section colors |
Formats without embedded color info (DST, EXP) use a default palette of 12 high-contrast colors, cycling on each color change.
How it works
- Detect the file format from magic bytes or extension
- Parse the format-specific binary encoding into a common
StitchCommandstream (stitch, jump, trim, color change, end) - Resolve relative movements into absolute coordinate segments grouped by thread color
- Render anti-aliased line segments with tiny-skia, scaled to fit the requested size
- Encode as PNG with proper alpha handling
License
MIT