Files
stitch-peek-rs/rustitch/src/pec.rs
T
2026-04-03 18:45:37 +02:00

30 lines
1.0 KiB
Rust

use crate::error::Error;
use crate::pes::pec::{decode_stitches, parse_pec_header};
use crate::types::{ResolvedDesign, StitchCommand};
/// Parse a standalone PEC file (`#PEC0001` prefix + PEC data).
pub fn parse(data: &[u8]) -> Result<(Vec<StitchCommand>, Vec<(u8, u8, u8)>), Error> {
if data.len() < 8 || &data[0..8] != b"#PEC0001" {
return Err(Error::InvalidHeader("missing #PEC0001 magic".into()));
}
let pec_data = &data[8..];
let (header, stitch_offset) = parse_pec_header(pec_data)?;
let commands = decode_stitches(&pec_data[stitch_offset..])?;
// Map PEC palette indices to RGB colors
let colors: Vec<(u8, u8, u8)> = header
.color_indices
.iter()
.map(|&idx| crate::palette::pec_color(idx))
.collect();
Ok((commands, colors))
}
/// Parse a standalone PEC file and resolve to a renderable design.
pub fn parse_and_resolve(data: &[u8]) -> Result<ResolvedDesign, Error> {
let (commands, colors) = parse(data)?;
crate::resolve::resolve(&commands, colors)
}