forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bgralayers.pas
2244 lines (1989 loc) · 72.5 KB
/
bgralayers.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 | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRALayers;
{$i bgrabitmap.inc}{$H+}{$H+}
{$IFDEF FPC}
{$MODESWITCH ADVANCEDRECORDS}
{$ENDIF}
interface
uses
Classes, SysUtils, Types, BGRATypes, BGRABitmapTypes, {$IFNDEF FPC}GraphType, SyncObjs, {$ENDIF} BGRABitmap, BGRAGraphics,
BGRAMemDirectory, BGRATransform, {$IFDEF OBJ}fgl{$ELSE}generics.collections{$ENDIF}, BGRALayerOriginal;
type
TBGRACustomLayeredBitmap = class;
TBGRACustomLayeredBitmapClass = class of TBGRACustomLayeredBitmap;
{ TBGRALayerOriginalEntry }
TBGRALayerOriginalEntry = record
Guid: TGuid;
Instance: TBGRALayerCustomOriginal;
class operator{$IFDEF OBJ}={$ELSE}Equal{$ENDIF}(const AEntry1,AEntry2: TBGRALayerOriginalEntry): boolean;
end;
TBGRALayerOriginalList = {$IFDEF OBJ}specialize TFPGList{$ELSE}TList{$ENDIF}<TBGRALayerOriginalEntry>;
TBGRALayeredBitmap = class;
TBGRALayeredBitmapClass = class of TBGRALayeredBitmap;
TBGRALayeredBitmapSaveToStreamProc = procedure(AStream: TStream; ALayers: TBGRACustomLayeredBitmap);
TBGRALayeredBitmapLoadFromStreamProc = procedure(AStream: TStream; ALayers: TBGRACustomLayeredBitmap);
TBGRALayeredBitmapCheckStreamProc = function(AStream: TStream): boolean;
{ TBGRACustomLayeredBitmap }
TBGRACustomLayeredBitmap = class(TGBRACustomGraphic)
private
FFrozenRange: array of record
firstLayer,lastLayer: integer;
image: TBGRABitmap;
linearBlend: boolean;
end;
FLinearBlend: boolean;
FMemDirectory: TMemDirectory;
FMemDirectoryOwned: boolean;
function GetDefaultBlendingOperation: TBlendOperation;
function GetHasMemFiles: boolean;
function GetLinearBlend: boolean;
procedure SetLinearBlend(AValue: boolean);
protected
function GetNbLayers: integer; virtual; abstract;
function GetMemDirectory: TMemDirectory;
function GetBlendOperation(Layer: integer): TBlendOperation; virtual; abstract;
function GetLayerVisible(layer: integer): boolean; virtual; abstract;
function GetLayerOpacity(layer: integer): byte; virtual; abstract;
function GetLayerName(layer: integer): string; virtual;
function GetLayerOffset(layer: integer): TPoint; virtual;
function GetLayerFrozenRange(layer: integer): integer;
function GetLayerFrozen(layer: integer): boolean; virtual;
function GetLayerUniqueId(layer: integer): integer; virtual;
function GetLayerOriginal({%H-}layer: integer): TBGRALayerCustomOriginal; virtual;
function GetLayerOriginalMatrix({%H-}layer: integer): TAffineMatrix; virtual;
function GetLayerOriginalGuid({%H-}layer: integer): TGuid; virtual;
function GetTransparent: Boolean; override;
function GetEmpty: boolean; override;
procedure SetWidth(Value: Integer); override;
procedure SetHeight(Value: Integer); override;
procedure SetMemDirectory(AValue: TMemDirectory);
procedure SetTransparent(Value: Boolean); override;
procedure SetLayerFrozen(layer: integer; AValue: boolean); virtual;
function RangeIntersect(first1,last1,first2,last2: integer): boolean;
procedure RemoveFrozenRange(index: integer);
function ContainsFrozenRange(first,last: integer): boolean;
public
procedure SaveToFile(const filenameUTF8: string); override;
procedure SaveToStream(Stream: TStream); override;
procedure SaveToStreamAs(Stream: TStream; AExtension: string);
constructor Create; override;
destructor Destroy; override;
function ToString: {$IFDEF FPC}ansistring{$ELSE}string{$ENDIF}; override;
function GetLayerBitmapDirectly(layer: integer): TBGRABitmap; virtual;
function GetLayerBitmapCopy(layer: integer): TBGRABitmap; virtual; abstract;
function ComputeFlatImage(ASeparateXorMask: boolean = false): TBGRABitmap; overload;
function ComputeFlatImage(firstLayer, lastLayer: integer; ASeparateXorMask: boolean = false): TBGRABitmap; overload;
function ComputeFlatImage(ARect: TRect; ASeparateXorMask: boolean = false): TBGRABitmap; overload;
function ComputeFlatImage(ARect: TRect; firstLayer, lastLayer: integer; ASeparateXorMask: boolean = false): TBGRABitmap; overload;
procedure Draw(ACanvas: TCanvas; const Rect: TRect); overload; override;
procedure Draw(Canvas: TCanvas; x,y: integer); overload;
procedure Draw(Canvas: TCanvas; x,y: integer; firstLayer, lastLayer: integer); overload;
procedure Draw(Dest: TBGRABitmap; x,y: integer); overload;
procedure Draw(Dest: TBGRABitmap; x,y: integer; ASeparateXorMask: boolean); overload;
procedure Draw(Dest: TBGRABitmap; AX,AY: integer; firstLayer, lastLayer: integer; ASeparateXorMask: boolean = false); overload;
procedure FreezeExceptOneLayer(layer: integer); overload;
procedure Freeze(firstLayer, lastLayer: integer); overload;
procedure Freeze; overload;
procedure Unfreeze; overload;
procedure Unfreeze(layer: integer); overload;
procedure Unfreeze(firstLayer, lastLayer: integer); overload;
procedure NotifyLoaded; virtual;
procedure NotifySaving; virtual;
property NbLayers: integer read GetNbLayers;
property BlendOperation[layer: integer]: TBlendOperation read GetBlendOperation;
property LayerVisible[layer: integer]: boolean read GetLayerVisible;
property LayerOpacity[layer: integer]: byte read GetLayerOpacity;
property LayerName[layer: integer]: string read GetLayerName;
property LayerOffset[layer: integer]: TPoint read GetLayerOffset;
property LayerFrozen[layer: integer]: boolean read GetLayerFrozen;
property LayerUniqueId[layer: integer]: integer read GetLayerUniqueId;
property LayerOriginal[layer: integer]: TBGRALayerCustomOriginal read GetLayerOriginal;
property LayerOriginalMatrix[layer: integer]: TAffineMatrix read GetLayerOriginalMatrix;
property LayerOriginalGuid[layer: integer]: TGuid read GetLayerOriginalGuid;
property LinearBlend: boolean read GetLinearBlend write SetLinearBlend; //use linear blending unless specified
property DefaultBlendingOperation: TBlendOperation read GetDefaultBlendingOperation;
property MemDirectory: TMemDirectory read GetMemDirectory write SetMemDirectory;
property MemDirectoryOwned: boolean read FMemDirectoryOwned write FMemDirectoryOwned;
property HasMemFiles: boolean read GetHasMemFiles;
end;
TOriginalRenderStatus = (orsNone, orsDraft, orsProof);
TOriginalChangeEvent = procedure (ASender: TObject; AOriginal: TBGRALayerCustomOriginal) of object;
TBGRALayerInfo = record
UniqueId: integer;
Name: string;
x, y: integer;
Source: TBGRABitmap;
blendOp: TBlendOperation;
Opacity: byte;
Visible: boolean;
Owner: boolean;
Frozen: boolean;
OriginalMatrix: TAffineMatrix;
OriginalRenderStatus: TOriginalRenderStatus;
OriginalGuid: TGuid;
end;
{ TBGRALayeredBitmap }
TBGRALayeredBitmap = class(TBGRACustomLayeredBitmap)
private
FNbLayers: integer;
FLayers: array of TBGRALayerInfo;
FOriginalChange: TOriginalChangeEvent;
FWidth,FHeight: integer;
FOriginals: TBGRALayerOriginalList;
FOriginalEditor: TBGRAOriginalEditor;
FOriginalEditorOriginal: TBGRALayerCustomOriginal;
protected
function GetWidth: integer; override;
function GetHeight: integer; override;
function GetNbLayers: integer; override;
function GetBlendOperation(Layer: integer): TBlendOperation; override;
function GetLayerVisible(layer: integer): boolean; override;
function GetLayerOpacity(layer: integer): byte; override;
function GetLayerOffset(layer: integer): TPoint; override;
function GetLayerName(layer: integer): string; override;
function GetLayerFrozen(layer: integer): boolean; override;
function GetLayerUniqueId(layer: integer): integer; override;
function GetLayerOriginal(layer: integer): TBGRALayerCustomOriginal; override;
function GetLayerOriginalMatrix(layer: integer): TAffineMatrix; override;
function GetLayerOriginalGuid(layer: integer): TGuid; override;
function GetLayerOriginalRenderStatus(layer: integer): TOriginalRenderStatus;
function GetOriginalCount: integer;
function GetOriginalByIndex(AIndex: integer): TBGRALayerCustomOriginal;
procedure SetBlendOperation(Layer: integer; op: TBlendOperation);
procedure SetLayerVisible(layer: integer; AValue: boolean);
procedure SetLayerOpacity(layer: integer; AValue: byte);
procedure SetLayerOffset(layer: integer; AValue: TPoint);
procedure SetLayerName(layer: integer; AValue: string);
procedure SetLayerFrozen(layer: integer; AValue: boolean); override;
procedure SetLayerUniqueId(layer: integer; AValue: integer);
procedure SetLayerOriginalMatrix(layer: integer; AValue: TAffineMatrix);
procedure SetLayerOriginalGuid(layer: integer; const AValue: TGuid);
procedure SetLayerOriginalRenderStatus(layer: integer; AValue: TOriginalRenderStatus);
procedure FindOriginal(AGuid: TGuid;
out ADir: TMemDirectory;
out AClass: TBGRALayerOriginalAny);
procedure StoreOriginal(AOriginal: TBGRALayerCustomOriginal);
procedure OriginalChange(ASender: TObject);
public
procedure LoadFromFile(const filenameUTF8: string); override;
procedure LoadFromStream(stream: TStream); override;
procedure SetSize(AWidth, AHeight: integer); virtual;
procedure Clear; override;
procedure ClearOriginals;
procedure RemoveLayer(index: integer);
procedure InsertLayer(index: integer; fromIndex: integer);
procedure Assign(ASource: TBGRACustomLayeredBitmap; ASharedLayerIds: boolean = false); overload;
function MoveLayerUp(index: integer): integer;
function MoveLayerDown(index: integer): integer;
function AddLayer(Source: TBGRABitmap; Opacity: byte = 255): integer; overload;
function AddLayer(Source: TBGRABitmap; Position: TPoint; BlendOp: TBlendOperation; Opacity: byte = 255; Shared: boolean = false): integer; overload;
function AddLayer(Source: TBGRABitmap; Position: TPoint; Opacity: byte = 255): integer; overload;
function AddLayer(Source: TBGRABitmap; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddLayer(AName: string; Source: TBGRABitmap; Opacity: byte = 255): integer; overload;
function AddLayer(AName: string; Source: TBGRABitmap; Position: TPoint; BlendOp: TBlendOperation; Opacity: byte = 255; Shared: boolean = false): integer; overload;
function AddLayer(AName: string; Source: TBGRABitmap; Position: TPoint; Opacity: byte = 255): integer; overload;
function AddLayer(AName: string; Source: TBGRABitmap; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddSharedLayer(Source: TBGRABitmap; Opacity: byte = 255): integer; overload;
function AddSharedLayer(Source: TBGRABitmap; Position: TPoint; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddSharedLayer(Source: TBGRABitmap; Position: TPoint; Opacity: byte = 255): integer; overload;
function AddSharedLayer(Source: TBGRABitmap; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddLayerFromFile(AFileName: string; Opacity: byte = 255): integer; overload;
function AddLayerFromFile(AFileName: string; Position: TPoint; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddLayerFromFile(AFileName: string; Position: TPoint; Opacity: byte = 255): integer; overload;
function AddLayerFromFile(AFileName: string; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddOwnedLayer(ABitmap: TBGRABitmap; Opacity: byte = 255): integer; overload;
function AddOwnedLayer(ABitmap: TBGRABitmap; Position: TPoint; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddOwnedLayer(ABitmap: TBGRABitmap; Position: TPoint; Opacity: byte = 255): integer; overload;
function AddOwnedLayer(ABitmap: TBGRABitmap; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddLayerFromOriginal(AGuid: TGuid; Opacity: byte = 255): integer; overload;
function AddLayerFromOriginal(AGuid: TGuid; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddLayerFromOriginal(AGuid: TGuid; Matrix: TAffineMatrix; Opacity: byte = 255): integer; overload;
function AddLayerFromOriginal(AGuid: TGuid; Matrix: TAffineMatrix; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddLayerFromOwnedOriginal(AOriginal: TBGRALayerCustomOriginal; Opacity: byte = 255): integer; overload;
function AddLayerFromOwnedOriginal(AOriginal: TBGRALayerCustomOriginal; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddLayerFromOwnedOriginal(AOriginal: TBGRALayerCustomOriginal; Matrix: TAffineMatrix; Opacity: byte = 255): integer; overload;
function AddLayerFromOwnedOriginal(AOriginal: TBGRALayerCustomOriginal; Matrix: TAffineMatrix; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload;
function AddOriginal(AOriginal: TBGRALayerCustomOriginal; AOwned: boolean = true): integer;
function RemoveOriginal(AOriginal: TBGRALayerCustomOriginal): boolean;
procedure DeleteOriginal(AIndex: integer);
procedure NotifyLoaded; override;
procedure NotifySaving; override;
procedure RenderLayerFromOriginal(layer: integer; ADraft: boolean = false);
procedure RenderOriginalsIfNecessary(ADraft: boolean = false);
procedure RemoveUnusedOriginals;
destructor Destroy; override;
constructor Create; overload; override;
constructor Create(AWidth, AHeight: integer); overload; virtual;
function GetLayerBitmapDirectly(layer: integer): TBGRABitmap; override;
function GetLayerBitmapCopy(layer: integer): TBGRABitmap; override;
function GetLayerIndexFromId(AIdentifier: integer): integer;
function Duplicate(ASharedLayerIds: boolean = false): TBGRALayeredBitmap;
function ProduceLayerUniqueId: integer;
procedure RotateCW;
procedure RotateCCW;
procedure HorizontalFlip;
procedure VerticalFlip;
procedure Resample(AWidth, AHeight: integer; AResampleMode: TResampleMode; AFineResampleFilter: TResampleFilter = rfLinear);
procedure SetLayerBitmap(layer: integer; ABitmap: TBGRABitmap; AOwned: boolean);
function DrawEditor(ADest: TBGRABitmap; ALayerIndex: integer; X, Y: Integer; APointSize: single): TRect; overload;
function DrawEditor(ADest: TBGRABitmap; ALayerIndex: integer; AMatrix: TAffineMatrix; APointSize: single): TRect; overload;
procedure MouseMove(Shift: TShiftState; X, Y: Single; out ACursor: TOriginalEditorCursor);
procedure MouseDown(RightButton: boolean; Shift: TShiftState; X, Y: Single; out ACursor: TOriginalEditorCursor);
procedure MouseUp(RightButton: boolean; Shift: TShiftState; X, Y: Single; out ACursor: TOriginalEditorCursor);
property Width : integer read GetWidth;
property Height: integer read GetHeight;
property NbLayers: integer read GetNbLayers;
property BlendOperation[layer: integer]: TBlendOperation read GetBlendOperation write SetBlendOperation;
property LayerVisible[layer: integer]: boolean read GetLayerVisible write SetLayerVisible;
property LayerOpacity[layer: integer]: byte read GetLayerOpacity write SetLayerOpacity;
property LayerName[layer: integer]: string read GetLayerName write SetLayerName;
property LayerBitmap[layer: integer]: TBGRABitmap read GetLayerBitmapDirectly;
property LayerOffset[layer: integer]: TPoint read GetLayerOffset write SetLayerOffset;
property LayerUniqueId[layer: integer]: integer read GetLayerUniqueId write SetLayerUniqueId;
property LayerOriginal[layer: integer]: TBGRALayerCustomOriginal read GetLayerOriginal;
property LayerOriginalGuid[layer: integer]: TGuid read GetLayerOriginalGuid write SetLayerOriginalGuid;
property LayerOriginalMatrix[layer: integer]: TAffineMatrix read GetLayerOriginalMatrix write SetLayerOriginalMatrix;
property LayerOriginalRenderStatus[layer: integer]: TOriginalRenderStatus read GetLayerOriginalRenderStatus write SetLayerOriginalRenderStatus;
function IndexOfOriginal(AGuid: TGuid): integer; overload;
function IndexOfOriginal(AOriginal: TBGRALayerCustomOriginal): integer; overload;
property OriginalCount: integer read GetOriginalCount;
property Original[AIndex: integer]: TBGRALayerCustomOriginal read GetOriginalByIndex;
property OnOriginalChange: TOriginalChangeEvent read FOriginalChange write FOriginalChange;
end;
TAffineMatrix = BGRABitmapTypes.TAffineMatrix;
function BGRALayerOriginalEntry(AGuid: TGuid): TBGRALayerOriginalEntry; overload;
function BGRALayerOriginalEntry(AInstance: TBGRALayerCustomOriginal): TBGRALayerOriginalEntry; overload;
procedure RegisterLayeredBitmapWriter(AExtensionUTF8: string; AWriter: TBGRALayeredBitmapClass);
procedure RegisterLayeredBitmapReader(AExtensionUTF8: string; AReader: TBGRACustomLayeredBitmapClass);
function TryCreateLayeredBitmapWriter(AExtensionUTF8: string): TBGRALayeredBitmap;
function TryCreateLayeredBitmapReader(AExtensionUTF8: string): TBGRACustomLayeredBitmap;
var
LayeredBitmapSaveToStreamProc : TBGRALayeredBitmapSaveToStreamProc;
LayeredBitmapLoadFromStreamProc : TBGRALayeredBitmapLoadFromStreamProc;
LayeredBitmapCheckStreamProc: TBGRALayeredBitmapCheckStreamProc;
type
TOnLayeredBitmapLoadStartProc = procedure(AFilenameUTF8: string) of object;
TOnLayeredBitmapLoadProgressProc = procedure(APercentage: integer) of object;
TOnLayeredBitmapLoadedProc = procedure() of object;
procedure OnLayeredBitmapLoadFromStreamStart;
procedure OnLayeredBitmapLoadStart(AFilenameUTF8: string);
procedure OnLayeredBitmapLoadProgress(APercentage: integer);
procedure OnLayeredBitmapLoaded();
procedure RegisterLoadingHandler(AStart: TOnLayeredBitmapLoadStartProc; AProgress: TOnLayeredBitmapLoadProgressProc;
ADone: TOnLayeredBitmapLoadedProc);
procedure UnregisterLoadingHandler(AStart: TOnLayeredBitmapLoadStartProc; AProgress: TOnLayeredBitmapLoadProgressProc;
ADone: TOnLayeredBitmapLoadedProc);
function Equal(const AGuid1, AGuid2: TGuid): boolean;
implementation
uses BGRAUTF8, BGRABlend, BGRAMultiFileType;
const
OriginalsDirectory = 'originals';
var
OnLayeredBitmapLoadStartProc: TOnLayeredBitmapLoadStartProc;
OnLayeredBitmapLoadProgressProc: TOnLayeredBitmapLoadProgressProc;
OnLayeredBitmapLoadedProc: TOnLayeredBitmapLoadedProc;
var
NextLayerUniqueId: {$IFDEF FPC}cardinal;{$ELSE}Int64;{$ENDIF}
LayeredBitmapReaders: array of record
extension: string;
theClass: TBGRACustomLayeredBitmapClass;
end;
LayeredBitmapWriters: array of record
extension: string;
theClass: TBGRALayeredBitmapClass;
end;
function Equal(const AGuid1, AGuid2: TGuid): boolean;
begin
result := CompareMem(@AGuid1, @AGuid2, sizeof(TGuid));
end;
{ TBGRALayerOriginalEntry }
class operator TBGRALayerOriginalEntry.{$IFDEF OBJ}={$ELSE}Equal{$ENDIF}(const AEntry1,
AEntry2: TBGRALayerOriginalEntry): boolean;
begin
result := Equal(AEntry1.Guid, AEntry2.Guid);
end;
function BGRALayerOriginalEntry(AGuid: TGuid): TBGRALayerOriginalEntry;
begin
result.Guid := AGuid;
result.Instance := nil;
end;
function BGRALayerOriginalEntry(AInstance: TBGRALayerCustomOriginal): TBGRALayerOriginalEntry;
begin
result.Guid := AInstance.Guid;
result.Instance := AInstance;
end;
{ TBGRALayeredBitmap }
function TBGRALayeredBitmap.GetLayerUniqueId(layer: integer): integer;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
Result:= FLayers[layer].UniqueId;
end;
function TBGRALayeredBitmap.GetLayerOriginal(layer: integer): TBGRALayerCustomOriginal;
var
idxOrig: Integer;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
if Equal(FLayers[layer].OriginalGuid, {=} GUID_NULL) then exit(nil);
idxOrig := IndexOfOriginal(FLayers[layer].OriginalGuid);
if idxOrig = -1 then exit(nil);
result := Original[idxOrig];
end;
end;
function TBGRALayeredBitmap.GetLayerOriginalMatrix(layer: integer
): TAffineMatrix;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
result := FLayers[layer].OriginalMatrix;
end;
function TBGRALayeredBitmap.GetLayerOriginalGuid(layer: integer): TGuid;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
result := FLayers[layer].OriginalGuid;
end;
function TBGRALayeredBitmap.GetLayerOriginalRenderStatus(layer: integer
): TOriginalRenderStatus;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
result := FLayers[layer].OriginalRenderStatus;
end;
procedure TBGRALayeredBitmap.SetLayerUniqueId(layer: integer; AValue: integer);
var i: integer;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
for i := 0 to NbLayers-1 do
if (i <> layer) and (FLayers[layer].UniqueId = AValue) then
raise Exception.Create('Another layer has the same identifier');
FLayers[layer].UniqueId := AValue;
end;
end;
procedure TBGRALayeredBitmap.SetLayerOriginalMatrix(layer: integer;
AValue: TAffineMatrix);
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
if AffMatrixEqual(FLayers[layer].OriginalMatrix, {=} AValue) then exit;
FLayers[layer].OriginalMatrix := AValue;
if not Equal(FLayers[layer].OriginalGuid, {<>} GUID_NULL) then
begin
FLayers[layer].OriginalRenderStatus := orsNone;
Unfreeze(layer);
end;
end;
end;
procedure TBGRALayeredBitmap.SetLayerOriginalGuid(layer: integer;
const AValue: TGuid);
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
if Equal(FLayers[layer].OriginalGuid, {=} AValue) then exit;
FLayers[layer].OriginalGuid := AValue;
if not Equal(AValue, {<>} GUID_NULL) and (IndexOfOriginal(AValue) <> -1) then
begin
FLayers[layer].OriginalRenderStatus := orsNone;
Unfreeze(layer);
end;
end;
end;
procedure TBGRALayeredBitmap.SetLayerOriginalRenderStatus(layer: integer;
AValue: TOriginalRenderStatus);
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
if FLayers[layer].OriginalRenderStatus = AValue then exit;
FLayers[layer].OriginalRenderStatus := AValue;
Unfreeze(layer);
end;
end;
procedure TBGRALayeredBitmap.FindOriginal(AGuid: TGuid; out
ADir: TMemDirectory; out AClass: TBGRALayerOriginalAny);
var
c: String;
begin
ADir := nil;
AClass := nil;
if HasMemFiles then
begin
ADir := MemDirectory.FindPath(OriginalsDirectory+'/'+GUIDToString(AGuid));
if ADir <> nil then
begin
c := ADir.RawStringByFilename['class'];
AClass := FindLayerOriginalClass(c);
end;
end;
end;
procedure TBGRALayeredBitmap.StoreOriginal(AOriginal: TBGRALayerCustomOriginal);
var
dir, subdir: TMemDirectory;
storage: TBGRAMemOriginalStorage;
begin
dir := MemDirectory.Directory[MemDirectory.AddDirectory(OriginalsDirectory)];
subdir := dir.Directory[dir.AddDirectory(GUIDToString(AOriginal.Guid))];
storage := TBGRAMemOriginalStorage.Create(subdir);
try
AOriginal.SaveToStorage(storage);
storage.RawString['class'] := AOriginal.StorageClassName;
finally
storage.Free;
end;
end;
procedure TBGRALayeredBitmap.OriginalChange(ASender: TObject);
var
i: Integer;
orig: TBGRALayerCustomOriginal;
begin
orig := TBGRALayerCustomOriginal(ASender);
for i := 0 to NbLayers-1 do
if Equal(LayerOriginalGuid[i], {=} orig.Guid) then
LayerOriginalRenderStatus[i] := orsNone;
if Assigned(FOriginalChange) then
FOriginalChange(self, orig);
end;
function TBGRALayeredBitmap.GetOriginalCount: integer;
begin
if Assigned(FOriginals) then
result := FOriginals.Count;
end;
function TBGRALayeredBitmap.GetOriginalByIndex(AIndex: integer
): TBGRALayerCustomOriginal;
var
dir: TMemDirectory;
c: TBGRALayerOriginalAny;
guid: TGuid;
storage: TBGRAMemOriginalStorage;
begin
if (AIndex < 0) or (AIndex >= OriginalCount) then
raise ERangeError.Create('Index out of bounds');
result := FOriginals[AIndex].Instance;
guid := FOriginals[AIndex].Guid;
// load original on the fly
if (result = nil) and not Equal(guid, {<>} GUID_NULL) then
begin
FindOriginal(guid, dir, c);
if not Assigned(dir) then
raise exception.Create('Original directory not found');
if not Assigned(c) then
raise exception.Create('Original class not found (it can be registered with the RegisterLayerOriginal function)');
result := c.Create;
result.Guid := guid;
storage := TBGRAMemOriginalStorage.Create(dir);
try
result.LoadFromStorage(storage);
finally
storage.Free;
end;
FOriginals[AIndex] := BGRALayerOriginalEntry(result);
result.OnChange:= {$IFDEF OBJ}@{$ENDIF}OriginalChange;
end;
end;
function TBGRALayeredBitmap.GetWidth: integer;
begin
Result:= FWidth;
end;
function TBGRALayeredBitmap.GetHeight: integer;
begin
Result:= FHeight;
end;
function TBGRALayeredBitmap.GetNbLayers: integer;
begin
Result:= FNbLayers;
end;
function TBGRALayeredBitmap.GetBlendOperation(Layer: integer): TBlendOperation;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
Result:= FLayers[layer].blendOp;
end;
function TBGRALayeredBitmap.GetLayerVisible(layer: integer): boolean;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
Result:= FLayers[layer].Visible;
end;
function TBGRALayeredBitmap.GetLayerOpacity(layer: integer): byte;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
Result:= FLayers[layer].Opacity;
end;
function TBGRALayeredBitmap.GetLayerOffset(layer: integer): TPoint;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
with FLayers[layer] do
Result:= Point(x,y);
end;
function TBGRALayeredBitmap.GetLayerName(layer: integer): string;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
if not FLayers[layer].Owner and (FLayers[layer].Source <> nil) then
Result := FLayers[layer].Source.Caption
else
Result:= FLayers[layer].Name;
if Result = '' then
result := inherited GetLayerName(layer);
end;
end;
function TBGRALayeredBitmap.GetLayerFrozen(layer: integer): boolean;
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
Result:= FLayers[layer].Frozen;
end;
procedure TBGRALayeredBitmap.SetBlendOperation(Layer: integer;
op: TBlendOperation);
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
if FLayers[layer].blendOp <> op then
begin
FLayers[layer].blendOp := op;
Unfreeze(layer);
end;
end;
end;
procedure TBGRALayeredBitmap.SetLayerVisible(layer: integer; AValue: boolean);
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
if FLayers[layer].Visible <> AValue then
begin
FLayers[layer].Visible := AValue;
Unfreeze(layer);
end;
end;
end;
procedure TBGRALayeredBitmap.SetLayerOpacity(layer: integer; AValue: byte);
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
if FLayers[layer].Opacity <> AValue then
begin
FLayers[layer].Opacity := AValue;
Unfreeze(layer);
end;
end;
end;
procedure TBGRALayeredBitmap.SetLayerOffset(layer: integer; AValue: TPoint);
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
if (FLayers[layer].x <> AValue.x) or
(FLayers[layer].y <> AValue.y) then
begin
if not Equal(FLayers[layer].OriginalGuid, {<>} GUID_NULL) then
raise exception.Create('The offset of the layer is computed from an original. You can change it by changing the layer original matrix.');
FLayers[layer].x := AValue.x;
FLayers[layer].y := AValue.y;
Unfreeze(layer);
end;
end;
end;
procedure TBGRALayeredBitmap.SetLayerName(layer: integer; AValue: string);
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
begin
if not FLayers[layer].Owner and (FLayers[layer].Source <> nil) then
FLayers[layer].Source.Caption := AValue
else
FLayers[layer].Name := AValue;
end;
end;
procedure TBGRALayeredBitmap.SetLayerFrozen(layer: integer; AValue: boolean);
begin
if (layer < 0) or (layer >= NbLayers) then
raise Exception.Create('Index out of bounds')
else
FLayers[layer].Frozen := AValue;
end;
function TBGRALayeredBitmap.GetLayerBitmapDirectly(layer: integer): TBGRABitmap;
begin
if (layer < 0) or (layer >= NbLayers) then
result := nil
else
begin
if FLayers[layer].OriginalRenderStatus = orsNone then
RenderLayerFromOriginal(layer, true);
Result:= FLayers[layer].Source;
end;
end;
procedure TBGRALayeredBitmap.LoadFromFile(const filenameUTF8: string);
var bmp: TBGRABitmap;
ext: string;
temp: TBGRACustomLayeredBitmap;
i: integer;
stream: TFileStreamUTF8;
begin
ext := UTF8LowerCase(ExtractFileExt(filenameUTF8));
for i := 0 to high(LayeredBitmapReaders) do
if '.'+LayeredBitmapReaders[i].extension = ext then
begin
temp := LayeredBitmapReaders[i].theClass.Create;
try
temp.LoadFromFile(filenameUTF8);
Assign(temp);
finally
temp.Free;
end;
exit;
end;
//when using "data" extension, simply deserialize
if (ext='.dat') or (ext='.data') then
begin
if Assigned(LayeredBitmapLoadFromStreamProc) then
begin
stream := TFileStreamUTF8.Create(filenameUTF8, fmOpenRead, fmShareDenyWrite);
try
LayeredBitmapLoadFromStreamProc(stream, self);
finally
stream.Free;
end;
end else
raise exception.Create('Enable layer deserialization by calling BGRAStreamLayers.RegisterStreamLayers');
end else
begin
bmp := TBGRABitmap.Create(filenameUTF8, True);
Clear;
SetSize(bmp.Width,bmp.Height);
AddOwnedLayer(bmp);
end;
end;
procedure TBGRALayeredBitmap.LoadFromStream(stream: TStream);
var bmp: TBGRABitmap;
begin
if Assigned(LayeredBitmapLoadFromStreamProc) then
begin
if not Assigned(LayeredBitmapCheckStreamProc) or
LayeredBitmapCheckStreamProc(stream) then
begin
LayeredBitmapLoadFromStreamProc(Stream, self);
exit;
end;
end;
bmp := TBGRABitmap.Create(stream);
Clear;
SetSize(bmp.Width,bmp.Height);
AddOwnedLayer(bmp);
end;
procedure TBGRALayeredBitmap.SetSize(AWidth, AHeight: integer);
begin
Unfreeze;
FWidth := AWidth;
FHeight := AHeight;
end;
procedure TBGRALayeredBitmap.Clear;
var i: integer;
begin
Unfreeze;
for i := NbLayers-1 downto 0 do
RemoveLayer(i);
MemDirectory := nil;
ClearOriginals;
end;
procedure TBGRALayeredBitmap.ClearOriginals;
var
i: Integer;
begin
if Assigned(FOriginals) then
begin
for i := 0 to OriginalCount-1 do
FOriginals[i].Instance.Free;
FreeAndNil(FOriginals);
end;
end;
procedure TBGRALayeredBitmap.RemoveLayer(index: integer);
var i: integer;
begin
if (index < 0) or (index >= NbLayers) then exit;
Unfreeze;
if FLayers[index].Owner then FLayers[index].Source.Free;
for i := index to FNbLayers-2 do
FLayers[i] := FLayers[i+1];
Dec(FNbLayers);
end;
procedure TBGRALayeredBitmap.InsertLayer(index: integer; fromIndex: integer);
var info: TBGRALayerInfo;
i: integer;
begin
if (index < 0) or (index > NbLayers) or (index = fromIndex) then exit;
if (fromIndex < 0) or (fromIndex >= NbLayers) then exit;
Unfreeze;
info := FLayers[fromIndex];
for i := fromIndex to FNbLayers-2 do
FLayers[i] := FLayers[i+1];
for i := FNbLayers-1 downto index+1 do
FLayers[i] := FLayers[i-1];
FLayers[index] := info;
end;
procedure TBGRALayeredBitmap.Assign(ASource: TBGRACustomLayeredBitmap; ASharedLayerIds: boolean);
var i,idx: integer;
begin
Clear;
SetSize(ASource.Width,ASource.Height);
LinearBlend:= ASource.LinearBlend;
for i := 0 to ASource.NbLayers-1 do
begin
idx := AddOwnedLayer(ASource.GetLayerBitmapCopy(i),ASource.LayerOffset[i],ASource.BlendOperation[i],ASource.LayerOpacity[i]);
LayerName[idx] := ASource.LayerName[i];
LayerVisible[idx] := ASource.LayerVisible[i];
if ASharedLayerIds and (ASource is TBGRALayeredBitmap) then
LayerUniqueId[idx] := TBGRALayeredBitmap(ASource).LayerUniqueId[idx];
end;
end;
function TBGRALayeredBitmap.MoveLayerUp(index: integer): integer;
begin
if (index >= 0) and (index <= NbLayers-2) then
begin
InsertLayer(index+1,index);
result := index+1;
end else
result := -1;
end;
function TBGRALayeredBitmap.MoveLayerDown(index: integer): integer;
begin
if (index > 0) and (index <= NbLayers-1) then
begin
InsertLayer(index-1,index);
result := index-1;
end else
result := -1;
end;
function TBGRALayeredBitmap.AddLayer(Source: TBGRABitmap; Opacity: byte
): integer;
begin
result := AddLayer(Source, Point(0,0), DefaultBlendingOperation, Opacity, False);
end;
function TBGRALayeredBitmap.AddLayer(Source: TBGRABitmap; Position: TPoint;
BlendOp: TBlendOperation; Opacity: byte; Shared: boolean): integer;
begin
result := AddLayer(Source.Caption,Source,Position,BlendOp,Opacity,Shared);
end;
function TBGRALayeredBitmap.AddLayer(Source: TBGRABitmap; Position: TPoint;
Opacity: byte): integer;
begin
result := AddLayer(Source,Position,DefaultBlendingOperation,Opacity);
end;
function TBGRALayeredBitmap.AddLayer(Source: TBGRABitmap;
BlendOp: TBlendOperation; Opacity: byte): integer;
begin
result := AddLayer(Source,Point(0,0),BlendOp,Opacity);
end;
function TBGRALayeredBitmap.AddLayer(AName: string; Source: TBGRABitmap;
Opacity: byte): integer;
begin
result := AddLayer(AName,Source,Point(0,0),Opacity);
end;
function TBGRALayeredBitmap.AddLayer(AName: string; Source: TBGRABitmap;
Position: TPoint; BlendOp: TBlendOperation; Opacity: byte; Shared: boolean): integer;
begin
if length(FLayers) = FNbLayers then
setlength(FLayers, length(FLayers)*2+1);
FLayers[FNbLayers].Name := AName;
FLayers[FNbLayers].X := Position.X;
FLayers[FNbLayers].Y := Position.Y;
FLayers[FNbLayers].blendOp := BlendOp;
FLayers[FNbLayers].Opacity := Opacity;
FLayers[FNbLayers].Visible := true;
FLayers[FNbLayers].Frozen := false;
FLayers[FNbLayers].UniqueId := ProduceLayerUniqueId;
FLayers[FNbLayers].OriginalMatrix := AffineMatrixIdentity;
FLayers[FNbLayers].OriginalRenderStatus := orsNone;
FLayers[FNbLayers].OriginalGuid := GUID_NULL;
if Shared then
begin
FLayers[FNbLayers].Source := Source;
FLayers[FNbLayers].Owner := false;
end else
begin
FLayers[FNbLayers].Source := Source.Duplicate as TBGRABitmap;
FLayers[FNbLayers].Owner := true;
end;
result := FNbLayers;
inc(FNbLayers);
if (FNbLayers = 1) and (FWidth = 0) and (FHeight = 0) and (Source <> nil) then
SetSize(Source.Width,Source.Height);
end;
function TBGRALayeredBitmap.AddLayer(AName: string; Source: TBGRABitmap;
Position: TPoint; Opacity: byte): integer;
begin
result := AddLayer(AName, Source, Position, DefaultBlendingOperation, Opacity);
end;
function TBGRALayeredBitmap.AddLayer(AName: string; Source: TBGRABitmap;
BlendOp: TBlendOperation; Opacity: byte): integer;
begin
result := AddLayer(AName, Source, Point(0,0), blendOp, Opacity);
end;
function TBGRALayeredBitmap.AddSharedLayer(Source: TBGRABitmap; Opacity: byte
): integer;
begin