forked from mike-lischke/GraphicEx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicColor.pas
5090 lines (4603 loc) · 170 KB
/
GraphicColor.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
unit GraphicColor;
// The original code is GraphicCompression.pas, released November 1, 1999.
//
// The initial developer of the original code is Mike Lischke (www.soft-gems.net),
//
// Copyright (C) 1999-2003 Mike Lischke. All Rights Reserved.
//----------------------------------------------------------------------------------------------------------------------
//
// This file is part of the image library GraphicEx.
//
// GraphicColor contains the implementation of the color conversion manager.
// This class is responsible for converting between these color schemes/formats:
// - RGB(A)
// - BGR(A)
// - CMY(K)
// - CIE L*a*b*
// - PhotoYCC, standard YCbCr
// - indexed
// - grayscale (with alpha, which is ignored currently)
//
// Additional tasks are:
// - conversions between bit depths (1, 2, 4, 8, 16 bits)
// - palette creation
// - gamma tables creation and application
// - masked pixel transfer for interlaced images
// - big endian swap
// - plane (planar) -> interleaved (interlaced) conversion
//
// Notes:
// - Throughout the entire unit I used the terms BPS and SPP for "bits per sample" and
// "samples per pixel", respectively. A sample is one component per pixel. For indexed color schemes
// there's only 1 sample per pixel, for RGB there are 3 (red, green and blue) and so on.
// - The bit depth of multi sample formats like RGB must be equal for each color component.
// - Because of the large amount of possible combinations (color schemes, sample depth, gamma, byte swap)
// I limited the accepted combinations to pratical ones. This leaves currently out:
// + gamma correction for 16 bit values
// + conversion to 16 bit (target) grayscale with alpha
// + samples sizes less than 8 bits for multi-sample schemes (RGB etc.)
// + indexed schemes with planes (e.g. 16 colors indexed as 4 planes each with one bit per sample)
// - For now there is no conversion between indexed and non-indexed formats. Also between grayscale
// and any other scheme is no conversion possible.
//
//----------------------------------------------------------------------------------------------------------------------
interface
{$Include GraphicConfiguration.inc}
{$Include Compilers.inc}
{$ifdef COMPILER_7_UP}
// For some things to work we need code, which is classified as being unsafe for .NET.
// We switch off warnings about that fact. We know it and we accept it.
{$warn UNSAFE_TYPE off}
{$warn UNSAFE_CAST off}
{$warn UNSAFE_CODE off}
{$endif COMPILER_7_UP}
uses
Windows, Graphics, GraphicStrings;
const
// This is the value for average CRT monitors, adjust it if your monitor differs.
DefaultDisplayGamma = 2.2;
type
PCMYK = ^TCMYK;
TCMYK = packed record
C, M, Y, K: Byte;
end;
PCMYK16 = ^TCMYK16;
TCMYK16 = packed record
C, M, Y, K: Word;
end;
PCMY = ^TCMY;
TCMY = packed record
C, M, Y: Byte;
end;
PCMY16 = ^TCMY16;
TCMY16 = packed record
C, M, Y: Word;
end;
PRGB = ^TRGB;
TRGB = packed record
R, G, B: Byte;
end;
PRGB16 = ^TRGB16;
TRGB16 = packed record
R, G, B: Word;
end;
PRGB32 = ^TRGB32;
TRGB32 = packed record
R, G, B: Cardinal;
end;
PRGBFloat = ^TRGBFloat;
TRGBFloat = packed record
R, G, B: Single;
end;
PRGBA = ^TRGBA;
TRGBA = packed record
R, G, B, A: Byte;
end;
PRGBA16 = ^TRGBA16;
TRGBA16 = packed record
R, G, B, A: Word;
end;
PBGR = ^TBGR;
TBGR = packed record
B, G, R: Byte;
end;
PBGR16 = ^TBGR16;
TBGR16 = packed record
B, G, R: Word;
end;
PBGRA = ^TBGRA;
TBGRA = packed record
B, G, R, A: Byte;
end;
PBGRA16 = ^TBGRA16;
TBGRA16 = packed record
B, G, R, A: Word;
end;
PHLS = ^THLS;
THLS = packed record
H, L, S: Byte;
end;
PHLSFloat = ^THLSFloat;
THLSFloat = packed record
H, L, S: Single;
end;
TColorScheme = (
csUnknown,
csIndexed, // Palette format.
csIndexedA, // Palette format with alpha channel.
csG, // Gray scale.
csGA, // Gray scale with alpha channel.
csRGB, // Red, green, blue.
csRGBA, // RGB with alpha channel
csBGR, // RGB in reversed order.
csBGRA, // BGR with alpha channel.
csCMY, // Cyan, agenta, yellow.
csCMYK, // CMY with black.
csCIELab, // CIE color format using luminance and chromaticities.
csITULab, // ITU L*a*b*
csCIELog2L, // CIE Log2(L)
csCIELog2Luv, // CIE Log2(L) (u', v')
csYCbCr, // Another format using luminance and chromaticities.
csPhotoYCC // A modified YCbCr version used for photo CDs.
);
TConvertOptions = set of (
coAlpha, // alpha channel is to be considered (this value is usually automatically set depending on
// the color scheme)
coApplyGamma, // target only, gamma correction must take place
coNeedByteSwap, // endian switch needed
coLabByteRange, // CIE L*a*b* only, luminance range is from 0..255 instead 0..100
coLabChromaOffset // CIE L*a*b* only, chrominance values a and b are given in 0..255 instead -128..127
);
// format of the raw data to create a color palette from
TRawPaletteFormat = (
pfInterlaced8Triple, // rgb triple with 8 bits per component
pfInterlaced8Quad, // rgb quad with 8 bits per component (fourth entry is reserved as in Windows' logical palette)
pfPlane8Triple, // 3 separate planes of data with 8 bits per component
pfPlane8Quad,
pfInterlaced16Triple,// rgb triple with 16 bits per component
pfInterlaced16Quad,
pfPlane16Triple, // 3 separate planes of data with 16 bits per component
pfPlane16Quad
);
// TConversionMethod describes the general parameter list to which each implemented conversion method conforms.
// Note: Source is defined as open array parameter to allow plane and interlaced source data.
TConversionMethod = procedure(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte) of object;
TColorManager = class
private
FChanged: Boolean; // set if any of the parameters changed
FSourceBPS, // bits per sample of source data (allowed values are 1, 2, 4, 8, 16)
FTargetBPS, // bits per sample of target data (allowed values are 1, 2, 4, 8, 16)
FSourceSPP, // samples per source pixel (allowed values are 1, 3, 4)
FTargetSPP: Byte; // samples per target pixel (allowed values are 1, 3, 4)
FMainGamma, // primary gamma value which is usually read from a file (default is 1)
FDisplayGamma: Single; // (constant) gamma value of the current monitor (default is 2.2)
FGammaTable: array[Byte] of Byte; // contains precalculated gamma values for each possible component value
// (range is 0..255)
FYCbCrCoefficients: array[0..2] of Single;
FHSubsampling,
FVSubSampling: Byte; // additional parameters used for YCbCr conversion
FCrToRedTable, // lookup tables used for YCbCr conversion
FCbToBlueTable,
FCrToGreenTable,
FCbToGreenTable: array of Integer;
FSourceScheme,
FTargetScheme: TColorScheme;
FRowConversion: TConversionMethod; // procedure variable for the actual conversion method used
FSourceOptions,
FTargetOptions: TConvertOptions;
procedure SetSourceOptions(const Value: TConvertOptions); // options to control conversion
protected
// Low level conversion helper used to convert one pixel component.
function ComponentGammaConvert(Value: Byte): Byte;
function ComponentNoConvert16(Value: Word): Word;
function ComponentNoConvert8(Value: Byte): Byte;
function ComponentScaleConvert(Value: Word): Byte;
function ComponentScaleGammaConvert(Value: Word): Byte;
function ComponentSwapScaleGammaConvert(Value: Word): Byte;
function ComponentSwapScaleConvert(Value: Word): Byte;
function ComponentSwapConvert(Value: Word): Word;
// row conversion routines
procedure RowConvertBGR2BGR(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertBGR2RGB(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertCIELAB2BGR(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertCIELAB2RGB(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertCMYK2BGR(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertCMYK2RGB(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertGray(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertIndexed8(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertIndexedBoth16(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertIndexedSource16(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertIndexedTarget16(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertRGB2BGR(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertRGB2RGB(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertPhotoYCC2BGR(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertPhotoYCC2RGB(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertYCbCr2BGR(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
procedure RowConvertYCbCr2RGB(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
// other general routines
procedure CreateYCbCrLookup;
function GetPixelFormat(Index: Integer): TPixelFormat;
procedure PrepareConversion;
procedure SetSourceBitsPerSample(const Value: Byte);
procedure SetSourceColorScheme(const Value: TColorScheme);
procedure SetSourceSamplesPerPixel(const Value: Byte);
procedure SetTargetBitsPerSample(const Value: Byte);
procedure SetTargetColorScheme(const Value: TColorScheme);
procedure SetTargetSamplesPerPixel(const Value: Byte);
public
constructor Create;
procedure ConvertRow(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
function CreateColorPalette(Data: array of Pointer; DataFormat: TRawPaletteFormat; ColorCount: Cardinal;
RGB: Boolean): HPALETTE;
function CreateGrayscalePalette(MinimumIsWhite: Boolean): HPALETTE;
procedure SetGamma(MainGamma: Single; DisplayGamma: Single = DefaultDisplayGamma);
procedure SetYCbCrParameters(Values: array of Single; HSubSampling, VSubSampling: Byte);
property SourceBitsPerSample: Byte read FSourceBPS write SetSourceBitsPerSample;
property SourceColorScheme: TColorScheme read FSourceScheme write SetSourceColorScheme;
property SourceOptions: TConvertOptions read FSourceOptions write SetSourceOptions;
property SourcePixelFormat: TPixelFormat index 0 read GetPixelFormat;
property SourceSamplesPerPixel: Byte read FSourceSPP write SetSourceSamplesPerPixel;
property TargetBitsPerSample: Byte read FTargetBPS write SetTargetBitsPerSample;
property TargetColorScheme: TColorScheme read FTargetScheme write SetTargetColorScheme;
property TargetOptions: TConvertOptions read FTargetOptions write FTargetOptions;
property TargetPixelFormat: TPixelFormat index 1 read GetPixelFormat;
property TargetSamplesPerPixel: Byte read FTargetSPP write SetTargetSamplesPerPixel;
end;
// common color convertion functions
function HLStoRGB(const HLS: THLSFloat): TRGBFloat;
function RGBToHLS(const RGB: TRGBFloat): THLSFloat;
function HLSInterpolation(const HLS1, HLS2: THLSFloat; Ratio: Extended): THLSFloat;
function RGBInterpolation(const RGB1, RGB2: TRGBFloat; Ratio: Extended): TRGBFloat; overload;
function RGBInterpolation(const RGB1, RGB2: TRGB; Ratio: Extended): TRGB; overload;
// color utility functions
function BrightenColor(const Color: TColor; Amount: Extended): TColor; overload;
function BrightenColor(const Color: TRGB; Amount: Extended): TRGB; overload;
function DarkenColor(const Color: TColor; Amount: Extended): TColor; overload;
function DarkenColor(const Color: TRGB; Amount: Extended): TRGB; overload;
function MakeHLS(const H, L, S: Byte): THLS; overload;
function MakeHLS(const H, L, S: Single): THLSFloat; overload;
function MakeRGB(const R, G, B: Byte): TRGB; overload;
function MakeRGB(const R, G, B: Single): TRGBFloat; overload;
// general utility functions
function ClampByte(Value: Integer): Byte;
function MulDiv16(Number, Numerator, Denominator: Word): Word;
//----------------------------------------------------------------------------------------------------------------------
implementation
uses
Math, SysUtils;
type
EColorConversionError = class(Exception);
//----------------- helper functions -----------------------------------------------------------------------------------
procedure ShowError(const Msg: String);
begin
raise EColorConversionError.Create(Msg);
end;
//----------------------------------------------------------------------------------------------------------------------
function ClampByte(Value: Integer): Byte;
// ensures Value is in the range 0..255, values < 0 are clamped to 0 and values > 255 are clamped to 255
asm
OR EAX, EAX
JNS @@positive
XOR EAX, EAX
RET
@@positive:
CMP EAX, 255
JBE @@OK
MOV EAX, 255
@@OK:
end;
//----------------------------------------------------------------------------------------------------------------------
function MulDiv16(Number, Numerator, Denominator: Word): Word;
// faster equivalent to Windows' MulDiv function
// Number is passed via AX
// Numerator is passed via DX
// Denominator is passed via CX
// Result is passed via AX
// Note: no error checking takes place. Denominator must be > 0!
asm
MUL DX
DIV CX
end;
//----------------- common color conversion functions ------------------------------------------------------------------
function HLStoRGB(const HLS: THLSFloat): TRGBFloat;
// converts from HLS (hue, luminance, saturation) to RGB using floating point math
// Input parameters and result values are all in the range 0..1.
//--------------- local function --------------------------------------------
function HueToRGB(m1, m2, hue: Extended): Extended;
begin
if hue > 1 then
hue := hue - 1
else
if hue < 0 then
hue := hue + 1;
if 6 * hue < 1 then
Result := m1 + (m2 - m1) * hue * 6
else
if 2 * hue < 1 then
Result := m2
else
if 3 * hue < 2 then
Result := m1 + (m2 - m1) * (2 / 3 - hue) * 6
else
Result := m1;
end;
//--------------- end local function ----------------------------------------
var
m1, m2: Single;
begin
with HLS, Result do
begin
if S = 0 then
begin
// achromatic case (no hue)
R := L;
G := L;
B := L
end
else
begin
if L <= 0.5 then
m2 := L * (S + 1)
else
m2 := L + S - L * S;
m1 := 2 * L - m2;
R := HueToRGB(m1, m2, H + 1 / 3);
G := HueToRGB(m1, m2, H);
B := HueToRGB(m1, m2, H - 1 / 3)
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function RGBToHLS(const RGB: TRGBFloat): THLSFloat;
// converts from RGB to HLS using floating point math
// Input parameters and result values are all in the range 0..1.
var
Delta,
Max,
Min: Extended;
begin
with RGB, Result do
begin
Max := MaxValue([R, G, B]);
Min := MinValue([R, G, B]);
L := (Max + Min) / 2;
if Max = Min then
begin
// achromatic case
S := 0;
H := 0; // undefined
end
else
begin
Delta := Max - Min;
if L < 0.5 then
S := Delta / (Max + Min)
else
S := Delta / (2 - (Max + Min));
if R = Max then
H := (G - B) / Delta
else
if G = Max then
H := 2 + (B - R) / Delta
else
if B = Max then
H := 4 + (R - G) / Delta;
H := H / 6;
if H < 0 then
H := H + 1;
end
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function HLSInterpolation(const HLS1, HLS2: THLSFloat; Ratio: Extended): THLSFloat;
// interpolates linearly from HLS1 to HLS2 with the given ratio
// Parameters as well as result are in the range 0..1.
begin
if Ratio <= 0 then
Result := HLS1
else
if Ratio >= 1 then
Result := HLS2
else
begin
Result.H := HLS1.H + (HLS2.H - HLS1.H) * Ratio;
Result.L := HLS1.L + (HLS2.L - HLS1.L) * Ratio;
Result.S := HLS1.S + (HLS2.S - HLS1.S) * Ratio;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function RGBInterpolation(const RGB1, RGB2: TRGB; Ratio: Extended): TRGB;
// interpolates linearly from RGB1 to RGB2 with the given ratio using the HLS color space
// which produces more natural results
// Parameters as well as result are in the range 0..255.
var
HLS1,
HLS2: THLSFloat;
RGB: TRGBFloat;
begin
if Ratio <= 0 then
Result := RGB1
else
if Ratio >= 1 then
Result := RGB2
else
begin
HLS1 := RGBToHLS(MakeRGB(RGB1.R / 255, RGB1.G / 255, RGB1.B / 255));
HLS2 := RGBToHLS(MakeRGB(RGB2.R / 255, RGB2.G / 255, RGB2.B / 255));
HLS2.H := HLS1.H + (HLS2.H - HLS1.H) * Ratio;
HLS2.L := HLS1.L + (HLS2.L - HLS1.L) * Ratio;
HLS2.S := HLS1.S + (HLS2.S - HLS1.S) * Ratio;
RGB := HLSToRGB(HLS2);
Result.R := Round(RGB.R * 255);
Result.G := Round(RGB.G * 255);
Result.B := Round(RGB.B * 255);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function RGBInterpolation(const RGB1, RGB2: TRGBFloat; Ratio: Extended): TRGBFloat;
// interpolates linearly from RGB1 to RGB2 with the given ratio using the HLS color space
// which produces more natural results
// Parameters as well as result are in the range 0..1.
var
HLS1, HLS2: THLSFloat;
begin
if Ratio <= 0 then
Result := RGB1
else
if Ratio >= 1 then
Result := RGB2
else
begin
HLS1 := RGBToHLS(RGB1);
HLS2 := RGBToHLS(RGB2);
HLS2.H := HLS1.H + (HLS1.H - HLS2.H) * Ratio;
HLS2.L := HLS1.L + (HLS1.L - HLS2.L) * Ratio;
HLS2.S := HLS1.S + (HLS1.S - HLS2.S) * Ratio;
Result := HLSToRGB(HLS2);
end;
end;
//----------------- color utility functions ----------------------------------------------------------------------------
function BrightenColor(const Color: TColor; Amount: Extended): TColor;
// Brightens the given RGB color by the given amount using the HLS color model (increasing luminance).
// Amount is a percent value (in the range 0..1) which determines by which amount the source color should
// be brightened.
var
WinColor: COLORREF;
HLS: THLSFloat;
RGB: TRGBFloat;
begin
WinColor := ColorToRGB(Color);
HLS := RGBToHLS(MakeRGB((WinColor and $FF) / 255, ((WinColor shr 8) and $FF) / 255, ((WinColor shr 16) and $FF) / 255));
// brighten means to increase luminance
HLS.L := (1 + Amount) * HLS.L;
RGB := HLSToRGB(HLS);
Result := Windows.RGB(ClampByte(Round(255 * RGB.R)), ClampByte(Round(255 * RGB.G)), ClampByte(Round(255 * RGB.B)));
end;
//----------------------------------------------------------------------------------------------------------------------
function BrightenColor(const Color: TRGB; Amount: Extended): TRGB;
var
HLS: THLSFloat;
RGB: TRGBFloat;
begin
HLS := RGBToHLS(MakeRGB(Color.R / 255, Color.G / 255, Color.B / 255));
HLS.L := (1 + Amount) * HLS.L;
RGB := HLSToRGB(HLS);
Result := MakeRGB(ClampByte(Round(255 * RGB.R)), ClampByte(Round(255 * RGB.G)), ClampByte(Round(255 * RGB.B)));
end;
//----------------------------------------------------------------------------------------------------------------------
function DarkenColor(const Color: TColor; Amount: Extended): TColor;
// Darkens the given RGB color by the given amount using the HLS color model (decreasing luminance).
// Amount is a percent value (in the range 0..1) which determines by which amount the source color should
// be darkened.
var
WinColor: COLORREF;
HLS: THLSFloat;
RGB: TRGBFloat;
begin
WinColor := ColorToRGB(Color);
HLS := RGBToHLS(MakeRGB((WinColor and $FF) / 255, ((WinColor shr 8) and $FF) / 255, ((WinColor shr 16) and $FF) / 255));
// darken means to decrease luminance
HLS.L := (1 - Amount) * HLS.L;
RGB := HLSToRGB(HLS);
Result := Windows.RGB(ClampByte(Round(255 * RGB.R)), ClampByte(Round(255 * RGB.G)), ClampByte(Round(255 * RGB.B)));
end;
//----------------------------------------------------------------------------------------------------------------------
function DarkenColor(const Color: TRGB; Amount: Extended): TRGB;
var
HLS: THLSFloat;
RGB: TRGBFloat;
begin
HLS := RGBToHLS(MakeRGB(Color.R / 255, Color.G / 255, Color.B / 255));
// darken means to decrease luminance
HLS.L := (1 - Amount) * HLS.L;
RGB := HLSToRGB(HLS);
Result := MakeRGB(ClampByte(Round(255 * RGB.R)), ClampByte(Round(255 * RGB.G)), ClampByte(Round(255 * RGB.B)));
end;
//----------------------------------------------------------------------------------------------------------------------
function MakeHLS(const H, L, S: Byte): THLS;
begin
Result.H := H;
Result.L := L;
Result.S := S;
end;
//----------------------------------------------------------------------------------------------------------------------
function MakeHLS(const H, L, S: Single): THLSFloat;
begin
Result.H := H;
Result.L := L;
Result.S := S;
end;
//----------------------------------------------------------------------------------------------------------------------
function MakeRGB(const R, G, B: Byte): TRGB;
begin
Result.R := R;
Result.G := G;
Result.B := B;
end;
//----------------------------------------------------------------------------------------------------------------------
function MakeRGB(const R, G, B: Single): TRGBFloat;
begin
Result.R := R;
Result.G := G;
Result.B := B;
end;
//----------------- TColorManager --------------------------------------------------------------------------------------
constructor TColorManager.Create;
// set some default values
begin
FSourceBPS := 8;
FTargetBPS := 8;
FSourceSPP := 3; // 24 bit format
FTargetSPP := 3; // 24 bit format
SetGamma(1, DefaultDisplayGamma);
FSourceScheme := csRGB;
FTargetScheme := csBGR;
// defaults are from CCIR Recommendation 601-1
FYCbCrCoefficients[0] := 0.299;
FYCbCrCoefficients[1] := 0.587;
FYCbCrCoefficients[2] := 0.114;
FHSubSampling := 1;
FVSubSampling := 1;
FChanged := True;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TColorManager.SetSourceOptions(const Value: TConvertOptions);
begin
if FSourceOptions <> Value then
begin
FSourceOptions := Value;
FChanged := True;
end;
end;
//----------------- low level conversion routines ----------------------------------------------------------------------
// These routines are used for conversions from 16 to 8 bit values, either with gamma correction or byte swap (or both).
function TColorManager.ComponentNoConvert8(Value: Byte): Byte;
begin
Result := Value;
end;
//----------------------------------------------------------------------------------------------------------------------
function TColorManager.ComponentNoConvert16(Value: Word): Word;
begin
Result := Value;
end;
//----------------------------------------------------------------------------------------------------------------------
function TColorManager.ComponentGammaConvert(Value: Byte): Byte;
begin
Result := FGammaTable[Value];
end;
//----------------------------------------------------------------------------------------------------------------------
function TColorManager.ComponentScaleConvert(Value: Word): Byte;
begin
Result := MulDiv16(Value, 255, 65535);
end;
//----------------------------------------------------------------------------------------------------------------------
function TColorManager.ComponentScaleGammaConvert(Value: Word): Byte;
begin
Result := FGammaTable[MulDiv16(Value, 255, 65535)];
end;
//----------------------------------------------------------------------------------------------------------------------
function TColorManager.ComponentSwapScaleGammaConvert(Value: Word): Byte;
begin
Result := FGammaTable[MulDiv16(Swap(Value), 255, 65535)];
end;
//----------------------------------------------------------------------------------------------------------------------
function TColorManager.ComponentSwapScaleConvert(Value: Word): Byte;
begin
Result := MulDiv16(Swap(Value), 255, 65535);
end;
//----------------------------------------------------------------------------------------------------------------------
function TColorManager.ComponentSwapConvert(Value: Word): Word;
begin
Result := Swap(Value);
end;
//----------------- row conversion routines ----------------------------------------------------------------------------
// Notes: Each method takes parameters for source and target data as well as the count of pixels to work on. This count
// determines the number of pixels in the target buffer. The actual source count may differ for special color
// schemes (like YCbCr) or interlaced lines.
// Mask is a parameter which determines (in a repeative manner) which source pixel should actually be transferred
// to the target buffer. A 1 in the corresponding bit (MSB is leftmost pixel) causes the transfer to happen.
// Usually, this parameter is $FF to transfer all pixels, but for interlaced images (e.g. as in PNG format)
// this will differ to limit pixel transfers. The bit mask only describes which target pixel is to skip. Source
// pixel must be packed.
// Windows DIBs are always byte aligned, so we don't need checks for byte alignments (in target).
procedure TColorManager.RowConvertBGR2BGR(Source: array of Pointer; Target: Pointer; Count: Cardinal; Mask: Byte);
// same as ConvertBGR2RGB but for BGR target schemes
var
SourceR16,
SourceG16,
SourceB16,
SourceA16: PWord;
SourceR8,
SourceG8,
SourceB8,
SourceA8: PByte;
TargetRun16: PBGR16;
TargetRunA16: PBGRA16;
TargetRun8: PBGR;
TargetRunA8: PBGRA;
BitRun: Byte;
Convert8_8: function(Value: Byte): Byte of object;
Convert16_8: function(Value: Word): Byte of object;
Convert16_8Alpha: function(Value: Word): Byte of object;
Convert16_16: function(Value: Word): Word of object;
SourceIncrement,
TargetIncrement: Cardinal;
CopyAlpha: Boolean;
begin
BitRun := $80;
// determine alpha handling once
CopyAlpha := False;
if coAlpha in FSourceOptions then
begin
SourceIncrement := SizeOf(TRGBA);
TargetIncrement := SizeOf(TRGB);
if coAlpha in FTargetOptions then
CopyAlpha := True;
end
else
begin
SourceIncrement := SizeOf(TRGB);
if coAlpha in FTargetOptions then
TargetIncrement := SizeOf(TRGBA)
else
TargetIncrement := SizeOf(TRGB);
end;
// in planar mode source increment is always 1
if Length(Source) > 1 then
SourceIncrement := 1;
case FSourceBPS of
8:
begin
if Length(Source) = 1 then
begin
// interleaved mode
SourceB8 := Source[0];
SourceG8 := SourceB8; Inc(SourceG8);
SourceR8 := SourceG8; Inc(SourceR8);
SourceA8 := SourceR8; Inc(SourceA8);
end
else
begin
SourceB8 := Source[0];
SourceG8 := Source[1];
SourceR8 := Source[2];
if coAlpha in FSourceOptions then
SourceA8 := Source[3]
else
SourceA8 := nil;
end;
case FTargetBPS of
8: // 888 to 888
begin
if coApplyGamma in FTargetOptions then
Convert8_8 := ComponentGammaConvert
else
Convert8_8 := ComponentNoConvert8;
if CopyAlpha then
begin
TargetRunA8 := Target;
while Count > 0 do
begin
if Boolean(Mask and BitRun) then
begin
TargetRunA8.R := Convert8_8(SourceR8^);
TargetRunA8.G := Convert8_8(SourceG8^);
TargetRunA8.B := Convert8_8(SourceB8^);
// alpha values are never gamma corrected
TargetRunA8.A := SourceA8^;
Inc(SourceB8, SourceIncrement);
Inc(SourceG8, SourceIncrement);
Inc(SourceR8, SourceIncrement);
Inc(SourceA8, SourceIncrement);
end;
asm ROR BYTE PTR [BitRun], 1 end;
Dec(Count);
Inc(TargetRunA8);
end;
end
else
begin
TargetRun8 := Target;
while Count > 0 do
begin
if Boolean(Mask and BitRun) then
begin
TargetRun8.R := Convert8_8(SourceR8^);
TargetRun8.G := Convert8_8(SourceG8^);
TargetRun8.B := Convert8_8(SourceB8^);
Inc(SourceB8, SourceIncrement);
Inc(SourceG8, SourceIncrement);
Inc(SourceR8, SourceIncrement);
end;
asm ROR BYTE PTR [BitRun], 1 end;
Dec(Count);
Inc(PByte(TargetRun8), TargetIncrement);
end;
end;
end;
16: // 888 to 161616
begin
if coApplyGamma in FTargetOptions then
Convert8_8 := ComponentGammaConvert
else
Convert8_8 := ComponentNoConvert8;
if coNeedByteSwap in FSourceOptions then
Convert16_16 := ComponentSwapConvert
else
Convert16_16 := ComponentNoConvert16;
if Length(Source) = 1 then
begin
SourceB8 := Source[0];
SourceG8 := SourceB8; Inc(SourceG8);
SourceR8 := SourceG8; Inc(SourceR8);
SourceA8 := SourceR8; Inc(SourceA8);
end
else
begin
SourceB8 := Source[0];
SourceG8 := Source[1];
SourceR8 := Source[2];
if coAlpha in FSourceOptions then
SourceA8 := Source[3]
else
SourceA8 := nil;
end;
if CopyAlpha then
begin
TargetRunA16 := Target;
while Count > 0 do
begin
if Boolean(Mask and BitRun) then
begin
TargetRunA16.R := Convert16_16(MulDiv16(Convert8_8(SourceR8^), 65535, 255));
TargetRunA16.G := Convert16_16(MulDiv16(Convert8_8(SourceG8^), 65535, 255));
TargetRunA16.B := Convert16_16(MulDiv16(Convert8_8(SourceB8^), 65535, 255));
TargetRunA16.A := Convert16_16(MulDiv16(SourceA8^, 65535, 255));
Inc(SourceB8, SourceIncrement);
Inc(SourceG8, SourceIncrement);
Inc(SourceR8, SourceIncrement);
Inc(SourceA8, SourceIncrement);
end;
asm ROR BYTE PTR [BitRun], 1 end;
Dec(Count);
Inc(TargetRunA16);
end;
end
else
begin
TargetRun16 := Target;
while Count > 0 do
begin
if Boolean(Mask and BitRun) then
begin
TargetRun16.R := Convert16_16(MulDiv16(Convert8_8(SourceR8^), 65535, 255));
TargetRun16.G := Convert16_16(MulDiv16(Convert8_8(SourceG8^), 65535, 255));
TargetRun16.B := Convert16_16(MulDiv16(Convert8_8(SourceB8^), 65535, 255));
Inc(SourceB8, SourceIncrement);
Inc(SourceG8, SourceIncrement);
Inc(SourceR8, SourceIncrement);
end;
asm ROR BYTE PTR [BitRun], 1 end;
Dec(Count);
Inc(PWord(TargetRun16), TargetIncrement);
end;
end;
end;
end;
end;
16:
begin
if Length(Source) = 1 then
begin
SourceB16 := Source[0];
SourceG16 := SourceB16; Inc(SourceG16);
SourceR16 := SourceG16; Inc(SourceR16);
SourceA16 := SourceR16; Inc(SourceA16);
end
else
begin
SourceB16 := Source[0];
SourceG16 := Source[1];
SourceR16 := Source[2];
if coAlpha in FSourceOptions then
SourceA16 := Source[3]
else
SourceA16 := nil;
end;