forked from bgrabitmap/BGRABitmapDelphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgrapalette.pas
1471 lines (1326 loc) · 41.4 KB
/
bgrapalette.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 BGRAPalette;
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType, BGRAGraphics,{$ENDIF} AvgLvlTree, BGRABitmapTypes, FPImage;
const
MaxLastAddedColors = 10;
type
TBGRAPaletteFormat = integer;
const
palUnknown : TBGRAPaletteFormat = 0;
palPaintDotNet : TBGRAPaletteFormat = 1;
palGimp : TBGRAPaletteFormat = 2;
palAdobeSwatchExchange : TBGRAPaletteFormat = 3;
palKOffice : TBGRAPaletteFormat = 4;
palJascPSP : TBGRAPaletteFormat = 5;
palCustom : TBGRAPaletteFormat = 100;
type
TBGRAIndexedPaletteEntry = packed record
Color: TBGRAPixel;
Index: UInt32;
end;
PBGRAIndexedPaletteEntry = ^TBGRAIndexedPaletteEntry;
TBGRAWeightedPaletteEntry = packed record
Color: TBGRAPixel;
Weight: UInt32;
end;
PBGRAWeightedPaletteEntry = ^TBGRAWeightedPaletteEntry;
ArrayOfWeightedColor = array of TBGRAWeightedPaletteEntry;
TBGRAPixelComparer = function (p1,p2 : PBGRAPixel): boolean;
{ TBGRACustomPalette }
TBGRACustomPalette = class
private
function GetDominantColor: TBGRAPixel;
protected
function GetCount: integer; virtual; abstract;
function GetColorByIndex(AIndex: integer): TBGRAPixel; virtual; abstract;
public
function ContainsColor(AValue: TBGRAPixel): boolean; virtual; abstract;
function IndexOfColor(AValue: TBGRAPixel): integer; virtual; abstract;
function GetAsArrayOfColor: ArrayOfTBGRAPixel; virtual; abstract;
function GetAsArrayOfWeightedColor: ArrayOfWeightedColor; virtual; abstract;
procedure AssignTo(AImage: TFPCustomImage); overload;
procedure AssignTo(APalette: TFPPalette); overload;
property DominantColor: TBGRAPixel read GetDominantColor;
property Count: integer read GetCount;
property Color[AIndex: integer]: TBGRAPixel read GetColorByIndex;
end;
{ TBGRAAvgLvlPalette }
TBGRAAvgLvlPalette = class(TBGRACustomPalette)
protected
FTree: TAvgLvlTree;
FArray: array of PBGRAPixel;
FLastAddedColors: packed array[0..MaxLastAddedColors-1] of PBGRAPixel;
FLastAddedColorCount: integer;
function GetCount: integer; override;
function GetColorByIndex(AIndex: integer): TBGRAPixel; override;
function OnCompareItems({%H-}Tree: TAvgLvlTree; Data1, Data2: Pointer): integer; virtual;
procedure FreeEntry(AEntry: PBGRAPixel); virtual; abstract;
procedure NeedArray; virtual;
procedure ClearArray; virtual;
procedure AddLastColor(AColor: PBGRAPixel);
function GetLastColor(AValue: TBGRAPixel): PBGRAPixel;
procedure ClearLastColors;
public
constructor Create; overload;
function ContainsColor(AValue: TBGRAPixel): boolean; override;
function IndexOfColor(AValue: TBGRAPixel): integer; override;
procedure Clear; virtual;
destructor Destroy; override;
function GetAsArrayOfColor: ArrayOfTBGRAPixel; override;
function GetAsArrayOfWeightedColor: ArrayOfWeightedColor; override;
end;
{ TBGRAPalette }
TBGRAPalette = class(TBGRAAvgLvlPalette)
protected
function CreateEntry(AColor: TBGRAPixel): PBGRAPixel; virtual;
procedure FreeEntry(AEntry: PBGRAPixel); override;
procedure IncludePixel(PPixel: PBGRAPixel); virtual;
procedure ExceptionUnknownPaletteFormat;
procedure ExceptionInvalidPaletteFormat;
public
constructor Create(ABitmap: TBGRACustomBitmap); overload; virtual;
constructor Create(APalette: TBGRACustomPalette); overload; virtual;
constructor Create(AColors: ArrayOfTBGRAPixel); overload; virtual;
constructor Create(AColors: ArrayOfWeightedColor); overload; virtual;
function AddColor(AValue: TBGRAPixel): boolean; virtual;
procedure AddColors(ABitmap: TBGRACustomBitmap); overload; virtual;
procedure AddColors(APalette: TBGRACustomPalette); overload; virtual;
function RemoveColor(AValue: TBGRAPixel): boolean; virtual;
procedure LoadFromFile(AFilenameUTF8: string); virtual;
procedure LoadFromStream(AStream: TStream; AFormat: TBGRAPaletteFormat); virtual;
procedure SaveToFile(AFilenameUTF8: string); virtual;
procedure SaveToStream(AStream: TStream; AFormat: TBGRAPaletteFormat); virtual;
function DetectPaletteFormat(AStream: TStream): TBGRAPaletteFormat; overload; virtual;
function DetectPaletteFormat(AFilenameUTF8: string): TBGRAPaletteFormat; overload;
function SuggestPaletteFormat(AFilenameUTF8: string): TBGRAPaletteFormat; virtual;
end;
{ TBGRAIndexedPalette }
TBGRAIndexedPalette = class(TBGRAPalette)
private
FCurrentIndex: UInt32;
protected
procedure NeedArray; override;
function CreateEntry(AColor: TBGRAPixel): PBGRAPixel; override;
procedure FreeEntry(AEntry: PBGRAPixel); override;
public
function RemoveColor({%H-}AValue: TBGRAPixel): boolean; override;
function IndexOfColor(AValue: TBGRAPixel): integer; override;
procedure Clear; override;
end;
{ TBGRAWeightedPalette }
TBGRAWeightedPalette = class(TBGRAPalette)
private
protected
function CreateEntry(AColor: TBGRAPixel): PBGRAPixel; override;
procedure FreeEntry(AEntry: PBGRAPixel); override;
function GetWeightByIndex(AIndex: Integer): UInt32; virtual;
procedure IncludePixel(PPixel: PBGRAPixel); override;
public
constructor Create(AColors: ArrayOfWeightedColor); override;
function GetAsArrayOfWeightedColor: ArrayOfWeightedColor; override;
function IncColor(AValue: TBGRAPixel; out NewWeight: UInt32): boolean;
function DecColor(AValue: TBGRAPixel; out NewWeight: UInt32): boolean;
property Weight[AIndex: Integer]: UInt32 read GetWeightByIndex;
end;
{ TBGRAReferencePalette }
TBGRAReferencePalette = class(TBGRAAvgLvlPalette)
protected
procedure FreeEntry({%H-}AEntry: PBGRAPixel); override;
public
function AddColor(AValue: PBGRAPixel): boolean;
function RemoveColor(AValue: PBGRAPixel): boolean;
end;
{ TBGRACustomApproxPalette }
TBGRACustomApproxPalette = class(TBGRACustomPalette)
private
function FindNearestColorIgnoreAlpha(AValue: TBGRAPixel): TBGRAPixel; {$ifdef inline}inline;{$endif}
function FindNearestColorIndexIgnoreAlpha(AValue: TBGRAPixel): integer; {$ifdef inline}inline;{$endif}
protected
function GetWeightByIndex({%H-}AIndex: Integer): UInt32; virtual;
public
function FindNearestColor(AValue: TBGRAPixel; AIgnoreAlpha: boolean): TBGRAPixel; overload;
function FindNearestColor(AValue: TBGRAPixel): TBGRAPixel; overload; virtual; abstract;
function FindNearestColorIndex(AValue: TBGRAPixel; AIgnoreAlpha: boolean): integer; overload;
function FindNearestColorIndex(AValue: TBGRAPixel): integer; overload; virtual; abstract;
property Weight[AIndex: Integer]: UInt32 read GetWeightByIndex;
end;
{ TBGRA16BitPalette }
TBGRA16BitPalette = class(TBGRACustomApproxPalette)
protected
function GetCount: integer; override;
function GetColorByIndex(AIndex: integer): TBGRAPixel; override;
public
function ContainsColor(AValue: TBGRAPixel): boolean; override;
function IndexOfColor(AValue: TBGRAPixel): integer; override;
function GetAsArrayOfColor: ArrayOfTBGRAPixel; override;
function GetAsArrayOfWeightedColor: ArrayOfWeightedColor; override;
function FindNearestColor(AValue: TBGRAPixel): TBGRAPixel; override;
function FindNearestColorIndex(AValue: TBGRAPixel): integer; override;
end;
{ TBGRACustomColorQuantizer }
TBGRACustomColorQuantizer = class
protected
function GetDominantColor: TBGRAPixel; virtual;
function GetPalette: TBGRACustomApproxPalette; virtual; abstract;
function GetSourceColor(AIndex: integer): TBGRAPixel; virtual; abstract;
function GetSourceColorCount: Integer; virtual; abstract;
function GetReductionColorCount: integer; virtual; abstract;
procedure SetReductionColorCount(AValue: Integer); virtual; abstract;
public
constructor Create(APalette: TBGRACustomPalette; ASeparateAlphaChannel: boolean); overload; virtual; abstract;
constructor Create(AColors: array of TBGRAPixel; ASeparateAlphaChannel: boolean); overload;
constructor Create(ABitmap: TBGRACustomBitmap; AAlpha: TAlphaChannelPaletteOption); overload; virtual; abstract;
constructor Create(APalette: TBGRACustomPalette; ASeparateAlphaChannel: boolean; AReductionColorCount: integer); overload; virtual; abstract;
constructor Create(AColors: array of TBGRAPixel; ASeparateAlphaChannel: boolean; AReductionColorCount: integer); overload;
constructor Create(ABitmap: TBGRACustomBitmap; AAlpha: TAlphaChannelPaletteOption; AReductionColorCount: integer); overload; virtual; abstract;
procedure ApplyDitheringInplace(AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap; ABounds: TRect); overload; virtual; abstract;
procedure ApplyDitheringInplace(AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap); overload;
function GetDitheredBitmap(AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap; ABounds: TRect): TBGRACustomBitmap; overload; virtual; abstract;
function GetDitheredBitmap(AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap): TBGRACustomBitmap; overload;
procedure SaveBitmapToFile(AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap; AFilenameUTF8: string); overload;
procedure SaveBitmapToFile(AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap; AFilenameUTF8: string; AFormat: TBGRAImageFormat); overload;
procedure SaveBitmapToStream(AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap; AStream: TStream; AFormat: TBGRAImageFormat); virtual; abstract;
function GetDitheredBitmapIndexedData(ABitDepth: integer; AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap; out AScanlineSize: BGRAPtrInt): Pointer; overload;
function GetDitheredBitmapIndexedData(ABitDepth: integer; AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap): Pointer; overload;
function GetDitheredBitmapIndexedData(ABitDepth: integer; AByteOrder: TRawImageByteOrder; AAlgorithm: TDitheringAlgorithm;
ABitmap: TBGRACustomBitmap; out AScanlineSize: BGRAPtrInt): Pointer; overload; virtual; abstract;
property SourceColorCount: Integer read GetSourceColorCount;
property SourceColor[AIndex: integer]: TBGRAPixel read GetSourceColor;
property ReductionColorCount: Integer read GetReductionColorCount write SetReductionColorCount;
property ReducedPalette: TBGRACustomApproxPalette read GetPalette;
property DominantColor: TBGRAPixel read GetDominantColor;
end;
TBGRAColorQuantizerAny = class of TBGRACustomColorQuantizer;
var
BGRAColorQuantizerFactory: TBGRAColorQuantizerAny;
function BGRARequiredBitDepth(ABitmap: TBGRACustomBitmap; AAlpha: TAlphaChannelPaletteOption): integer; overload;
function BGRARequiredBitDepth(APalette: TBGRACustomPalette): integer; overload;
type
TPaletteReaderProc = function(APalette: TBGRAPalette; AStream: TStream): boolean;
TPaletteWriterProc = procedure(APalette: TBGRAPalette; AStream: TStream);
TCheckPaletteFormatProc = function(ABuf256: string): boolean;
procedure BGRARegisterPaletteFormat(AFormatIndex: TBGRAPaletteFormat; AExtension: string; ADescription: string;
AReadProc: TPaletteReaderProc; AWriteProc: TPaletteWriterProc; ACheckFormatProc: TCheckPaletteFormatProc);
function BGRARegisteredPaletteFormatFilter(AAllSupportedDescription: string) : string;
procedure ArrayOfWeightedColor_QuickSort(AColors: ArrayOfWeightedColor; AMinIndex,
AMaxIndex: BGRANativeInt; AComparer: TBGRAPixelComparer = nil);
procedure ArrayOfWeightedColor_InsertionSort(AColors: ArrayOfWeightedColor; AMinIndex,
AMaxIndex: BGRANativeInt; AComparer: TBGRAPixelComparer = nil);
procedure ArrayOfTBGRAPixel_QuickSort(AColors: ArrayOfTBGRAPixel; AMinIndex,
AMaxIndex: BGRANativeInt; AComparer: TBGRAPixelComparer = nil);
procedure ArrayOfTBGRAPixel_InsertionSort(AColors: ArrayOfTBGRAPixel; AMinIndex,
AMaxIndex: BGRANativeInt; AComparer: TBGRAPixelComparer = nil);
implementation
uses BGRAUTF8 {$IFDEF FPC} ,bufstream{$ENDIF};
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;
const
InsertionSortLimit = 10;
procedure ArrayOfWeightedColor_InsertionSort(AColors: ArrayOfWeightedColor; AMinIndex,
AMaxIndex: BGRANativeInt; AComparer: TBGRAPixelComparer = nil);
var i,j,insertPos: BGRANativeInt;
compared: TBGRAWeightedPaletteEntry;
begin
if @AComparer = nil then AComparer := @IsDWordGreater;
for i := AMinIndex+1 to AMaxIndex do
begin
insertPos := i;
compared := AColors[i];
while (insertPos > AMinIndex) and AComparer(@AColors[insertPos-1].Color,@compared.Color) do
dec(insertPos);
if insertPos <> i then
begin
for j := i downto insertPos+1 do
AColors[j] := AColors[j-1];
AColors[insertPos] := compared;
end;
end;
end;
procedure ArrayOfWeightedColor_QuickSort(AColors: ArrayOfWeightedColor; AMinIndex,
AMaxIndex: BGRANativeInt; AComparer: TBGRAPixelComparer = nil);
var Pivot: TBGRAPixel;
CurMin,CurMax,i : BGRANativeInt;
procedure Swap(a,b: BGRANativeInt);
var Temp: TBGRAWeightedPaletteEntry;
begin
if a = b then exit;
Temp := AColors[a];
AColors[a] := AColors[b];
AColors[b] := Temp;
end;
begin
if @AComparer = nil then AComparer := @IsDWordGreater;
if AMaxIndex-AMinIndex+1 <= InsertionSortLimit then
begin
ArrayOfWeightedColor_InsertionSort(AColors,AMinIndex,AMaxIndex,AComparer);
exit;
end;
Pivot := AColors[(AMinIndex+AMaxIndex) shr 1].Color;
CurMin := AMinIndex;
CurMax := AMaxIndex;
i := CurMin;
while i < CurMax do
begin
if AComparer(@AColors[i].Color, @Pivot) then
begin
Swap(i, CurMax);
dec(CurMax);
end else
begin
if AComparer(@Pivot, @AColors[i].Color) then
begin
Swap(i, CurMin);
inc(CurMin);
end;
inc(i);
end;
end;
if AComparer(@Pivot, @AColors[i].Color) then
begin
Swap(i, CurMin);
inc(CurMin);
end;
if CurMin > AMinIndex then ArrayOfWeightedColor_QuickSort(AColors,AMinIndex,CurMin,AComparer);
if CurMax < AMaxIndex then ArrayOfWeightedColor_QuickSort(AColors,CurMax,AMaxIndex,AComparer);
end;
procedure ArrayOfTBGRAPixel_InsertionSort(AColors: ArrayOfTBGRAPixel; AMinIndex,
AMaxIndex: BGRANativeInt; AComparer: TBGRAPixelComparer = nil);
var i,j,insertPos: BGRANativeInt;
compared: TBGRAPixel;
begin
if @AComparer = nil then AComparer := @IsDWordGreater;
for i := AMinIndex+1 to AMaxIndex do
begin
insertPos := i;
compared := AColors[i];
while (insertPos > AMinIndex) and AComparer(@AColors[insertPos-1],@compared) do
dec(insertPos);
if insertPos <> i then
begin
for j := i downto insertPos+1 do
AColors[j] := AColors[j-1];
AColors[insertPos] := compared;
end;
end;
end;
procedure ArrayOfTBGRAPixel_QuickSort(AColors: ArrayOfTBGRAPixel; AMinIndex,
AMaxIndex: BGRANativeInt; AComparer: TBGRAPixelComparer = nil);
var Pivot: TBGRAPixel;
CurMin,CurMax,i : BGRANativeInt;
procedure Swap(a,b: BGRANativeInt);
var Temp: TBGRAPixel;
begin
if a = b then exit;
Temp := AColors[a];
AColors[a] := AColors[b];
AColors[b] := Temp;
end;
begin
if @AComparer = nil then AComparer := @IsDWordGreater;
if AMaxIndex-AMinIndex+1 <= InsertionSortLimit then
begin
ArrayOfTBGRAPixel_InsertionSort(AColors,AMinIndex,AMaxIndex,AComparer);
exit;
end;
Pivot := AColors[(AMinIndex+AMaxIndex) shr 1];
CurMin := AMinIndex;
CurMax := AMaxIndex;
i := CurMin;
while i < CurMax do
begin
if AComparer(@AColors[i], @Pivot) then
begin
Swap(i, CurMax);
dec(CurMax);
end else
begin
if AComparer(@Pivot, @AColors[i]) then
begin
Swap(i, CurMin);
inc(CurMin);
end;
inc(i);
end;
end;
if AComparer(@Pivot, @AColors[i]) then
begin
Swap(i, CurMin);
inc(CurMin);
end;
if CurMin > AMinIndex then ArrayOfTBGRAPixel_QuickSort(AColors,AMinIndex,CurMin,AComparer);
if CurMax < AMaxIndex then ArrayOfTBGRAPixel_QuickSort(AColors,CurMax,AMaxIndex,AComparer);
end;
{$i paletteformats.inc}
function BGRARequiredBitDepth(ABitmap: TBGRACustomBitmap; AAlpha: TAlphaChannelPaletteOption): integer;
var
palette: TBGRAPalette;
p: PBGRAPixel;
i: BGRANativeInt;
transparentEntry: boolean;
begin
palette := TBGRAPalette.Create;
p := ABitmap.Data;
transparentEntry := false;
if AAlpha = acIgnore then
begin
for i := ABitmap.NbPixels-1 downto 0 do
begin
palette.AddColor(BGRA(p^.red,p^.green,p^.blue));
inc(p);
if palette.Count > 256 then break;
end;
end else
if AAlpha = acTransparentEntry then
begin
for i := ABitmap.NbPixels-1 downto 0 do
begin
if p^.alpha < 128 then
transparentEntry:= true
else
palette.AddColor(BGRA(p^.red,p^.green,p^.blue));
inc(p);
if palette.Count > 256 then break;
end;
end else
begin
for i := ABitmap.NbPixels-1 downto 0 do
begin
palette.AddColor(p^);
inc(p);
if palette.Count > 256 then break;
end;
end;
if palette.Count+byte(transparentEntry) > 256 then
begin
if (AAlpha = acFullChannelInPalette) and ABitmap.HasTransparentPixels then
result := 32
else
if (AAlpha = acTransparentEntry) and ABitmap.HasTransparentPixels then
result := 25
else
result := 24;
end else
begin
result := 8;
while (result > 0) and (1 shl (result shr 1) >= palette.Count) do result := result shr 1;
end;
palette.Free;
end;
function BGRARequiredBitDepth(APalette: TBGRACustomPalette): integer;
var i: integer;
hasTransp: boolean;
begin
if APalette.Count > 256 then
begin
hasTransp := false;
for i := 0 to APalette.Count-1 do
if APalette.Color[i].alpha <> 255 then
begin
hasTransp:= true;
break;
end;
if hasTransp then
result := 32
else
result := 24;
end else
begin
result := 8;
while (result > 0) and (1 shl (result shr 1) >= APalette.Count) do result := result shr 1;
end;
end;
{ TBGRA16BitPalette }
function TBGRA16BitPalette.GetCount: integer;
begin
result := 65537;
end;
function TBGRA16BitPalette.GetColorByIndex(AIndex: integer): TBGRAPixel;
begin
if (AIndex >= 65536) or (AIndex < 0) then
result := BGRAPixelTransparent
else
result := Color16BitToBGRA(AIndex);
end;
function TBGRA16BitPalette.ContainsColor(AValue: TBGRAPixel): boolean;
begin
if AValue.alpha = 0 then
result := true
else
result := (AValue.alpha = 255) and BGRAPixelEqual(FindNearestColor(AValue),{=}AValue);
end;
function TBGRA16BitPalette.IndexOfColor(AValue: TBGRAPixel): integer;
var idx: integer;
begin
if AValue.Alpha = 0 then
result := 65536
else
begin
idx := BGRAToColor16Bit(AValue);
if BGRAPixelEqual(Color16BitToBGRA(idx),{=}AValue) then
result := idx
else
result := -1;
end;
end;
function TBGRA16BitPalette.GetAsArrayOfColor: ArrayOfTBGRAPixel;
begin
result := nil;
raise exception.Create('Palette too big');
end;
function TBGRA16BitPalette.GetAsArrayOfWeightedColor: ArrayOfWeightedColor;
begin
result := nil;
raise exception.Create('Palette too big');
end;
function TBGRA16BitPalette.FindNearestColor(AValue: TBGRAPixel): TBGRAPixel;
begin
if AValue.alpha = 0 then result := BGRAPixelTransparent
else
result := GetColorByIndex(BGRAToColor16Bit(AValue));
end;
function TBGRA16BitPalette.FindNearestColorIndex(AValue: TBGRAPixel): integer;
begin
result := BGRAToColor16Bit(AValue);
end;
{ TBGRAIndexedPalette }
procedure TBGRAIndexedPalette.NeedArray;
var Node: TAvgLvlTreeNode;
n: UInt32;
begin
n := Count;
if UInt32(length(FArray)) <> n then
begin
setLength(FArray,n);
for Node in FTree do
with PBGRAIndexedPaletteEntry(Node.Data)^ do
begin
if Index < n then //index is unsigned so always >= 0
FArray[Index] := @Color;
end;
end;
end;
function TBGRAIndexedPalette.CreateEntry(AColor: TBGRAPixel): PBGRAPixel;
{$IFnDEF FPC}
var
BGRAIndexedPaletteEntry : PBGRAIndexedPaletteEntry;
{$ENDIF}
begin
{$IFDEF FPC}
result := PBGRAPixel(GetMem(sizeOf(TBGRAIndexedPaletteEntry)));
{$ELSE}
GetMem(BGRAIndexedPaletteEntry, sizeOf(TBGRAIndexedPaletteEntry));
result := PBGRAPixel(BGRAIndexedPaletteEntry);
{$ENDIF}
result^ := AColor;
PBGRAIndexedPaletteEntry(result)^.Index := FCurrentIndex;
Inc(FCurrentIndex);
end;
procedure TBGRAIndexedPalette.FreeEntry(AEntry: PBGRAPixel);
begin
FreeMem(AEntry);
end;
function TBGRAIndexedPalette.RemoveColor(AValue: TBGRAPixel): boolean;
begin
Result:= false;
raise exception.Create('It is not possible to remove a color from an indexed palette');
end;
function TBGRAIndexedPalette.IndexOfColor(AValue: TBGRAPixel): integer;
Var Node: TAvgLvlTreeNode;
begin
Node := FTree.Find(@AValue);
if Assigned(Node) then
result := PBGRAIndexedPaletteEntry(Node.Data)^.Index
else
result := -1;
end;
procedure TBGRAIndexedPalette.Clear;
begin
inherited Clear;
FCurrentIndex := 0;
end;
{ TBGRACustomColorQuantizer }
function TBGRACustomColorQuantizer.GetDominantColor: TBGRAPixel;
begin
result := ReducedPalette.DominantColor;
end;
constructor TBGRACustomColorQuantizer.Create(AColors: array of TBGRAPixel;
ASeparateAlphaChannel: boolean);
var palette: TBGRAPalette;
i: Integer;
begin
palette := TBGRAPalette.Create;
for i := 0 to high(AColors) do
palette.AddColor(AColors[i]);
Create(palette, ASeparateAlphaChannel);
palette.Free;
end;
constructor TBGRACustomColorQuantizer.Create(AColors: array of TBGRAPixel;
ASeparateAlphaChannel: boolean; AReductionColorCount: integer);
var palette: TBGRAPalette;
i: Integer;
begin
palette := TBGRAPalette.Create;
for i := 0 to high(AColors) do
palette.AddColor(AColors[i]);
Create(palette, ASeparateAlphaChannel, AReductionColorCount);
palette.Free;
end;
procedure TBGRACustomColorQuantizer.ApplyDitheringInplace(
AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap);
begin
ApplyDitheringInplace(AAlgorithm, ABitmap, rect(0,0,ABitmap.Width,ABitmap.Height));
end;
function TBGRACustomColorQuantizer.GetDitheredBitmap(
AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap
): TBGRACustomBitmap;
begin
result := GetDitheredBitmap(AAlgorithm, ABitmap, rect(0,0,ABitmap.Width,ABitmap.Height));
end;
procedure TBGRACustomColorQuantizer.SaveBitmapToFile(
AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap;
AFilenameUTF8: string);
begin
SaveBitmapToFile(AAlgorithm, ABitmap, AFilenameUTF8, SuggestImageFormat(AFilenameUTF8));
end;
procedure TBGRACustomColorQuantizer.SaveBitmapToFile(
AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap;
AFilenameUTF8: string; AFormat: TBGRAImageFormat);
var
stream: TFileStreamUTF8;
begin
stream := TFileStreamUTF8.Create(AFilenameUTF8,fmCreate);
try
SaveBitmapToStream(AAlgorithm, ABitmap, stream, AFormat);
finally
stream.Free;
end;
end;
function TBGRACustomColorQuantizer.GetDitheredBitmapIndexedData(
ABitDepth: integer; AAlgorithm: TDitheringAlgorithm; ABitmap: TBGRACustomBitmap;
out AScanlineSize: BGRAPtrInt): Pointer;
begin
result := GetDitheredBitmapIndexedData(ABitDepth,
{$IFDEF ENDIAN_LITTLE}riboLSBFirst{$ELSE}riboMSBFirst{$endif},
AAlgorithm, ABitmap, AScanlineSize);
end;
function TBGRACustomColorQuantizer.GetDitheredBitmapIndexedData(
ABitDepth: integer; AAlgorithm: TDitheringAlgorithm;
ABitmap: TBGRACustomBitmap): Pointer;
var dummy: BGRAPtrInt;
begin
result := GetDitheredBitmapIndexedData(ABitDepth, AAlgorithm, ABitmap, dummy);
end;
{ TBGRACustomPalette }
function TBGRACustomPalette.GetDominantColor: TBGRAPixel;
var
w: ArrayOfWeightedColor;
i: Integer;
maxWeight, totalWeight: UInt32;
begin
result := BGRAWhite;
maxWeight := 0;
w := GetAsArrayOfWeightedColor;
totalWeight:= 0;
for i := 0 to high(w) do
inc(totalWeight, w[i].Weight);
for i := 0 to high(w) do
if (w[i].Weight > maxWeight) and (BGRAToGSBA(w[i].Color).saturation > 16000) then
begin
maxWeight:= w[i].Weight;
result := w[i].Color;
end;
if maxWeight > totalWeight div 20 then exit;
for i := 0 to high(w) do
if (w[i].Weight > maxWeight) and (BGRAToGSBA(w[i].Color).lightness < 56000) and (BGRAToGSBA(w[i].Color).lightness > 16000) then
begin
maxWeight:= w[i].Weight;
result := w[i].Color;
end;
if maxWeight > 0 then exit;
for i := 0 to high(w) do
if (w[i].Weight > maxWeight) then
begin
maxWeight:= w[i].Weight;
result := w[i].Color;
end;
end;
procedure TBGRACustomPalette.AssignTo(AImage: TFPCustomImage);
begin
AImage.UsePalette := true;
AssignTo(AImage.Palette);
end;
procedure TBGRACustomPalette.AssignTo(APalette: TFPPalette);
var i: integer;
begin
APalette.Clear;
APalette.Capacity := Count;
for i := 0 to Count-1 do
APalette.Color[i] := BGRAToFPColor(Color[i]);
end;
{ TBGRACustomApproxPalette }
function TBGRACustomApproxPalette.FindNearestColorIgnoreAlpha(AValue: TBGRAPixel): TBGRAPixel;
var saveAlpha: byte;
begin
if AValue.alpha = 0 then
result := BGRAPixelTransparent
else
begin
saveAlpha := AValue.alpha;
AValue.alpha := 255;
result := FindNearestColor(AValue);
result.alpha := saveAlpha;
end;
end;
function TBGRACustomApproxPalette.FindNearestColorIndexIgnoreAlpha(
AValue: TBGRAPixel): integer;
begin
if AValue.alpha = 0 then
result := -1
else
begin
AValue.alpha := 255;
result := FindNearestColorIndex(AValue);
end;
end;
function TBGRACustomApproxPalette.GetWeightByIndex(AIndex: Integer): UInt32;
begin
result := 1;
end;
function TBGRACustomApproxPalette.FindNearestColor(AValue: TBGRAPixel; AIgnoreAlpha: boolean): TBGRAPixel;
begin
if AIgnoreAlpha then
result := FindNearestColorIgnoreAlpha(AValue)
else
result := FindNearestColor(AValue);
end;
function TBGRACustomApproxPalette.FindNearestColorIndex(AValue: TBGRAPixel;
AIgnoreAlpha: boolean): integer;
begin
if AIgnoreAlpha then
result := FindNearestColorIndexIgnoreAlpha(AValue)
else
result := FindNearestColorIndex(AValue);
end;
{ TBGRAWeightedPalette }
function TBGRAWeightedPalette.GetWeightByIndex(AIndex: Integer): UInt32;
begin
NeedArray;
if (AIndex >= 0) and (AIndex < length(FArray)) then
result := PBGRAWeightedPaletteEntry(FArray[AIndex])^.Weight
else
raise ERangeError.Create('Index out of bounds');
end;
procedure TBGRAWeightedPalette.IncludePixel(PPixel: PBGRAPixel);
var dummy: UInt32;
begin
IncColor(PPixel^,dummy);
end;
constructor TBGRAWeightedPalette.Create(AColors: ArrayOfWeightedColor);
var
i: Integer;
begin
inherited Create;
for i := 0 to high(AColors) do
with AColors[i] do IncColor(Color,Weight);
end;
function TBGRAWeightedPalette.GetAsArrayOfWeightedColor: ArrayOfWeightedColor;
var
i: BGRANativeInt;
begin
NeedArray;
setlength(result, length(FArray));
for i := 0 to high(result) do
result[i] := PBGRAWeightedPaletteEntry(FArray[i])^;
end;
function TBGRAWeightedPalette.CreateEntry(AColor: TBGRAPixel): PBGRAPixel;
{$IFnDEF FPC}
var
BGRAWeightedPaletteEntry : PBGRAWeightedPaletteEntry;
{$ENDIF}
begin
{$IFDEF FPC}
result := PBGRAPixel(GetMem(sizeOf(TBGRAWeightedPaletteEntry)));
{$ELSE}
GetMem(BGRAWeightedPaletteEntry, sizeOf(TBGRAWeightedPaletteEntry));
result := PBGRAPixel(BGRAWeightedPaletteEntry);
{$ENDIF}
result^ := AColor;
PBGRAWeightedPaletteEntry(result)^.Weight := 1;
end;
procedure TBGRAWeightedPalette.FreeEntry(AEntry: PBGRAPixel);
begin
FreeMem(AEntry);
end;
function TBGRAWeightedPalette.IncColor(AValue: TBGRAPixel; out NewWeight: UInt32
): boolean;
Var Node: TAvgLvlTreeNode;
Entry: PBGRAPixel;
begin
Entry := GetLastColor(AValue);
if Entry <> nil then
begin
NewWeight := PBGRAWeightedPaletteEntry(Entry)^.Weight+1;
PBGRAWeightedPaletteEntry(Entry)^.Weight := NewWeight;
result := false;
exit;
end;
Node := FTree.Find(@AValue);
if Assigned(Node) then
begin
Entry := PBGRAPixel(Node.Data);
NewWeight := PBGRAWeightedPaletteEntry(Entry)^.Weight+1;
PBGRAWeightedPaletteEntry(Entry)^.Weight := NewWeight;
AddLastColor(Entry);
result := false;
end
else
begin
Entry := CreateEntry(AValue);
FTree.Add(Entry);
ClearArray;
NewWeight := PBGRAWeightedPaletteEntry(Entry)^.Weight;
AddLastColor(Entry);
result := true;
end;
end;
function TBGRAWeightedPalette.DecColor(AValue: TBGRAPixel; out NewWeight: UInt32
): boolean;
var
Node : TAvgLvlTreeNode;
Entry: PBGRAPixel;
begin
Node := FTree.Find(@AValue);
if Assigned(Node) then
begin
Entry := PBGRAPixel(Node.Data);
NewWeight := PBGRAWeightedPaletteEntry(Entry)^.Weight;
if NewWeight >= 2 then
begin
dec(NewWeight);
PBGRAWeightedPaletteEntry(Entry)^.Weight := NewWeight;
end
else
begin
NewWeight := 0;
FreeEntry(Entry);
FTree.Delete(Node);
ClearArray;
ClearLastColors;
end;
result := true;
end else
begin
result := false;
NewWeight := 0;
end;
end;
{ TBGRAReferencePalette }
procedure TBGRAReferencePalette.FreeEntry(AEntry: PBGRAPixel);
begin
//nothing
end;
function TBGRAReferencePalette.AddColor(AValue: PBGRAPixel): boolean;
begin
if Assigned(GetLastColor(AValue^)) then
begin
result := false;
exit;
end;
AddLastColor(AValue);
if Assigned(FTree.Find(AValue)) then
begin
result := false;
end
else
begin
result := true;
FTree.Add(AValue);
ClearArray;
end;
end;
function TBGRAReferencePalette.RemoveColor(AValue: PBGRAPixel): boolean;
var
Node : TAvgLvlTreeNode;
begin
Node := FTree.Find(AValue);
if Assigned(Node) then
begin
FTree.Delete(Node);
result := true;
ClearArray;
ClearLastColors;
end else
result := false;
end;
{ TBGRAAvgLvlPalette }