/*! 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;
}
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, *yValues, *attributeValues;
    EmbStitchList* pointer;
    double xx = 0.0;
    double yy = 0.0;
    int flags = 0;
    int i = 0;
    unsigned char* attributeCompressed, *xCompressed, *yCompressed, *decodedColors, *encodedColors;
	unsigned char prevByte = 0;
	EmbThreadList *colorPointer;

    FILE* file = fopen(fileName, "wb");
    if(file == 0)
    {
        /*TODO: set status here "Error opening HUS file for write:" */
        return 0;
    }

    stitchCount = embStitch_count(pattern->stitchList);
	minColors = embThread_count(pattern->threadList);
	decodedColors = (unsigned char *) malloc(minColors* 4 *sizeof(unsigned char));
	encodedColors = (unsigned char *) malloc(minColors* 4 *sizeof(unsigned char));
    /* 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));

    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);
    fclose(file);
    return 1;
}