-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmidlevelio.pas
1414 lines (1183 loc) · 45.8 KB
/
midlevelio.pas
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. *)
unit MidlevelIO;
(* Mid-level stuff supporting the parser, covered by the same license etc. The *)
(* entry points are named ReadXXX() but each one has substantial side effects *)
(* in that it optionally outputs the data to a second file for checking etc. *)
(* and optionally applies patches at locations identified on the command line. *)
(* MarkMLl *)
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, PatchAndIO;
const
NotOpen= 0; (* Handle value for output file checks *)
(* Note: these characters might be contentious with old FPC etc. versions that
aren't happy with UTF-8.
*)
{$ifndef VER3 }
Dot= '.';
Arrow= ' -> ';
Up= '^ ';
{$else }
Dot= '·';
Arrow= ' → ';
Up= '↑ ';
{$endif VER3 }
(* The banner matches address + 16 hex bytes + padding but should optimise to a
length if StrUtils is imported, other underlines match the text with which
they're associated.
*)
Banner= '========================================================';
type
PecStitch= record
command: byte;
ordinate: longint
end;
var
OptionTrimToPause: boolean= false;
OptionTrimToChange: boolean= false;
OptionChangeToDummy: boolean= false;
(* Optional thumbnail colours from the PEC header. These are not necessarily the
same as the expected thread colours.
*)
ThumbnailColourMap: array[0..255] of integer;
(* Read an unsigned 8-bit number.
*)
function ReadU8(var pesIn, pesOut: File): byte;
(* Read an unsigned 16-bit number.
*)
function ReadU16(var pesIn, pesOut: File): word;
(* Read a signed 16-bit number.
*)
function ReadS16(var pesIn, pesOut: File): smallint;
(* Read an unsigned 32-bit number.
*)
function ReadU32(var pesIn, pesOut: File): longword;
(* Read a 32-bit float. PES files appear to use the standard IEEE format, but
this should not be assumed in the general case.
*)
function ReadF32(var pesIn, pesOut: File): single;
(* Read a block of bytes. This would normally be called for padding etc. and
any attempt to return a block will probably be non-portable.
*)
function ReadU8N(var pesIn, pesOut: File; n: integer): TScalarArray;
(* Read a block of words. This would normally be called for padding etc. and
any attempt to return a block will probably be non-portable.
*)
function ReadU16N(var pesIn, pesOut: File; n: integer): TScalarArray;
(* Read a block of words. This would normally be called for padding etc. and
any attempt to return a block will probably be non-portable.
*)
function ReadS16N(var pesIn, pesOut: File; n: integer): TScalarArray;
(* Read a block of floats. This would normally be called for padding etc. and
any attempt to return a block will probably be non-portable.
*)
function ReadF32N(var pesIn, pesOut: File; n: integer): TScalarArray; // NOT USED
(* Read and discard a block of bytes. Assume that this represents a thumbnail
in simple bitmap format and if possible dump it using PNM format rather than
ASCII.
*)
procedure ReadU8G(var pesIn, pesOut: File; width, height, index: integer);
(* Read a string of fixed length. Raise an exception at EOF.
*)
function ReadN(var pesIn, pesOut: File; n: integer= 1): ansistring;
(* Read a length word followed by a string (i.e. this is a "Pascal-style" or
"counted" string with a 16-bit length). Raise an exception at EOF.
*)
function ReadC(var pesIn, pesOut: File): ansistring;
(* Read a string terminated by a zero byte. Raise an exception at EOF.
*)
function ReadZ(var pesIn, pesOut: File): ansistring; // NOT USED
(* Used during testing/debugging.
*)
procedure TestUnpackStitch();
(* Read either one or two bytes comprising a stitch ordinate (signed) and
optional command bits masked into a separate byte. The x parameter indicates
whether it is the x or y stitch of a pair, the t1 parameter controls the y
parameter after a trim which should alternate between 1 and 2.
*)
function ReadQ(var pesIn, pesOut: File; stitch: longint; x, t1: boolean): PecStitch;
(* These believed to be from a Brother Innovis 955.
*)
function ColourName(index: integer): string;
implementation
const
traceTop= (1024 * 1024) - 1;
var
traceStart, traceBytes: longint;
trace: array[0..traceTop] of byte;
(********************************************************************************)
(* *)
(* Utility procedures: input/output formatting etc. available to all rules. *)
(* *)
(********************************************************************************)
(* Inspection of a PES file indicates that it is little-endian. Exceptions *)
(* raised in these functions are assumed to be fatal, and to result in a back- *)
(* trace of the rules that got us here. *)
(* Assuming 16 bytes per row of hex bytes, mark the middle.
*)
function pad(index: integer; bytes: integer= 16): string;
begin
if index = bytes div 2 - 1 then
result := Dot
else
result := ' '
end { pad } ;
(* Output what is assumed to be a 20-bit address as five hex digits plus a
padding space.
*)
function hex6(l: longint; padding: string= ' '): string;
begin
result := HexStr(l, 5);
while Length(result) < 5 do
result := '0' + result;
result += padding;
end { hex6 } ;
(* Output a byte as two hex digits plus a padding space.
*)
function hex3(b: byte; padding: string= ' '): string;
begin
result := HexStr(b, 2);
while Length(result) < 2 do
result := '0' + result;
result += padding;
end { hex3 } ;
(* Sanitise an output character.
*)
function safeChar(b: byte): string;
begin
case b of
$00..
$1f: result := Dot;
$7f..
$ff: result := Dot
otherwise
result := Chr(b)
end
end { safeChar } ;
(* Common dump output for raw hex-formatted data, i.e. just about everything
except possibly some graphics. Assume that once a field has been parsed as a
specific type it will also be output in that format, so that we don't have to
worry about e.g. decoding floating-point numbers here.
*)
procedure doneReadHexAscii(bytes: integer= 16);
var
lines, i, j, charsOutput: integer;
chars: string;
begin
lines := traceBytes div bytes;
for i := 0 to lines do begin
if traceBytes = 0 then // TODO : Tidy this up!
break;
Write(hex6(traceStart)); (* Five digits plus padding *)
Write(' ');
charsOutput := 7;
// TODO : Data type (U8 etc.) here?
chars := '';
for j := 0 to bytes - 1 do begin
if j >= traceBytes then
break;
Write(hex3(trace[bytes * i + j], pad(j, bytes))); (* Two digits plus padding *)
chars += safeChar(trace[bytes * i + j]);
charsOutput += 3;
end;
(* Assuming 16 bytes expressed as hex, the address + bytes + padding will match *)
(* the === banner used to separate major sections of output. *)
while charsOutput < (7 + 3 * bytes + 1) do begin
Write(' ');
charsOutput += 1
end;
WriteLn(chars);
traceStart += bytes;
traceBytes -= bytes;
if traceBytes < 0 then
traceBytes := 0
end
end { doneReadHexAscii } ;
(* These believed to be from a Brother Innovis 955.
*)
function ColourName(index: integer): string;
begin
case index of
0: result := 'Zero';
1: result := 'Prussian Blue';
2: result := 'Blue';
3: result := 'Teal Green';
4: result := 'Corn Flower Blue';
5: result := 'Red';
6: result := 'Reddish Brown';
7: result := 'Magenta';
8: result := 'Light Lilac';
9: result := 'Lilac';
10: result := 'Mint Green';
11: result := 'Deep Gold';
12: result := 'Orange';
13: result := 'Yellow';
14: result := 'Lime Green';
15: result := 'Brass';
16: result := 'Silver';
17: result := 'Russet Brown';
18: result := 'Cream Brown';
19: result := 'Pewter';
20: result := 'Black';
21: result := 'Ultramarine';
22: result := 'Royal Purple';
23: result := 'Dark Gray';
24: result := 'Dark Brown';
25: result := 'Deep Rose';
26: result := 'Light Brown';
27: result := 'Salmon Pink';
28: result := 'Vermilion';
29: result := 'White';
30: result := 'Violet';
31: result := 'Seacrest';
32: result := 'Sky Blue';
33: result := 'Pumpkin';
34: result := 'Cream Yellow';
35: result := 'Khaki'; (* How did this get to be #feca15? *)
36: result := 'Clay Brown';
37: result := 'Leaf Green';
38: result := 'Peacock Blue';
39: result := 'Gray';
40: result := 'Warm Gray';
41: result := 'Dark Olive';
42: result := 'Linen';
43: result := 'Pink';
44: result := 'Deep Green';
45: result := 'Lavender';
46: result := 'Wisteria Violet';
47: result := 'Beige';
48: result := 'Carmine';
49: result := 'Amber Red';
50: result := 'Olive Green';
51: result := 'Dark Fuchsia';
52: result := 'Tangerine';
53: result := 'Light Blue';
54: result := 'Emerald Green';
55: result := 'Purple';
56: result := 'Moss Green';
57: result := 'Flesh Pink';
58: result := 'Harvest Gold';
59: result := 'Electric Blue';
60: result := 'Lemon Yellow';
61: result := 'Fresh Green';
62: result := 'Applique Material';
63: result := 'Applique Position';
64: result := 'Applique'
otherwise
result := ''
end
end { ColourName } ;
procedure doneReadHexPbm(width, height, index: integer; bytes: integer= 8);
var
lines, i, j, charsOutput: integer;
chars: string;
(* Convert a byte into 0/1 bits, LSB first. Note that IntToBin() does this
MSB first.
*)
function byteToStr(b: byte; bits: integer): string;
{$define MONOSPACED_RELIABLE }
const
{$ifdef MONOSPACED_RELIABLE }
mark= Dot;
space= ' ';
{$else }
mark= '1';
space= '0';
{$endif MONOSPACED_RELIABLE }
begin
result := '';
while bits > 0 do begin
if Odd(b) then
result += mark
else
result += space;
b := b >> 1;
bits -= 1
end
end { byteToStr } ;
begin
for i := 1 to 7 + (3 * bytes) + 1 do
chars += ' ';
WriteLn(chars, '|P1');
Write(chars, '|# Thumbnail ', index);
if (index <= 255) and (ThumbnailColourMap[index] <> -1) then
WriteLn(', colour ', ThumbnailColourMap[index], ' (',
ColourName(ThumbnailColourMap[index]), ')')
else
WriteLn;
WriteLn(chars, '|', width, ' ', height);
lines := traceBytes div bytes;
for i := 0 to lines do begin
if traceBytes = 0 then // TODO : Tidy this up!
break;
Write(hex6(traceStart)); (* Five digits plus padding *)
Write(' ');
charsOutput := 7;
// TODO : Data type (U8 etc.) here?
chars := '';
for j := 0 to bytes - 1 do begin
if j >= traceBytes then
break;
Write(hex3(trace[bytes * i + j], pad(j, bytes))); (* Two digits plus padding *)
chars += byteToStr(trace[bytes * i + j], 8);
charsOutput += 3;
end;
while charsOutput < (7 + 3 * bytes + 1) do begin
Write(' ');
charsOutput += 1
end;
WriteLn('|', chars);
traceStart += bytes;
traceBytes -= bytes;
if traceBytes < 0 then
traceBytes := 0
end
end { doneReadHexPbm } ;
(* Clear storage used to dump what's been read, and note the start point in the
file. If there's anything buffered on entry this will be output in generic
hex/ASCII format.
*)
procedure startRead(readPos: longint);
begin
if traceBytes <> 0 then
doneReadHexAscii;
traceStart := readPos;
traceBytes := 0
end { startRead } ;
(* Store intermediate data that's been read, the parameter type here does not
determine the output format.
*)
procedure dumpRead(const s: string);
var
i: integer;
begin
for i := 1 to Length(s) do begin
trace[traceBytes] := Ord(s[i]);
traceBytes += 1;
Assert(traceBytes <= traceTop, 'Internal error: trace buffer overflow')
end
end { dumpRead } ;
(* Store intermediate data that's been read, the parameter type here does not
determine the output format.
*)
procedure dumpRead(b: byte);
begin
trace[traceBytes] := b;
traceBytes += 1;
Assert(traceBytes <= traceTop, 'Internal error: trace buffer overflow')
end { dumpRead } ;
(* Store intermediate data that's been read, the parameter type here does not
determine the output format.
*)
procedure dumpRead(w: word);
var
scratch: record
case boolean of
false: (b: array[0..1] of byte);
true: (w: word)
end;
i: integer;
begin
scratch.w := w; (* Parameter order as read from file *)
for i := 0 to 1 do
dumpRead(scratch.b[i])
end { dumpRead } ;
(* Store intermediate data that's been read, the parameter type here does not
determine the output format.
*)
procedure dumpRead(s: smallint);
begin
{$push }
{$r- }
dumpRead(word(s))
{$pop }
end { dumpRead } ;
(* Store intermediate data that's been read, the parameter type here does not
determine the output format.
*)
procedure dumpRead(l: longword);
var
scratch: record
case boolean of
false: (b: array[0..3] of byte);
true: (l: longword)
end;
i: integer;
begin
scratch.l := l; (* Parameter order as read from file *)
for i := 0 to 3 do
dumpRead(scratch.b[i])
end { dumpRead } ;
(* Store intermediate data that's been read, the parameter type here does not
determine the output format.
*)
procedure dumpRead(s: single);
begin
{$push }
{$r- }
dumpRead(longword(s))
{$pop }
end { dumpRead } ;
(* Output data that's been read, the parameter type determines the format
(string, decimal numeric, graphics block).
*)
procedure doneRead(const s: string);
begin
dumpRead(s);
doneReadHexAscii
end { doneRead } ;
(* Output data that's been read, the parameter type determines the format
(string, decimal numeric, graphics block).
*)
procedure doneRead(b: byte);
begin
dumpRead(b);
doneReadHexAscii
end { doneRead } ;
(* Output data that's been read, the parameter type determines the format
(string, decimal numeric, graphics block).
*)
procedure doneRead(w: word);
begin
dumpRead(w);
doneReadHexAscii
end { doneRead } ;
(* Output data that's been read, the parameter type determines the format
(string, decimal numeric, graphics block).
*)
procedure doneRead(s: smallint);
begin
dumpRead(s);
doneReadHexAscii
end { doneRead } ;
(* Output data that's been read, the parameter type determines the format
(string, decimal numeric, graphics block).
*)
procedure doneRead(l: longword);
begin
dumpRead(l);
doneReadHexAscii
end { doneRead } ;
(* Output data that's been read, the parameter type determines the format
(string, decimal numeric, graphics block).
*)
procedure doneRead(f: single);
begin
dumpRead(f);
doneReadHexAscii
end { doneRead } ;
(* Read an unsigned 8-bit number.
*)
function ReadU8(var pesIn, pesOut: File): byte;
const
thisName= 'ReadU8()';
sz= SizeOf(byte);
var
patchLoc: longint;
buffer: TScalar;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
Assert(sz = 1, 'Internal error: bad U8 size');
Assert(SizeOf(result) = sz, 'Internal error: bad U8 result size');
Assert(SizeOf(buffer.u8) = sz, 'Internal error: bad U8 buffer size');
patchLoc := FilePos(pesIn);
startRead(patchLoc);
StartPatch(patchLoc);
ReadScalar(pesIn, thisName, buffer, sz);
result := buffer.u8;
doneRead(buffer.u8);
(* The result might be adjusted for endianness, but is always returned without *)
(* other modification so that the parser can validate the content of the file. *)
(* If any systematic modification is to be done in response to a command-line *)
(* option it will affect what is written to a binary output file, but the text *)
(* output for user inspection will be unaffected although a comment might be *)
(* inserted. *)
if TFileRec(pesOut).Handle > NotOpen then
if PatchVerb(patchLoc) <> PatchNone then
PatchAndWriteScalar(pesOut, thisName, buffer, sz, 'U8', patchLoc)
else
WriteScalar(pesOut, thisName, buffer, sz)
end { ReadU8 } ;
(* Read an unsigned 16-bit number.
*)
function ReadU16(var pesIn, pesOut: File): word;
const
thisName= 'ReadU16()';
sz= SizeOf(word);
var
patchLoc: longint;
buffer: TScalar;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
Assert(sz = 2, 'Internal error: bad U16 size');
Assert(SizeOf(result) = sz, 'Internal error: bad U16 result size');
Assert(SizeOf(buffer.u16) = sz, 'Internal error: bad U16 buffer size');
patchLoc := FilePos(pesIn);
startRead(patchLoc);
StartPatch(patchLoc);
ReadScalar(pesIn, thisName, buffer, sz);
result := LEtoN(buffer.u16);
doneRead(buffer.u16);
(* The result might be adjusted for endianness, but is always returned without *)
(* other modification so that the parser can validate the content of the file. *)
(* If any systematic modification is to be done in response to a command-line *)
(* option it will affect what is written to a binary output file, but the text *)
(* output for user inspection will be unaffected although a comment might be *)
(* inserted. *)
if TFileRec(pesOut).Handle > NotOpen then
if PatchVerb(patchLoc) <> PatchNone then
PatchAndWriteScalar(pesOut, thisName, buffer, sz, 'U16', patchLoc)
else
WriteScalar(pesOut, thisName, buffer, sz)
end { ReadU16 } ;
(* Read a signed 16-bit number.
*)
function ReadS16(var pesIn, pesOut: File): smallint;
const
thisName= 'ReadS16()';
sz= SizeOf(smallint);
var
patchLoc: longint;
buffer: TScalar;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
Assert(sz = 2, 'Internal error: bad S16 size');
Assert(SizeOf(result) = sz, 'Internal error: bad S16 result size');
Assert(SizeOf(buffer.s16) = sz, 'Internal error: bad S16 buffer size');
patchLoc := FilePos(pesIn);
startRead(patchLoc);
StartPatch(patchLoc);
ReadScalar(pesIn, thisName, buffer, sz);
result := LEtoN(buffer.s16);
doneRead(buffer.s16);
(* The result might be adjusted for endianness, but is always returned without *)
(* other modification so that the parser can validate the content of the file. *)
(* If any systematic modification is to be done in response to a command-line *)
(* option it will affect what is written to a binary output file, but the text *)
(* output for user inspection will be unaffected although a comment might be *)
(* inserted. *)
if TFileRec(pesOut).Handle > NotOpen then
if PatchVerb(patchLoc) <> PatchNone then
PatchAndWriteScalar(pesOut, thisName, buffer, sz, 'S16', patchLoc)
else
WriteScalar(pesOut, thisName, buffer, sz)
end { ReadS16 } ;
(* Read an unsigned 32-bit number.
*)
function ReadU32(var pesIn, pesOut: File): longword;
const
thisName= 'ReadU32()';
sz= SizeOf(longword);
var
patchLoc: longint;
buffer: TScalar;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
Assert(sz = 4, 'Internal error: bad U32 size');
Assert(SizeOf(result) = sz, 'Internal error: bad U32 result size');
Assert(SizeOf(buffer.u32) = sz, 'Internal error: bad U32 buffer size');
patchLoc := FilePos(pesIn);
startRead(patchLoc);
StartPatch(patchLoc);
ReadScalar(pesIn, thisName, buffer, sz);
result := LEtoN(buffer.u32);
doneRead(buffer.u32);
(* The result might be adjusted for endianness, but is always returned without *)
(* other modification so that the parser can validate the content of the file. *)
(* If any systematic modification is to be done in response to a command-line *)
(* option it will affect what is written to a binary output file, but the text *)
(* output for user inspection will be unaffected although a comment might be *)
(* inserted. *)
if TFileRec(pesOut).Handle > NotOpen then
if PatchVerb(patchLoc) <> PatchNone then
PatchAndWriteScalar(pesOut, thisName, buffer, sz, 'U32', patchLoc)
else
WriteScalar(pesOut, thisName, buffer, sz)
end { ReadU32 } ;
(* Read a 32-bit float. PES files appear to use the standard IEEE format, but
this should not be assumed in the general case.
*)
function ReadF32(var pesIn, pesOut: File): single;
const
thisName= 'ReadF32()';
sz= SizeOf(single);
var
patchLoc: longint;
buffer: TScalar;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
Assert(sz = 4, 'Internal error: bad F32 size');
Assert(SizeOf(result) = sz, 'Internal error: bad F32 result size');
Assert(SizeOf(buffer.f32) = sz, 'Internal error: bad F32 buffer size');
patchLoc := FilePos(pesIn);
startRead(patchLoc);
StartPatch(patchLoc);
ReadScalar(pesIn, thisName, buffer, sz);
doneRead(buffer.f32); (* Dump before in-situ endianness swap *)
buffer.u32 := LEtoN(buffer.u32);
result := buffer.f32;
(* The result might be adjusted for endianness, but is always returned without *)
(* other modification so that the parser can validate the content of the file. *)
(* If any systematic modification is to be done in response to a command-line *)
(* option it will affect what is written to a binary output file, but the text *)
(* output for user inspection will be unaffected although a comment might be *)
(* inserted. *)
if TFileRec(pesOut).Handle > NotOpen then begin
buffer.u32 := NtoLE(buffer.u32);
if PatchVerb(patchLoc) <> PatchNone then
// TODO : This patch variant untested
PatchAndWriteScalar(pesOut, thisName, buffer, sz, 'F32', patchLoc)
else
WriteScalar(pesOut, thisName, buffer, sz)
end
end { ReadF32 } ;
(* Read a block of bytes. This would normally be called for padding etc. and
any attempt to return a block will probably be non-portable.
*)
function ReadU8N(var pesIn, pesOut: File; n: integer): TScalarArray;
const
thisName= 'ReadU8N()';
sfx= 'U8';
sz= 1;
var
patchLoc: longint;
i: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
patchLoc := FilePos(pesIn);
startRead(patchLoc);
StartPatch(patchLoc);
SetLength(result, n);
if n > 0 then begin
readVector(pesIn, thisName, result, sz);
for i := 0 to Length(result) - 2 do
dumpRead(result[i].u8);
doneRead(result[Length(result) - 1].u8);
(* The result might be adjusted for endianness, but is always returned without *)
(* other modification so that the parser can validate the content of the file. *)
(* If any systematic modification is to be done in response to a command-line *)
(* option it will affect what is written to a binary output file, but the text *)
(* output for user inspection will be unaffected although a comment might be *)
(* inserted. *)
if TFileRec(pesOut).Handle > NotOpen then
if PatchVerb(patchLoc) <> PatchNone then
patchAndWriteVector(pesOut, thisName, result, sz, sfx, patchLoc)
else
writeVector(pesOut, thisName, result, sz)
end
end { ReadU8N } ;
(* Read a block of words. This would normally be called for padding etc. and
any attempt to return a block will probably be non-portable.
*)
function ReadU16N(var pesIn, pesOut: File; n: integer): TScalarArray;
const
thisName= 'ReadU16N()';
sfx= 'U16';
sz= 2;
var
patchLoc: longint;
i: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
patchLoc := FilePos(pesIn);
startRead(patchLoc);
StartPatch(patchLoc);
SetLength(result, n);
if n > 0 then begin
readVector(pesIn, thisName, result, sz);
for i := 0 to Length(result) - 2 do
dumpRead(result[i].u16);
doneRead(result[Length(result) - 1].u16);
(* The result might be adjusted for endianness, but is always returned without *)
(* other modification so that the parser can validate the content of the file. *)
(* If any systematic modification is to be done in response to a command-line *)
(* option it will affect what is written to a binary output file, but the text *)
(* output for user inspection will be unaffected although a comment might be *)
(* inserted. *)
if TFileRec(pesOut).Handle > NotOpen then
if PatchVerb(patchLoc) <> PatchNone then
patchAndWriteVector(pesOut, thisName, result, sz, sfx, patchLoc)
else
writeVector(pesOut, thisName, result, sz)
end
end { ReadU16N } ;
(* Read a block of words. This would normally be called for padding etc. and
any attempt to return a block will probably be non-portable.
*)
function ReadS16N(var pesIn, pesOut: File; n: integer): TScalarArray;
const
thisName= 'ReadS16N()';
sfx= 'S16';
sz= 2;
var
patchLoc: longint;
i: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
patchLoc := FilePos(pesIn);
startRead(patchLoc);
StartPatch(patchLoc);
SetLength(result, n);
if n > 0 then begin
readVector(pesIn, thisName, result, sz);
for i := 0 to Length(result) - 2 do
dumpRead(result[i].s16);
doneRead(result[Length(result) - 1].s16);
(* The result might be adjusted for endianness, but is always returned without *)
(* other modification so that the parser can validate the content of the file. *)
(* If any systematic modification is to be done in response to a command-line *)
(* option it will affect what is written to a binary output file, but the text *)
(* output for user inspection will be unaffected although a comment might be *)
(* inserted. *)
if TFileRec(pesOut).Handle > NotOpen then
if PatchVerb(patchLoc) <> PatchNone then
patchAndWriteVector(pesOut, thisName, result, sz, sfx, patchLoc)
else
writeVector(pesOut, thisName, result, sz)
end
end { ReadS16N } ;
(* Read a block of floats. This would normally be called for padding etc. and
any attempt to return a block will probably be non-portable.
*)
function ReadF32N(var pesIn, pesOut: File; n: integer): TScalarArray; // NOT USED
const
thisName= 'ReadF32N()';
sfx= 'F32';
sz= 4;
var
patchLoc: longint;
i: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
patchLoc := FilePos(pesIn);
startRead(patchLoc);
StartPatch(patchLoc);
SetLength(result, n);
if n > 0 then begin
readVector(pesIn, thisName, result, sz);
for i := 0 to Length(result) - 2 do
dumpRead(result[i].f32);
doneRead(result[Length(result) - 1].f32);
(* The result might be adjusted for endianness, but is always returned without *)
(* other modification so that the parser can validate the content of the file. *)
(* If any systematic modification is to be done in response to a command-line *)
(* option it will affect what is written to a binary output file, but the text *)
(* output for user inspection will be unaffected although a comment might be *)
(* inserted. *)
if TFileRec(pesOut).Handle > NotOpen then
if PatchVerb(patchLoc) <> PatchNone then
patchAndWriteVector(pesOut, thisName, result, sz, sfx, patchLoc)
else
writeVector(pesOut, thisName, result, sz)
end
end { ReadF32N } ;
(* Read and discard a block of bytes. Assume that this represents a thumbnail
in simple bitmap format and if possible dump it using PNM format rather than
ASCII.
*)
procedure ReadU8G(var pesIn, pesOut: File; width, height, index: integer);
const
thisName= 'ReadU8G()';
sfx= 'U8';
sz= 1;
var
patchLoc: longint;
bitmap: TScalarArray;
i: integer;
begin
{$ifdef HAS_CURRENTROUTINE }
Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
{$I %CURRENTROUTINE%} + '()');
{$endif HAS_CURRENTROUTINE }
patchLoc := FilePos(pesIn);
startRead(patchLoc);
StartPatch(patchLoc);
SetLength(bitmap, width * height);
if width * height > 0 then begin
readVector(pesIn, thisName, bitmap, sz);
for i := 0 to Length(bitmap) - 1 do