-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGFIEFormat.pas
More file actions
467 lines (383 loc) · 13.6 KB
/
GFIEFormat.pas
File metadata and controls
467 lines (383 loc) · 13.6 KB
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
(*
Greenfish Icon Editor Pro
Copyright (c) 2012-13 B. Szalkai
This program 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 3 of the License, or
(at your option) any later version.
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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
// GFIE native document format reading and writing
unit GFIEFormat;
interface
uses
LclIntf, LclType,
SysUtils, Classes, Graphics, DocClass;
function gfieDetect(Data: Pointer; Size: integer): boolean;
function gfieLoadFromStream(Doc: TIconDoc; s: TStream;
MaxWidth, MaxHeight: integer): boolean;
procedure gfieSaveToStream(Doc: TIconDoc; s: TStream; compressed: boolean);
implementation
uses
Math, StrUtils, BitmapEx, bmExUtils, Layers, BlendModes, gfDataTree, BMP, PNG, ieShared;
// Converting enums to string
const
SelStateToStr: array[TSelState] of string =
('none', 'selecting', 'floating');
function StrToSelState(s: string): TSelState;
var
i: TSelState;
begin
s := UpperCase(s);
for i in TSelState do
if s = UpperCase(SelStateToStr[i]) then Exit(i);
Result := stNone;
end;
const
BlendModeToStr: array[TBlendMode] of string =
('normal', 'mask', 'behind', 'dissolve',
'hue', 'hueShift', 'saturation',
'darken', 'multiply', 'colorBurn', 'linearBurn', 'darkerColor',
'lighten', 'screen', 'colorDodge', 'linearDodge', 'lighterColor',
'overlay', 'softLight', 'hardLight',
'vividLight', 'linearLight', 'pinLight', 'hardMix',
'difference', 'exclusion');
function StrToBlendMode(s: string): TBlendMode;
var
i: TBlendMode;
begin
s := UpperCase(s);
for i in TBlendMode do
if s = UpperCase(BlendModeToStr[i]) then Exit(i);
Result := bmNormal;
end;
function gfieDetect;
begin
Result := (Size >= 4) and (PCardinal(Data)^ = GF_DATA_TREE_SIG);
end;
function gfieLoadFromStream;
var
i, j: integer;
p: TPoint;
t: TgfDataTree;
pg: TDocPage;
ls: TLayers;
l: TLayer;
function ReadPoint: TPoint;
begin
if t.Descend('x') then begin Result.X := t.Node.AsInt; t.Ascend; end else Result.X := 0;
if t.Descend('y') then begin Result.Y := t.Node.AsInt; t.Ascend; end else Result.Y := 0;
end;
function ReadRect: TRect;
begin
if t.Descend('left') then begin Result.Left := t.Node.AsInt; t.Ascend; end else Result.Left := 0;
if t.Descend('top') then begin Result.Top := t.Node.AsInt; t.Ascend; end else Result.Top := 0;
if t.Descend('right') then begin Result.Right := t.Node.AsInt; t.Ascend; end else Result.Right := 0;
if t.Descend('bottom') then begin Result.Bottom := t.Node.AsInt; t.Ascend; end else Result.Bottom := 0;
end;
procedure ReadRawImage(bm: TBitmap32; const fmt: string);
var
sMem: TMemoryStream;
dpi: double;
begin
sMem := TMemoryStream.Create;
try
sMem.Size := t.Node.DataSize;
Move(t.Node.Data^, sMem.Memory^, t.Node.DataSize);
if fmt = 'BMP' then
bmpLoadFromStream(bm, sMem) else
if fmt = 'PNG' then
pngLoadFromStream(bm, sMem, dpi);
bm.Resize(Min(bm.Width, MaxWidth), Min(bm.Height, MaxHeight));
finally
sMem.Free;
end;
end;
procedure ReadBitmap32(bm: TBitmap32);
var
i: integer;
pSrc, pDest: PColor32;
fmt: string;
bmTemp: TBitmap32;
begin
// get image format
if not t.Descend('format') then Exit;
fmt := UpperCase(t.Node.AsString);
t.Ascend;
if (fmt <> 'BMP') and (fmt <> 'PNG') then Exit;
// load image
if not t.Descend('data') then Exit;
ReadRawImage(bm, fmt);
t.Ascend;
// load inversion mask
if t.Descend('inversionMask') then
begin
bmTemp := TBitmap32.Create;
try
ReadRawImage(bmTemp, fmt);
t.Ascend;
pSrc := bmTemp.Data;
pDest := bm.Data;
for i := 1 to bm.Width * bm.Height do
begin
if pSrc^ = cl32White then pDest^ := cl32Inverted;
inc(pSrc); inc(pDest);
end;
finally
bmTemp.Free;
end;
end; // inversion mask
end;
procedure ReadBitmap1(bm: TBitmap1);
var
bm32: TBitmap32;
begin
bm32 := TBitmap32.Create;
try
ReadBitmap32(bm32);
bm.Assign(bm32);
finally
bm32.Free;
end;
end;
begin
Result := False;
try
t := TgfDataTree.Create;
try
// load data file
if not t.LoadFromStream(s) then Exit;
// clear the document
Doc.Clear;
// load metadata
if t.Descend('metadata') then
with Doc.Metadata do
begin
if t.Descend('title') then begin Title := t.Node.AsString; t.Ascend; end;
if t.Descend('author') then begin Author := t.Node.AsString; t.Ascend; end;
if t.Descend('copyright') then begin Copyright := t.Node.AsString; t.Ascend; end;
if t.Descend('comments') then begin Comments := t.Node.AsString; t.Ascend; end;
if t.Descend('loopCount') then begin LoopCount := t.Node.AsInt; t.Ascend; end;
if t.Descend('dpi') then begin DPI := t.Node.AsDouble; t.Ascend; end;
t.Ascend;
end;
// load pages
if t.Descend('pages') then
begin
for i := 0 to High(i) do
begin
if not t.Descend('page' + IntToStr(i)) then Break;
pg := Doc.NewPage;
// load layers
if t.Descend('layers') then
begin
ls := pg.Layers;
// load dimensions
if t.Descend('size') then begin p := ReadPoint; ls.Resize(Min(MaxWidth, p.X), Min(MaxHeight, p.Y)); t.Ascend; end;
// load layer objects
for j := 0 to High(j) do
begin
if not t.Descend('layer' + IntToStr(j)) then Break;
l := ls.NewLayer;
if t.Descend('name') then begin l.Name := t.Node.AsString; t.Ascend; end;
if t.Descend('visible') then begin l.Visible := t.Node.AsBoolean; t.Ascend; end;
if t.Descend('selected') then begin l.Selected := t.Node.AsBoolean; t.Ascend; end;
if t.Descend('image') then begin ReadBitmap32(l.Image); t.Ascend; end;
if t.Descend('opacity') then begin l.Opacity := t.Node.AsInt; t.Ascend; end;
if t.Descend('blendMode') then begin l.BlendMode := StrToBlendMode(t.Node.AsString); t.Ascend; end;
t.Ascend;
end; // if descend to layerX
// load selection
if t.Descend('selection') then
begin
if t.Descend('state') then begin ls.SelState := StrToSelState(t.Node.AsString); t.Ascend; end;
case ls.SelState of
stSelecting: if t.Descend('mask') then
begin
ReadBitmap1(ls.Selection.Mask);
t.Ascend;
end;
stFloating: begin
if t.Descend('image') then begin ReadBitmap32(ls.Selection.Image); t.Ascend; end;
if t.Descend('box') then begin ls.Selection.Box := ReadRect; t.Ascend; end;
if t.Descend('angle') then begin ls.Selection.Angle := t.Node.AsDouble; t.Ascend; end;
if t.Descend('depth') then begin ls.Selection.Depth := t.Node.AsInt; t.Ascend; end;
end;
end; // case selection state
t.Ascend;
end; // if descend to selection
t.Ascend;
end; // if descend to layers
// load etc.
if t.Descend('hotSpot') then begin pg.HotSpot := ReadPoint; t.Ascend; end;
if t.Descend('frameRate') then begin pg.FrameRate := t.Node.AsInt; t.Ascend; end;
if t.Descend('dpi') then begin pg.DPI := t.Node.AsDouble; t.Ascend; end;
t.Ascend;
end; // if descend to pageX
t.Ascend;
end; // if descend to pages
finally
t.Free;
end;
// success
Result := True;
except
end;
end;
procedure gfieSaveToStream;
var
i, j: integer;
t: TgfDataTree;
pg: TDocPage;
ls: TLayers;
l: TLayer;
procedure WritePoint(const Value: TPoint);
begin
t.NewChild('x'); t.Node.AsInt := Value.X; t.Ascend;
t.NewChild('y'); t.Node.AsInt := Value.Y; t.Ascend;
end;
procedure WriteRect(const Value: TRect);
begin
t.NewChild('left'); t.Node.AsInt := Value.Left; t.Ascend;
t.NewChild('top'); t.Node.AsInt := Value.Top; t.Ascend;
t.NewChild('right'); t.Node.AsInt := Value.Right; t.Ascend;
t.NewChild('bottom'); t.Node.AsInt := Value.Bottom; t.Ascend;
end;
procedure WriteRawImage(bm: TBitmap32);
var
sMem: TMemoryStream;
begin
sMem := TMemoryStream.Create;
try
if compressed then
pngSaveToStream(bm, sMem, PNG_COMPRESSION_HIGH, 0.0)
else
bmpSaveToStream(bm, sMem);
t.Node.DataSize := sMem.Size;
Move(sMem.Memory^, t.Node.Data^, sMem.Size);
finally
sMem.Free;
end;
end;
procedure WriteBitmap32(bm: TBitmap32);
var
i: integer;
pSrc, pDest: PColor32;
bmTemp: TBitmap32;
begin
// write format
t.NewChild('format');
t.Node.AsString := StrUtils.IfThen(compressed, 'png', 'bmp');
t.Ascend;
// write image data
t.NewChild('data');
WriteRawImage(bm);
t.Ascend;
// write inversion mask
if bm.HasColor(cl32Inverted) then
begin
bmTemp := TBitmap32.Create;
try
bmTemp.Resize(bm.Width, bm.Height);
pSrc := bm.Data;
pDest := bmTemp.Data;
for i := 1 to bm.Width * bm.Height do
begin
if pSrc^ = cl32Inverted then pDest^ := cl32White;
inc(pSrc); inc(pDest);
end;
t.NewChild('inversionMask');
WriteRawImage(bmTemp);
t.Ascend;
finally
bmTemp.Free;
end;
end; // inversion mask
end;
procedure WriteBitmap1(bm: TBitmap1);
var
bm32: TBitmap32;
begin
bm32 := TBitmap32.Create;
try
bm32.Resize(bm.Width, bm.Height);
bm32.FillColor(cl32Black);
bm32.Draw(0, 0, bm);
WriteBitmap32(bm32);
finally
bm32.Free;
end;
end;
begin
t := TgfDataTree.Create;
try
// save metadata
t.NewChild('metadata');
with Doc.Metadata do
begin
if Title <> '' then begin t.NewChild('title'); t.Node.AsString := Title; t.Ascend; end;
if Author <> '' then begin t.NewChild('author'); t.Node.AsString := Author; t.Ascend; end;
if Copyright <> '' then begin t.NewChild('copyright'); t.Node.AsString := Copyright; t.Ascend; end;
if Comments <> '' then begin t.NewChild('comments'); t.Node.AsString := Comments; t.Ascend; end;
t.NewChild('loopCount'); t.Node.AsInt := LoopCount; t.Ascend;
if DPI > 0 then begin t.NewChild('dpi'); t.Node.AsDouble := DPI; t.Ascend; end;
end;
t.Ascend;
// save pages
t.NewChild('pages');
for i := 0 to Doc.PageCount - 1 do
begin
t.NewChild('page' + IntToStr(i));
pg := Doc.Pages[i];
// save layers
t.NewChild('layers');
ls := pg.Layers;
// save dimensions
t.NewChild('size'); WritePoint(Point(ls.Width, ls.Height)); t.Ascend;
// save layer objects
for j := 0 to ls.LayerCount - 1 do
begin
t.NewChild('layer' + IntToStr(j));
l := ls[j];
t.NewChild('name'); t.Node.AsString := l.Name; t.Ascend;
t.NewChild('visible'); t.Node.AsBoolean := l.Visible; t.Ascend;
t.NewChild('selected'); t.Node.AsBoolean := l.Selected; t.Ascend;
t.NewChild('image'); WriteBitmap32(l.Image); t.Ascend;
t.NewChild('opacity'); t.Node.AsInt := l.Opacity; t.Ascend;
t.NewChild('blendMode'); t.Node.AsString := BlendModeToStr[l.BlendMode]; t.Ascend;
t.Ascend; // layer
end;
// save selection
t.NewChild('selection');
t.NewChild('state'); t.Node.AsString := SelStateToStr[ls.SelState]; t.Ascend;
case ls.SelState of
stSelecting: begin
t.NewChild('mask'); WriteBitmap1(ls.Selection.Mask); t.Ascend;
end;
stFloating: begin
t.NewChild('image'); WriteBitmap32(ls.Selection.Image); t.Ascend;
t.NewChild('box'); WriteRect(ls.Selection.Box); t.Ascend;
t.NewChild('angle'); t.Node.AsDouble := ls.Selection.Angle; t.Ascend;
t.NewChild('depth'); t.Node.AsInt := ls.Selection.Depth; t.Ascend;
end;
end; // case selection state
t.Ascend; // selection
t.Ascend; // layers
t.NewChild('hotSpot'); WritePoint(pg.HotSpot); t.Ascend;
t.NewChild('frameRate'); t.Node.AsInt := pg.FrameRate; t.Ascend;
if pg.DPI > 0 then begin t.NewChild('dpi'); t.Node.AsDouble := pg.DPI; t.Ascend; end;
t.Ascend; // pageX
end; // for i
t.Ascend; // pages
// save data file
t.SaveToStream(s);
finally
t.Free;
end;
end;
end.