forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bgracolorquantization.pas
2090 lines (1909 loc) · 66.1 KB
/
bgracolorquantization.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
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRAColorQuantization;
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType, BGRAGraphics,{$ENDIF} BGRAPalette, BGRABitmapTypes;
type
TBGRAColorBox = class;
TBGRAColorTree = class;
TBGRAApproxPalette = class;
TBiggestLeafMethod = (blMix, blApparentInterval, blWeight);
{ TDimensionMinMax }
TDimensionMinMax = object
Minimum: UInt32;
Maximum: UInt32;
function Size: UInt32;
function Contains(AValue: UInt32): boolean;
function PointLike: boolean;
procedure SetAsPoint(AValue: UInt32);
function GetCenter: UInt32;
procedure GrowToInclude(AValue: UInt32);
end;
TColorDimension = (cdFast,cdRed,cdGreen,cdBlue,cdAlpha,cdRGB,cdRG,cdGB,cdRB,cdRInvG,cdGInvB,cdRInvB,cdRInvGB,cdGInvRB,cdBInvRG,
cdSaturation);
TColorDimensions = set of TColorDimension;
{ TBGRAColorQuantizer }
TBGRAColorQuantizer = class(TBGRACustomColorQuantizer)
private
FColors: ArrayOfWeightedColor;
FPalette: TBGRAApproxPalette;
FReductionColorCount: Integer;
FReductionKeepContrast: boolean;
FSeparateAlphaChannel: boolean;
procedure Init(ABox: TBGRAColorBox);
procedure NormalizeArrayOfColors(AColors: ArrayOfTBGRAPixel; ARedBounds, AGreenBounds, ABlueBounds, AAlphaBounds: TDimensionMinMax; AUniform: boolean); overload;
procedure NormalizeArrayOfColors(AColors: ArrayOfTBGRAPixel; AColorBounds, AAlphaBounds: TDimensionMinMax); overload;
protected
function GetPalette: TBGRACustomApproxPalette; override;
function GetSourceColor(AIndex: integer): TBGRAPixel; override;
function GetSourceColorCount: Integer; override;
function GetReductionColorCount: integer; override;
procedure SetReductionColorCount(AValue: Integer); override;
public
constructor Create(APalette: TBGRACustomPalette; ASeparateAlphaChannel: boolean); override;
constructor Create(ABitmap: TBGRACustomBitmap; AAlpha: TAlphaChannelPaletteOption); override;
constructor Create(APalette: TBGRACustomPalette; ASeparateAlphaChannel: boolean; AReductionColorCount: integer); override;
constructor Create(ABitmap: TBGRACustomBitmap; AAlpha: TAlphaChannelPaletteOption; AReductionColorCount: integer); override;
destructor Destroy; override;
procedure ApplyDitheringInplace(AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap; ABounds: TRect); override;
function GetDitheredBitmap(AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap; ABounds: TRect): TBGRACustomBitmap; overload; override;
function GetDitheredBitmapIndexedData(ABitDepth: integer; AByteOrder: TRawImageByteOrder; AAlgorithm: TDitheringAlgorithm;
ABitmap: TBGRACustomBitmap; out AScanlineSize: BGRAPtrInt): Pointer; overload; override;
procedure SaveBitmapToStream(AAlgorithm: TDitheringAlgorithm;
ABitmap: TBGRACustomBitmap; AStream: TStream; AFormat: TBGRAImageFormat); override;
end;
{ TBGRAApproxPalette }
TBGRAApproxPalette = class(TBGRACustomApproxPalette)
private
FTree: TBGRAColorTree;
FColors: ArrayOfWeightedColor;
protected
function GetCount: integer; override;
function GetColorByIndex(AIndex: integer): TBGRAPixel; override;
function GetWeightByIndex(AIndex: Integer): UInt32; override;
procedure Init(const AColors: ArrayOfTBGRAPixel);
public
constructor Create(const AColors: ArrayOfTBGRAPixel); overload;
constructor Create(const AColors: ArrayOfWeightedColor); overload;
constructor Create(AOwnedSplitTree: TBGRAColorTree); overload;
destructor Destroy; override;
function ContainsColor(AValue: TBGRAPixel): boolean; override;
function IndexOfColor(AValue: TBGRAPixel): integer; override;
function FindNearestColor(AValue: TBGRAPixel): TBGRAPixel; override;
function FindNearestColorIndex(AValue: TBGRAPixel): integer; override;
function GetAsArrayOfColor: ArrayOfTBGRAPixel; override;
function GetAsArrayOfWeightedColor: ArrayOfWeightedColor; override;
end;
{ TBGRAApproxPaletteViaLargerPalette }
TBGRAApproxPaletteViaLargerPalette = class(TBGRAApproxPalette)
private
FLarger: TBGRACustomApproxPalette;
FLargerColors: array of record
approxColor: TBGRAPixel;
approxColorIndex: integer;
end;
FLargerOwned: boolean;
FTransparentColorIndex: integer;
protected
function FindNearestLargerColorIndex(AValue: TBGRAPixel): integer; virtual;
function SlowFindNearestColorIndex(AValue: TBGRAPixel): integer;
public
constructor Create(const AColors: ArrayOfTBGRAPixel; ALarger: TBGRACustomApproxPalette; ALargerOwned: boolean);
destructor Destroy; override;
function FindNearestColor(AValue: TBGRAPixel): TBGRAPixel; override;
function FindNearestColorIndex(AValue: TBGRAPixel): integer; override;
function GetAsArrayOfWeightedColor: ArrayOfWeightedColor; override;
end;
TIsChannelStrictlyGreaterFunc = TBGRAPixelComparer;
TIsChannelGreaterThanOrEqualToValueFunc = function (p : PBGRAPixel; v: UInt32): boolean;
TColorBoxBounds = array[TColorDimension] of TDimensionMinMax;
{ TBGRAColorBox }
TBGRAColorBox = class
private
FBounds: TColorBoxBounds;
FTotalWeight: UInt32;
FColors: ArrayOfWeightedColor;
FDimensions: TColorDimensions;
FPureTransparentColorCount: integer;
function GetApparentInterval(ADimension: TColorDimension): UInt32;
function GetAverageColor: TBGRAPixel;
function GetAverageColorOrMainColor: TBGRAPixel;
function GetBounds(ADimension: TColorDimension): TDimensionMinMax;
function GetColorCount(ACountPureTransparent: boolean): integer;
function GetHasPureTransparentColor: boolean;
function GetInferiorColor: TBGRAPixel;
function GetLargestApparentDimension: TColorDimension;
function GetLargestApparentInterval: UInt32;
function GetPointLike: boolean;
function GetSuperiorColor: TBGRAPixel;
procedure Init(AColors: ArrayOfWeightedColor; AOwner: boolean);
procedure SortBy(ADimension: TColorDimension);
function GetMedianIndex(ADimension : TColorDimension; AMinValue, AMaxValue: UInt32): integer;
public
constructor Create(ADimensions: TColorDimensions; AColors: ArrayOfWeightedColor; AOwner: boolean); overload;
constructor Create(ADimensions: TColorDimensions; const AColors: ArrayOfTBGRAPixel; AAlpha: TAlphaChannelPaletteOption = acFullChannelInPalette); overload;
constructor Create(ADimensions: TColorDimensions; ABounds: TColorBoxBounds); overload;
constructor Create(ADimensions: TColorDimensions; APalette: TBGRACustomPalette); overload;
constructor Create(ADimensions: TColorDimensions; ABitmap: TBGRACustomBitmap; AAlpha: TAlphaChannelPaletteOption); overload;
constructor Create(ADimensions: TColorDimensions; AColors: PBGRAPixel; ANbPixels: integer; AAlpha: TAlphaChannelPaletteOption); overload;
function BoundsContain(AColor: TBGRAPixel): boolean;
function MedianCut(ADimension: TColorDimension; out SuperiorMiddle: UInt32): TBGRAColorBox;
function Duplicate : TBGRAColorBox;
property Bounds[ADimension: TColorDimension]: TDimensionMinMax read GetBounds;
property ApparentInterval[AChannel: TColorDimension]: UInt32 read GetApparentInterval;
property LargestApparentDimension: TColorDimension read GetLargestApparentDimension;
property LargestApparentInterval: UInt32 read GetLargestApparentInterval;
property PointLike: boolean read GetPointLike;
property AverageColor: TBGRAPixel read GetAverageColor;
property SuperiorColor: TBGRAPixel read GetSuperiorColor;
property InferiorColor: TBGRAPixel read GetInferiorColor;
property AverageColorOrMainColor: TBGRAPixel read GetAverageColorOrMainColor;
function GetAsArrayOfColors(AIncludePureTransparent: boolean): ArrayOfTBGRAPixel;
property TotalWeight: UInt32 read FTotalWeight;
property ColorCount[ACountPureTransparent: boolean]: integer read GetColorCount;
property HasPureTransparentColor: boolean read GetHasPureTransparentColor;
property PureTransparentColorCount: integer read FPureTransparentColorCount;
end;
TBGRALeafColorMode = (lcAverage, lcCenter, lcExtremum, lcMix);
{ TBGRAColorTree }
TBGRAColorTree = class
private
FLeaf: TBGRAColorBox;
FIsLeaf: boolean;
FLargestApparentInterval: integer;
FWeight: UInt32;
FLeafColor: TBGRAPixel;
FLeafColorIndex: integer;
FLeafColorComputed: boolean;
FMinBorder, FMaxBorder: array[TColorDimension] of boolean;
FCenterColor: TBGRAPixel;
FAverageColor: TBGRAPixel;
FPureTransparentColorCount: integer;
FPureTransparentColorIndex: integer;
FDimension: TColorDimension;
FPixelValueComparer: TIsChannelGreaterThanOrEqualToValueFunc;
FSuperiorMiddle: UInt32;
FInferiorBranch, FSuperiorBranch: TBGRAColorTree;
function GetApproximatedColorCount: integer;
function GetHasPureTransparentColor: boolean;
function GetLeafCount: integer;
procedure Init(ALeaf: TBGRAColorBox; AOwned: boolean);
procedure InternalComputeLeavesColor(ALeafColor: TBGRALeafColorMode; var AStartIndex: integer);
procedure CheckColorComputed;
public
constructor Create(ABox: TBGRAColorBox; AOwned: boolean); overload;
constructor Create(ADimensions: TColorDimensions; APalette: TBGRACustomPalette); overload;
constructor Create(ADimensions: TColorDimensions; ABitmap: TBGRACustomBitmap; AAlpha: TAlphaChannelPaletteOption); overload;
destructor Destroy; override;
procedure FreeLeaves;
function FindBiggestLeaf(AMethod: TBiggestLeafMethod): TBGRAColorTree;
property LargestApparentInterval: integer read FLargestApparentInterval;
property Weight: UInt32 read FWeight;
property IsLeaf: boolean read FIsLeaf;
function TrySplitLeaf: boolean;
procedure ComputeLeavesColor(ALeafColor: TBGRALeafColorMode);
function ApproximateColor(AColor: TBGRAPixel): TBGRAPixel;
function ApproximateColorIndex(AColor: TBGRAPixel): integer;
function GetAsArrayOfApproximatedColors: ArrayOfTBGRAPixel;
function GetAsArrayOfWeightedColors: ArrayOfWeightedColor;
procedure SplitIntoPalette(ACount: integer; AMethod: TBiggestLeafMethod;
ALeafColor: TBGRALeafColorMode);
function SplitIntoPaletteWithSubPalette(ACount: integer; AMethod: TBiggestLeafMethod;
ALeafColor: TBGRALeafColorMode; ASubPaletteCount: integer): ArrayOfTBGRAPixel;
property LeafCount: integer read GetLeafCount;
property ApproximatedColorCount: integer read GetApproximatedColorCount;
property HasPureTransparentColor: boolean read GetHasPureTransparentColor;
property PureTransparentColorCount: integer read FPureTransparentColorCount;
end;
function GetPixelStrictComparer(ADimension: TColorDimension): TIsChannelStrictlyGreaterFunc;
function GetPixelValueComparer(ADimension: TColorDimension): TIsChannelGreaterThanOrEqualToValueFunc;
function BGRAColorCount(ABitmap: TBGRACustomBitmap; AAlpha: TAlphaChannelPaletteOption): integer;
const AllColorDimensions = [cdRed,cdGreen,cdBlue,cdAlpha,cdRGB,cdRG,cdGB,cdRB,cdRInvG,cdGInvB,cdRInvB,cdRInvGB,cdGInvRB,cdBInvRG,
cdSaturation];
implementation
uses BGRADithering, FPImage, FPWriteBMP, BGRAWritePNG, math;
const MedianMinPercentage = 0.2;
const RedShift = 1;
GreenShift = 2;
AlphaShift = 1;
SaturationShift = 2;
function GetDimensionValue(APixel: TBGRAPixel; ADimension: TColorDimension): UInt32;
var v: UInt32;
{$IFDEF BDS}_BGRADWord : BGRADWord;{$ENDIF}//#
begin
case ADimension of
cdFast: begin
{$IFDEF BDS}move(APixel , _BGRADWord, sizeof(BGRADWord));{$ENDIF}
result := {$IFDEF BDS}_BGRADWord{$ELSE}BGRADWord(APixel){$ENDIF};
end;
cdRed: result := GammaExpansionTab[APixel.red] shl RedShift;
cdGreen: result := GammaExpansionTab[APixel.green] shl GreenShift;
cdBlue: result := GammaExpansionTab[APixel.blue];
cdAlpha: result := (APixel.alpha + (APixel.alpha shl 8)) shl AlphaShift;
cdRGB: result := GammaExpansionTab[APixel.blue] + (GammaExpansionTab[APixel.red] shl RedShift) + (GammaExpansionTab[APixel.green] shl GreenShift);
cdRG: result := (GammaExpansionTab[APixel.red] shl RedShift) + (GammaExpansionTab[APixel.green] shl GreenShift);
cdGB: result := GammaExpansionTab[APixel.blue] + (GammaExpansionTab[APixel.green] shl GreenShift);
cdRB: result := (GammaExpansionTab[APixel.red] shl RedShift) + GammaExpansionTab[APixel.blue];
cdRInvG: result := (GammaExpansionTab[APixel.red] shl RedShift) + ((not GammaExpansionTab[APixel.green]) shl GreenShift);
cdGInvB: result := (GammaExpansionTab[APixel.green] shl GreenShift) + (not GammaExpansionTab[APixel.blue]);
cdRInvB: result := (GammaExpansionTab[APixel.red] shl RedShift) + (not GammaExpansionTab[APixel.blue]);
cdRInvGB: result := (GammaExpansionTab[APixel.red] shl RedShift) + ((not GammaExpansionTab[APixel.green]) shl GreenShift) + (not GammaExpansionTab[APixel.blue]);
cdGInvRB: result := (GammaExpansionTab[APixel.green] shl GreenShift) + ((not GammaExpansionTab[APixel.red]) shl RedShift) + (not GammaExpansionTab[APixel.blue]);
cdBInvRG: result := (GammaExpansionTab[APixel.blue]) + ((not GammaExpansionTab[APixel.red]) shl RedShift) + ((not GammaExpansionTab[APixel.green]) shl GreenShift);
cdSaturation: with GammaExpansion(APixel) do
begin
v := red;
if green>v then v := green;
if blue>v then v := blue;
result := v;
v := red;
if green<v then v := green;
if blue<v then v := blue;
result := result -v;
result := result shl SaturationShift;
end
else raise exception.Create('Unknown dimension');
end;
end;
function IsRGBGreater(p1, p2: PBGRAPixel): boolean;
begin
result := ((GammaExpansionTab[p1^.red] shl RedShift)+(GammaExpansionTab[p1^.green] shl GreenShift)+GammaExpansionTab[p1^.blue]) >
((GammaExpansionTab[p2^.red] shl RedShift)+(GammaExpansionTab[p2^.green] shl GreenShift)+GammaExpansionTab[p2^.blue]);
end;
function IsRGBGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
with p^ do
result := ((GammaExpansionTab[red] shl RedShift)+(GammaExpansionTab[green] shl GreenShift)+GammaExpansionTab[blue]) >= v;
end;
function IsRGGreater(p1, p2: PBGRAPixel): boolean;
begin
result := ((GammaExpansionTab[p1^.red] shl RedShift)+(GammaExpansionTab[p1^.green] shl GreenShift)) >
((GammaExpansionTab[p2^.red] shl RedShift)+(GammaExpansionTab[p2^.green] shl GreenShift));
end;
function IsRGGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
with p^ do
result := ((GammaExpansionTab[red] shl RedShift)+(GammaExpansionTab[green] shl GreenShift)) >= v;
end;
function IsGBGreater(p1, p2: PBGRAPixel): boolean;
begin
result := ((GammaExpansionTab[p1^.green] shl GreenShift)+GammaExpansionTab[p1^.blue]) >
((GammaExpansionTab[p2^.green] shl GreenShift)+GammaExpansionTab[p2^.blue]);
end;
function IsGBGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
with p^ do
result := ((GammaExpansionTab[green] shl GreenShift)+GammaExpansionTab[blue]) >= v;
end;
function IsRBGreater(p1, p2: PBGRAPixel): boolean;
begin
result := ((GammaExpansionTab[p1^.red] shl RedShift)+GammaExpansionTab[p1^.blue]) >
((GammaExpansionTab[p2^.red] shl RedShift)+GammaExpansionTab[p2^.blue]);
end;
function IsRBGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
with p^ do
result := ((GammaExpansionTab[red] shl RedShift)+GammaExpansionTab[blue]) >= v;
end;
function IsRInvGGreater(p1, p2: PBGRAPixel
): boolean;
begin
result := (GammaExpansionTab[p1^.red]+ ((not GammaExpansionTab[p1^.green]) shl GreenShift)) >
(GammaExpansionTab[p2^.red]+((not GammaExpansionTab[p2^.green]) shl GreenShift));
end;
function IsRInvGGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
with p^ do
result := (GammaExpansionTab[red]+((not GammaExpansionTab[green]) shl GreenShift)) >= v;
end;
function IsGInvBGreater(p1, p2: PBGRAPixel
): boolean;
begin
result := (GammaExpansionTab[p1^.green] shl GreenShift + not GammaExpansionTab[p1^.blue]) >
(GammaExpansionTab[p2^.green] shl GreenShift + not GammaExpansionTab[p2^.blue]);
end;
function IsGInvBGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
with p^ do
result := (GammaExpansionTab[green] shl GreenShift + not GammaExpansionTab[blue]) >= v;
end;
function IsRInvBGreater(p1, p2: PBGRAPixel
): boolean;
begin
result := (GammaExpansionTab[p1^.red] shl RedShift + not GammaExpansionTab[p1^.blue]) >
(GammaExpansionTab[p2^.red] shl RedShift + not GammaExpansionTab[p2^.blue]);
end;
function IsRInvBGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
with p^ do
result := (GammaExpansionTab[red] shl RedShift + not GammaExpansionTab[blue]) >= v;
end;
function IsRInvGBGreater(p1, p2: PBGRAPixel
): boolean;
begin
result := (GammaExpansionTab[p1^.red] shl RedShift + ((not GammaExpansionTab[p1^.green]) shl GreenShift) + not GammaExpansionTab[p1^.blue]) >
(GammaExpansionTab[p2^.red] shl RedShift + ((not GammaExpansionTab[p2^.green]) shl GreenShift) + not GammaExpansionTab[p2^.blue]);
end;
function IsRInvGBGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
with p^ do
result := (GammaExpansionTab[red] shl RedShift + ((not GammaExpansionTab[green]) shl GreenShift) + not GammaExpansionTab[blue]) >= v;
end;
function IsGInvRBGreater(p1, p2: PBGRAPixel
): boolean;
begin
result := (GammaExpansionTab[p1^.green] shl GreenShift + ((not GammaExpansionTab[p1^.red]) shl RedShift) + not GammaExpansionTab[p1^.blue]) >
(GammaExpansionTab[p2^.green] shl GreenShift + ((not GammaExpansionTab[p2^.red]) shl RedShift) + not GammaExpansionTab[p2^.blue]);
end;
function IsGInvRBGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
with p^ do
result := (GammaExpansionTab[green] shl GreenShift + ((not GammaExpansionTab[red]) shl RedShift) + not GammaExpansionTab[blue]) >= v;
end;
function IsBInvRGGreater(p1, p2: PBGRAPixel
): boolean;
begin
result := (GammaExpansionTab[p1^.blue] + ((not GammaExpansionTab[p1^.red]) shl RedShift) + ((not GammaExpansionTab[p1^.green]) shl GreenShift)) >
(GammaExpansionTab[p2^.blue] + ((not GammaExpansionTab[p2^.red]) shl RedShift) + ((not GammaExpansionTab[p2^.green]) shl GreenShift));
end;
function IsBInvRGGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
with p^ do
result := (GammaExpansionTab[blue] + ((not GammaExpansionTab[red]) shl RedShift) + ((not GammaExpansionTab[green]) shl GreenShift)) >= v;
end;
function IsSaturationGreater(p1, p2: PBGRAPixel): boolean;
begin
result := GetDimensionValue(p1^,cdSaturation) > GetDimensionValue(p2^,cdSaturation);
end;
function IsSaturationGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
result := GetDimensionValue(p^,cdSaturation) >= v;
end;
function IsRedGreater(p1, p2: PBGRAPixel): boolean;
begin
result := p1^.red > p2^.red;
end;
function IsRedGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
result := GammaExpansionTab[p^.red] shl RedShift >= v;
end;
function IsGreenGreater(p1, p2: PBGRAPixel
): boolean;
begin
result := p1^.green > p2^.green;
end;
function IsGreenGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
result := GammaExpansionTab[p^.green] shl GreenShift >= v;
end;
function IsBlueGreater(p1, p2: PBGRAPixel
): boolean;
begin
result := p1^.blue > p2^.blue;
end;
function IsBlueGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
result := GammaExpansionTab[p^.blue] >= v;
end;
function IsAlphaGreater(p1, p2: PBGRAPixel
): boolean;
begin
result := p1^.alpha > p2^.alpha;
end;
function IsAlphaGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
begin
result := (p^.alpha + p^.alpha shl 8) shl AlphaShift >= v;
end;
function IsDWordGreater(p1, p2: PBGRAPixel
): boolean;
{$IFDEF BDS}var _BGRADWord, _BGRADWord2 : BGRADWord;{$ENDIF}//#
begin
{$IFDEF BDS}
move(p1^ , _BGRADWord, sizeof(BGRADWord));
move(p2^ , _BGRADWord2, sizeof(BGRADWord));
result := _BGRADWord > _BGRADWord2;
{$ELSE}//#
result := BGRADWord(p1^) > BGRADWord(p2^);
{$ENDIF};
end;
function IsDWordGreaterThanValue(p: PBGRAPixel;
v: UInt32): boolean;
{$IFDEF BDS}var _BGRADWord : BGRADWord;{$ENDIF}//#
begin
{$IFDEF BDS}
move(p^ , _BGRADWord, sizeof(BGRADWord));
result := _BGRADWord >= v;
{$ELSE}//#
result := BGRADWord(p^) >= v;
{$ENDIF};
end;
function GetPixelStrictComparer(ADimension: TColorDimension
): TIsChannelStrictlyGreaterFunc;
begin
case ADimension of
cdFast: result := @IsDWordGreater;
cdRed: result := @IsRedGreater;
cdGreen: result := @IsGreenGreater;
cdBlue: result := @IsBlueGreater;
cdAlpha: result := @IsAlphaGreater;
cdRGB: result := @IsRGBGreater;
cdRG: result := @IsRGGreater;
cdGB: result := @IsGBGreater;
cdRB: result := @IsRBGreater;
cdRInvG: result := @IsRInvGGreater;
cdGInvB: result := @IsGInvBGreater;
cdRInvB: result := @IsRInvBGreater;
cdRInvGB: result := @IsRInvGBGreater;
cdGInvRB: result := @IsGInvRBGreater;
cdBInvRG: result := @IsBInvRGGreater;
cdSaturation: result := @IsSaturationGreater;
else raise Exception.Create('Unknown dimension');
end;
end;
function GetPixelValueComparer(ADimension: TColorDimension
): TIsChannelGreaterThanOrEqualToValueFunc;
begin
case ADimension of
cdFast: result := @IsDWordGreaterThanValue;
cdRed: result := @IsRedGreaterThanValue;
cdGreen: result := @IsGreenGreaterThanValue;
cdBlue: result := @IsBlueGreaterThanValue;
cdAlpha: result := @IsAlphaGreaterThanValue;
cdRGB: result := @IsRGBGreaterThanValue;
cdRG: result := @IsRGGreaterThanValue;
cdGB: result := @IsGBGreaterThanValue;
cdRB: result := @IsRBGreaterThanValue;
cdRInvG: result := @IsRInvGGreaterThanValue;
cdGInvB: result := @IsGInvBGreaterThanValue;
cdRInvB: result := @IsRInvBGreaterThanValue;
cdRInvGB: result := @IsRInvGBGreaterThanValue;
cdGInvRB: result := @IsGInvRBGreaterThanValue;
cdBInvRG: result := @IsBInvRGGreaterThanValue;
cdSaturation: result := @IsSaturationGreaterThanValue;
else raise Exception.Create('Unknown dimension');
end;
end;
function BGRAColorCount(ABitmap: TBGRACustomBitmap;
AAlpha: TAlphaChannelPaletteOption): integer;
var
box: TBGRAColorBox;
begin
box := TBGRAColorBox.Create(AllColorDimensions,ABitmap,AAlpha);
result := box.ColorCount[True];
box.Free;
end;
const
ApproxPaletteDimensions = [cdAlpha,cdRInvG,cdGInvB,cdRInvB,cdRInvGB,cdGInvRB,cdBInvRG,cdRGB];
{ TBGRAApproxPaletteViaLargerPalette }
function TBGRAApproxPaletteViaLargerPalette.FindNearestLargerColorIndex(
AValue: TBGRAPixel): integer;
begin
result := FLarger.FindNearestColorIndex(AValue);
end;
function TBGRAApproxPaletteViaLargerPalette.SlowFindNearestColorIndex(
AValue: TBGRAPixel): integer;
var diff,curDiff: BGRANativeInt;
i: BGRANativeInt;
begin
if AValue.alpha = 0 then
begin
result := FTransparentColorIndex;
exit;
end;
diff := BGRAWordDiff(AValue, FColors[0].Color);
result := 0;
for i := 0 to high(FColors) do
begin
curDiff := BGRAWordDiff(AValue, FColors[i].Color);
if curDiff < diff then
begin
result := i;
diff := curDiff;
end;
end;
end;
constructor TBGRAApproxPaletteViaLargerPalette.Create(
const AColors: ArrayOfTBGRAPixel; ALarger: TBGRACustomApproxPalette; ALargerOwned: boolean);
var i: integer;
largeWeighted: ArrayOfWeightedColor;
begin
inherited Create(AColors);
FTransparentColorIndex:= -1;
for i := 0 to high(FColors) do
begin
FColors[i].Weight := 0;
if FColors[i].Color.alpha = 0 then FTransparentColorIndex:= i;
end;
FLarger := ALarger;
FLargerOwned := ALargerOwned;
largeWeighted := FLarger.GetAsArrayOfWeightedColor;
setlength(FLargerColors, length(largeWeighted));
for i := 0 to high(FLargerColors) do
with FLargerColors[i] do
begin
approxColorIndex := SlowFindNearestColorIndex(largeWeighted[i].Color);
if approxColorIndex = -1 then
approxColor := BGRAPixelTransparent
else
begin
approxColor := FColors[approxColorIndex].Color;
inc(FColors[approxColorIndex].Weight, largeWeighted[i].Weight);
end;
end;
end;
destructor TBGRAApproxPaletteViaLargerPalette.Destroy;
begin
if FLargerOwned then FreeAndNil(FLarger);
inherited Destroy;
end;
function TBGRAApproxPaletteViaLargerPalette.FindNearestColor(AValue: TBGRAPixel
): TBGRAPixel;
var index: integer;
begin
index := FindNearestLargerColorIndex(AValue);
if index = -1 then
result := BGRAPixelTransparent
else
Result:= FLargerColors[index].approxColor;
end;
function TBGRAApproxPaletteViaLargerPalette.FindNearestColorIndex(
AValue: TBGRAPixel): integer;
var index: integer;
begin
index := FindNearestLargerColorIndex(AValue);
if index = -1 then
result := -1
else
Result:= FLargerColors[index].approxColorIndex;
end;
function TBGRAApproxPaletteViaLargerPalette.GetAsArrayOfWeightedColor: ArrayOfWeightedColor;
var
i: Integer;
begin
setlength(result, length(FColors));
for i := 0 to high(FColors) do
result[i] := FColors[i];
end;
{ TBGRAApproxPalette }
function TBGRAApproxPalette.GetCount: integer;
begin
result := length(FColors);
end;
function TBGRAApproxPalette.GetColorByIndex(AIndex: integer): TBGRAPixel;
begin
if (AIndex < 0) or (AIndex >= length(FColors)) then
raise ERangeError.Create('Index out of bounds');
result := FColors[AIndex].Color;
end;
function TBGRAApproxPalette.GetWeightByIndex(AIndex: integer): UInt32;
begin
if (AIndex < 0) or (AIndex >= length(FColors)) then
raise ERangeError.Create('Index out of bounds');
result := FColors[AIndex].Weight;
end;
procedure TBGRAApproxPalette.Init(const AColors: ArrayOfTBGRAPixel);
var
weightedColors: ArrayOfWeightedColor;
i: BGRANativeInt;
begin
setlength(weightedColors, length(AColors));
for i := 0 to high(weightedColors) do
with weightedColors[i] do
begin
Color := AColors[i];
Weight := 1;
end;
FTree := TBGRAColorTree.Create(TBGRAColorBox.Create(ApproxPaletteDimensions,weightedColors,True),True);
FTree.SplitIntoPalette(length(AColors),blApparentInterval,lcAverage);
FColors := FTree.GetAsArrayOfWeightedColors;
end;
constructor TBGRAApproxPalette.Create(const AColors: ArrayOfTBGRAPixel);
begin
Init(AColors);
end;
constructor TBGRAApproxPalette.Create(const AColors: ArrayOfWeightedColor);
begin
FTree := TBGRAColorTree.Create(TBGRAColorBox.Create(ApproxPaletteDimensions,AColors,True),True);
FTree.SplitIntoPalette(length(AColors),blApparentInterval,lcAverage);
FColors := FTree.GetAsArrayOfWeightedColors;
end;
constructor TBGRAApproxPalette.Create(AOwnedSplitTree: TBGRAColorTree);
begin
FTree := AOwnedSplitTree;
FColors := FTree.GetAsArrayOfWeightedColors;
end;
destructor TBGRAApproxPalette.Destroy;
begin
FreeAndNil(FTree);
inherited Destroy;
end;
function TBGRAApproxPalette.ContainsColor(AValue: TBGRAPixel): boolean;
begin
result := (IndexOfColor(AValue)<>-1);
end;
function TBGRAApproxPalette.IndexOfColor(AValue: TBGRAPixel): integer;
{$IFDEF BDS}var _BGRADWord : BGRADWord;{$ENDIF}//#
begin
result := FTree.ApproximateColorIndex(AValue);
{$IFDEF BDS}move(AValue , _BGRADWord, sizeof(BGRADWord));{$ENDIF}
if (result <> -1) and not (BGRADWord(FColors[result].Color) = {$IFDEF BDS}_BGRADWord{$ELSE}BGRADWord(AValue){$ENDIF}) then result := -1;
end;
function TBGRAApproxPalette.FindNearestColor(AValue: TBGRAPixel): TBGRAPixel;
begin
result := FTree.ApproximateColor(AValue);
end;
function TBGRAApproxPalette.FindNearestColorIndex(AValue: TBGRAPixel): integer;
begin
result := FTree.ApproximateColorIndex(AValue);
end;
function TBGRAApproxPalette.GetAsArrayOfColor: ArrayOfTBGRAPixel;
var
i: BGRANativeInt;
begin
setlength(result, length(FColors));
for i := 0 to high(result) do
result[i] := FColors[i].Color;
end;
function TBGRAApproxPalette.GetAsArrayOfWeightedColor: ArrayOfWeightedColor;
var
i: BGRANativeInt;
begin
if Assigned(FTree) then
result := FTree.GetAsArrayOfWeightedColors
else
begin
setlength(result, length(FColors));
for i := 0 to high(result) do
result[i] := FColors[i];
end;
end;
{ TBGRAColorQuantizer }
procedure TBGRAColorQuantizer.Init(ABox: TBGRAColorBox);
begin
FColors := ABox.FColors;
if ABox.HasPureTransparentColor then
begin
setlength(FColors,length(FColors)+1);
with FColors[high(FColors)] do
begin
Color := BGRAPixelTransparent;
Weight:= ABox.PureTransparentColorCount;
end;
end;
ABox.FColors := nil;
ABox.Free;
FReductionColorCount := 256;
FReductionKeepContrast := true;
end;
procedure TBGRAColorQuantizer.SetReductionColorCount(AValue: Integer);
begin
if AValue < 1 then AValue := 1;
if FReductionColorCount=AValue then Exit;
FReductionColorCount:=AValue;
FreeAndNil(FPalette);
end;
procedure TBGRAColorQuantizer.NormalizeArrayOfColors(
AColors: ArrayOfTBGRAPixel; ARedBounds, AGreenBounds, ABlueBounds,
AAlphaBounds: TDimensionMinMax; AUniform: boolean);
var
curRedBounds, curGreenBounds, curBlueBounds, curAlphaBounds: TDimensionMinMax;
RedSub,RedMul,RedDiv,RedAdd: BGRANativeUInt;
GreenSub,GreenMul,GreenDiv,GreenAdd: BGRANativeUInt;
BlueSub,BlueMul,BlueDiv,BlueAdd: BGRANativeUInt;
AlphaSub,AlphaMul,AlphaDiv,AlphaAdd: BGRANativeUInt;
i: BGRANativeInt;
colorBounds: TDimensionMinMax;
begin
if length(AColors)=0 then exit;
if AUniform then
begin
colorBounds := ABlueBounds;
colorBounds.GrowToInclude(AGreenBounds.Minimum shr GreenShift);
colorBounds.GrowToInclude(AGreenBounds.Maximum shr GreenShift);
colorBounds.GrowToInclude(ARedBounds.Minimum shr RedShift);
colorBounds.GrowToInclude(ARedBounds.Maximum shr RedShift);
NormalizeArrayOfColors(AColors, colorBounds, AAlphaBounds);
exit;
end;
curRedBounds.SetAsPoint(GetDimensionValue(AColors[0],cdRed));
curGreenBounds.SetAsPoint(GetDimensionValue(AColors[0],cdGreen));
curBlueBounds.SetAsPoint(GetDimensionValue(AColors[0],cdBlue));
curAlphaBounds.SetAsPoint(GetDimensionValue(AColors[0],cdAlpha));
for i := 1 to high(AColors) do
with AColors[i] do
begin
curRedBounds.GrowToInclude(GetDimensionValue(AColors[i],cdRed));
curGreenBounds.GrowToInclude(GetDimensionValue(AColors[i],cdGreen));
curBlueBounds.GrowToInclude(GetDimensionValue(AColors[i],cdBlue));
curAlphaBounds.GrowToInclude(GetDimensionValue(AColors[i],cdAlpha));
end;
RedSub := curRedBounds.Minimum shr RedShift;
RedMul := ARedBounds.Size shr RedShift;
RedDiv := curRedBounds.Size shr RedShift;
RedAdd := ARedBounds.Minimum shr RedShift;
if RedDiv = 0 then RedDiv := 1;
GreenSub := curGreenBounds.Minimum shr GreenShift;
GreenMul := AGreenBounds.Size shr GreenShift;
GreenDiv := curGreenBounds.Size shr GreenShift;
GreenAdd := AGreenBounds.Minimum shr GreenShift;
if GreenDiv = 0 then GreenDiv := 1;
BlueSub := curBlueBounds.Minimum;
BlueMul := ABlueBounds.Size;
BlueDiv := curBlueBounds.Size;
BlueAdd := ABlueBounds.Minimum;
if BlueDiv = 0 then BlueDiv := 1;
AlphaSub := curAlphaBounds.Minimum shr (AlphaShift+8);
AlphaMul := AAlphaBounds.Size shr (AlphaShift+8);
AlphaDiv := curAlphaBounds.Size shr (AlphaShift+8);
AlphaAdd := AAlphaBounds.Minimum shr (AlphaShift+8);
if AlphaDiv = 0 then AlphaDiv := 1;
for i := 0 to high(AColors) do
with AColors[i] do
begin
red := GammaCompressionTab[((GammaExpansionTab[red]-RedSub)*RedMul+(RedDiv shr 1)) div RedDiv + RedAdd];
green := GammaCompressionTab[((GammaExpansionTab[green]-GreenSub)*GreenMul+(GreenDiv shr 1)) div GreenDiv + GreenAdd];
blue := GammaCompressionTab[((GammaExpansionTab[blue]-BlueSub)*BlueMul+(BlueDiv shr 1)) div BlueDiv + BlueAdd];
alpha := ((alpha-AlphaSub)*AlphaMul+(AlphaDiv shr 1)) div AlphaDiv + AlphaAdd;
end;
end;
procedure TBGRAColorQuantizer.NormalizeArrayOfColors(
AColors: ArrayOfTBGRAPixel; AColorBounds, AAlphaBounds: TDimensionMinMax);
var
curColorBounds, curAlphaBounds: TDimensionMinMax;
ColorSub,ColorMul,ColorDiv,ColorAdd: BGRANativeUInt;
AlphaSub,AlphaMul,AlphaDiv,AlphaAdd: BGRANativeUInt;
i: BGRANativeInt;
begin
if length(AColors)=0 then exit;
curColorBounds.SetAsPoint(GammaExpansionTab[AColors[0].red]);
curColorBounds.GrowToInclude(GammaExpansionTab[AColors[0].green]);
curColorBounds.GrowToInclude(GammaExpansionTab[AColors[0].blue]);
curAlphaBounds.SetAsPoint(AColors[0].alpha);
for i := 1 to high(AColors) do
with AColors[i] do
begin
curColorBounds.GrowToInclude(GammaExpansionTab[red]);
curColorBounds.GrowToInclude(GammaExpansionTab[green]);
curColorBounds.GrowToInclude(GammaExpansionTab[blue]);
curAlphaBounds.GrowToInclude(alpha);
end;
ColorSub := curColorBounds.Minimum;
ColorMul := AColorBounds.Size;
ColorDiv := curColorBounds.Size;
ColorAdd := AColorBounds.Minimum;
if ColorDiv = 0 then ColorDiv := 1;
AlphaSub := curAlphaBounds.Minimum;
AlphaMul := AAlphaBounds.Size shr 8;
AlphaDiv := curAlphaBounds.Size;
AlphaAdd := AAlphaBounds.Minimum shr 8;
if AlphaDiv = 0 then AlphaDiv := 1;
for i := 0 to high(AColors) do
with AColors[i] do
begin
red := GammaCompressionTab[((GammaExpansionTab[red]-ColorSub)*ColorMul+(ColorDiv shr 1)) div ColorDiv + ColorAdd];
green := GammaCompressionTab[((GammaExpansionTab[green]-ColorSub)*ColorMul+(ColorDiv shr 1)) div ColorDiv + ColorAdd];
blue := GammaCompressionTab[((GammaExpansionTab[blue]-ColorSub)*ColorMul+(ColorDiv shr 1)) div ColorDiv + ColorAdd];
alpha := ((alpha-AlphaSub)*AlphaMul+(AlphaDiv shr 1)) div AlphaDiv + AlphaAdd;
end;
end;
function TBGRAColorQuantizer.GetSourceColorCount: Integer;
begin
result := length(FColors);
end;
function TBGRAColorQuantizer.GetReductionColorCount: integer;
begin
result := FReductionColorCount;
end;
function TBGRAColorQuantizer.GetPalette: TBGRACustomApproxPalette;
var
tree: TBGRAColorTree;
procedure MakeTreeErrorDiffusionFriendly;
var moreColors: ArrayOfWeightedColor;
box: TBGRAColorBox;
begin
moreColors := tree.GetAsArrayOfWeightedColors;
tree.free;
box := TBGRAColorBox.Create([cdRed,cdGreen,cdBlue,cdAlpha],moreColors,True);
tree := TBGRAColorTree.Create(box,True);
tree.SplitIntoPalette(box.ColorCount[true], blApparentInterval, lcAverage);
end;
var
originalBox: TBGRAColorBox;
colors: ArrayOfTBGRAPixel;
bounds: array[TColorDimension] of TDimensionMinMax;
nbLarge,nbOriginal: integer;
begin
if not Assigned(FPalette) then
if FReductionColorCount >= length(FColors) then
begin
originalBox := TBGRAColorBox.Create([cdRed,cdGreen,cdBlue,cdAlpha],FColors, False);
tree := TBGRAColorTree.Create(originalBox,True);
tree.SplitIntoPalette(originalBox.ColorCount[true], blApparentInterval, lcAverage);
FPalette := TBGRAApproxPalette.Create(tree);
end else
begin
originalBox := TBGRAColorBox.Create(AllColorDimensions, FColors, False);
bounds[cdRed] := originalBox.Bounds[cdRed];
bounds[cdGreen] := originalBox.Bounds[cdGreen];
bounds[cdBlue] := originalBox.Bounds[cdBlue];
bounds[cdAlpha] := originalBox.Bounds[cdAlpha];
if originalBox.HasPureTransparentColor then bounds[cdAlpha].Minimum := 0;
if FReductionColorCount = 1 then
begin
setlength(colors,1);
colors[0] := originalBox.AverageColor;
originalBox.Free;
FPalette := TBGRAApproxPalette.Create(colors);
end else
begin
tree := TBGRAColorTree.Create(originalBox,True);
if FReductionColorCount <= 64 then
begin
nbLarge := 128;
nbOriginal := originalBox.ColorCount[True];
if nbOriginal < 128 then nbLarge:= nbOriginal;
colors := tree.SplitIntoPaletteWithSubPalette(nbLarge, blMix,lcMix, FReductionColorCount);
MakeTreeErrorDiffusionFriendly;
if FReductionColorCount <= 4 then
NormalizeArrayOfColors(colors, bounds[cdRed],bounds[cdGreen],bounds[cdBlue],bounds[cdAlpha],true);
FPalette := TBGRAApproxPaletteViaLargerPalette.Create(colors, TBGRAApproxPalette.Create(tree), True);