-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTGifAnimate.pas
305 lines (276 loc) · 10.9 KB
/
FTGifAnimate.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
unit FTGifAnimate;
(******************************************************************************
Unit to make an animated GIF.
Author: Finn Tolderlund
Denmark
Date: 14.07.2003
homepage:
http://www.tolderlund.eu/
e-mail:
This unit requires the GIFImage.pas unit from Anders Melander.
The GIFImage.pas unit can be obtained from my homepage above.
This unit can freely be used and distributed.
Disclaimer:
Use of this unit is on your own responsibility.
I will not under any circumstance be held responsible for anything
which may or may not happen as a result of using this unit.
******************************************************************************
History:
19.07.2003 Added GifAnimateEndGif function.
24.07.2003 Added link to an example Delphi project at Earl F. Glynn's website.
02.09.2003 Renamed function GifAnimateEnd to GifAnimateEndPicture.
Added overloaded function GifAnimateAddImage where you can specify
a specific TransparentColor.
22.08.2008 Added new overloaded function GifAnimateAddImage with Loops and Disposal parameter.
23.08.2008 Added new overloaded function GifAnimateAddImage with TGIFAppExtNSLoop and TGIFGraphicControlExtension parameter.
25.09.2008 Added new overloaded function GifAnimateBegin with Width and Height parameter.
19.10.2008 Posted this new version on my web site.
******************************************************************************)
(******************************************************************************
Example of use:
procedure TFormSphereMovie.MakeGifButtonClick(Sender: TObject);
// BitMapArray is an array of TBitmap.
var
FrameIndex: Integer;
Picture: TPicture;
begin
Screen.Cursor := crHourGlass;
try
GifAnimateBegin;
{Step through each frame in in-memory list}
for FrameIndex := Low(BitMapArray) to High(BitMapArray) do
begin
// add frame to animated gif
GifAnimateAddImage(BitMapArray[FrameIndex], False, MillisecondsPerFrame);
end;
// We are using a TPicture but we could have used a TGIFImage instead.
// By not using TGIFImage directly we do not have to add GIFImage to the uses clause.
// By using TPicture we only need to add GifAnimate to the uses clause.
Picture := GifAnimateEndPicture;
Picture.SaveToFile(ExtractFilePath(ParamStr(0)) + 'sphere.gif'); // save gif
ImageMovieFrame.Picture.Assign(Picture); // display gif
Picture.Free;
finally
Screen.Cursor := crDefault;
end;
end;
******************************************************************************)
(******************************************************************************
For a complete Delphi project with source, goto one of these pages:
http://homepages.borland.com/efg2lab/Graphics/SphereInCubeMovie.htm
http://www.efg2.com/Lab/Graphics/SphereInCubeMovie.htm
******************************************************************************)
interface
uses
Windows, SysUtils, Graphics, GIFImage;
procedure GifAnimateBegin; overload;
procedure GifAnimateBegin(Width, Height: Integer); overload;
function GifAnimateEndPicture: TPicture;
function GifAnimateEndGif: TGIFImage;
function GifAnimateAddImage(Source: TGraphic; Transparent: Boolean; DelayMS: Word): Integer; overload;
// Transparent=True uses lower left pixel as transparent color
function GifAnimateAddImage(Source: TGraphic; TransparentColor: TColor; DelayMS: Word; Loop: Word): Integer; overload;
// TransparentColor<>-1 uses that color as the transparent.
// Note: There is no guaranteee that the color will actually be in the GIF's color palette.
function GifAnimateAddImage(Source: TGraphic; pLeft, pTop: Word; NSLoop, GCExt: Boolean; Loops: Word; TransparentColor: TColor; DelayMS: Word; Disposal: TDisposalMethod): Integer; overload;
// TransparentColor<>-1 uses that color as the transparent.
// Note: There is no guaranteee that the color will actually be in the GIF's color palette.
function GifAnimateAddImage(pSource: TGraphic; pLeft, pTop: Word; TransparentColor: TColor; pLoopExt: TGIFAppExtNSLoop; pGCExt: TGIFGraphicControlExtension): Integer; overload;
function GetColorIndex(GIF: TGIFSubImage; Color: TColor): Integer;
implementation
var
GIF: TGIFImage;
function TransparentIndex(GIF: TGIFSubImage): byte;
begin
// Use the lower left pixel as the transparent color
Result := GIF.Pixels[0, GIF.Height-1];
end;
function GifAnimateAddImage(Source: TGraphic; Transparent: Boolean; DelayMS: Word): Integer;
var
Ext : TGIFGraphicControlExtension;
LoopExt: TGIFAppExtNSLoop;
begin
// Add the source image to the animation
Result := GIF.Add(Source);
// Netscape Loop extension must be the first extension in the first frame!
if (Result = 0) then
begin
LoopExt := TGIFAppExtNSLoop.Create(GIF.Images[Result]);
LoopExt.Loops := 0; // Number of loops (0 = forever)
GIF.Images[Result].Extensions.Add(LoopExt);
end;
// Add Graphic Control Extension
Ext := TGIFGraphicControlExtension.Create(GIF.Images[Result]);
Ext.Delay := DelayMS div 10; // 30; // Animation delay (30 = 300 mS)
// if (Result > 0) then
if (Transparent) then
begin
Ext.Transparent := True;
Ext.TransparentColorIndex := TransparentIndex(GIF.Images[Result]);
end;
GIF.Images[Result].Extensions.Add(Ext);
end;
function GetColorIndex(GIF: TGIFSubImage; Color: TColor): Integer;
var
idx, x, y: Integer;
begin
// Find index for color in the colormap.
// The same color can be in the colormap more than once.
// Not all color indexes may be in use, so check if this index is being used.
// Return only an index which is actually being used in the image.
// If the index is not being used in the image,
// try to find the next index for the color in the colormap.
for idx := 0 to GIF.ActiveColorMap.Count - 1 do
if GIF.ActiveColorMap.Colors[idx] = Color then
begin
// Found an index, is it being used in the image?
for y := 0 to GIF.Height-1 do
for x := 0 to GIF.Width-1 do
if GIF.Pixels[x, y] = idx then
begin
Result := idx; // Index is used in image.
Exit;
end;
// Index not used in the image, try next index.
end;
Result := -1; // didn't find index for the color
end;
function GifAnimateAddImage(Source: TGraphic; TransparentColor: TColor; DelayMS: Word; Loop: Word): Integer;
var
Ext : TGIFGraphicControlExtension;
LoopExt: TGIFAppExtNSLoop;
idx: Integer;
begin
// Add the source image to the animation
Result := GIF.Add(Source);
// Netscape Loop extension must be the first extension in the first frame!
if (Result = 0) then
begin
LoopExt := TGIFAppExtNSLoop.Create(GIF.Images[Result]);
LoopExt.Loops := Loop; // Number of loops (0 = forever)
GIF.Images[Result].Extensions.Add(LoopExt);
end;
// Add Graphic Control Extension
Ext := TGIFGraphicControlExtension.Create(GIF.Images[Result]);
Ext.Delay := DelayMS div 10; // 30; // Animation delay (30 = 300 mS)
if TransparentColor <> -1 then
begin
idx := GetColorIndex(GIF.Images[Result], TransparentColor);
if idx in [0..255] then
begin
Ext.Transparent := True;
Ext.TransparentColorIndex := idx;
end;
end;
GIF.Images[Result].Extensions.Add(Ext);
end;
function GifAnimateAddImage(Source: TGraphic; pLeft, pTop: Word; NSLoop, GCExt: Boolean; Loops: Word; TransparentColor: TColor; DelayMS: Word; Disposal: TDisposalMethod): Integer;
var
Ext : TGIFGraphicControlExtension;
LoopExt: TGIFAppExtNSLoop;
idx: Integer;
begin
// Add the source image to the animation
Result := GIF.Add(Source);
GIF.Images[Result].Left := pLeft;
GIF.Images[Result].Top := pTop;
// Netscape Loop extension must be the first extension in the first frame!
if {(Result = 0) and} NSLoop then
begin
LoopExt := TGIFAppExtNSLoop.Create(GIF.Images[Result]);
LoopExt.Loops := Loops; // Number of loops (0 = forever)
GIF.Images[Result].Extensions.Add(LoopExt);
end;
// Add Graphic Control Extension
if GCExt then
begin
Ext := TGIFGraphicControlExtension.Create(GIF.Images[Result]);
Ext.Delay := DelayMS div 10; // 30; // Animation delay (30 = 300 mS)
Ext.Disposal := Disposal;
if TransparentColor <> -1 then
begin
idx := GetColorIndex(GIF.Images[Result], TransparentColor);
if idx in [0..255] then
begin
Ext.Transparent := True;
Ext.TransparentColorIndex := idx;
end;
end;
GIF.Images[Result].Extensions.Add(Ext);
end;
end;
function GifAnimateAddImage(pSource: TGraphic; pLeft, pTop: Word; TransparentColor: TColor; pLoopExt: TGIFAppExtNSLoop; pGCExt: TGIFGraphicControlExtension): Integer;
var
GCExt: TGIFGraphicControlExtension;
LoopExt: TGIFAppExtNSLoop;
idx: Integer;
begin
// Add the source image to the animation
Result := GIF.Add(pSource);
GIF.Images[Result].Left := pLeft;
GIF.Images[Result].Top := pTop;
// Netscape Loop extension must be the first extension in the first frame!
if {(Result = 0) and} Assigned(pLoopExt) then
begin
LoopExt := TGIFAppExtNSLoop.Create(GIF.Images[Result]);
LoopExt.Loops := pLoopExt.Loops; // Number of loops (0 = forever)
GIF.Images[Result].Extensions.Add(LoopExt);
end;
// Add Graphic Control Extension
if Assigned(pGCExt) then
begin
GCExt := TGIFGraphicControlExtension.Create(GIF.Images[Result]);
GCExt.Delay := pGCExt.Delay;
GCExt.Disposal := pGCExt.Disposal;
if pGCExt.Transparent or (TransparentColor <> -1) then
begin
idx := GetColorIndex(GIF.Images[Result], TransparentColor);
if idx in [0..255] then
begin
GCExt.Transparent := True;
GCExt.TransparentColorIndex := idx;
end;
end;
GIF.Images[Result].Extensions.Add(GCExt);
end;
end;
procedure GifAnimateBegin;
begin
GIF.Free;
GIF := TGIFImage.Create;
GIF.ColorReduction := rmNone; // rmQuantize rmNone
// GIF.DitherMode := dmNearest; // no dither, use nearest color in palette
GIF.DitherMode := dmNearest; // dmNearest dmFloydSteinberg
GIF.Compression := gcLZW;
end;
procedure GifAnimateBegin(Width, Height: Integer);
begin
GIF.Free;
GIF := TGIFImage.Create;
GIF.Width := Width;
GIF.Height := Height;
GIF.ColorReduction := rmNone; // rmQuantize rmNone
// GIF.DitherMode := dmNearest; // no dither, use nearest color in palette
GIF.DitherMode := dmNearest; // dmNearest dmFloydSteinberg
GIF.Compression := gcLZW;
end;
function GifAnimateEndPicture: TPicture;
begin
Result := TPicture.Create;
Result.Assign(GIF);
GIF.Free;
GIF := nil;
end;
function GifAnimateEndGif: TGIFImage;
begin
Result := TGIFImage.Create;
Result.Assign(GIF);
GIF.Free;
GIF := nil;
end;
initialization
GIF := nil;
finalization
GIF.Free;
end.