-
Notifications
You must be signed in to change notification settings - Fork 4
/
pesdump.lpr
1821 lines (1544 loc) · 53.8 KB
/
pesdump.lpr
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* FPC 2.6.0 FPC 2.6.0 FPC 2.6.0 FPC 2.6.0 FPC 2.6.0 FPC 2.6.0 FPC 2.6.0 FPC 2. *)
program PesDump;
(* This is a no-frills program to parse and dump the content of a PES file as *)
(* described at https://edutechwiki.unige.ch/en/Embroidery_format_PES with no *)
(* attempt at converting it into a machine-readable format. The initial impetus *)
(* was having a file which crashed a Brother Innovis-97E when it was loaded, *)
(* this appeared (and with the help of this program is now confirmed) to be *)
(* because it contained pause commands but the ability to compare a troublesome *)
(* file against a good one would have been useful. *)
(* *)
(* It is written in the style of a conventional "recursive descent" parser, *)
(* without lookahead, and is intentionally naive to allow it to be read and *)
(* possibly modified by non-specialists. Assume that like most Pascal programs *)
(* the highest-level elements (i.e. the description of the overall file, the *)
(* description of the standard header etc.) are at the bottom of this program *)
(* unit. Copyright (c) 2020 Mark Morgan Lloyd *)
(********************************************************************************)
(* *)
(* The principal reference for factual information used while writing this *)
(* program was from EduTechWiki which is subject to the CC BY-NC-SA Licence. *)
(* The above document cites Trevor Adams's "Frno7's Wiki" as its major source, *)
(* this explicitly uses GFDLv1.3 and GPLv3. Trevor Adams cites Linus Torvalds's *)
(* PesConvert program which is not accompanied by a licence indication but is *)
(* hosted by kernel.org and as such it is reasonable to assume that the same *)
(* license (GPLv2 with no "or any later version" clause) is intended to apply. *)
(* Torvalds cites a PHP script by Robert Heel which is covered by GPLv2 with an *)
(* "any later version" clause, this indirectly cites Trevor Adams (GPLv3). *)
(* *)
(* Because of this mixed heritage, and taking into account that my use of the *)
(* EduTechWiki document (hence other cited material) has been restricted to *)
(* factual information plus some type and field names, and noting that Torvalds *)
(* omits an explicit license choice, I think it appropriate that this program *)
(* should be licensed using GPLv3.
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 3 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, see <https://www.gnu.org/licenses/>.
*)
(* MarkMLl *)
(* *)
(********************************************************************************)
// Current state: supports PES v1, some support for other versions roughed in
// but inoperative.
{$mode objfpc}{$H+}
uses
Classes, SysUtils, PatchAndIO, MidlevelIO,
StrUtils, DateUtils; (* Last two units are optional *)
(* If FPC version 3.2.0 or later is used, it is able to validate that error *)
(* messages correctly identify what function was being executed at the time: *)
(* this is important since it includes parsing rules where an error indicates *)
(* either something wrong in the input file or a flaw in the program. Versions *)
(* of FPC predating 2.2.4 lack the FPC_FULLVERSION predefined, so cannot fail *)
(* gracefully when they try to determine whether the CURRENTROUTINE expansion *)
(* is available. Other factors mandate that in practice FPC older than 2.6.0 *)
(* will be unable to compile this source file without significant modification. *)
{$undef HAS_CURRENTROUTINE }
{$if FPC_FULLVERSION >= 030200 } (* Requires FPC 2.2.4 minimum *)
{$define HAS_CURRENTROUTINE } (* Requires FPC 3.2.0 minimum *)
{$assertions on } (* Make sure name checks are operative *)
{$endif FPC_FULLVERSION }
(* State variables: rule stack for diagnostics, counters etc. *)
var
params: TStringList= nil;
hasOptions: boolean= false;
hasCommands: boolean= false;
pesIn, pesOut: file;
ruleStack: TStringList;
poppedRule: string= '[Empty]';
(* Counters and min/max sizes output at program termination. *)
countPesStitchesNormal: longword= 0;
countPesStitchesJump: longword= 0;
countPesStitchesOther: longword= 0;
countPesStitchesTotal: longword= 0;
countPesColours: longword= 0;
minPesX: longint= 0;
maxPesX: longint= 0;
minPesY: longint= 0;
maxPesY: longint= 0;
countPecHalfstitches: longword= 0;
countPecColourChanges: longword= 0;
countPecPauseCommands: longword= 0;
countPecTrimCommands: longword= 0;
countPecJumpCommands: longword= 0;
minPecX: longint= 0;
maxPecX: longint= 0;
minPecY: longint= 0;
maxPecY: longint= 0;
(********************************************************************************)
(* *)
(* Sundry code to help diagnostics. *)
(* *)
(********************************************************************************)
(* Push the name of the current rule onto a stack.
*)
procedure pushRule(const rule: string);
begin
ruleStack.Append(rule)
end { pushRule } ;
(* Pop the top of the rule name stack. The popped string is saved since it will
be needed if the file ends prematurely.
*)
function popRule(): string;
var
i: integer;
begin
result := '';
i := ruleStack.Count - 1;
if i >= 0 then begin
result := ruleStack[i];
poppedRule := result;
ruleStack.Delete(i)
end else begin
result := ''; (* Empty string indicates empty stack *)
poppedRule := '[Rule stack underflow]'
end
end { popRule } ;
(* Peek at the rule at the top of the stack.
*)
function peekRule(): string;
var
i: integer;
begin
result := '';
i := ruleStack.Count - 1;
if i >= 0 then
result := ruleStack[i]
end { peekRule } ;
(* Pretty-print a header for the start of rule execution.
*)
procedure header;
var
i: integer;
hdr, underline: string;
begin
hdr := '';
for i := 0 to ruleStack.Count - 1 do begin
if hdr <> '' then
hdr += Arrow;
hdr += ruleStack[i]
end;
underline := '';
// "Temporary" hack here to allow for multibyte UTF-8 arrows.
i := Length(hdr);
{$ifdef VER3 }
i -= 2 * (ruleStack.Count - 1);
{$endif VER3 }
while Length(underline) <> i do
underline += '-';
WriteLn(hdr);
WriteLn(underline)
end { header } ;
(********************************************************************************)
(* *)
(* Rules defining the file format (start reading at the end of this section). *)
(* *)
(********************************************************************************)
(* If a rule returns false then it indicates that it doesn't match the input, *)
(* and broadly speaking it is valid for the caller to try another rule; in this *)
(* case the rule name is popped off the stack used for diagnostic output. If a *)
(* rule raises an exception then no recovery is possible, the rule name is left *)
(* on the stack and will appear in diagnostic output. The working principle of *)
(* the parser is that on exit from the top-level rule (pes_file) the rule name *)
(* stack will be empty and the input file will be at EOF. *)
var
pecSectionByteOffset: longword= $ffffffff;
waitingForPec: boolean= false;
segmentBlockCount: longword= $ffffffff;
cSewSegBlockCount: longword= $ffffffff;
pecThumbnailByteOffset: longword= $ffffffff;
thumbnailWidth: longword= 0; (* In bytes, 8x pixels per byte *)
thumbnailHeight: longword= 0; (* In rows/pixels *)
thumbnailColours: longword= 0; (* Zero is 1 colour, 0xff is no colour *)
(* Parse the PEC stitchlist section.
*)
function pec_stitchListSubsection(): boolean;
const
thisName= 'pec_stitchListSubsection()';
var
backtrack: int64;
count, colour, ignoreOrdinates: integer;
stitch: PecStitch;
x: longint= 0;
y: longint= 0;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
WriteLn('Colour: [1] ', ThumbnailColourMap[1]);
count := 0;
colour := 1;
ignoreOrdinates := 0;
stitch := ReadQ(pesIn, pesOut, count div 2 + 1, Odd(count), not Odd(countPecTrimCommands div 2));
while stitch.command <> $ff do begin
Write('Stitch ', count div 2 + 1);
(* A colour change is documented as two bytes $fe $b0, after they've been *)
(* decoded to a command and an ordinate they're $f0 and -336 respectively. What *)
(* is and what isn't counted as a valid half-stitch here is a bit of a hack but *)
(* appears to produce the right result when compared with what's in the PES. *)
// Optimisation/command here: suppress colour change if it's actually the same colour.
if (stitch.command = { $fe } $f0) and (stitch.ordinate = { $b0 } -336) then begin
Write(' colour change from [', colour, '] ', ThumbnailColourMap[colour]);
colour += 1;
Write(' to [', colour, '] ', ThumbnailColourMap[colour], ',');
countPecColourChanges += 1;
ignoreOrdinates := 2;
countPecHalfstitches += 1
end else begin
if stitch.command and $40 <> $00 then begin
Write(' pause,');
countPecPauseCommands += 1
end;
if stitch.command and $20 <> $00 then begin
Write(' trim,');
countPecTrimCommands += 1
end;
if stitch.command and $10 <> $00 then begin
Write(' jump,');
countPecJumpCommands += 1
end;
if stitch.command and $60 = $00 then
countPecHalfstitches += 1
end;
if not Odd(count) then (* Count starts at zero. First (half-) *)
Write(' x: ') (* stitch is 1 x, second 1 y, etc. *)
else
Write(' y: ');
(* The stitch positions are relative so always output a sign. Ignore the *)
(* ordinate associated with a colour change, and pragmatically assume that the *)
(* next ordinate (i.e. the y associated with the colour change's x) is also to *)
(* be ignored although it does appear to be slightly variable (alternating 1 *)
(* and 2 as mentioned in one of the documents). *)
if stitch.ordinate >= 0 then
Write('+');
WriteLn(stitch.ordinate);
if ignoreOrdinates = 0 then begin
if Odd(count) then begin
x += stitch.ordinate;
// WriteLn('X absolute ', x); (* For debugging *)
if x < minPecX then
minPecX := x;
if x > maxPecX then
maxPecX := x
end else begin
y += stitch.ordinate;
// WriteLn('Y absolute ', y); (* For debugging *)
if y < minPecY then
minPecY := y;
if y > maxPecY then
maxPecY := y
end
end else
ignoreOrdinates -= 1;
stitch := ReadQ(pesIn, pesOut, count div 2 + 1, Odd(count), not Odd(countPecTrimCommands div 2));
count += 1
end;
WriteLn('End of stitch sequence');
WriteLn(Up + 'OK ', popRule());
result := true
end { pec_stitchListSubsection } ;
(* Parse the PEC thumbnail section.
*)
function pec_thumbnailImageSubsection(): boolean;
const
thisName= 'pec_thumbnailImageSubsection()';
var
backtrack: int64;
i: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
if FilePos(pesIn) <> pecThumbnailByteOffset then begin
WriteLn('*** In ', peekRule(), ': thumbnail image isn''t contiguous with stitchlist');
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
for i := 0 to thumbnailColours do begin
WriteLn('Thumbnail colour: ', i);
ReadU8G(pesIn, pesOut, thumbnailWidth, thumbnailHeight, i)
end;
WriteLn(Up + 'OK ', popRule());
result := true
end { pec_thumbnailImageSubsection } ;
(* Parse the PEC header.
*)
function pec_header(): boolean;
const
thisName= 'pec_header()';
var
backtrack: int64;
i, v: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
WriteLn('NOTE: absolute location of the PEC header is stored in the PES header');
if ReadN(pesIn, pesOut, 3) <> 'LA:' then begin
WriteLn('*** In ', peekRule(), ': bad PEC magic number');
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
ReadN(pesIn, pesOut, 16);
if ReadN(pesIn, pesOut, 1) <> #$0d then begin
WriteLn('*** In ', peekRule(), ': bad PEC name termination');
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
ReadU8N(pesIn, pesOut, 12);
ReadU16N(pesIn, pesOut, 1);
thumbnailWidth := ReadU8(pesIn, pesOut);
WriteLn('Thumbnail width (bytes): ', thumbnailWidth);
thumbnailHeight := ReadU8(pesIn, pesOut);
WriteLn('Thumbnail height (rows): ', thumbnailHeight);
ReadU8N(pesIn, pesOut, 12);
for i := 0 to 255 do
ThumbnailColourMap[i] := -1;
thumbnailColours := ReadU8(pesIn, pesOut);
WriteLn('Thumbnail colours (-1): ', thumbnailColours);
thumbnailColours := (thumbnailColours + 1) mod 256;
for i := 0 to thumbnailColours - 1 do begin
v := ReadU8(pesIn, pesOut);
WriteLn('Thumbnail colour ', i, ': ', v, ' (', ColourName(v), ')');
if i <= 255 then
ThumbnailColourMap[i] := v
end;
ReadU8N(pesIn, pesOut, 462 - (thumbnailColours - 1));
WriteLn('NOTE: padding must keep PEC header length constant at 512 (0x200) bytes');
WriteLn(Up + 'OK ', popRule());
result := true
end { pec_header } ;
(* Parse the PEC body, comprising stitchlist and thumbnail bitmaps.
*)
function pec_body(): boolean;
const
thisName= 'pec_body()';
var
backtrack: int64;
v, width, height: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
if FilePos(pesIn) <> pecSectionByteOffset + 512 then begin
WriteLn('*** In ', peekRule(), ': bad PEC header padding length');
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
ReadS16(pesIn, pesOut);
v := ReadU16(pesIn, pesOut);
pecThumbnailByteOffset := pecSectionByteOffset + v + 512;
WriteLn('Thumbnail image offset: ', v, ' (0x', HexStr(pecThumbnailByteOffset, 6), ')');
WriteLn('NOTE: this must be adjusted if there are insertion/deletion patches in the PEC stitchlist');
ReadU16N(pesIn, pesOut, 2);
width := ReadS16(pesIn, pesOut);
WriteLn('Width: ', width);
height := ReadS16(pesIn, pesOut);
WriteLn('height: ', height);
ReadU16N(pesIn, pesOut, 2);
if not pec_stitchListSubsection() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
if not pec_thumbnailImageSubsection() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
WriteLn(Up + 'OK ', popRule());
result := true
end { pec_body } ;
(* Parse the PEC header and body.
*)
function pec_part(): boolean;
const
thisName= 'pec_part()';
var
backtrack: int64;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
if not pec_header() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
if not pec_body() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
WriteLn(Up + 'OK ', popRule());
result := true
end { pec_part } ;
(* Parse a PEC addendum.
*)
function pec_addendum(): boolean;
const
thisName= 'pec_addendum()';
var
backtrack: int64;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
// TODO : Fill this in.
WriteLn('In pec_addendum, at ', FilePos(pesIn), ' of total ', FileSize(pesIn));
WriteLn(Up + 'OK ', popRule());
result := true
end { pec_addendum } ;
(* Parse extents.
*)
function pes_extents(): boolean;
const
thisName= 'pes_extents()';
var
backtrack: int64;
v: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
v := ReadS16(pesIn, pesOut);
WriteLn('Extents left: ', v);
v := ReadS16(pesIn, pesOut);
WriteLn('Extents top: ', v);
v := ReadS16(pesIn, pesOut);
WriteLn('Extents right: ', v);
v := ReadS16(pesIn, pesOut);
WriteLn('Extents bottom: ', v);
v := ReadS16(pesIn, pesOut);
WriteLn('Extents left position: ', v);
v := ReadS16(pesIn, pesOut);
WriteLn('Extents top position: ', v);
v := ReadS16(pesIn, pesOut);
WriteLn('Extents right position: ', v);
v := ReadS16(pesIn, pesOut);
WriteLn('Extents bottom position: ', v);
WriteLn(Up + 'OK ', popRule());
result := true
end { pes_extents } ;
(* Parse affine transform.
*)
function pes_affineTransform(): boolean;
const
thisName= 'pes_affineTransform()';
var
backtrack: int64;
v: single;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
v := ReadF32(pesIn, pesOut);
WriteLn('Transform scale X: ', v);
v := ReadF32(pesIn, pesOut);
WriteLn('Transform skew Y: ', v);
v := ReadF32(pesIn, pesOut);
WriteLn('Transform skew X: ', v);
v := ReadF32(pesIn, pesOut);
WriteLn('Transform scale Y: ', v);
v := ReadF32(pesIn, pesOut);
WriteLn('Transform xlate X: ', v);
v := ReadF32(pesIn, pesOut);
WriteLn('Transform xlate Y: ', v);
WriteLn(Up + 'OK ', popRule());
result := true
end { pes_affinetransform } ;
(* Parse block geometry comprising extents and affine transform.
*)
function pes_blockGeometry(): boolean;
const
thisName= 'pes_blockGeometry()';
var
backtrack: int64;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
if not pes_extents() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
if not pes_affineTransform() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
WriteLn(Up + 'OK ', popRule());
result := true
end { pes_blockGeometry } ;
(* Parse a CEmbOne section. Because the parser does not have lookahead, the
magic number was handled by the caller.
*)
function pes_embOne(): boolean;
const
thisName= 'pes_embOne()';
var
backtrack: int64;
v: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
if not pes_blockGeometry() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
v := ReadU16(pesIn, pesOut); (* Annotated as "'1' (typical)" *)
v := ReadS16(pesIn, pesOut);
WriteLn('CSewSeg x coordinate translation(?): ', v);
v := ReadS16(pesIn, pesOut);
WriteLn('CSewSeg y coordinate translation(?): ', v);
v := ReadS16(pesIn, pesOut);
WriteLn('CSewSeg width: ', v);
v := ReadS16(pesIn, pesOut);
WriteLn('CSewSeg height: ', v);
ReadU8N(pesIn, pesOut, 8); (* Padding *)
cSewSegBlockCount := ReadU16(pesIn, pesOut);
WriteLn('CSewSeg blockCount: ', cSewSegBlockCount);
(* Undocumented padding. *)
v := ReadU16(pesIn, pesOut);
if v <> $ffff then begin
WriteLn('*** In ', peekRule(), ': unexpected padding (1)');
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
v := ReadU16(pesIn, pesOut);
if v <> $0000 then begin
WriteLn('*** In ', peekRule(), ': unexpected padding (2)');
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
WriteLn(Up + 'OK ', popRule());
result := true
end { pes_embOne } ;
(* CSewSeg stitch list.
*)
function pes_sewSegStitchList(): boolean;
const
thisName= 'pes_sewSegStitchList()';
var
backtrack: int64;
i, t, v: integer;
{$ifdef FPC }
stitch: TScalarArray;
{$endif FPC }
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
t := ReadU16(pesIn, pesOut);
Write('Stitch type: ');
case t of
0: WriteLn('normal');
1: WriteLn('jump');
otherwise
WriteLn(t)
end;
v := ReadU16(pesIn, pesOut);
WriteLn('Thread index (+1): ', v);
v := ReadU16(pesIn, pesOut);
WriteLn('Number of coordinates: ', v);
for i := 0 to v - 1 do begin
{$ifdef FPC }
stitch := ReadS16N(pesIn, pesOut, 2);
WriteLn('Stitch ', i + 1, ': ', stitch[0].s16, ',', stitch[1].s16);
if stitch[0].s16 < minPesX then
minPesX := stitch[0].s16;
if stitch[0].s16 > maxPesX then
maxPesX := stitch[0].s16;
if stitch[1].s16 < minPesY then
minPesY := stitch[1].s16;
if stitch[1].s16 > maxPesY then
maxPesY := stitch[1].s16;
{$else }
x := ReadS16();
y := ReadS16();
WriteLn('Stitch ', i + 1, ': ', x, ',', y);
if x < minPesX then
minPesX := x;
if x > maxPesX then
maxPesX := x;
if y < minPesY then
minPesY := y;
if y > maxPesY then
maxPesY := y;
{$endif FPC }
case t of
0: countPesStitchesNormal += 1;
1: countPesStitchesJump += 1
otherwise
countPesStitchesOther += 1
end;
countPesStitchesTotal += 1
end;
WriteLn(Up + 'OK ', popRule());
result := true
end { pes_sewSegStitchList } ;
(* CSewSeg colo(u)r list.
*)
function pes_sewSegColorList(): boolean;
const
thisName= 'pes_sewSegColorList()';
var
backtrack: int64;
v: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
v := ReadU16(pesIn, pesOut);
WriteLn('Block index of change: ', v);
v := ReadU16(pesIn, pesOut);
WriteLn('Thread palette/index: ', v);
countPesColours += 1;
WriteLn(Up + 'OK ', popRule());
result := true
end { pes_sewSegColorList } ;
(* CSewSeg segment block.
*)
function pes_sewSegSegmentBlock(): boolean;
const
thisName= 'pes_sewSegSegmentBlock()';
var
backtrack: int64;
v: integer;
function endStitchBlocks(): boolean;
begin
// This is a "temporary" warning in case the block counter is also being used to
// indicate cut/pause etc.
if (v > 1000) and ( v <> $8003) then
WriteLn('WARNING: unreasonably large colour count.');
endStitchBlocks := v <> $8003
end { endStitchBlocks } ;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
repeat
if not pes_sewSegStitchList() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
v := ReadU16(pesIn, pesOut);
until endStitchBlocks(); (* Special repeat-stitch-block code *)
while v > 0 do
if not pes_sewSegColorList() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end else
v -= 1;
WriteLn(Up + 'OK ', popRule());
result := true
end { pes_sewSegSegmentBlock } ;
(* Parse a CSewSeg section. Because the parser does not have lookahead, the
magic number was handled by the caller.
*)
function pes_sewSeg(): boolean;
const
thisName= 'pes_sewSeg()';
var
backtrack: int64;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
if not pes_sewSegSegmentBlock() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
WriteLn(Up + 'OK ', popRule());
result := true
end { pes_sewSeg } ;
// TODO : Fill these in
function pes_embCirc(): boolean;
begin
result := false
end { pes_embCirc } ;
function pes_embRect(): boolean;
begin
result := false
end { pes_embRect } ;
function pes_embLine(): boolean;
begin
result := false
end { pes_embLine } ;
function pes_embPunch(): boolean;
begin
result := false
end { pes_embPunch } ;
function pes_embNText(): boolean;
begin
result := false
end { pes_embNText } ;
(* Expect a PES body. This will comprise a number of sections, in particular
one or more CEmbOne and CSewSeg sections, an embedded PEC part, and possibly
a PEC addendum which unlike the other sections is not identified by a magic
number.
*)
function pes_body(): boolean;
const
thisName= 'pes_body()';
var
backtrack: int64;
s: string;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
result := false;
pushRule(thisName);
backtrack := FilePos(pesIn);
header;
while not Eof(pesIn) do begin
if FilePos(pesIn) = pecSectionByteOffset then begin
waitingForPec := false;
if not pec_part() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end;
if FilePos(pesIn) <> FileSize(pesIn) then
WriteLn('NOTE: at ', FilePos(pesIn), ' (', HexStr(FileSize(pesIn), 5), ') of total ', FileSize(pesIn));
if not pec_addendum() then begin
{$ifdef ERROR2 }
WriteLn('*** In ', peekRule(), ': unable to parse ', poppedRule);
{$endif ERROR2 }
WriteLn(Up + 'Backtrack ', popRule());
Seek(pesIn, backtrack);
exit
end
end else begin