/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readMit(EmbPattern* pattern, const char* fileName) { unsigned char data[2]; EmbFile* file = 0; if(!pattern) { embLog_error("format-mit.c readMit(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-mit.c readMit(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-mit.c readMit(), cannot open %s for reading\n", fileName); return 0; } /* embPattern_loadExternalColorFile(pattern, fileName); TODO: review this and uncomment or remove it */ while(binaryReadBytes(file, data, 2) == 2) { embPattern_addStitchRel(pattern, mitDecodeStitch(data[0]) / 10.0, mitDecodeStitch(data[1]) / 10.0, NORMAL, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch && pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; }
/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writePec(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; if(!embStitchList_count(pattern->stitchList)) { embLog_error("format-pec.c writePec(), pattern contains no stitches\n"); return 0; } /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); file = embFile_open(fileName, "wb"); if(!file) { embLog_error("format-pec.c writePec(), cannot open %s for writing\n", fileName); return 0; } embPattern_flipVertical(pattern); /* TODO: There needs to be a matching flipVertical() call after the write to ensure multiple writes from the same pattern work properly */ embPattern_fixColorCount(pattern); embPattern_correctForMaxStitchLength(pattern,12.7, 204.7); embPattern_scale(pattern, 10.0); binaryWriteBytes(file, "#PEC0001", 8); writePecStitches(pattern, file, fileName); embFile_close(file); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readPec(EmbPattern* pattern, const char* fileName) { unsigned int graphicsOffset; unsigned char colorChanges; int i; EmbFile* file = 0; if(!pattern) { embLog_error("format-pec.c readPec(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-pec.c readPec(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-pec.c readPec(), cannot open %s for reading\n", fileName); return 0; } embFile_seek(file, 0x38, SEEK_SET); colorChanges = (unsigned char)binaryReadByte(file); for(i = 0; i <= colorChanges; i++) { embPattern_addThread(pattern, pecThreads[binaryReadByte(file) % 65]); } /* Get Graphics offset */ embFile_seek(file, 0x20A, SEEK_SET); graphicsOffset = (unsigned int)(binaryReadUInt8(file)); graphicsOffset |= (binaryReadUInt8(file) << 8); graphicsOffset |= (binaryReadUInt8(file) << 16); (void)binaryReadByte(file); /* 0x31 */ (void)binaryReadByte(file); /* 0xFF */ (void)binaryReadByte(file); /* 0xF0 */ /* Get X and Y size in .1 mm */ /* 0x210 */ binaryReadInt16(file); /* x size */ binaryReadInt16(file); /* y size */ binaryReadInt16(file); /* 0x01E0 */ binaryReadInt16(file); /* 0x01B0 */ binaryReadInt16(file); /* distance left from start */ binaryReadInt16(file); /* distance up from start */ /* Begin Stitch Data */ /* 0x21C */ /*unsigned int end = graphicsOffset + 0x208; */ readPecStitches(pattern, file); embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); embPattern_flipVertical(pattern); return 1; }
/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writeExp(EmbPattern* pattern, const char* fileName) { #ifdef ARDUINO /* ARDUINO TODO: This is temporary. Remove when complete. */ return 0; /* ARDUINO TODO: This is temporary. Remove when complete. */ #else /* ARDUINO TODO: This is temporary. Remove when complete. */ EmbFile* file = 0; EmbStitchList* stitches = 0; double dx = 0.0, dy = 0.0; double xx = 0.0, yy = 0.0; int flags = 0; unsigned char b[4]; if(!pattern) { embLog_error("format-exp.c writeExp(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-exp.c writeExp(), fileName argument is null\n"); return 0; } if(!embStitchList_count(pattern->stitchList)) { embLog_error("format-exp.c writeExp(), pattern contains no stitches\n"); return 0; } /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); file = embFile_open(fileName, "wb"); if(!file) { embLog_error("format-exp.c writeExp(), cannot open %s for writing\n", fileName); return 0; } /* write stitches */ stitches = pattern->stitchList; while(stitches) { dx = stitches->stitch.xx * 10.0 - xx; dy = stitches->stitch.yy * 10.0 - yy; xx = stitches->stitch.xx * 10.0; yy = stitches->stitch.yy * 10.0; flags = stitches->stitch.flags; expEncode(b, (char)roundDouble(dx), (char)roundDouble(dy), flags); if((b[0] == 128) && ((b[1] == 1) || (b[1] == 2) || (b[1] == 4))) { embFile_printf(file, "%c%c%c%c", b[0], b[1], b[2], b[3]); } else { embFile_printf(file, "%c%c", b[0], b[1]); } stitches = stitches->next; } embFile_printf(file, "\x1a"); embFile_close(file); return 1; #endif /* ARDUINO TODO: This is temporary. Remove when complete. */ }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int read10o(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; if(!pattern) { embLog_error("format-10o.c read10o(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-10o.c read10o(), fileName argument is null\n"); return 0; } file = embFile_open(fileName,"rb"); if(!file) { embLog_error("format-10o.c read10o(), cannot open %s for reading\n", fileName); return 0; } embPattern_loadExternalColorFile(pattern, fileName); while(1) { int x, y; int stitchType = NORMAL; unsigned char ctrl = (unsigned char)embFile_getc(file); if(embFile_eof(file)) break; y = embFile_getc(file); if(embFile_eof(file)) break; x = embFile_getc(file); if(embFile_eof(file)) break; if(ctrl & 0x20) x = -x; if(ctrl & 0x40) y = -y; if(ctrl & 0x01) stitchType = TRIM; if((ctrl & 0x5) == 5) { stitchType = STOP; } if(ctrl == 0xF8 || ctrl == 0x91 || ctrl == 0x87) { embPattern_addStitchRel(pattern, 0, 0, END, 1); break; } embPattern_addStitchRel(pattern, x / 10.0, y / 10.0, stitchType, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readDsb(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; if(!pattern) { embLog_error("format-dsb.c readDsb(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-dsb.c readDsb(), fileName argument is null\n"); return 0; } file = embFile_open(fileName,"rb"); if(!file) { embLog_error("format-dsb.c readDsb(), cannot open %s for reading\n", fileName); return 0; } embPattern_loadExternalColorFile(pattern, fileName); /*TODO: READ 512 BYTE HEADER INTO header[] */ /* for(i = 0; i < 512; i++) { header[i] = embFile_getc(file); } */ embFile_seek(file, 0x200, SEEK_SET); while(1) { int x, y; unsigned char ctrl; int stitchType = NORMAL; ctrl =(unsigned char)embFile_getc(file); if(embFile_eof(file)) break; y = embFile_getc(file); if(embFile_eof(file)) break; x = embFile_getc(file); if(embFile_eof(file)) break; if(ctrl & 0x01) stitchType = TRIM; if(ctrl & 0x20) x = -x; if(ctrl & 0x40) y = -y; /* ctrl & 0x02 - Speed change? */ /* TODO: review this line */ /* ctrl & 0x04 - Clutch? */ /* TODO: review this line */ if((ctrl & 0x05) == 0x05) { stitchType = STOP; } if(ctrl == 0xF8 || ctrl == 0x91 || ctrl == 0x87) { embPattern_addStitchRel(pattern, 0, 0, END, 1); break; } embPattern_addStitchRel(pattern, x / 10.0, y / 10.0, stitchType, 1); } embFile_close(file); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readSst(EmbPattern* pattern, const char* fileName) { int fileLength; EmbFile* file = 0; if(!pattern) { embLog_error("format-sst.c readSst(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-sst.c readSst(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-sst.c readSst(), cannot open %s for reading\n", fileName); return 0; } embPattern_loadExternalColorFile(pattern, fileName); embFile_seek(file, 0, SEEK_END); fileLength = embFile_tell(file); embFile_seek(file, 0xA0, SEEK_SET); /* skip the all zero header */ while(embFile_tell(file) < fileLength) { int stitchType = NORMAL; int b1 = (int) binaryReadByte(file); int b2 = (int) binaryReadByte(file); unsigned char commandByte = binaryReadByte(file); if(commandByte == 0x04) { embPattern_addStitchRel(pattern, 0, 0, END, 1); break; } if((commandByte & 0x01) == 0x01) stitchType = STOP; if((commandByte & 0x02) == 0x02) stitchType = JUMP; if((commandByte & 0x10) != 0x10) b2 = -b2; if((commandByte & 0x40) == 0x40) b1 = -b1; embPattern_addStitchRel(pattern, b1 / 10.0, b2 / 10.0, stitchType, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; /*TODO: finish readSst */ }
/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writeT01(EmbPattern* pattern, const char* fileName) { EmbRect boundingRect; EmbFile* file = 0; int xx, yy, dx, dy, flags; int co = 1, st = 0; int ax, ay, mx, my; EmbStitchList* pointer = 0; if (!embStitchList_count(pattern->stitchList)) { embLog_error("format-t01.c writeDst(), pattern contains no stitches\n"); return 0; } /* Check for an END stitch and add one if it is not present */ if (pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); file = embFile_open(fileName, "wb"); if (!file) { embLog_error("format-t01.c writet01(), cannot open %s for writing\n", fileName); return 0; } embPattern_correctForMaxStitchLength(pattern, 12.1, 12.1); xx = yy = 0; co = 1; co = embThreadList_count(pattern->threadList); st = 0; st = embStitchList_count(pattern->stitchList); flags = NORMAL; boundingRect = embPattern_calcBoundingBox(pattern); ax = ay = mx = my = 0; xx = yy = 0; pointer = pattern->stitchList; while (pointer) { /* convert from mm to 0.1mm for file format */ dx = roundDouble(pointer->stitch.xx * 10.0) - xx; dy = roundDouble(pointer->stitch.yy * 10.0) - yy; xx = roundDouble(pointer->stitch.xx * 10.0); yy = roundDouble(pointer->stitch.yy * 10.0); flags = pointer->stitch.flags; encode_record(file, dx, dy, flags); pointer = pointer->next; } embFile_close(file); return 1; }
/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writeKsm(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; /* TODO: change writeKsm() to use EmbFile */ EmbStitchList* pointer = 0; double xx = 0, yy = 0, dx = 0, dy = 0; int flags = 0; int i = 0; unsigned char b[4]; if(!pattern) { embLog_error("format-ksm.c writeKsm(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-ksm.c writeKsm(), fileName argument is null\n"); return 0; } if(!embStitchList_count(pattern->stitchList)) { embLog_error("format-ksm.c writeKsm(), pattern contains no stitches\n"); return 0; } /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); file = embFile_open(fileName, "wb"); if(!file) { embLog_error("format-ksm.c writeKsm(), cannot open %s for writing\n", fileName); return 0; } for(i = 0; i < 0x80; i++) { binaryWriteInt(file, 0); } /* write stitches */ xx = yy = 0; pointer = pattern->stitchList; while(pointer) { dx = pointer->stitch.xx - xx; dy = pointer->stitch.yy - yy; xx = pointer->stitch.xx; yy = pointer->stitch.yy; flags = pointer->stitch.flags; ksmEncode(b, (char)(dx * 10.0), (char)(dy * 10.0), flags); embFile_printf(file, "%c%c", b[0], b[1]); pointer = pointer->next; } embFile_printf(file, "\x1a"); embFile_close(file); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readFxy(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; if(!pattern) { embLog_error("format-fxy.c readFxy(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-fxy.c readFxy(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-fxy.c readFxy(), cannot open %s for reading\n", fileName); return 0; } embPattern_loadExternalColorFile(pattern, fileName); embFile_seek(file, 0x100, SEEK_SET); /* TODO: review for combining code. This line appears to be the only difference from the GT format. */ while(!embFile_eof(file)) { int stitchType = NORMAL; int b1 = (int)binaryReadByte(file); int b2 = (int)binaryReadByte(file); unsigned char commandByte = binaryReadByte(file); if(commandByte == 0x91) { embPattern_addStitchRel(pattern, 0, 0, END, 1); break; } if((commandByte & 0x01) == 0x01) stitchType = TRIM; if((commandByte & 0x02) == 0x02) stitchType = STOP; if((commandByte & 0x20) == 0x20) b1 = -b1; if((commandByte & 0x40) == 0x40) b2 = -b2; embPattern_addStitchRel(pattern, b2 / 10.0, b1 / 10.0, stitchType, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch && pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readKsm(EmbPattern* pattern, const char* fileName) { int prevStitchType = NORMAL; char b[3]; EmbFile* file = 0; if(!pattern) { embLog_error("format-ksm.c readKsm(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-ksm.c readKsm(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-ksm.c readKsm(), cannot open %s for reading\n", fileName); return 0; } embFile_seek(file, 0x200, SEEK_SET); while(embFile_read(b, 1, 3, file) == 3) { int flags = NORMAL; if(((prevStitchType & 0x08) == 0x08) && (b[2] & 0x08) == 0x08) { flags = STOP; } else if((b[2] & 0x1F) != 0) { flags = TRIM; } prevStitchType = b[2]; if(b[2] & 0x40) b[1] = -b[1]; if(b[2] & 0x20) b[0] = -b[0]; embPattern_addStitchRel(pattern, b[1] / 10.0, b[0] / 10.0, flags, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; }
/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writeMit(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; EmbStitchList* pointer = 0; double xx = 0, yy = 0, dx = 0, dy = 0; int flags = 0; if(!pattern) { embLog_error("format-mit.c writeMit(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-mit.c writeMit(), fileName argument is null\n"); return 0; } if(!embStitchList_count(pattern->stitchList)) { embLog_error("format-mit.c writeMit(), pattern contains no stitches\n"); return 0; } /* Check for an END stitch and add one if it is not present */ if (pattern->lastStitch->stitch.flags != END) { embPattern_addStitchRel(pattern, 0, 0, END, 1); } file = embFile_open(fileName, "wb"); if (!file) { embLog_error("format-mit.c writeMit(), cannot open %s for writing\n", fileName); return 0; } embPattern_correctForMaxStitchLength(pattern, 0x1F, 0x1F); xx = yy = 0; pointer = pattern->stitchList; while (pointer) { dx = pointer->stitch.xx - xx; dy = pointer->stitch.yy - yy; xx = pointer->stitch.xx; yy = pointer->stitch.yy; flags = pointer->stitch.flags; embFile_putc(mitEncodeStitch(dx), file); embFile_putc(mitEncodeStitch(dy), file); pointer = pointer->next; } embFile_close(file); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readOfm(EmbPattern* pattern, const char* fileName) { int unknownCount = 0; int key = 0, classNameLength; char* s = 0; EmbFile* fileCompound = 0; EmbFile* file = 0; bcf_file* bcfFile = 0; if(!pattern) { embLog_error("format-ofm.c readOfm(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-ofm.c readOfm(), fileName argument is null\n"); return 0; } fileCompound = embFile_open(fileName, "rb"); if(!fileCompound) { embLog_error("format-ofm.c readOfm(), cannot open %s for reading\n", fileName); return 0; } bcfFile = (bcf_file*)malloc(sizeof(bcf_file)); if(!bcfFile) { embLog_error("format-ofm.c readOfm(), unable to allocate memory for bcfFile\n"); return 0; } bcfFile_read(fileCompound, bcfFile); file = GetFile(bcfFile, fileCompound, "EdsIV Object"); bcf_file_free(bcfFile); bcfFile = 0; embFile_seek(file, 0x1C6, SEEK_SET); ofmReadThreads(file, pattern); embFile_seek(file, 0x110, SEEK_CUR); binaryReadInt32(file); classNameLength = binaryReadInt16(file); s = (char*)malloc(sizeof(char) * classNameLength); if(!s) { embLog_error("format-ofm.c readOfm(), unable to allocate memory for s\n"); return 0; } binaryReadBytes(file, (unsigned char*)s, classNameLength); /* TODO: check return value */ unknownCount = binaryReadInt16(file); /* TODO: determine what unknown count represents */ binaryReadInt16(file); key = ofmReadClass(file); while(1) { if(key == 0xFEFF) { break; } if(key == 0x809C) { ofmReadExpanded(file, pattern); } else { ofmReadColorChange(file, pattern); } key = binaryReadUInt16(file); if(key == 0xFFFF) { ofmReadClass(file); } } embFile_close(fileCompound); embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); embPattern_flip(pattern, 1, 1); return 1; }
/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writeCsv(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; EmbStitchList* sList = 0; EmbThreadList* tList = 0; EmbRect boundingRect; int i = 0; int stitchCount = 0; int threadCount = 0; if(!pattern) { embLog_error("format-csv.c writeCsv(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-csv.c writeCsv(), fileName argument is null\n"); return 0; } sList = pattern->stitchList; stitchCount = embStitchList_count(sList); tList = pattern->threadList; threadCount = embThreadList_count(tList); boundingRect = embPattern_calcBoundingBox(pattern); if(!stitchCount) { embLog_error("format-csv.c writeCsv(), pattern contains no stitches\n"); return 0; } /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) { embPattern_addStitchRel(pattern, 0, 0, END, 1); stitchCount++; } file = embFile_open(fileName, "w"); if(!file) { embLog_error("format-csv.c writeCsv(), cannot open %s for writing\n", fileName); return 0; } /* write header */ embFile_printf(file, "\"#\",\"Embroidermodder 2 CSV Embroidery File\"\n"); embFile_printf(file, "\"#\",\"http://embroidermodder.github.io\"\n"); embFile_printf(file, "\n"); embFile_printf(file, "\"#\",\"General Notes:\"\n"); embFile_printf(file, "\"#\",\"This file can be read by Excel or LibreOffice as CSV (Comma Separated Value) or with a text editor.\"\n"); embFile_printf(file, "\"#\",\"Lines beginning with # are comments.\"\n"); embFile_printf(file, "\"#\",\"Lines beginning with > are variables: [VAR_NAME], [VAR_VALUE]\"\n"); embFile_printf(file, "\"#\",\"Lines beginning with $ are threads: [THREAD_NUMBER], [RED], [GREEN], [BLUE], [DESCRIPTION], [CATALOG_NUMBER]\"\n"); embFile_printf(file, "\"#\",\"Lines beginning with * are stitch entries: [STITCH_TYPE], [X], [Y]\"\n"); embFile_printf(file, "\n"); embFile_printf(file, "\"#\",\"Stitch Entry Notes:\"\n"); embFile_printf(file, "\"#\",\"STITCH instructs the machine to move to the position [X][Y] and then make a stitch.\"\n"); embFile_printf(file, "\"#\",\"JUMP instructs the machine to move to the position [X][Y] without making a stitch.\"\n"); embFile_printf(file, "\"#\",\"TRIM instructs the machine to cut the thread before moving to the position [X][Y] without making a stitch.\"\n"); embFile_printf(file, "\"#\",\"COLOR instructs the machine to stop temporarily so that the user can change to a different color thread before resuming.\"\n"); embFile_printf(file, "\"#\",\"END instructs the machine that the design is completed and there are no further instructions.\"\n"); embFile_printf(file, "\"#\",\"UNKNOWN encompasses instructions that may not be supported currently.\"\n"); embFile_printf(file, "\"#\",\"[X] and [Y] are absolute coordinates in millimeters (mm).\"\n"); embFile_printf(file, "\n"); /* write variables */ embFile_printf(file,"\"#\",\"[VAR_NAME]\",\"[VAR_VALUE]\"\n"); embFile_printf(file, "\">\",\"STITCH_COUNT:\",\"%u\"\n", (unsigned int)stitchCount); embFile_printf(file, "\">\",\"THREAD_COUNT:\",\"%u\"\n", (unsigned int)threadCount); embFile_printf(file, "\">\",\"EXTENTS_LEFT:\",\"%f\"\n", boundingRect.left); embFile_printf(file, "\">\",\"EXTENTS_TOP:\",\"%f\"\n", boundingRect.top); embFile_printf(file, "\">\",\"EXTENTS_RIGHT:\",\"%f\"\n", boundingRect.right); embFile_printf(file, "\">\",\"EXTENTS_BOTTOM:\",\"%f\"\n", boundingRect.bottom); embFile_printf(file, "\">\",\"EXTENTS_WIDTH:\",\"%f\"\n", embRect_width(boundingRect)); embFile_printf(file, "\">\",\"EXTENTS_HEIGHT:\",\"%f\"\n", embRect_height(boundingRect)); embFile_printf(file,"\n"); /* write colors */ embFile_printf(file, "\"#\",\"[THREAD_NUMBER]\",\"[RED]\",\"[GREEN]\",\"[BLUE]\",\"[DESCRIPTION]\",\"[CATALOG_NUMBER]\"\n"); i = 1; while(tList) { embFile_printf(file, "\"$\",\"%d\",\"%d\",\"%d\",\"%d\",\"%s\",\"%s\"\n", i, /* TODO: fix segfault that backtraces here when libembroidery-convert from dst to csv. */ (int)tList->thread.color.r, (int)tList->thread.color.g, (int)tList->thread.color.b, tList->thread.description, tList->thread.catalogNumber); i++; tList = tList->next; } embFile_printf(file, "\n"); /* write stitches */ embFile_printf(file, "\"#\",\"[STITCH_TYPE]\",\"[X]\",\"[Y]\"\n"); while(sList) { EmbStitch s = sList->stitch; embFile_printf(file, "\"*\",\"%s\",\"%f\",\"%f\"\n", csvStitchFlagToStr(s.flags), s.xx, s.yy); sList = sList->next; } embFile_close(file); return 1; }
/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writeVp3(EmbPattern* pattern, const char* fileName) { EmbFile *file = 0; EmbRect bounds; int remainingBytesPos, remainingBytesPos2; int colorSectionStitchBytes; int first = 1; int numberOfColors = 0; EmbColor color; EmbStitchList *mainPointer = 0, *pointer = 0; if(!pattern) { embLog_error("format-vp3.c writeVp3(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-vp3.c writeVp3(), fileName argument is null\n"); return 0; } if(!embStitchList_count(pattern->stitchList)) { embLog_error("format-vp3.c writeVp3(), pattern contains no stitches\n"); return 0; } bounds = embPattern_calcBoundingBox(pattern); file = embFile_open(fileName, "wb"); if(!file) { embLog_error("format-vp3.c writeVp3(), cannot open %s for writing\n", fileName); return 0; } embPattern_correctForMaxStitchLength(pattern, 3200.0, 3200.0); /* VP3 can encode signed 16bit deltas */ embPattern_flipVertical(pattern); binaryWriteBytes(file, "%vsm%", 5); binaryWriteByte(file, 0); vp3WriteString(file, "Embroidermodder"); binaryWriteByte(file, 0); binaryWriteByte(file, 2); binaryWriteByte(file, 0); remainingBytesPos = embFile_tell(file); binaryWriteInt(file, 0); /* placeholder */ vp3WriteString(file, ""); binaryWriteIntBE(file, bounds.right * 1000); binaryWriteIntBE(file, bounds.bottom * 1000); binaryWriteIntBE(file, bounds.left * 1000); binaryWriteIntBE(file, bounds.top * 1000); binaryWriteInt(file, 0); /* this would be some (unknown) function of thread length */ binaryWriteByte(file, 0); numberOfColors = 0; mainPointer = pattern->stitchList; while(mainPointer) { int flag; EmbColor newColor; pointer = mainPointer; flag = pointer->stitch.flags; newColor = embThreadList_getAt(pattern->threadList, pointer->stitch.color).color; if(newColor.r != color.r || newColor.g != color.g || newColor.b != color.b) { numberOfColors++; color.r = newColor.r; color.g = newColor.g; color.b = newColor.b; } else if(flag & END || flag & STOP) { numberOfColors++; } while(pointer && (flag == pointer->stitch.flags)) { pointer = pointer->next; } mainPointer = pointer; } binaryWriteByte(file, numberOfColors); binaryWriteByte(file, 12); binaryWriteByte(file, 0); binaryWriteByte(file, 1); binaryWriteByte(file, 0); binaryWriteByte(file, 3); binaryWriteByte(file, 0); remainingBytesPos2 = embFile_tell(file); binaryWriteInt(file, 0); /* placeholder */ binaryWriteIntBE(file, 0); /* origin X */ binaryWriteIntBE(file, 0); /* origin Y */ binaryWriteByte(file, 0); binaryWriteByte(file, 0); binaryWriteByte(file, 0); binaryWriteIntBE(file, bounds.right * 1000); binaryWriteIntBE(file, bounds.bottom * 1000); binaryWriteIntBE(file, bounds.left * 1000); binaryWriteIntBE(file, bounds.top * 1000); binaryWriteIntBE(file, (bounds.right - bounds.left) * 1000); binaryWriteIntBE(file, (bounds.bottom - bounds.top) * 1000); vp3WriteString(file, ""); binaryWriteShortBE(file, 25700); binaryWriteIntBE(file, 4096); binaryWriteIntBE(file, 0); binaryWriteIntBE(file, 0); binaryWriteIntBE(file, 4096); binaryWriteBytes(file, "xxPP\x01\0", 6); vp3WriteString(file, ""); binaryWriteShortBE(file, numberOfColors); mainPointer = pattern->stitchList; while(mainPointer) { char colorName[8] = { 0 }; double lastX, lastY; int colorSectionLengthPos; EmbStitch s; int lastColor; if(!first) binaryWriteByte(file, 0); binaryWriteByte(file, 0); binaryWriteByte(file, 5); binaryWriteByte(file, 0); colorSectionLengthPos = embFile_tell(file); binaryWriteInt(file, 0); /* placeholder */ pointer = mainPointer; color = embThreadList_getAt(pattern->threadList, pointer->stitch.color).color; if(first && pointer->stitch.flags & JUMP && pointer->next->stitch.flags & JUMP) pointer = pointer->next; s = pointer->stitch; embLog_print("format-vp3.c DEBUG %d, %lf, %lf\n", s.flags, s.xx, s.yy); binaryWriteIntBE(file, s.xx * 1000); binaryWriteIntBE(file, -s.yy * 1000); pointer = pointer->next; first = 0; lastX = s.xx; lastY = s.yy; lastColor = s.color; binaryWriteByte(file, 1); binaryWriteByte(file, 0); embLog_print("format-vp3.c writeVp3(), switching to color (%d, %d, %d)\n", color.r, color.g, color.b); binaryWriteByte(file, color.r); binaryWriteByte(file, color.g); binaryWriteByte(file, color.b); binaryWriteByte(file, 0); binaryWriteByte(file, 0); binaryWriteByte(file, 0); binaryWriteByte(file, 5); binaryWriteByte(file, 40); vp3WriteString(file, ""); sprintf(colorName, "#%02x%02x%02x", color.b, color.g, color.r); vp3WriteString(file, colorName); vp3WriteString(file, ""); binaryWriteIntBE(file, 0); binaryWriteIntBE(file, 0); vp3WriteStringLen(file, "\0", 1); colorSectionStitchBytes = embFile_tell(file); binaryWriteInt(file, 0); /* placeholder */ binaryWriteByte(file, 10); binaryWriteByte(file, 246); binaryWriteByte(file, 0); while(pointer) { int dx, dy; EmbStitch s = pointer->stitch; if(s.color != lastColor) break; if(s.flags & END || s.flags & STOP) break; dx = (s.xx - lastX) * 10; dy = (s.yy - lastY) * 10; lastX = lastX + dx / 10.0; /* output is in ints, ensure rounding errors do not sum up */ lastY = lastY + dy / 10.0; if(dx < -127 || dx > 127 || dy < -127 || dy > 127) { binaryWriteByte(file, 128); binaryWriteByte(file, 1); binaryWriteShortBE(file, dx); binaryWriteShortBE(file, dy); binaryWriteByte(file, 128); binaryWriteByte(file, 2); } else { binaryWriteByte(file, dx); binaryWriteByte(file, dy); } pointer = pointer->next; } vp3PatchByteCount(file, colorSectionStitchBytes, -4); vp3PatchByteCount(file, colorSectionLengthPos, -3); mainPointer = pointer; } vp3PatchByteCount(file, remainingBytesPos2, -4); vp3PatchByteCount(file, remainingBytesPos, -4); embFile_close(file); embPattern_flipVertical(pattern); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readVp3(EmbPattern* pattern, const char* fileName) { unsigned char magicString[5]; unsigned char some; unsigned char* softwareVendorString = 0; unsigned char v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18; unsigned char* anotherSoftwareVendorString = 0; int numberOfColors; long colorSectionOffset; unsigned char magicCode[6]; short someShort; unsigned char someByte; int bytesRemainingInFile; unsigned char* fileCommentString = 0; /* some software writes used settings here */ int hoopConfigurationOffset; unsigned char* anotherCommentString = 0; int i; EmbFile* file = 0; if(!pattern) { embLog_error("format-vp3.c readVp3(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-vp3.c readVp3(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-vp3.c readVp3(), cannot open %s for reading\n", fileName); return 0; } binaryReadBytes(file, magicString, 5); /* %vsm% */ /* TODO: check return value */ some = binaryReadByte(file); /* 0 */ softwareVendorString = vp3ReadString(file); someShort = binaryReadInt16(file); someByte = binaryReadByte(file); bytesRemainingInFile = binaryReadInt32(file); fileCommentString = vp3ReadString(file); hoopConfigurationOffset = (int)embFile_tell(file); vp3ReadHoopSection(file); anotherCommentString = vp3ReadString(file); /* TODO: review v1 thru v18 variables and use emb_unused() if needed */ v1 = binaryReadByte(file); v2 = binaryReadByte(file); v3 = binaryReadByte(file); v4 = binaryReadByte(file); v5 = binaryReadByte(file); v6 = binaryReadByte(file); v7 = binaryReadByte(file); v8 = binaryReadByte(file); v9 = binaryReadByte(file); v10 = binaryReadByte(file); v11 = binaryReadByte(file); v12 = binaryReadByte(file); v13 = binaryReadByte(file); v14 = binaryReadByte(file); v15 = binaryReadByte(file); v16 = binaryReadByte(file); v17 = binaryReadByte(file); v18 = binaryReadByte(file); binaryReadBytes(file, magicCode, 6); /* 0x78 0x78 0x55 0x55 0x01 0x00 */ /* TODO: check return value */ anotherSoftwareVendorString = vp3ReadString(file); numberOfColors = binaryReadInt16BE(file); embLog_error("format-vp3.c Number of Colors: %d\n", numberOfColors); colorSectionOffset = (int)embFile_tell(file); for(i = 0; i < numberOfColors; i++) { EmbThread t; char tableSize; int startX, startY, offsetToNextColorX, offsetToNextColorY; unsigned char* threadColorNumber, *colorName, *threadVendor; int unknownThreadString, numberOfBytesInColor; embFile_seek(file, colorSectionOffset, SEEK_SET); embLog_error("format-vp3.c Color Check Byte #1: 0 == %d\n", binaryReadByte(file)); embLog_error("format-vp3.c Color Check Byte #2: 5 == %d\n", binaryReadByte(file)); embLog_error("format-vp3.c Color Check Byte #3: 0 == %d\n", binaryReadByte(file)); colorSectionOffset = binaryReadInt32BE(file); colorSectionOffset += embFile_tell(file); startX = binaryReadInt32BE(file); startY = binaryReadInt32BE(file); embPattern_addStitchAbs(pattern, startX / 1000, -startY / 1000, JUMP, 0); tableSize = binaryReadByte(file); binaryReadByte(file); t.color.r = binaryReadByte(file); t.color.g = binaryReadByte(file); t.color.b = binaryReadByte(file); embPattern_addThread(pattern, t); embFile_seek(file, 6*tableSize - 1, SEEK_CUR); threadColorNumber = vp3ReadString(file); colorName = vp3ReadString(file); threadVendor = vp3ReadString(file); offsetToNextColorX = binaryReadInt32BE(file); offsetToNextColorY = binaryReadInt32BE(file); unknownThreadString = binaryReadInt16BE(file); embFile_seek(file, unknownThreadString, SEEK_CUR); numberOfBytesInColor = binaryReadInt32BE(file); embFile_seek(file, 0x3, SEEK_CUR); while(embFile_tell(file) < colorSectionOffset - 1) { int lastFilePosition = embFile_tell(file); int x = vp3Decode(binaryReadByte(file)); int y = vp3Decode(binaryReadByte(file)); if(x == 0x80) { switch (y) { case 0x00: case 0x03: break; case 0x01: x = vp3DecodeInt16(binaryReadInt16BE(file)); y = vp3DecodeInt16(binaryReadInt16BE(file)); binaryReadInt16BE(file); embPattern_addStitchRel(pattern, x/ 10.0, y / 10.0, TRIM, 1); break; default: break; } } else { embPattern_addStitchRel(pattern, x / 10.0, y / 10.0, NORMAL, 1); } if(embFile_tell(file) == lastFilePosition) { embLog_error("format-vp3.c could not read stitch block in entirety\n"); return 0; } } if(i + 1 < numberOfColors) embPattern_addStitchRel(pattern, 0, 0, STOP, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); embPattern_flipVertical(pattern); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readT01(EmbPattern* pattern, const char* fileName) { unsigned char b[3]; EmbFile* file = 0; if(!pattern) { embLog_error("format-t01.c readt01(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-t01.c readt01(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-t01.c readt01(), cannot open %s for reading\n", fileName); return 0; } embPattern_loadExternalColorFile(pattern, fileName); while(embFile_read(b, 1, 3, file) == 3) { int flags; int x = 0; int 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 = decodeRecordFlags(b[2]); embPattern_addStitchRel(pattern, x / 10.0, y / 10.0, flags, 1); if(flags == END) break; } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readInb(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; unsigned char fileDescription[8]; unsigned char nullVal; int stitchCount; short width; short height; short colorCount; short unknown3; short unknown2; short imageWidth; short imageHeight; unsigned char bytesUnknown[300]; short nullbyte; short left; short right; short top; short bottom; int x = 0; int y = 0; int i; int fileLength; if(!pattern) { embLog_error("format-inb.c readInb(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-inb.c readInb(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-inb.c readInb(), cannot open %s for reading\n", fileName); return 0; } embPattern_loadExternalColorFile(pattern, fileName); embFile_seek(file, 0, SEEK_END); fileLength = embFile_tell(file); binaryReadBytes(file, fileDescription, 8); nullVal = binaryReadByte(file); binaryReadInt16(file); stitchCount = binaryReadInt32(file); width = binaryReadInt16(file); height = binaryReadInt16(file); colorCount = binaryReadInt16(file); unknown3 = binaryReadInt16(file); unknown2 = binaryReadInt16(file); imageWidth = binaryReadInt16(file); imageHeight = binaryReadInt16(file); binaryReadBytes(file, bytesUnknown, 300); nullbyte = binaryReadInt16(file); left = binaryReadInt16(file); right = binaryReadInt16(file); top = binaryReadInt16(file); bottom = binaryReadInt16(file); embFile_seek(file, 0x2000, SEEK_SET); /* Calculate stitch count since header has been seen to be blank */ stitchCount = (int)((fileLength - 0x2000) / 3); for(i = 0; i < stitchCount; i++) { unsigned char type; int stitch = NORMAL; x = binaryReadByte(file); y = binaryReadByte(file); type = binaryReadByte(file); if((type & 0x40) > 0) x = -x; if((type & 0x10) > 0) y = -y; if((type & 1) > 0) stitch = STOP; if((type & 2) > 0) stitch = TRIM; embPattern_addStitchRel(pattern, x / 10.0, y / 10.0, stitch, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); embPattern_flipVertical(pattern); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readXxx(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; unsigned char b0, b1; int dx = 0, dy = 0; int flags; char endOfStream = 0; int numberOfColors; int paletteOffset; int i; char thisStitchJump = 0; EmbStitchList* lastStitch = 0; EmbStitchList* secondLast = 0; if(!pattern) { embLog_error("format-xxx.c readXxx(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-xxx.c readXxx(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-xxx.c readXxx(), cannot open %s for reading\n", fileName); return 0; } embFile_seek(file, 0x27, SEEK_SET); numberOfColors = binaryReadInt16(file); embFile_seek(file, 0xFC, SEEK_SET); paletteOffset = binaryReadInt32(file); embFile_seek(file, paletteOffset + 6, SEEK_SET); for(i = 0; i < numberOfColors; i++) { EmbThread thread; binaryReadByte(file); thread.color.r = binaryReadByte(file); thread.color.g = binaryReadByte(file); thread.color.b = binaryReadByte(file); embPattern_addThread(pattern, thread); } embFile_seek(file, 0x100, SEEK_SET); for(i = 0; !endOfStream && embFile_tell(file) < paletteOffset; i++) { flags = NORMAL; if(thisStitchJump) flags = TRIM; thisStitchJump = 0; b0 = binaryReadByte(file); b1 = binaryReadByte(file); if(b0 == 0x7E || b0 == 0x7D) /* TODO: ARE THERE OTHER BIG JUMP CODES? */ { dx = b1 + (binaryReadByte(file) << 8); dx = ((short) dx); dy = binaryReadInt16(file); flags = TRIM; } else if(b0 == 0x7F) { if(b1 != 0x17 && b1 != 0x46 && b1 >= 8) /* TODO: LOOKS LIKE THESE CODES ARE IN THE HEADER */ { b0 = 0; b1 = 0; thisStitchJump = 1; flags = STOP; } else if(b1 == 1) { flags = TRIM; b0 = binaryReadByte(file); b1 = binaryReadByte(file); } else { continue; } dx = xxxDecodeByte(b0); dy = xxxDecodeByte(b1); } else { dx = xxxDecodeByte(b0); dy = xxxDecodeByte(b1); } embPattern_addStitchRel(pattern, dx / 10.0, dy / 10.0, flags, 1); } lastStitch = pattern->stitchList; secondLast = 0; if(lastStitch) { while(lastStitch->next) { secondLast = lastStitch; lastStitch = lastStitch->next; } if((!pattern->stitchList) && lastStitch->stitch.flags == STOP && secondLast) { free(lastStitch); lastStitch = 0; secondLast->next = NULL; embPattern_changeColor(pattern, pattern->currentColorIndex - 1); } } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch && pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readCsv(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; int numColorChanges = 0; int size = 1024; int pos = 0; int c = 0; int cellNum = 0; int process = 0; int csvMode = CSV_MODE_NULL; int expect = CSV_EXPECT_QUOTE1; int flags = 0; double xx = 0.0; double yy = 0.0; unsigned char r = 0, g = 0, b = 0; char* buff = 0; if(!pattern) { embLog_error("format-csv.c readCsv(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-csv.c readCsv(), fileName argument is null\n"); return 0; } buff = (char*)malloc(size); if(!buff) { embLog_error("format-csv.c readCsv(), unable to allocate memory for buff\n"); return 0; } file = embFile_open(fileName,"r"); if(!file) { embLog_error("format-csv.c readCsv(), cannot open %s for reading\n", fileName); return 0; } else { pos = 0; do { c = embFile_getc(file); switch(c) { case '"': if(expect == CSV_EXPECT_QUOTE1) { expect = CSV_EXPECT_QUOTE2; } else if(expect == CSV_EXPECT_QUOTE2) expect = CSV_EXPECT_COMMA; break; case ',': if(expect == CSV_EXPECT_COMMA) { process = 1; } break; case '\n': if(expect == CSV_EXPECT_COMMA) { process = 1; } else if(expect == CSV_EXPECT_QUOTE1) { /* Do Nothing. We encountered a blank line. */ } else { embLog_error("format-csv.c readCsv(), premature newline\n"); return 0; } break; } if(pos >= size - 1) { size *= 2; buff = (char*)realloc(buff,size); if(!buff) { embLog_error("format-csv.c readCsv(), cannot re-allocate memory for buff\n"); return 0; } } if(process) { buff[pos] = 0; pos = 0; process = 0; cellNum++; expect = CSV_EXPECT_QUOTE1; if(csvMode == CSV_MODE_NULL) { if (!strcmp(buff, "#")) { csvMode = CSV_MODE_COMMENT; } else if(!strcmp(buff, ">")) { csvMode = CSV_MODE_VARIABLE; } else if(!strcmp(buff, "$")) { csvMode = CSV_MODE_THREAD; } else if(!strcmp(buff, "*")) { csvMode = CSV_MODE_STITCH; } else { /* TODO: error */ return 0; } } else if(csvMode == CSV_MODE_COMMENT) { /* Do Nothing */ } else if(csvMode == CSV_MODE_VARIABLE) { /* Do Nothing */ } else if(csvMode == CSV_MODE_THREAD) { if(cellNum == 2) { /* Do Nothing. Ignore Thread Number */ } else if(cellNum == 3) r = (unsigned char)atoi(buff); else if(cellNum == 4) g = (unsigned char)atoi(buff); else if(cellNum == 5) b = (unsigned char)atoi(buff); else if(cellNum == 6) { /* TODO: Thread Description */ } else if(cellNum == 7) { /* TODO: Thread Catalog Number */ EmbThread t; t.color.r = r; t.color.g = g; t.color.b = b; t.description = "TODO:DESCRIPTION"; t.catalogNumber = "TODO:CATALOG_NUMBER"; embPattern_addThread(pattern, t); csvMode = CSV_MODE_NULL; cellNum = 0; } else { /* TODO: error */ return 0; } } else if(csvMode == CSV_MODE_STITCH) { if(cellNum == 2) { flags = csvStrToStitchFlag(buff); if(flags == STOP) numColorChanges++; } else if(cellNum == 3) xx = atof(buff); else if(cellNum == 4) { yy = atof(buff); embPattern_addStitchAbs(pattern, xx, yy, flags, 1); csvMode = CSV_MODE_NULL; cellNum = 0; } else { /* TODO: error */ return 0; } } if(c == '\n') { csvMode = CSV_MODE_NULL; cellNum = 0; } } else { if(expect == CSV_EXPECT_QUOTE2 && c != '"') buff[pos++] = (char)c; } } while(c != EOF); embFile_close(file); } /* if not enough colors defined, fill in random colors */ while(embThreadList_count(pattern->threadList) < numColorChanges) { embPattern_addThread(pattern, embThread_getRandom()); } free(buff); buff = 0; return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readStx(EmbPattern* pattern, const char* fileName) { int i, threadCount; unsigned char* gif = 0; /* public Bitmap Image; */ StxThread* stxThreads = 0; unsigned char headerBytes[7]; char* header = 0; char filetype[4], version[5]; int paletteLength, imageLength, something1, stitchDataOffset, something3, threadDescriptionOffset, stitchCount, left, right, colors; int val1, val2, val3, val4, val5, val6; int vala1, vala2, vala3, vala4, vala5, vala6; int bottom,top; EmbFile* file = 0; if(!pattern) { embLog_error("format-stx.c readStx(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-stx.c readStx(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-stx.c readStx(), cannot open %s for reading\n", fileName); return 0; } binaryReadBytes(file, headerBytes, 7); header = (char*)headerBytes; memcpy(filetype, &header[0], 3); memcpy(version, &header[3], 4); filetype[3] = '\0'; version[4] = '\0'; binaryReadByte(file); paletteLength = binaryReadInt32(file); imageLength = binaryReadInt32(file); something1 = binaryReadInt32(file); stitchDataOffset = binaryReadInt32(file); something3 = binaryReadInt32(file); threadDescriptionOffset = binaryReadInt32(file); stitchCount = binaryReadInt32(file); colors = binaryReadInt32(file); right = binaryReadInt16(file); left = binaryReadInt16(file); bottom = binaryReadInt16(file); top = binaryReadInt16(file); gif = (unsigned char*)malloc(imageLength); if(!gif) { embLog_error("format-stx.c readStx(), unable to allocate memory for gif\n"); return 0; } binaryReadBytes(file, gif, imageLength); /*Stream s2 = new MemoryStream(gif); */ /*Image = new Bitmap(s2); */ threadCount = binaryReadInt16(file); stxThreads = (StxThread*)malloc(sizeof(StxThread) * threadCount); if(!stxThreads) { embLog_error("format-stx.c readStx(), unable to allocate memory for stxThreads\n"); return 0; } for(i = 0; i < threadCount; i++) { EmbThread t; StxThread st; stxReadThread(&st, file); t.color.r = st.StxColor.r; t.color.g = st.StxColor.g; t.color.b = st.StxColor.b; t.description = st.ColorName; t.catalogNumber = st.ColorCode; embPattern_addThread(pattern, t); stxThreads[i] = st; } binaryReadInt32(file); binaryReadInt32(file); binaryReadInt32(file); binaryReadInt16(file); binaryReadUInt8(file); val1 = binaryReadInt16(file); val2 = binaryReadInt16(file); val3 = binaryReadInt16(file); val4 = binaryReadInt16(file); val5 = binaryReadInt16(file); /* 0 */ val6 = binaryReadInt16(file); /* 0 */ vala1 = binaryReadInt16(file); vala2 = binaryReadInt16(file); vala3 = binaryReadInt16(file); vala4 = binaryReadInt16(file); vala5 = binaryReadInt16(file); /* 0 */ vala6 = binaryReadInt16(file); /* 0 */ binaryReadInt32(file); /* 0 */ binaryReadInt32(file); /* 0 */ /* br.BaseStream.Position = stitchDataOffset; */ for(i = 1; i < stitchCount; ) { char b0 = binaryReadByte(file); char b1 = binaryReadByte(file); if(b0 == -128) { switch (b1) { case 1: b0 = binaryReadByte(file); b1 = binaryReadByte(file); /*embPattern_addStitchRel(b0, b1, STOP);*/ i++; break; case 2: b0 = binaryReadByte(file); b1 = binaryReadByte(file); embPattern_addStitchRel(pattern, b0 / 10.0, b1 / 10.0, JUMP, 1); i++; break; case -94: /* NOTE: Is this a syncronize */ break; default: /*Debugger.Break(); */ break; } } else { embPattern_addStitchRel(pattern, b0 / 10.0, b1 / 10.0, NORMAL, 1); i++; } } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); embPattern_flipVertical(pattern); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readPcs(EmbPattern* pattern, const char* fileName) { char allZeroColor = 1; int i = 0; unsigned char b[9]; double dx = 0, dy = 0; int flags = 0, st = 0; unsigned char version, hoopSize; unsigned short colorCount; EmbFile* file = 0; if(!pattern) { embLog_error("format-pcs.c readPcs(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-pcs.c readPcs(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-pcs.c readPcs(), cannot open %s for reading\n", fileName); return 0; } version = binaryReadByte(file); hoopSize = binaryReadByte(file); /* 0 for PCD, 1 for PCQ (MAXI), 2 for PCS with small hoop(80x80), */ /* and 3 for PCS with large hoop (115x120) */ switch(hoopSize) { case 2: pattern->hoop.width = 80.0; pattern->hoop.height = 80.0; break; case 3: pattern->hoop.width = 115; pattern->hoop.height = 120.0; break; } colorCount = binaryReadUInt16(file); for(i = 0; i < colorCount; i++) { EmbThread t; t.color.r = binaryReadByte(file); t.color.g = binaryReadByte(file); t.color.b = binaryReadByte(file); t.catalogNumber = ""; t.description = ""; if(t.color.r || t.color.g || t.color.b) { allZeroColor = 0; } embPattern_addThread(pattern, t); binaryReadByte(file); } if(allZeroColor) embPattern_loadExternalColorFile(pattern, fileName); st = binaryReadUInt16(file); /* READ STITCH RECORDS */ for(i = 0; i < st; i++) { flags = NORMAL; if(embFile_read(b, 1, 9, file) != 9) break; if(b[8] & 0x01) { flags = STOP; } else if(b[8] & 0x04) { flags = TRIM; } else if(b[8] != 0) { /* TODO: ONLY INTERESTED IN THIS CASE TO LEARN MORE ABOUT THE FORMAT */ } dx = pcsDecode(b[1], b[2], b[3]); dy = pcsDecode(b[5], b[6], b[7]); embPattern_addStitchAbs(pattern, dx / 10.0, dy / 10.0, flags, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; }
/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writePcs(EmbPattern* pattern, const char* fileName) { EmbStitchList* pointer = 0; EmbThreadList* threadPointer = 0; EmbFile* file = 0; int i = 0; unsigned char colorCount = 0; double xx = 0.0, yy = 0.0; if(!pattern) { embLog_error("format-pcs.c writePcs(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-pcs.c writePcs(), fileName argument is null\n"); return 0; } if(!embStitchList_count(pattern->stitchList)) { embLog_error("format-pcs.c writePcs(), pattern contains no stitches\n"); return 0; } /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); file = embFile_open(fileName, "wb"); if(!file) { embLog_error("format-pcs.c writePcs(), cannot open %s for writing\n", fileName); return 0; } binaryWriteByte(file, (unsigned char)'2'); binaryWriteByte(file, 3); /* TODO: select hoop size defaulting to Large PCS hoop */ colorCount = (unsigned char)embThreadList_count(pattern->threadList); binaryWriteUShort(file, (unsigned short)colorCount); threadPointer = pattern->threadList; i = 0; while(threadPointer) { EmbColor color = threadPointer->thread.color; binaryWriteByte(file, color.r); binaryWriteByte(file, color.g); binaryWriteByte(file, color.b); binaryWriteByte(file, 0); threadPointer = threadPointer->next; i++; } for(; i < 16; i++) { binaryWriteUInt(file, 0); /* write remaining colors to reach 16 */ } binaryWriteUShort(file, (unsigned short)embStitchList_count(pattern->stitchList)); /* write stitches */ xx = yy = 0; pointer = pattern->stitchList; while(pointer) { pcsEncode(file, roundDouble(pointer->stitch.xx * 10.0), roundDouble(pointer->stitch.yy * 10.0), pointer->stitch.flags); pointer = pointer->next; } embFile_close(file); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readBro(EmbPattern* pattern, const char* fileName) { unsigned char x55; short unknown1, unknown2, unknown3, unknown4, moreBytesToEnd; char name[8]; int stitchType; EmbFile* file = 0; if(!pattern) { embLog_error("format-bro.c readBro(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-bro.c readBro(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-bro.c readBro(), cannot open %s for reading\n", fileName); return 0; } embPattern_loadExternalColorFile(pattern, fileName); x55 = binaryReadByte(file); unknown1 = binaryReadInt16(file); /* TODO: determine what this unknown data is */ embFile_read(name, 1, 8, file); unknown2 = binaryReadInt16(file); /* TODO: determine what this unknown data is */ unknown3 = binaryReadInt16(file); /* TODO: determine what this unknown data is */ unknown4 = binaryReadInt16(file); /* TODO: determine what this unknown data is */ moreBytesToEnd = binaryReadInt16(file); embFile_seek(file, 0x100, SEEK_SET); while(1) { short b1, b2; stitchType = EM_NORMAL; b1 = binaryReadByte(file); b2 = binaryReadByte(file); if(b1 == -128) { unsigned char bCode = binaryReadByte(file); b1 = binaryReadInt16(file); b2 = binaryReadInt16(file); if(bCode == 2) { stitchType = STOP; } else if(bCode == 3) { stitchType = TRIM; } else if(bCode == 0x7E) { embPattern_addStitchRel(pattern, 0, 0, END, 1); break; } } embPattern_addStitchRel(pattern, b1 / 10.0, b2 / 10.0, stitchType, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readZsk(EmbPattern* pattern, const char* fileName) { char b[3]; EmbFile* file = 0; int stitchType; unsigned char colorNumber; if(!pattern) { embLog_error("format-zsk.c readZsk(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-zsk.c readZsk(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-zsk.c readZsk(), cannot open %s for reading\n", fileName); return 0; } embFile_seek(file, 0x230, SEEK_SET); colorNumber = binaryReadUInt8(file); while(colorNumber != 0) { EmbThread t; t.color.r = binaryReadUInt8(file); t.color.g = binaryReadUInt8(file); t.color.b = binaryReadUInt8(file); t.catalogNumber = ""; t.description = ""; embPattern_addThread(pattern, t); embFile_seek(file, 0x48, SEEK_CUR); colorNumber = binaryReadUInt8(file); } embFile_seek(file, 0x2E, SEEK_CUR); while(embFile_read(b, 1, 3, file) == 3) { stitchType = NORMAL; if (b[0] & 0x4) { b[2] = -b[2]; } if (b[0] & 0x8) { b[1] = -b[1]; } if (b[0] & 0x02) { stitchType = JUMP; } if(b[0] & 0x20) { if (b[1] == 2) { stitchType = TRIM; } else if (b[1] == -1) { break; } else { if (b[2] != 0) { colorNumber = b[2]; } stitchType = STOP; /* TODO: need to determine what b[1] is used for.*/ embPattern_changeColor(pattern, colorNumber - 1); } b[1] = 0; b[2] = 0; } embPattern_addStitchRel(pattern, b[1] / 10.0, b[2] / 10.0, stitchType, 0); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if (pattern->lastStitch->stitch.flags != END) { embPattern_addStitchRel(pattern, 0, 0, END, 1); } return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readVip(EmbPattern* pattern, const char* fileName) { int fileLength; int i; unsigned char prevByte = 0; unsigned char *attributeData = 0, *decodedColors = 0, *attributeDataDecompressed = 0; unsigned char *xData = 0, *xDecompressed = 0, *yData = 0, *yDecompressed = 0; VipHeader header; EmbFile* file = 0; if(!pattern) { embLog_error("format-vip.c readVip(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-vip.c readVip(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-vip.c readVip(), cannot open %s for reading\n", fileName); return 0; } embFile_seek(file, 0x0, SEEK_END); fileLength = embFile_tell(file); embFile_seek(file, 0x00, SEEK_SET); header.magicCode = binaryReadInt32(file); header.numberOfStitches = binaryReadInt32(file); header.numberOfColors = binaryReadInt32(file); header.postitiveXHoopSize = binaryReadInt16(file); header.postitiveYHoopSize = binaryReadInt16(file); header.negativeXHoopSize = binaryReadInt16(file); header.negativeYHoopSize = binaryReadInt16(file); header.attributeOffset = binaryReadInt32(file); header.xOffset = binaryReadInt32(file); header.yOffset = binaryReadInt32(file); /*stringVal = (unsigned char*)malloc(sizeof(unsigned char)*8); TODO: review this and uncomment or remove if(!stringVal) { embLog_error("format-vip.c readVip(), cannot allocate memory for stringVal\n"); return 0; } */ binaryReadBytes(file, header.stringVal, 8); /* TODO: check return value */ header.unknown = binaryReadInt16(file); header.colorLength = binaryReadInt32(file); decodedColors = (unsigned char*)malloc(header.numberOfColors*4); if(!decodedColors) { embLog_error("format-vip.c readVip(), cannot allocate memory for decodedColors\n"); return 0; } for(i = 0; i < header.numberOfColors*4; ++i) { unsigned char inputByte = binaryReadByte(file); unsigned char tmpByte = (unsigned char) (inputByte ^ vipDecodingTable[i]); decodedColors[i] = (unsigned char) (tmpByte ^ prevByte); prevByte = inputByte; } for(i = 0; i < header.numberOfColors; i++) { EmbThread thread; int startIndex = i << 2; thread.color.r = decodedColors[startIndex]; thread.color.g = decodedColors[startIndex + 1]; thread.color.b = decodedColors[startIndex + 2]; /* printf("%d\n", decodedColors[startIndex + 3]); */ embPattern_addThread(pattern, thread); } embFile_seek(file, header.attributeOffset, SEEK_SET); attributeData = (unsigned char*)malloc(header.xOffset - header.attributeOffset); if(!attributeData) { embLog_error("format-vip.c readVip(), cannot allocate memory for attributeData\n"); return 0; } binaryReadBytes(file, attributeData, header.xOffset - header.attributeOffset); /* TODO: check return value */ attributeDataDecompressed = vipDecompressData(attributeData, header.xOffset - header.attributeOffset, header.numberOfStitches); embFile_seek(file, header.xOffset, SEEK_SET); xData = (unsigned char*)malloc(header.yOffset - header.xOffset); if(!xData) { embLog_error("format-vip.c readVip(), cannot allocate memory for xData\n"); return 0; } binaryReadBytes(file, xData, header.yOffset - header.xOffset); /* TODO: check return value */ xDecompressed = vipDecompressData(xData, header.yOffset - header.xOffset, header.numberOfStitches); embFile_seek(file, header.yOffset, SEEK_SET); yData = (unsigned char*)malloc(fileLength - header.yOffset); if(!yData) { embLog_error("format-vip.c readVip(), cannot allocate memory for yData\n"); return 0; } binaryReadBytes(file, yData, fileLength - header.yOffset); /* TODO: check return value */ yDecompressed = vipDecompressData(yData, fileLength - header.yOffset, header.numberOfStitches); for(i = 0; i < header.numberOfStitches; i++) { embPattern_addStitchRel(pattern, vipDecodeByte(xDecompressed[i]) / 10.0, vipDecodeByte(yDecompressed[i]) / 10.0, vipDecodeStitchType(attributeDataDecompressed[i]), 1); } embPattern_addStitchRel(pattern, 0, 0, END, 1); embFile_close(file); free(attributeData); attributeData = 0; free(xData); xData = 0; free(yData); yData = 0; free(attributeDataDecompressed); attributeDataDecompressed = 0; free(xDecompressed); xDecompressed = 0; free(yDecompressed); yDecompressed = 0; return 1; }
/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writeXxx(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; int i; EmbRect rect; int endOfStitches; EmbThreadList* colors = 0; int curColor = 0; if(!pattern) { embLog_error("format-xxx.c writeXxx(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-xxx.c writeXxx(), fileName argument is null\n"); return 0; } if(!embStitchList_count(pattern->stitchList)) { embLog_error("format-xxx.c writeXxx(), pattern contains no stitches\n"); return 0; } /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch && pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); file = embFile_open(fileName, "wb"); if(!file) { embLog_error("format-xxx.c writeXxx(), cannot open %s for writing\n", fileName); return 0; } embPattern_correctForMaxStitchLength(pattern, 124, 127); for(i = 0; i < 0x17; i++) { binaryWriteByte(file, 0x00); } binaryWriteUInt(file, (unsigned int) embStitchList_count(pattern->stitchList)); for(i = 0; i < 0x0C; i++) { binaryWriteByte(file, 0x00); } binaryWriteUShort(file, (unsigned short)embThreadList_count(pattern->threadList)); binaryWriteShort(file, 0x0000); rect = embPattern_calcBoundingBox(pattern); binaryWriteShort(file, (short)(embRect_width(rect) * 10.0)); binaryWriteShort(file, (short)(embRect_height(rect) * 10.0)); binaryWriteShort(file, (short)(embRect_width(rect) / 2.0 * 10)); /*TODO: xEnd from start point x=0 */ binaryWriteShort(file, (short)(embRect_height(rect) / 2.0 * 10)); /*TODO: yEnd from start point y=0 */ binaryWriteShort(file, (short)(embRect_width(rect)/2.0 * 10)); /*TODO: left from start x = 0 */ binaryWriteShort(file, (short)(embRect_height(rect)/2.0 * 10)); /*TODO: bottom from start y = 0 */ for(i = 0; i < 0xC5; i++) { binaryWriteByte(file, 0x00); } binaryWriteInt(file, 0x0000); /* place holder for end of stitches */ xxxEncodeDesign(file, pattern); endOfStitches = embFile_tell(file); embFile_seek(file, 0xFC, SEEK_SET); binaryWriteUInt(file, endOfStitches); embFile_seek(file, 0, SEEK_END); binaryWriteByte(file, 0x7F); /* is this really correct? */ binaryWriteByte(file, 0x7F); binaryWriteByte(file, 0x03); binaryWriteByte(file, 0x14); binaryWriteByte(file, 0x00); binaryWriteByte(file, 0x00); colors = pattern->threadList; while(colors) { binaryWriteByte(file, 0x00); binaryWriteByte(file, colors->thread.color.r); binaryWriteByte(file, colors->thread.color.g); binaryWriteByte(file, colors->thread.color.b); curColor++; colors = colors->next; } for(i = 0; i < (22 - curColor); i++) { binaryWriteUInt(file, 0x01000000); } binaryWriteByte(file, 0x00); binaryWriteByte(file, 0x01); embFile_close(file); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readEmd(EmbPattern* pattern, const char* fileName) { unsigned char b0 = 0, b1 = 0; char dx = 0, dy = 0; int flags = EM_NORMAL; char endOfStream = 0; unsigned char jemd0[6]; /* TODO: more descriptive name */ int width, height, colors; int i; EmbFile* file = 0; if(!pattern) { embLog_error("format-emd.c readEmd(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-emd.c readEmd(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-emd.c readEmd(), cannot open %s for reading\n", fileName); return 0; } embPattern_loadExternalColorFile(pattern, fileName); binaryReadBytes(file, jemd0, 6); /* TODO: check return value */ width = binaryReadInt16(file); height = binaryReadInt16(file); colors = binaryReadInt16(file); embFile_seek(file, 0x30, SEEK_SET); for(i = 0; !endOfStream; i++) { flags = EM_NORMAL; b0 = binaryReadUInt8(file); b1 = binaryReadUInt8(file); if(b0 == 0x80) { if(b1 == 0x2A) { embPattern_addStitchRel(pattern, 0, 0, STOP, 1); continue; } else if(b1 == 0x80) { b0 = binaryReadUInt8(file); b1 = binaryReadUInt8(file); flags = TRIM; } else if(b1 == 0xFD) { embPattern_addStitchRel(pattern, 0, 0, END, 1); break; } else { continue; } } dx = emdDecode(b0); dy = emdDecode(b1); embPattern_addStitchRel(pattern, dx / 10.0, dy / 10.0, flags, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; }
/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writeVip(EmbPattern* pattern, const char* fileName) { EmbRect boundingRect; int stitchCount, minColors, patternColor; int attributeSize = 0; int xCompressedSize = 0; int yCompressedSize = 0; double previousX = 0; double previousY = 0; unsigned char* xValues = 0, *yValues = 0, *attributeValues = 0; EmbStitchList* pointer = 0; double xx = 0.0; double yy = 0.0; int flags = 0; int i = 0; unsigned char* attributeCompressed = 0, *xCompressed = 0, *yCompressed = 0, *decodedColors = 0, *encodedColors = 0; unsigned char prevByte = 0; EmbThreadList* colorPointer = 0; EmbFile* file = 0; if(!pattern) { embLog_error("format-vip.c writeVip(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-vip.c writeVip(), fileName argument is null\n"); return 0; } stitchCount = embStitchList_count(pattern->stitchList); if(!stitchCount) { embLog_error("format-vip.c writeVip(), pattern contains no stitches\n"); return 0; } /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch && pattern->lastStitch->stitch.flags != END) { embPattern_addStitchRel(pattern, 0, 0, END, 1); stitchCount++; } file = embFile_open(fileName, "wb"); if(file == 0) { embLog_error("format-vip.c writeVip(), cannot open %s for writing\n", fileName); return 0; } minColors = embThreadList_count(pattern->threadList); decodedColors = (unsigned char*)malloc(minColors << 2); if(!decodedColors) return 0; encodedColors = (unsigned char*)malloc(minColors << 2); if(encodedColors) /* TODO: review this line. It looks clearly wrong. If not, note why. */ { free(decodedColors); decodedColors = 0; return 0; } /* embPattern_correctForMaxStitchLength(pattern, 0x7F, 0x7F); */ patternColor = minColors; if(minColors > 24) minColors = 24; binaryWriteUInt(file, 0x0190FC5D); binaryWriteUInt(file, stitchCount); binaryWriteUInt(file, minColors); boundingRect = embPattern_calcBoundingBox(pattern); binaryWriteShort(file, (short) roundDouble(boundingRect.right * 10.0)); binaryWriteShort(file, (short) -roundDouble(boundingRect.top * 10.0 - 1.0)); binaryWriteShort(file, (short) roundDouble(boundingRect.left * 10.0)); binaryWriteShort(file, (short) -roundDouble(boundingRect.bottom * 10.0 - 1.0)); binaryWriteUInt(file, 0x38 + (minColors << 3)); xValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount)); yValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount)); attributeValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount)); if(xValues && yValues && attributeValues) { pointer = pattern->stitchList; while(pointer) { xx = pointer->stitch.xx; yy = pointer->stitch.yy; flags = pointer->stitch.flags; xValues[i] = vipEncodeByte((xx - previousX) * 10.0); previousX = xx; yValues[i] = vipEncodeByte((yy - previousY) * 10.0); previousY = yy; attributeValues[i] = vipEncodeStitchType(flags); pointer = pointer->next; i++; } attributeCompressed = vipCompressData(attributeValues, stitchCount, &attributeSize); xCompressed = vipCompressData(xValues, stitchCount, &xCompressedSize); yCompressed = vipCompressData(yValues, stitchCount, &yCompressedSize); binaryWriteUInt(file, (unsigned int) (0x38 + (minColors << 3) + attributeSize)); binaryWriteUInt(file, (unsigned int) (0x38 + (minColors << 3) + attributeSize + xCompressedSize)); binaryWriteUInt(file, 0x00000000); binaryWriteUInt(file, 0x00000000); binaryWriteUShort(file, 0x0000); binaryWriteInt(file, minColors << 2); colorPointer = pattern->threadList; for(i = 0; i < minColors; i++) { int byteChunk = i << 2; EmbColor currentColor = colorPointer->thread.color; decodedColors[byteChunk] = currentColor.r; decodedColors[byteChunk + 1] = currentColor.g; decodedColors[byteChunk + 2] = currentColor.b; decodedColors[byteChunk + 3] = 0x01; colorPointer = colorPointer->next; } for(i = 0; i < minColors << 2; ++i) { unsigned char tmpByte = (unsigned char) (decodedColors[i] ^ vipDecodingTable[i]); prevByte = (unsigned char) (tmpByte ^ prevByte); binaryWriteByte(file, prevByte); } for(i = 0; i <= minColors; i++) { binaryWriteInt(file, 1); } binaryWriteUInt(file, 0); /* string length */ binaryWriteShort(file, 0); binaryWriteBytes(file, (char*) attributeCompressed, attributeSize); binaryWriteBytes(file, (char*) xCompressed, xCompressedSize); binaryWriteBytes(file, (char*) yCompressed, yCompressedSize); } if(attributeCompressed) { free(attributeCompressed); attributeCompressed = 0; } if(xCompressed) { free(xCompressed); xCompressed = 0; } if(yCompressed) { free(yCompressed); yCompressed = 0; } if(attributeValues) { free(attributeValues); attributeValues = 0; } if(xValues) { free(xValues); xValues = 0; } if(yValues) { free(yValues); yValues = 0; } if(decodedColors) { free(decodedColors); decodedColors = 0; } if(encodedColors) { free(encodedColors); encodedColors = 0; } embFile_close(file); return 1; }
/*! Reads a file with the given \a fileName and loads the data into \a pattern. * Returns \c true if successful, otherwise returns \c false. */ int readU00(EmbPattern* pattern, const char* fileName) { int i; char dx = 0, dy = 0; int flags = NORMAL; char endOfStream = 0; EmbFile* file = 0; if(!pattern) { embLog_error("format-u00.c readU00(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-u00.c readU00(), fileName argument is null\n"); return 0; } file = embFile_open(fileName, "rb"); if(!file) { embLog_error("format-u00.c readU00(), cannot open %s for reading\n", fileName); return 0; } /* 16 3byte RGB's start @ 0x08 followed by 14 bytes between 0 and 15 with index of color for each color change */ embFile_seek(file, 0x08, SEEK_SET); for(i = 0; i < 16; i++) { EmbThread t; t.color.r = binaryReadUInt8(file); t.color.g = binaryReadUInt8(file); t.color.b = binaryReadUInt8(file); embPattern_addThread(pattern, t); } embFile_seek(file, 0x100, SEEK_SET); for(i = 0; !endOfStream; i++) { char negativeX , negativeY; unsigned char b0 = binaryReadUInt8(file); unsigned char b1 = binaryReadUInt8(file); unsigned char b2 = binaryReadUInt8(file); if(b0 == 0xF8 || b0 == 0x87 || b0 == 0x91) { break; } if((b0 & 0x0F) == 0) { flags = NORMAL; } else if((b0 & 0x1f) == 1) { flags = JUMP; } else if((b0 & 0x0F) > 0) { flags = STOP; } negativeX = ((b0 & 0x20) > 0); negativeY = ((b0 & 0x40) > 0); dx = (char) b2; dy = (char) b1; if(negativeX) dx = (char) -dx; if(negativeY) dy = (char) -dy; embPattern_addStitchRel(pattern, dx / 10.0, dy / 10.0, flags, 1); } embFile_close(file); /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); return 1; }