forked from bgrabitmap/BGRABitmapDelphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgraanimatedgif.pas
1267 lines (1109 loc) · 39.7 KB
/
bgraanimatedgif.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
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRAAnimatedGif;
{$i bgrabitmap.inc}{$H+}{$H+}
interface
uses
Classes, SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType, {$ENDIF}
{$IFDEF BGRABITMAP_USE_LCL}Graphics, {$ENDIF}
BGRAGraphics, FPImage, BGRABitmap, BGRABitmapTypes,
BGRAPalette, BGRAGifFormat;
type
TDisposeMode = BGRAGifFormat.TDisposeMode;
TGifSubImage = BGRAGifFormat.TGifSubImage;
TGifSubImageArray = BGRAGifFormat.TGifSubImageArray;
//how to deal with the background under the GIF animation
TGifBackgroundMode = (gbmSimplePaint, gbmEraseBackground,
gbmSaveBackgroundOnce, gbmUpdateBackgroundContinuously);
{ TBGRAAnimatedGif }
TBGRAAnimatedGif = class(TGBRACustomGraphic)
private
FAspectRatio: single;
FWidth, FHeight: integer;
FBackgroundColor: TColor;
FPrevDate: TDateTime;
FPaused: boolean;
FTimeAccumulator: double;
FCurrentImage, FWantedImage: integer;
FTotalAnimationTime: BGRAInt64;
FPreviousDisposeMode: TDisposeMode;
FBackgroundImage, FPreviousVirtualScreen, FStretchedVirtualScreen,
FInternalVirtualScreen, FRestoreImage: TBGRABitmap;
FImageChanged: boolean;
procedure CheckFrameIndex(AIndex: integer);
function GetAverageDelayMs: integer;
function GetCount: integer;
function GetFrameDelayMs(AIndex: integer): integer;
function GetFrameDisposeMode(AIndex: integer): TDisposeMode;
function GetFrameHasLocalPalette(AIndex: integer): boolean;
function GetFrameImage(AIndex: integer): TBGRABitmap;
function GetFrameImagePos(AIndex: integer): TPoint;
function GetTimeUntilNextImage: integer;
procedure Render(StretchWidth, StretchHeight: integer);
procedure SetAspectRatio(AValue: single);
procedure SetBackgroundColor(AValue: TColor);
procedure SetFrameDelayMs(AIndex: integer; AValue: integer);
procedure SetFrameDisposeMode(AIndex: integer; AValue: TDisposeMode);
procedure SetFrameHasLocalPalette(AIndex: integer; AValue: boolean);
procedure SetFrameImage(AIndex: integer; AValue: TBGRABitmap);
procedure SetFrameImagePos(AIndex: integer; AValue: TPoint);
procedure UpdateSimple(Canvas: TCanvas; ARect: TRect;
DrawOnlyIfChanged: boolean = True);
procedure UpdateEraseBackground(Canvas: TCanvas; ARect: TRect;
DrawOnlyIfChanged: boolean = True);
procedure Init;
function GetBitmap: TBitmap;
function GetMemBitmap: TBGRABitmap;
procedure SaveBackgroundOnce(Canvas: TCanvas; ARect: TRect);
procedure SetCurrentImage(Index: integer);
protected
FImages: TGifSubImageArray;
{TGraphic}
procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
function GetEmpty: boolean; override;
function GetHeight: integer; override;
function GetTransparent: boolean; override;
function GetWidth: integer; override;
procedure SetHeight({%H-}Value: integer); override;
procedure SetTransparent({%H-}Value: boolean); override;
procedure SetWidth({%H-}Value: integer); override;
procedure ClearViewer; virtual;
public
EraseColor: TColor;
BackgroundMode: TGifBackgroundMode;
LoopCount: BGRAWord;
LoopDone: Integer;
constructor Create(filenameUTF8: string); overload;
constructor Create(stream: TStream); overload;
constructor Create(stream: TStream; AMaxImageCount: integer); overload;
constructor Create; overload; override;
function Duplicate: TBGRAAnimatedGif;
function AddFrame(AImage: TFPCustomImage; X,Y: integer; ADelayMs: integer;
ADisposeMode: TDisposeMode = dmErase; AHasLocalPalette: boolean = false) : integer;
procedure InsertFrame(AIndex: integer; AImage: TFPCustomImage; X,Y: integer; ADelayMs: integer;
ADisposeMode: TDisposeMode = dmErase; AHasLocalPalette: boolean = false);
procedure DeleteFrame(AIndex: integer; AEnsureNextFrameDoesNotChange: boolean);
//add a frame that replaces completely the previous one
function AddFullFrame(AImage: TFPCustomImage; ADelayMs: integer;
AHasLocalPalette: boolean = true): integer;
procedure InsertFullFrame(AIndex: integer;
AImage: TFPCustomImage; ADelayMs: integer;
AHasLocalPalette: boolean = true);
procedure ReplaceFullFrame(AIndex: integer;
AImage: TFPCustomImage; ADelayMs: integer;
AHasLocalPalette: boolean = true);
{TGraphic}
procedure LoadFromStream(Stream: TStream); overload; override;
procedure LoadFromStream(Stream: TStream; AMaxImageCount: integer); overload;
procedure SaveToStream(Stream: TStream); overload; override;
procedure LoadFromFile(const AFilenameUTF8: string); override;
procedure SaveToFile(const AFilenameUTF8: string); override;
class function GetFileExtensions: string; {$IFNDEF BDS}override;{$ENDIF}
procedure SetSize(AWidth,AHeight: integer); virtual;
procedure SaveToStream(Stream: TStream; AQuantizer: TBGRAColorQuantizerAny;
ADitheringAlgorithm: TDitheringAlgorithm); overload; virtual;
procedure Clear; override;
destructor Destroy; override;
procedure Pause;
procedure Resume;
procedure Show(Canvas: TCanvas; ARect: TRect); overload;
procedure Update(Canvas: TCanvas; ARect: TRect); overload;
procedure Hide(Canvas: TCanvas; ARect: TRect); overload;
property BackgroundColor: TColor Read FBackgroundColor write SetBackgroundColor;
property Count: integer Read GetCount;
property Width: integer Read FWidth;
property Height: integer Read FHeight;
property Paused: boolean Read FPaused;
property Bitmap: TBitmap Read GetBitmap;
property MemBitmap: TBGRABitmap Read GetMemBitmap;
property CurrentImage: integer Read FCurrentImage Write SetCurrentImage;
property TimeUntilNextImageMs: integer read GetTimeUntilNextImage;
property FrameImage[AIndex: integer]: TBGRABitmap read GetFrameImage write SetFrameImage;
property FrameHasLocalPalette[AIndex: integer]: boolean read GetFrameHasLocalPalette write SetFrameHasLocalPalette;
property FrameImagePos[AIndex: integer]: TPoint read GetFrameImagePos write SetFrameImagePos;
property FrameDelayMs[AIndex: integer]: integer read GetFrameDelayMs write SetFrameDelayMs;
property FrameDisposeMode[AIndex: integer]: TDisposeMode read GetFrameDisposeMode write SetFrameDisposeMode;
property AspectRatio: single read FAspectRatio write SetAspectRatio;
property TotalAnimationTimeMs: BGRAInt64 read FTotalAnimationTime;
property AverageDelayMs: integer read GetAverageDelayMs;
end;
{ TBGRAReaderGIF }
TBGRAReaderGIF = class(TFPCustomImageReader)
protected
procedure InternalRead(Str: TStream; Img: TFPCustomImage); override;
function InternalCheck(Str: TStream): boolean; override;
end;
{ TBGRAWriterGIF }
TBGRAWriterGIF = class(TFPCustomImageWriter)
protected
procedure InternalWrite(Str: TStream; Img: TFPCustomImage); override;
end;
const
GifBackgroundModeStr: array[TGifBackgroundMode] of string =
('gbmSimplePaint', 'gbmEraseBackground', 'gbmSaveBackgroundOnce',
'gbmUpdateBackgroundContinuously');
implementation
uses BGRABlend, BGRAUTF8;
const
{$IFDEF ENDIAN_LITTLE}
AlphaMask = $FF000000;
{$ELSE}
AlphaMask = $000000FF;
{$ENDIF}
{ TBGRAAnimatedGif }
class function TBGRAAnimatedGif.GetFileExtensions: string;
begin
Result := 'gif';
end;
procedure TBGRAAnimatedGif.SetSize(AWidth, AHeight: integer);
begin
ClearViewer;
FWidth := AWidth;
FHeight := AHeight;
end;
procedure TBGRAAnimatedGif.SaveToStream(Stream: TStream;
AQuantizer: TBGRAColorQuantizerAny;
ADitheringAlgorithm: TDitheringAlgorithm);
var data: TGIFData;
begin
data.Height:= Height;
data.Width := Width;
data.AspectRatio := 1;
data.BackgroundColor := BackgroundColor;
data.Images := FImages;
data.LoopCount := LoopCount;
GIFSaveToStream(data, Stream, AQuantizer, ADitheringAlgorithm);
end;
procedure TBGRAAnimatedGif.Render(StretchWidth, StretchHeight: integer);
var
curDate: TDateTime;
previousImage, nextImage: integer;
begin
if FInternalVirtualScreen = nil then
begin
FInternalVirtualScreen := TBGRABitmap.Create(FWidth, FHeight);
if (Count = 0) and (BackgroundColor <> clNone) then
FInternalVirtualScreen.Fill(BackgroundColor)
else
FInternalVirtualScreen.Fill(BGRAPixelTransparent);
FImageChanged := True;
end;
if Count = 0 then
exit;
previousImage := FCurrentImage;
curDate := Now;
if FWantedImage <> -1 then
begin
nextImage := FWantedImage;
FTimeAccumulator := 0;
FWantedImage := -1;
end
else
if FCurrentImage = -1 then
begin
nextImage := 0;
FTimeAccumulator := 0;
FPreviousDisposeMode := dmNone;
end
else
begin
if not FPaused then
FTimeAccumulator := FTimeAccumulator + (curDate - FPrevDate) * 24 * 60 * 60 * 1000;
if FTotalAnimationTime > 0 then FTimeAccumulator:= frac(FTimeAccumulator/FTotalAnimationTime)*FTotalAnimationTime;
nextImage := FCurrentImage;
while FTimeAccumulator > FImages[nextImage].DelayMs do
begin
FTimeAccumulator := FTimeAccumulator -FImages[nextImage].DelayMs;
Inc(nextImage);
if nextImage >= Count then
begin
if (LoopCount > 0) and (LoopDone >= LoopCount-1) then
begin
LoopDone := LoopCount;
dec(nextImage);
break;
end else
begin
nextImage := 0;
inc(LoopDone);
end;
end;
if nextImage = previousImage then
begin
if not ((LoopCount > 0) and (LoopDone >= LoopCount-1)) then
begin
Inc(nextImage);
if nextImage >= Count then
nextImage := 0;
end;
break;
end;
end;
end;
FPrevDate := curDate;
while FCurrentImage <> nextImage do
begin
Inc(FCurrentImage);
if FCurrentImage >= Count then
begin
FCurrentImage := 0;
FPreviousDisposeMode := dmErase;
end;
case FPreviousDisposeMode of
dmErase: FInternalVirtualScreen.Fill(BGRAPixelTransparent);
dmRestore: if FRestoreImage <> nil then
FInternalVirtualScreen.PutImage(0, 0, FRestoreImage, dmSet);
end;
with FImages[FCurrentImage] do
begin
if disposeMode = dmRestore then
begin
if FRestoreImage = nil then
FRestoreImage := TBGRABitmap.Create(FWidth, FHeight);
FRestoreImage.PutImage(0, 0, FInternalVirtualScreen, dmSet);
end;
if Image <> nil then
FInternalVirtualScreen.PutImage(Position.X, Position.Y, Image,
dmSetExceptTransparent);
FPreviousDisposeMode := DisposeMode;
end;
FImageChanged := True;
previousImage := FCurrentImage;
FInternalVirtualScreen.InvalidateBitmap;
end;
if FStretchedVirtualScreen <> nil then
FStretchedVirtualScreen.FreeReference;
if (FInternalVirtualScreen.Width = StretchWidth) and
(FInternalVirtualScreen.Height = StretchHeight) then
FStretchedVirtualScreen := TBGRABitmap(FInternalVirtualScreen.NewReference)
else
FStretchedVirtualScreen :=
TBGRABitmap(FInternalVirtualScreen.Resample(StretchWidth, StretchHeight));
end;
procedure TBGRAAnimatedGif.SetAspectRatio(AValue: single);
begin
if AValue < 0.25 then AValue := 0.25;
if AValue > 4 then AValue := 4;
if FAspectRatio=AValue then Exit;
FAspectRatio:=AValue;
end;
procedure TBGRAAnimatedGif.SetBackgroundColor(AValue: TColor);
begin
if FBackgroundColor=AValue then Exit;
FBackgroundColor:=AValue;
end;
procedure TBGRAAnimatedGif.SetFrameDelayMs(AIndex: integer; AValue: integer);
begin
CheckFrameIndex(AIndex);
if AValue < 0 then AValue := 0;
FTotalAnimationTime := FTotalAnimationTime + AValue - FImages[AIndex].DelayMs;
FImages[AIndex].DelayMs := AValue;
end;
procedure TBGRAAnimatedGif.SetFrameDisposeMode(AIndex: integer;
AValue: TDisposeMode);
begin
CheckFrameIndex(AIndex);
FImages[AIndex].DisposeMode := AValue;
end;
procedure TBGRAAnimatedGif.SetFrameHasLocalPalette(AIndex: integer;
AValue: boolean);
begin
CheckFrameIndex(AIndex);
FImages[AIndex].HasLocalPalette := AValue;
end;
procedure TBGRAAnimatedGif.SetFrameImage(AIndex: integer; AValue: TBGRABitmap);
var ACopy: TBGRABitmap;
begin
CheckFrameIndex(AIndex);
ACopy := AValue.Duplicate as TBGRABitmap;
FImages[AIndex].Image.FreeReference;
FImages[AIndex].Image := ACopy;
end;
procedure TBGRAAnimatedGif.SetFrameImagePos(AIndex: integer; AValue: TPoint);
begin
CheckFrameIndex(AIndex);
FImages[AIndex].Position := AValue;
end;
procedure TBGRAAnimatedGif.UpdateSimple(Canvas: TCanvas; ARect: TRect;
DrawOnlyIfChanged: boolean = True);
begin
if FPreviousVirtualScreen <> nil then
begin
FPreviousVirtualScreen.FreeReference;
FPreviousVirtualScreen := nil;
end;
Render(ARect.Right - ARect.Left, ARect.Bottom - ARect.Top);
if FImageChanged then
begin
FStretchedVirtualScreen.Draw(Canvas, ARect.Left, ARect.Top, False);
FImageChanged := False;
end
else
if not DrawOnlyIfChanged then
FStretchedVirtualScreen.Draw(Canvas, ARect.Left, ARect.Top, False);
FPreviousVirtualScreen := TBGRABitmap(FStretchedVirtualScreen.NewReference);
end;
procedure TBGRAAnimatedGif.CheckFrameIndex(AIndex: integer);
begin
if (AIndex < 0) or (AIndex >= Count) then Raise ERangeError.Create('Index out of bounds');
end;
function TBGRAAnimatedGif.GetAverageDelayMs: integer;
var sum: BGRAInt64;
i: Integer;
begin
if Count > 0 then
begin
sum := 0;
for i := 0 to Count-1 do
inc(sum, FrameDelayMs[i]);
result := sum div Count;
end else
result := 100; //default
end;
function TBGRAAnimatedGif.GetCount: integer;
begin
Result := length(FImages);
end;
function TBGRAAnimatedGif.GetFrameDelayMs(AIndex: integer): integer;
begin
CheckFrameIndex(AIndex);
result := FImages[AIndex].DelayMs;
end;
function TBGRAAnimatedGif.GetFrameDisposeMode(AIndex: integer): TDisposeMode;
begin
CheckFrameIndex(AIndex);
result := FImages[AIndex].DisposeMode;
end;
function TBGRAAnimatedGif.GetFrameHasLocalPalette(AIndex: integer): boolean;
begin
CheckFrameIndex(AIndex);
result := FImages[AIndex].HasLocalPalette;
end;
function TBGRAAnimatedGif.GetFrameImage(AIndex: integer): TBGRABitmap;
begin
CheckFrameIndex(AIndex);
result := FImages[AIndex].Image;
end;
function TBGRAAnimatedGif.GetFrameImagePos(AIndex: integer): TPoint;
begin
CheckFrameIndex(AIndex);
result := FImages[AIndex].Position;
end;
function TBGRAAnimatedGif.GetTimeUntilNextImage: integer;
var
acc: double;
begin
if Count <= 1 then result := 60*1000 else
if (FWantedImage <> -1) or (FCurrentImage = -1) then
result := 0
else
begin
acc := FTimeAccumulator;
if not FPaused then acc := acc +( (Now- FPrevDate) * 24 * 60 * 60 * 1000);
if acc >= FImages[FCurrentImage].DelayMs then
result := 0
else
result := round(FImages[FCurrentImage].DelayMs-FTimeAccumulator);
end;
end;
constructor TBGRAAnimatedGif.Create(filenameUTF8: string);
begin
inherited Create;
Init;
LoadFromFile(filenameUTF8);
end;
constructor TBGRAAnimatedGif.Create(stream: TStream);
begin
inherited Create;
Init;
LoadFromStream(stream);
end;
constructor TBGRAAnimatedGif.Create(stream: TStream; AMaxImageCount: integer);
begin
inherited Create;
Init;
LoadFromStream(stream, AMaxImageCount);
end;
constructor TBGRAAnimatedGif.Create;
begin
inherited Create;
Init;
LoadFromStream(nil);
end;
function TBGRAAnimatedGif.Duplicate: TBGRAAnimatedGif;
var
i: integer;
begin
Result := TBGRAAnimatedGif.Create;
setlength(Result.FImages, length(FImages));
for i := 0 to high(FImages) do
begin
Result.FImages[i] := FImages[i];
FImages[i].Image.NewReference;
end;
Result.FWidth := FWidth;
Result.FHeight := FHeight;
Result.FBackgroundColor := FBackgroundColor;
end;
function TBGRAAnimatedGif.AddFrame(AImage: TFPCustomImage; X, Y: integer;
ADelayMs: integer; ADisposeMode: TDisposeMode; AHasLocalPalette: boolean
): integer;
begin
result := length(FImages);
setlength(FImages, length(FImages)+1);
if ADelayMs < 0 then ADelayMs:= 0;
with FImages[result] do
begin
Image := TBGRABitmap.Create(AImage);
Position := Point(x,y);
DelayMs := ADelayMs;
HasLocalPalette := AHasLocalPalette;
DisposeMode := ADisposeMode;
end;
inc(FTotalAnimationTime, ADelayMs);
end;
procedure TBGRAAnimatedGif.InsertFrame(AIndex: integer; AImage: TFPCustomImage; X,
Y: integer; ADelayMs: integer; ADisposeMode: TDisposeMode;
AHasLocalPalette: boolean);
var i: integer;
begin
if (AIndex < 0) or (AIndex > Count) then
raise ERangeError.Create('Index out of bounds');
setlength(FImages, length(FImages)+1);
if ADelayMs < 0 then ADelayMs:= 0;
for i := high(FImages) downto AIndex+1 do
FImages[i] := FImages[i-1];
with FImages[AIndex] do
begin
Image := TBGRABitmap.Create(AImage);
Position := Point(x,y);
DelayMs := ADelayMs;
HasLocalPalette := AHasLocalPalette;
DisposeMode := ADisposeMode;
end;
inc(FTotalAnimationTime, ADelayMs);
end;
function TBGRAAnimatedGif.AddFullFrame(AImage: TFPCustomImage;
ADelayMs: integer; AHasLocalPalette: boolean): integer;
begin
if (AImage.Width <> Width) or (AImage.Height <> Height) then
raise exception.Create('Size mismatch');
if Count > 0 then
FrameDisposeMode[Count-1] := dmErase;
result := AddFrame(AImage, 0,0, ADelayMs, dmErase, AHasLocalPalette);
end;
procedure TBGRAAnimatedGif.InsertFullFrame(AIndex: integer;
AImage: TFPCustomImage; ADelayMs: integer; AHasLocalPalette: boolean);
var nextImage: TBGRABitmap;
begin
if (AIndex < 0) or (AIndex > Count) then
raise ERangeError.Create('Index out of bounds');
if AIndex = Count then
AddFullFrame(AImage, ADelayMs, AHasLocalPalette)
else
begin
//if previous image did not clear up, ensure that
//next image will stay the same
if (AIndex > 0) and (FrameDisposeMode[AIndex-1] <> dmErase) then
begin
CurrentImage := AIndex;
nextImage := MemBitmap.Duplicate as TBGRABitmap;
FrameImagePos[AIndex] := Point(0,0);
FrameImage[AIndex] := nextImage;
FrameHasLocalPalette[AIndex] := true;
FreeAndNil(nextImage);
FrameDisposeMode[AIndex-1] := dmErase;
end;
InsertFrame(AIndex, AImage, 0,0, ADelayMs, dmErase, AHasLocalPalette);
end;
end;
procedure TBGRAAnimatedGif.ReplaceFullFrame(AIndex: integer;
AImage: TFPCustomImage; ADelayMs: integer; AHasLocalPalette: boolean);
begin
DeleteFrame(AIndex, True);
if AIndex > 0 then FrameDisposeMode[AIndex-1] := dmErase;
InsertFrame(AIndex, AImage, 0,0, ADelayMs, dmErase, AHasLocalPalette);
end;
procedure TBGRAAnimatedGif.DeleteFrame(AIndex: integer;
AEnsureNextFrameDoesNotChange: boolean);
var
nextImage: TBGRABitmap;
i: Integer;
begin
CheckFrameIndex(AIndex);
//if this frame did not clear up, ensure that
//next image will stay the same
if AEnsureNextFrameDoesNotChange and
((AIndex < Count-1) and (FrameDisposeMode[AIndex] <> dmErase)) then
begin
CurrentImage := AIndex+1;
nextImage := MemBitmap.Duplicate as TBGRABitmap;
FrameImagePos[AIndex+1] := Point(0,0);
FrameImage[AIndex+1] := nextImage;
FrameHasLocalPalette[AIndex+1] := true;
FreeAndNil(nextImage);
end;
dec(FTotalAnimationTime, FImages[AIndex].DelayMs);
FImages[AIndex].Image.FreeReference;
for i := AIndex to Count-2 do
FImages[i] := FImages[i+1];
SetLength(FImages, Count-1);
if (CurrentImage >= Count) then
CurrentImage := 0;
end;
procedure TBGRAAnimatedGif.LoadFromStream(Stream: TStream);
begin
LoadFromStream(Stream, maxLongint);
end;
procedure TBGRAAnimatedGif.LoadFromStream(Stream: TStream;
AMaxImageCount: integer);
var data: TGIFData;
i: integer;
begin
data := GIFLoadFromStream(Stream, AMaxImageCount);
ClearViewer;
Clear;
FWidth := data.Width;
FHeight := data.Height;
FBackgroundColor := data.BackgroundColor;
FAspectRatio:= data.AspectRatio;
LoopDone := 0;
LoopCount := data.LoopCount;
SetLength(FImages, length(data.Images));
FTotalAnimationTime:= 0;
for i := 0 to high(FImages) do
begin
FImages[i] := data.Images[i];
FTotalAnimationTime := FTotalAnimationTime + FImages[i].DelayMs;
end;
end;
procedure TBGRAAnimatedGif.SaveToStream(Stream: TStream);
begin
SaveToStream(Stream, BGRAColorQuantizerFactory, daFloydSteinberg);
end;
procedure TBGRAAnimatedGif.LoadFromFile(const AFilenameUTF8: string);
var stream: TFileStreamUTF8;
begin
stream := TFileStreamUTF8.Create(AFilenameUTF8,fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(stream);
finally
Stream.Free;
end;
end;
procedure TBGRAAnimatedGif.SaveToFile(const AFilenameUTF8: string);
var
Stream: TFileStreamUTF8;
begin
Stream := TFileStreamUTF8.Create(AFilenameUTF8, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
procedure TBGRAAnimatedGif.Draw(ACanvas: TCanvas; const Rect: TRect);
begin
if FBackgroundImage <> nil then
FreeAndNil(FBackgroundImage);
SaveBackgroundOnce(ACanvas, Rect);
if FPreviousVirtualScreen <> nil then
begin
FPreviousVirtualScreen.FreeReference;
FPreviousVirtualScreen := nil;
end;
Render(Rect.Right - Rect.Left, Rect.Bottom - Rect.Top);
FStretchedVirtualScreen.Draw(ACanvas, Rect.Left, Rect.Top, false);
FImageChanged := False;
FPreviousVirtualScreen := TBGRABitmap(FStretchedVirtualScreen.Duplicate);
end;
function TBGRAAnimatedGif.GetEmpty: boolean;
begin
Result := (length(FImages) = 0);
end;
function TBGRAAnimatedGif.GetHeight: integer;
begin
Result := FHeight;
end;
function TBGRAAnimatedGif.GetTransparent: boolean;
begin
Result := True;
end;
function TBGRAAnimatedGif.GetWidth: integer;
begin
Result := FWidth;
end;
procedure TBGRAAnimatedGif.SetHeight(Value: integer);
begin
//not implemented
end;
procedure TBGRAAnimatedGif.SetTransparent(Value: boolean);
begin
//not implemented
end;
procedure TBGRAAnimatedGif.SetWidth(Value: integer);
begin
//not implemented
end;
procedure TBGRAAnimatedGif.ClearViewer;
begin
FCurrentImage := -1;
FWantedImage := -1;
FTimeAccumulator := 0;
if FStretchedVirtualScreen <> nil then
FStretchedVirtualScreen.FreeReference;
if FPreviousVirtualScreen <> nil then
FPreviousVirtualScreen.FreeReference;
FInternalVirtualScreen.Free;
FRestoreImage.Free;
FBackgroundImage.Free;
FInternalVirtualScreen := nil;
FStretchedVirtualScreen := nil;
FRestoreImage := nil;
FBackgroundImage := nil;
FPreviousVirtualScreen := nil;
FPreviousDisposeMode := dmNone;
end;
procedure TBGRAAnimatedGif.SaveBackgroundOnce(Canvas: TCanvas; ARect: TRect);
begin
if (FBackgroundImage <> nil) and
((FBackgroundImage.Width <> ARect.Right - ARect.Left) or
(FBackgroundImage.Height <> ARect.Bottom - ARect.Top)) then
FreeAndNil(FBackgroundImage);
if (BackgroundMode in [gbmSaveBackgroundOnce, gbmUpdateBackgroundContinuously]) and
(FBackgroundImage = nil) then
begin
FBackgroundImage := TBGRABitmap.Create(ARect.Right - ARect.Left,
ARect.Bottom - ARect.Top);
FBackgroundImage.GetImageFromCanvas(Canvas, ARect.Left, ARect.Top);
end;
end;
procedure TBGRAAnimatedGif.SetCurrentImage(Index: integer);
begin
if (Index >= 0) and (Index < Length(FImages)) then
FWantedImage := Index;
end;
procedure TBGRAAnimatedGif.Clear;
var
i: integer;
begin
inherited Clear;
for i := 0 to Count - 1 do
FImages[i].Image.FreeReference;
FImages := nil;
LoopDone := 0;
LoopCount := 0;
end;
destructor TBGRAAnimatedGif.Destroy;
begin
Clear;
if FStretchedVirtualScreen <> nil then
FStretchedVirtualScreen.FreeReference;
if FPreviousVirtualScreen <> nil then
FPreviousVirtualScreen.FreeReference;
FInternalVirtualScreen.Free;
FRestoreImage.Free;
FBackgroundImage.Free;
inherited Destroy;
end;
procedure TBGRAAnimatedGif.Pause;
begin
FPaused := True;
end;
procedure TBGRAAnimatedGif.Resume;
begin
FPaused := False;
end;
procedure TBGRAAnimatedGif.Show(Canvas: TCanvas; ARect: TRect);
begin
Canvas.StretchDraw(ARect, self);
end;
procedure TBGRAAnimatedGif.Update(Canvas: TCanvas; ARect: TRect);
var
n: integer;
PChangePix, PNewPix, PBackground, PNewBackground: {$IFDEF BDS}PBGRAPixel;{$ELSE}PBGRALongWord;{$ENDIF}
oldpix, newpix, newbackpix: BGRALongWord;
NewBackgroundImage: TBGRABitmap;
{$IFDEF BDS}Temp : TBGRAPixel;{$ENDIF}
begin
if (BackgroundMode = gbmUpdateBackgroundContinuously) and
(FBackgroundImage = nil) then
BackgroundMode := gbmSaveBackgroundOnce;
SaveBackgroundOnce(Canvas, ARect);
case BackgroundMode of
gbmSimplePaint:
begin
UpdateSimple(Canvas, ARect);
exit;
end;
gbmEraseBackground:
begin
UpdateEraseBackground(Canvas, ARect);
exit;
end;
gbmSaveBackgroundOnce, gbmUpdateBackgroundContinuously:
begin
if FPreviousVirtualScreen <> nil then
begin
if (FPreviousVirtualScreen.Width <> ARect.Right - ARect.Left) or
(FPreviousVirtualScreen.Height <> ARect.Bottom - ARect.Top) then
begin
FPreviousVirtualScreen.FreeReference;
FPreviousVirtualScreen := nil;
end
else
FPreviousVirtualScreen := TBGRABitmap(FPreviousVirtualScreen.GetUnique);
end;
Render(ARect.Right - ARect.Left, ARect.Bottom - ARect.Top);
if FImageChanged then
begin
if BackgroundMode = gbmUpdateBackgroundContinuously then
begin
NewBackgroundImage :=
TBGRABitmap.Create(FStretchedVirtualScreen.Width,
FStretchedVirtualScreen.Height);
NewBackgroundImage.GetImageFromCanvas(Canvas, ARect.Left, ARect.Top);
if FPreviousVirtualScreen = nil then
begin
FPreviousVirtualScreen := TBGRABitmap.Create(FWidth, FHeight);
FPreviousVirtualScreen.Fill(BGRAPixelTransparent);
end;
PChangePix := {$IFDEF BDS}FPreviousVirtualScreen.Data;{$ELSE}PBGRALongWord(FPreviousVirtualScreen.Data);{$ENDIF}
PNewPix := {$IFDEF BDS}FStretchedVirtualScreen.Data;{$ELSE}PBGRALongWord(FStretchedVirtualScreen.Data);{$ENDIF}
PBackground := {$IFDEF BDS}FBackgroundImage.Data;{$ELSE}PBGRALongWord(FBackgroundImage.Data);{$ENDIF}
PNewBackground := {$IFDEF BDS}NewBackgroundImage.Data;{$ELSE}PBGRALongWord(NewBackgroundImage.Data);{$ENDIF}
for n := FStretchedVirtualScreen.NbPixels - 1 downto 0 do
begin
{$IFDEF BDS}move(PChangePix^, oldpix, sizeof(BGRADWord));{$ELSE}oldpix := PChangePix^;{$ENDIF}
if (oldpix and AlphaMask = AlphaMask) then //pixel opaque précédent
begin
{$IFDEF BDS}move(PNewBackground^, newbackpix, sizeof(BGRADWord));{$ELSE}newbackpix := PNewBackground^;{$ENDIF} //pixel opaque
if (newbackpix <> oldpix) then //stocke nouveau fond
{$IFDEF BDS}move(newbackpix, PBackground^, sizeof(BGRADWord));{$ELSE}PBackground^ := newbackpix;{$ENDIF} //pixel opaque
end;
{$IFDEF BDS}move(PNewPix^, newpix, sizeof(BGRADWord));{$ELSE}newpix := PNewPix^;{$ENDIF}
if newpix and AlphaMask = AlphaMask then
{$IFDEF BDS}move(newpix, PChangePix^, sizeof(BGRADWord)){$ELSE}PChangePix^ := newpix{$ENDIF} //pixel opaque
else if newpix and AlphaMask > 0 then
begin
{$IFDEF BDS}
move(PBackground^ , PChangePix^, SizeOf(BGRADWord));
move(newpix , temp, SizeOf(BGRADWord));
DrawPixelInlineNoAlphaCheck(PChangePix, temp);
{$ELSE}
PChangePix^ := PBackground^;
DrawPixelInlineNoAlphaCheck(PBGRAPixel(PChangePix), PBGRAPixel(@newpix)^);
{$ENDIF}
end
else if {$IFDEF BDS}BGRADWord(PChangePix^){$ELSE}PChangePix^{$ENDIF} and AlphaMask <> 0 then
{$IFDEF BDS}move(PBackground^, PChangePix^, sizeof(BGRADWord));{$ELSE}PChangePix^ := PBackground^;{$ENDIF} //efface précédent
{ if newpix and AlphaMask > AlphaLimit then PChangePix^ := newpix or AlphaMask //pixel opaque
else if PChangePix^ and AlphaMask <> 0 then PChangePix^ := PBackground^; //efface précédent}
Inc(PNewPix);
Inc(PChangePix);
Inc(PBackground);
Inc(PNewBackground);
end;
NewBackgroundImage.Free;
FPreviousVirtualScreen.InvalidateBitmap;
FPreviousVirtualScreen.Draw(Canvas, ARect.Left, ARect.Top, false);
FPreviousVirtualScreen.PutImage(0, 0, FStretchedVirtualScreen, dmSet);
end
else
begin
if FPreviousVirtualScreen = nil then
begin
FStretchedVirtualScreen.Draw(Canvas, ARect.Left, ARect.Top, false);
FPreviousVirtualScreen :=
TBGRABitmap(FStretchedVirtualScreen.NewReference);
end
else
begin
PChangePix := {$IFDEF BDS}FPreviousVirtualScreen.Data;{$ELSE}PBGRALongWord(FPreviousVirtualScreen.Data);{$ENDIF}
PNewPix := {$IFDEF BDS}FStretchedVirtualScreen.Data;{$ELSE}PBGRALongWord(FStretchedVirtualScreen.Data);{$ENDIF}
PBackground := {$IFDEF BDS}FBackgroundImage.Data;{$ELSE}PBGRALongWord(FBackgroundImage.Data);{$ENDIF}
for n := FStretchedVirtualScreen.NbPixels - 1 downto 0 do
begin
{$IFDEF BDS}move(PNewPix^, newpix, sizeof(BGRADWord));{$ELSE}newpix := PNewPix^;{$ENDIF}
if newpix and AlphaMask = AlphaMask then
{$IFDEF BDS}move(newpix, PChangePix^, sizeof(BGRADWord)){$ELSE}PChangePix^ := newpix{$ENDIF} //pixel opaque
else if newpix and AlphaMask > 0 then
begin
{$IFDEF BDS}
move(PBackground^ , PChangePix^, SizeOf(BGRADWord));
move(newpix , temp, SizeOf(BGRADWord));
DrawPixelInlineNoAlphaCheck(PChangePix, temp);
{$ELSE}
PChangePix^ := PBackground^;
DrawPixelInlineNoAlphaCheck(PBGRAPixel(PChangePix), PBGRAPixel(@newpix)^);
{$ENDIF}
end