forked from Embroidermodder/libembroidery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-jef.c
374 lines (343 loc) · 13 KB
/
format-jef.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
static const EmbThread jefThreads[] = {
{ { 0, 0, 0 }, "Black", "" },
{ { 0, 0, 0 }, "Black", "" },
{ { 255, 255, 255 }, "White", "" },
{ { 255, 255, 23 }, "Yellow", "" },
{ { 250, 160, 96 }, "Orange", "" },
{ { 92, 118, 73 }, "Olive Green", "" },
{ { 64, 192, 48 }, "Green", "" },
{ { 101, 194, 200 }, "Sky", "" },
{ { 172, 128, 190 }, "Purple", "" },
{ { 245, 188, 203 }, "Pink", "" },
{ { 255, 0, 0 }, "Red", "" },
{ { 192, 128, 0 }, "Brown", "" },
{ { 0, 0, 240 }, "Blue", "" },
{ { 228, 195, 93 }, "Gold", "" },
{ { 165, 42, 42 }, "Dark Brown", "" },
{ { 213, 176, 212 }, "Pale Violet", "" },
{ { 252, 242, 148 }, "Pale Yellow", "" },
{ { 240, 208, 192 }, "Pale Pink", "" },
{ { 255, 192, 0 }, "Peach", "" },
{ { 201, 164, 128 }, "Beige", "" },
{ { 155, 61, 75 }, "Wine Red", "" },
{ { 160, 184, 204 }, "Pale Sky", "" },
{ { 127, 194, 28 }, "Yellow Green", "" },
{ { 185, 185, 185 }, "Silver Grey", "" },
{ { 160, 160, 160 }, "Grey", "" },
{ { 152, 214, 189 }, "Pale Aqua", "" },
{ { 184, 240, 240 }, "Baby Blue", "" },
{ { 54, 139, 160 }, "Powder Blue", "" },
{ { 79, 131, 171 }, "Bright Blue", "" },
{ { 56, 106, 145 }, "Slate Blue", "" },
{ { 0, 32, 107 }, "Nave Blue", "" },
{ { 229, 197, 202 }, "Salmon Pink", "" },
{ { 249, 103, 107 }, "Coral", "" },
{ { 227, 49, 31 }, "Burnt Orange", "" },
{ { 226, 161, 136 }, "Cinnamon", "" },
{ { 181, 148, 116 }, "Umber", "" },
{ { 228, 207, 153 }, "Blonde", "" },
{ { 225, 203, 0 }, "Sunflower", "" },
{ { 225, 173, 212 }, "Orchid Pink", "" },
{ { 195, 0, 126 }, "Peony Purple", "" },
{ { 128, 0, 75 }, "Burgundy", "" },
{ { 160, 96, 176 }, "Royal Purple", "" },
{ { 192, 64, 32 }, "Cardinal Red", "" },
{ { 202, 224, 192 }, "Opal Green", "" },
{ { 137, 152, 86 }, "Moss Green", "" },
{ { 0, 170, 0 }, "Meadow Green", "" },
{ { 33, 138, 33 }, "Dark Green", "" },
{ { 93, 174, 148 }, "Aquamarine", "" },
{ { 76, 191, 143 }, "Emerald Green", "" },
{ { 0, 119, 114 }, "Peacock Green", "" },
{ { 112, 112, 112 }, "Dark Grey", "" },
{ { 242, 255, 255 }, "Ivory White", "" },
{ { 177, 88, 24 }, "Hazel", "" },
{ { 203, 138, 7 }, "Toast", "" },
{ { 247, 146, 123 }, "Salmon", "" },
{ { 152, 105, 45 }, "Cocoa Brown", "" },
{ { 162, 113, 72 }, "Sienna", "" },
{ { 123, 85, 74 }, "Sepia", "" },
{ { 79, 57, 70 }, "Dark Sepia", "" },
{ { 82, 58, 151 }, "Violet Blue", "" },
{ { 0, 0, 160 }, "Blue Ink", "" },
{ { 0, 150, 222 }, "Solar Blue", "" },
{ { 178, 221, 83 }, "Green Dust", "" },
{ { 250, 143, 187 }, "Crimson", "" },
{ { 222, 100, 158 }, "Floral Pink", "" },
{ { 181, 80, 102 }, "Wine", "" },
{ { 94, 87, 71 }, "Olive Drab", "" },
{ { 76, 136, 31 }, "Meadow", "" },
{ { 228, 220, 121 }, "Mustard", "" },
{ { 203, 138, 26 }, "Yellow Ochre", "" },
{ { 198, 170, 66 }, "Old Gold", "" },
{ { 236, 176, 44 }, "Honeydew", "" },
{ { 248, 128, 64 }, "Tangerine", "" },
{ { 255, 229, 5 }, "Canary Yellow", "" },
{ { 250, 122, 122 }, "Vermillion", "" },
{ { 107, 224, 0 }, "Bright Green", "" },
{ { 56, 108, 174 }, "Ocean Blue", "" },
{ { 227, 196, 180 }, "Beige Grey", "" },
{ { 227, 172, 129 }, "Bamboo", "" }
};
#define HOOP_126X110 0
#define HOOP_110X110 1
#define HOOP_50X50 2
#define HOOP_140X200 3
#define HOOP_230X200 4
static int jefGetHoopSize(int width, int height)
{
if (width < 50 && height < 50) {
return HOOP_50X50;
}
if (width < 110 && height < 110) {
return HOOP_110X110;
}
if (width < 140 && height < 200) {
return HOOP_140X200;
}
return ((int)HOOP_110X110);
}
static char jefDecode(unsigned char inputByte)
{
if (inputByte >= 0x80)
return (char)((-~inputByte) - 1);
return ((char)inputByte);
}
static void jefSetHoopFromId(EmbPattern* pattern, int hoopCode)
{
if (!pattern) {
embLog("ERROR: format-jef.c jefSetHoopFromId(), pattern argument is null\n");
return;
}
switch (hoopCode) {
case HOOP_126X110:
pattern->hoop.height = 126.0;
pattern->hoop.width = 110.0;
break;
case HOOP_50X50:
pattern->hoop.height = 50.0;
pattern->hoop.width = 50.0;
break;
case HOOP_110X110:
pattern->hoop.height = 110.0;
pattern->hoop.width = 110.0;
break;
case HOOP_140X200:
pattern->hoop.height = 140.0;
pattern->hoop.width = 200.0;
break;
case HOOP_230X200:
pattern->hoop.height = 230.0;
pattern->hoop.width = 200.0;
break;
}
}
struct hoop_padding {
int left;
int right;
int top;
int bottom;
};
/*! 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 readJef(EmbPattern* pattern, EmbFile* file, const char* fileName)
{
int stitchOffset, formatFlags, numberOfColors, numberOfStitchs;
int hoopSize, i;
struct hoop_padding bounds, rectFrom110x110, rectFrom50x50, rectFrom200x140, rect_from_custom;
int stitchCount;
char date[8], time[8];
unsigned char b0 = 0, b1 = 0;
char dx = 0, dy = 0;
int flags = 0;
stitchOffset = binaryReadInt32(file);
formatFlags = binaryReadInt32(file); /* TODO: find out what this means */
binaryReadBytes(file, (unsigned char*)date, 8); /* TODO: check return value */
binaryReadBytes(file, (unsigned char*)time, 8); /* TODO: check return value */
numberOfColors = binaryReadInt32(file);
numberOfStitchs = binaryReadInt32(file);
hoopSize = binaryReadInt32(file);
jefSetHoopFromId(pattern, hoopSize);
bounds.left = binaryReadInt32(file);
bounds.top = binaryReadInt32(file);
bounds.right = binaryReadInt32(file);
bounds.bottom = binaryReadInt32(file);
rectFrom110x110.left = binaryReadInt32(file);
rectFrom110x110.top = binaryReadInt32(file);
rectFrom110x110.right = binaryReadInt32(file);
rectFrom110x110.bottom = binaryReadInt32(file);
rectFrom50x50.left = binaryReadInt32(file);
rectFrom50x50.top = binaryReadInt32(file);
rectFrom50x50.right = binaryReadInt32(file);
rectFrom50x50.bottom = binaryReadInt32(file);
rectFrom200x140.left = binaryReadInt32(file);
rectFrom200x140.top = binaryReadInt32(file);
rectFrom200x140.right = binaryReadInt32(file);
rectFrom200x140.bottom = binaryReadInt32(file);
rect_from_custom.left = binaryReadInt32(file);
rect_from_custom.top = binaryReadInt32(file);
rect_from_custom.right = binaryReadInt32(file);
rect_from_custom.bottom = binaryReadInt32(file);
for (i = 0; i < numberOfColors; i++) {
embPattern_addThread(pattern, jefThreads[binaryReadInt32(file) % 79]);
}
embFile_seek(file, stitchOffset, SEEK_SET);
stitchCount = 0;
while (stitchCount < numberOfStitchs + 100) {
flags = NORMAL;
b0 = (unsigned char)embFile_getc(file);
if (embFile_eof(file)) {
break;
}
b1 = (unsigned char)embFile_getc(file);
if (embFile_eof(file)) {
break;
}
if (b0 == 0x80) {
if (b1 & 1) {
b0 = (unsigned char)embFile_getc(file);
if (embFile_eof(file))
break;
b1 = (unsigned char)embFile_getc(file);
if (embFile_eof(file))
break;
flags = STOP;
} else if ((b1 == 2) || (b1 == 4) || b1 == 6) {
flags = TRIM;
b0 = (unsigned char)embFile_getc(file);
if (embFile_eof(file)) {
break;
}
b1 = (unsigned char)embFile_getc(file);
if (embFile_eof(file)) {
break;
}
} else if (b1 == 0x10) {
embPattern_addStitchRel(pattern, 0.0, 0.0, END, 1);
break;
}
}
dx = jefDecode(b0);
dy = jefDecode(b1);
embPattern_addStitchRel(pattern, dx / 10.0, dy / 10.0, flags, 1);
}
return 1;
}
static void jefEncode(unsigned char* b, char dx, char dy, int flags)
{
if (!b) {
embLog("ERROR: format-jef.c expEncode(), b argument is null\n");
return;
}
if (flags == STOP) {
b[0] = 0x80;
b[1] = 1;
b[2] = dx;
b[3] = dy;
} else if (flags == END) {
b[0] = 0x80;
b[1] = 0x10;
b[2] = 0;
b[3] = 0;
} else if (flags == TRIM || flags == JUMP) {
b[0] = 0x80;
b[1] = 2;
b[2] = dx;
b[3] = dy;
} else {
b[0] = dx;
b[1] = dy;
}
}
/*! 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 writeJef(EmbPattern* pattern, EmbFile* file, const char* fileName)
{
int colorlistSize, minColors, designWidth, designHeight, i;
EmbRect boundingRect;
EmbTime time;
EmbStitch st;
double dx = 0.0, dy = 0.0, xx = 0.0, yy = 0.0;
unsigned char b[4];
embPattern_correctForMaxStitchLength(pattern, 12.7, 12.7);
colorlistSize = pattern->threads->count;
minColors = EMB_MAX_2(colorlistSize, 6);
binaryWriteInt(file, 0x74 + (minColors * 4));
binaryWriteInt(file, 0x0A);
embTime_initNow(&time);
writeInt(file, (int)(time.year + 1900), 4);
writeInt(file, (int)(time.month + 1), 2); /* TODO: pad with zeros all date/time entries */
writeInt(file, (int)(time.day), 2);
writeInt(file, (int)(time.hour), 2);
writeInt(file, (int)(time.minute), 2);
writeInt(file, (int)(time.second), 2);
binaryWriteByte(file, 0x00);
binaryWriteByte(file, 0x00);
binaryWriteInt(file, pattern->threads->count);
binaryWriteInt(file, pattern->stitchList->count + EMB_MAX_2(0, (6 - colorlistSize) * 2) + 1);
boundingRect = embPattern_calcBoundingBox(pattern);
designWidth = (int)(embRect_width(boundingRect) * 10.0);
designHeight = (int)(embRect_width(boundingRect) * 10.0);
binaryWriteInt(file, jefGetHoopSize(designWidth, designHeight));
/* Distance from center of Hoop */
binaryWriteInt(file, (int)(designWidth / 2)); /* left */
binaryWriteInt(file, (int)(designHeight / 2)); /* top */
binaryWriteInt(file, (int)(designWidth / 2)); /* right */
binaryWriteInt(file, (int)(designHeight / 2)); /* bottom */
/* Distance from default 110 x 110 Hoop */
if (EMB_MIN_2(550 - designWidth / 2, 550 - designHeight / 2) >= 0) {
binaryWriteInt(file, EMB_MAX_2(-1, 550 - designWidth / 2)); /* left */
binaryWriteInt(file, EMB_MAX_2(-1, 550 - designHeight / 2)); /* top */
binaryWriteInt(file, EMB_MAX_2(-1, 550 - designWidth / 2)); /* right */
binaryWriteInt(file, EMB_MAX_2(-1, 550 - designHeight / 2)); /* bottom */
} else {
binaryWriteInt(file, -1);
binaryWriteInt(file, -1);
binaryWriteInt(file, -1);
binaryWriteInt(file, -1);
}
/* Distance from default 50 x 50 Hoop */
if (EMB_MIN_2(250 - designWidth / 2, 250 - designHeight / 2) >= 0) {
binaryWriteInt(file, (int)EMB_MAX_2(-1, 250 - designWidth / 2)); /* left */
binaryWriteInt(file, (int)EMB_MAX_2(-1, 250 - designHeight / 2)); /* top */
binaryWriteInt(file, (int)EMB_MAX_2(-1, 250 - designWidth / 2)); /* right */
binaryWriteInt(file, (int)EMB_MAX_2(-1, 250 - designHeight / 2)); /* bottom */
} else {
binaryWriteInt(file, -1);
binaryWriteInt(file, -1);
binaryWriteInt(file, -1);
binaryWriteInt(file, -1);
}
/* Distance from default 140 x 200 Hoop */
binaryWriteInt(file, (int)(700 - designWidth / 2)); /* left */
binaryWriteInt(file, (int)(1000 - designHeight / 2)); /* top */
binaryWriteInt(file, (int)(700 - designWidth / 2)); /* right */
binaryWriteInt(file, (int)(1000 - designHeight / 2)); /* bottom */
/* repeated Distance from default 140 x 200 Hoop */
/* TODO: Actually should be distance to custom hoop */
binaryWriteInt(file, (int)(630 - designWidth / 2)); /* left */
binaryWriteInt(file, (int)(550 - designHeight / 2)); /* top */
binaryWriteInt(file, (int)(630 - designWidth / 2)); /* right */
binaryWriteInt(file, (int)(550 - designHeight / 2)); /* bottom */
for (i = 0; i < pattern->threads->count; i++) {
int j = embThread_findNearestColor_fromThread(pattern->threads->thread[i].color, jefThreads, 79);
binaryWriteInt(file, j);
}
for (i = 0; i < (minColors - colorlistSize); i++) {
binaryWriteInt(file, 0x0D);
}
for (i = 0; i < pattern->stitchList->count; i++) {
st = pattern->stitchList->stitch[i];
dx = st.x * 10.0 - xx;
dy = st.y * 10.0 - yy;
xx = st.x * 10.0;
yy = st.y * 10.0;
jefEncode(b, (char)roundDouble(dx), (char)roundDouble(dy), st.flags);
if ((b[0] == 0x80) && ((b[1] == 1) || (b[1] == 2) || (b[1] == 4) || (b[1] == 0x10))) {
embFile_write(b, 1, 4, file);
} else {
embFile_write(b, 1, 2, file);
}
}
return 1;
}