-
Notifications
You must be signed in to change notification settings - Fork 18
/
cGraphFX.pas
367 lines (274 loc) · 11 KB
/
cGraphFX.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
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
UNIT cGraphFX;
{=============================================================================================================
Gabriel Moraru
2023.08.05
See Copyright.txt
--------------------------------------------------------------------------------------------------------------
Graphic effects:
* Crop image
* Tile image
* Flip image
* Enhance image (Darkness, Brightness, Contrast, Saturation)
External dependencies:
* None (had VclGraphUtil)
---------------------------------------------------------------------------------------------------}
INTERFACE
USES
Winapi.Windows, System.SysUtils, Vcl.Graphics, ccStreamBuff;
TYPE
RTileType = record
Horizon : Boolean;
Vertical: Boolean;
OneRow : Boolean;
end;
PTileParams= ^RTileParams; { Parameters for the 'Tile' routine }
RTileParams = record
TileType : RTileType;
TileAuto : Boolean; { False = No tile, True = Auto Tile (small images will be tiled. If wallpaper's height at least 40% of desktop's height, then 'Perfect Tile' effect will be applied (see the manual for details)). }
TileThreshold : Integer; {%} { Any image with an area higher or equal with desktop's size is considered 'normal'. Any image with area between 99% and this value is considered 'small' and it will be stretched to fill the desktop. Any image below this value is considered 'tiny' (to small to be stretched). }
procedure Reset;
procedure WriteToStream (Stream: TCubicBuffStream);
procedure ReadFromStream(Stream: TCubicBuffStream);
end;
PEnhanceParams = ^REnhanceParams; { Parameters for the 'Enhance' routine }
REnhanceParams= record
Smooth : Boolean;
Darkness : ShortInt; {-128..127}
Brightness : Smallint; {-32768..32767}
Contrast : Smallint;
Saturation : Smallint;
procedure Reset;
procedure WriteToStream (Stream: TCubicBuffStream);
procedure ReadFromStream(Stream: TCubicBuffStream);
end;
{ Crop }
procedure CropBitmap (BMP: TBitmap; X, Y, W, H: Integer); overload; { XYWH are crop coordinates }
procedure CropBitmap (BMP: TBitmap; W, H: Integer); overload; { The image is centered before crop (crop edges in equal proportions) }
procedure CropBitmap (BMP: TBitmap; CONST MasterBMP: TBitmap); overload; { Crop the 'BMP' to fit into the MasterBMP }
{ Tile }
function TileBitmap (BMP: TBitmap; CONST OutWidth, OutHeight: Integer): TBitmap;
procedure TileBitmapMirror (BMP: TBitmap; CONST OutWidth, OutHeight: Integer; TileType: RTileType); { Use a mirror effect for every odd image }
{ Mirror }
procedure FlipDown (Bmp: TBitmap); { 16ms }
procedure FlipRight (Bmp: TBitmap); { 18ms }
IMPLEMENTATION
USES
System.Types, cGraphBitmap;
{---------------------------------------------------------------------------------------------------
CROP
---------------------------------------------------------------------------------------------------}
procedure CropBitmap(BMP: TBitmap; X, Y, W, H: Integer); { XYWH are crop coordinates }
begin
Assert(W > 0);
Assert(H > 0);
if BMP.Width < W
then W:= BMP.Width;
if BMP.Height < h
then h:= BMP.Height;
BitBlt(BMP.Canvas.Handle, 0, 0, W, H, BMP.Canvas.Handle, X, Y, SRCCOPY);
BMP.Width := W;
BMP.Height:= H;
end;
{ The image is centered before crop (crop edges in equal proportions) }
procedure CropBitmap(BMP: TBitmap; W, H: Integer);
VAR HalfX, HalfY: Integer;
begin
HalfX:= (BMP.Width - W) DIV 2;
HalfY:= (BMP.Height - H) DIV 2;
if HalfX < 0
then HalfX:= 0;
if HalfY < 0
then HalfY:= 0;
CropBitmap(BMP, HalfX, HalfY, W, H);
end;
procedure CropBitmap(BMP: TBitmap; CONST MasterBMP: TBitmap); { Crop the 'ToCropBMP' to fit into the MasterBMP. Basically, it is identical with the procedure above but instead of an integer parameter I give a BMP parameter }
VAR HalfX, HalfY: Integer;
begin
HalfX:= Round((BMP.Width - MasterBMP.Width) / 2);
HalfY:= Round((BMP.Height - MasterBMP.Height) / 2);
if HalfX < 0
then HalfX:= 0;
if HalfY < 0
then HalfY:= 0;
CropBitmap(BMP, HalfX, HalfY, MasterBMP.Width, MasterBMP.Height);
end;
{---------------------------------------------------------------------------------------------------
TILE
---------------------------------------------------------------------------------------------------}
function TileBitmap(BMP: TBitmap; CONST OutWidth, OutHeight: Integer): TBitmap; { Tile a image without using a secondary bitmap }
VAR
TileX, TileY: integer;
begin
{ Create bitmap and set its size }
Result:= CreateBitmap(OutWidth, OutHeight);
if Result= NIL then EXIT;
for TileX:= 0 TO (OutWidth div BMP.Width) do
for TileY:= 0 TO (OutHeight div BMP.Height) do
Result.Canvas.Draw (TileX* BMP.Width, TileY* BMP.Height, BMP);
end;
{ Use a mirror effect for every odd image }
procedure TileBitmapMirror(BMP: TBitmap; CONST OutWidth, OutHeight: Integer; TileType: RTileType);
VAR
w, h, iCol, iRow: Integer;
OutputBMP, MirrorBMP, MirrorDownBMP, DoubleMirror: TBitmap;
procedure StitchBitmap(CurrBitmap: TBitmap);
begin
OutputBMP.Canvas.Draw (iCol* w, iRow* h, CurrBitmap)
end;
begin
MirrorBMP := NIL;
MirrorDownBMP:= NIL;
DoubleMirror := NIL;
OutputBMP:= CreateBitmap(OutWidth, OutHeight);
TRY
//FillBitmap(OutputBMP, TileType.BkgColor); del
w:= BMP.Width;
h:= BMP.Height;
{ Flip image horizontally }
if TileType.Horizon then
begin
MirrorBMP:= TBitmap.Create;
MirrorBMP.Assign(BMP); { Make a copy of input image }
FlipRight (MirrorBMP);
end;
{ Flip image vertically (head down) }
if TileType.Vertical then
begin
MirrorDownBMP:= TBitmap.Create;
MirrorDownBMP.Assign(BMP);
FlipDown (MirrorDownBMP);
end;
{ Flip image vertically and horizontally }
if TileType.Vertical
AND TileType.Horizon then
begin
DoubleMirror:= TBitmap.Create;
DoubleMirror.Assign(BMP);
FlipDown (DoubleMirror);
FlipRight (DoubleMirror);
end;
if TileType.OneRow
then
{ Flip only horizontally }
for iCol:= 0 TO (OutWidth div w) DO
if (iCol mod 2 = 0) { Even columns }
then OutputBMP.Canvas.Draw (iCol* w, (OutHeight - h) DIV 2, BMP)
else
if TileType.Horizon
then OutputBMP.Canvas.Draw (iCol* w, (OutHeight - h) DIV 2, MirrorBMP) { Apply mirrored BMP }
else OutputBMP.Canvas.Draw (iCol* w, (OutHeight - h) DIV 2, BMP) { Apply normal BMP }
else
{ Flip horizontally & also vertically }
for iRow:= 0 TO (OutHeight div h) DO
for iCol:= 0 TO (OutWidth div w) DO
if (iRow mod 2 = 0) { Randurile Even }
then
if (iCol mod 2 = 0) { Even columns }
then StitchBitmap( BMP)
else
if TileType.Horizon
then StitchBitmap( MirrorBMP) { Apply mirrored BMP }
else StitchBitmap( BMP) { Apply normal BMP }
else { Randurile impare }
if (iCol mod 2 = 0) { Even columns }
then
if TileType.Vertical
then StitchBitmap( MirrorDownBMP)
else StitchBitmap( BMP)
else
if TileType.Horizon
AND TileType.Vertical
then StitchBitmap( DoubleMirror)
else
if TileType.Horizon
then StitchBitmap( MirrorBMP)
else
if TileType.Vertical
then StitchBitmap( MirrorDownBMP)
else StitchBitmap( BMp);
BMP.Assign(OutputBMP);
FINALLY
FreeAndNil(MirrorDownBMP);
FreeAndNil(MirrorBMP);
FreeAndNil(DoubleMirror);
FreeAndNil(OutputBMP);
END;
end;
{-------------------------------------------------------------------------------------------------------------
MIRROR
Tester: c:\MyProjects\Projects GRAPHICS\Rotate, flip\RotateTester.dpr
-------------------------------------------------------------------------------------------------------------}
procedure FlipDown(Bmp: TBitmap); { 16ms } { Same as JanFx.FlipDown (20ms) }
var dx, dy: integer;
begin
dx:= Bmp.Width;
dy:= Bmp.Height;
Bmp.Canvas.CopyRect(Rect(0,0,dx,dy), Bmp.Canvas,Rect(0,dy,dx,0));
end;
{ Mirror left-right }
procedure FlipRight(Bmp: TBitmap); { 18ms }
var dx, dy: integer;
begin
dx:= Bmp.Width;
dy:= Bmp.Height;
Bmp.Canvas.CopyRect(Rect(0,0,dx,dy), Bmp.Canvas,Rect(dx,0,0,dy));
end;
{-------------------------------------------------------------------------------------------------------------
TILE
-------------------------------------------------------------------------------------------------------------}
procedure RTileParams.Reset;
begin
TileAuto := TRUE;
TileType.Vertical := FALSE;
TileType.Horizon := TRUE;
TileType.OneRow := TRUE;
TileThreshold := 40;{%} { Any image with an area higher or equal with desktop's size is considered 'normal'. Any image with are between 99% and this value is considered 'small' and it will be stretched to fill the desktop. Any image below this value is considered 'tiny' (to small to be stretched). }
end;
procedure RTileParams.WriteToStream(Stream: TCubicBuffStream);
begin
Stream.WriteBoolean (TileAuto);
Stream.WriteBoolean (TileType.Horizon);
Stream.WriteBoolean (TileType.OneRow);
Stream.WriteBoolean (TileType.Vertical);
Stream.WriteInteger (TileThreshold);
Stream.WritePadding(32);
end;
procedure RTileParams.ReadFromStream(Stream: TCubicBuffStream);
begin
TileAuto := Stream.ReadBoolean;
TileType.Horizon := Stream.ReadBoolean;
TileType.OneRow := Stream.ReadBoolean;
TileType.Vertical:= Stream.ReadBoolean;
TileThreshold := Stream.ReadInteger;
Stream.ReadPadding(32);
end;
{-------------------------------------------------------------------------------------------------------------
ENHANCE
-------------------------------------------------------------------------------------------------------------}
procedure REnhanceParams.Reset;
begin
Smooth := FALSE;
Darkness := 0;
Brightness := 0;
Contrast := 0;
Saturation := 255;
end;
procedure REnhanceParams.WriteToStream(Stream: TCubicBuffStream);
begin
Stream.WriteSmallInt (Brightness);
Stream.WriteSmallInt (Contrast);
Stream.WriteSmallInt (Saturation);
Stream.WriteShortInt (Darkness);
Stream.WriteBoolean (Smooth);
Stream.WritePadding(32);
end;
procedure REnhanceParams.ReadFromStream(Stream: TCubicBuffStream);
begin
Brightness := Stream.ReadSmallInt;
Contrast := Stream.ReadSmallInt;
Saturation := Stream.ReadSmallInt;
Darkness := Stream.ReadShortInt;
Smooth := Stream.ReadBoolean;
Stream.ReadPadding(32);
end;
end.