-
Notifications
You must be signed in to change notification settings - Fork 6
/
pes.php
425 lines (394 loc) · 9.07 KB
/
pes.php
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?php
/*
Embroidery Reader - an application to view .pes embroidery designs
Copyright (C) 2009 Nathan Crawford
Converted from C# to php 2009 Robert Heel
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
A copy of the full GPL 2 license can be found in the docs directory.
You can contact me at http://www.njcrawford.com/contact.php.
*/
class color {
public $r;
public $g;
public $b;
function __construct($r=0,$g=0,$b=0){
$this->r=$r;
$this->g=$g;
$this->b=$b;
}
}
class point {
public $x;
public $y;
function __construct($x=0,$y=0){
$this->x=$x;
$this->y=$y;
}
}
class stitchBlock {
public $color; // color
public $colorIndex; // int32
public $stitchesTotal; // int32
public $stitches; // array
function __construct(){
$this->color=new color();
}
}
class PesFile {
var $imageWidth;
var $imageHeight;
var $pesHeader; // int64
var $blocks; // stitchBlock
var $colorTable;
var $startStitches = 0; // int64
var $lastError = ""; // string
var $pesNum = ""; // string
var $min; // point
//means we couldn't figure out some or all
//of the colors, best guess will be used
private $colorWarning = false; // bool
private $formatWarning = false; // bool
private $classWarning = false; // bool
function PesFile($filename) {
$this->OpenFile($filename);
}
function OpenFile($filename) {
$file=fopen($filename,'r');
if($file) {
// 4 bytes
$startFileSig = fread($file,4);
if ($startFileSig!="#PES") {
//this is not a file that we can read
$this->lastError = "Missing #PES at beginning of file";
fclose($file);
return;
}
// 4 bytes
fread($file,4);
// pecstart
$pecstart = $this->readInt32($file,4);
// Design dimensions
$this->imageWidth=$this->readInt16($file);
$this->imageHeight=$this->readInt16($file);
// Color table
fseek($file,$pecstart+48);
$numColors = $this->readInt8($file)+1;
for ($x = 0; $x < $numColors; $x++) {
$colorList[] = $this->readInt8($file);
}
// Stitch data
fseek($file,$pecstart + 532);
$thisPartIsDone = false;
$prevX = 0;
$prevY = 0;
$maxX = 0;
$minX = 0;
$maxY = 0;
$minY = 0;
$colorNum = -1;
$colorIndex = 0;
$tempStitches = array();
while (!$thisPartIsDone && !feof($file)) {
$val1 = $this->readInt8($file);
$val2 = $this->readInt8($file);
if ($val1 == 255 && $val2 == 0) {
//end of stitches
$thisPartIsDone = true;
//add the last block
$curBlock = new stitchBlock();
$curBlock->stitches = $tempStitches;
$curBlock->stitchesTotal = count($tempStitches);
$colorNum++;
$colorIndex = $colorList[$colorNum];
$curBlock->colorIndex = $colorIndex;
$curBlock->color = $this->getColorFromIndex($colorIndex);
$this->blocks[]=$curBlock;
} elseif ($val1 == 254 && $val2 == 176) {
//color switch, start a new block
$curBlock = new stitchBlock();
$curBlock->stitches = $tempStitches;
$curBlock->stitchesTotal = count($tempStitches);
$colorNum++;
$colorIndex = $colorList[$colorNum];
$curBlock->colorIndex = $colorIndex;
$curBlock->color = $this->getColorFromIndex($colorIndex);
$this->blocks[]=$curBlock;
$tempStitches = array();
//read useless(?) byte
fread($file,1);
} else {
$deltaX = 0;
$deltaY = 0;
if (($val1 & 128) == 128) {//$80
//this is a jump stitch
$deltaX = (($val1 & 15) * 256) + $val2;
if (($deltaX & 2048) == 2048) {//$0800
$deltaX = $deltaX - 4096;
}
//read next byte for Y value
$val2 = $this->readInt8($file);
} else {
//normal stitch
$deltaX = $val1;
if ($deltaX > 63) {
$deltaX = $deltaX - 128;
}
}
if (($val2 & 128) == 128) {//$80
//this is a jump stitch
$val3 = $this->readInt8($file);
$deltaY = (($val2 & 15) * 256) + $val3;
if (($deltaY & 2048) == 2048) {
$deltaY = $deltaY - 4096;
}
} else {
//normal stitch
$deltaY = $val2;
if ($deltaY > 63) {
$deltaY = $deltaY - 128;
}
}
$prevX = $prevX + $deltaX;
$prevY = $prevY + $deltaY;
$tempStitches[]=new point($prevX, $prevY);
if ($prevX > $maxX) {
$maxX = $prevX;
} elseif ($prevX < $minX) {
$minX = $prevX;
}
if ($prevY > $maxY) {
$maxY = $prevY;
} elseif ($prevY < $minY) {
$minY = $prevY;
}
}
}
$this->imageWidth = $maxX - $minX;
$this->imageHeight = $maxY - $minY;
$this->min->x = $minX;
$this->min->y = $minY;
}
}
function readInt8($file){
return(ord(fread($file,1)));
}
function readInt16($file){
return(array_shift(unpack('v',fread($file,2))));
}
function readInt32($file){
return(array_shift(unpack('V',fread($file,4))));
}
function getColorFromIndex($index){
switch ($index){
case 1:
$color=new color(14, 31, 124);
break;
case 2:
$color=new color(10, 85, 163);
break;
case 3:
$color=new color(48, 135, 119);
break;
case 4:
$color=new color(75, 107, 175);
break;
case 5:
$color=new color(237, 23, 31);
break;
case 6:
$color=new color(209, 92, 0);
break;
case 7:
$color=new color(145, 54, 151);
break;
case 8:
$color=new color(228, 154, 203);
break;
case 9:
$color=new color(145, 95, 172);
break;
case 10:
$color=new color(157, 214, 125);
break;
case 11:
$color=new color(232, 169, 0);
break;
case 12:
$color=new color(254, 186, 53);
break;
case 13:
$color=new color(255, 255, 0);
break;
case 14:
$color=new color(112, 188, 31);
break;
case 15:
$color=new color(192, 148, 0);
break;
case 16:
$color=new color(168, 168, 168);
break;
case 17:
$color=new color(123, 111, 0);
break;
case 18:
$color=new color(255, 255, 179);
break;
case 19:
$color=new color(79, 85, 86);
break;
case 20:
$color=new color(0, 0, 0);
break;
case 21:
$color=new color(11, 61, 145);
break;
case 22:
$color=new color(119, 1, 118);
break;
case 23:
$color=new color(41, 49, 51);
break;
case 24:
$color=new color(42, 19, 1);
break;
case 25:
$color=new color(246, 74, 138);
break;
case 26:
$color=new color(178, 118, 36);
break;
case 27:
$color=new color(252, 187, 196);
break;
case 28:
$color=new color(254, 55, 15);
break;
case 29:
$color=new color(240, 240, 240);
break;
case 30:
$color=new color(106, 28, 138);
break;
case 31:
$color=new color(168, 221, 196);
break;
case 32:
$color=new color(37, 132, 187);
break;
case 33:
$color=new color(254, 179, 67);
break;
case 34:
$color=new color(255, 240, 141);
break;
case 35:
$color=new color(208, 166, 96);
break;
case 36:
$color=new color(209, 84, 0);
break;
case 37:
$color=new color(102, 186, 73);
break;
case 38:
$color=new color(19, 74, 70);
break;
case 39:
$color=new color(135, 135, 135);
break;
case 40:
$color=new color(216, 202, 198);
break;
case 41:
$color=new color(67, 86, 7);
break;
case 42:
$color=new color(254, 227, 197);
break;
case 43:
$color=new color(249, 147, 188);
break;
case 44:
$color=new color(0, 56, 34);
break;
case 45:
$color=new color(178, 175, 212);
break;
case 46:
$color=new color(104, 106, 176);
break;
case 47:
$color=new color(239, 227, 185);
break;
case 48:
$color=new color(247, 56, 102);
break;
case 49:
$color=new color(181, 76, 100);
break;
case 50:
$color=new color(19, 43, 26);
break;
case 51:
$color=new color(199, 1, 85);
break;
case 52:
$color=new color(254, 158, 50);
break;
case 53:
$color=new color(168, 222, 235);
break;
case 54:
$color=new color(0, 103, 26);
break;
case 55:
$color=new color(78, 41, 144);
break;
case 56:
$color=new color(47, 126, 32);
break;
case 57:
$color=new color(253, 217, 222);
break;
case 58:
$color=new color(255, 217, 17);
break;
case 59:
$color=new color(9, 91, 166);
break;
case 60:
$color=new color(240, 249, 112);
break;
case 61:
$color=new color(227, 243, 91);
break;
case 62:
$color=new color(255, 200, 100);
break;
case 63:
$color=new color(255, 200, 150);
break;
case 64:
$color=new color(255, 200, 200);
break;
default:
$color=new color(0, 0, 0);
$this->colorWarning = true;
break;
}
return $color;
}
}
?>