-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrwgifcore.pas
782 lines (736 loc) · 18.3 KB
/
rwgifcore.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
{$mode TP}
{$PACKRECORDS 1}
unit rwgifcore;
{ ************************************************
** GIF Decoding and Encoding procedures **
** for Borland/Turbo Pascal 7.0 **
** **
** Written by Tristan Tarrant, 1994 **
** **
** ( Supports GIF87a/GIF89a ) **
************************************************ }
interface
uses dos,xmisc;
const
{ Error constants used in GIF decoder }
GoodRead = 0; { No errors encountered during encoding/decoding }
BadFile = 1; { Physical problem with the media}
BadRead = 2; { Could not read/interpret part of the file }
UnexpectedEOF = 3; { File too short during decoding}
BadCode = 4; { Code encountered during decoding was not expected}
BadFirstCode = 5; { The first code was invalid}
NoFile = 6; { Could not open the file for read/write}
BadSymbolSize = 7; { Number of bits not supported}
NoCode = -1;
Type
GifLineProcType = procedure( Var pixels; line, width : integer );
GifPixelProcType = function : integer;
Var
{ Pointers to custom procedures to deal with lines. GifOutLineProc
is called with three parameters : an untyped var, containing
the uncompressed data, and two integer values, containing the
line number and the width of the line.
GifInPixelProc should instead return a pixels value, -1 if at the
end of the data. }
GifOutLineProc : GifLineProcType;
{ GifOutLineProc is called with an untyped variable containing a row's
worth of pixels. The current line is given in line and the number of
pixels in a line is given in width}
GifInPixelProc : GifPixelProcType;
{ GifInPixelProc should return a pixel value, -1 if at the end of the data.
Data should be returned width first (i.e. all pixels in row 0, then all
pixels in row 1, etc.}
GifPalette : array[0..767] of byte;
{ GifPalette is an array which contains the palette of the last loaded
GIF file}
function LoadGif( f : string ) : integer;
{ This function loads a GIF file f and returns an error code.
It uses the #GIFLineProc# procedure to send the decoded picture
to the application. The palette of the picture is stored in the
global variable #GifPalette#}
function SaveGif( f : string; width, depth, bits : integer; var palette ) : integer;
{ This function saves a GIF file f with using screen size width*depth
and with a color resolution of bits. For a 256 colour image bits is 8.
Palette contains the palette of the picture that is being saved.
SaveGIF uses #GIFInPixelProc# to get the picture data from the application.
It returns an error code}
function GifError( ErrorCode : integer ) : string;
{ This function converts an error code returned by SaveGIF into a string}
Implementation
type
GifHeader =
record
sig : array[1..6] of char;
screenwidth, screendepth : word;
flags, background, aspect : byte;
end;
ImageBlock =
record
left, top, width, depth : word;
flags : byte;
end;
FileInfo =
record
width, depth, bits,
flags, background : integer;
palette : array[1..768] of byte;
end;
ControlBlock =
record
blocksize, flags : byte;
delay : word;
transparentcolour, terminator : byte;
end;
PlainText =
record
blocksize : byte;
left, top, gridwidth, gridheight : word;
cellwidth, cellheight, forecolour, backcolour : byte;
end;
Application =
record
blocksize : byte;
applstring : array[1..8] of char;
authentication : array[1..3] of char;
end;
const
TableSize = 5003;
{ These values will be masked with the codes output from the
decoder to remove spurious bits }
CodeMask : array[1..13] of word =
( $0000,
$0001, $0003,
$0007, $000F,
$001F, $003F,
$007F, $00FF,
$01FF, $03FF,
$07FF, $0FFF );
LargestCode = 4095;
function UnpackImage( var F : File; bits : integer; Var fi : FileInfo ) : integer;
var
bits2, codesize, codesize2, nextcode, thiscode,
oldtoken, currentcode, oldcode, bitsleft, blocksize,
line, pass, byt, p, q, u : integer;
b : array[0..255] of byte;
linebuffer, firstcodestack, lastcodestack : ^TByteArray;
codestack : ^TIntArray;
const
wordmasktable : array[0..15] of word =
( $0000, $0001, $0003, $0007, $000F, $001F,
$003F, $007F, $00FF, $01FF, $03FF, $07FF,
$0FFF, $1FFF, $3FFF, $7FFF );
inctable : array[0..4] of integer = ( 8, 8, 4, 2, 0 );
starttable : array[0..4] of integer = ( 0, 4, 2, 1, 0 );
begin
pass := 0;
line := 0;
byt := 0;
p := 0;
q := 0;
blocksize := 0;
fillchar( b, 256, 0 );
bitsleft := 8;
if ( bits < 2 ) or ( bits > 8 ) then
begin
UnpackImage := BadSymbolSize;
exit;
end;
bits2 := 1 shl bits;
nextcode := bits2 + 2;
codesize := bits + 1;
codesize2 := 1 shl codesize;
oldcode := NoCode;
oldtoken := NoCode;
getmem( firstcodestack, 4096 );
getmem( lastcodestack, 4096 );
getmem( codestack, 8192 );
getmem( linebuffer, fi.width );
while true do
begin
if bitsleft = 8 then
begin
inc(p);
if p>=q then
begin
blocksize := 0;
blockread( F, blocksize, 1);
if blocksize>0 then
begin
p:=0;
blockread( F, b, blocksize, q );
if q<>blocksize then
begin
freemem( firstcodestack, 4096 );
freemem( lastcodestack, 4096 );
freemem( codestack, 8192 );
freemem( linebuffer, fi.width );
UnpackImage := UnexpectedEOF;
exit;
end;
end else
begin
freemem( firstcodestack, 4096 );
freemem( lastcodestack, 4096 );
freemem( codestack, 8192 );
freemem( linebuffer, fi.width );
UnpackImage := UnexpectedEOF;
exit;
end;
end;
bitsleft := 0;
end;
thiscode := b[p];
currentcode := codesize + bitsleft;
if currentcode <=8 then
begin
b[p] := b[p] shr codesize;
bitsleft := currentcode;
end else
begin
inc(p);
if p>=q then
begin
blocksize := 0;
blockread( F, blocksize, 1);
if blocksize>0 then
begin
p:=0;
blockread( F, b, blocksize, q );
if q<>blocksize then
begin
freemem( firstcodestack, 4096 );
freemem( lastcodestack, 4096 );
freemem( codestack, 8192 );
freemem( linebuffer, fi.width );
UnpackImage := UnexpectedEOF;
exit;
end;
end else
begin
freemem( firstcodestack, 4096 );
freemem( lastcodestack, 4096 );
freemem( codestack, 8192 );
freemem( linebuffer, fi.width );
UnpackImage := UnexpectedEOF;
exit;
end;
end;
thiscode := thiscode or ( b[p] shl (8-bitsleft) );
if currentcode <= 16 then
begin
bitsleft := currentcode - 8;
b[p] := b[p] shr bitsleft;
end else
begin
inc(p);
if p>=q then
begin
blocksize := 0;
blockread( F, blocksize, 1);
if blocksize>0 then
begin
p:=0;
blockread( F, b, blocksize, q );
if q<>blocksize then
begin
freemem( firstcodestack, 4096 );
freemem( lastcodestack, 4096 );
freemem( codestack, 8192 );
freemem( linebuffer, fi.width );
UnpackImage := UnexpectedEOF;
exit;
end;
end else
begin
freemem( firstcodestack, 4096 );
freemem( lastcodestack, 4096 );
freemem( codestack, 8192 );
freemem( linebuffer, fi.width );
UnpackImage := UnexpectedEOF;
exit;
end;
end;
thiscode := thiscode or ( b[p] shl (16-bitsleft) );
bitsleft := currentcode - 16;
b[p] := b[p] shr bitsleft;
end;
end;
thiscode := thiscode and wordmasktable[codesize];
currentcode := thiscode;
if thiscode = bits2+1 then break;
if thiscode > nextcode then
begin
freemem( firstcodestack, 4096 );
freemem( lastcodestack, 4096 );
freemem( codestack, 8192 );
freemem( linebuffer, fi.width );
UnpackImage := BadCode;
exit;
end;
if thiscode = bits2 then
begin
nextcode := bits2+2;
codesize := bits + 1;
codesize2 := 1 shl codesize;
oldtoken := NoCode;
OldCode := NoCode;
continue;
end;
u := 0;
if thiscode = nextcode then
begin
if oldcode = NoCode then
begin
freemem( firstcodestack, 4096 );
freemem( lastcodestack, 4096 );
freemem( codestack, 8192 );
freemem( linebuffer, fi.width );
UnpackImage := BadFirstCode;
exit;
end;
firstcodestack^[u] := oldtoken;
inc( u );
thiscode := oldcode;
end;
while thiscode >= bits2 do
begin
firstcodestack^[u] := lastcodestack^[thiscode];
inc( u );
thiscode := codestack^[thiscode];
end;
oldtoken := thiscode;
while true do
begin
linebuffer^[byt] := thiscode;
inc( byt );
if byt >= fi.width then
begin
GifOutLineProc( linebuffer^, line, fi.width );
byt := 0;
if fi.flags and $40 = $40 then
begin
line := line + inctable[pass];
if line >= fi.depth then
begin
inc(pass);
line := starttable[pass];
end;
end else inc(line);
end;
if u <= 0 then break;
dec( u );
thiscode := firstcodestack^[u];
end;
if (nextcode < 4096) and (oldcode <> NoCode) then
begin
codestack^[nextcode] := oldcode;
lastcodestack^[nextcode] := oldtoken;
inc( nextcode );
if (nextcode >= codesize2) and (codesize < 12) then
begin
inc( codesize );
codesize2 := 1 shl codesize;
end;
end;
oldcode := currentcode;
end;
freemem( firstcodestack, 4096 );
freemem( lastcodestack, 4096 );
freemem( codestack, 8192 );
freemem( linebuffer, fi.width );
UnpackImage := GoodRead;
end; { UnpackImage }
procedure SkipExtension( Var F : File );
var
pt : PlainText;
cb : ControlBlock;
ap : Application;
i : integer;
a, n, c : byte;
r : word;
begin
blockread( F, c, 1 );
case c of
$01 :
begin
blockread( F, pt, sizeof( PlainText ) );
blockread( F, n, 1 );
while n > 0 do
begin
for i := 0 to n-1 do
blockread( F, a, 1 );
blockread( F, n, 1 );
end;
end;
$F9 :
blockread( F, cb, sizeof( ControlBlock ) );
$FE :
begin
blockread( F, n, 1 );
while n > 0 do
begin
for i:= 0 to n-1 do
blockread( F, a, 1 );
blockread( F, n, 1 );
end;
end;
$FF :
begin
blockread( F, ap, sizeof( Application ) );
blockread( F, n, 1 );
while n > 0 do
begin
for i := 0 to n-1 do
blockread( F, a, 1 );
blockread( F, n, 1 );
end;
end;
else
begin
blockread( F, n, 1 );
for i := 0 to n-1 do
blockread( F, a, 1 );
end;
end;
end; { SkipExtension }
function UnpackGIF( Var F : File ) : integer;
var
gh : GifHeader;
iblk : ImageBlock;
t : longint;
b, c : integer;
r : word;
ch : char;
fi : FileInfo;
begin
blockread( F, gh, SizeOf(GifHeader), r );
if ( gh.sig[1]+gh.sig[2]+gh.sig[3]<>'GIF' ) or ( r<>SizeOf(GifHeader) ) then
begin
UnpackGIF := BadFile;
exit;
end;
fi.width := gh.screenwidth;
fi.depth := gh.screendepth;
fi.bits := gh.flags and $07 + 1;
fi.background := gh.background;
if ( gh.flags and $80 )=$80 then
begin
c:=3*( 1 shl fi.bits );
blockread( F, fi.palette, c, r );
if r<>c then
begin
UnpackGIF := BadRead;
exit;
end;
for b := 0 to 255 do
begin
// GIFPalette[b*3] := fi.palette[b*3+1] shr 2;
// GIFPalette[b*3+1] := fi.palette[b*3+2] shr 2;
// GIFPalette[b*3+2] := fi.palette[b*3+3] shr 2;
GIFPalette[b*3] := fi.palette[b*3+1];
GIFPalette[b*3+1] := fi.palette[b*3+2];
GIFPalette[b*3+2] := fi.palette[b*3+3];
end;
end;
blockread( F, ch, 1 );
while ( ch = ',' ) or ( ch = '!' ) or ( ch = #0 ) do
begin
case ch of
',' : begin
blockread( F, iblk, SizeOf(ImageBlock), r );
if r<>SizeOf(ImageBlock) then
begin
UnpackGIF := BadRead;
Exit;
end;
fi.width := iblk.width;
fi.depth := iblk.depth;
if ( iblk.flags and $80 )=$80 then
begin
b := 3*(1 shl (iblk.flags and $07 + 1));
blockread( F, fi.palette, b, r );
if r<>b then
begin
UnpackGIF := BadRead;
Exit;
end;
for b := 0 to 255 do
begin
// GIFPalette[b*3] := fi.palette[b*3+1] shr 2;
// GIFPalette[b*3+1] := fi.palette[b*3+2] shr 2;
// GIFPalette[b*3+1] := fi.palette[b*3+3] shr 2;
GIFPalette[b*3] := fi.palette[b*3+1];
GIFPalette[b*3+1] := fi.palette[b*3+2];
GIFPalette[b*3+2] := fi.palette[b*3+3];
end;
end;
if EOF( F ) then
begin
UnpackGIF := BadFile;
Exit;
end;
c:=0;
blockread( F, c, 1 );
fi.flags:=iblk.flags;
t := UnpackImage( F, c, fi );
UnpackGif:=t;
exit;
end;
'!' : SkipExtension( F );
end;
end;
end; { UnpackGIF }
function LoadGif;
var
D: DirStr;
N: NameStr;
E: ExtStr;
FileHandle : File;
begin
FSplit( F, D, N, E );
if E='' then E:='.GIF';
F := D+N+E;
{$I-}
assign( FileHandle, F );
reset( FileHandle, 1 );
{$I+}
if ioresult = 0 then
LoadGif := UnpackGif( FileHandle )
else
LoadGif := NoFile;
{$I-}
close( FileHandle );
{$I+}
end; { LoadGif }
function WriteScreenDesc( var fp : file; width, depth, bits, background : integer; var palette ) : integer;
var
gh : GIFHeader;
i : integer;
gifsig : string;
pal : TByteArray absolute palette;
a : byte;
begin
FillChar( gh, sizeof(GIFHeader),0 );
gifsig := 'GIF87a';
move( gifsig[1], gh.sig[1], 6 );
gh.screenwidth := width;
gh.screendepth := depth;
gh.background := background;
gh.aspect := 0;
gh.flags := $80 or ((bits-1) shl 4) or ((bits-1) and $07);
blockwrite( fp, gh, sizeof(GIFHeader) );
for i := 0 to (1 shl bits)*3-1 do
begin
// a := pal[i] shl 2;
a := pal[i];
blockwrite( fp, a, 1 );
end;
WriteScreenDesc := 0;
end;
function WriteImageDesc( var fp : file; left, top, width, depth, bits : integer ) : integer;
var
ib : ImageBlock;
ch : char;
begin
fillchar( ib, sizeof(ImageBlock), 0 );
ch := ',';
blockwrite( fp, ch, 1 );
ib.left := left;
ib.top := top;
ib.width := width;
ib.depth := depth;
ib.flags := bits-1;
blockwrite( fp, ib, sizeof(ImageBlock) );
WriteImageDesc := 0;
end;
function CompressImage( var fp : file; mincodesize : word ) : integer;
var
prefixcode, suffixchar, hx, d : integer;
codebuffer, newcode : ^TByteArray;
oldcode, currentcode : ^TIntArray;
codesize, clearcode, eofcode, bitoffset,
byteoffset, bitsleft, maxcode, freecode : integer;
procedure InitTable( mincodesize : integer );
var
i : integer;
begin
codesize := mincodesize + 1;
clearcode := 1 shl mincodesize;
eofcode := clearcode+1;
freecode := clearcode+2;
maxcode := 1 shl codesize;
for i := 0 to tablesize-1 do
currentcode^[i] := 0;
end;
procedure Deallocate;
begin
freemem( newcode, tablesize+1 );
freemem( currentcode, (tablesize+1)*2 );
freemem( oldcode, (tablesize+1)*2 );
freemem( codebuffer, 260 );
end;
procedure FlushFile( var fp : file; n : integer );
var
a : byte;
begin
a := n;
blockwrite( fp, a, 1 );
blockwrite( fp, codebuffer^[0], n );
end;
procedure WriteCode( var fp : file; code : integer );
var
temp : longint;
begin
byteoffset := bitoffset shr 3;
bitsleft := bitoffset and 7;
if byteoffset >= 254 then
begin
FlushFile( fp, byteoffset );
codebuffer^[0] := codebuffer^[byteoffset];
bitoffset := bitsleft;
byteoffset := 0;
end;
if bitsleft > 0 then
begin
temp := ( longint(code) shl bitsleft ) or codebuffer^[byteoffset];
codebuffer^[byteoffset] := temp;
codebuffer^[byteoffset+1] := temp shr 8;
codebuffer^[byteoffset+2] := temp shr 16;
end else
begin
codebuffer^[byteoffset] := code;
codebuffer^[byteoffset+1] := code shr 8;
end;
bitoffset := bitoffset + codesize;
end;
begin
if (mincodesize<2) or (mincodesize>9) then
if mincodesize = 1 then
mincodesize := 2
else
begin
CompressImage := 1;
exit;
end;
getmem( codebuffer, 260 );
getmem( oldcode, (tablesize+1)*2 );
getmem( currentcode, (tablesize+1)*2 );
getmem( newcode, tablesize+1 );
bitoffset := 0;
InitTable( mincodesize );
blockwrite( fp, mincodesize, 1 );
suffixchar := GIFInPixelProc;
if suffixchar < 0 then
begin
CompressImage := 1;
Deallocate;
exit;
end;
prefixcode := suffixchar;
suffixchar := GIFInPixelProc;
while suffixchar>=0 do
begin
hx := (prefixcode xor (suffixchar shl 5)) mod tablesize;
d := 1;
while true do
begin
if currentcode^[hx] = 0 then
begin
writecode( fp, prefixcode );
d := freecode;
if freecode <= largestcode then
begin
oldcode^[hx] := prefixcode;
newcode^[hx] := suffixchar;
currentcode^[hx] := freecode;
inc(freecode);
end;
if d=maxcode then
begin
if codesize<12 then
begin
inc(codesize);
maxcode := maxcode shl 1;
end else
begin
writecode( fp, clearcode );
InitTable( mincodesize );
end;
end;
prefixcode := suffixchar;
break;
end;
if (oldcode^[hx] = prefixcode) and (newcode^[hx] = suffixchar ) then
begin
prefixcode := currentcode^[hx];
break;
end;
hx := hx + d;
d := d + 2;
if hx >= tablesize then hx := hx - tablesize;
end;
suffixchar := GIFInPixelProc;
end;
writecode( fp, prefixcode );
writecode( fp, eofcode );
if bitoffset >0 then FlushFile( fp, (bitoffset+7) div 8 );
FlushFile( fp, 0 );
CompressImage := 0;
Deallocate;
end;
function WriteGif( var fp : file; width, depth, bits : integer; var palette ) : integer;
var
ch : char;
begin
if WriteScreenDesc( fp, width, depth, bits, 0, palette )>0 then
WriteGIF := 1
else
if WriteImageDesc( fp, 0, 0, width, depth, bits )>0 then
WriteGIF := 2
else
if CompressImage( fp, bits )>0 then
WriteGIF := 3
else
begin
WriteGIF := 0;
ch := ';';
blockwrite( fp, ch, 1 );
end;
end;
function SaveGif( f : string; width, depth, bits : integer; var palette ) : integer;
var
D: DirStr;
N: NameStr;
E: ExtStr;
FileHandle : File;
begin
FSplit( F, D, N, E );
if E='' then E:='.GIF';
F := D+N+E;
{$I-}
assign( FileHandle, F );
rewrite( FileHandle, 1 );
{$I+}
if ioresult = 0 then
SaveGif := WriteGif( FileHandle, width, depth, bits, palette )
else
SaveGif := NoFile;
{$I-}
close( FileHandle );
{$I+}
end;
function GifError;
begin
case ErrorCode of
GoodRead : GifError := 'Ok';
BadFile : GifError := 'Bad File';
BadRead : GifError := 'Bad Read';
UnexpectedEOF : GifError := 'Unexpected End';
BadCode : GifError := 'Bad LZW Code';
BadFirstCode : GifError := 'Bad First Code';
BadSymbolSize : GifError := 'Bad Symbol Size';
NoFile : GifError := 'File Not Found';
else GifError := 'Unknown';
end;
end; { GifError }
end.