forked from cutec-chris/gifanim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gifanim.pas
1129 lines (1001 loc) · 29.6 KB
/
gifanim.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
{
Copyright (C) 2009 Laurent Jacques
This source is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This code 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.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
Version 1.4
}
unit GifAnim;
{$mode objfpc}{$H+}
interface
uses
Classes, LCLProc, Lresources, SysUtils, Controls, Graphics, ExtCtrls,
IntfGraphics, FPimage, Contnrs, GraphType, dialogs;
const
EXT_INTRODUCER = $21;
EXT_GRAPHICS_CONTROL = $F9;
EXT_PLAIN_TEXT = $01;
EXT_APPLICATION = $FF;
EXT_COMMENT = $FE;
DSC_LOCAL_IMAGE = $2C;
ID_TRANSPARENT = $01;
ID_COLOR_TABLE_SIZE = $07;
ID_SORT = $20;
ID_INTERLACED = $40;
ID_COLOR_TABLE = $80;
ID_IMAGE_DESCRIPTOR = $2C;
ID_TRAILER = $3B;
CODE_TABLE_SIZE = 4096;
type
TRGB = packed record
Red, Green, Blue: byte;
end;
TGIFHeader = packed record
Signature: array[0..2] of char; //* Header Signature (always "GIF") */
Version: array[0..2] of char; //* GIF format version("87a" or "89a") */
ScreenWidth: word; //* Width of Display Screen in Pixels */
ScreenHeight: word; //* Height of Display Screen in Pixels */
Packedbit, //* Screen and Color Map Information */
BackgroundColor, //* Background Color Index */
AspectRatio: byte; //* Pixel Aspect Ratio */
end;
TGifImageDescriptor = packed record
Left, //* X position of image on the display */
Top, //* Y position of image on the display */
Width, //* Width of the image in pixels */
Height: word; //* Height of the image in pixels */
Packedbit: byte; //* Image and Color Table Data Information */
end;
TGifGraphicsControlExtension = packed record
BlockSize, //* Size of remaining fields (always 04h) */
Packedbit: byte; //* Method of graphics disposal to use */
DelayTime: word; //* Hundredths of seconds to wait */
ColorIndex, //* Transparent Color Index */
Terminator: byte; //* Block Terminator (always 0) */
end;
TGifAnim = class;
{ TGifImage }
TGifImage = class
private
FBitmap: TBitmap;
FPosX: word;
FPosY: word;
FDelay: word;
FMethod: byte;
public
constructor Create;
destructor Destroy; override;
property Bitmap: TBitmap Read FBitmap;
property Delay: word Read FDelay;
property Method: byte Read FMethod;
property PosX: word Read FPosX;
property PosY: word Read FPosY;
end;
{ TGifList }
TGifList = class(TObjectList)
private
protected
function GetItems(Index: integer): TGifImage;
procedure SetItems(Index: integer; AGifImage: TGifImage);
public
function Add(AGifImage: TGifImage): integer;
function Extract(Item: TGifImage): TGifImage;
function Remove(AGifImage: TGifImage): integer;
function IndexOf(AGifImage: TGifImage): integer;
function First: TGifImage;
function Last: TGifImage;
procedure Insert(Index: integer; AGifImage: TGifImage);
property Items[Index: integer]: TGifImage Read GetItems Write SetItems; default;
end;
{ TGifLoader }
TGifLoader = class
private
FGifHeader: TGIFHeader;
FGifDescriptor: TGifImageDescriptor;
FGifGraphicsCtrlExt: TGifGraphicsControlExtension;
FGifUseGraphCtrlExt: boolean;
FGifBackgroundColor: byte;
FInterlaced: boolean;
FScanLine: PByte;
FLineSize: integer;
FDisposalMethod: byte;
FEmpty: boolean;
FFileName: string;
FHeight: integer;
FIsTransparent: boolean;
FWidth: integer;
FPalette: TFPPalette;
FLocalHeight: integer;
FLocalWidth: integer;
procedure ReadPalette(Stream: TStream; Size: integer);
procedure ReadScanLine(Stream: TStream);
procedure ReadHeader(Stream: TStream);
procedure ReadGlobalPalette(Stream: TStream);
procedure ReadGraphCtrlExt;
procedure SetInterlaced(const AValue: boolean);
procedure SetTransparent(const AValue: boolean);
function SkipBlock(Stream: TStream): byte;
procedure WriteScanLine(Img: TFPCustomImage);
procedure ReadGifBitmap(Stream: TStream);
public
constructor Create(const FileName: string);
destructor Destroy; override;
function LoadAllBitmap(var AGifList: TGifList): boolean;
function LoadFromLazarusResource(const ResName: String; var AGifList: TGifList): boolean;
function LoadFirstBitmap(var ABitmap: TBitmap): boolean;
property Empty: boolean Read FEmpty;
property Height: integer Read FHeight;
property Width: integer Read FWidth;
property IsTransparent: boolean Read FIsTransparent Write SetTransparent;
property Interlaced: boolean Read FInterlaced Write SetInterlaced;
end;
{ TGifAnim }
TGifAnim = class(TGraphicControl)
private
{ Private declarations }
FAnimate: boolean;
FEmpty: boolean;
FFileName: string;
FGifBitmaps: TGifList;
FOnFrameChanged: TNotifyEvent;
FOnStart: TNotifyEvent;
FOnStop: TNotifyEvent;
FWait: TTimer;
FCurrentImage: integer;
FGifHeight: integer;
FGifWidth: integer;
procedure OnTime(Sender: TObject);
procedure SetAnimate(const AValue: boolean);
procedure SetFileName(const AValue: string);
procedure DefineSize(AWidth, AHeight: integer);
protected
{ Protected declarations }
BufferImg: TBitmap;
CurrentView: TBitmap;
procedure CalculatePreferredSize(
var PreferredWidth, PreferredHeight: integer;
WithThemeSpace: boolean); override;
procedure DoAutoSize; override;
procedure DoStartAnim;
procedure DoStopAnim;
class function GetControlClassDefaultSize: TPoint; override;
procedure GifChanged;
procedure LoadFromFile(const Filename: string); virtual;
procedure Paint; override;
procedure ResetImage;
procedure SetColor(Value: TColor); override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Empty: boolean Read FEmpty;
property GifBitmaps: TGifList Read FGifBitmaps;
property GifIndex: integer Read FCurrentImage;
function LoadFromLazarusResource(const ResName: String): boolean;
published
{ Published declarations }
property Anchors;
property AutoSize default True;
property Animate: boolean Read FAnimate Write SetAnimate default True;
property BorderSpacing;
property Color default clBtnFace;
property Constraints;
property FileName: string Read FFileName Write SetFileName;
property Height;
property OnClick;
property OnDblClick;
property OnFrameChanged: TNotifyEvent Read FOnFrameChanged Write FOnFrameChanged;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnStartAnim: TNotifyEvent Read FOnStart Write FOnStart;
property OnStopAnim: TNotifyEvent Read FOnStop Write FOnStop;
property ParentShowHint;
property ShowHint;
property Visible;
property Width;
end;
procedure Register;
implementation
uses LazIDEIntf, propedits;
Type
TGifFileNamePropertyEditor=class(TFileNamePropertyEditor)
protected
function GetFilter: String; override;
function GetInitialDirectory: string; override;
end;
function TGifFileNamePropertyEditor.GetFilter: String;
begin
Result := 'GIF|*.gif';
end;
function TGifFileNamePropertyEditor.GetInitialDirectory: string;
begin
Result:= ExtractFilePath(LazarusIDE.ActiveProject.ProjectInfoFile);
end;
procedure Register;
begin
RegisterComponents('Wile64', [TGifAnim]);
RegisterPropertyEditor(TypeInfo(String),
TGifAnim, 'FileName', TGifFileNamePropertyEditor);
end;
{ TGifAnim }
constructor TGifAnim.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := [csCaptureMouse, csClickEvents, csDoubleClicks];
AutoSize := True;
SetInitialBounds(0, 0, GetControlClassDefaultSize.X, GetControlClassDefaultSize.Y);
FEmpty := True;
FCurrentImage := 0;
CurrentView := TBitmap.Create;
if not (csDesigning in ComponentState) then
begin
BufferImg := TBitmap.Create;
FWait := TTimer.Create(Self);
with FWait do
begin
Interval := 100;
OnTimer := @OnTime;
Enabled := False;
end;
end;
Animate := True;
end;
destructor TGifAnim.Destroy;
begin
inherited Destroy;
if assigned(FGifBitmaps) then
FreeAndNil(FGifBitmaps);
BufferImg.Free;
CurrentView.Free;
end;
function TGifAnim.LoadFromLazarusResource(const ResName: String): boolean;
var
GifLoader: TGifLoader;
StateAnimate: boolean;
Resource: TLResource;
begin
Result:=false;
StateAnimate:= Animate;
FWait.Enabled:= false;
ResetImage;
Resource:=nil;
Resource:=LazarusResources.Find(ResName);
if Resource <> nil then
if CompareText(LazarusResources.Find(ResName).ValueType, 'gif')=0 then begin
GifLoader := TGifLoader.Create(Filename);
FEmpty := not GifLoader.LoadFromLazarusResource(ResName, FGifBitmaps);
DefineSize(GifLoader.Width, GifLoader.Height);
GifLoader.Free;
Result:= FEmpty;
end;
if not Empty then
GifChanged;
FWait.Enabled:= StateAnimate;
end;
procedure TGifAnim.LoadFromFile(const Filename: string);
var
GifLoader: TGifLoader;
begin
FEmpty := True;
if not FileExists(Filename) then
Exit;
GifLoader := TGifLoader.Create(Filename);
if (csDesigning in ComponentState) then
FEmpty := not GifLoader.LoadFirstBitmap(CurrentView)
else
FEmpty := not GifLoader.LoadAllBitmap(FGifBitmaps);
DefineSize(GifLoader.Width, GifLoader.Height);
GifLoader.Free;
end;
procedure TGifAnim.OnTime(Sender: TObject);
begin
if (not Empty) and Visible then
begin
if FCurrentImage > GifBitmaps.Count - 1 then
FCurrentImage := 0;
if assigned(FOnFrameChanged) then
FOnFrameChanged(self);
Paint;
Inc(FCurrentImage);
end;
end;
procedure TGifAnim.SetAnimate(const AValue: boolean);
begin
if FAnimate = AValue then
exit;
FAnimate := AValue;
if not (csDesigning in ComponentState) then
begin
FWait.Enabled := Animate;
if Animate then
DoStartAnim
else
DoStopAnim;
end;
end;
procedure TGifAnim.SetFileName(const AValue: string);
var
fn: string;
begin
if (FFileName = AValue) then
exit;
FFileName := AValue;
ResetImage;
if (FFileName = '') then exit;
if (csDesigning in ComponentState) then
begin
fn:= ExtractFileName(AValue);
FFileName:= ExtractFilePath(AValue);
FFileName:= ExtractRelativepath(ExtractFilePath(LazarusIDE.ActiveProject.ProjectInfoFile) ,FFileName);
FFileName:=FFileName+fn;
LoadFromFile(FFileName+fn);
end
else begin
FFileName := AValue;
LoadFromFile(FFileName);
end;
if not Empty then
GifChanged;
end;
procedure TGifAnim.DefineSize(AWidth, AHeight: integer);
begin
if (AWidth = FGifWidth) and (AHeight = FGifHeight) then
Exit;
FGifWidth := AWidth;
FGifHeight := AHeight;
Height := FGifHeight;
Width := FGifWidth;
if not (csDesigning in ComponentState) then
begin
BufferImg.Height := Height;
BufferImg.Width := Width;
end;
end;
procedure TGifAnim.CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
WithThemeSpace: boolean);
begin
PreferredWidth := FGifWidth;
PreferredHeight := FGifHeight;
end;
procedure TGifAnim.DoAutoSize;
var
ModifyWidth, ModifyHeight: boolean;
NewWidth: integer;
NewHeight: integer;
begin
if AutoSizing then
Exit; // we shouldn't come here in the first place
BeginAutoSizing;
try
GetPreferredSize(NewWidth, NewHeight);
ModifyWidth := [akLeft, akRight] * (Anchors + AnchorAlign[Align]) <> [akLeft, akRight];
ModifyHeight := [akTop, akBottom] * (Anchors + AnchorAlign[Align]) <> [akTop, akBottom];
if not ModifyWidth then
NewWidth := Width;
if not ModifyHeight then
NewHeight := Height;
if (NewWidth <> Width) or (NewHeight <> Height) then
begin
SetBounds(Left, Top, NewWidth, NewHeight);
end;
finally
EndAutoSizing;
end;
end;
class function TGifAnim.GetControlClassDefaultSize: TPoint;
begin
Result.X := 90;
Result.Y := 90;
end;
procedure TGifAnim.GifChanged;
begin
if not (csDesigning in ComponentState) then
begin
BufferImg.Canvas.Brush.Color := (self.Color);
BufferImg.Canvas.FillRect(Rect(0, 0, Width, Height));
with GifBitmaps.Items[FCurrentImage] do
BufferImg.Canvas.Draw(PosX, PosY, Bitmap);
CurrentView.Assign(BufferImg);
end;
InvalidatePreferredSize;
AdjustSize;
end;
procedure TGifAnim.Paint;
begin
if (not Empty) and Visible then
begin
if not (csDesigning in ComponentState) then
begin
if (FCurrentImage < GifBitmaps.Count) then
with GifBitmaps.Items[FCurrentImage] do
begin
BufferImg.Canvas.Brush.Color := (self.Color);
if FCurrentImage = 0 then
BufferImg.Canvas.FillRect(Rect(0, 0, Width, Height));
if Delay <> 0 then
FWait.Interval := Delay * 10;
BufferImg.Canvas.Draw(PosX, PosY, Bitmap);
CurrentView.Assign(BufferImg);
case Method of
//0 : Not specified...
//1 : No change Background
2: BufferImg.Canvas.FillRect(
Rect(PosX, PosY, Bitmap.Width + PosX, Bitmap.Height + PosY));
3: BufferImg.Canvas.FillRect(Rect(0, 0, Width, Height));
end;
end;
end
else
begin
Canvas.Brush.Color := (self.Color);
Canvas.FillRect(Rect(0, 0, Width, Height));
end;
Canvas.Draw(0, 0, CurrentView);
end;
inherited Paint;
end;
procedure TGifAnim.ResetImage;
begin
if assigned(FGifBitmaps) then
FreeAndNil(FGifBitmaps);
FCurrentImage:=0;
with CurrentView do
begin
Canvas.Brush.Color := (self.Color);
Canvas.FillRect(Rect(0, 0, Width, Height));
end;
end;
procedure TGifAnim.SetColor(Value: TColor);
begin
inherited SetColor(Value);
end;
procedure TGifAnim.DoStartAnim;
begin
if assigned(OnStartAnim) then
OnStartAnim(Self);
end;
procedure TGifAnim.DoStopAnim;
begin
if assigned(OnStopAnim) then
OnStartAnim(Self);
end;
{ TGifLoader }
constructor TGifLoader.Create(const FileName: string);
begin
FFileName := FileName;
FGifUseGraphCtrlExt := False;
FPalette := TFPPalette.Create(0);
FHeight := 20;
FWidth := 20;
end;
destructor TGifLoader.Destroy;
begin
inherited Destroy;
FPalette.Free;
end;
function TGifLoader.LoadAllBitmap(var AGifList: TGifList): boolean;
var
GifStream: TMemoryStream;
Introducer: byte;
FPImage: TLazIntfImage;
ImgFormatDescription: TRawImageDescription;
GifBitmap: TGifImage;
begin
Result := False;
if not FileExists(FFileName) then
exit;
if not assigned(AGifList) then
AGifList := TGifList.Create(True);
GifStream := TMemoryStream.Create;
GifStream.LoadFromFile(FFileName);
GifStream.Position := 0;
ReadHeader(GifStream);
if (FGifHeader.Version <> '89a') then
Exit;
// skip first block extention if exist
repeat
Introducer := SkipBlock(GifStream);
until (Introducer = ID_IMAGE_DESCRIPTOR) or (Introducer = ID_TRAILER);
repeat
ReadGifBitmap(GifStream);
// decode Gif bitmap in Scanline buffer
ReadScanLine(GifStream);
// Create temp Fp Image for put scanline pixel
FPImage := TLazIntfImage.Create(FLocalWidth, FLocalHeight);
ImgFormatDescription.Init_BPP32_B8G8R8A8_BIO_TTB(FLocalWidth,
FLocalHeight);
FPImage.DataDescription := ImgFormatDescription;
WriteScanLine(FPImage);
GifBitmap := TGifImage.Create;
GifBitmap.FBitmap.LoadFromIntfImage(FPImage);
GifBitmap.FPosX := FGifDescriptor.Left;
GifBitmap.FPosY := FGifDescriptor.Top;
GifBitmap.FMethod := FDisposalMethod;
GifBitmap.FDelay := FGifGraphicsCtrlExt.DelayTime;
AGifList.Add(GifBitmap);
FPImage.Free;
FreeMem(FScanLine, FLineSize);
// reset FGifUseGraphCtrlExt flag
FGifUseGraphCtrlExt := False;
repeat
Introducer := SkipBlock(GifStream);
until (Introducer = ID_IMAGE_DESCRIPTOR) or (Introducer = ID_TRAILER);
until (Introducer = ID_TRAILER);
GifStream.Free;
Result := True;
end;
function TGifLoader.LoadFromLazarusResource(const ResName: String; var AGifList: TGifList): boolean;
var
GifStream: TLazarusResourceStream;
Introducer: byte;
FPImage: TLazIntfImage;
ImgFormatDescription: TRawImageDescription;
GifBitmap: TGifImage;
begin
Result := False;
if not assigned(AGifList) then
AGifList := TGifList.Create(True);
GifStream := TLazarusResourceStream.Create(ResName, nil);
GifStream.Position := 0;
ReadHeader(GifStream);
if (FGifHeader.Version <> '89a') then
Exit;
// skip first block extention if exist
repeat
Introducer := SkipBlock(GifStream);
until (Introducer = ID_IMAGE_DESCRIPTOR) or (Introducer = ID_TRAILER);
repeat
ReadGifBitmap(GifStream);
// decode Gif bitmap in Scanline buffer
ReadScanLine(GifStream);
// Create temp Fp Image for put scanline pixel
FPImage := TLazIntfImage.Create(FLocalWidth, FLocalHeight);
ImgFormatDescription.Init_BPP32_B8G8R8A8_BIO_TTB(FLocalWidth,
FLocalHeight);
FPImage.DataDescription := ImgFormatDescription;
WriteScanLine(FPImage);
GifBitmap := TGifImage.Create;
GifBitmap.FBitmap.LoadFromIntfImage(FPImage);
GifBitmap.FPosX := FGifDescriptor.Left;
GifBitmap.FPosY := FGifDescriptor.Top;
GifBitmap.FMethod := FDisposalMethod;
GifBitmap.FDelay := FGifGraphicsCtrlExt.DelayTime;
AGifList.Add(GifBitmap);
FPImage.Free;
FreeMem(FScanLine, FLineSize);
// reset FGifUseGraphCtrlExt flag
FGifUseGraphCtrlExt := False;
repeat
Introducer := SkipBlock(GifStream);
until (Introducer = ID_IMAGE_DESCRIPTOR) or (Introducer = ID_TRAILER);
until (Introducer = ID_TRAILER);
GifStream.Free;
Result := True;
end;
function TGifLoader.LoadFirstBitmap(var ABitmap: TBitmap): boolean;
var
GifStream: TMemoryStream;
Introducer: byte;
FPImage: TLazIntfImage;
ImgFormatDescription: TRawImageDescription;
begin
Result := False;
if not FileExists(FFileName) then
exit;
if not assigned(ABitmap) then
ABitmap := TBitmap.Create;
GifStream := TMemoryStream.Create;
GifStream.LoadFromFile(FFileName);
GifStream.Position := 0;
ReadHeader(GifStream);
if (FGifHeader.Version <> '89a') then
Exit;
// skip first block extention if exist
repeat
Introducer := SkipBlock(GifStream);
until (Introducer = ID_IMAGE_DESCRIPTOR) or (Introducer = ID_TRAILER);
ReadGifBitmap(GifStream);
// decode Gif bitmap in Scanline buffer
ReadScanLine(GifStream);
// Create temp Fp Image for put scanline pixel
FPImage := TLazIntfImage.Create(FLocalWidth, FLocalHeight);
ImgFormatDescription.Init_BPP32_B8G8R8A8_BIO_TTB(FLocalWidth,
FLocalHeight);
FPImage.DataDescription := ImgFormatDescription;
WriteScanLine(FPImage);
ABitmap.LoadFromIntfImage(FPImage);
FPImage.Free;
FreeMem(FScanLine, FLineSize);
// reset FGifUseGraphCtrlExt flag
FGifUseGraphCtrlExt := False;
GifStream.Free;
Result := True;
end;
procedure TGifLoader.SetTransparent(const AValue: boolean);
begin
if FIsTransparent = AValue then
exit;
FIsTransparent := AValue;
end;
function TGifLoader.SkipBlock(Stream: TStream): byte;
var
Introducer, Labels, SkipByte: byte;
begin
Introducer := 0;
Labels := 0;
SkipByte := 0;
Stream.Read(Introducer, 1);
if Introducer = EXT_INTRODUCER then
begin
Stream.Read(Labels, 1);
case Labels of
EXT_COMMENT,
EXT_APPLICATION:
while True do
begin
Stream.Read(SkipByte, 1);
if SkipByte = 0 then
Break;
Stream.Seek(SkipByte, soFromCurrent);
end;
EXT_GRAPHICS_CONTROL:
begin
Stream.Read(FGifGraphicsCtrlExt, SizeOf(FGifGraphicsCtrlExt));
FGifUseGraphCtrlExt := True;
end;
EXT_PLAIN_TEXT:
begin
Stream.Read(SkipByte, 1);
Stream.Seek(SkipByte, soFromCurrent);
while True do
begin
Stream.Read(SkipByte, 1);
if SkipByte = 0 then
Break;
Stream.Seek(SkipByte, soFromCurrent);
end;
end;
end;
end;
Result := Introducer;
end;
procedure TGifLoader.ReadScanLine(Stream: TStream);
var
OldPos, UnpackedSize, PackedSize: longint;
I: integer;
Data, Bits, Code: cardinal;
SourcePtr: PByte;
InCode: cardinal;
CodeSize: cardinal;
CodeMask: cardinal;
FreeCode: cardinal;
OldCode: cardinal;
Prefix: array[0..CODE_TABLE_SIZE - 1] of cardinal;
Suffix, Stack: array [0..CODE_TABLE_SIZE - 1] of byte;
StackPointer: PByte;
DataComp, Target: PByte;
B, FInitialCodeSize, FirstChar: byte;
ClearCode, EOICode: word;
begin
FInitialCodeSize := 0;
B := 0;
DataComp := nil;
// initialisation du dictionnaire de decompression
Stream.Read(FInitialCodeSize, 1);
// Recherche la taille des données compresser
OldPos := Stream.Position;
PackedSize := 0;
repeat
Stream.Read(B, 1);
if B > 0 then
begin
Inc(PackedSize, B);
Stream.Seek(B, soFromCurrent);
end;
until B = 0;
Getmem(DataComp, PackedSize);
// lecture des données conpresser
SourcePtr := DataComp;
Stream.Position := OldPos;
repeat
Stream.Read(B, 1);
if B > 0 then
begin
Stream.ReadBuffer(SourcePtr^, B);
Inc(SourcePtr, B);
end;
until B = 0;
SourcePtr := DataComp;
Target := FScanLine;
CodeSize := FInitialCodeSize + 1;
ClearCode := 1 shl FInitialCodeSize;
EOICode := ClearCode + 1;
FreeCode := ClearCode + 2;
OldCode := CODE_TABLE_SIZE;
CodeMask := (1 shl CodeSize) - 1;
UnpackedSize := FLocalWidth * FLocalHeight;
for I := 0 to ClearCode - 1 do
begin
Prefix[I] := CODE_TABLE_SIZE;
Suffix[I] := I;
end;
StackPointer := @Stack;
FirstChar := 0;
Data := 0;
Bits := 0;
//Decompression LZW gif
while (UnpackedSize > 0) and (PackedSize > 0) do
begin
Inc(Data, SourcePtr^ shl Bits);
Inc(Bits, 8);
while Bits >= CodeSize do
begin
Code := Data and CodeMask;
Data := Data shr CodeSize;
Dec(Bits, CodeSize);
if Code = EOICode then
Break;
if Code = ClearCode then
begin
CodeSize := FInitialCodeSize + 1;
CodeMask := (1 shl CodeSize) - 1;
FreeCode := ClearCode + 2;
OldCode := CODE_TABLE_SIZE;
Continue;
end;
if Code > FreeCode then
Break;
if OldCode = CODE_TABLE_SIZE then
begin
FirstChar := Suffix[Code];
Target^ := FirstChar;
Inc(Target);
Dec(UnpackedSize);
OldCode := Code;
Continue;
end;
InCode := Code;
if Code = FreeCode then
begin
StackPointer^ := FirstChar;
Inc(StackPointer);
Code := OldCode;
end;
while Code > ClearCode do
begin
StackPointer^ := Suffix[Code];
Inc(StackPointer);
Code := Prefix[Code];
end;
FirstChar := Suffix[Code];
StackPointer^ := FirstChar;
Inc(StackPointer);
Prefix[FreeCode] := OldCode;
Suffix[FreeCode] := FirstChar;
if (FreeCode = CodeMask) and (CodeSize < 12) then
begin
Inc(CodeSize);
CodeMask := (1 shl CodeSize) - 1;
end;
if FreeCode < CODE_TABLE_SIZE - 1 then
Inc(FreeCode);
OldCode := InCode;
repeat
Dec(StackPointer);
Target^ := StackPointer^;
Inc(Target);
Dec(UnpackedSize);
until StackPointer = @Stack;
end;
Inc(SourcePtr);
Dec(PackedSize);
end;
FreeMem(DataComp);
end;
procedure TGifLoader.ReadHeader(Stream: TStream);
begin
Stream.Read(FGifHeader, SizeOf(FGifHeader));
with FGifHeader do
begin
FGifBackgroundColor := BackgroundColor;
FWidth := ScreenWidth;
FHeight := ScreenHeight;
FLocalWidth := ScreenWidth;
FLocalHeight := ScreenHeight;
IsTransparent := False;
end;
ReadGlobalPalette(Stream);
end;
procedure TGifLoader.ReadGlobalPalette(Stream: TStream);
var
ColorTableSize: integer;
begin
if (FGifHeader.Packedbit and ID_COLOR_TABLE) <> 0 then
begin
ColorTableSize := FGifHeader.Packedbit and ID_COLOR_TABLE_SIZE + 1;
ReadPalette(Stream, 1 shl ColorTableSize);
end;
end;
procedure TGifLoader.ReadGraphCtrlExt;
var
C: TFPColor;
begin
IsTransparent := (FGifGraphicsCtrlExt.Packedbit and ID_TRANSPARENT) <> 0;
FDisposalMethod := (FGifGraphicsCtrlExt.Packedbit and $1C) shr 2;
if IsTransparent then
begin
// if Transparent bitmap change alpha channel
FGifBackgroundColor := FGifGraphicsCtrlExt.ColorIndex;
C := FPalette[FGifBackgroundColor];
C.alpha := alphaTransparent;
FPalette[FGifBackgroundColor] := C;
end;
end;
procedure TGifLoader.SetInterlaced(const AValue: boolean);
begin
if FInterlaced = AValue then
exit;
FInterlaced := AValue;
end;
procedure TGifLoader.ReadPalette(Stream: TStream; Size: integer);
var
RGBEntry: TRGB;
I: integer;
C: TFPColor;
begin
FPalette.Clear;
FPalette.Count := 0;
Fillchar(RGBEntry, SizeOf(RGBEntry), 0);
for I := 0 to Size - 1 do
begin
Stream.Read(RGBEntry, SizeOf(RGBEntry));
with C do
begin
Red := RGBEntry.Red or (RGBEntry.Red shl 8);
Green := RGBEntry.Green or (RGBEntry.Green shl 8);
Blue := RGBEntry.Blue or (RGBEntry.Blue shl 8);
Alpha := alphaOpaque;
end;
FPalette.Add(C);
end;
end;
procedure TGifLoader.WriteScanLine(Img: TFPCustomImage);
var
Row, Col: integer;
Pass, Every: byte;
P: PByte;
begin
P := FScanLine;
if Interlaced then
begin
for Pass := 1 to 4 do
begin
case Pass of
1:
begin
Row := 0;
Every := 8;
end;
2:
begin