added all remaining formats

This commit is contained in:
2026-04-03 18:45:37 +02:00
parent 473da90b01
commit 9a367b4d10
20 changed files with 589 additions and 193 deletions
+18 -13
View File
@@ -37,8 +37,10 @@ pub fn parse(data: &[u8]) -> Result<Vec<StitchCommand>, Error> {
let b2 = stitch_data[i + 2];
i += 3;
// End of file: byte 2 bits 0 and 1 both set, and specific pattern
if b2 & 0x03 == 0x03 {
// End of file: standard DST EOF pattern (0x00, 0x00, 0xF3)
// Bits 0 and 1 of byte 2 are always set in valid DST records,
// so we must check the full EOF pattern, not just those bits.
if b0 == 0x00 && b1 == 0x00 && b2 == 0xF3 {
commands.push(StitchCommand::End);
break;
}
@@ -46,14 +48,17 @@ pub fn parse(data: &[u8]) -> Result<Vec<StitchCommand>, Error> {
let dx = decode_dx(b0, b1, b2);
let dy = decode_dy(b0, b1, b2);
// Mask off the always-set bits 0,1 to get control flags
let flags = b2 & 0xFC;
// Color change: byte 2 bit 7
if b2 & 0x80 != 0 {
if flags & 0x80 != 0 {
commands.push(StitchCommand::ColorChange);
continue;
}
// Jump: byte 2 bit 6
if b2 & 0x40 != 0 {
if flags & 0x40 != 0 {
commands.push(StitchCommand::Jump { dx, dy });
continue;
}
@@ -166,8 +171,8 @@ mod tests {
#[test]
fn decode_end_marker() {
// b2 = 0x03 means end
let data = [0x00, 0x00, 0x03];
// DST EOF = 0x00, 0x00, 0xF3
let data = [0x00, 0x00, 0xF3];
let cmds = parse(&data).unwrap_err();
assert!(matches!(cmds, Error::NoStitchData));
}
@@ -175,9 +180,9 @@ mod tests {
#[test]
fn decode_simple_stitch() {
// A normal stitch followed by end
// dx=+1: b0 bit 0. dy=+1: b0 bit 7. b2=0x00 (normal stitch)
// Then end marker
let data = [0x81, 0x00, 0x00, 0x00, 0x00, 0x03];
// dx=+1: b0 bit 0. dy=+1: b0 bit 7. b2=0x03 (always-set bits)
// Then end marker 0x00, 0x00, 0xF3
let data = [0x81, 0x00, 0x03, 0x00, 0x00, 0xF3];
let cmds = parse(&data).unwrap();
assert!(matches!(cmds[0], StitchCommand::Stitch { dx: 1, dy: -1 }));
assert!(matches!(cmds[1], StitchCommand::End));
@@ -185,16 +190,16 @@ mod tests {
#[test]
fn decode_jump() {
// b2 bit 6 = jump
let data = [0x01, 0x00, 0x40, 0x00, 0x00, 0x03];
// b2 bit 6 = jump, with always-set bits 0,1
let data = [0x01, 0x00, 0x43, 0x00, 0x00, 0xF3];
let cmds = parse(&data).unwrap();
assert!(matches!(cmds[0], StitchCommand::Jump { dx: 1, dy: 0 }));
}
#[test]
fn decode_color_change() {
// b2 bit 7 = color change
let data = [0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03];
// b2 bit 7 = color change, with always-set bits 0,1
let data = [0x00, 0x00, 0x83, 0x01, 0x00, 0x03, 0x00, 0x00, 0xF3];
let cmds = parse(&data).unwrap();
assert!(matches!(cmds[0], StitchCommand::ColorChange));
}