forked from Embroidermodder/libembroidery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-stx.c
242 lines (215 loc) · 8.39 KB
/
format-stx.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
typedef struct SubDescriptor_ {
int someNum; /* TODO: better variable naming */
int someInt; /* TODO: better variable naming */
int someOtherInt; /* TODO: better variable naming */
char* colorCode;
char* colorName;
} SubDescriptor;
typedef struct StxThread_ {
char* colorCode;
char* colorName;
char* sectionName;
SubDescriptor* subDescriptors;
EmbColor stxColor;
} StxThread;
static int stxReadThread(StxThread* thread, EmbFile* file)
{
int j, colorNameLength, sectionNameLength;
int somethingSomething, somethingSomething2, somethingElse, numberOfOtherDescriptors; /* TODO: determine what these represent */
int codeLength = 0;
char* codeBuff = 0;
char* codeNameBuff = 0;
char* sectionNameBuff = 0;
EmbColor col;
unsigned char whatIsthis; /* TODO: determine what this represents */
if (!thread) {
embLog("ERROR: format-stx.c stxReadThread(), thread argument is null\n");
return 0;
}
if (!file) {
embLog("ERROR: format-stx.c stxReadThread(), file argument is null\n");
return 0;
}
codeLength = binaryReadUInt8(file);
codeBuff = (char*)malloc(codeLength);
if (!codeBuff) {
embLog("ERROR: format-stx.c stxReadThread(), unable to allocate memory for codeBuff\n");
return 0;
}
binaryReadBytes(file, (unsigned char*)codeBuff, codeLength); /* TODO: check return value */
thread->colorCode = codeBuff;
colorNameLength = binaryReadUInt8(file);
codeNameBuff = (char*)malloc(colorNameLength);
if (!codeNameBuff) {
embLog("ERROR: format-stx.c stxReadThread(), unable to allocate memory for codeNameBuff\n");
return 0;
}
binaryReadBytes(file, (unsigned char*)codeNameBuff, colorNameLength); /* TODO: check return value */
thread->colorName = codeNameBuff;
col.r = binaryReadUInt8(file);
col.b = binaryReadUInt8(file);
col.g = binaryReadUInt8(file);
whatIsthis = binaryReadUInt8(file);
sectionNameLength = binaryReadUInt8(file);
sectionNameBuff = (char*)malloc(sectionNameLength);
if (!sectionNameBuff) {
embLog("ERROR: format-stx.c stxReadThread(), unable to allocate memory for sectionNameBuff\n");
return 0;
}
binaryReadBytes(file, (unsigned char*)sectionNameBuff, sectionNameLength); /* TODO: check return value */
thread->sectionName = sectionNameBuff;
somethingSomething = binaryReadInt32(file);
somethingSomething2 = binaryReadInt32(file);
somethingElse = binaryReadInt32(file);
numberOfOtherDescriptors = binaryReadInt16(file);
thread->subDescriptors = (SubDescriptor*)malloc(sizeof(SubDescriptor) * numberOfOtherDescriptors);
if (!thread->subDescriptors) {
embLog("ERROR: format-stx.c stxReadThread(), unable to allocate memory for thread->subDescriptors\n");
return 0;
}
for (j = 0; j < numberOfOtherDescriptors; j++) {
SubDescriptor sd;
char *subCodeBuff, *subColorNameBuff;
int subCodeLength, subColorNameLength;
sd.someNum = binaryReadInt16(file);
/* Debug.Assert(sd.someNum == 1); TODO: review */
sd.someInt = binaryReadInt32(file);
subCodeLength = binaryReadUInt8(file);
subCodeBuff = (char*)malloc(subCodeLength);
if (!subCodeBuff) {
embLog("ERROR: format-stx.c stxReadThread(), unable to allocate memory for subCodeBuff\n");
return 0;
}
binaryReadBytes(file, (unsigned char*)subCodeBuff, subCodeLength); /* TODO: check return value */
sd.colorCode = subCodeBuff;
subColorNameLength = binaryReadUInt8(file);
subColorNameBuff = (char*)malloc(subColorNameLength);
if (!subColorNameBuff) {
embLog("ERROR: format-stx.c stxReadThread(), unable to allocate memory for subColorNameBuff\n");
return 0;
}
binaryReadBytes(file, (unsigned char*)subColorNameBuff, subColorNameLength); /* TODO: check return value */
sd.colorName = subColorNameBuff;
sd.someOtherInt = binaryReadInt32(file);
thread->subDescriptors[j] = sd;
}
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. */
static int readStx(EmbPattern* pattern, EmbFile* file, const char* fileName)
{
int i, threadCount;
unsigned char* gif = 0;
/* public Bitmap Image; */
StxThread* stxThreads = 0;
unsigned char headerBytes[7];
char* header = 0;
char filetype[4], version[5];
int paletteLength, imageLength, something1, stitchDataOffset, something3, threadDescriptionOffset, stitchCount, left, right, colors;
int val1, val2, val3, val4, val5, val6;
int vala1, vala2, vala3, vala4, vala5, vala6;
int bottom, top;
binaryReadBytes(file, headerBytes, 7); /* TODO: check return value */
header = (char*)headerBytes;
memcpy(filetype, &header[0], 3);
memcpy(version, &header[3], 4);
filetype[3] = '\0';
version[4] = '\0';
binaryReadByte(file);
paletteLength = binaryReadInt32(file);
imageLength = binaryReadInt32(file);
something1 = binaryReadInt32(file);
stitchDataOffset = binaryReadInt32(file);
something3 = binaryReadInt32(file);
threadDescriptionOffset = binaryReadInt32(file);
stitchCount = binaryReadInt32(file);
colors = binaryReadInt32(file);
right = binaryReadInt16(file);
left = binaryReadInt16(file);
bottom = binaryReadInt16(file);
top = binaryReadInt16(file);
gif = (unsigned char*)malloc(imageLength);
if (!gif) {
embLog("ERROR: format-stx.c readStx(), unable to allocate memory for gif\n");
return 0;
}
binaryReadBytes(file, gif, imageLength); /* TODO: check return value */
/*Stream s2 = new MemoryStream(gif); TODO: review */
/*Image = new Bitmap(s2); TODO: review */
threadCount = binaryReadInt16(file);
stxThreads = (StxThread*)malloc(sizeof(StxThread) * threadCount);
if (!stxThreads) {
embLog("ERROR: format-stx.c readStx(), unable to allocate memory for stxThreads\n");
return 0;
}
for (i = 0; i < threadCount; i++) {
EmbThread t;
StxThread st;
stxReadThread(&st, file);
t.color.r = st.stxColor.r;
t.color.g = st.stxColor.g;
t.color.b = st.stxColor.b;
t.description = st.colorName;
t.catalogNumber = st.colorCode;
embPattern_addThread(pattern, t);
stxThreads[i] = st;
}
binaryReadInt32(file);
binaryReadInt32(file);
binaryReadInt32(file);
binaryReadInt16(file);
binaryReadUInt8(file);
val1 = binaryReadInt16(file);
val2 = binaryReadInt16(file);
val3 = binaryReadInt16(file);
val4 = binaryReadInt16(file);
val5 = binaryReadInt16(file); /* 0 */
val6 = binaryReadInt16(file); /* 0 */
vala1 = binaryReadInt16(file);
vala2 = binaryReadInt16(file);
vala3 = binaryReadInt16(file);
vala4 = binaryReadInt16(file);
vala5 = binaryReadInt16(file); /* 0 */
vala6 = binaryReadInt16(file); /* 0 */
binaryReadInt32(file); /* 0 */
binaryReadInt32(file); /* 0 */
/* br.BaseStream.Position = stitchDataOffset; TODO: review */
for (i = 1; i < stitchCount;) {
char b0 = binaryReadByte(file);
char b1 = binaryReadByte(file);
if (b0 == -128) {
switch (b1) {
case 1:
b0 = binaryReadByte(file);
b1 = binaryReadByte(file);
/*embPattern_addStitchRel(b0, b1, STOP); TODO: review */
i++;
break;
case 2:
b0 = binaryReadByte(file);
b1 = binaryReadByte(file);
embPattern_addStitchRel(pattern, b0 / 10.0, b1 / 10.0, JUMP, 1);
i++;
break;
case -94:
/* TODO: Is this a syncronize? If so document it in the comments. */
break;
default:
/*Debugger.Break(); TODO: review */
break;
}
} else {
embPattern_addStitchRel(pattern, b0 / 10.0, b1 / 10.0, NORMAL, 1);
i++;
}
}
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 writeStx(EmbPattern* pattern, EmbFile* file, const char* fileName)
{
return 0; /*TODO: finish writeStx */
}