aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Embroidery/libembroidery/format-mit.c
blob: 4df19a95f136775ba390e4147686a001052b71dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "format-mit.h"
#include "emb-file.h"
#include "emb-logging.h"
#include "helpers-binary.h"

static int mitDecodeStitch(unsigned char value)
{
	if (value & 0x80)
	{
		return -(value & 0x1F);
	}
    return value;
}

/*! 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->stitch.flags != END)
        embPattern_addStitchRel(pattern, 0, 0, END, 1);

    return 1;
}

static unsigned char mitEncodeStitch(double value)
{
	if (value < 0)
	{
		return 0x80 | (unsigned char)(-value);
	}
	return (unsigned char)value;
}

/*! 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;
}

/* kate: bom off; indent-mode cstyle; indent-width 4; replace-trailing-space-save on; */