Refactor jef reader for .jef files

This commit is contained in:
Leonardo Murça 2025-03-11 13:54:12 -03:00
parent 5ddf1e78b0
commit 3d7c14089c

View file

@ -82,54 +82,52 @@ const colors = [
new Color(227, 172, 129, "Bamboo"), new Color(227, 172, 129, "Bamboo"),
]; ];
function jefDecode(inputByte) { const jefDecode = (byte) => (byte >= 0x80 ? -(~byte & 0xff) - 1 : byte);
return inputByte >= 0x80 ? -(~inputByte & 0xff) - 1 : inputByte; 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) { export function jefRead(file, pattern) {
file.seek(24); file.seek(24);
const numberOfColors = file.getInt32(file.tell(), true); const colorCount = file.getInt32(file.tell(), true);
const numberOfStitches = file.getInt32(file.tell(), true); const stitchCount = file.getInt32(file.tell(), true);
file.seek(file.tell() + 84); file.seek(file.tell() + 84);
for (let i = 0; i < numberOfColors; i += 1) { addColorsToPattern(file, pattern, colorCount);
pattern.addColor(colors[file.getUint32(file.tell(), true) % 78]); file.seek(file.tell() + (6 - colorCount) * 4);
}
for (let i = 0; i < 6 - numberOfColors; i += 1) {
file.getUint32();
}
let flags, processStitches(file, pattern, stitchCount);
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;
}
pattern.invertPatternVertical(); pattern.invertPatternVertical();
} }
export const jefColors = colors; export const jefColors = colors;