forked from Embroidermodder/libembroidery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-shv.c
177 lines (166 loc) · 6.85 KB
/
format-shv.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*****************************************
* SHV Colors
****************************************/
static const int shvThreadCount = 42;
static const EmbThread shvThreads[] = {
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 255 }, "Blue", "TODO:CATALOG_NUMBER" },
{ { 51, 204, 102 }, "Green", "TODO:CATALOG_NUMBER" },
{ { 255, 0, 0 }, "Red", "TODO:CATALOG_NUMBER" },
{ { 255, 0, 255 }, "Purple", "TODO:CATALOG_NUMBER" },
{ { 255, 255, 0 }, "Yellow", "TODO:CATALOG_NUMBER" },
{ { 127, 127, 127 }, "Grey", "TODO:CATALOG_NUMBER" },
{ { 51, 154, 255 }, "Light Blue", "TODO:CATALOG_NUMBER" },
{ { 0, 255, 0 }, "Light Green", "TODO:CATALOG_NUMBER" },
{ { 255, 127, 0 }, "Orange", "TODO:CATALOG_NUMBER" },
{ { 255, 160, 180 }, "Pink", "TODO:CATALOG_NUMBER" },
{ { 153, 75, 0 }, "Brown", "TODO:CATALOG_NUMBER" },
{ { 255, 255, 255 }, "White", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 255, 127, 127 }, "Light Red", "TODO:CATALOG_NUMBER" },
{ { 255, 127, 255 }, "Light Purple", "TODO:CATALOG_NUMBER" },
{ { 255, 255, 153 }, "Light Yellow", "TODO:CATALOG_NUMBER" },
{ { 192, 192, 192 }, "Light Grey", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 255, 165, 65 }, "Light Orange", "TODO:CATALOG_NUMBER" },
{ { 255, 204, 204 }, "Light Pink", "TODO:CATALOG_NUMBER" },
{ { 175, 90, 10 }, "Light Brown", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 127 }, "Dark Blue", "TODO:CATALOG_NUMBER" },
{ { 0, 127, 0 }, "Dark Green", "TODO:CATALOG_NUMBER" },
{ { 127, 0, 0 }, "Dark Red", "TODO:CATALOG_NUMBER" },
{ { 127, 0, 127 }, "Dark Purple", "TODO:CATALOG_NUMBER" },
{ { 200, 200, 0 }, "Dark Yellow", "TODO:CATALOG_NUMBER" },
{ { 60, 60, 60 }, "Dark Gray", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 0, 0, 0 }, "Black", "TODO:CATALOG_NUMBER" },
{ { 232, 63, 0 }, "Dark Orange", "TODO:CATALOG_NUMBER" },
{ { 255, 102, 122 }, "Dark Pink", "TODO:CATALOG_NUMBER" }
};
static char shvDecode(unsigned char inputByte)
{
if (inputByte >= 0x80) {
return (char)-((unsigned char)((~inputByte) + 1));
}
return ((char)inputByte);
}
static short shvDecodeShort(unsigned short inputByte)
{
if (inputByte > 0x8000) {
return (short)-((unsigned short)((~inputByte) + 1));
}
return ((short)inputByte);
}
/*! Reads a file with the given \a fileName and loads the data into \a pattern.
* Returns \c true if successful, otherwise returns \c false. */
static int readShv(EmbPattern* pattern, EmbFile* file, 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;
int stitchesPerColor[256];
int stitchesSinceChange = 0;
int currColorIndex = 0;
unsigned short sx, sy;
embFile_seek(file, strlen(headerText), SEEK_SET);
fileNameLength = binaryReadUInt8(file);
embFile_seek(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) {
embFile_seek(file, ((designHeight + 1) * designWidth) / 2, SEEK_CUR);
} else {
embFile_seek(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 int stitchCount, colorNumber;
stitchCount = binaryReadUInt32BE(file);
colorNumber = binaryReadUInt8(file);
embPattern_addThread(pattern, shvThreads[colorNumber % 43]);
stitchesPerColor[i] = stitchCount;
embFile_seek(file, 9, SEEK_CUR);
}
embFile_seek(file, -2, SEEK_CUR);
for (i = 0; !embFile_eof(file); i++) {
unsigned char b0, b1;
int flags;
if (inJump) {
flags = JUMP;
} else {
flags = NORMAL;
}
b0 = binaryReadUInt8(file);
b1 = binaryReadUInt8(file);
if (stitchesSinceChange >= stitchesPerColor[currColorIndex]) {
embPattern_addStitchRel(pattern, 0, 0, STOP, 1);
currColorIndex++;
stitchesSinceChange = 0;
}
if (b0 == 0x80) {
stitchesSinceChange++;
if (b1 == 3) {
continue;
} else if (b1 == 0x02) {
inJump = 0;
continue;
} else if (b1 == 0x01) {
stitchesSinceChange += 2;
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);
stitchesSinceChange++;
embPattern_addStitchRel(pattern, dx / 10.0, dy / 10.0, flags, 1);
}
embFile_close(file);
embPattern_end(pattern);
embPattern_flipVertical(pattern);
return 1;
}
/*! Writes the data from \a pattern to a file with the given \a fileName.
* Returns \c true if successful, otherwise returns \c false. */
static int writeShv(EmbPattern* pattern, EmbFile* file, const char* fileName)
{
return 0; /*TODO: finish writeShv */
}