-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUGraphic.pas
More file actions
1417 lines (1244 loc) · 39.3 KB
/
Copy pathUGraphic.pas
File metadata and controls
1417 lines (1244 loc) · 39.3 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
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
// Global Graphic Functions
// Date 21.11.19
// 03.01.09 nk add support for png file format (Portable Network Graphics)
// 21.12.09 nk opt expand image color from Integer to Int64 (32bit > MaxInt)
// 10.11.10 nk add fmShareDenyNone in read-only file mode
// 12.11.10 nk upd to Delphi XE (2011) Unicode (most parts remain ANSI)
// 12.12.10 nk add ThumbnailBitmap makes a thumbnail preview with given size
// 08.12.11 nk add GetBitmapSize and IsBitmapTiled
// 14.01.12 nk add GetContrastColor to define the best contrasted (readable) color
// 17.01.12 nk add ResizeBitmap to rescale a given bitmap to a new size
// 29.07.12 nk add support for .gif and .png image file formats / include GifImg and PngImage
// 29.07.12 nk add LoadGraphicFile to load different graphic files as bitmap
// 26.05.14 nk upd to Delphi XE3 (VER240 Version 24)
// 25.05.16 nk add GetHeatColor for improved coloring modes for heatmaps
// 01.02.17 nk add CropBitmap and CropBitmaps to cut out desired rectangle from bitmap
// 21.11.19 nk add GrayscaleBitmap to convert colored bitmap to grayscale
unit UGraphic;
interface
uses //XE3//26.05.14 nk add System.UITypes
Windows, Messages, SysUtils, Classes, Forms, StdCtrls, Graphics, GraphUtil,
Math, Jpeg, GifImg, PngImage, TypInfo, Variants, ExtDlgs, ShellApi, UITypes,
UGlobal, USystem;
const
RGB_BIT = 8;
RGB_HALF = 127;
RGB_FULL = 255;
RGB_GAMMA = 0.80;
MODE_CONTRAST = -6; //V5//25.05.16 nk add
MODE_LAVA = -15; //V5//25.05.16 nk add
MODE_AQUA = -16; //V5//23.06.16 nk add
MODE_BIPOLAR = -21; //V5//25.05.16 nk add
JPG_BIT = 24;
JPG_RES = 16777216;
GIF_BIT = 8;
GIF_RES = 256;
GIF_HEADER = 'GIF';
GIF_VER87 = 'GIF87A';
GIF_VER89 = 'GIF89A';
JPG_PARAM = [$01, $D0, $D1, $D2, $D3, $D4, $D5, $D6, $D7];
JPG_VALID: array[0..1] of Byte = ($FF, $D8);
PNG_VALID: array[0..7] of Byte = (137, 80, 78, 71, 13, 10, 26, 10); //03.01.09 nk add
TILE_SIZES: array[0..8] of Word = (8, 16, 32, 64, 128, 256, 512, 1024, 2048); //08.12.11 nk add
//03.01.09 nk add ff - avaliable color modes for PNG
PNG_GRAYSCALE = 0;
PNG_RGB = 2;
PNG_PALETTE = 3;
PNG_GRAYSCALEALPHA = 4;
PNG_RGBALPHA = 6;
type
TColArray = array[0..2] of Byte;
TPngSig = array[0..7] of Byte; //03.01.09 nk add
TImageSize = record //03.08.08 nk add
Width: Integer;
Height: Integer;
Colors: Int64; //21.12.09 nk old=Integer - may result in RangeCheckError
Bits: Integer; //03.01.09 nk add //if 32bit color=2^32 > MaxInt!
end;
TGifHead = record //12.11.10 nk old=TGifHeader (conflict with UGif) / 03.08.08 nk add
Signature: array [0..5] of AnsiChar; //xe//
Width, Height: Word;
end;
TMotorolaWord = record //03.08.08 nk add
case Byte of
0: (Value: Word);
1: (Byte1, Byte2: Byte);
end;
TRGB24 = packed record //12.12.10 nk add ff
B: Byte;
G: Byte;
R: Byte;
end;
PRGB24 = ^TRGB24;
procedure SetContrast(Image: TBitmap; Value: Integer);
procedure SetLightness(Image: TBitmap; Value: Integer);
procedure Sharpen(sbm, tbm: TBitmap; alpha: Single); //nk//not tested!
procedure SetTint(Image: TBitmap; White, Black: Byte);
procedure SetColor(Image: TBitmap; Red, Green, Blue: Byte);
procedure SetGradient(Canvas: TCanvas; Hdir: Boolean; Colors: array of TColor);
procedure FlipHorizontal(Source, Target: TBitmap);
procedure FlipVertical(Source, Target: TBitmap);
procedure CopyBitmap(Source, Target: TBitmap); //17.12.09 nk add
procedure ResizeBitmap(var Image: TBitmap; Dx, Dy: Integer); //17.01.12 nk add
procedure CropBitmap(Bitmap: TBitmap; X, Y, W, H: Integer); //01.02.17 nk add
procedure CropBitmaps(InBitmap, OutBitmap: TBitmap; X, Y, W, H: Integer); //01.02.17 nk add
procedure TransparentBitmap(Source, Target: TBitmap; Color: TColor); //31.12.07 nk add
procedure AntialiasBitmap(Bitmap: TBitmap; Percent: Integer); //31.01.10 nk add
procedure ThumbnailBitmap(Source, Target: TBitmap; SizeX, SizeY: Word); //12.12.10 nk add
function InvertBitmap(Bitmap: TBitmap): TBitmap;
function GrayscaleBitmap(Bitmap: TBitmap): TBitmap; //21.11.19 nk add
function ChangeColor(Bitmap: TBitmap; ColOld, ColNew: TColor): TBitmap; //14.04.10 nk add
function IsPngFormat(PngFile: string): Boolean; //19.10.09 nk add
function IsGifFormat(GifFile: string): Boolean; //20.11.10 nk add
function IsBitmapTiled(BitmapFile: string): Boolean; //08.12.11 nk add
function GetInvWord(Stream: TFileStream): Word; //03.08.08 nk add
function GetBitmapFormat(Bitmap: TBitmap): Integer;
function GetWaveColor(Wave: Word): TColor; //05.08.09 nk add
function GetGradientColor(GradPos, GradMax: Integer; MinColor, MaxColor: TColor): TColor; //12.04.11 nk add
function GetHeatColor(GradPos, GradMax, Mode: Integer): TColor; //V5//25.05.16 nk add
function GetInverseColor(Color: TColor): TColor; //03.02.10 nk add
function GetContrastColor(Color: TColor): TColor; //14.01.12 nk add
function GetBrighterColor(Color: TColor; Percent: Byte): TColor;
function GetDarkerColor(Color: TColor; Percent: Byte): TColor;
function GetMixedColor(Color1, Color2: TColor; Blend: Real): TColor; //31.01.10 nk add
function GetBitmapSize(BitmapFile: string): TImageSize; //07.12.11 nk add
function GetBmpSize(BmpFile: string): TImageSize; //03.08.08 nk add ff
function GetGifSize(GifFile: string): TImageSize;
function GetJpgSize(JpgFile: string): TImageSize;
function GetPngSize(PngFile: string): TImageSize; //03.01.09 nk add ff
function GetPngColors(ColorType, BitDepth: Byte): Integer;
function GetRgbColors(Color: TColor; var R, G, B: Byte): Integer; //12.12.08 nk add
function LoadGraphicFile(const FileName: string): TBitmap; //29.07.12 nk add
implementation
procedure SetGradient(Canvas: TCanvas; Hdir: Boolean; Colors: array of TColor);
var
x, y, z: Integer;
wid, cnum, dim: Integer;
pos, pto, sto: Integer;
mult: Double;
rect: TRect;
col: TColor;
pen: TPenStyle;
a: TColArray;
b: array of TColArray;
begin
rect := Canvas.ClipRect;
cnum := High(Colors);
if cnum > 0 then begin
if Hdir then //horizontal
dim := rect.Right - rect.Left
else //vertical
dim := rect.Bottom - rect.Top;
SetLength(b, cnum + 1);
for x := 0 to cnum do begin
Colors[x] := ColorToRGB(Colors[x]);
b[x][0] := GetRvalue(Colors[x]);
b[x][1] := GetGvalue(Colors[x]);
b[x][2] := GetBvalue(Colors[x]);
end;
wid := Canvas.Pen.Width;
pen := Canvas.Pen.Style;
col := Canvas.Pen.Color;
Canvas.Pen.Width := 1;
Canvas.Pen.Style := psSolid;
sto := Round(dim / cnum);
for y := 0 to cnum - 1 do begin
if y = cnum - 1 then
pto := dim - y * sto - 1
else
pto := sto;
for x := 0 to pto do begin
pos := x + y * sto;
mult := x / pto;
for z := 0 to 2 do
a[z] := Trunc(b[y][z] + ((b[y + 1][z] - b[y][z]) * mult));
Canvas.Pen.Color := RGB(a[0], a[1], a[2]);
if Hdir then begin //horizontal
Canvas.MoveTo(rect.Left + pos, rect.Top);
Canvas.LineTo(rect.Left + pos, rect.Bottom);
end else begin //vertical
Canvas.MoveTo(rect.Left, rect.Top + pos);
Canvas.LineTo(rect.Right, rect.Top + pos);
end;
end;
end;
b := nil;
Canvas.Pen.Width := wid;
Canvas.Pen.Style := pen;
Canvas.Pen.Color := col;
end;
end;
procedure SetContrast(Image: TBitmap; Value: Integer);
var
p: PByteArray;
r, g, b, x, y: Integer;
rg, gg, bg: Integer;
begin
for y := 0 to Image.Height - 1 do begin
p := Image.ScanLine[y];
for x := 0 to Image.Width - 1 do begin
r := p[x * 3];
g := p[x * 3 + 1];
b := p[x * 3 + 2];
rg := (Abs(RGB_HALF - r) * Value) div RGB_FULL;
gg := (Abs(RGB_HALF - g) * Value) div RGB_FULL;
bg := (Abs(RGB_HALF - b) * Value) div RGB_FULL;
if r > RGB_HALF then r := r + rg else r := r - rg;
if g > RGB_HALF then g := g + gg else g := g - gg;
if b > RGB_HALF then b := b + bg else b := b - bg;
p[x * 3 ] := IntToByte(r);
p[x * 3 + 1] := IntToByte(g);
p[x * 3 + 2] := IntToByte(b);
end;
end;
end;
procedure SetLightness(Image: TBitmap; Value: Integer);
var
p: PByteArray;
r, g, b, x, y: Integer;
begin
for y := 0 to Image.Height - 1 do begin
p := Image.ScanLine[y];
for x := 0 to Image.Width - 1 do begin
r := p[x * 3];
g := p[x * 3 + 1];
b := p[x * 3 + 2];
p[x * 3] := IntToByte(r + ((RGB_FULL - r) * Value) div RGB_FULL);
p[x * 3 + 1] := IntToByte(g + ((RGB_FULL - g) * Value) div RGB_FULL);
p[x * 3 + 2] := IntToByte(b + ((RGB_FULL - b) * Value) div RGB_FULL);
end;
end;
end;
procedure Sharpen(sbm, tbm: TBitmap; alpha: Single);
//to sharpen, alpha must be >1.
//pixelformat pf24bit
//sharpens sbm to tbm
var
i, j, k: integer;
sr: array[0..2] of PByte;
st: array[0..4] of pRGBTriple;
tr: PByte;
tt, p: pRGBTriple;
beta: Single;
inta, intb: integer;
bmh, bmw: integer;
re, gr, bl: integer;
BytesPerScanline: integer;
begin
//sharpening is blending of the current pixel
//with the average of the surrounding ones,
//but with a negative weight for the average
Assert((sbm.Width > 2) and (sbm.Height > 2), 'Bitmap must be at least 3x3');
Assert((alpha > 1) and (alpha < 6), 'Alpha must be >1 and <6');
beta := (alpha - 1) / 5; //we assume alpha>1 and beta<1
intb := round(beta * $10000);
inta := round(alpha * $10000); //integer scaled alpha and beta
sbm.PixelFormat := pf24bit;
tbm.PixelFormat := pf24bit;
tbm.Width := sbm.Width;
tbm.Height := sbm.Height;
bmw := sbm.Width - 2;
bmh := sbm.Height - 2;
BytesPerScanline := (((bmw + 2) * 24 + 31) and not 31) div 8;
tr := tbm.Scanline[0];
tt := pRGBTriple(tr);
sr[0] := sbm.Scanline[0];
st[0] := pRGBTriple(sr[0]);
for j := 0 to bmw + 1 do
begin
tt^ := st[0]^;
inc(tt); inc(st[0]); //first row unchanged
end;
sr[1] := PByte(integer(sr[0]) - BytesPerScanline);
sr[2] := PByte(integer(sr[1]) - BytesPerScanline);
for i := 1 to bmh do
begin
Dec(tr, BytesPerScanline);
tt := pRGBTriple(tr);
st[0] := pRGBTriple(integer(sr[0]) + 3); //top
st[1] := pRGBTriple(sr[1]); //left
st[2] := pRGBTriple(integer(sr[1]) + 3); //center
st[3] := pRGBTriple(integer(sr[1]) + 6); //right
st[4] := pRGBTriple(integer(sr[2]) + 3); //bottom
tt^ := st[1]^; //1st col unchanged
for j := 1 to bmw do begin
//calcutate average weighted by -beta
re := 0; gr := 0; bl := 0;
for k := 0 to 4 do
begin
re := re + st[k]^.rgbtRed;
gr := gr + st[k]^.rgbtGreen;
bl := bl + st[k]^.rgbtBlue;
inc(st[k]);
end;
re := (intb * re + $7FFF) shr 16;
gr := (intb * gr + $7FFF) shr 16;
bl := (intb * bl + $7FFF) shr 16;
//add center pixel weighted by alpha
p := pRGBTriple(st[1]); //after inc, st[1] is at center
re := (inta * p^.rgbtRed + $7FFF) shr 16 - re;
gr := (inta * p^.rgbtGreen + $7FFF) shr 16 - gr;
bl := (inta * p^.rgbtBlue + $7FFF) shr 16 - bl;
//clamp and move into target pixel
inc(tt);
if re < 0 then
re := 0
else
if re > 255 then
re := 255;
if gr < 0 then
gr := 0
else
if gr > 255 then
gr := 255;
if bl < 0 then
bl := 0
else
if bl > 255 then
bl := 255;
//this looks stupid, but avoids function calls
tt^.rgbtRed := re;
tt^.rgbtGreen := gr;
tt^.rgbtBlue := bl;
end;
inc(tt);
inc(st[1]);
tt^ := st[1]^; //Last col unchanged
sr[0] := sr[1];
sr[1] := sr[2];
Dec(sr[2], BytesPerScanline);
end;
// copy last row
Dec(tr, BytesPerScanline);
tt := pRGBTriple(tr);
st[1] := pRGBTriple(sr[1]);
for j := 0 to bmw + 1 do
begin
tt^ := st[1]^;
inc(tt); inc(st[1]);
end;
end;
procedure CropBitmap(Bitmap: TBitmap; X, Y, W, H: Integer);
begin //01.02.17 nk add
BitBlt(Bitmap.Canvas.Handle, 0, 0, W, H, Bitmap.Canvas.Handle, X, Y, SRCCOPY);
Bitmap.Width := W;
Bitmap.Height := H;
end;
procedure CropBitmaps(InBitmap, OutBitmap: TBitmap; X, Y, W, H: Integer);
begin //01.02.17 nk add
OutBitmap.PixelFormat := InBitmap.PixelFormat;
OutBitmap.Width := W;
OutBitmap.Height := H;
BitBlt(OutBitmap.Canvas.Handle, 0, 0, W, H, InBitmap.Canvas.Handle, X, Y, SRCCOPY);
end;
procedure ResizeBitmap(var Image: TBitmap; Dx, Dy: Integer);
var //17.01.12 nk add - rescale Image to new width Dx and height Dy
new: TRect;
bmp: TBitmap;
begin
try
bmp := TBitmap.Create;
try
new := Rect(0, 0, Dx, Dy);
bmp.Width := Dx;
bmp.Height := Dy;
bmp.Canvas.StretchDraw(new, Image);
Image.Assign(bmp);
finally
bmp.Free;
end;
except
//ignore
end;
end;
procedure TransparentBitmap(Source, Target: TBitmap; Color: TColor);
begin //change the transparent color of a bitmap //nk//NOT TESTED
Source.Transparent := True;
Source.TransparentMode := tmFixed;
Source.TransparentColor := Color;
Target.Canvas.Draw(0, 0, Source);
end;
procedure CopyBitmap(Source, Target: TBitmap);
begin //17.12.09 nk add - NOT yet tested!
with Target do begin
Width := Source.Width;
Height:= Source.Height;
StretchBlt(Canvas.Handle, 0, 0, Width, Height, Source.Canvas.Handle, 0, 0, Source.Width, Source.Height, SRCCOPY);
end;
end;
procedure FlipHorizontal(Source, Target: TBitmap);
begin
with Target do begin
Width := Source.Width;
Height := Source.Height;
StretchBlt(Canvas.Handle, 0, 0, Width, Height, Source.Canvas.Handle,
Source.Width, 0, -Source.Width, Source.Height, SRCCOPY);
end;
end;
procedure FlipVertical(Source, Target: TBitmap);
begin
with Target do begin
Width := Source.Width;
Height := Source.Height;
StretchBlt(Canvas.Handle, 0, 0, Width, Height, Source.Canvas.Handle,
0, Source.Height, Source.Width, -Source.Height, SRCCOPY);
end;
end;
procedure SetTint(Image: TBitmap; White, Black: Byte);
var
x, y: Integer;
p: PByteArray;
begin
Image.PixelFormat := pf24Bit;
for y := 0 to Image.Height - 1 do begin
p := Image.ScanLine[y];
for x := 0 to Image.Width * 3 - 1 do begin
if (p[x] = RGB_FULL) then p[x] := 12 * White div 10 + 135; // white dots 147..255
if (p[x] = 0) then p[x] := 12 * (10 - Black div 10); // black dots 0..108
end;
end;
end;
procedure SetColor(Image: TBitmap; Red, Green, Blue: Byte);
var
r, g, b: Byte;
x, y: Integer;
col: TColor;
begin
for y := 0 to Image.Height do begin
for x := 0 to Image.Width do begin
col := Image.Canvas.Pixels[x, y];
r := GetRValue(col) + Red;
g := GetGValue(col) + Green;
b := GetBValue(col) + Blue;
Image.Canvas.Pixels[x, y] := RGB(r, g, b);
end;
end;
end;
function IsPngFormat(PngFile: string): Boolean;
var //20.11.10 nk opt - return True if PngFile is a PNG image file
x: Integer;
ps: TPngSig;
fs: TFileStream;
begin
Result := False;
if PngFile = cEMPTY then Exit;
//20.11.10 nk mov down FillChar(ps, SizeOf(ps), cNUL);
fs := TFileStream.Create(PngFile, fmOpenRead or fmShareDenyNone); //10.11.10 nk add fmShareDenyNone
try
FillChar(ps, SizeOf(ps), cNUL);
fs.read(ps[0], SizeOf(ps));
for x := Low(ps) to High(ps) do
if ps[x] <> PNG_VALID[x] then Exit;
Result := True;
finally
fs.Free;
end;
end;
function IsGifFormat(GifFile: string): Boolean;
var //20.11.10 nk add - return True if GifFile is a GIF image file
gh: TGifHead;
fs: TFileStream;
begin
Result := False;
if GifFile = cEMPTY then Exit;
fs := TFileStream.Create(GifFile, fmOpenRead or fmShareDenyNone);
try
FillChar(gh, SizeOf(TGifHead), cNULL);
with fs do begin
Seek(0, soFromBeginning);
ReadBuffer(gh, SizeOf(TGifHead));
end;
Result := (Pos(GIF_HEADER, string(gh.Signature)) = 1);
finally
fs.Free;
end;
end;
function InvertBitmap(Bitmap: TBitmap): TBitmap;
var //fast function to invert the colors of a bitmap
x, y: Integer;
p: PByteArray;
begin
with Bitmap do begin
PixelFormat := pf24Bit;
for y := 0 to Height - 1 do begin
p := ScanLine[y];
for x := 0 to Width * 3 - 1 do p[x] := RGB_FULL - p[x];
end;
end;
Result := Bitmap;
end;
function GrayscaleBitmap(Bitmap: TBitmap): TBitmap;
var //21.11.19 nk add - convert colored Bitmap to grayscale
gray, r, g, b: Byte;
x, y: Integer;
cpix: Longint;
begin
with Bitmap do begin
for x := 0 to Width - 1 do begin
for y := 0 to Height - 1 do begin
cpix := ColorToRGB(Canvas.Pixels[x, y]);
r := cpix;
g := cpix shr 8;
b := cpix shr 16;
gray := Round(0.3 * r + 0.6 * g + 0.1 * b);
Canvas.Pixels[x, y] := RGB(gray, gray, gray);
end;
end;
end;
Result := Bitmap;
end;
function ChangeColor(Bitmap: TBitmap; ColOld, ColNew: TColor): TBitmap;
var //14.04.10 nk add
x, y: Longint;
bmp: TBitmap;
col: TColor;
begin
bmp := TBitmap.Create;
with bmp do begin
PixelFormat := pf24bit;
Width := Bitmap.Width;
Height := Bitmap.Height;
end;
for x := 0 to Bitmap.Width - 1 do begin
for y := 0 to Bitmap.Height - 1 do begin
col := Bitmap.Canvas.Pixels[x, y];
if col = ColOld then //replace pixel color
col := ColNew;
bmp.Canvas.Pixels[x, y] := col;
end;
end;
Result := bmp; //do NOT free bitmap here
end;
procedure AntialiasBitmap(Bitmap: TBitmap; Percent: Integer);
var //31.01.10 nk add (Source http://www.delphipraxis.net)
r1, r2, g1, g2, b1, b2: Byte;
l, p: Integer;
r, g, b: Integer;
area: TRect;
pix, prevscan, nextscan, hpix: ^TColArray;
begin
area := Rect(0, 0, Bitmap.Width, Bitmap.Height);
Bitmap.PixelFormat := pf24bit;
with Bitmap.Canvas do begin
Brush.Style := bsClear;
for l := area.Top to area.Bottom - 1 do begin
pix := Bitmap.ScanLine[l];
if l <> area.Top then
prevscan := Bitmap.ScanLine[l - 1]
else
prevscan := nil;
if l <> area.Bottom - 1 then
nextscan := Bitmap.ScanLine[l + 1]
else
nextscan := nil;
for p := area.Left to area.Right - 1 do begin
r1 := pix^[2];
g1 := pix^[1];
b1 := pix^[0];
if p <> area.Left then begin //pixel left
hpix := pix;
Dec(hpix);
r2 := hpix^[2];
g2 := hpix^[1];
b2 := hpix^[0];
if (r1 <> r2) or (g1 <> g2) or (b1 <> b2) then begin
r := r1 + (r2 - r1) * HALF div (Percent + HALF);
g := g1 + (g2 - g1) * HALF div (Percent + HALF);
b := b1 + (b2 - b1) * HALF div (Percent + HALF);
hpix^[2] := r;
hpix^[1] := g;
hpix^[0] := b;
end;
end;
if p <> area.Right - 1 then begin //pixel right
hpix := pix;
Inc(hpix);
r2 := hpix^[2];
g2 := hpix^[1];
b2 := hpix^[0];
if (r1 <> r2) or (g1 <> g2) or (b1 <> b2) then begin
r := r1 + (r2 - r1) * HALF div (Percent + HALF);
g := g1 + (g2 - g1) * HALF div (Percent + HALF);
b := b1 + (b2 - b1) * HALF div (Percent + HALF);
hpix^[2] := r;
hpix^[1] := g;
hpix^[0] := b;
end;
end;
if prevscan <> nil then begin //pixel up
r2 := prevscan^[2];
g2 := prevscan^[1];
b2 := prevscan^[0];
if (r1 <> r2) or (g1 <> g2) or (b1 <> b2) then begin
r := r1 + (r2 - r1) * HALF div (Percent + HALF);
g := g1 + (g2 - g1) * HALF div (Percent + HALF);
b := b1 + (b2 - b1) * HALF div (Percent + HALF);
prevscan^[2] := r;
prevscan^[1] := g;
prevscan^[0] := b;
end;
Inc(prevscan);
end;
if nextscan <> nil then begin //pixel down
r2 := nextscan^[2];
g2 := nextscan^[1];
b2 := nextscan^[0];
if (r1 <> r2) or (g1 <> g2) or (b1 <> b2) then begin
r := r1 + (r2 - r1) * HALF div (Percent + HALF);
g := g1 + (g2 - g1) * HALF div (Percent + HALF);
b := b1 + (b2 - b1) * HALF div (Percent + HALF);
nextscan^[2] := r;
nextscan^[1] := g;
nextscan^[0] := b;
end;
Inc(nextscan);
end;
Inc(pix);
end;
end;
end;
end;
procedure ThumbnailBitmap(Source, Target: TBitmap; SizeX, SizeY: Word);
var //V5//20.03.16 nk opt (Source http://www.swissdelphicenter.ch)
r, g, b: Integer;
h, w, x, y, ix, iy: Integer;
x1, x2, x3, ny1, ny2, ny3: Integer;
iSrc, iDst, s1: Integer;
dx, dy: Integer;
rb, rd, rs: Integer;
iRed, iGrn, iBlu, iRatio: Longword;
xscale, yscale: Single;
c1, c2, c3, c4, c5: TRGB24;
pt, pt1: PRGB24;
lutX, lutY: array of Integer;
begin
if Source.PixelFormat <> pf24bit then Source.PixelFormat := pf24bit;
if Target.PixelFormat <> pf24bit then Target.PixelFormat := pf24bit;
Target.Width := SizeX;
Target.Height := SizeY;
w := SizeX;
h := SizeY;
if (Source.Width <= SizeX) and (Source.Height <= SizeY) then begin
Target.Assign(Source);
Exit;
end;
iDst := (w * 24 + 31) and not 31;
iDst := iDst div RGB_BIT; //BytesPerScanline
iSrc := (Source.Width * 24 + 31) and not 31;
iSrc := iSrc div RGB_BIT;
xscale := 1.0 / (w / Source.Width);
yscale := 1.0 / (h / Source.Height);
//create x lookup table
SetLength(lutX, w);
x1 := 0;
x2 := Trunc(xscale);
for x := 0 to w - 1 do begin
lutX[x] := x2 - x1;
x1 := x2;
x2 := Trunc((x + 2) * xscale);
end;
//create y lookup table
SetLength(lutY, h);
x1 := 0;
x2 := Trunc(yscale);
for x := 0 to h - 1 do begin
lutY[x] := x2 - x1;
x1 := x2;
x2 := Trunc((x + 2) * yscale);
end;
dec(w);
dec(h);
rd := Integer(Target.ScanLine[0]);
rb := Integer(Source.ScanLine[0]);
rs := rb;
for y := 0 to h do begin
dy := lutY[y];
x1 := 0;
x3 := 0;
for x := 0 to w do begin
dx := lutX[x];
iRed := 0;
iGrn := 0;
iBlu := 0;
rs := rb;
for iy := 1 to dy do begin
pt := PRGB24(rs + x1);
for ix := 1 to dx do begin
iRed := iRed + pt.R;
iGrn := iGrn + pt.G;
iBlu := iBlu + pt.B;
Inc(pt);
end;
rs := rs - iSrc;
end;
iRatio := MAXWORD div (dx * dy);
pt1 := PRGB24(rd + x3);
pt1.R := (iRed * iRatio) shr 16;
pt1.G := (iGrn * iRatio) shr 16;
pt1.B := (iBlu * iRatio) shr 16;
x1 := x1 + 3 * dx;
Inc(x3, 3);
end;
rd := rd - iDst;
rb := rs;
end;
if Target.Height < 3 then Exit;
//anti-alias...
s1 := Integer(Target.ScanLine[0]);
iDst := Integer(Target.ScanLine[1]) - s1;
ny1 := Integer(s1);
ny2 := ny1 + iDst;
ny3 := ny2 + iDst;
for y := 1 to Target.Height - 2 do begin
for x := 0 to Target.Width - 3 do begin
x1 := x * 3;
x2 := x1 + 3;
x3 := x1 + 6;
c1 := PRGB24(ny1 + x1)^;
c2 := PRGB24(ny1 + x3)^;
c3 := PRGB24(ny2 + x2)^;
c4 := PRGB24(ny3 + x1)^;
c5 := PRGB24(ny3 + x3)^;
r := (c1.R + c2.R + (c3.R * -12) + c4.R + c5.R) div -8;
g := (c1.G + c2.G + (c3.G * -12) + c4.G + c5.G) div -8;
b := (c1.B + c2.B + (c3.B * -12) + c4.B + c5.B) div -8;
if r < 0 then r := 0 else if r > RGB_FULL then r := RGB_FULL;
if g < 0 then g := 0 else if g > RGB_FULL then g := RGB_FULL;
if b < 0 then b := 0 else if b > RGB_FULL then b := RGB_FULL;
pt1 := PRGB24(ny2 + x2);
pt1.R := r;
pt1.G := g;
pt1.B := b;
end;
Inc(ny1, iDst);
Inc(ny2, iDst);
Inc(ny3, iDst);
end;
end;
function GetInvWord(Stream: TFileStream): Word;
var //invert Motorola to Intel byte order
mw: TMotorolaWord;
begin
Result := 0;
if Stream = nil then Exit;
Stream.Read(mw.Byte2, SizeOf(Byte));
Stream.Read(mw.Byte1, SizeOf(Byte));
Result := mw.Value;
end;
function GetBitmapFormat(Bitmap: TBitmap): Integer;
begin //get pixel format (color depth) [bit] of a bitmap
case Bitmap.PixelFormat of
pf1bit: Result := 1;
pf4bit: Result := 4;
pf8bit: Result := 8;
pf15bit: Result := 15;
pf16bit: Result := 16;
pf24bit: Result := 24;
pf32bit: Result := 32;
else
Result := CLEAR;
end;
end;
function GetBrighterColor(Color: TColor; Percent: Byte): TColor;
begin //Percent 0..100%
if Color = clBlack then //12.05.09 nk opt
Result := clSilver
else
Result := ColorAdjustLuma(ColorToRGB(Color), Percent, False);
end;
function GetDarkerColor(Color: TColor; Percent: Byte): TColor;
begin //Percent 0..100%
if Color = clWhite then //12.05.09 nk opt
Result := clSilver
else
Result := ColorAdjustLuma(ColorToRGB(Color), -Percent, False);
end;
function GetMixedColor(Color1, Color2: TColor; Blend: Real): TColor;
var //31.01.10 nk add Blend 0.0..1.0 (Source http://www.delphipraxis.net)
r, g, b: Byte; //nk//not yet tested !
y1, y2: Byte;
begin
Color1 := ColorToRGB(Color1);
Color2 := ColorToRGB(Color2);
y1 := GetRValue(Color1);
y2 := GetRValue(Color2);
r := Round(y1 + (y2 - y1) * Blend);
y1 := GetGValue(Color1);
y2 := GetGValue(Color2);
g := Round(y1 + (y2 - y1) * Blend);
y1 := GetBValue(Color1);
y2 := GetBValue(Color2);
b := Round(y1 + (y2 - y1) * Blend);
Result := RGB(r, g, b);
end;
function IsBitmapTiled(BitmapFile: string): Boolean;
var //08.12.11 nk add - return True if Bitmap can be tiled
i: Integer; //to tile a bitmap, width and height must be equal
bsize: TImageSize; //and must be a multiple of 8 (e.g. 512x512 pixels)
begin
Result := False;
bsize := GetBitmapSize(BitmapFile);
if bsize.Width = bsize.Height then begin
for i := 0 to High(TILE_SIZES) do begin
if bsize.Width = TILE_SIZES[i] then begin
Result := True;
Exit;
end;
end;
end;
end;
function GetBitmapSize(BitmapFile: string): TImageSize;
var //07.12.11 nk add - return size of requested bitmap
ext: string;
begin
Result.Width := NONE;
Result.Height := NONE;
Result.Colors := NONE;
Result.Bits := NONE;
ext := LowerCase(ExtractFileExt(BitmapFile));
if ext = BMP_END then begin
Result := GetBmpSize(BitmapFile);
Exit;
end;
if ext = GIF_END then begin
Result := GetGifSize(BitmapFile);
Exit;
end;
if ext = JPG_END then begin
Result := GetJpgSize(BitmapFile);
Exit;
end;
if ext = PNG_END then begin
Result := GetPngSize(BitmapFile);
Exit;
end;
end;
function GetBmpSize(BmpFile: string): TImageSize;
var //10.11.10 nk opt
fs: TFileStream;
ih: TBitmapInfoHeader;
fh: TBitmapFileHeader;
begin
Result.Width := NONE;
Result.Height := NONE;
Result.Colors := NONE;
Result.Bits := NONE;
if BmpFile = cEMPTY then Exit;
fs := TFileStream.Create(BmpFile, fmOpenRead or fmShareDenyNone); //10.11.10 nk add fmShareDenyNone
try
fs.Read(fh, SizeOf(fh)); //1st we have to read the file header
fs.Read(ih, SizeOf(ih)); //else the info header is corrupted
Result.Width := ih.biWidth;
Result.Height := ih.biHeight;
Result.Bits := ih.biBitCount;
Result.Colors := Round(Power(2, ih.biBitCount));
finally
fs.Free;
end;
end;
function GetGifSize(GifFile: string): TImageSize;
var //12.11.10 nk opt - returm width and height in [pixels] of GIF image file
gh: TGifHead;
fs: TFileStream;
begin
Result.Width := NONE;
Result.Height := NONE;
Result.Colors := GIF_RES; //03.01.09 nk opt ff
Result.Bits := GIF_BIT;
if GifFile = cEMPTY then Exit;
fs := TFileStream.Create(GifFile, fmOpenRead or fmShareDenyNone); //10.11.10 nk add fmShareDenyNone
try
FillChar(gh, SizeOf(TGifHead), cNULL);
with fs do begin
Seek(0, soFromBeginning);
ReadBuffer(gh, SizeOf(TGifHead));
end;
if Pos(GIF_HEADER, string(gh.Signature)) = 1 then begin //xe//
Result.Width := gh.Width;
Result.Height := gh.Height;
end;
finally
fs.Free;
end;
end;
function GetJpgSize(JpgFile: string): TImageSize;
var //10.11.10 nk opt - returm width and height in [pixels] of JPG image file
seg: Byte;
len: Word;
x: Integer;
rlen: LongInt;
sig: array[0..1] of Byte;
dum: array[0..15] of Byte;
fs: TFileStream;
begin
Result.Width := NONE;
Result.Height := NONE;
Result.Colors := JPG_RES; //03.01.09 nk opt ff
Result.Bits := JPG_BIT;