summaryrefslogtreecommitdiff
path: root/src/format-readers/dst.js
blob: 8da4fd96c92bf5b0a28576ae55085c6c4792fd5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// @ts-nocheck
import { stitchTypes } from "../file-renderer/pattern";

function decodeExp(b2) {
  let returnCode = 0;
  if (b2 === 0xf3) {
    return stitchTypes.end;
  }
  if ((b2 & 0xc3) === 0xc3) {
    return stitchTypes.trim | stitchTypes.stop;
  }
  if (b2 & 0x80) {
    returnCode |= stitchTypes.trim;
  }
  if (b2 & 0x40) {
    returnCode |= stitchTypes.stop;
  }
  return returnCode;
}

export function dstRead(file, pattern) {
  let flags,
    x,
    y,
    prevJump = false,
    thisJump = false,
    b = [],
    byteCount = file.byteLength;
  file.seek(512);

  while (file.tell() < byteCount - 3) {
    b[0] = file.getUint8();
    b[1] = file.getUint8();
    b[2] = file.getUint8();
    x = 0;
    y = 0;
    if (b[0] & 0x01) {
      x += 1;
    }
    if (b[0] & 0x02) {
      x -= 1;
    }
    if (b[0] & 0x04) {
      x += 9;
    }
    if (b[0] & 0x08) {
      x -= 9;
    }
    if (b[0] & 0x80) {
      y += 1;
    }
    if (b[0] & 0x40) {
      y -= 1;
    }
    if (b[0] & 0x20) {
      y += 9;
    }
    if (b[0] & 0x10) {
      y -= 9;
    }
    if (b[1] & 0x01) {
      x += 3;
    }
    if (b[1] & 0x02) {
      x -= 3;
    }
    if (b[1] & 0x04) {
      x += 27;
    }
    if (b[1] & 0x08) {
      x -= 27;
    }
    if (b[1] & 0x80) {
      y += 3;
    }
    if (b[1] & 0x40) {
      y -= 3;
    }
    if (b[1] & 0x20) {
      y += 27;
    }
    if (b[1] & 0x10) {
      y -= 27;
    }
    if (b[2] & 0x04) {
      x += 81;
    }
    if (b[2] & 0x08) {
      x -= 81;
    }
    if (b[2] & 0x20) {
      y += 81;
    }
    if (b[2] & 0x10) {
      y -= 81;
    }
    flags = decodeExp(b[2]);
    thisJump = flags & stitchTypes.jump;
    if (prevJump) {
      flags |= stitchTypes.jump;
    }
    pattern.addStitchRel(x, y, flags, true);
    prevJump = thisJump;
  }
  pattern.addStitchRel(0, 0, stitchTypes.end, true);
  pattern.invertPatternVertical();
}