Beispiel #1
0
int readXxx(EmbPattern* pattern, const char* fileName)
{
    FILE* 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;
    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 = fopen(fileName, "rb");
    if(!file)
    {
        embLog_error("format-xxx.c readXxx(), cannot open %s for reading\n", fileName);
        return 0;
    }

    fseek(file, 0x27, SEEK_SET);
    numberOfColors = binaryReadInt16(file);
    fseek(file, 0xFC, SEEK_SET);
    paletteOffset = binaryReadInt32(file);
    fseek(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);
    }
    fseek(file, 0x100, SEEK_SET);

    for(i = 0; !endOfStream && ftell(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);
            secondLast->next = NULL;
            embPattern_changeColor(pattern, pattern->currentColorIndex - 1);
        }
    }
    embPattern_addStitchRel(pattern, 0, 0, END, 1);
    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 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;
}