-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathICO.pas
More file actions
285 lines (237 loc) · 6.83 KB
/
ICO.pas
File metadata and controls
285 lines (237 loc) · 6.83 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
(*
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/>.
*)
unit ICO;
interface
uses
LclIntf, LclType, SysUtils, Classes, BitmapEx, Layers, DocClass;
const
// These constants denote in an ICO/CUR file whether the file is an
// icon or a cursor file
ID_ICO = 1;
ID_CUR = 2;
type
PICONDIR = ^ICONDIR;
ICONDIR = packed record
idReserved: word;
idType: word;
idCount: word;
end;
PICONDIRENTRY = ^ICONDIRENTRY;
ICONDIRENTRY = packed record
bWidth: byte;
bHeight: byte;
bColorCount: byte;
bReserved: byte;
wPlanes_XHotSpot: word; // [icon]_[cursor]
wBitCount_YHotSpot: word; // [icon]_[cursor]
dwBytesInRes: Cardinal;
dwImageOffset: Cardinal;
end;
function icoDetect(Data: Pointer; Size: integer; IsCursor: boolean): boolean;
function icoLoadFromStream(Doc: TIconDoc; s: TStream): boolean;
procedure icoSaveToStream(Doc: TIconDoc; s: TStream;
AsCursor: boolean; PNGLimit: integer);
function icoLoadFromFile(Doc: TIconDoc; const fn: string): boolean;
procedure icoSaveToFile(Doc: TIconDoc; const fn: string;
AsCursor: boolean; PNGLimit: integer);
implementation
uses
Math, PixelFormats, BMP, PNG;
// Leaves the stream position unchanged
function icoLoadPageFromStream(bm: TBitmap32; var HotSpot: TPoint; s: TStream;
StreamZeroPoint: integer; const Entry: ICONDIRENTRY; AsCursor: boolean): boolean;
const
PNG_Signature = $474e50; // 'PNG'
var
i, oldPos: integer;
dpi: double;
begin
Result := False;
try
// initialize
oldPos := s.Position;
// load information from directory entry
with Entry do
begin
if AsCursor then
HotSpot := Point(wPlanes_XHotSpot, wBitCount_YHotSpot) else
HotSpot := Point(0, 0);
// jump to data block
s.Position := StreamZeroPoint + Integer(dwImageOffset);
end;
// does it contain raw PNG data?
i := Integer(s.ReadDWord);
s.Seek(-4, soFromCurrent);
if i shr 8 = PNG_Signature then
Result := pngLoadFromStream(bm, s, dpi) else
Result := dibRead(bm, s, -1, True) and (bm.Width <> 0) and (bm.Height <> 0);
// restore stream position
s.Position := oldPos;
except
end;
end;
// Does not reset the stream position
procedure icoSavePageToStream(bm: TBitmap32; const HotSpot: TPoint; s: TStream;
StreamZeroPoint: integer; var Entry: ICONDIRENTRY;
PNGCompress, AsCursor: boolean);
var
Palette: TiePalette;
PixelFormat: TPixelFormat32;
nBits: integer;
begin
Palette := TiePalette.Create;
try
// get palette
PixelFormat := GetPixelFormat32(bm, palBW, palWin16, nil, Palette);
nBits := pf32ToBitCount[PixelFormat];
with Entry do
begin
bWidth := bm.Width;
bHeight := bm.Height;
if nBits < 8 then bColorCount := 1 shl nBits else bColorCount := 0;
bReserved := 0;
if AsCursor then
begin
wPlanes_XHotSpot := HotSpot.X;
wBitCount_YHotSpot := HotSpot.Y;
end else
begin
wPlanes_XHotSpot := 1;
wBitCount_YHotSpot := nBits;
end;
dwImageOffset := s.Position - StreamZeroPoint;
end;
// write data
if PNGCompress then
// write as PNG
pngSaveToStream(bm, s, PNG_COMPRESSION_HIGH, 0.0) else
dibWrite(bm, s, True, PixelFormat, Palette, nil);
finally
Palette.Free;
end;
with Entry do dwBytesInRes := s.Position - StreamZeroPoint - dwImageOffset;
end;
function icoDetect;
begin
Result := (Size >= 6) and (PICONDIR(Data).idReserved = 0) and
(PICONDIR(Data).idType = IfThen(IsCursor, ID_CUR, ID_ICO)) and
(PICONDIR(Data).idCount <> 0);
end;
function icoLoadFromStream;
var
i, StreamZeroPoint: integer;
dir: ICONDIR;
Entry: ICONDIRENTRY;
bm: TBitmap32;
Page: TDocPage;
HotSpot: TPoint;
begin
Result := False;
try
// set the zero point
StreamZeroPoint := s.Position;
// load directory
s.ReadBuffer(dir, SizeOf(dir));
// verify data
if dir.idCount = 0 then Exit;
// load pages
Doc.Clear;
bm := TBitmap32.Create;
try
for i := 0 to dir.idCount - 1 do
begin
s.ReadBuffer(Entry, SizeOf(Entry));
if icoLoadPageFromStream(bm, HotSpot, s,
StreamZeroPoint, Entry, dir.idType = ID_CUR) then
begin
Page := Doc.NewPage;
Page.HotSpot := HotSpot;
Page.Layers.Assign(bm);
end;
end;
finally
bm.Free;
end;
Result := (Doc.PageCount <> 0);
except
end;
end;
procedure icoSaveToStream;
var
i: integer;
StreamZeroPoint, oldPos: integer;
dir: ICONDIR;
Entry: ICONDIRENTRY;
bm: TBitmap32;
r: TRect;
begin
// set the zero point
StreamZeroPoint := s.Position;
// write directory
dir.idReserved := 0;
dir.idType := IfThen(AsCursor, ID_CUR, ID_ICO);
dir.idCount := Doc.PageCount;
s.WriteBuffer(dir, SizeOf(dir));
// calculate size of directory entries
s.Position := StreamZeroPoint + SizeOf(ICONDIR) +
SizeOf(ICONDIRENTRY) * Doc.PageCount;
// write all pages
bm := TBitmap32.Create;
try
for i := 0 to Doc.PageCount - 1 do
begin
with Doc.Pages[i] do
begin
r := Rect(0, 0, Min(256, Layers.Width), Min(256, Layers.Height));
Layers.Render(bm, r, lssAll, True);
icoSavePageToStream(bm, HotSpot, s, StreamZeroPoint, Entry,
PNGCompressIcon(bm.Width, bm.Height, PNGLimit), AsCursor);
end;
oldPos := s.Position;
s.Position := StreamZeroPoint + SizeOf(ICONDIR) + SizeOf(ICONDIRENTRY) * i;
s.WriteBuffer(Entry, SizeOf(Entry));
s.Position := oldPos;
end;
finally
bm.Free;
end;
end;
function icoLoadFromFile;
var
s: TStream;
begin
try
s := TFileStream.Create(fn, fmOpenRead);
try
Result := icoLoadFromStream(Doc, s);
finally
s.Free;
end;
except
Result := False;
end;
end;
procedure icoSaveToFile;
var
s: TStream;
begin
s := TFileStream.Create(fn, fmCreate);
try
icoSaveToStream(Doc, s, AsCursor, PNGLimit);
finally
s.Free;
end;
end;
end.