forked from mike-lischke/GraphicEx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GraphicCompression.pas
2621 lines (2234 loc) · 74.8 KB
/
GraphicCompression.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
unit GraphicCompression;
// The original code is GraphicCompression.pas, released November 1, 1999.
//
// The initial developer of the original code is Mike Lischke (www.soft-gems.net),
//
// Copyright (C) 1999-2003 Mike Lischke. All Rights Reserved.
//----------------------------------------------------------------------------------------------------------------------
//
// This file is part of the image library GraphicEx.
//
// GraphicCompression contains various encoder/decoder classes used to handle compressed
// data in the various image classes.
//
// Currently supported methods are:
// - LZW (Lempel-Ziff-Welch)
// + TIF
// + GIF
// - RLE (run length encoding)
// + TGA,
// + PCX,
// + packbits
// + SGI
// + CUT
// + RLA
// + PSP
// - CCITT
// + raw G3 (fax T.4)
// + modified G3 (CCITT RLE)
// + modified G3 w/ word alignment (CCITT RLEW)
// - LZ77
// - Thunderscan
// - JPEG
// - PCD Huffmann encoding (photo CD)
//
//----------------------------------------------------------------------------------------------------------------------
interface
{$I Compilers.inc}
{$I GraphicConfiguration.inc}
uses
Windows, Classes, SysUtils, Graphics,
zLibEx, ZLibExApi; // general inflate/deflate and LZ77 compression support
type
// abstract decoder class to define the base functionality of an encoder/decoder
TDecoder = class
public
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); virtual; abstract;
procedure DecodeEnd; virtual;
procedure DecodeInit; virtual;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); virtual; abstract;
procedure EncodeInit; virtual;
procedure EncodeEnd; virtual;
end;
// generally, there should be no need to cover the decoder classes by conditional symbols
// because the image classes which use the decoder classes are already covered and if they
// aren't compiled then the decoders are also not compiled (more precisely: not linked)
TTargaRLEDecoder = class(TDecoder)
private
FColorDepth: Cardinal;
public
constructor Create(ColorDepth: Cardinal);
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
// Lempel-Ziff-Welch encoder/decoder class
// TIFF LZW compression / decompression is a bit different to the common LZW code
TTIFFLZWDecoder = class(TDecoder)
public
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TPackbitsRLEDecoder = class(TDecoder)
public
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TPCXRLEDecoder = class(TDecoder)
public
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TSGIRLEDecoder = class(TDecoder)
private
FSampleSize: Byte; // 8 or 16 bits
public
constructor Create(SampleSize: Byte);
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TCUTRLEDecoder = class(TDecoder)
public
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TPSPRLEDecoder = class(TDecoder)
public
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
// Note: We need a different LZW decoder class for GIF because the bit order is reversed compared to that
// of TIFF and the code size increment is handled slightly different.
TGIFLZWDecoder = class(TDecoder)
private
FInitialCodeSize: Byte;
public
constructor Create(InitialCodeSize: Byte);
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TRLADecoder = class(TDecoder)
public
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TStateEntry = record
NewState: array[Boolean] of Cardinal;
RunLength: Integer;
end;
TStateArray = array of TStateEntry;
TCCITTDecoder = class(TDecoder)
private
FOptions: Integer; // determines some options how to proceed
// Bit 0: if set then two-dimensional encoding was used, otherwise one-dimensional
// Bit 1: if set then data is uncompressed
// Bit 2: if set then fill bits are used before EOL codes so that EOL codes always end at
// at a byte boundary (not used in this context)
FIsWhite, // alternating flag used while coding
FSwapBits: Boolean; // True if the order of all bits in a byte must be swapped
FWhiteStates,
FBlackStates,
F2DStates: TStateArray;
FWidth: Cardinal; // need to know how line length for modified huffman encoding
// coding/encoding variables
FBitsLeft,
FMask,
FBits: Byte;
FPackedSize,
FRestWidth: Cardinal;
FSource,
FTarget: PByte;
FFreeTargetBits: Byte;
FWordAligned: Boolean;
//some fields for 2D compression. Will be used in Fax3 (T4) as well as Fax4 (T6) decoders
fChangingElems: array [Boolean] of array of Integer; //coordinates of these pixels on prev. row
fcurChangingElem: Integer; //even (0,2,...) means changing from white to black,
//odd (1,3,...) are black to white
fprevChangingElem: Integer;
fRowUsed: Boolean;
fBitPos: Integer;
procedure MakeStates;
protected
procedure ReverseBits(Source: Pointer; PackedSize: Integer);
function FillRun(RunLength: Cardinal): Boolean;
function DoFiniteStateMachine(const states: TStateArray): Integer;
function FindRunLength: Integer;
function Find2DCode: Integer;
function NextBit: Boolean;
//2D decompression routines
procedure UpdateChangingElem;
public
constructor Create(Options: Integer; SwapBits, WordAligned: Boolean; Width: Cardinal);
end;
TCCITTFax3Decoder = class(TCCITTDecoder)
public
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TCCITTFax4Decoder = class (TCCITTDecoder)
public
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TCCITTMHDecoder = class(TCCITTDecoder) // modified Huffman RLE
public
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TLZ77Decoder = class(TDecoder)
private
FStream: TZStreamRec;
FZLibResult, // contains the return code of the last ZLib operation
FFlushMode: Integer; // one of flush constants declard in ZLib.pas
// this is usually Z_FINISH for PSP and Z_PARTIAL_FLUSH for PNG
FAutoReset: Boolean; // TIF, PSP and PNG share this decoder, TIF needs a reset for each
// decoder run
function GetAvailableInput: Integer;
function GetAvailableOutput: Integer;
public
constructor Create(FlushMode: Integer; AutoReset: Boolean);
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure DecodeEnd; override;
procedure DecodeInit; override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
property AvailableInput: Integer read GetAvailableInput;
property AvailableOutput: Integer read GetAvailableOutput;
property ZLibResult: Integer read FZLibResult;
end;
TThunderDecoder = class(TDecoder)
private
FWidth: Cardinal; // width of a scanline in pixels
public
constructor Create(Width: Cardinal);
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
TPCDDecoder = class(TDecoder)
private
FData: PByte; // decoder must read some data
public
constructor Create(Raw: Pointer);
procedure Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer); override;
procedure Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal); override;
end;
//----------------------------------------------------------------------------------------------------------------------
implementation
uses
Math,
GraphicEx,
GraphicStrings,
GraphicColor;
const // LZW encoding and decoding support
NoLZWCode = 4096;
type
EGraphicCompression = class(Exception);
//----------------------------------------------------------------------------------------------------------------------
procedure CompressionError(ErrorString: String); overload;
begin
raise EGraphicCompression.Create(ErrorString);
end;
//----------------- TDecoder (generic decoder class) -------------------------------------------------------------------
procedure TDecoder.DecodeEnd;
// called after all decompression has been done
begin
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TDecoder.DecodeInit;
// called before any decompression can start
begin
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TDecoder.EncodeEnd;
// called after all compression has been done
begin
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TDecoder.EncodeInit;
// called before any compression can start
begin
end;
//----------------- TTargaRLEDecoder -----------------------------------------------------------------------------------
constructor TTargaRLEDecoder.Create(ColorDepth: Cardinal);
begin
FColorDepth := ColorDepth;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTargaRLEDecoder.Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer);
type
PCardinalArray = ^TCardinalArray;
TCardinalArray = array[0..MaxInt div 4 - 1] of Cardinal;
var
I: Integer;
SourcePtr,
TargetPtr: PByte;
RunLength: Cardinal;
SourceCardinal: Cardinal;
begin
TargetPtr := Dest;
SourcePtr := Source;
// unrolled decoder loop to speed up process
case FColorDepth of
8:
while UnpackedSize > 0 do
begin
RunLength := 1 + (SourcePtr^ and $7F);
if SourcePtr^ > $7F then
begin
Inc(SourcePtr);
FillChar(TargetPtr^, RunLength, SourcePtr^);
Inc(TargetPtr, RunLength);
Inc(SourcePtr);
end
else
begin
Inc(SourcePtr);
Move(SourcePtr^, TargetPtr^, RunLength);
Inc(SourcePtr, RunLength);
Inc(TargetPtr, RunLength);
end;
Dec(UnpackedSize, RunLength);
end;
15,
16:
while UnpackedSize > 0 do
begin
RunLength := 1 + (SourcePtr^ and $7F);
if SourcePtr^ > $7F then
begin
Inc(SourcePtr);
for I := 0 to RunLength - 1 do
begin
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
TargetPtr^ := SourcePtr^;
Dec(SourcePtr);
Inc(TargetPtr);
end;
Inc(SourcePtr, 2);
end
else
begin
Inc(SourcePtr);
Move(SourcePtr^, TargetPtr^, 2 * RunLength);
Inc(SourcePtr, 2 * RunLength);
Inc(TargetPtr, 2 * RunLength);
end;
Dec(UnpackedSize, RunLength);
end;
24:
while UnpackedSize > 0 do
begin
RunLength := 1 + (SourcePtr^ and $7F);
if SourcePtr^ > $7F then
begin
Inc(SourcePtr);
for I := 0 to RunLength - 1 do
begin
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
TargetPtr^ := SourcePtr^;
Dec(SourcePtr, 2);
Inc(TargetPtr);
end;
Inc(SourcePtr, 3);
end
else
begin
Inc(SourcePtr);
Move(SourcePtr^, TargetPtr^, 3 * RunLength);
Inc(SourcePtr, 3 * RunLength);
Inc(TargetPtr, 3 * RunLength);
end;
Dec(UnpackedSize, RunLength);
end;
32:
while UnpackedSize > 0 do
begin
RunLength := 1 + (SourcePtr^ and $7F);
if SourcePtr^ > $7F then
begin
Inc(SourcePtr);
SourceCardinal := PCardinalArray(SourcePtr)[0];
for I := 0 to RunLength - 1 do
PCardinalArray(TargetPtr)[I] := SourceCardinal;
Inc(TargetPtr, 4 * RunLength);
Inc(SourcePtr, 4);
end
else
begin
Inc(SourcePtr);
Move(SourcePtr^, TargetPtr^, 4 * RunLength);
Inc(SourcePtr, 4 * RunLength);
Inc(TargetPtr, 4 * RunLength);
end;
Dec(UnpackedSize, RunLength);
end;
end;
Source := SourcePtr;
end;
//----------------------------------------------------------------------------------------------------------------------
function GetPixel(P: PByte; BPP: Byte): Cardinal;
// Retrieves a pixel value from a Buffer. The actual size and order of the bytes is not important
// since we are only using the value for comparisons with other pixels.
begin
Result := P^;
Inc(P);
Dec(BPP);
while BPP > 0 do
begin
Result := Result shl 8;
Result := Result or P^;
Inc(P);
Dec(BPP);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function CountDiffPixels(P: PByte; BPP: Byte; Count: Integer): Integer;
// counts pixels in Buffer until two identical adjacent ones found
var
N: Integer;
Pixel,
NextPixel: Cardinal;
begin
N := 0;
NextPixel := 0; // shut up compiler
if Count = 1 then Result := Count
else
begin
Pixel := GetPixel(P, BPP);
while Count > 1 do
begin
Inc(P, BPP);
NextPixel := GetPixel(P, BPP);
if NextPixel = Pixel then Break;
Pixel := NextPixel;
Inc(N);
Dec(Count);
end;
if NextPixel = Pixel then
Result := N
else
Result := N + 1;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function CountSamePixels(P: PByte; BPP: Byte; Count: Integer): Integer;
var
Pixel,
NextPixel: Cardinal;
begin
Result := 1;
Pixel := GetPixel(P, BPP);
Dec(Count);
while Count > 0 do
begin
Inc(P, BPP);
NextPixel := GetPixel(P, BPP);
if NextPixel <> Pixel then
Break;
Inc(Result);
Dec(Count);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTargaRLEDecoder.Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal);
// Encodes "Count" bytes pointed to by Source into the Buffer supplied with Target and returns the
// number of bytes stored in Target. BPP denotes bytes per pixel color depth.
// Note: The target Buffer must provide enough space to hold the compressed data. Using a size of
// twice the size of the input Buffer is sufficent.
var
DiffCount, // pixel count until two identical
SameCount: Integer; // number of identical adjacent pixels
SourcePtr,
TargetPtr: PByte;
BPP: Integer;
begin
BytesStored := 0;
SourcePtr := Source;
TargetPtr := Dest;
BytesStored := 0;
// +1 for 15 bits to get the correct 2 bytes per pixel
BPP := (FColorDepth + 1) div 8;
while Count > 0 do
begin
DiffCount := CountDiffPixels(SourcePtr, BPP, Count);
SameCount := CountSamePixels(SourcePtr, BPP, Count);
if DiffCount > 128 then
DiffCount := 128;
if SameCount > 128 then
SameCount := 128;
if DiffCount > 0 then
begin
// create a raw packet
TargetPtr^ := DiffCount - 1; Inc(TargetPtr);
Dec(Count, DiffCount);
Inc(BytesStored, (DiffCount * BPP) + 1);
while DiffCount > 0 do
begin
TargetPtr^ := SourcePtr^; Inc(SourcePtr); Inc(TargetPtr);
if BPP > 1 then
begin
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
end;
if BPP > 2 then
begin
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
end;
if BPP > 3 then
begin
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
end;
Dec(DiffCount);
end;
end;
if SameCount > 1 then
begin
// create a RLE packet
TargetPtr^ := (SameCount - 1) or $80; Inc(TargetPtr);
Dec(Count, SameCount);
Inc(BytesStored, BPP + 1);
Inc(SourcePtr, (SameCount - 1) * BPP);
TargetPtr^ := SourcePtr^; Inc(SourcePtr); Inc(TargetPtr);
if BPP > 1 then
begin
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
end;
if BPP > 2 then
begin
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
end;
if BPP > 3 then
begin
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
end;
end;
end;
end;
//----------------- TTIFFLZWDecoder ------------------------------------------------------------------------------------
procedure TTIFFLZWDecoder.Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer);
var
I: Integer;
Data, // current data
Bits, // counter for bit management
Code: Cardinal; // current code value
SourcePtr: PByte;
InCode: Cardinal; // Buffer for passed code
CodeSize: Cardinal;
CodeMask: Cardinal;
FreeCode: Cardinal;
OldCode: Cardinal;
Prefix: array[0..4095] of Cardinal; // LZW prefix
Suffix, // LZW suffix
Stack: array [0..4095] of Byte; // stack
StackPointer: PByte;
Target: PByte;
FirstChar: Byte; // Buffer for decoded byte
ClearCode,
EOICode: Word;
begin
Target := Dest;
SourcePtr := Source;
// initialize parameter
ClearCode := 1 shl 8;
EOICode := ClearCode + 1;
FreeCode := ClearCode + 2;
OldCode := NoLZWCode;
CodeSize := 9;
CodeMask := (1 shl CodeSize) - 1;
// init code table
for I := 0 to ClearCode - 1 do
begin
Prefix[I] := NoLZWCode;
Suffix[I] := I;
end;
// initialize stack
StackPointer := @Stack;
FirstChar := 0;
Data := 0;
Bits := 0;
while (PackedSize > 0) and (UnpackedSize > 0) do
begin
// read code from bit stream
Inc(Data, Cardinal(SourcePtr^) shl (24 - Bits));
Inc(Bits, 8);
while Bits >= CodeSize do
begin
// current code
Code := (Data and ($FFFFFFFF - CodeMask)) shr (32 - CodeSize);
// mask it
Data := Data shl CodeSize;
Dec(Bits, CodeSize);
if Code = EOICode then
Exit;
// handling of clear codes
if Code = ClearCode then
begin
// reset of all variables
CodeSize := 9;
CodeMask := (1 shl CodeSize) - 1;
FreeCode := ClearCode + 2;
OldCode := NoLZWCode;
Continue;
end;
// check whether it is a valid, already registered code
if Code > FreeCode then
Break;
// handling for the first LZW code: print and keep it
if OldCode = NoLZWCode then
begin
FirstChar := Suffix[Code];
Target^ := FirstChar;
Inc(Target);
Dec(UnpackedSize);
OldCode := Code;
Continue;
end;
// keep the passed LZW code
InCode := Code;
// the first LZW code is always smaller than FFirstCode
if Code = FreeCode then
begin
StackPointer^ := FirstChar;
Inc(StackPointer);
Code := OldCode;
end;
// loop to put decoded bytes onto the stack
while Code > ClearCode do
begin
StackPointer^ := Suffix[Code];
Inc(StackPointer);
Code := Prefix[Code];
end;
// place new code into code table
FirstChar := Suffix[Code];
StackPointer^ := FirstChar;
Inc(StackPointer);
Prefix[FreeCode] := OldCode;
Suffix[FreeCode] := FirstChar;
if FreeCode < 4096 then
Inc(FreeCode);
// increase code size if necessary
if (FreeCode = CodeMask) and
(CodeSize < 12) then
begin
Inc(CodeSize);
CodeMask := (1 shl CodeSize) - 1;
end;
// put decoded bytes (from the stack) into the target Buffer
OldCode := InCode;
repeat
Dec(StackPointer);
Target^ := StackPointer^;
Inc(Target);
Dec(UnpackedSize);
until Cardinal(StackPointer) <= Cardinal(@Stack);
end;
Inc(SourcePtr);
Dec(PackedSize);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTIFFLZWDecoder.Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal);
begin
end;
//----------------- TPackbitsRLEDecoder --------------------------------------------------------------------------------
procedure TPackbitsRLEDecoder.Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer);
// decodes a simple run-length encoded strip of size PackedSize
var
SourcePtr,
TargetPtr: PByte;
N: Integer;
begin
TargetPtr := Dest;
SourcePtr := Source;
while (UnpackedSize > 0) and (PackedSize > 0) do
begin
N := ShortInt(SourcePtr^);
Inc(SourcePtr);
Dec(PackedSize);
if N < 0 then // replicate next Byte -N + 1 times
begin
if N = -128 then
Continue; // nop
N := -N + 1;
if N > UnpackedSize then
N := UnpackedSize;
FillChar(TargetPtr^, N, SourcePtr^);
Inc(SourcePtr);
Dec(PackedSize);
Inc(TargetPtr, N);
Dec(UnpackedSize, N);
end
else
begin // copy next N + 1 bytes literally
Inc(N);
if N > UnpackedSize then
N := UnpackedSize;
if N > PackedSize then
N := PackedSize;
Move(SourcePtr^, TargetPtr^, N);
Inc(TargetPtr, N);
Inc(SourcePtr, N);
Dec(PackedSize, N);
Dec(UnpackedSize, N);
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPackbitsRLEDecoder.Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal);
begin
end;
//----------------- TPCXRLEDecoder -------------------------------------------------------------------------------------
procedure TPCXRLEDecoder.Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer);
var
Count: Integer;
SourcePtr,
TargetPtr: PByte;
begin
SourcePtr := Source;
TargetPtr := Dest;
while UnpackedSize > 0 do
begin
if (SourcePtr^ and $C0) = $C0 then
begin
// RLE-Code
Count := SourcePtr^ and $3F;
Inc(SourcePtr);
if UnpackedSize < Count then
Count := UnpackedSize;
FillChar(TargetPtr^, Count, SourcePtr^);
Inc(SourcePtr);
Inc(TargetPtr, Count);
Dec(UnpackedSize, Count);
end
else
begin
// not compressed
TargetPtr^ := SourcePtr^;
Inc(SourcePtr);
Inc(TargetPtr);
Dec(UnpackedSize);
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPCXRLEDecoder.Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal);
begin
end;
//----------------- TSGIRLEDecoder -------------------------------------------------------------------------------------
constructor TSGIRLEDecoder.Create(SampleSize: Byte);
begin
FSampleSize := SampleSize;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TSGIRLEDecoder.Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer);
var
Source8,
Target8: PByte;
Source16,
Target16: PWord;
Pixel: Byte;
Pixel16: Word;
RunLength: Cardinal;
begin
if FSampleSize = 8 then
begin
Source8 := Source;
Target8 := Dest;
while True do
begin
Pixel := Source8^;
Inc(Source8);
RunLength := Pixel and $7F;
if RunLength = 0 then
Break;
if (Pixel and $80) <> 0 then
begin
Move(Source8^, Target8^, RunLength);
Inc(Target8, RunLength);
Inc(Source8, RunLength);
end
else
begin
Pixel := Source8^;
Inc(Source8);
FillChar(Target8^, RunLength, Pixel);
Inc(Target8, RunLength);
end;
end;
end
else
begin
// 16 bits per sample
Source16 := Source;
Target16 := Dest;
while True do
begin
// SGI images are stored in big endian style, swap this one repeater value for it
Pixel16 := Swap(Source16^);
Inc(Source16);
RunLength := Pixel16 and $7F;
if RunLength = 0 then
Break;
if (Pixel16 and $80) <> 0 then
begin
Move(Source16^, Target16^, 2 * RunLength);
Inc(Source16, RunLength);
Inc(Target16, RunLength);
end
else
begin
Pixel16 := Source16^;
Inc(Source16);
while RunLength > 0 do
begin
Target16^ := Pixel16;
Inc(Target16);
Dec(RunLength);
end;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TSGIRLEDecoder.Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal);
begin
end;
//----------------- TCUTRLE --------------------------------------------------------------------------------------------
procedure TCUTRLEDecoder.Decode(var Source, Dest: Pointer; PackedSize, UnpackedSize: Integer);
var
TargetPtr: PByte;
Pixel: Byte;
RunLength: Cardinal;
Run: PByte absolute Source; //PByte alias for Source
begin
TargetPtr := Dest;
// Skip first two bytes per row (I don't know their meaning).
Inc(Run, 2);
while True do
begin
Pixel := Run^;
Inc(Run);
if Pixel = 0 then
Break;
RunLength := Pixel and $7F;
if (Pixel and $80) = 0 then
begin
Move(Run^, TargetPtr^, RunLength);
Inc(TargetPtr, RunLength);
Inc(Run, RunLength);
end
else
begin
Pixel := Run^;
Inc(Run);
FillChar(TargetPtr^, RunLength, Pixel);
Inc(TargetPtr, RunLength);
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TCUTRLEDecoder.Encode(Source, Dest: Pointer; Count: Cardinal; var BytesStored: Cardinal);
begin
end;
//----------------- TPSPRLEDecoder -------------------------------------------------------------------------------------