-
Notifications
You must be signed in to change notification settings - Fork 2
/
PNG.pas
119 lines (96 loc) · 2.75 KB
/
PNG.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
(*
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 PNG;
interface
uses
LclIntf, LclType, CIntf, SysUtils, Classes, Graphics, BitmapEx, Math;
const
SIG_PNG = $474e5089; // PNG file signature
PNG_COMPRESSION_NORMAL = 5;
PNG_COMPRESSION_HIGH = 9;
function pngDetect(Data: Pointer; Size: integer): boolean;
function pngLoadFromStream(bm: TBitmap32; s: TStream): boolean;
procedure pngSaveToStream(bm: TBitmap32; s: TStream; CompressionLevel: integer);
function pngLoadFromFile(bm: TBitmap32; const fn: string): boolean;
procedure pngSaveToFile(bm: TBitmap32; const fn: string; CompressionLevel: integer);
// Loads a PNG file into a TBitmap object
function pngLoadFromFileBM(bm: TBitmap; const fn: string; BackColor: TColor): boolean;
implementation
function pngDetect;
begin
Result := (Size >= 4) and (PCardinal(Data)^ = SIG_PNG);
end;
function pngLoadFromStream;
var
p, w, h: integer;
begin
Result := False;
try
p := s.Position;
Result := pngLoad(nil, @w, @h, IOFunction_ReadFromStream, s);
s.Position := p;
if not Result then Exit;
bm.Resize(w, h);
Result := pngLoad(bm.Data, @w, @h, IOFunction_ReadFromStream, s);
if not Result then Exit;
Result := True;
except
end;
end;
procedure pngSaveToStream;
begin
pngSave(bm.Data, bm.Width, bm.Height, IOFunction_WriteToStream, s,
CompressionLevel);
end;
function pngLoadFromFile;
var
s: TStream;
begin
try
s := TFileStream.Create(fn, fmOpenRead);
try
Result := pngLoadFromStream(bm, s);
finally
s.Free;
end;
except
Result := False;
end;
end;
procedure pngSaveToFile;
var
s: TStream;
begin
s := TFileStream.Create(fn, fmCreate);
try
pngSaveToStream(bm, s, CompressionLevel);
finally
s.Free;
end;
end;
function pngLoadFromFileBM;
var
bm32: TBitmap32;
begin
bm32 := TBitmap32.Create;
try
Result := pngLoadFromFile(bm32, fn);
if not Result then Exit;
bm32.ToBitmap(bm, BackColor);
finally
bm32.Free;
end;
end;
end.