/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writeInf(EmbPattern* pattern, const char* fileName) { EmbThreadList* pointer = 0; int i = 1, bytesRemaining; FILE* file = 0; if(!pattern) { embLog_error("format-inf.c writeInf(), pattern argument is null\n"); return 0; } if(!fileName) { embLog_error("format-inf.c writeInf(), fileName argument is null\n"); return 0; } file = fopen(fileName, "wb"); if(!file) { embLog_error("format-inf.c writeInf(), cannot open %s for writing\n", fileName); return 0; } binaryWriteUIntBE(file, 0x01); binaryWriteUIntBE(file, 0x08); /* write place holder offset */ binaryWriteUIntBE(file, 0x00); binaryWriteUIntBE(file, embThreadList_count(pattern->threadList)); pointer = pattern->threadList; while(pointer) { char buffer[50]; EmbColor c; c = pointer->thread.color; sprintf(buffer, "RGB(%d,%d,%d)", (int)c.r, (int)c.g, (int)c.b); binaryWriteUShortBE(file, (unsigned short)(14 + strlen(buffer))); /* record length */ binaryWriteUShortBE(file, (unsigned short)i); /* record number */ binaryWriteByte(file, c.r); binaryWriteByte(file, c.g); binaryWriteByte(file, c.b); binaryWriteUShortBE(file, (unsigned short)i); /* needle number */ binaryWriteBytes(file, "RGB\0", 4); fprintf(file, buffer); binaryWriteByte(file, 0); pointer = pointer->next; i++; } fseek(file, -8, SEEK_END); bytesRemaining = ftell(file); fseek(file, 8, SEEK_SET); binaryWriteUIntBE(file, bytesRemaining); fclose(file); return 1; }
void writePecStitches(EmbPattern* pattern, EmbFile* file, const char* fileName) { EmbStitchList* tempStitches = 0; EmbRect bounds; unsigned char image[38][48]; int i, flen, currentThreadCount, graphicsOffsetLocation, graphicsOffsetValue, height, width; double xFactor, yFactor; const char* forwardSlashPos = strrchr(fileName, '/'); const char* backSlashPos = strrchr(fileName, '\\'); const char* dotPos = strrchr(fileName, '.'); const char* start = 0; if(!pattern) { embLog_error("format-pec.c writePecStitches(), pattern argument is null\n"); return; } if(!file) { embLog_error("format-pec.c writePecStitches(), file argument is null\n"); return; } if(!fileName) { embLog_error("format-pec.c writePecStitches(), fileName argument is null\n"); return; } if(forwardSlashPos) { start = forwardSlashPos + 1; } if(backSlashPos && backSlashPos > start) { start = backSlashPos + 1; } if(!start) { start = fileName; } binaryWriteBytes(file, "LA:", 3); flen = (int)(dotPos - start); while(start < dotPos) { binaryWriteByte(file, (unsigned char)*start); start++; } for(i = 0; i < (int)(16-flen); i++) { binaryWriteByte(file, (unsigned char)0x20); } binaryWriteByte(file, 0x0D); for(i = 0; i < 12; i++) { binaryWriteByte(file, (unsigned char)0x20); } binaryWriteByte(file, (unsigned char)0xFF); binaryWriteByte(file, (unsigned char)0x00); binaryWriteByte(file, (unsigned char)0x06); binaryWriteByte(file, (unsigned char)0x26); for(i = 0; i < 12; i++) { binaryWriteByte(file, (unsigned char)0x20); } currentThreadCount = embThreadList_count(pattern->threadList); binaryWriteByte(file, (unsigned char)(currentThreadCount-1)); for(i = 0; i < currentThreadCount; i++) { binaryWriteByte(file, (unsigned char)embThread_findNearestColorInArray(embThreadList_getAt(pattern->threadList, i).color, (EmbThread*)pecThreads, pecThreadCount)); } for(i = 0; i < (int)(0x1CF - currentThreadCount); i++) { binaryWriteByte(file, (unsigned char)0x20); } binaryWriteShort(file, (short)0x0000); graphicsOffsetLocation = embFile_tell(file); /* placeholder bytes to be overwritten */ binaryWriteByte(file, (unsigned char)0x00); binaryWriteByte(file, (unsigned char)0x00); binaryWriteByte(file, (unsigned char)0x00); binaryWriteByte(file, (unsigned char)0x31); binaryWriteByte(file, (unsigned char)0xFF); binaryWriteByte(file, (unsigned char)0xF0); bounds = embPattern_calcBoundingBox(pattern); height = roundDouble(embRect_height(bounds)); width = roundDouble(embRect_width(bounds)); /* write 2 byte x size */ binaryWriteShort(file, (short)width); /* write 2 byte y size */ binaryWriteShort(file, (short)height); /* Write 4 miscellaneous int16's */ binaryWriteShort(file, (short)0x1E0); binaryWriteShort(file, (short)0x1B0); binaryWriteUShortBE(file, (unsigned short)(0x9000 | -roundDouble(bounds.left))); binaryWriteUShortBE(file, (unsigned short)(0x9000 | -roundDouble(bounds.top))); pecEncode(file, pattern); graphicsOffsetValue = embFile_tell(file) - graphicsOffsetLocation + 2; embFile_seek(file, graphicsOffsetLocation, SEEK_SET); binaryWriteByte(file, (unsigned char)(graphicsOffsetValue & 0xFF)); binaryWriteByte(file, (unsigned char)((graphicsOffsetValue >> 8) & 0xFF)); binaryWriteByte(file, (unsigned char)((graphicsOffsetValue >> 16) & 0xFF)); embFile_seek(file, 0x00, SEEK_END); /* Writing all colors */ clearImage(image); tempStitches = pattern->stitchList; yFactor = 32.0 / height; xFactor = 42.0 / width; while(tempStitches->next) { int x = roundDouble((tempStitches->stitch.xx - bounds.left) * xFactor) + 3; int y = roundDouble((tempStitches->stitch.yy - bounds.top) * yFactor) + 3; image[y][x] = 1; tempStitches = tempStitches->next; } writeImage(file, image); /* Writing each individual color */ tempStitches = pattern->stitchList; for(i = 0; i < currentThreadCount; i++) { clearImage(image); while(tempStitches->next) { int x = roundDouble((tempStitches->stitch.xx - bounds.left) * xFactor) + 3; int y = roundDouble((tempStitches->stitch.yy - bounds.top) * yFactor) + 3; if(tempStitches->stitch.flags & STOP) { tempStitches = tempStitches->next; break; } image[y][x] = 1; tempStitches = tempStitches->next; } writeImage(file, image); } }
void vp3WriteStringLen(EmbFile* file, const char* str, int len) { binaryWriteUShortBE(file, len); binaryWriteBytes(file, str, len); }