-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathQuick.ImageFX.GDI.pas
1533 lines (1403 loc) · 39.8 KB
/
Quick.ImageFX.GDI.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) 2013-2020 Kike P�rez
Unit : Quick.ImageFX.GDI
Description : Image manipulation with multiple graphic libraries
Author : Kike P�rez
Version : 4.0
Created : 10/05/2013
Modified : 11/01/2020
This file is part of QuickImageFX: https://github.com/exilon/QuickImageFX
***************************************************************************
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.GDI;
interface
uses
Windows,
Classes,
Controls,
Vcl.ImgList,
System.SysUtils,
Vcl.Graphics,
Winapi.ShellAPI,
GDIPAPI,
GDIPOBJ,
GDIPUTIL,
Vcl.Imaging.pngimage,
Vcl.Imaging.jpeg,
Vcl.Imaging.GIFImg,
Quick.Base64,
Quick.ImageFX,
Quick.ImageFX.Types;
const
MaxPixelCountA = MaxInt Div SizeOf (TRGBQuad);
type
TScanlineMode = (smHorizontal, smVertical);
PRGBAArray = ^TRGBAArray;
TRGBAArray = Array [0..MaxPixelCountA -1] Of TRGBQuad;
PGPColorArr = ^TGPColorArr;
TGPColorArr = array[0..500] of TGPColor;
pRGBQuadArray = ^TRGBQuadArray;
TRGBQuadArray = ARRAY [0 .. $EFFFFFF] OF TRGBQuad;
TRGBArray = ARRAY[0..32767] OF TRGBTriple;
pRGBArray = ^TRGBArray;
TImageFXGDI = class(TImageFX,IImageFX)
private
fBitmap : TBitmap;
procedure InitBitmap;
procedure DoScanlines(ScanlineMode : TScanlineMode);
procedure DoAntialias(bmp1,bmp2 : TBitmap);
procedure DoAlpha(bmp: TBitMap; Alpha: Byte);
procedure GPBitmapToBitmap(gpbmp : TGPBitmap; bmp : TBitmap);
function ResizeImage(w, h : Integer; ResizeOptions : TResizeOptions) : IImageFX;
procedure SetPixelImage(const x, y: Integer; const P: TPixelInfo; bmp : TBitmap);
function GetPixelImage(const x, y: Integer; bmp : TBitmap): TPixelInfo;
function AntiAliasing: IImageFX;
function AsPNG2: TPngImage;
class function ColorIsLight(Color: TColor): Boolean; static;
function ExtractFileIcon(const FileName: string; IconIndex: Word): IImageFX;
function ExtractResourceIcon(const ResourceName: string): IImageFX;
function Resize2(x, y: Integer; NoResizeIfSmaller: Boolean = False): IImageFX;
function RotateFlip(RotateFlipType: TRotateFlipType): IImageFX;
function SetAlpha(Alpha: Byte): IImageFX;
protected
function GetPixel(const x, y: Integer): TPixelInfo;
procedure SetPixel(const x, y: Integer; const P: TPixelInfo);
public
constructor Create; overload;
constructor Create(fromfile : string); overload;
destructor Destroy; override;
property Pixel[const x, y: Integer]: TPixelInfo read GetPixel write SetPixel;
function NewBitmap(w,h : Integer) : IImageFX;
function LoadFromFile(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;
procedure GetResolution(var x,y : Integer); overload;
function AspectRatio : Double;
function IsEmpty : Boolean;
function Clone : IImageFX;
function IsGray : Boolean;
procedure Assign(Graphic : TGraphic);
function Clear(pcolor : TColor = clWhite) : IImageFX;
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 DrawCentered(Graphic : TGraphic; alpha : Double = 1) : IImageFX; overload;
function DrawCentered(stream: TStream; alpha : Double = 1) : IImageFX; overload;
function AsBitmap : TBitmap;
function AsPNG : TPngImage;
function AsJPG : TJpegImage;
function AsGIF : TGifImage;
function AsString(imgFormat : TImageFormat = ifJPG) : string;
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 SaveToStream(stream : TStream; imgFormat : TImageFormat = ifJPG);
function Resize(w, h : Integer) : IImageFX; overload;
function Resize(w, h : Integer; ResizeMode : TResizeMode; ResizeFlags : TResizeFlags = []; ResampleMode : TResamplerMode = rsLinear) : 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;
end;
{Devuelve una imagen reducida al tama�o especificado y en formato JPG}
function ResizeImage(bmp : TBitmap; maxWidth,maxHeight : Integer; Proporcional : Boolean) : TBitmap;
{Hace Crop de un Bitmap}
procedure CropBitmap(var bmp : TBitmap; aColor : TColor);
{Redimensionar PNG}
procedure PNGResize(var png : TPNGImage; MaxWidth, MaxHeight : Integer);
implementation
constructor TImageFXGDI.Create;
begin
inherited;
InitBitmap;
end;
procedure TImageFXGDI.InitBitmap;
begin
fBitmap := TBitmap.Create;
fBitmap.PixelFormat := pf32bit;
fBitmap.AlphaFormat := afDefined;
end;
function TImageFXGDI.IsEmpty: Boolean;
begin
Result := fBitmap.Empty;
end;
function TImageFXGDI.IsGray: Boolean;
begin
raise ENotImplemented.Create('Not implemented!');
end;
constructor TImageFXGDI.Create(fromfile: string);
var
GPBitmap : TGPBitmap;
begin
GPBitmap := TGPBitmap.Create(fromfile);
try
if not Assigned(fBitmap) then InitBitmap;
GPBitmapToBitmap(GPBitmap,fBitmap);
finally
GPBitmap.Free;
end;
end;
destructor TImageFXGDI.Destroy;
begin
if Assigned(fBitmap) then fBitmap.Free;
inherited;
end;
function TImageFXGDI.NewBitmap(w, h: Integer): IImageFX;
begin
if Assigned(Self.fBitmap) then
begin
//Self.fBitmap.Canvas.FillRect();
Self.fBitmap.SetSize(w, h);
end
else Result := Self;
end;
procedure TImageFXGDI.GetResolution(var x,y : Integer);
begin
if Assigned(fBitmap) then
begin
x := fBitmap.Width;
y := fBitmap.Height;
LastResult := arOk;
end
else
begin
x := -1;
y := -1;
LastResult := arCorruptedData;
end;
end;
function TImageFXGDI.AspectRatio : Double;
begin
if Assigned(fBitmap) then Result := fBitmap.width / fBitmap.Height
else Result := 0;
end;
function TImageFXGDI.LoadFromFile(const fromfile : string; CheckIfFileExists : Boolean = False) : IImageFX;
var
GPBitmap : TGPBitmap;
begin
Result := Self;
if (CheckIfFileExists) and (not FileExists(fromfile)) then
begin
LastResult := arFileNotExist;
raise Exception.Create(Format('File "%s" not found',[fromfile]));
end;
GPBitmap := TGPBitmap.Create(fromfile);
try
if not Assigned(fBitmap) then InitBitmap;
GPBitmapToBitmap(GPBitmap,fBitmap);
finally
GPBitmap.Free;
end;
end;
procedure TImageFXGDI.Assign(Graphic : TGraphic);
var
ms : TMemoryStream;
begin
ms := TMemoryStream.Create;
try
LoadFromStream(ms);
finally
ms.Free;
end;
end;
function TImageFXGDI.LoadFromStream(stream: TStream) : IImageFX;
var
Picture : TPicture;
begin
Result := Self;
if (not Assigned(stream)) or (stream.Size = 0) then
begin
LastResult := arZeroBytes;
raise Exception.Create('Stream is empty!');
end;
Picture := TPicture.Create;
try
stream.Seek(0,soBeginning);
Picture.Bitmap.LoadFromStream(stream);
if not Assigned(fBitmap) then InitBitmap;
fBitmap.Assign(Picture.Bitmap);
finally
Picture.Free;
end;
end;
function TImageFXGDI.LoadFromString(const str: string) : IImageFX;
var
stream : TStringStream;
begin
Result := Self;
if str = '' then Exit;
stream := TStringStream.Create(Base64Decode(str));
try
fBitmap.LoadFromStream(stream);
finally
stream.Free;
end;
end;
function TImageFXGDI.Resize2(x, y : Integer; NoResizeIfSmaller : Boolean = False) : IImageFX;
var
src : TGPBitmap;
dst : TBitmap;
Graphics: TGPGraphics;
begin
Result := Self;
//Si uno de los valores es 0, lo calcula proporcionalmente
if x + y = 0 then
begin
x := fBitmap.Width;
y := fBitmap.Height;
end
else
begin
if y = 0 then y := Round(fBitmap.Height / fBitmap.Width * x);
if x = 0 then x := Round(fBitmap.Width / fBitmap.Height* y);
end;
//Si es m�s peque�o no agrandar
if NoResizeIfSmaller then
begin
if (fBitmap.Width < x) or (fBitmap.Height < y) then Exit;
end;
try
// create graphics object for output image
// create the output bitmap in desired size
src := TGPBitmap.Create(fBitmap.Handle,fBitmap.Palette);
dst := TBitmap.Create;
dst.PixelFormat := pf32bit;
dst.HandleType := bmDIB;
dst.AlphaFormat := afDefined;
dst.Width := x;
dst.Height := y;
Graphics := TGPGraphics.Create(dst.Canvas.Handle);
try
//Graphics.Clear(ColorRefToARGB(ColorToRGB(clNone)));
Graphics.Clear(MakeColor(0,0,0,0));
// set the composition mode to copy
Graphics.SetCompositingMode(CompositingModeSourceCopy);
// set high quality rendering modes
Graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
//Graphics.SetInterpolationMode(InterpolationModeHighQuality);
Graphics.SetPixelOffsetMode(PixelOffsetModeHighQuality);
Graphics.SetSmoothingMode(SmoothingModeHighQuality);
// draw the input image on the output in modified size
Graphics.DrawImage(src, 0, 0, x, y);
fBitmap.Assign(dst);
finally
Graphics.Free;
src.Free;
dst.Free;
end;
except
raise Exception.Create('Error scaling image');
end;
end;
function TImageFXGDI.ResizeImage(w, h : Integer; ResizeOptions : TResizeOptions) : IImageFX;
var
bmp : TBitmap;
srcRect,
tgtRect : TRect;
crop : Integer;
srcRatio : Double;
nw, nh : Integer;
begin
Result := Self;
if (not Assigned(fBitmap)) or ((fBitmap.Width * fBitmap.Height) = 0) then
begin
LastResult := arZeroBytes;
Exit;
end;
//if any value is 0, calculates proportionaly
if (w * h) = 0 then
begin
ResizeOptions.ResizeMode := rmFitToBounds;
if w > h then
begin
nh := (w * fBitmap.Height) div fBitmap.Width;
h := nh;
nw := w;
end
else
begin
nw := (h * fBitmap.Width) div fBitmap.Height;
w := nw;
nh := h;
end;
end;
case ResizeOptions.ResizeMode of
rmScale: //recalculate width or height target size to preserve original aspect ratio
begin
if fBitmap.Width > fBitmap.Height then
begin
nh := (w * fBitmap.Height) div fBitmap.Width;
h := nh;
nw := w;
end
else
begin
nw := (h * fBitmap.Width) div fBitmap.Height;
w := nw;
nh := h;
end;
srcRect := Rect(0,0,fBitmap.Width,fBitmap.Height);
end;
rmCropToFill: //preserve target aspect ratio cropping original image to fill whole size
begin
nw := w;
nh := h;
crop := Round(h / w * fBitmap.Width);
if crop < fBitmap.Height then
begin
//target image is wider, so crop top and bottom
srcRect.Left := 0;
srcRect.Top := (fBitmap.Height - crop) div 2;
srcRect.Width := fBitmap.Width;
srcRect.Height := crop;
end
else
begin
//target image is narrower, so crop left and right
crop := Round(w / h * fBitmap.Height);
srcRect.Left := (fBitmap.Width - crop) div 2;
srcRect.Top := 0;
srcRect.Width := crop;
srcRect.Height := fBitmap.Height;
end;
end;
rmFitToBounds: //resize image to fit max bounds of target size
begin
srcRatio := fBitmap.Width / fBitmap.Height;
if fBitmap.Width > fBitmap.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,fBitmap.Width,fBitmap.Height);
end;
else
begin
nw := w;
nh := h;
srcRect := Rect(0,0,fBitmap.Width,fBitmap.Height);
end;
end;
//if image is smaller no upsizes
if ResizeOptions.NoMagnify then
begin
if (fBitmap.Width < nw) or (fBitmap.Height < nh) then
begin
//if FitToBounds then preserve original size
if ResizeOptions.ResizeMode = rmFitToBounds then
begin
nw := fBitmap.Width;
nh := fBitmap.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
if not (ResizeOptions.ResamplerMode in [rsAuto,rsGDIStrech]) then raise Exception.Create('Only GDIStrech Resampler mode supported!');
bmp := TBitmap.Create;
try
bmp.Width := w;
bmp.Height := h;
if ResizeOptions.FillBorders then
begin
bmp.Canvas.Brush.Color := ResizeOptions.BorderColor;
bmp.Canvas.FillRect(Rect(0,0,w,h));
end;
bmp.Canvas.StretchDraw(tgtRect,fBitmap);
//StretchTransfer(bmp,tgtRect,tgtRect,fBitmap,srcRect,Resam,dmOpaque,nil);
try
fBitmap.Assign(bmp);
LastResult := arOk;
except
LastResult := arCorruptedData;
end;
finally
bmp.Free;
end;
end;
function TImageFXGDI.Resize(w, h : Integer) : IImageFX;
begin
Result := ResizeImage(w,h,ResizeOptions);
end;
function TImageFXGDI.Resize(w, h : Integer; ResizeMode : TResizeMode; ResizeFlags : TResizeFlags = []; ResampleMode : TResamplerMode = rsLinear) : IImageFX;
var
ResizeOptions : TResizeOptions;
begin
ResizeOptions := TResizeOptions.Create;
try
if (rfNoMagnify in ResizeFlags) then ResizeOptions.NoMagnify := True else ResizeOptions.NoMagnify := False;
if (rfCenter in ResizeFlags) then ResizeOptions.Center := True else ResizeOptions.Center := False;
if (rfFillBorders in ResizeFlags) then ResizeOptions.FillBorders := True else ResizeOptions.FillBorders := False;
Result := ResizeImage(w,h,ResizeOptions);
finally
ResizeOptions.Free;
end;
end;
function TImageFXGDI.Rotate180: IImageFX;
begin
raise ENotImplemented.Create('Not implemented!');
end;
function TImageFXGDI.Rotate270: IImageFX;
begin
raise ENotImplemented.Create('Not implemented!');
end;
function TImageFXGDI.Rotate90 : IImageFX;
begin
Result := RotateFlip(Rotate90FlipNone);
end;
function TImageFXGDI.RotateAngle(rotAngle : Single) : IImageFX;
begin
raise ENotImplemented.Create('Not implemented!');
end;
function TImageFXGDI.RotateBy(RoundAngle: Integer): IImageFX;
begin
raise ENotImplemented.Create('Not implemented!');
end;
function TImageFXGDI.RotateFlip(RotateFlipType : TRotateFlipType) : IImageFX;
var
src : TGPBitmap;
begin
Result := Self;
//
src := TGPBitmap.Create(fBitmap.Handle,fBitmap.Palette);
try
src.RotateFlip(RotateFlipType);
GPBitmapToBitmap(src,fBitmap);
finally
src.Free;
end;
end;
function TImageFXGDI.FlipX : IImageFX;
begin
Result := Self;
RotateFlip(RotateNoneFlipX);
end;
function TImageFXGDI.FlipY : IImageFX;
begin
Result := Self;
RotateFlip(RotateNoneFlipY);
end;
function TImageFXGDI.GrayScale : IImageFX;
type
PPixelRec = ^TPixelRec;
TPixelRec = packed record
B: Byte;
G: Byte;
R: Byte;
Reserved: Byte;
end;
var
X: Integer;
Y: Integer;
Gray: Byte;
Pixel: PPixelRec;
begin
Result := Self;
Assert(fBitmap.PixelFormat = pf32Bit);
fBitmap.PixelFormat := pf32bit;
for Y := 0 to (fBitmap.Height - 1) do
begin
Pixel := fBitmap.ScanLine[Y];
for X := 0 to (fBitmap.Width - 1) do
begin
//Gray := (Pixel.B + Pixel.G + Pixel.R) div 3;
Gray := Round(0.30 * Pixel.R + 0.59 * Pixel.G + 0.11 * Pixel.B);
//Gray := (Pixel.R shr 2) + (Pixel.R shr 4) + (Pixel.G shr 1) + (Pixel.G shr 4) + (Pixel.B shr 3);
Pixel.R := Gray;
Pixel.G := Gray;
Pixel.B := Gray;
Inc(Pixel);
end;
end;
end;
function TImageFXGDI.Lighten(StrenghtPercent : Integer = 30) : IImageFX;
type
PPixelRec = ^TPixelRec;
TPixelRec = packed record
B: Byte;
G: Byte;
R: Byte;
Reserved: Byte;
end;
var
X: Integer;
Y: Integer;
Pixel: PPixelRec;
begin
Result := Self;
Assert(fBitmap.PixelFormat = pf32Bit);
fBitmap.PixelFormat := pf32bit;
fBitmap.AlphaFormat := afDefined;
for Y := 0 to (fBitmap.Height - 1) do
begin
Pixel := fBitmap.ScanLine[Y];
for X := 0 to (fBitmap.Width - 1) do
begin
Pixel.R := Round(Pixel.R+((Pixel.R/255)*StrenghtPercent));
Pixel.G := Round(Pixel.G+((Pixel.G/255)*StrenghtPercent));
Pixel.B := Round(Pixel.B+((Pixel.B/255)*StrenghtPercent));
If Pixel.R < 0 Then Pixel.R := 0 Else If Pixel.R > 255 Then Pixel.R := 255;
If Pixel.G < 0 Then Pixel.G := 0 Else If Pixel.G > 255 Then Pixel.G := 255;
If Pixel.B < 0 Then Pixel.B := 0 Else If Pixel.B > 255 Then Pixel.B := 255;
Inc(Pixel);
end;
end;
end;
function TImageFXGDI.Darken(StrenghtPercent : Integer = 30) : IImageFX;
type
PPixelRec = ^TPixelRec;
TPixelRec = packed record
B: Byte;
G: Byte;
R: Byte;
Reserved: Byte;
end;
var
X: Integer;
Y: Integer;
Pixel: PPixelRec;
begin
Result := Self;
Assert(fBitmap.PixelFormat = pf32Bit);
fBitmap.PixelFormat := pf32bit;
fBitmap.AlphaFormat := afDefined;
for Y := 0 to (fBitmap.Height - 1) do
begin
Pixel := fBitmap.ScanLine[Y];
for X := 0 to (fBitmap.Width - 1) do
begin
Pixel.R := Round(Pixel.R-((Pixel.R/255)*StrenghtPercent));
Pixel.G := Round(Pixel.G-((Pixel.G/255)*StrenghtPercent));
Pixel.B := Round(Pixel.B-((Pixel.B/255)*StrenghtPercent));
Inc(Pixel);
end;
end;
end;
function TImageFXGDI.Tint(mColor : TColor) : IImageFX;
var
scanline: PRGBQuad;
y: Integer;
x: Integer;
RGB : TRGB;
begin
Result := Self;
Assert(fBitmap.PixelFormat = pf32bit);
RGB := ColorToRGBValues(mColor);
for y := 0 to fBitmap.Height - 1 do
begin
scanline := fBitmap.ScanLine[y];
for x := 0 to fBitmap.Width - 1 do
begin
with scanline^ do
begin
{if (rgbBlue = 255) and (rgbGreen = 255) and (rgbRed = 255) then
begin
FillChar(scanline^, sizeof(TRGBQuad), 0);
end;}
if RGB.B > -1 then rgbBlue := RGB.B;
if RGB.G > -1 then rgbGreen := RGB.G;
if RGB.R > -1 then rgbRed := RGB.R;
end;
inc(scanline);
end;
end;
end;
function TImageFXGDI.TintAdd(R, G , B : Integer) : IImageFX;
var
scanline: PRGBQuad;
y: Integer;
x: Integer;
begin
Result := Self;
Assert(fBitmap.PixelFormat = pf32bit);
for y := 0 to fBitmap.Height - 1 do
begin
scanline := fBitmap.ScanLine[y];
for x := 0 to fBitmap.Width - 1 do
begin
with scanline^ do
begin
{if (rgbBlue = 255) and (rgbGreen = 255) and (rgbRed = 255) then
begin
FillChar(scanline^, sizeof(TRGBQuad), 0);
end;}
rgbBlue := rgbBlue + B;
rgbGreen := rgbGreen + G;
rgbRed := rgbRed + R;
end;
inc(scanline);
end;
end;
end;
function TImageFXGDI.TintBlue : IImageFX;
begin
Result := TintAdd(0,0,-1);
end;
function TImageFXGDI.TintRed : IImageFX;
begin
Result := TintAdd(-1,0,0);
end;
function TImageFXGDI.TintGreen : IImageFX;
begin
Result := TintAdd(0,-1,0);
end;
function TImageFXGDI.Solarize : IImageFX;
begin
Result := TintAdd(255,-1,-1);
end;
function TImageFXGDI.ScanlineH;
begin
Result := Self;
DoScanLines(smHorizontal);
end;
function TImageFXGDI.ScanlineV;
begin
Result := Self;
DoScanLines(smVertical);
end;
procedure TImageFXGDI.DoScanLines(ScanLineMode : TScanlineMode);
type
PPixelRec = ^TPixelRec;
TPixelRec = packed record
B: Byte;
G: Byte;
R: Byte;
Reserved: Byte;
end;
var
X: Integer;
Y: Integer;
Pixel: PPixelRec;
begin
Assert(fBitmap.PixelFormat = pf32Bit);
fBitmap.PixelFormat := pf32bit;
fBitmap.AlphaFormat := afDefined;
for Y := 0 to (fBitmap.Height - 1) do
begin
Pixel := fBitmap.ScanLine[Y];
for X := 0 to (fBitmap.Width - 1) do
begin
{Standard equation}
if ScanLineMode = smHorizontal then
begin
if Odd(y) then
begin
Pixel.R := Round(Pixel.R-((Pixel.R/255)*100));
Pixel.G := Round(Pixel.G-((Pixel.G/255)*100));
Pixel.B := Round(Pixel.B-((Pixel.B/255)*100));
end;
end
else
begin
if Odd(x) then
begin
Pixel.R := Round(Pixel.R-((Pixel.R/255)*100));
Pixel.G := Round(Pixel.G-((Pixel.G/255)*100));
Pixel.B := Round(Pixel.B-((Pixel.B/255)*100));
end;
end;
Inc(Pixel);
end;
end;
end;
procedure TImageFXGDI.SaveToPNG(const outfile : string);
var
Encoder: TGUID;
GPBitmap : TGPBitmap;
begin
GPBitmap.Create(fBitmap.Handle,fBitmap.Palette);
try
// get encoder and encode the output image
if GetEncoderClsid('image/png', Encoder) <> -1 then GPBitmap.Save(outfile, Encoder)
else raise Exception.Create('Failed to get encoder.');
finally
GPBitmap.Free;
end;
end;
function TImageFXGDI.GetPixel(const x, y: Integer): TPixelInfo;
begin
Result := GetPixelImage(x,y,fBitmap);
end;
procedure TImageFXGDI.SetPixel(const x, y: Integer; const P: TPixelInfo);
begin
SetPixelImage(x,y,P,fBitmap);
end;
procedure TImageFXGDI.SaveToStream(stream : TStream; imgFormat : TImageFormat = ifJPG);
var
graf : TGraphic;
begin
stream.Size:= 0;
graf := nil;
try
case imgFormat of
ifBMP: graf := Self.AsBitmap;
ifJPG: graf := Self.AsJPG;
ifPNG: graf := Self.AsPNG;
ifGIF: graf := Self.AsGIF;
else raise Exception.Create('SaveToStream format not supported!');
end;
graf.SaveToStream(stream);
finally
graf.Free;
end;
end;
procedure TImageFXGDI.SetPixelImage(const x, y: Integer; const P: TPixelInfo; bmp : TBitmap);
begin
bmp.Canvas.Pixels[x,y] := RGB(P.R,P.G,P.B);
end;
function TImageFXGDI.GetPixelImage(const x, y: Integer; bmp : TBitmap) : TPixelInfo;
var
lRGB : TRGB;
begin
lRGB := ColorToRGBValues(bmp.Canvas.Pixels[x,y]);
Result.R := lRGB.R;
Result.G := lRGB.G;
Result.B := lRGB.B;
end;
function TImageFXGDI.AsString(imgFormat : TImageFormat = ifJPG) : string;
var
ss : TStringStream;
begin
LastResult := arConversionError;
ss := TStringStream.Create;
try
case imgFormat of
ifBMP : fBitmap.SaveToStream(ss);
ifJPG : Self.AsJPG.SaveToStream(ss);
ifPNG : Self.AsPNG.SaveToStream(ss);
ifGIF : Self.AsGIF.SaveToStream(ss);
else raise Exception.Create('Format unknow!');
end;
Result := Base64Encode(ss.DataString);
LastResult := arOk;
finally
ss.Free;
end;
end;
procedure TImageFXGDI.SaveToJPG(const outfile : string);
var
Encoder: TGUID;
GPBitmap : TGPBitmap;
begin
GPBitmap.Create(fBitmap.Handle,fBitmap.Palette);
try
// get encoder and encode the output image
if GetEncoderClsid('image/jpeg', Encoder) <> -1 then GPBitmap.Save(outfile, Encoder)
else raise Exception.Create('Failed to get encoder.');
finally
GPBitmap.Free;
end;
end;
procedure TImageFXGDI.SaveToBMP(const outfile : string);
var
Encoder: TGUID;
GPBitmap : TGPBitmap;
begin
GPBitmap.Create(fBitmap.Handle,fBitmap.Palette);
try
// get encoder and encode the output image
if GetEncoderClsid('image/bmp', Encoder) <> -1 then GPBitmap.Save(outfile, Encoder)
else raise Exception.Create('Failed to get encoder.');
finally
GPBitmap.Free;
end;
end;
procedure TImageFXGDI.SaveToGIF(const outfile : string);
var
Encoder: TGUID;
GPBitmap : TGPBitmap;
begin
GPBitmap.Create(fBitmap.Handle,fBitmap.Palette);
try
// get encoder and encode the output image
if GetEncoderClsid('image/gif', Encoder) <> -1 then GPBitmap.Save(outfile, Encoder)
else raise Exception.Create('Failed to get encoder.');
finally
GPBitmap.Free;
end;
end;
procedure TImageFXGDI.GPBitmapToBitmap(gpbmp : TGPBitmap; bmp : TBitmap);
var
Graphics : TGPGraphics;
begin
try
bmp.PixelFormat := pf32bit;
bmp.HandleType := bmDIB;
bmp.AlphaFormat := afDefined;
bmp.Width := gpbmp.GetWidth;
bmp.Height := gpbmp.GetHeight;
Graphics := TGPGraphics.Create(bmp.Canvas.Handle);
Graphics.Clear(ColorRefToARGB(ColorToRGB(clNone)));
// set the composition mode to copy
Graphics.SetCompositingMode(CompositingModeSourceCopy);
// set high quality rendering modes
Graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
Graphics.SetPixelOffsetMode(PixelOffsetModeHighQuality);
Graphics.SetSmoothingMode(SmoothingModeHighQuality);
// draw the input image on the output in modified size
Graphics.DrawImage(gpbmp,0,0,gpbmp.GetWidth,gpbmp.GetHeight);
finally
Graphics.Free;
end;
end;
function TImageFXGDI.AsPNG2 : TPngImage;
var
png : TPngImage;
bmp : TBitmap;