Compare commits
	
		
			No commits in common. "a014780b5e69492496657945129e28f2e4b69012" and "a7cdc1239d9491585aaaa30d5af5044161a3191a" have entirely different histories.
		
	
	
		
			a014780b5e
			...
			a7cdc1239d
		
	
		
					 2 changed files with 42 additions and 40 deletions
				
			
		| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
{
 | 
			
		||||
  "name": "embroidery-viewer",
 | 
			
		||||
  "private": true,
 | 
			
		||||
  "version": "1.2.2",
 | 
			
		||||
  "version": "1.2.1",
 | 
			
		||||
  "type": "module",
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "dev": "vite",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -82,52 +82,54 @@ const colors = [
 | 
			
		|||
  new Color(227, 172, 129, "Bamboo"),
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
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++;
 | 
			
		||||
  }
 | 
			
		||||
function jefDecode(inputByte) {
 | 
			
		||||
  return inputByte >= 0x80 ? -(~inputByte & 0xff) - 1 : inputByte;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function jefRead(file, pattern) {
 | 
			
		||||
  file.seek(24);
 | 
			
		||||
  const colorCount = file.getInt32(file.tell(), true);
 | 
			
		||||
  const stitchCount = file.getInt32(file.tell(), true);
 | 
			
		||||
  const numberOfColors = file.getInt32(file.tell(), true);
 | 
			
		||||
  const numberOfStitches = file.getInt32(file.tell(), true);
 | 
			
		||||
  file.seek(file.tell() + 84);
 | 
			
		||||
 | 
			
		||||
  addColorsToPattern(file, pattern, colorCount);
 | 
			
		||||
  file.seek(file.tell() + (6 - colorCount) * 4);
 | 
			
		||||
  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();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  processStitches(file, pattern, stitchCount);
 | 
			
		||||
  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;
 | 
			
		||||
  }
 | 
			
		||||
  pattern.invertPatternVertical();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const jefColors = colors;
 | 
			
		||||
export const jefColors = colors;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue