Add dst algorithm

This commit is contained in:
Leonardo Murça 2022-11-24 23:02:23 -03:00
parent 3c413170bd
commit ff80b6ddb9
3 changed files with 111 additions and 1 deletions

View file

@ -1,5 +1,6 @@
import { jDataView } from "./jdataview";
import { pesRead } from "../format-readers/pes";
import { dstRead } from "../format-readers/dst";
import { Pattern } from "./pattern";
String.prototype.endsWith = function (suffix) {
@ -12,6 +13,8 @@ function displayFileText(filename, evt, canvas) {
filename = filename.toLowerCase();
if (filename.endsWith("pes")) {
pesRead(view, pattern);
} else if (filename.endsWith("dst")) {
dstRead(view, pattern);
}
pattern.moveToPositive();
pattern.drawShape(canvas);

107
src/format-readers/dst.js Normal file
View file

@ -0,0 +1,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();
}

View file

@ -9,7 +9,7 @@
let rejectedFiles;
let areAcceptedFilesRendered = false;
const fileRequirements = {
supportedFormats: [".pes"],
supportedFormats: [".pes", ".dst"],
maxSize: 700000,
};