/*! 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 readPhc(EmbPattern* pattern, const char* fileName) { int colorChanges, version, bytesInSection2; unsigned short pecOffset, bytesInSection, bytesInSection3; char pecAdd; FILE* file = 0; int i; if(!pattern) { embLog_error("format-phc.c readPhc(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-phc.c readPhc(), fileName argument is null\n"); return 0; } file = fopen(fileName, "rb"); if(!file) { embLog_error("format-phc.c readPhc(), cannot open %s for reading\n", fileName); return 0; } fseek(file, 0x07, SEEK_SET); version = binaryReadByte(file) - 0x30; /* converting from ansi number */ fseek(file, 0x4D, SEEK_SET); colorChanges = binaryReadUInt16(file); for(i = 0; i < colorChanges; i++) { EmbThread t = pecThreads[(int)binaryReadByte(file)]; embPattern_addThread(pattern, t); } fseek(file, 0x2B, SEEK_SET); pecAdd = binaryReadByte(file); binaryReadUInt32(file); /* file length */ pecOffset = binaryReadUInt16(file); fseek(file, pecOffset + pecAdd, SEEK_SET); bytesInSection = binaryReadUInt16(file); fseek(file, bytesInSection, SEEK_CUR); bytesInSection2 = binaryReadUInt32(file); fseek(file, bytesInSection2, SEEK_CUR); bytesInSection3 = binaryReadUInt16(file); fseek(file, bytesInSection3 + 0x12, SEEK_CUR); readPecStitches(pattern, file); fclose(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; /*TODO: finish ReadPhc */ }
bcf_directory_entry* CompoundFileDirectoryEntry(FILE* file) { bcf_directory_entry* dir = (bcf_directory_entry*)malloc(sizeof(bcf_directory_entry)); if(!dir) { embLog_error("compound-file-directory.c CompoundFileDirectoryEntry(), cannot allocate memory for dir\n"); } /* TODO: avoid crashing. null pointer will be accessed */ memset(dir->directoryEntryName, 0, 32); parseDirectoryEntryName(file, dir); dir->next = 0; dir->directoryEntryNameLength = binaryReadUInt16(file); dir->objectType = (unsigned char)binaryReadByte(file); if( (dir->objectType != ObjectTypeStorage) && (dir->objectType != ObjectTypeStream) && (dir->objectType != ObjectTypeRootEntry)) { embLog_error("compound-file-directory.c CompoundFileDirectoryEntry(), unexpected object type: %d\n", dir->objectType); return 0; } dir->colorFlag = (unsigned char)binaryReadByte(file); dir->leftSiblingId = binaryReadUInt32(file); dir->rightSiblingId = binaryReadUInt32(file); dir->childId = binaryReadUInt32(file); readCLSID(file, dir); dir->stateBits = binaryReadUInt32(file); dir->creationTime = parseTime(file); dir->modifiedTime = parseTime(file); dir->startingSectorLocation = binaryReadUInt32(file); dir->streamSize = binaryReadUInt32(file); /* This should really be __int64 or long long, but for our uses we should never run into an issue */ dir->streamSizeHigh = binaryReadUInt32(file); /* top portion of int64 */ return dir; }
/*! 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; FILE* fileCompound = 0; FILE* 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 = fopen(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; fseek(file, 0x1C6, SEEK_SET); ofmReadThreads(file, pattern); fseek(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); unknownCount = binaryReadInt16(file); /* unknown count */ 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); } } embPattern_addStitchRel(pattern, 0.0, 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 readNew(EmbPattern* pattern, const char* fileName) { unsigned int stitchCount; unsigned char data[3]; FILE* file = 0; if(!pattern) { embLog_error("format-new.c readNew(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-new.c readNew(), fileName argument is null\n"); return 0; } file = fopen(fileName, "rb"); if(!file) { embLog_error("format-new.c readNew(), cannot open %s for reading\n", fileName); return 0; } embPattern_loadExternalColorFile(pattern, fileName); stitchCount = binaryReadUInt16(file); while(binaryReadBytes(file, data, 3) == 3) { int x = decodeNewStitch(data[0]); int y = decodeNewStitch(data[1]); int flag = NORMAL; char val = data[2]; if(data[2] & 0x40) { x = -x; } if(data[2] & 0x20) { y = -y; } if(data[2] & 0x10) { flag = TRIM; } if(data[2] & 0x01) { flag = JUMP; } if((val & 0x1E) == 0x02) { flag = STOP; } /* Unknown values, possibly TRIM 155 = 1001 1011 = 0x9B 145 = 1001 0001 = 0x91 */ /*val = (data[2] & 0x1C); if(val != 0 && data[2] != 0x9B && data[2] != 0x91) { int z = 1; }*/ embPattern_addStitchRel(pattern, x / 10.0, y / 10.0, flag, 1); } embPattern_addStitchRel(pattern, 0.0, 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 readInf(EmbPattern* pattern, const char* fileName) { int numberOfColors; int i; FILE* file = 0; if(!pattern) { embLog_error("format-inf.c readInf(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-inf.c readInf(), fileName argument is null\n"); return 0; } file = fopen(fileName, "rb"); if(!file) { /* NOTE: The .inf format is an optional color file. Do not log an error if the file does not exist */ return 0; } binaryReadUInt32BE(file); binaryReadUInt32BE(file); binaryReadUInt32BE(file); numberOfColors = binaryReadUInt32BE(file); embThreadList_free(pattern->threadList); pattern->threadList = 0; pattern->lastThread = 0; for(i = 0; i < numberOfColors; i++) { char colorType[50]; char colorDescription[50]; EmbThread t; binaryReadUInt16(file); binaryReadUInt16(file); t.color.r = binaryReadByte(file); t.color.g = binaryReadByte(file); t.color.b = binaryReadByte(file); t.catalogNumber = ""; t.description = ""; embPattern_addThread(pattern, t); binaryReadUInt16(file); binaryReadString(file, colorType, 50); binaryReadString(file, colorDescription, 50); } fclose(file); return 1; }
static void parseDirectoryEntryName(FILE* file, bcf_directory_entry* dir) { int i; unsigned short unicodechar; for(i = 0; i < 32; ++i) { unicodechar = binaryReadUInt16(file); if(unicodechar != 0x0000) { dir->directoryEntryName[i] = (char)unicodechar; } } }
int readDat(EmbPattern* pattern, const char* fileName) { int fileLength, stitchesRemaining; FILE* file = 0; file = fopen(fileName, "rb"); if(!file) { return 0; } embPattern_loadExternalColorFile(pattern, fileName); fseek(file, 0x00, SEEK_END); fileLength = ftell(file); fseek(file, 0x02, SEEK_SET); stitchesRemaining = binaryReadUInt16(file); fseek(file, 0x100, SEEK_SET); while(ftell(file)< fileLength) { int b1 = (int)binaryReadUInt8(file); int b2 = (int)binaryReadUInt8(file); unsigned char b0 = binaryReadByte(file); int stitchType = NORMAL; stitchesRemaining--; if((b0 & 0x02) == 0) stitchType = TRIM; if(b0 == 0x87) { stitchType = STOP; } if(b0 == 0xF8) { break; } if(b1 >= 0x80) { b1 = -(b1 & 0x7F); } if(b2 >= 0x80) { b2 = -(b2 & 0x7F); } embPattern_addStitchRel(pattern, b1 / 10.0, b2 / 10.0, stitchType, 1); } fclose(file); 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 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; }
int readShv(EmbPattern* pattern, const char* fileName) { int i; char inJump = 0; unsigned char fileNameLength, designWidth, designHeight; char halfDesignWidth, halfDesignHeight, halfDesignWidth2, halfDesignHeight2; char* headerText = "Embroidery disk created using software licensed from Viking Sewing Machines AB, Sweden"; char dx = 0, dy = 0; char numberOfColors; unsigned short magicCode; int something; short left,top,right,bottom; char something2, numberOfSections, something3; FILE* file = 0; file = fopen(fileName, "rb"); if(!file) { return 0; } fseek(file, strlen(headerText), SEEK_SET); fileNameLength = binaryReadUInt8(file); fseek(file, fileNameLength, SEEK_CUR); designWidth = binaryReadUInt8(file); designHeight = binaryReadUInt8(file); halfDesignWidth = binaryReadUInt8(file); halfDesignHeight = binaryReadUInt8(file); halfDesignWidth2 = binaryReadUInt8(file); halfDesignHeight2 = binaryReadUInt8(file); if((designHeight % 2) == 1) fseek(file, ((designHeight + 1)*designWidth)/2, SEEK_CUR); else fseek(file, (designHeight*designWidth)/2, SEEK_CUR); numberOfColors = binaryReadUInt8(file); magicCode = binaryReadUInt16(file); binaryReadByte(file); something = binaryReadInt32(file); left = binaryReadInt16(file); top = binaryReadInt16(file); right = binaryReadInt16(file); bottom = binaryReadInt16(file); something2 = binaryReadByte(file); numberOfSections = binaryReadUInt8(file); something3 = binaryReadByte(file); for(i = 0; i < numberOfColors; i++) { unsigned char colorNumber; binaryReadUInt16(file); binaryReadUInt16(file); colorNumber = binaryReadUInt8(file); embPattern_addThread(pattern, jefThreads[colorNumber]); fseek(file, 9, SEEK_CUR); } fseek(file, -2, SEEK_CUR); for(i = 0; !feof(file); i++) { unsigned char b0, b1; int flags; if(inJump) flags = JUMP; else flags = NORMAL; b0 = binaryReadUInt8(file); b1 = binaryReadUInt8(file); if(b0 == 0 && b1 == 0 && i > 0) flags = STOP; if(b0 == 0x80) { if(b1 == 3) { /*embPattern_addStitchRel(pattern, 0, 0, STOP); TODO: review this */ continue; } else if(b1 == 0x02) { inJump = 0; continue; } else if(b1 == 0x01) { unsigned short sx, sy; sx = binaryReadUInt8(file); sx = (unsigned short)(sx << 8 | binaryReadUInt8(file)); sy = binaryReadUInt8(file); sy = (unsigned short)(sy << 8 | binaryReadUInt8(file)); flags = TRIM; inJump = 1; embPattern_addStitchRel(pattern, shvDecodeShort(sx) / 10.0, shvDecodeShort(sy) / 10.0, flags, 1); continue; } } dx = shvDecode(b0); dy = shvDecode(b1); embPattern_addStitchRel(pattern, dx / 10.0, dy / 10.0, flags, 1); } embPattern_addStitchRel(pattern, 0, 0, END, 1); embPattern_flipVertical(pattern); fclose(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; }