Repository navigation
Expand file tree
/
Copy pathTesterForm.pas
More file actions
640 lines (478 loc) · 20.2 KB
/
Copy pathTesterForm.pas
File metadata and controls
640 lines (478 loc) · 20.2 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
UNIT TesterForm;
{=============================================================================================================
Gabriel Moraru
2024.12
www.GabrielMoraru.com
Github.com/GabrielOnDelphi/Delphi-LightSaber/blob/main/System/Copyright.txt
--------------------------------------------------------------------------------------------------------------
Description
Tests multiple (13) Delphi resizing algorithms that can be found on the Internet for free to find the best one.
The test include also some personal or 3rd party algorithms.
If you don't have access to their source code, you will have to switch off the $3RDPARTY switch.
Tests:
Resample up
Resample down.
Requires the LightSaber library:
https://github.com/GabrielOnDelphi/Delphi-LightSaber
------------------------------------------------------------------------------------------------------------
Conclusions
In general, all tested algorithms seem suitable to resize down a high res image but some are faster than others.
The story is different when you try to resize up (a small res image to high resolution).
The winner is Windows.StretchBlt. See StretchF in LightVcl.Graph.ResizeWin.
Also see: https://msdn.microsoft.com/en-us/library/windows/desktop/dd162950(v=vs.85).aspx
=============================================================================================================}
INTERFACE
USES
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Actions, System.Classes,
FMX.Graphics, FMX.Surfaces,
Vcl.Controls, Vcl.Forms, Vcl.ExtCtrls, Vcl.FileCtrl, Vcl.ActnList, Vcl.StdCtrls, Spin, Vcl.Graphics, Vcl.Imaging.pngimage,
LightCore, LightCore.Time, LightCore.Types, LightCore.INIFile, LightCore.IO, LightVcl.Visual.FileListBox, LightVcl.Common.IO,
LightVcl.Visual.Splitter, LightVcl.Visual.Memo, LightVcl.Visual.CheckBox, LightVcl.Visual.PathEdit, LightCore.AppData, LightVcl.Visual.AppDataForm, LightVcl.Visual.AppData;
type
TfrmResample = class(TLightForm)
actBitBlt : TAction;
actDephiStrtchDrw : TAction;
actFMXGraphics : TAction;
actGr32 : TAction;
actHBResize : TAction;
ActionList : TActionList;
actJanFXSmoothRes : TAction;
actJanFxStretch : TAction;
actMadshi : TAction;
actMsThumbnails : TAction;
actResizeMMX : TAction;
actScaleImage : TAction;
btnBitBlt : TButton;
btnDelphiScaleImg : TButton;
btnDephiStrtchDrw : TButton;
btnGr32 : TButton;
btnHB : TButton;
btnJanSmooth : TButton;
btnJanStretch : TButton;
btnMadshi : TButton;
btnMsThumbnails : TButton;
btnResizeMMX : TButton;
Button2 : TButton;
chkTrimRam : TLightCheckBox;
CubicSplitter1 : TCubicSplitter;
Files : TLightFileList;
Panel1 : TPanel;
Panel2 : TPanel;
Panel3 : TPanel;
Path : TlightPathEdit;
spnJan : TSpinEdit;
spnGr32Filter : TSpinEdit;
btnHBQckDwn : TButton;
btnHBHard : TButton;
Panel5 : TPanel;
btnOrig : TButton;
btnRefresh : TButton;
Label2 : TLabel;
Panel4 : TPanel;
lblNewWidth : TLabel;
spnWidth : TSpinEdit;
Panel7 : TPanel;
Panel6 : TPanel;
Preview : TImage;
chkStretch : TLightCheckBox;
chkSaveOutput : TLightCheckBox;
Button1 : TButton;
actWic : TAction;
Button3 : TButton;
actGDI : TAction;
procedure actBitBltExecute (Sender: TObject);
procedure actDephiStrtchDrwExecute (Sender: TObject);
procedure actFMXGraphicsExecute (Sender: TObject);
procedure actGr32Execute (Sender: TObject);
procedure actHBResizeExecute (Sender: TObject);
procedure actJanFXSmoothResExecute (Sender: TObject);
procedure actJanFxStretchExecute (Sender: TObject);
procedure actMadshiExecute (Sender: TObject);
procedure actMsThumbnailsExecute (Sender: TObject);
procedure actResizeMMXExecute (Sender: TObject);
procedure actScaleImageExecute (Sender: TObject);
procedure btnHBQckDwnClick (Sender: TObject);
procedure btnOrigClick (Sender: TObject);
procedure btnRefreshClick (Sender: TObject);
procedure btnHBHardClick (Sender: TObject);
procedure FilesDblClick (Sender: TObject);
procedure FormDestroy (Sender: TObject);
procedure spnJanChange (Sender: TObject);
procedure chkStretchClick (Sender: TObject);
procedure actWicExecute (Sender: TObject);
procedure actGDIExecute (Sender: TObject);
private
BmpOut: Vcl.Graphics.TBitmap;
Loader: Vcl.Graphics.TBitmap;
function Ratio: Double;
procedure TimerStop(AlgorithmName: string; Time: Double);
function NewHeight: Integer;
function Scale: Double;
procedure ShowOutput(FileName: string);
procedure LoadInput24;
procedure PrepareOutput24;
procedure LoadInput32;
public
procedure FormPostInitialize; {don't forget inherited in FormPostInitialize!} override;
end;
VAR
frmResample: TfrmResample;
IMPLEMENTATION {$R *.dfm}
{$DEFINE HardID} // Undefine if you don't have the Hardware ID Extractor lib https://GabrielMoraru.com/my-delphi-code/delphi-libraries/hardware-id-extractor/
{$DEFINE HBert} // Undefine if you don't have the HBert lib
{$DEFINE 3rdPARTY} // Undefine if you don't have these 3rd party libs. They are free libraries. You can get them from: github.com/graphics32/graphics32, JanFX (Jedi lib)
{.DEFINE FastJpg} // USE THIS IN "PROJECT OPTIONS"! Undefine if you don't have this 3rd party lib. You can get it for free from www.github.com/galfar/PasJpeg2000
USES
{$IFDEF HardID} chHardID, {$ENDIF}
{$IFDEF HBert} GraphHBResize, {$ENDIF}
{$IFDEF 3RDPARTY} janFXStretch, GraphSmoothResizeASM, GraphMadGraphics32, {$ENDIF}
LightCore.Math, LightCore.Sound, LightCore.Debugger, LightVcl.Common.Debugger, LightCore.Colors,
LightVcl.Graph.Resize, LightVcl.Graph.ResizeVCL, LightVcl.Graph.ResizeGr32, LightVcl.Graph.ResizeWinGDI, LightVcl.Graph.ResizeFMX, LightVcl.Graph.ResizeWinWIC, LightVcl.Graph.ResizeWinBlt, LightVcl.Graph.Loader, LightCore.Graph.Loader.Resolution, LightVcl.Graph.Bitmap, LightVcl.Graph.ResizeWinThumb;
procedure TfrmResample.FormPostInitialize;
begin
inherited FormPostInitialize;
btnHB.Visible := AppData.RunningHome;
btnHBQckDwn.Visible:= AppData.RunningHome;
btnHBHard.Visible := AppData.RunningHome;
Path.Path:= Appdata.AppFolder;
AlphaBlend:= FALSE;
Files.SelectFirstItem;
SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS);
BmpOut:= Vcl.Graphics.TBitmap.Create;
Show;
btnOrigClick(Self);
//actMsThumbnailsExecute(Self);
end;
procedure TfrmResample.FormDestroy(Sender: TObject);
begin
FreeAndNil(BmpOut);
FreeAndNil(Loader);
end;
procedure TfrmResample.btnOrigClick(Sender: TObject);
begin
FreeAndNil(Loader); //ToDo 1: disable all buttons if Loader = nil;
if FileExistsMsg(Files.FileName) then
begin
Loader:= LoadGraph(Files.FileName);
Preview.Picture.Assign(Loader);
Preview.Stretch := chkStretch.Checked;
end;
end;
procedure TfrmResample.FilesDblClick(Sender: TObject);
VAR Width, Height: Integer;
begin
GetImageRes(Files.FileName, Width, Height);
Caption:= ' File: '+ Files.FileName+ ' - '+ GetFileSizeFormat(Files.FileName)+ i2s(Width)+ 'x'+ i2s(Height);
btnOrigClick(Sender);
end;
procedure TfrmResample.btnRefreshClick(Sender: TObject);
begin
Files.ReadFileNames;
end;
procedure TfrmResample.spnJanChange(Sender: TObject);
begin
Caption:= ResampleFilters[spnJan.Value].Name;
end;
procedure TfrmResample.chkStretchClick(Sender: TObject);
begin
Preview.Stretch:= chkStretch.Checked;
end;
{-------------------------------------------------------------------------------------------------------------
SIZES
-------------------------------------------------------------------------------------------------------------}
function TfrmResample.Ratio: Double;
begin
Result:= Loader.Width / Loader.Height;
end;
function TfrmResample.Scale: Double;
begin
Result:= spnWidth.Value / Loader.Width;
end;
function TfrmResample.NewHeight: Integer;
begin
Result:= RoundEx(spnWidth.Value / Ratio);
end;
{-------------------------------------------------------------------------------------------------------------
Output
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.TimerStop(AlgorithmName: string; Time: Double);
begin
VAR s:= IntToStr(Round(Time))+ 'ms';
{$IFDEF HardID}
s:= s+ ' / '+ LightCore.FormatBytes(ProcessCurrentMem, 2);
{$ENDIF}
s:= s+ ' ['+ AlgorithmName+ '] ';
Caption:= s;
end;
function OutputFolder: string;
begin
Result:= Appdata.AppFolder+ 'Output\';
ForceDirectoriesMsg(Result);
end;
function OutputFileName(CONST AlgorithmName: string; Time: Double): string;
begin
Result:= OutputFolder+ AlgorithmName+ ' ('+ IntToStr(Round(Time)) +'ms).bmp'
end;
procedure TfrmResample.ShowOutput(FileName: string);
begin
Preview.Picture.Bitmap.Assign(BmpOut); // If we do Preview.Picture.Assign(BmpOut) directly, then the TImage.Stretch won't work!
if chkSaveOutput.Checked
then BmpOut.SaveToFile(FileName);
Bip30;
end;
{-------------------------------------------------------------------------------------------------------------
PREPARE INP/OUT
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.LoadInput32;
begin
Caption:= '';
Preview.Picture := NIL;
// Unfortunatelly we have to reload the image as pf32
FreeAndNil(Loader); //ToDo 1: disable all buttons if Loader = nil;
if FileExistsMsg(Files.FileName)
then Loader:= LoadGraph(Files.FileName);
Loader.PixelFormat:= pf32bit;
ClearBitmap(BmpOut);
BmpOut.PixelFormat:= pf32bit;
BmpOut.SetSize(spnWidth.Value, NewHeight);
{$IFDEF HardID}
if chkTrimRam.Checked then chHardID.TrimWorkingSet;
{$ENDIF}
end;
procedure TfrmResample.LoadInput24;
begin
Caption:= '';
Preview.Picture := NIL;
BmpOut.Assign(Loader);
BmpOut.PixelFormat := pf24bit;
{$IFDEF HardID}
if chkTrimRam.Checked then chHardID.TrimWorkingSet;
{$ENDIF}
end;
procedure TfrmResample.PrepareOutput24;
begin
Caption:= '';
Preview.Picture := NIL;
ClearBitmap(BmpOut);
BmpOut.PixelFormat := pf24bit;
BmpOut.SetSize(spnWidth.Value, NewHeight);
{$IFDEF HardID}
if chkTrimRam.Checked then chHardID.TrimWorkingSet;
{$ENDIF}
end;
{-------------------------------------------------------------------------------------------------------------
1. JAN FX - Smooth Resize
61 ms
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actJanFXSmoothResExecute(Sender: TObject);
CONST
AlgorithmName= '01 JanFX SmoothResize';
begin
PrepareOutput24;
TimerStart;
SmoothResize(Loader, BmpOut); // BmpOut.PixelFormat:= pf24bit;
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
2. JAN FX - Stretch
745 ms
Resize down: very good but terribly slow (use BLT instead)
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actJanFxStretchExecute(Sender: TObject);
VAR AlgorithmName: string;
begin
PrepareOutput24;
AlgorithmName:= '02 JanFX Stretch-'+ i2s(spnJan.Value);
TimerStart;
janFxStretch.Stretch_(Loader, BmpOut, ResampleFilters[spnJan.Value].Filter, ResampleFilters[spnJan.Value].Width);
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
3. GR32 - StretchImage
184 ms
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actGr32Execute(Sender: TObject);
begin
LoadInput24;
TimerStart;
LightVcl.Graph.ResizeGr32.StretchGr32(BmpOut, Scale, Scale, spnGr32Filter.Value, MitchellKernel);
TimerStop('03 Gr32-'+ i2s(spnGr32Filter.Value), TimerElapsed);
ShowOutput(OutputFileName('03 Gr32 '+ i2s(spnGr32Filter.Value), TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
HB
570 ms
Resize down: VERY smooth. Better q than JanFX.SmoothResize.
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actHBResizeExecute(Sender: TObject);
CONST AlgorithmName= '04 HB';
begin
Loader.PixelFormat:= pf24bit;
PrepareOutput24;
TimerStart;
{$IFDEF 3RDPARTY}
GraphHBResize.HSmoothResize(Loader, BmpOut, spnWidth.Value, newHeight); {$ENDIF}
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
procedure TfrmResample.btnHBQckDwnClick(Sender: TObject);
CONST AlgorithmName= '04a HB HardDownscale';
begin
LoadInput24;
TimerStart;
{$IFDEF 3RDPARTY} GraphHBResize.QuickDownscaleFac2(BmpOut); {$ENDIF}
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
procedure TfrmResample.btnHBHardClick(Sender: TObject);
CONST AlgorithmName= '04c HB QuickDownScale';
begin
LoadInput24;
TimerStart;
{$IFDEF 3RDPARTY} GraphHBResize.HardDownscaleFac2(BmpOut); {$ENDIF}
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
MadShi
FAST!
Same timing, same quality.
I think it is the same algorithm as SmoothResizeASM.
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actMadshiExecute(Sender: TObject);
CONST AlgorithmName= '05 madGraphics';
begin
Caption:= '';
Preview.Picture := NIL;
LoadInput32; // Madshi is the only one that requires pf32
BmpOut.Assign(Loader); // Strange. MadShi requires the output image to be present in BmpOut
BmpOut.SetSize(spnWidth.Value, NewHeight);
TimerStart;
GraphMadGraphics32.StretchBitmap32(Loader, BmpOut);
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
Embarcadero ScaleImage
190 ms
BEST (in scale down)
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actScaleImageExecute(Sender: TObject);
CONST AlgorithmName= '06 VCL.ScaleImage';
begin
PrepareOutput24;
TimerStart;
LightVcl.Graph.ResizeVCL.ScaleImage(Loader, BmpOut, Scale);
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
DELPHI STRETCHDRAW
40 ms
The worst image quality when scaling up.
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actDephiStrtchDrwExecute(Sender: TObject);
CONST AlgorithmName= '07 VCL.Stretch';
begin
PrepareOutput24;
TimerStart;
LightVcl.Graph.ResizeVCL.CanvasStretch(Loader, BmpOut);
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
MMX
76 ms
Very good to resize up but not down.
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actResizeMMXExecute(Sender: TObject);
CONST AlgorithmName= '08 SmoothResize ASM';
begin
PrepareOutput24;
TimerStart;
BmpOut:= GraphSmoothResizeASM.SmoothResizeMMX(Loader, spnWidth.Value, NewHeight);
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
Microsoft Thumb
88 ms
Resize down:
Too smooth, distorts the image, However, it improves the colors.
Fastest ever!
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actMsThumbnailsExecute(Sender: TObject);
CONST AlgorithmName= '09 Windows.Thumbnail';
begin
PrepareOutput24;
TimerStart;
VAR ThumbObj:= LightVcl.Graph.ResizeWinThumb.TFileThumb.Create;
TRY
ThumbObj.Width := spnWidth.Value;
ThumbObj.FilePath := Files.FileName; // whenever you set a FilePath a new ThumbBmp is made
ThumbObj.GenerateThumbnail;
BmpOut.Assign(ThumbObj.ThumbBmp);
FINALLY
FreeAndNil(ThumbObj);
END;
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
BITBLT
150 ms
--------------------------------------------------------------------------------------------------------------
Resize down: VERY smooth. Better than JanFX.SmoothResize.
Resize up: better (sharper) than JanFX.SmoothResize
Time: similar to JanFx }
procedure TfrmResample.actBitBltExecute(Sender: TObject);
CONST AlgorithmName= '10 Windows.StretchBlt';
begin
LoadInput24;
TimerStart;
Stretch(BmpOut, spnWidth.Value, NewHeight);
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
FMX
Absolutelly the slowest!
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actFMXGraphicsExecute(Sender: TObject);
CONST AlgorithmName = '11 FMX.CreateThumbnail';
begin
LoadInput32;
BmpOut.Assign(Loader);
TimerStart;
ResizeFMX(BmpOut, spnWidth.Value, NewHeight);
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
{-------------------------------------------------------------------------------------------------------------
WIC
Resize down: too smooth, too slow
-------------------------------------------------------------------------------------------------------------}
procedure TfrmResample.actWicExecute(Sender: TObject);
CONST AlgorithmName= '12 Windows.WIC';
begin
LoadInput32; // Madshi is the only one that requires pf32
BmpOut.Assign(Loader); // Strange. MadShi requires the output image to be present in BmpOut
TimerStart;
LightVcl.Graph.ResizeWinWIC.ResizeBitmapWic(BmpOut, spnWidth.Value, NewHeight); // pf32
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
procedure TfrmResample.actGDIExecute(Sender: TObject);
CONST AlgorithmName= '13 Windows.GDI';
begin
LoadInput24;
//BmpOut.Assign(Loader);
TimerStart;
LightVcl.Graph.ResizeWinGDI.ResizeBitmapGDI(Loader, BmpOut, spnWidth.Value, NewHeight);
TimerStop(AlgorithmName, TimerElapsed);
ShowOutput(OutputFileName(AlgorithmName, TimerElapsed));
end;
end.