-
Notifications
You must be signed in to change notification settings - Fork 29
/
Quick.ImageFX.Vips.pas
1285 lines (1149 loc) · 33.7 KB
/
Quick.ImageFX.Vips.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
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ ***************************************************************************
Copyright (c) 2023-2024 Kike Pérez
Unit : Quick.ImageFX.Vips
Description : Image manipulation with LibVips
Author : Kike Pérez
Version : 1.0
Created : 12/10/2023
Modified : 27/02/2024
This file is part of QuickImageFX: https://github.com/exilon/QuickImageFX
Third-party libraries used:
LibVips (https://github.com/libvips/libvips)
CCR-Exif from Chris Rolliston (https://code.google.com/archive/p/ccr-exif)
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.ImageFX.Vips;
{$i QuickImageFX.inc}
interface
uses
{$IFDEF MSWINDOWS}
Winapi.Windows,
Vcl.ImgList,
Vcl.Controls,
System.SysUtils,
Winapi.ShellAPI,
Vcl.Graphics,
Vcl.Imaging.pngimage,
Vcl.Imaging.jpeg,
Vcl.Imaging.GIFImg,
{$ENDIF}
System.UITypes,
System.Classes,
System.Math,
Quick.Image.Engine.Vips,
CCR.Exif,
Quick.ImageFX,
Quick.ImageFX.Types,
Quick.Base64;
type
TImageFXVips = class(TImageFX,IImageFX)
private
fVipsImage : TVipsImage;
procedure DoScanlines(ScanlineMode : TScanlineMode);
function ResizeImage(w, h : Integer; ResizeOptions : TResizeOptions) : IImageFX;
procedure SaveToStreamWithoutCompression(stream : TStream; imgFormat : TImageFormat = ifJPG);
function IsEmpty: Boolean;
protected
function GetPixel(const x, y: Integer): TPixelInfo;
procedure SetPixel(const x, y: Integer; const P: TPixelInfo);
public
property Pixel[const x, y: Integer]: TPixelInfo read GetPixel write SetPixel;
constructor Create; overload; override;
constructor Create(const fromfile : string); overload; override;
destructor Destroy; override;
function Width : Integer;
function Height : Integer;
function NewBitmap(w, h : Integer) : IImageFX;
function IsNullOrEmpty : Boolean; override;
function LoadFromFile(const fromfile : string; CheckIfFileExists : Boolean = False) : IImageFX;
function LoadFromFile2(const fromfile : string; CheckIfFileExists : Boolean = False) : IImageFX;
function LoadFromStream(stream : TStream) : IImageFX;
function LoadFromString(const str : string) : IImageFX;
function LoadFromImageList(imgList : TImageList; ImageIndex : Integer) : IImageFX;
function LoadFromIcon(Icon : TIcon) : IImageFX;
function LoadFromFileIcon(const FileName : string; IconIndex : Word) : IImageFX;
function LoadFromFileExtension(const aFilename : string; LargeIcon : Boolean) : IImageFX;
function LoadFromResource(const ResourceName : string) : IImageFX;
function LoadFromImage(image : pVipsImage) : IImageFX;
procedure GetResolution(var x,y : Integer); overload;
function AspectRatio : Double;
function GetPixelImage(const x, y: Integer; image : pVipsImage): TPixelInfo;
procedure SetPixelImage(const x, y: Integer; const P: TPixelInfo; image : pVipsImage);
function IsGray : Boolean;
procedure Assign(Graphic : TGraphic);
function Clear(pcolor : TColor = clNone) : IImageFX;
function Resize(w, h : Integer) : IImageFX; overload;
function Resize(w, h : Integer; ResizeMode : TResizeMode; ResizeFlags : TResizeFlags = []; ResampleMode : TResamplerMode = rsLinear) : IImageFX; overload;
function Draw(Graphic : TGraphic; x, y : Integer; alpha : Double = 1) : IImageFX; overload;
function Draw(stream : TStream; x, y : Integer; alpha : Double = 1) : IImageFX; overload;
function Draw(overlay : TVipsImage; x, y : Integer; alpha : Double = 1) : IImageFX; overload;
function DrawCentered(Graphic : TGraphic; alpha : Double = 1) : IImageFX; overload;
function DrawCentered(stream: TStream; alpha : Double = 1) : IImageFX; overload;
function Rotate90 : IImageFX;
function Rotate180 : IImageFX;
function Rotate270 : IImageFX;
function RotateBy(RoundAngle : Integer) : IImageFX;
function RotateAngle(RotAngle : Single) : IImageFX;
function FlipX : IImageFX;
function FlipY : IImageFX;
function GrayScale : IImageFX;
function ScanlineH : IImageFX;
function ScanlineV : IImageFX;
function Lighten(StrenghtPercent : Integer = 30) : IImageFX;
function Darken(StrenghtPercent : Integer = 30) : IImageFX;
function Tint(mColor : TColor) : IImageFX;
function TintAdd(R, G , B : Integer) : IImageFX;
function TintBlue : IImageFX;
function TintRed : IImageFX;
function TintGreen : IImageFX;
function Solarize : IImageFX;
function Rounded(RoundLevel : Integer = 27) : IImageFX;
function AntiAliasing : IImageFX;
function SetAlpha(Alpha : Byte) : IImageFX;
procedure SaveToPNG(const outfile : string); override;
procedure SaveToJPG(const outfile : string); override;
procedure SaveToBMP(const outfile : string); override;
procedure SaveToGIF(const outfile : string); override;
procedure SaveToFile(const outfile : string; aImgFormat : TImageFormat); override;
function AsImage : pVipsImage;
function AsBitmap : TBitmap;
function AsPNG : TPngImage;
function AsJPG : TJpegImage;
function AsGIF : TGifImage;
function AsString(imgFormat : TImageFormat = ifJPG) : string;
function CloneImage : pVipsImage;
function Clone : IImageFX;
procedure SaveToStream(stream : TStream; imgFormat : TImageFormat = ifJPG);
end;
implementation
constructor TImageFXVips.Create;
begin
inherited Create;
fVipsImage := TVipsImage.Create;
end;
constructor TImageFXVips.Create(const fromfile: string);
begin
Create;
LoadFromFile(fromfile);
end;
destructor TImageFXVips.Destroy;
begin
if Assigned(fVipsImage) then fVipsImage.Free;
inherited;
end;
{function TImageFX.LoadFromFile(fromfile: string) : TImageFX;
var
fs: TFileStream;
FirstBytes: AnsiString;
Graphic: TGraphic;
bmp : TBitmap;
begin
Result := Self;
if not FileExists(fromfile) then
begin
fLastResult := arFileNotExist;
Exit;
end;
Graphic := nil;
fs := TFileStream.Create(fromfile, fmOpenRead);
try
SetLength(FirstBytes, 8);
fs.Read(FirstBytes[1], 8);
if Copy(FirstBytes, 1, 2) = 'BM' then
begin
Graphic := TBitmap.Create;
end else
if FirstBytes = #137'PNG'#13#10#26#10 then
begin
Graphic := TPngImage.Create;
end else
if Copy(FirstBytes, 1, 3) = 'GIF' then
begin
Graphic := TGIFImage.Create;
end else
if Copy(FirstBytes, 1, 2) = #$FF#$D8 then
begin
Graphic := TJPEGImage.Create;
end;
if Assigned(Graphic) then
begin
try
fs.Seek(0, soFromBeginning);
Graphic.LoadFromStream(fs);
bmp := TBitmap.Create;
try
InitBitmap(bmp);
bmp.Assign(Graphic);
fBitmap.Assign(bmp);
fLastResult := arOk;
finally
bmp.Free;
end;
except
end;
Graphic.Free;
end;
finally
fs.Free;
end;
end;}
function TImageFXVips.LoadFromFile(const fromfile: string; CheckIfFileExists : Boolean = False) : IImageFX;
var
fs : TFileStream;
begin
Result := Self;
if (CheckIfFileExists) and (not FileExists(fromfile)) then
begin
LastResult := arFileNotExist;
raise Exception.Create(Format('File "%s" not found',[fromfile]));
end;
//loads file into VipsImage
fs := TFileStream.Create(fromfile,fmShareDenyWrite);
try
Result := Self.LoadFromStream(fs);
finally
fs.Free;
end;
if fVipsImage.GetVipsImage = nil then
begin
LastResult := arUnknowFmtType;
raise Exception.Create(Format('Error loading image "%s"',[fromfile]));
end
else LastResult := arOk;
end;
function TImageFXVips.LoadFromFile2(const fromfile: string; CheckIfFileExists : Boolean = False) : IImageFX;
begin
Result := Self;
if (CheckIfFileExists) and (not FileExists(fromfile)) then
begin
LastResult := arFileNotExist;
raise Exception.Create(Format('File "%s" not found',[fromfile]));
end;
//loads file into VipsImage
LastResult := arUnknowFmtType;
try
fVipsImage.LoadFromFile(fromfile);
except
on E : Exception do raise Exception.CreateFmt('Error loading image "%s" (%s)',[fromfile, e.Message]);
end;
LastResult := arOk;
end;
function TImageFXVips.LoadFromStream(stream: TStream) : IImageFX;
var
ExifData : TExifData;
begin
Result := Self;
ExifData := nil;
if (not Assigned(stream)) or (stream.Size = 0) then
begin
LastResult := arZeroBytes;
raise Exception.Create('Stream is empty!');
end;
stream.Position := 0;
fVipsImage.LoadFromStream(stream);
if fVipsImage.IsNullOrEmpty then raise EInvalidGraphic.CreateFmt('Unknow Graphic format (%s)',[vips_error_buffer()]);
//if ExifRotation then fVipsImage.RotateByExif;
if ExifRotation then ProcessExifRotation(stream);
LastResult := arOk;
end;
function TImageFXVips.LoadFromString(const str: string) : IImageFX;
var
stream : TStringStream;
begin
Result := Self;
if str = '' then
begin
LastResult := arZeroBytes;
Exit;
end;
stream := TStringStream.Create(Base64Decode(str));
try
LoadFromStream(stream);
LastResult := arOk;
finally
stream.Free;
end;
end;
function TImageFXVips.LoadFromFileIcon(const FileName : string; IconIndex : Word) : IImageFX;
var
Icon : TIcon;
begin
Result := Self;
Icon := TIcon.Create;
try
Icon.Handle := ExtractAssociatedIcon(hInstance,pChar(FileName),IconIndex);
Icon.Transparent := True;
//fBitmap.Assign(Icon);
finally
Icon.Free;
end;
end;
function TImageFXVips.LoadFromResource(const ResourceName : string) : IImageFX;
var
icon : TIcon;
//GPBitmap : TGPBitmap;
begin
Result := Self;
icon:=TIcon.Create;
try
icon.LoadFromResourceName(HInstance,ResourceName);
icon.Transparent:=True;
Self.LoadFromIcon(icon);
finally
icon.Free;
end;
end;
function TImageFXVips.LoadFromImageList(imgList : TImageList; ImageIndex : Integer) : IImageFX;
var
icon : TIcon;
begin
Result := Self;
icon := TIcon.Create;
try
imgList.GetIcon(ImageIndex,icon);
icon.Transparent := True;
Self.LoadFromIcon(icon);
LastResult := arOk;
finally
icon.Free;
end;
end;
function TImageFXVips.LoadFromIcon(Icon : TIcon) : IImageFX;
var
ms : TMemoryStream;
bmp : TBitmap;
begin
Result := Self;
ms := TMemoryStream.Create;
try
bmp := TBitmap.Create;
try
InitBitmap(bmp);
bmp.Assign(Icon);
bmp.SaveToStream(ms);
finally
bmp.Free;
end;
Self.LoadFromStream(ms);
finally
ms.Free;
end;
end;
function TImageFXVips.LoadFromFileExtension(const aFilename : string; LargeIcon : Boolean) : IImageFX;
var
icon : TIcon;
aInfo : TSHFileInfo;
begin
Result := Self;
LastResult := arUnknowFmtType;
if GetFileInfo(ExtractFileExt(aFilename),aInfo,LargeIcon) then
begin
icon := TIcon.Create;
try
Icon.Handle := AInfo.hIcon;
Icon.Transparent := True;
Self.LoadFromIcon(Icon);
LastResult := arOk;
finally
icon.Free;
DestroyIcon(aInfo.hIcon);
end;
end;
end;
function TImageFXVips.LoadFromImage(image : pVipsImage) : IImageFX;
begin
Result := Self;
fVipsImage := image;
end;
function TImageFXVips.AspectRatio : Double;
var
w, h : Integer;
begin
if fVipsImage <> nil then
begin
w := vips_image_get_width(fVipsImage);
h := vips_image_get_height(fVipsImage);
Result := w / h;
end
else Result := 0;
end;
procedure TImageFXVips.GetResolution(var x,y : Integer);
begin
if Assigned(fVipsImage) then
begin
x := fVipsImage.Width;
y := fVipsImage.Height;
LastResult := arOk;
end
else
begin
x := -1;
y := -1;
LastResult := arCorruptedData;
end;
end;
function TImageFXVips.IsNullOrEmpty: Boolean;
begin
Result := fVipsImage = nil;
end;
function TImageFXVips.IsGray: Boolean;
begin
if Assigned(fVipsImage) then Result := vips_image_hasalpha(fVipsImage) = 0
else Result := False;
end;
function TImageFXVips.Clear(pcolor : TColor = clNone) : IImageFX;
begin
Result := Self;
fVipsImage.Clear(pcolor);
LastResult := arOk;
end;
function TImageFXVips.ResizeImage(w, h : Integer; ResizeOptions : TResizeOptions) : IImageFX;
var
ImgAux : pVipsImage;
srcRect,
tgtRect : TRect;
crop : Integer;
srcRatio : Double;
nw, nh : Integer;
Resampler : TVipsKernel;
begin
Result := Self;
if (not Assigned(fVipsImage)) or ((Self.Width * Self.Height) = 0) then
begin
LastResult := arZeroBytes;
Exit;
end;
//if any value is 0, calculates proportionaly
if (w * h) = 0 then
begin
//scales max w or h
if ResizeOptions.ResizeMode = rmScale then
begin
if (h = 0) and (Self.Height > Self.Width) then
begin
h := w;
w := 0;
end;
end
else ResizeOptions.ResizeMode := rmFitToBounds;
begin
if w > h then
begin
nh := (w * Self.Height) div Self.Width;
h := nh;
end
else
begin
nw := (h * Self.Width) div Self.Height;
w := nw;
end;
end;
end;
case ResizeOptions.ResizeMode of
rmScale: //recalculate width or height target size to preserve original aspect ratio
begin
if Self.Width > Self.Height then
begin
nh := (w * Self.Height) div Self.Width;
h := nh;
nw := w;
end
else
begin
nw := (h * Self.Width) div Self.Height;
w := nw;
nh := h;
end;
srcRect := Rect(0,0,Self.Width,Self.Height);
end;
rmCropToFill: //preserve target aspect ratio cropping original image to fill whole size
begin
nw := w;
nh := h;
crop := Round(h / w * Self.Width);
if crop < Self.Height then
begin
//target image is wider, so crop top and bottom
srcRect.Left := 0;
srcRect.Top := (Self.Height - crop) div 2;
srcRect.Width := Self.Width;
srcRect.Height := crop;
end
else
begin
//target image is narrower, so crop left and right
crop := Round(w / h * Self.Height);
srcRect.Left := (Self.Width - crop) div 2;
srcRect.Top := 0;
srcRect.Width := crop;
srcRect.Height := Self.Height;
end;
end;
rmFitToBounds: //resize image to fit max bounds of target size
begin
srcRatio := Self.Width / Self.Height;
if Self.Width > Self.Height then
begin
nw := w;
nh := Round(w / srcRatio);
if nh > h then //too big
begin
nh := h;
nw := Round(h * srcRatio);
end;
end
else
begin
nh := h;
nw := Round(h * srcRatio);
if nw > w then //too big
begin
nw := w;
nh := Round(w / srcRatio);
end;
end;
srcRect := Rect(0,0,Self.Width,Self.Height);
end;
else
begin
nw := w;
nh := h;
srcRect := Rect(0,0,Self.Width,Self.Height);
end;
end;
//if image is smaller no upsizes
if ResizeOptions.NoMagnify then
begin
if (Self.Width < nw) or (Self.Height < nh) then
begin
//if FitToBounds then preserve original size
if ResizeOptions.ResizeMode = rmFitToBounds then
begin
nw := Self.Width;
nh := Self.Height;
end
else
begin
//all cases no resizes, but CropToFill needs to grow to fill target size
if ResizeOptions.ResizeMode <> rmCropToFill then
begin
if not ResizeOptions.SkipSmaller then
begin
LastResult := arAlreadyOptim;
nw := srcRect.Width;
nh := srcRect.Height;
w := nw;
h := nh;
end
else Exit;
end;
end;
end;
end;
if ResizeOptions.Center then
begin
tgtRect.Top := (h - nh) div 2;
tgtRect.Left := (w - nw) div 2;
tgtRect.Width := nw;
tgtRect.Height := nh;
end
else tgtRect := Rect(0,0,nw,nh);
//selects resampler algorithm
case ResizeOptions.ResamplerMode of
rsAuto :
begin
if (w < Self.Width ) or (h < Self.Height) then Resampler := TVipsKernel.VIPS_KERNEL_LINEAR
else Resampler := TVipsKernel.VIPS_KERNEL_LINEAR;
end;
rsNearest : Resampler := TVipsKernel.VIPS_KERNEL_NEAREST;
rsLinear : Resampler := TVipsKernel.VIPS_KERNEL_LINEAR;
rsVipsCubic : Resampler := TVipsKernel.VIPS_KERNEL_CUBIC;
rsVipsLanczos2 : Resampler := TVipsKernel.VIPS_KERNEL_LANCZOS2;
rsVipsLanczos3 : Resampler := TVipsKernel.VIPS_KERNEL_LANCZOS3
else Resampler := TVipsKernel.VIPS_KERNEL_LINEAR;
end;
//fVipsImage.Resize(srcRect.Width, srcRect.Height,Resampler);
fVipsImage.Resize(nw, nh,Resampler);
//ImgAux := cvCreateImage(CvSize(w,h),fVipsImage^.depth,fVipsImage^.nChannels);
//fills outside zone borders with a color
if (ResizeOptions.FillBorders) and ((tgtRect.Width < w) or (tgtRect.Height < h)) then
begin
//cvRectangle(ImgAux,cvPoint(0,0),cvPoint(w,h),ColorToScalar(ResizeOptions.BorderColor),CV_FILLED);
fVipsImage.CanvasResize(w,h, TVipsExtend.VIPS_EXTEND_WHITE);
end;
if (fVipsImage.Width > nw) or (fVipsImage.Height > nh) then fVipsImage.CanvasResize(w,h, TVipsExtend.VIPS_EXTEND_WHITE);
//set source and target regions
//cvSetImageROI(fVipsImage, CvRect(srcRect.Left,srcRect.Top,srcRect.Width,srcRect.Height));
//cvSetImageROI(ImgAux, CvRect(tgtRect.Left,tgtRect.Top,tgtRect.Width,tgtRect.Height));
//resize
try
//cvResize(fVipsImage,ImgAux,Resampler);
//cvResetImageROI(fVipsImage);
//cvResetImageROI(ImgAux);
//cvReleaseImage(fVipsImage);
//fVipsImage := ImgAux;
LastResult := arOk;
except
LastResult := arCorruptedData;
//if ImgAux <> nil then cvReleaseImage(ImgAux);
end;
end;
function TImageFXVips.Resize(w, h : Integer) : IImageFX;
begin
Result := ResizeImage(w,h,ResizeOptions);
end;
function TImageFXVips.Resize(w, h : Integer; ResizeMode : TResizeMode; ResizeFlags : TResizeFlags = []; ResampleMode : TResamplerMode = rsLinear) : IImageFX;
var
lResizeOptions : TResizeOptions;
begin
lResizeOptions := TResizeOptions.Create;
try
if (rfNoMagnify in ResizeFlags) then lResizeOptions.NoMagnify := True else lResizeOptions.NoMagnify := False;
if (rfCenter in ResizeFlags) then lResizeOptions.Center := True else lResizeOptions.Center := False;
if (rfFillBorders in ResizeFlags) then lResizeOptions.FillBorders := True else lResizeOptions.FillBorders := False;
Result := ResizeImage(w,h,lResizeOptions);
finally
lResizeOptions.Free;
end;
end;
procedure TImageFXVips.Assign(Graphic : TGraphic);
var
ms : TMemoryStream;
begin
ms := TMemoryStream.Create;
try
LoadFromStream(ms);
finally
ms.Free;
end;
end;
function TImageFXVips.DrawCentered(Graphic : TGraphic; alpha : Double = 1) : IImageFX;
begin
Result := Draw(Graphic,(fVipsImage.Width - Graphic.Width) div 2, (fVipsImage.Height - Graphic.Height) div 2,alpha);
end;
function TImageFXVips.DrawCentered(stream: TStream; alpha : Double = 1) : IImageFX;
begin
Result := Draw(stream,-1,-1,alpha);
end;
function TImageFXVips.Draw(Graphic : TGraphic; x: Integer; y: Integer; alpha: Double = 1) : IImageFX;
var
stream : TStream;
begin
stream := TStringStream.Create;
try
Graphic.SaveToStream(stream);
Result := Draw(stream,x,y,alpha);
finally
stream.Free;
end;
end;
function TImageFXVips.Draw(stream: TStream; x: Integer; y: Integer; alpha: Double = 1) : IImageFX;
var
overlay : TVipsImage;
begin
Result := Self;
try
//get overlay image
if (stream = nil) or (stream.Size = 0) then raise EArgumentException.Create('Stream cannot be empty!');
overlay := TVipsImage.Create;
try
overlay.LoadFromStream(stream);
overlay.Linear(alpha, 0.0);
fVipsImage.DrawImage(overlay,x,y,TVipsBlendMode.VIPS_BLEND_MODE_OVER);
finally
overlay.Free;
end;
except
on E : Exception do raise Exception.CreateFmt('Drawing overlay error: (%s)', [e.Message]);
end;
end;
function TImageFXVips.Draw(overlay : TVipsImage; x, y : Integer; alpha : Double = 1) : IImageFX;
begin
Result := Self;
overlay.Linear(alpha, 0.0);
fVipsImage.DrawImage(overlay, x, y, TVipsBlendMode.VIPS_BLEND_MODE_OVER);
end;
function TImageFXVips.NewBitmap(w, h : Integer) : IImageFX;
begin
Result := Self;
if fVipsImage <> nil then fVipsImage.Free;
fVipsImage := TVipsImage.Create(w,h);
end;
function TImageFXVips.IsEmpty : Boolean;
begin
Result := fVipsImage <> nil;
end;
function TImageFXVips.Rotate90 : IImageFX;
begin
Result := Self;
LastResult := arRotateError;
fVipsImage.Rotate(TVipsAngle.VIPS_ANGLE_D90);
LastResult := arOk;
end;
function TImageFXVips.Rotate180 : IImageFX;
begin
Result := Self;
LastResult := arRotateError;
fVipsImage.Rotate(TVipsAngle.VIPS_ANGLE_D180);
LastResult := arOk;
end;
function TImageFXVips.Rotate270 : IImageFX;
begin
Result := Self;
LastResult := arRotateError;
fVipsImage.Rotate(TVipsAngle.VIPS_ANGLE_D270);
LastResult := arOk;
end;
function TImageFXVips.RotateBy(RoundAngle : Integer) : IImageFX;
begin
// do with vips_similarity()
raise ENotImplemented.Create('RotateBy Not implemented yet!');
end;
function TImageFXVips.RotateAngle(RotAngle: Single) : IImageFX;
begin
// do with vips_similarity()
raise ENotImplemented.Create('RotateAngle Not implemented yet!');
end;
function TImageFXVips.FlipX : IImageFX;
begin
Result := Self;
LastResult := arRotateError;
fVipsImage.Flip(TVipsDirection.VIPS_DIRECTION_HORIZONTAL);
LastResult := arOk;
end;
function TImageFXVips.FlipY : IImageFX;
begin
Result := Self;
LastResult := arRotateError;
fVipsImage.Flip(TVipsDirection.VIPS_DIRECTION_VERTICAL);
LastResult := arOk;
end;
function TImageFXVips.GrayScale : IImageFX;
begin
Result := Self;
LastResult := arColorizeError;
fVipsImage.GrayScale;
LastResult := arOk;
end;
function TImageFXVips.Height: Integer;
begin
Result := fVipsImage.Height;
end;
function TImageFXVips.Lighten(StrenghtPercent : Integer = 30) : IImageFX;
begin
Result := Self;
LastResult := arColorizeError;
fVipsImage.Linear(StrenghtPercent / 100, 0.0);
LastResult := arOk;
end;
function TImageFXVips.Darken(StrenghtPercent : Integer = 30) : IImageFX;
begin
Result := Self;
LastResult := arColorizeError;
fVipsImage.Linear(1 + ( StrenghtPercent / 100), 0.0);
LastResult := arOk;
end;
function TImageFXVips.Tint(mColor : TColor) : IImageFX;
var
RGB : TRGB;
begin
Result := Self;
LastResult := arColorizeError;
fVipsImage.ColourSpace(TVipsInterpretation.VIPS_INTERPRETATION_RGB);
LastResult := arOk;
end;
function TImageFXVips.TintAdd(R, G , B : Integer) : IImageFX;
const
alpha = 1;
var
x, y : Integer;
srcPix : TPixelInfo;
srcHasAlpha : Boolean;
begin
Result := Self;
LastResult := arColorizeError;
{if fVipsImage.nChannels > 3 then srcHasAlpha := True
else srcHasAlpha := False;
for y := 0 to fVipsImage^.height - 1 do
begin
for x := 0 to fVipsImage^.width - 1 do
begin
srcPix := Self.Pixel[x,y];
srcPix.R := srcPix.R * R; //R
srcPix.G := srcPix.G * G;; //G
srcPix.B := srcPix.B * B;; //B
if (not srcHasAlpha) or (srcPix.A > 0) then Self.Pixel[x,y] := srcPix;
end;
end;
LastResult := arOk;}
end;
function TImageFXVips.TintBlue : IImageFX;
begin
Result := Tint(clBlue);
end;
function TImageFXVips.TintRed : IImageFX;
begin
Result := Tint(clRed);
end;
function TImageFXVips.Width: Integer;
begin
Result := fVipsImage.Width;
end;
function TImageFXVips.TintGreen : IImageFX;
begin
Result := Tint(clGreen);
end;
function TImageFXVips.Solarize : IImageFX;
begin
Result := TintAdd(255,-1,-1);
end;
function TImageFXVips.ScanlineH;
begin
Result := Self;
DoScanLines(smHorizontal);
end;
function TImageFXVips.ScanlineV;
begin
Result := Self;
DoScanLines(smVertical);
end;
procedure TImageFXVips.DoScanLines(ScanLineMode : TScanlineMode);
var
dolines : Boolean;
row,col : Integer;
pix : TPixelInfo;
begin
LastResult := arColorizeError; dolines := False;
if ScanlineMode = TScanlineMode.smHorizontal then
begin
for row := 0 to fVipsImage.height - 1 do
begin
dolines := not dolines;
if dolines then
for col := 0 to fVipsImage.width - 1 do
begin
pix := Pixel[col,row];
pix.R := 5; //R
pix.G := 5; //G
pix.B := 5; //B
Pixel[col,row] := pix;
end;
end;
end
else
begin
for row := 0 to fVipsImage.height - 1 do
begin
for col := 0 to fVipsImage.width - 1 do
begin
if dolines then
begin
pix := Pixel[col,row];
pix.R := 5; //R
pix.G := 5; //G
pix.B := 5; //B
Pixel[col,row] := pix;
end;
dolines := not dolines;
end;
end;
end;
end;
procedure TImageFXVips.SaveToPNG(const outfile : string);
begin
LastResult := arConversionError;
fVipsImage.SaveToFile(outfile, TVipsImageFormat.ifPNG);
LastResult := arOk;
end;
procedure TImageFXVips.SaveToJPG(const outfile : string);
begin
LastResult := arConversionError;
fVipsImage.SaveToFile(outfile, TVipsImageFormat.ifJPEG);
LastResult := arOk;
end;
procedure TImageFXVips.SaveToBMP(const outfile : string);
begin
LastResult := arConversionError;
fVipsImage.SaveToFile(outfile, TVipsImageFormat.ifBMP);
LastResult := arOk;
end;
procedure TImageFXVips.SaveToFile(const outfile: string; aImgFormat: TImageFormat);
begin
LastResult := arConversionError;
fVipsImage.SaveToFile(outfile, TVipsImageFormat(aImgFormat));
LastResult := arOk;
end;
procedure TImageFXVips.SaveToGIF(const outfile : string);
begin
LastResult := arConversionError;
fVipsImage.SaveToFile(outfile, TVipsImageFormat.ifGIF);
LastResult := arOk;
end;
function TImageFXVips.AsBitmap : TBitmap;