-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vcl.Graphics.MultipleWICImage.pas
301 lines (261 loc) · 6.78 KB
/
Vcl.Graphics.MultipleWICImage.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
// 只是簡單的 TWICImage 清單類別,暫時沒有打算做成像 TGifImage 那樣,
unit Vcl.Graphics.MultipleWICImage;
interface
uses
System.RTLConsts, System.SysUtils, System.Classes, System.Generics.Collections,
Winapi.Windows, Winapi.ActiveX, Winapi.Wincodec,
Vcl.Graphics;
type
TDataSourceType = (_DST_Non, _DST_Stream, _DST_File);
TDataSource = record
DataType: TDataSourceType;
Stream: TStream;
Filename: string;
end;
TWICImageEx = class(TWICImage)
private
// FKeepSource: Boolean;
FSource: TDataSource;
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream); override;
procedure LoadFromFile(const Filename: string); override;
procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle; APalette: HPALETTE); override;
procedure ReloadFromSource;
end;
TWICImages = class(TObjectList<TWICImageEx>)
private
FFrameCount: LongWord;
FFrameIndex: LongWord;
FSourceStream: TMemoryStream;
FSource: TDataSource;
FCurrent: TWICImage;
procedure ExceptionImageFailed;
// procedure SetBuffer(WICImage: TWICImage);
function GetCurrent: TWICImage;
function GetCurrentImageSize: TSize;
procedure SetFrameIndex(NewIndex: LongWord);
public
constructor Create; reintroduce;
destructor Destroy; override;
procedure LoadFromStream(Stream: TStream);
procedure LoadFromFile(const Filename: string);
function Load(Index: LongWord): TWICImageEx;
procedure Prev;
procedure Next;
function ReadImage(Index: LongWord): TWICImageEx;
procedure ReadImages(Renew: Boolean = True);
property FrameIndex: LongWord read FFrameIndex write SetFrameIndex;
property Current: TWICImage read GetCurrent;
property CurrentImageSize: TSize read GetCurrentImageSize;
end;
implementation
resourcestring
ErrFmtImageFailed = 'Unable to extract image at index %d from %s';
{ TMWICImageEx }
procedure TWICImageEx.Assign(Source: TPersistent);
begin
if not (Source is TWICImageEx) then
begin
inherited;
Exit;
end;
end;
procedure TWICImageEx.LoadFromStream(Stream: TStream);
begin
inherited;
FSource.Stream := Stream;
FSource.Filename := '';
FSource.DataType := _DST_Stream;
end;
procedure TWICImageEx.LoadFromFile(const Filename: string);
begin
inherited;
FSource.Stream := nil;
FSource.Filename := Filename;
FSource.DataType := _DST_File;
end;
procedure TWICImageEx.LoadFromClipboardFormat(AFormat: Word; AData: THandle; APalette: HPALETTE);
begin
inherited;
FSource.Stream := nil;
FSource.Filename := '';
FSource.DataType := _DST_Non;
end;
procedure TWICImageEx.ReloadFromSource;
begin
case FSource.DataType of
_DST_Stream: LoadFromStream(FSource.Stream);
_DST_File: LoadFromFile(FSource.Filename);
end;
end;
{ TWICImages }
constructor TWICImages.Create;
begin
inherited;
FFrameCount := 0;
FFrameIndex := 0;
end;
destructor TWICImages.Destroy;
begin
if Assigned(FSourceStream) then
FSourceStream.Free;
inherited;
end;
procedure TWICImages.ExceptionImageFailed;
var
s: string;
begin
if FSource.Filename.IsEmpty then
if FSource.Stream is TFileStream then
s := 'file: ' + sLineBreak + TFileStream(FSource.Stream).FileName
else
s := 'stream.'
else
s := 'file: ' + sLineBreak + FSource.Filename;
raise Exception.CreateFmt(ErrFmtImageFailed, [FFrameIndex, s]) at ReturnAddress;
end;
procedure TWICImages.LoadFromStream(Stream: TStream);
begin
Self.Clear;
FSource.Stream := Stream;
FSource.Filename := '';
FSource.DataType := _DST_Stream;
if Assigned(FSourceStream) then
FSourceStream.Clear
else
FSourceStream := TMemoryStream.Create;
FSourceStream.LoadFromStream(Stream);
end;
procedure TWICImages.LoadFromFile(const Filename: string);
begin
Self.Clear;
FSource.Stream := nil;
FSource.Filename := Filename;
FSource.DataType := _DST_File;
if Assigned(FSourceStream) then
FSourceStream.Clear
else
FSourceStream := TMemoryStream.Create;
FSourceStream.LoadFromFile(Filename);
end;
function TWICImages.Load(Index: LongWord): TWICImageEx;
begin
if not Assigned(FSourceStream) then
Exit(nil);
Result := TWICImageEx.Create;
Result.FrameIndex := Index;
Result.LoadFromStream(FSourceStream);
end;
//procedure TWICImages.SetBuffer(WICImage: TWICImage);
//begin
// FBuffer := WICImage;
//end;
function TWICImages.GetCurrent: TWICImage;
begin
if Assigned(FCurrent) then
begin
Result := FCurrent;
end
else
begin
if FFrameIndex < LongWord(Self.Count) then
begin
Result := Self.Items[FFrameIndex];
if not Assigned(Result) then
Result := ReadImage(FFrameIndex);
end
else
begin
Result := ReadImage(FFrameIndex);
end;
FCurrent := Result;
end;
end;
function TWICImages.GetCurrentImageSize: TSize;
var
Image: TWICImage;
begin
Image := GetCurrent;
if not Assigned(Image) then
ExceptionImageFailed;
Result := TSize.Create(Image.Width, Image.Height);
end;
procedure TWICImages.SetFrameIndex(NewIndex: LongWord);
begin
if NewIndex = FFrameIndex then
Exit;
FCurrent := nil;
FFrameIndex := NewIndex;
end;
function TWICImages.ReadImage(Index: LongWord): TWICImageEx;
begin
if Self.Count = 0 then
begin
Result := Load(0);
FFrameCount := Result.FrameCount;
Self.Count := FFrameCount;
Self.Items[0] := Result;
end
else
begin
Result := Self.Items[0];
end;
if Index = 0 then
Exit;
if Index >= FFrameCount then
Exit(nil);
Result := Self.Items[Index];
if Assigned(Result) then
Exit;
Result := Load(Index);
Self.Items[Index] := Result;
end;
procedure TWICImages.ReadImages(Renew: Boolean);
var
WIC: TWICImageEx;
I: Integer;
begin
if Renew then
begin
Self.Clear;
I := 0;
repeat
WIC := Load(I);
Self.Add(WIC);
Inc(I);
until (LongWord(I) >= WIC.FrameCount);
end
else
begin
I := 0;
repeat
WIC := Self.Items[I];
if not Assigned(WIC) then
begin
WIC := Load(I);
Self.Items[I] := WIC;
end;
Inc(I);
until (LongWord(I) >= WIC.FrameCount);
end;
FFrameCount := WIC.FrameCount;
end;
procedure TWICImages.Next;
begin
Inc(FFrameIndex);
if FFrameIndex >= FFrameCount then
FFrameIndex := 0;
if Assigned(FCurrent) then
FCurrent := ReadImage(FFrameIndex);
end;
procedure TWICImages.Prev;
begin
if FFrameIndex > 0 then
Dec(FFrameIndex)
else
FFrameIndex := FFrameCount - 1;
if Assigned(FCurrent) then
FCurrent := ReadImage(FFrameIndex);
end;
end.