Merge pull request 'Refactor jef reader for .jef files' (#2) from refactor_jef into main
All checks were successful
Deploy / deploy (push) Successful in 16s

Reviewed-on: #2
This commit is contained in:
Leonardo Murça 2025-03-11 19:50:52 +00:00
commit a014780b5e
2 changed files with 40 additions and 42 deletions

View file

@ -1,7 +1,7 @@
{
"name": "embroidery-viewer",
"private": true,
"version": "1.2.1",
"version": "1.2.2",
"type": "module",
"scripts": {
"dev": "vite",

View file

@ -82,54 +82,52 @@ const colors = [
new Color(227, 172, 129, "Bamboo"),
];
function jefDecode(inputByte) {
return inputByte >= 0x80 ? -(~inputByte & 0xff) - 1 : inputByte;
const jefDecode = (byte) => (byte >= 0x80 ? -(~byte & 0xff) - 1 : byte);
const isSpecialStitch = (byte) => byte === 0x80;
const isStopOrTrim = (byte) => (byte & 0x01) !== 0 || byte === 0x02 || byte === 0x04;
const isEndOfPattern = (byte) => byte === 0x10;
const isStop = (byte) => byte & 0x01;
const readStitchData = (file) => ({ byte1: file.getUint8(), byte2: file.getUint8() });
const addColorsToPattern = (file, pattern, colorCount) => {
for (let i = 0; i < colorCount; i++) {
pattern.addColor(colors[file.getUint32(file.tell(), true) % colors.length]);
}
};
const determineStitchType = (file, byte1, byte2) => {
if (isSpecialStitch(byte1)) {
if (isStopOrTrim(byte2)) {
return { type: isStop(byte2) ? stitchTypes.stop : stitchTypes.trim, byte1: file.getUint8(), byte2: file.getUint8() };
} else if (isEndOfPattern(byte2)) {
return { type: stitchTypes.end, byte1: 0, byte2: 0, end: true };
}
}
return { type: stitchTypes.normal, byte1, byte2 };
}
const processStitches = (file, pattern, stitchCount) => {
let stitchesProcessed = 0;
while (stitchesProcessed < stitchCount + 100) {
let { byte1, byte2 } = readStitchData(file);
let { type, byte1: decodedByte1, byte2: decodedByte2, end } = determineStitchType(file, byte1, byte2);
pattern.addStitchRel(jefDecode(decodedByte1), jefDecode(decodedByte2), type, true);
if (end) break;
stitchesProcessed++;
}
}
export function jefRead(file, pattern) {
file.seek(24);
const numberOfColors = file.getInt32(file.tell(), true);
const numberOfStitches = file.getInt32(file.tell(), true);
const colorCount = file.getInt32(file.tell(), true);
const stitchCount = file.getInt32(file.tell(), true);
file.seek(file.tell() + 84);
for (let i = 0; i < numberOfColors; i += 1) {
pattern.addColor(colors[file.getUint32(file.tell(), true) % 78]);
}
for (let i = 0; i < 6 - numberOfColors; i += 1) {
file.getUint32();
}
addColorsToPattern(file, pattern, colorCount);
file.seek(file.tell() + (6 - colorCount) * 4);
let flags,
b0,
b1,
dx,
dy,
stitchCount = 0;
while (stitchCount < numberOfStitches + 100) {
flags = stitchTypes.normal;
b0 = file.getUint8();
b1 = file.getUint8();
if (b0 === 0x80) {
if (b1 & 0x01) {
b0 = file.getUint8();
b1 = file.getUint8();
flags = stitchTypes.stop;
} else if (b1 === 0x02 || b1 === 0x04) {
b0 = file.getUint8();
b1 = file.getUint8();
flags = stitchTypes.trim;
} else if (b1 === 0x10) {
pattern.addStitchRel(0, 0, stitchTypes.end, true);
break;
}
}
dx = jefDecode(b0);
dy = jefDecode(b1);
pattern.addStitchRel(dx, dy, flags, true);
stitchCount += 1;
}
processStitches(file, pattern, stitchCount);
pattern.invertPatternVertical();
}
export const jefColors = colors;
export const jefColors = colors;