-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralHelpers.pas
More file actions
221 lines (177 loc) · 5.28 KB
/
GeneralHelpers.pas
File metadata and controls
221 lines (177 loc) · 5.28 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
unit GeneralHelpers;
interface
uses
Winapi.Windows, System.SysUtils,
System.Classes, Vcl.Graphics, ShlWapi,
System.IOUtils, Math;
function ClampInt(const Value, MinValue, MaxValue: Integer): Integer;
function ColorDist2(const A, B: TRGBTriple): Double;
function StretchF(const BMP: TBitmap; OutWidth, OutHeight: Integer): TBitmap;
function ResizeBilinear(const Src: TBitmap; NewW, NewH: Integer): TBitmap;
function BoolToInt(B: Boolean): Integer;
function IntToBol(i: Integer): Boolean;
function IsPowerOfTwo(Value: Integer): Boolean;
function WinShortPath(const APath: string; MaxLen: UINT): string;
procedure CheckPath(const AFileName: string);
function FormatFileSize(Bytes: Int64): string;
implementation
function IsPowerOfTwo(Value: Integer): Boolean;
begin
Result := (Value > 0) and ((Value and (Value - 1)) = 0);
end;
function ClampInt(const Value, MinValue, MaxValue: Integer): Integer;
begin
if Value < MinValue then
Result := MinValue
else if Value > MaxValue then
Result := MaxValue
else
Result := Value;
end;
function ColorDist2(const A, B: TRGBTriple): Double;
begin
Result := Sqr(A.rgbtRed - B.rgbtRed) + Sqr(A.rgbtGreen - B.rgbtGreen) +
Sqr(A.rgbtBlue - B.rgbtBlue);
end;
type
TRGBTripleArray = array[0..0] of TRGBTriple;
PRGBTripleArray = ^TRGBTripleArray;
{$R-}
function ResizeBilinear(const Src: TBitmap; NewW, NewH: Integer): TBitmap;
var
x, y: Integer;
gx, gy: Single;
gxi, gyi: Integer;
tx, ty: Single;
c00, c10, c01, c11: TRGBTriple;
r, g, b: Single;
SrcLine0, SrcLine1: PRGBTripleArray;
DstLine: PRGBTripleArray;
xRatio, yRatio: Single;
begin
Result := TBitmap.Create;
Result.PixelFormat := pf24bit;
Result.Width := NewW;
Result.Height := NewH;
// Ensure source format
Src.PixelFormat := pf24bit;
xRatio := (Src.Width - 1) / NewW;
yRatio := (Src.Height - 1) / NewH;
for y := 0 to NewH - 1 do
begin
gy := y * yRatio;
gyi := Trunc(gy);
ty := gy - gyi;
SrcLine0 := Src.ScanLine[gyi];
SrcLine1 := Src.ScanLine[Min(gyi + 1, Src.Height - 1)];
DstLine := Result.ScanLine[y];
for x := 0 to NewW - 1 do
begin
gx := x * xRatio;
gxi := Trunc(gx);
tx := gx - gxi;
// Fetch 4 surrounding pixels
c00 := SrcLine0[gxi];
c10 := SrcLine0[Min(gxi + 1, Src.Width - 1)];
c01 := SrcLine1[gxi];
c11 := SrcLine1[Min(gxi + 1, Src.Width - 1)];
// Bilinear interpolation
b :=
c00.rgbtBlue * (1 - tx) * (1 - ty) +
c10.rgbtBlue * tx * (1 - ty) +
c01.rgbtBlue * (1 - tx) * ty +
c11.rgbtBlue * tx * ty;
g :=
c00.rgbtGreen * (1 - tx) * (1 - ty) +
c10.rgbtGreen * tx * (1 - ty) +
c01.rgbtGreen * (1 - tx) * ty +
c11.rgbtGreen * tx * ty;
r :=
c00.rgbtRed * (1 - tx) * (1 - ty) +
c10.rgbtRed * tx * (1 - ty) +
c01.rgbtRed * (1 - tx) * ty +
c11.rgbtRed * tx * ty;
DstLine[x].rgbtBlue := Round(b);
DstLine[x].rgbtGreen := Round(g);
DstLine[x].rgbtRed := Round(r);
end;
end;
end;
{$R+}
function StretchF(const BMP: TBitmap; OutWidth, OutHeight: Integer): TBitmap;
var
DestRect: TRect;
begin
Result := TBitmap.Create;
try
// If source and destination sizes are identical, just copy
if (BMP.Width = OutWidth) and (BMP.Height = OutHeight) then
begin
Result.Assign(BMP);
Exit;
end;
// always produce 24-bit output
Result.PixelFormat := pf24bit;
Result.SetSize(OutWidth, OutHeight);
DestRect := Rect(0, 0, OutWidth, OutHeight);
if (BMP.Width < 16) or (BMP.Height < 16) then
begin
// simple Delphi StretchDraw for small source bitmaps
Result.Canvas.StretchDraw(DestRect, BMP);
end
else
begin
// higher-quality halftone stretch for larger bitmaps
SetStretchBltMode(Result.Canvas.Handle, HALFTONE);
SetBrushOrgEx(Result.Canvas.Handle, 0, 0, nil);
StretchBlt(Result.Canvas.Handle, DestRect.Left, DestRect.Top,
DestRect.Width, DestRect.Height, BMP.Canvas.Handle, 0, 0, BMP.Width,
BMP.Height, SRCCOPY);
end;
except
FreeAndNil(Result);
raise;
end;
end;
function BoolToInt(B: Boolean): Integer;
begin
if B then
Result := 1
else
Result := 0;
end;
function IntToBol(i: Integer): Boolean;
begin
if i = 1 then
Result := true
else
Result := false;
end;
function WinShortPath(const APath: string; MaxLen: UINT): string;
var
buf: array [0 .. MAX_PATH] of Char;
begin
if PathCompactPathEx(buf, PChar(APath), MaxLen, 0) then
Result := buf
else
Result := APath;
end;
procedure CheckPath(const AFileName: string);
var
Dir: string;
begin
// Get just the directory portion of the full filename
Dir := TPath.GetDirectoryName(AFileName);
if (Dir <> '') and not TDirectory.Exists(Dir) then
TDirectory.CreateDirectory(Dir);
end;
function FormatFileSize(Bytes: Int64): string;
begin
if Bytes < 1024 then
Result := Format('%d B', [Bytes])
else if Bytes < 1024 * 1024 then
Result := Format('%.1f KB', [Bytes / 1024])
else
Result := Format('%.2f MB', [Bytes / (1024 * 1024)]);
end;
end.