-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLibspng.pas
191 lines (151 loc) · 5.65 KB
/
Libspng.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
unit Libspng;
(*
Libspng 0.6.2+ library wrapper.
@see https://libspng.org/
*)
(***) interface (***)
uses
SysUtils,
GraphTypes,
DlgMes,
Utils;
(* Decodes already loaded png file into Color32 image with alpha. Fails on invalid/malformed or huge image. *)
function DecodePng (PngBuf: pointer; PngBufSize: integer; MaxWidth: integer = GraphTypes.MAX_IMAGE_WIDTH; MaxHeight: integer = GraphTypes.MAX_IMAGE_HEIGHT): {On} GraphTypes.TRawImage32;
(***) implementation (***)
const
LIB_SPNG = 'libspng.dll';
NO_ERROR = 0;
SOME_ERROR = 1;
NO_FLAGS = 0;
SPNG_FMT_RGBA8 = 1;
SPNG_DECODE_TRNS = 1;
SPNG_COLOR_TYPE_GRAYSCALE = 0;
SPNG_COLOR_TYPE_TRUECOLOR = 2;
SPNG_COLOR_TYPE_INDEXED = 3;
SPNG_COLOR_TYPE_GRAYSCALE_ALPHA = 4;
SPNG_COLOR_TYPE_TRUECOLOR_ALPHA = 6;
type
TPngContext = pointer;
type
TPngHeader = packed record
Width: integer;
Height: integer;
BitDepth: byte;
ColorType: byte;
CompressionMethod: byte;
FilterMethod: byte;
InterlaceMethod: byte;
_Align1: array [1..3] of byte;
end;
TPngTransparency = packed record
Gray: word;
Red: word;
Green: word;
Blue: word;
NumPaletteEntries: integer;
PaletteAlphas: array [0..255] of byte;
end;
PMemoryHandlers = ^TMemoryHandlers;
TMemoryHandlers = packed record
Alloc: function (Size: integer): pointer; cdecl;
Realloc: function (Buf: pointer; NewSize: integer): pointer; cdecl;
AllocItems: function (NumItems, ItemSize: integer): pointer; cdecl;
Free: procedure (Buf: pointer); cdecl;
end;
function spng_ctx_new (Flags: integer): TPngContext; cdecl external LIB_SPNG;
function spng_ctx_new2 (MemoryHandlers: PMemoryHandlers; Flags: integer): TPngContext; cdecl external LIB_SPNG;
function spng_set_png_buffer (Context: TPngContext; Buf: pointer; Size: integer): integer; cdecl external LIB_SPNG;
function spng_set_image_limits (Context: TPngContext; MaxWidth, MaxHeight: integer): integer; cdecl external LIB_SPNG;
function spng_get_ihdr (Context: TPngContext; var Header: TPngHeader): integer; cdecl external LIB_SPNG;
function spng_get_trns (Context: TPngContext; var Transparency: TPngTransparency): integer; cdecl external LIB_SPNG;
function spng_decoded_image_size (Context: TPngContext; Format: integer; out ImageSize: integer): integer; cdecl external LIB_SPNG;
function spng_decode_image (Context: TPngContext; OutBuf: pointer; BufSize: integer; Format: integer; Flags: integer): integer; cdecl external LIB_SPNG;
procedure spng_ctx_free (Context: TPngContext); cdecl external LIB_SPNG;
function Bridge_Alloc (Size: integer): pointer; cdecl;
begin
GetMem(result, Size);
end;
function Bridge_Realloc (Buf: pointer; NewSize: integer): pointer; cdecl;
begin
ReallocMem(Buf, NewSize);
result := Buf;
end;
function Bridge_AllocItems (NumItems, ItemSize: integer): pointer; cdecl;
var
BufSize: integer;
begin
BufSize := NumItems * ItemSize;
GetMem(result, BufSize);
System.FillChar(result^, BufSize, #0);
end;
procedure Bridge_Free (Buf: pointer); cdecl;
begin
FreeMem(Buf);
end;
procedure InitMemoryHandlers (var MemoryHandlers: TMemoryHandlers);
begin
with MemoryHandlers do begin
Alloc := Bridge_Alloc;
Realloc := Bridge_Realloc;
AllocItems := Bridge_AllocItems;
Free := Bridge_Free;
end;
end;
function DecodePng (PngBuf: pointer; PngBufSize: integer; MaxWidth: integer = GraphTypes.MAX_IMAGE_WIDTH; MaxHeight: integer = GraphTypes.MAX_IMAGE_HEIGHT): {On} GraphTypes.TRawImage32;
var
LastResult: integer;
MemoryHandlers: TMemoryHandlers;
PngContext: TPngContext;
PngHeader: TPngHeader;
PngTransparency: TPngTransparency;
ImageSize: integer;
Pixels: GraphTypes.TArrayOfColor32;
RawImage32Setup: GraphTypes.TRawImage32Setup;
begin
{!} Assert(PngBuf <> nil);
{!} Assert(PngBufSize > 0);
{!} Assert(MaxWidth > 0);
{!} Assert(MaxHeight > 0);
result := nil;
LastResult := NO_ERROR;
PngContext := nil;
// * * * * * //
RawImage32Setup.Init;
RawImage32Setup.HasTransparency := false;
InitMemoryHandlers(MemoryHandlers);
PngContext := spng_ctx_new2(@MemoryHandlers, NO_FLAGS);
if PngContext = nil then begin
LastResult := SOME_ERROR;
end;
if LastResult = NO_ERROR then begin
LastResult := spng_set_png_buffer(PngContext, PngBuf, PngBufSize);
end;
if LastResult = NO_ERROR then begin
LastResult := spng_set_image_limits(PngContext, MaxWidth, MaxHeight);
end;
if LastResult = NO_ERROR then begin
LastResult := spng_get_ihdr(PngContext, PngHeader);
end;
if LastResult = NO_ERROR then begin
if (PngHeader.ColorType in [SPNG_COLOR_TYPE_GRAYSCALE_ALPHA, SPNG_COLOR_TYPE_TRUECOLOR_ALPHA]) or (spng_get_trns(PngContext, PngTransparency) = 0) then begin
RawImage32Setup.HasTransparency := true;
end;
LastResult := spng_decoded_image_size(PngContext, 1, ImageSize);
end;
if LastResult = NO_ERROR then begin
SetLength(Pixels, ImageSize div sizeof(Pixels[0]));
if RawImage32Setup.HasTransparency then begin
LastResult := spng_decode_image(PngContext, @Pixels[0], ImageSize, SPNG_FMT_RGBA8, SPNG_DECODE_TRNS);
end else begin
LastResult := spng_decode_image(PngContext, @Pixels[0], ImageSize, SPNG_FMT_RGBA8, NO_FLAGS);
end;
end;
if LastResult = NO_ERROR then begin
GraphTypes.RgbaToBgraPixels(Pixels);
result := GraphTypes.TRawImage32.Create(Pixels, PngHeader.Width, PngHeader.Height, ImageSize div PngHeader.Height, RawImage32Setup);
end;
if PngContext <> nil then begin
spng_ctx_free(PngContext);
end;
end;
end.